2
* Copyright: 2014 Canonical, Ltd
4
* This file is part of reminders
6
* reminders is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 3.
10
* reminders is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18
* Authors: Michael Zanetti <michael.zanetti@canonical.com>
26
Tags::Tags(QObject *parent) :
27
QAbstractListModel(parent)
29
foreach (Tag *tag, NotesStore::instance()->tags()) {
30
m_list.append(tag->guid());
31
connect(tag, &Tag::noteCountChanged, this, &Tags::noteCountChanged);
34
connect(NotesStore::instance(), &NotesStore::tagsLoadingChanged, this, &Tags::loadingChanged);
35
connect(NotesStore::instance(), &NotesStore::tagsErrorChanged, this, &Tags::errorChanged);
36
connect(NotesStore::instance(), &NotesStore::tagAdded, this, &Tags::tagAdded);
37
connect(NotesStore::instance(), &NotesStore::tagRemoved, this, &Tags::tagRemoved);
40
bool Tags::loading() const
42
return NotesStore::instance()->tagsLoading();
45
QString Tags::error() const
47
return NotesStore::instance()->tagsError();
50
int Tags::count() const
55
QVariant Tags::data(const QModelIndex &index, int role) const
57
Tag *tag = NotesStore::instance()->tag(m_list.at(index.row()));
64
return tag->noteCount();
69
int Tags::rowCount(const QModelIndex &parent) const
72
return m_list.count();
75
QHash<int, QByteArray> Tags::roleNames() const
77
QHash<int, QByteArray> roles;
78
roles.insert(RoleGuid, "guid");
79
roles.insert(RoleName, "name");
80
roles.insert(RoleNoteCount, "noteCount");
84
Tag* Tags::tag(int index) const
86
if (index < 0 || index >= m_list.count()) {
89
return NotesStore::instance()->tag(m_list.at(index));
94
NotesStore::instance()->refreshTags();
97
void Tags::tagAdded(const QString &guid)
99
Tag *tag = NotesStore::instance()->tag(guid);
100
connect(tag, &Tag::nameChanged, this, &Tags::nameChanged);
101
connect(tag, &Tag::noteCountChanged, this, &Tags::noteCountChanged);
103
beginInsertRows(QModelIndex(), m_list.count(), m_list.count());
109
void Tags::tagRemoved(const QString &guid)
111
beginRemoveRows(QModelIndex(), m_list.indexOf(guid), m_list.indexOf(guid));
112
m_list.removeAll(guid);
117
void Tags::nameChanged()
119
Tag *tag = static_cast<Tag*>(sender());
120
QModelIndex idx = index(m_list.indexOf(tag->guid()));
121
emit dataChanged(idx, idx, QVector<int>() << RoleName);
124
void Tags::noteCountChanged()
126
Tag *tag= static_cast<Tag*>(sender());
127
QModelIndex idx = index(m_list.indexOf(tag->guid()));
128
emit dataChanged(idx, idx, QVector<int>() << RoleNoteCount);