libkipi from trunk (KDE 4.3) : add support of kipi host settings "file timestamp...
[kdegraphics.git] / gwenview / lib / preferredimagemetainfomodel.cpp
blobe10c107996cb7af185d6bdbf0c76caaee8d5d8f1
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, Boston, MA 02110-1301, USA.
21 // Self
22 #include "preferredimagemetainfomodel.h"
24 // Qt
25 #include <QStringList>
27 // KDE
28 #include <kdebug.h>
31 namespace Gwenview {
34 struct PreferredImageMetaInfoModelPrivate {
35 const ImageMetaInfoModel* mModel;
36 QStringList mPreferredMetaInfoKeyList;
39 QVariant checkStateData(const QModelIndex& sourceIndex) const {
40 if (sourceIndex.parent().isValid() && sourceIndex.column() == 0) {
41 QString key = mModel->keyForIndex(sourceIndex);
42 bool checked = mPreferredMetaInfoKeyList.contains(key);
43 return QVariant(checked ? Qt::Checked: Qt::Unchecked);
44 } else {
45 return QVariant();
50 void sortPreferredMetaInfoKeyList() {
51 QStringList sortedList;
52 int groupCount = mModel->rowCount();
53 for (int groupRow = 0; groupRow < groupCount; ++groupRow) {
54 QModelIndex groupIndex = mModel->index(groupRow, 0);
55 int keyCount = mModel->rowCount(groupIndex);
56 for (int keyRow = 0; keyRow < keyCount; ++keyRow) {
57 QModelIndex keyIndex = mModel->index(keyRow, 0, groupIndex);
58 QString key = mModel->keyForIndex(keyIndex);
59 if (mPreferredMetaInfoKeyList.contains(key)) {
60 sortedList << key;
64 mPreferredMetaInfoKeyList = sortedList;
69 PreferredImageMetaInfoModel::PreferredImageMetaInfoModel(ImageMetaInfoModel* model, const QStringList& list)
70 : d(new PreferredImageMetaInfoModelPrivate) {
71 d->mModel = model;
72 setSourceModel(model);
73 d->mPreferredMetaInfoKeyList = list;
77 PreferredImageMetaInfoModel::~PreferredImageMetaInfoModel() {
78 delete d;
82 Qt::ItemFlags PreferredImageMetaInfoModel::flags(const QModelIndex& index) const {
83 QModelIndex sourceIndex = mapToSource(index);
84 Qt::ItemFlags fl = d->mModel->flags(sourceIndex);
85 if (sourceIndex.parent().isValid() && sourceIndex.column() == 0) {
86 fl |= Qt::ItemIsUserCheckable;
88 return fl;
92 QVariant PreferredImageMetaInfoModel::data(const QModelIndex& index, int role) const {
93 QModelIndex sourceIndex = mapToSource(index);
94 if (!sourceIndex.isValid()) {
95 return QVariant();
98 switch (role) {
99 case Qt::CheckStateRole:
100 return d->checkStateData(sourceIndex);
102 default:
103 return d->mModel->data(sourceIndex, role);
108 bool PreferredImageMetaInfoModel::setData(const QModelIndex& index, const QVariant& value, int role) {
109 QModelIndex sourceIndex = mapToSource(index);
110 if (role != Qt::CheckStateRole) {
111 return false;
114 if (!sourceIndex.parent().isValid()) {
115 return false;
118 QString key = d->mModel->keyForIndex(sourceIndex);
119 if (value == Qt::Checked) {
120 d->mPreferredMetaInfoKeyList << key;
121 d->sortPreferredMetaInfoKeyList();
122 } else {
123 d->mPreferredMetaInfoKeyList.removeAll(key);
125 emit preferredMetaInfoKeyListChanged(d->mPreferredMetaInfoKeyList);
126 emit dataChanged(index, index);
127 return true;
131 } // namespace