1 /***************************************************** vim:set ts=4 sw=4 sts=4:
2 Widget for listing Talkers. Based on QTreeView.
4 Copyright : (C) 2005 by Gary Cramblitt <garycramblitt@comcast.net>
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 // TalkerListWidget includes.
27 #include "talkerlistmodel.h"
28 #include "talkerlistmodel.moc"
36 #include "kconfiggroup.h"
38 // ----------------------------------------------------------------------------
40 TalkerListModel::TalkerListModel(TalkerCode::TalkerCodeList talkers
, QObject
*parent
) :
41 QAbstractListModel(parent
),
42 m_talkerCodes(talkers
),
47 int TalkerListModel::rowCount(const QModelIndex
&parent
) const
49 if (!parent
.isValid())
50 return m_talkerCodes
.count();
55 int TalkerListModel::columnCount(const QModelIndex
&parent
) const
61 QModelIndex
TalkerListModel::index(int row
, int column
, const QModelIndex
&parent
) const
63 if (!parent
.isValid())
64 return createIndex(row
, column
, 0);
69 QModelIndex
TalkerListModel::parent(const QModelIndex
& index
) const
75 QVariant
TalkerListModel::data(const QModelIndex
&index
, int role
) const
80 if (index
.row() < 0 || index
.row() >= m_talkerCodes
.count())
83 if (index
.column() < 0 || index
.column() >= 7)
86 if (role
== Qt::DisplayRole
)
87 return dataColumn(m_talkerCodes
.at(index
.row()), index
.column());
92 QVariant
TalkerListModel::dataColumn(const TalkerCode
& talkerCode
, int column
) const
96 case 0: return talkerCode
.id(); break;
97 case 1: return talkerCode
.languageCodeToLanguage(talkerCode
.fullLanguageCode()); break;
98 case 2: return talkerCode
.TalkerDesktopEntryNameToName(talkerCode
.desktopEntryName()); break;
99 case 3: return talkerCode
.voice(); break;
100 case 4: return talkerCode
.translatedGender(talkerCode
.gender()); break;
101 case 5: return talkerCode
.translatedVolume(talkerCode
.volume()); break;
102 case 6: return talkerCode
.translatedRate(talkerCode
.rate()); break;
107 Qt::ItemFlags
TalkerListModel::flags(const QModelIndex
&index
) const
109 if (!index
.isValid())
110 return Qt::ItemIsEnabled
;
112 return Qt::ItemIsEnabled
| Qt::ItemIsSelectable
;
115 QVariant
TalkerListModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
117 if (orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
)
120 case 0: return i18n("ID");
121 case 1: return i18n("Language");
122 case 2: return i18n("Synthesizer");
123 case 3: return i18n("Voice Code");
124 case 4: return i18n("Gender");
125 case 5: return i18nc("Volume of noise", "Volume");
126 case 6: return i18n("Rate");
132 bool TalkerListModel::removeRow(int row
, const QModelIndex
& parent
)
134 beginRemoveRows(parent
, row
, row
);
135 m_talkerCodes
.removeAt(row
);
136 for (int i
= 0; i
< m_talkerCodes
.count(); ++i
)
137 if (m_talkerCodes
[i
].id().toInt() > m_highestTalkerId
)
138 m_highestTalkerId
= m_talkerCodes
[i
].id().toInt();
143 void TalkerListModel::setDatastore(TalkerCode::TalkerCodeList talkers
)
145 m_talkerCodes
= talkers
;
146 m_highestTalkerId
= 0;
147 for (int i
= 0; i
< talkers
.count(); ++i
)
148 if (talkers
[i
].id().toInt() > m_highestTalkerId
) m_highestTalkerId
= talkers
[i
].id().toInt();
152 TalkerCode
TalkerListModel::getRow(int row
) const
154 if (row
< 0 || row
>= rowCount()) return TalkerCode();
155 return m_talkerCodes
[row
];
158 bool TalkerListModel::appendRow(TalkerCode
& talker
)
160 if (talker
.id().toInt() > m_highestTalkerId
) m_highestTalkerId
= talker
.id().toInt();
161 beginInsertRows(QModelIndex(), m_talkerCodes
.count(), m_talkerCodes
.count());
162 m_talkerCodes
.append(talker
);
167 bool TalkerListModel::updateRow(int row
, TalkerCode
& talker
)
169 for (int i
= 0; i
< m_talkerCodes
.count(); ++i
)
170 if (m_talkerCodes
[i
].id().toInt() > m_highestTalkerId
)
171 m_highestTalkerId
= m_talkerCodes
[i
].id().toInt();
172 m_talkerCodes
.replace(row
, talker
);
173 emit
dataChanged(index(row
, 0, QModelIndex()), index(row
, columnCount()-1, QModelIndex()));
177 bool TalkerListModel::swap(int i
, int j
)
179 m_talkerCodes
.swap(i
, j
);
180 emit
dataChanged(index(i
, 0, QModelIndex()), index(j
, columnCount()-1, QModelIndex()));
184 void TalkerListModel::clear()
186 m_talkerCodes
.clear();
187 m_highestTalkerId
= 0;
191 void TalkerListModel::loadTalkerCodesFromConfig(KConfig
* c
)
193 // Clear the model and view.
195 // Iterate through list of the TalkerCode IDs.
196 KConfigGroup
config(c
, "General");
197 QStringList talkerIDsList
= config
.readEntry("TalkerIDs", QStringList());
198 // kDebug() << "TalkerListModel::loadTalkerCodesFromConfig: talkerIDsList = " << talkerIDsList;
199 if (!talkerIDsList
.isEmpty())
201 QStringList::ConstIterator itEnd
= talkerIDsList
.constEnd();
202 for (QStringList::ConstIterator it
= talkerIDsList
.constBegin(); it
!= itEnd
; ++it
)
204 QString talkerID
= *it
;
205 kDebug() << "TalkerListWidget::loadTalkerCodes: talkerID = " << talkerID
;
206 KConfigGroup
talkGroup(c
, QString("Talker_") + talkerID
);
207 QString talkerCode
= talkGroup
.readEntry("TalkerCode");
208 TalkerCode tc
= TalkerCode(talkerCode
, true);
209 kDebug() << "TalkerCodeWidget::loadTalkerCodes: talkerCode = " << talkerCode
;
211 QString desktopEntryName
= talkGroup
.readEntry("DesktopEntryName", QString());
212 tc
.setDesktopEntryName(desktopEntryName
);