phrasemodel.cpp 9.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
 *  Copyright 2013-2015  Andreas Cord-Landwehr <cordlandwehr@kde.org>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License or (at your option) version 3 or any later version
 *  accepted by the membership of KDE e.V. (or its successor approved
 *  by the membership of KDE e.V.), which shall act as a proxy
 *  defined in Section 14 of version 3 of the license.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "phrasemodel.h"
#include "core/course.h"
#include "core/unit.h"
#include "core/phrase.h"
#include <QAbstractItemModel>
#include <QSignalMapper>
#include <KLocalizedString>
28
#include "artikulate_debug.h"
29
30
31
32

PhraseModel::PhraseModel(QObject *parent)
    : QAbstractItemModel(parent)
    , m_course(nullptr)
33
    , m_unitSignalMapper(new QSignalMapper)
34
    , m_phraseSignalMapper(new QSignalMapper)
35
{
36
37
    connect(m_unitSignalMapper, static_cast<void (QSignalMapper::*)(int)>(&QSignalMapper::mapped),
            this, &PhraseModel::onUnitChanged);
38
39
    connect(m_phraseSignalMapper, static_cast<void (QSignalMapper::*)(QObject *)>(&QSignalMapper::mapped),
            this, &PhraseModel::onPhraseChanged);
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
}

QHash< int, QByteArray > PhraseModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[TextRole] = "text";
    roles[DataRole] = "dataRole";

    return roles;
}

void PhraseModel::setCourse(Course *course)
{
    if (m_course == course) {
        return;
    }

    beginResetModel();

    if (m_course) {
        m_course->disconnect(this);
61
        foreach (auto const &unit, m_course->unitList()) {
62
            unit->disconnect(this);
63
            foreach (auto const &phrase, unit->phraseList()) {
64
                phrase->disconnect(this);
65
66
67
68
69
70
            }
        }
    }

    m_course = course;
    if (m_course) {
71
72
73
74
75
76
        // connect to unit changes
        connect(m_course, &Course::unitAboutToBeAdded, this, &PhraseModel::onUnitAboutToBeAdded);
        connect(m_course, &Course::unitAdded, this, &PhraseModel::onUnitAdded);
        connect(m_course, &Course::unitsAboutToBeRemoved, this, &PhraseModel::onUnitsAboutToBeRemoved);
        connect(m_course, &Course::unitsRemoved, this, &PhraseModel::onUnitsRemoved);

77
        // initial setting of signal mappings
78
        foreach (auto const &unit, m_course->unitList()) {
79
            // connect to phrase changes
80
81
82
83
            connect(unit, &Unit::phraseAboutToBeAdded, this, &PhraseModel::onPhraseAboutToBeAdded);
            connect(unit, static_cast<void (Unit::*)()>(&Unit::phraseAdded), this, &PhraseModel::onPhraseAdded);
            connect(unit, &Unit::phraseAboutToBeRemoved, this, &PhraseModel::onPhrasesAboutToBeRemoved);
            connect(unit, static_cast<void (Unit::*)()>(&Unit::phraseRemoved), this, &PhraseModel::onPhrasesRemoved);
84
            connect(unit, &Unit::titleChanged, m_unitSignalMapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
85
86
87
88
89
90
91
92

            // insert and connect all already existing phrases
            int phrases = unit->phraseList().count();
            for (int i = 0; i < phrases; ++i) {
                onPhraseAboutToBeAdded(unit->phraseList().at(i), i);
                endInsertRows();
            }
        }
93
        updateUnitMappings();
94
        updatePhraseMappings();
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    }

    // emit done
    endResetModel();
    emit courseChanged();
}

Course * PhraseModel::course() const
{
    return m_course;
}

QVariant PhraseModel::data(const QModelIndex &index, int role) const
{
    Q_ASSERT(m_course);

    if (!index.isValid()) {
        return QVariant();
    }

    if (!index.internalPointer()) {
116
117
118
        if (!m_course || m_course->unitList().size() == 0) {
            return QVariant();
        }
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        Unit *unit = m_course->unitList().at(index.row());
        switch(role)
        {
        case TextRole:
            return unit->title();
        case DataRole:
            return QVariant::fromValue<QObject*>(unit);
        default:
            return QVariant();
        }
    }
    else {
        Unit *unit = static_cast<Unit*>(index.internalPointer());
        switch(role)
        {
        case TextRole:
            return unit->phraseList().at(index.row())->text();
        case DataRole:
            return QVariant::fromValue<QObject*>(unit->phraseList().at(index.row()));
        default:
            return QVariant();
        }
    }
}

int PhraseModel::rowCount(const QModelIndex &parent) const
{
    if (!m_course) {
        return 0;
    }

    // no valid index -> must be (invisible) root
    if (!parent.isValid()) {
        return m_course->unitList().count();
    }

    // internal pointer -> must be a phrase
    if (parent.internalPointer()) {
        return 0;
    }

    // else -> must be a unit
    Unit *unit = m_course->unitList().at(parent.row());
    return unit->phraseList().count();
}

int PhraseModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 1;
}

QModelIndex PhraseModel::parent(const QModelIndex &child) const
{
173
    if (!child.internalPointer() || !m_course) {
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
        return QModelIndex();
    }
    Unit *parent = static_cast<Unit*>(child.internalPointer());
    for (int i = 0; i < m_course->unitList().count(); ++i) {
        if (m_course->unitList().at(i) == parent) {
            return createIndex(i, 0);
        }
    }
    return QModelIndex();
}

QModelIndex PhraseModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!parent.isValid()) { // unit elements
        return createIndex(row, column);
    } else { // phrase elements
        Unit *unit = m_course->unitList().at(parent.row());
        if (unit) {
            return createIndex(row, column, unit);
        }
    }
    return QModelIndex();
}

198
QModelIndex PhraseModel::indexPhrase(Phrase *phrase) const
199
200
201
202
203
204
205
206
{
    if (!phrase) {
        return QModelIndex();
    }
    Unit *unit = phrase->unit();
    return createIndex(unit->phraseList().indexOf(phrase), 0, unit);
}

207
208
QModelIndex PhraseModel::indexUnit(Unit *unit) const
{
209
    if (!unit || !m_course) {
210
211
212
213
214
        return QModelIndex();
    }
    return createIndex(m_course->unitList().indexOf(unit), 0);
}

215
216
217
218
219
bool PhraseModel::isUnit(const QModelIndex &index) const
{
    return (index.internalPointer() == nullptr);
}

220
221
222
void PhraseModel::onPhraseAboutToBeAdded(Phrase *phrase, int index)
{
    int unitIndex = m_course->unitList().indexOf(phrase->unit());
223
    connect(phrase, &Phrase::textChanged, m_phraseSignalMapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
224
225
226
227
228
229
    beginInsertRows(createIndex(unitIndex, 0), index, index);
}

void PhraseModel::onPhraseAdded()
{
    endInsertRows();
230
    updatePhraseMappings();
231
232
233
234
235
}

void PhraseModel::onPhrasesAboutToBeRemoved(int first, int last)
{
    //TODO better solution requires access to unit
236
    //TODO remove connections from m_phraseSignalMapper
237
238
239
240
241
242
243
244
    beginResetModel();
}

void PhraseModel::onPhrasesRemoved()
{
    endResetModel();
}

245
246
247
248
249
250
251
252
void PhraseModel::onPhraseChanged(QObject *phrase)
{
    Phrase *changedPhrase = qobject_cast<Phrase*>(phrase);
    Q_ASSERT(changedPhrase);
    QModelIndex index = indexPhrase(changedPhrase);
    emit dataChanged(index, index);
}

253
254
void PhraseModel::onUnitAboutToBeAdded(Unit *unit, int index)
{
255
    Q_UNUSED(unit)
256
    beginInsertRows(QModelIndex(), index, index);
257
    connect(unit, &Unit::titleChanged, m_unitSignalMapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
258
259
260
261
262
}

void PhraseModel::onUnitAdded()
{
    endInsertRows();
263
    updateUnitMappings();
264
265
266
267
}

void PhraseModel::onUnitsAboutToBeRemoved(int first, int last)
{
268
269
270
271
    for (int i = first; i <= last; ++i) {
        Unit *unit = m_course->unitList().at(i);
        disconnect(unit, &Unit::titleChanged, m_unitSignalMapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
    }
272
273
274
275
276
277
278
279
    beginRemoveRows(QModelIndex(), first, last);
}

void PhraseModel::onUnitsRemoved()
{
    endRemoveRows();
}

280
281
282
283
284
void PhraseModel::onUnitChanged(int index)
{
    emit dataChanged(createIndex(index, 0), createIndex(index, 0));
}

285
286
287
288
289
290
291
292
293
294
QVariant PhraseModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole) {
        return QVariant();
    }
    if (orientation == Qt::Vertical) {
        return QVariant(section + 1);
    }
    return QVariant(i18nc("@title:column", "Phrase"));
}
295

296
297
298
299
300
301
302
303
bool PhraseModel::isPhrase(const QModelIndex &index) const
{
    if (index.internalPointer()) {
        return true;
    }
    return false;
}

Andreas Cord-Landwehr's avatar
Andreas Cord-Landwehr committed
304
305
Phrase * PhraseModel::phrase(const QModelIndex &index) const
{
306
    if (index.internalPointer()) {
307
308
309
        Unit *unit = static_cast<Unit *>(index.internalPointer());
        return unit->phraseList().at(index.row());
    }
310
311
312
    if (!m_course->unitList().at(index.row())->phraseList().isEmpty()) {
        return m_course->unitList().at(index.row())->phraseList().first();
    }
313
314
    return nullptr;
}
315
316
317
318
319
320
321
322
323
324
325
326
327

Unit * PhraseModel::unit(const QModelIndex &index) const
{
    return m_course->unitList().at(index.row());
}

void PhraseModel::updateUnitMappings()
{
    int units = m_course->unitList().count();
    for (int i = 0; i < units; ++i) {
        m_unitSignalMapper->setMapping(m_course->unitList().at(i), i);
    }
}
328
329
330
331
332
333
334
335
336
337
338

void PhraseModel::updatePhraseMappings()
{
    //TODO this might be quite costly for long units
    // better, implement access based on index pairs
    foreach (const Unit *unit, m_course->unitList()) {
        foreach (Phrase *phrase, unit->phraseList()) {
            m_phraseSignalMapper->setMapping(phrase, phrase);
        }
    }
}