libkipi from trunk (KDE 4.3) : add support of kipi host settings "file timestamp...
[kdegraphics.git] / gwenview / lib / semanticinfo / tagmodel.cpp
blob3bd79a79f0556eefadbe3429d42edebb5fdf3efb
1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
2 /*
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.
21 // Self
22 #include "tagmodel.moc"
24 // Qt
26 // KDE
27 #include <kdebug.h>
28 #include <kicon.h>
30 // Local
31 #include "abstractsemanticinfobackend.h"
33 namespace Gwenview {
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);
47 return item;
51 TagModel::TagModel(QObject* parent)
52 : QStandardItemModel(parent)
53 , d(new TagModelPrivate) {
54 d->mBackEnd = 0;
55 setSortRole(SortRole);
59 TagModel::~TagModel() {
60 delete d;
64 void TagModel::setSemanticInfoBackEnd(AbstractSemanticInfoBackEnd* backEnd) {
65 d->mBackEnd = backEnd;
69 void TagModel::setTagSet(const TagSet& set) {
70 clear();
71 Q_FOREACH(const SemanticInfoTag& tag, set) {
72 QString label = d->mBackEnd->labelForTag(tag);
73 QStandardItem* item = createItem(tag, label, TagModel::FullyAssigned);
74 appendRow(item);
76 sort(0);
80 void TagModel::addTag(const SemanticInfoTag& tag, const QString& _label, TagModel::AssignmentStatus status) {
81 int row;
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) {
89 break;
92 if (row > 0) {
93 QStandardItem* _item = item(row - 1);
94 Q_ASSERT(_item);
95 if (_item->data(TagRole).toString() == tag) {
96 // Update, do not add
97 _item->setData(label.toLower(), SortRole);
98 _item->setData(status, AssignmentStatusRole);
99 return;
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) {
111 removeRow(row);
112 return;
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&)));
124 return tagModel;
128 } // namespace