File indexing completed on 2025-01-19 03:53:44
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2010-07-13 0007 * Description : Model for item versions 0008 * 0009 * SPDX-FileCopyrightText: 2010 by Martin Klapetek <martin dot klapetek at gmail dot com> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 #include "itemversionsmodel.h" 0016 0017 // KDE includes 0018 0019 #include <klocalizedstring.h> 0020 0021 // Local includes 0022 0023 #include "digikam_debug.h" 0024 #include "workingwidget.h" 0025 0026 namespace Digikam 0027 { 0028 0029 class Q_DECL_HIDDEN ItemVersionsModel::Private 0030 { 0031 public: 0032 0033 explicit Private() 0034 : data (nullptr), 0035 paintTree(false) 0036 { 0037 } 0038 0039 /// Complete paths with filenames and tree level 0040 QList<QPair<QString, int> >* data; 0041 0042 /// This is for delegate to paint it as selected 0043 QString currentSelectedImage; 0044 0045 /** If true, the delegate will paint items as a tree 0046 * if false, it will be painted as a list 0047 */ 0048 bool paintTree; 0049 }; 0050 0051 ItemVersionsModel::ItemVersionsModel(QObject* const parent) 0052 : QAbstractListModel(parent), 0053 d (new Private) 0054 { 0055 d->data = new QList<QPair<QString, int> >; 0056 } 0057 0058 ItemVersionsModel::~ItemVersionsModel() 0059 { 0060 /* 0061 qDeleteAll(d->data); 0062 */ 0063 delete d; 0064 } 0065 0066 Qt::ItemFlags ItemVersionsModel::flags(const QModelIndex& index) const 0067 { 0068 if (!index.isValid()) 0069 { 0070 return Qt::NoItemFlags; 0071 } 0072 0073 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; 0074 } 0075 0076 QVariant ItemVersionsModel::data(const QModelIndex& index, int role) const 0077 { 0078 if (!index.isValid()) 0079 { 0080 return QVariant(); 0081 } 0082 0083 if ((role == Qt::DisplayRole) && !d->data->isEmpty()) 0084 { 0085 return d->data->at(index.row()).first; 0086 } 0087 else if ((role == Qt::UserRole) && !d->data->isEmpty()) 0088 { 0089 return d->data->at(index.row()).second; 0090 } 0091 else if ((role == Qt::DisplayRole) && d->data->isEmpty()) 0092 { 0093 // TODO: make this text Italic 0094 0095 return QVariant(QString(i18n("No image selected"))); 0096 } 0097 0098 return QVariant(); 0099 } 0100 0101 int ItemVersionsModel::rowCount(const QModelIndex& parent) const 0102 { 0103 Q_UNUSED(parent) 0104 0105 return d->data->count(); 0106 } 0107 0108 void ItemVersionsModel::setupModelData(QList<QPair<QString, int> >& data) 0109 { 0110 beginResetModel(); 0111 0112 d->data->clear(); 0113 0114 if (!data.isEmpty()) 0115 { 0116 d->data->append(data); 0117 } 0118 else 0119 { 0120 d->data->append(qMakePair(QString(i18n("This is the original image")), 0)); 0121 } 0122 0123 endResetModel(); 0124 } 0125 0126 void ItemVersionsModel::clearModelData() 0127 { 0128 beginResetModel(); 0129 0130 if (!d->data->isEmpty()) 0131 { 0132 d->data->clear(); 0133 } 0134 0135 endResetModel(); 0136 } 0137 0138 void ItemVersionsModel::slotAnimationStep() 0139 { 0140 Q_EMIT dataChanged(createIndex(0, 0), createIndex(rowCount()-1, 1)); 0141 } 0142 0143 QString ItemVersionsModel::currentSelectedImage() const 0144 { 0145 return d->currentSelectedImage; 0146 } 0147 0148 void ItemVersionsModel::setCurrentSelectedImage(const QString& path) 0149 { 0150 d->currentSelectedImage = path; 0151 } 0152 0153 QModelIndex ItemVersionsModel::currentSelectedImageIndex() const 0154 { 0155 return index(listIndexOf(d->currentSelectedImage), 0); 0156 } 0157 0158 bool ItemVersionsModel::paintTree() const 0159 { 0160 return d->paintTree; 0161 } 0162 0163 void ItemVersionsModel::setPaintTree(bool paint) 0164 { 0165 d->paintTree = paint; 0166 } 0167 0168 int ItemVersionsModel::listIndexOf(const QString& item) const 0169 { 0170 for (int i = 0 ; i < d->data->size() ; ++i) 0171 { 0172 if (d->data->at(i).first == item) 0173 { 0174 return i; 0175 } 0176 } 0177 0178 return -1; 0179 } 0180 0181 } // namespace Digikam 0182 0183 #include "moc_itemversionsmodel.cpp"