Fix include
[kdeaccessibility.git] / kttsd / libkttsd / talkerlistmodel.cpp
blob7d390b698dd468c84566f703866d0890a06f82ba
1 /***************************************************** vim:set ts=4 sw=4 sts=4:
2 Widget for listing Talkers. Based on QTreeView.
3 -------------------
4 Copyright : (C) 2005 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 // Qt includes.
28 // KDE includes.
29 #include "klocale.h"
30 #include "kconfig.h"
31 #include "kdebug.h"
33 // TalkerListWidget includes.
34 #include "talkerlistmodel.h"
35 #include "talkerlistmodel.moc"
37 // ----------------------------------------------------------------------------
39 TalkerListModel::TalkerListModel(TalkerCode::TalkerCodeList talkers, QObject *parent) :
40 QAbstractListModel(parent),
41 m_talkerCodes(talkers),
42 m_highestTalkerId(0)
46 int TalkerListModel::rowCount(const QModelIndex &parent) const
48 if (!parent.isValid())
49 return m_talkerCodes.count();
50 else
51 return 0;
54 int TalkerListModel::columnCount(const QModelIndex &parent) const
56 Q_UNUSED(parent);
57 return 7;
60 QModelIndex TalkerListModel::index(int row, int column, const QModelIndex &parent) const
62 if (!parent.isValid())
63 return createIndex(row, column, 0);
64 else
65 return QModelIndex();
68 QModelIndex TalkerListModel::parent(const QModelIndex & index ) const
70 Q_UNUSED(index);
71 return QModelIndex();
74 QVariant TalkerListModel::data(const QModelIndex &index, int role) const
76 if (!index.isValid())
77 return QVariant();
79 if (index.row() < 0 || index.row() >= m_talkerCodes.count())
80 return QVariant();
82 if (index.column() < 0 || index.column() >= 7)
83 return QVariant();
85 if (role == Qt::DisplayRole)
86 return dataColumn(m_talkerCodes.at(index.row()), index.column());
87 else
88 return QVariant();
91 QVariant TalkerListModel::dataColumn(const TalkerCode& talkerCode, int column) const
93 switch (column)
95 case 0: return talkerCode.id(); break;
96 case 1: return talkerCode.languageCodeToLanguage(talkerCode.fullLanguageCode()); break;
97 case 2: return talkerCode.TalkerDesktopEntryNameToName(talkerCode.desktopEntryName()); break;
98 case 3: return talkerCode.voice(); break;
99 case 4: return talkerCode.translatedGender(talkerCode.gender()); break;
100 case 5: return talkerCode.translatedVolume(talkerCode.volume()); break;
101 case 6: return talkerCode.translatedRate(talkerCode.rate()); break;
103 return QVariant();
106 Qt::ItemFlags TalkerListModel::flags(const QModelIndex &index) const
108 if (!index.isValid())
109 return Qt::ItemIsEnabled;
111 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
114 QVariant TalkerListModel::headerData(int section, Qt::Orientation orientation, int role) const
116 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
117 switch (section)
119 case 0: return i18n("ID");
120 case 1: return i18n("Language");
121 case 2: return i18n("Synthesizer");
122 case 3: return i18n("Voice Code");
123 case 4: return i18n("Gender");
124 case 5: return i18n("Volume");
125 case 6: return i18n("Rate");
128 return QVariant();
131 bool TalkerListModel::removeRow(int row, const QModelIndex & parent)
133 beginRemoveRows(parent, row, row);
134 m_talkerCodes.removeAt(row);
135 for (int i = 0; i < m_talkerCodes.count(); ++i)
136 if (m_talkerCodes[i].id().toInt() > m_highestTalkerId)
137 m_highestTalkerId = m_talkerCodes[i].id().toInt();
138 endRemoveRows();
139 return true;
142 void TalkerListModel::setDatastore(TalkerCode::TalkerCodeList talkers)
144 m_talkerCodes = talkers;
145 m_highestTalkerId = 0;
146 for (int i = 0; i < talkers.count(); ++i)
147 if (talkers[i].id().toInt() > m_highestTalkerId) m_highestTalkerId = talkers[i].id().toInt();
148 emit reset();
151 TalkerCode TalkerListModel::getRow(int row) const
153 if (row < 0 || row >= rowCount()) return TalkerCode();
154 return m_talkerCodes[row];
157 bool TalkerListModel::appendRow(TalkerCode& talker)
159 if (talker.id().toInt() > m_highestTalkerId) m_highestTalkerId = talker.id().toInt();
160 beginInsertRows(QModelIndex(), m_talkerCodes.count(), m_talkerCodes.count());
161 m_talkerCodes.append(talker);
162 endInsertRows();
163 return true;
166 bool TalkerListModel::updateRow(int row, TalkerCode& talker)
168 for (int i = 0; i < m_talkerCodes.count(); ++i)
169 if (m_talkerCodes[i].id().toInt() > m_highestTalkerId)
170 m_highestTalkerId = m_talkerCodes[i].id().toInt();
171 m_talkerCodes.replace(row, talker);
172 emit dataChanged(index(row, 0, QModelIndex()), index(row, columnCount()-1, QModelIndex()));
173 return true;
176 bool TalkerListModel::swap(int i, int j)
178 m_talkerCodes.swap(i, j);
179 emit dataChanged(index(i, 0, QModelIndex()), index(j, columnCount()-1, QModelIndex()));
180 return true;
183 void TalkerListModel::clear()
185 m_talkerCodes.clear();
186 m_highestTalkerId = 0;
187 emit reset();
190 void TalkerListModel::loadTalkerCodesFromConfig(KConfig* c)
192 // Clear the model and view.
193 clear();
194 // Iterate through list of the TalkerCode IDs.
195 KConfigGroup config(c, "General");
196 QStringList talkerIDsList = config.readEntry("TalkerIDs", QStringList(), ',');
197 // kDebug() << "TalkerListModel::loadTalkerCodesFromConfig: talkerIDsList = " << talkerIDsList << endl;
198 if (!talkerIDsList.isEmpty())
200 QStringList::ConstIterator itEnd = talkerIDsList.constEnd();
201 for (QStringList::ConstIterator it = talkerIDsList.constBegin(); it != itEnd; ++it)
203 QString talkerID = *it;
204 kDebug() << "TalkerListWidget::loadTalkerCodes: talkerID = " << talkerID << endl;
205 KConfigGroup talkGroup(c, QString("Talker_") + talkerID);
206 QString talkerCode = talkGroup.readEntry("TalkerCode");
207 TalkerCode tc = TalkerCode(talkerCode, true);
208 kDebug() << "TalkerCodeWidget::loadTalkerCodes: talkerCode = " << talkerCode << endl;
209 tc.setId(talkerID);
210 QString desktopEntryName = talkGroup.readEntry("DesktopEntryName", QString());
211 tc.setDesktopEntryName(desktopEntryName);
212 appendRow(tc);