1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
3 Gwenview: an image viewer
4 Copyright 2008 Aurélien Gâteau <aurelien.gateau@free.fr>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
22 #include "tagmodel.moc"
31 #include "abstractsemanticinfobackend.h"
36 struct TagModelPrivate
{
37 AbstractSemanticInfoBackEnd
* mBackEnd
;
41 static QStandardItem
* createItem(const SemanticInfoTag
& tag
, const QString
& label
, TagModel::AssignmentStatus status
) {
42 QStandardItem
* item
= new QStandardItem(label
);
43 item
->setData(tag
, TagModel::TagRole
);
44 item
->setData(label
.toLower(), TagModel::SortRole
);
45 item
->setData(status
, TagModel::AssignmentStatusRole
);
46 item
->setData(KIcon("mail-tagged.png"), Qt::DecorationRole
);
51 TagModel::TagModel(QObject
* parent
)
52 : QStandardItemModel(parent
)
53 , d(new TagModelPrivate
) {
55 setSortRole(SortRole
);
59 TagModel::~TagModel() {
64 void TagModel::setSemanticInfoBackEnd(AbstractSemanticInfoBackEnd
* backEnd
) {
65 d
->mBackEnd
= backEnd
;
69 void TagModel::setTagSet(const TagSet
& set
) {
71 Q_FOREACH(const SemanticInfoTag
& tag
, set
) {
72 QString label
= d
->mBackEnd
->labelForTag(tag
);
73 QStandardItem
* item
= createItem(tag
, label
, TagModel::FullyAssigned
);
80 void TagModel::addTag(const SemanticInfoTag
& tag
, const QString
& _label
, TagModel::AssignmentStatus status
) {
82 QString label
= _label
.isEmpty() ? d
->mBackEnd
->labelForTag(tag
) : _label
;
84 const QString sortLabel
= label
.toLower();
85 // This is not optimal, implement dichotomic search if necessary
86 for (row
=0; row
< rowCount(); ++row
) {
87 const QModelIndex idx
= index(row
, 0);
88 if (idx
.data(SortRole
).toString().compare(sortLabel
) > 0) {
93 QStandardItem
* _item
= item(row
- 1);
95 if (_item
->data(TagRole
).toString() == tag
) {
97 _item
->setData(label
.toLower(), SortRole
);
98 _item
->setData(status
, AssignmentStatusRole
);
102 QStandardItem
* _item
= createItem(tag
, label
, status
);
103 insertRow(row
, _item
);
107 void TagModel::removeTag(const SemanticInfoTag
& tag
) {
108 // This is not optimal, implement dichotomic search if necessary
109 for (int row
=0; row
< rowCount(); ++row
) {
110 if (index(row
, 0).data(TagRole
).toString() == tag
) {
118 TagModel
* TagModel::createAllTagsModel(QObject
* parent
, AbstractSemanticInfoBackEnd
* backEnd
) {
119 TagModel
* tagModel
= new TagModel(parent
);
120 tagModel
->setSemanticInfoBackEnd(backEnd
);
121 tagModel
->setTagSet(backEnd
->allTags());
122 connect(backEnd
, SIGNAL(tagAdded(const SemanticInfoTag
&, const QString
&)),
123 tagModel
, SLOT(addTag(const SemanticInfoTag
&, const QString
&)));