more krazy fixes (include own header first, and missing e-mail addresses
[kdeaccessibility.git] / kttsd / kttsjobmgr / jobinfolistmodel.cpp
blob92280097231ec026d49ba8f89f1aa616b1f5609f
1 /***************************************************** vim:set ts=4 sw=4 sts=4:
2 Model for listing Jobs, typically in a QTreeView.
3 -------------------
4 Copyright : (C) 2006 by Gary Cramblitt <garycramblitt@comcast.net>
5 -------------------
6 Original author: Gary Cramblitt <garycramblitt@comcast.net>
7 Current Maintainer: Gary Cramblitt <garycramblitt@comcast.net>
8 ******************************************************************************/
10 /******************************************************************************
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 *******************************************************************************/
26 // JobInfoListModel includes.
27 #include "jobinfolistmodel.h"
28 #include "jobinfolistmodel.moc"
30 // Qt includes.
32 // KDE includes.
33 #include <klocale.h>
34 #include <kdebug.h>
35 #include <kspeech.h>
37 // KTTS includes.
40 // ----------------------------------------------------------------------------
42 JobInfoListModel::JobInfoListModel(JobInfoList jobs, QObject *parent) :
43 QAbstractListModel(parent),
44 m_jobs(jobs)
48 int JobInfoListModel::rowCount(const QModelIndex &parent) const
50 if (!parent.isValid())
51 return m_jobs.count();
52 else
53 return 0;
56 int JobInfoListModel::columnCount(const QModelIndex &parent) const
58 Q_UNUSED(parent);
59 return 7;
62 QModelIndex JobInfoListModel::index(int row, int column, const QModelIndex &parent) const
64 if (!parent.isValid())
65 return createIndex(row, column, 0);
66 else
67 return QModelIndex();
70 QModelIndex JobInfoListModel::parent(const QModelIndex & index ) const
72 Q_UNUSED(index);
73 return QModelIndex();
76 QVariant JobInfoListModel::data(const QModelIndex &index, int role) const
78 if (!index.isValid())
79 return QVariant();
81 if (index.row() < 0 || index.row() >= m_jobs.count())
82 return QVariant();
84 if (index.column() < 0 || index.column() >= 8)
85 return QVariant();
87 if (role == Qt::DisplayRole)
88 return dataColumn(m_jobs.at(index.row()), index.column());
89 else
90 return QVariant();
93 QVariant JobInfoListModel::dataColumn(const JobInfo& job, int column) const
95 switch (column)
97 case 0: return job.jobNum; break;
98 case 1: return job.applicationName; break;
99 case 2: return priorityToStr(job.priority); break;
100 case 3: return job.talkerID; break;
101 case 4: return stateToStr(job.state); break;
102 case 5: return job.sentenceNum; break;
103 case 6: return job.sentenceCount; break;
105 return QVariant();
108 Qt::ItemFlags JobInfoListModel::flags(const QModelIndex &index) const
110 if (!index.isValid())
111 return Qt::ItemIsEnabled;
113 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
116 QVariant JobInfoListModel::headerData(int section, Qt::Orientation orientation, int role) const
118 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
119 switch (section)
121 case 0: return i18n("Job Num");
122 case 1: return i18n("Owner");
123 case 2: return i18n("Priority");
124 case 3: return i18n("Talker ID");
125 case 4: return i18n("State");
126 case 5: return i18n("Position");
127 case 6: return i18n("Sentences");
130 return QVariant();
133 bool JobInfoListModel::removeRow(int row, const QModelIndex & parent)
135 beginRemoveRows(parent, row, row);
136 m_jobs.removeAt(row);
137 endRemoveRows();
138 return true;
141 void JobInfoListModel::setDatastore(JobInfoList jobs)
143 m_jobs = jobs;
144 emit reset();
147 JobInfo JobInfoListModel::getRow(int row) const
149 if (row < 0 || row >= rowCount()) return JobInfo();
150 return m_jobs[row];
153 bool JobInfoListModel::appendRow(JobInfo& job)
155 beginInsertRows(QModelIndex(), m_jobs.count(), m_jobs.count());
156 m_jobs.append(job);
157 endInsertRows();
158 return true;
161 bool JobInfoListModel::updateRow(int row, JobInfo& job)
163 m_jobs.replace(row, job);
164 emit dataChanged(index(row, 0, QModelIndex()), index(row, columnCount()-1, QModelIndex()));
165 return true;
168 bool JobInfoListModel::swap(int i, int j)
170 m_jobs.swap(i, j);
171 emit dataChanged(index(i, 0, QModelIndex()), index(j, columnCount()-1, QModelIndex()));
172 return true;
175 void JobInfoListModel::clear()
177 m_jobs.clear();
178 emit reset();
181 QModelIndex JobInfoListModel::jobNumToIndex(int jobNum)
183 for (int row = 0; row < m_jobs.count(); ++row)
184 if (getRow(row).jobNum == jobNum)
185 return createIndex(row, 0);
186 return QModelIndex();
190 * Convert a KTTSD job state integer into a display string.
191 * @param state KTTSD job state
192 * @return Display string for the state.
194 QString JobInfoListModel::stateToStr(int state) const
196 switch( state )
198 case KSpeech::jsQueued: return i18n("Queued");
199 case KSpeech::jsFiltering: return i18n("Filtering");
200 case KSpeech::jsSpeakable: return i18n("Waiting");
201 case KSpeech::jsSpeaking: return i18n("Speaking");
202 case KSpeech::jsPaused: return i18n("Paused");
203 case KSpeech::jsInterrupted: return i18n("Interrupted");
204 case KSpeech::jsFinished: return i18n("Finished");
205 default: return i18n("Unknown");
210 * Convert a KTTSD job priority into a display string.
211 * @param priority KTTSD job priority.
212 * @return Display string for priority.
214 QString JobInfoListModel::priorityToStr(int priority) const
216 switch (priority)
218 case KSpeech::jpAll: return i18n("All");
219 case KSpeech::jpScreenReaderOutput: return i18n("Screen Reader");
220 case KSpeech::jpWarning: return i18n("Warning");
221 case KSpeech::jpMessage: return i18n("Message");
222 case KSpeech::jpText: return i18n("Text");
223 default: return i18n("Unknown");