File indexing completed on 2024-04-28 04:18:52

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2008 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "preferredimagemetainfomodel.h"
0023 
0024 // Qt
0025 #include <QStringList>
0026 
0027 // KF
0028 
0029 namespace Gwenview
0030 {
0031 struct PreferredImageMetaInfoModelPrivate {
0032     const ImageMetaInfoModel *mModel;
0033     QStringList mPreferredMetaInfoKeyList;
0034 
0035     QVariant checkStateData(const QModelIndex &sourceIndex) const
0036     {
0037         if (sourceIndex.parent().isValid() && sourceIndex.column() == 0) {
0038             const QString key = mModel->keyForIndex(sourceIndex);
0039             const bool checked = mPreferredMetaInfoKeyList.contains(key);
0040             return QVariant(checked ? Qt::Checked : Qt::Unchecked);
0041         } else {
0042             return QVariant();
0043         }
0044     }
0045 
0046     void sortPreferredMetaInfoKeyList()
0047     {
0048         QStringList sortedList;
0049         int groupCount = mModel->rowCount();
0050         for (int groupRow = 0; groupRow < groupCount; ++groupRow) {
0051             const QModelIndex groupIndex = mModel->index(groupRow, 0);
0052             const int keyCount = mModel->rowCount(groupIndex);
0053             for (int keyRow = 0; keyRow < keyCount; ++keyRow) {
0054                 const QModelIndex keyIndex = mModel->index(keyRow, 0, groupIndex);
0055                 const QString key = mModel->keyForIndex(keyIndex);
0056                 if (mPreferredMetaInfoKeyList.contains(key)) {
0057                     sortedList << key;
0058                 }
0059             }
0060         }
0061         mPreferredMetaInfoKeyList = sortedList;
0062     }
0063 };
0064 
0065 PreferredImageMetaInfoModel::PreferredImageMetaInfoModel(ImageMetaInfoModel *model, const QStringList &list)
0066     : d(new PreferredImageMetaInfoModelPrivate)
0067 {
0068     d->mModel = model;
0069     setSourceModel(model);
0070     sort(0);
0071     setDynamicSortFilter(true);
0072     d->mPreferredMetaInfoKeyList = list;
0073 }
0074 
0075 PreferredImageMetaInfoModel::~PreferredImageMetaInfoModel()
0076 {
0077     delete d;
0078 }
0079 
0080 Qt::ItemFlags PreferredImageMetaInfoModel::flags(const QModelIndex &index) const
0081 {
0082     const QModelIndex sourceIndex = mapToSource(index);
0083     Qt::ItemFlags fl = d->mModel->flags(sourceIndex);
0084     if (sourceIndex.parent().isValid() && sourceIndex.column() == 0) {
0085         fl |= Qt::ItemIsUserCheckable;
0086     }
0087     return fl;
0088 }
0089 
0090 QVariant PreferredImageMetaInfoModel::data(const QModelIndex &index, int role) const
0091 {
0092     const QModelIndex sourceIndex = mapToSource(index);
0093     if (!sourceIndex.isValid()) {
0094         return {};
0095     }
0096 
0097     switch (role) {
0098     case Qt::CheckStateRole:
0099         return d->checkStateData(sourceIndex);
0100 
0101     default:
0102         return d->mModel->data(sourceIndex, role);
0103     }
0104 }
0105 
0106 bool PreferredImageMetaInfoModel::setData(const QModelIndex &index, const QVariant &value, int role)
0107 {
0108     QModelIndex sourceIndex = mapToSource(index);
0109     if (role != Qt::CheckStateRole) {
0110         return false;
0111     }
0112 
0113     if (!sourceIndex.parent().isValid()) {
0114         return false;
0115     }
0116 
0117     QString key = d->mModel->keyForIndex(sourceIndex);
0118     if (value == Qt::Checked) {
0119         d->mPreferredMetaInfoKeyList << key;
0120         d->sortPreferredMetaInfoKeyList();
0121     } else {
0122         d->mPreferredMetaInfoKeyList.removeAll(key);
0123     }
0124     Q_EMIT preferredMetaInfoKeyListChanged(d->mPreferredMetaInfoKeyList);
0125     Q_EMIT dataChanged(index, index);
0126     return true;
0127 }
0128 
0129 bool PreferredImageMetaInfoModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0130 {
0131     if (!left.parent().isValid()) {
0132         // Keep root entries in insertion order
0133         return left.row() < right.row();
0134     } else {
0135         // Sort leaf entries alphabetically
0136         return QSortFilterProxyModel::lessThan(left, right);
0137     }
0138 }
0139 
0140 } // namespace
0141 
0142 #include "moc_preferredimagemetainfomodel.cpp"