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, Boston, MA 02110-1301, USA.
22 #include "preferredimagemetainfomodel.h"
25 #include <QStringList>
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
);
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
)) {
64 mPreferredMetaInfoKeyList
= sortedList
;
69 PreferredImageMetaInfoModel::PreferredImageMetaInfoModel(ImageMetaInfoModel
* model
, const QStringList
& list
)
70 : d(new PreferredImageMetaInfoModelPrivate
) {
72 setSourceModel(model
);
73 d
->mPreferredMetaInfoKeyList
= list
;
77 PreferredImageMetaInfoModel::~PreferredImageMetaInfoModel() {
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
;
92 QVariant
PreferredImageMetaInfoModel::data(const QModelIndex
& index
, int role
) const {
93 QModelIndex sourceIndex
= mapToSource(index
);
94 if (!sourceIndex
.isValid()) {
99 case Qt::CheckStateRole
:
100 return d
->checkStateData(sourceIndex
);
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
) {
114 if (!sourceIndex
.parent().isValid()) {
118 QString key
= d
->mModel
->keyForIndex(sourceIndex
);
119 if (value
== Qt::Checked
) {
120 d
->mPreferredMetaInfoKeyList
<< key
;
121 d
->sortPreferredMetaInfoKeyList();
123 d
->mPreferredMetaInfoKeyList
.removeAll(key
);
125 emit
preferredMetaInfoKeyListChanged(d
->mPreferredMetaInfoKeyList
);
126 emit
dataChanged(index
, index
);