1 /*
2 SPDX-FileCopyrightText: 2013 Andreas Cord-Landwehr <cordlandwehr@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6
7 #include "phonememodel.h"
8 #include "core/language.h"
9 #include "core/phoneme.h"
10
11 #include <QSignalMapper>
12
13 #include "artikulate_debug.h"
14 #include <KLocalizedString>
15
PhonemeModel(QObject * parent)16 PhonemeModel::PhonemeModel(QObject *parent)
17 : QAbstractListModel(parent)
18 , m_language(nullptr)
19 , m_signalMapper(new QSignalMapper(this))
20 {
21 connect(m_signalMapper, SIGNAL(mapped(int)), SLOT(emitPhonemeChanged(int)));
22 }
23
roleNames() const24 QHash<int, QByteArray> PhonemeModel::roleNames() const
25 {
26 QHash<int, QByteArray> roles;
27 roles[TitleRole] = "title";
28 roles[IdRole] = "id";
29 roles[DataRole] = "dataRole";
30
31 return roles;
32 }
33
language() const34 Language *PhonemeModel::language() const
35 {
36 return m_language;
37 }
38
setLanguage(Language * language)39 void PhonemeModel::setLanguage(Language *language)
40 {
41 beginResetModel();
42 m_language = language;
43 emit languageChanged();
44 endResetModel();
45 }
46
data(const QModelIndex & index,int role) const47 QVariant PhonemeModel::data(const QModelIndex &index, int role) const
48 {
49 if (!index.isValid()) {
50 return QVariant();
51 }
52
53 if (index.row() >= m_language->phonemes().count()) {
54 return QVariant();
55 }
56
57 auto phoneme = m_language->phonemes().at(index.row());
58
59 switch (role) {
60 case Qt::DisplayRole:
61 return !phoneme->title().isEmpty() ? QVariant(phoneme->title()) : QVariant(i18nc("@item:inlistbox:", "unknown"));
62 case Qt::ToolTipRole:
63 return QVariant(phoneme->title());
64 case TitleRole:
65 return phoneme->title();
66 case IdRole:
67 return phoneme->id();
68 case DataRole:
69 return QVariant::fromValue<QObject *>(phoneme.get());
70 default:
71 return QVariant();
72 }
73 }
74
rowCount(const QModelIndex & parent) const75 int PhonemeModel::rowCount(const QModelIndex &parent) const
76 {
77 if (!m_language) {
78 return 0;
79 }
80
81 if (parent.isValid()) {
82 return 0;
83 }
84 return m_language->phonemes().count();
85 }
86
onPhonemeAboutToBeAdded(Phoneme * phoneme,int index)87 void PhonemeModel::onPhonemeAboutToBeAdded(Phoneme *phoneme, int index)
88 {
89 connect(phoneme, SIGNAL(titleChanged()), m_signalMapper, SLOT(map()));
90 beginInsertRows(QModelIndex(), index, index);
91 }
92
onPhonemeAdded()93 void PhonemeModel::onPhonemeAdded()
94 {
95 updateMappings();
96 endInsertRows();
97 }
98
onPhonemesAboutToBeRemoved(int first,int last)99 void PhonemeModel::onPhonemesAboutToBeRemoved(int first, int last)
100 {
101 beginRemoveRows(QModelIndex(), first, last);
102 }
103
onPhonemesRemoved()104 void PhonemeModel::onPhonemesRemoved()
105 {
106 endRemoveRows();
107 }
108
emitPhonemeChanged(int row)109 void PhonemeModel::emitPhonemeChanged(int row)
110 {
111 emit phonemeChanged(row);
112 emit dataChanged(index(row, 0), index(row, 0));
113 }
114
headerData(int section,Qt::Orientation orientation,int role) const115 QVariant PhonemeModel::headerData(int section, Qt::Orientation orientation, int role) const
116 {
117 if (role != Qt::DisplayRole) {
118 return QVariant();
119 }
120 if (orientation == Qt::Vertical) {
121 return QVariant(section + 1);
122 }
123 return QVariant(i18nc("@title:column", "Phoneme"));
124 }
125
updateMappings()126 void PhonemeModel::updateMappings()
127 {
128 if (!m_language) {
129 qCDebug(ARTIKULATE_LOG) << "Aborting to update mappings, language not set.";
130 return;
131 }
132 int phonemes = m_language->phonemes().count();
133 for (int i = 0; i < phonemes; i++) {
134 m_signalMapper->setMapping(m_language->phonemes().at(i).get(), i);
135 }
136 }
137