File indexing completed on 2025-01-19 03:50:49

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-02-12
0007  * Description : Table view column helpers
0008  *
0009  * SPDX-FileCopyrightText: 2017-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2013      by Michael G. Hansen <mike at mghansen dot de>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef DIGIKAM_TABLE_VIEW_COLUMNFACTORY_H
0017 #define DIGIKAM_TABLE_VIEW_COLUMNFACTORY_H
0018 
0019 // Qt includes
0020 
0021 #include <QObject>
0022 #include <QHash>
0023 #include <QWidget>
0024 
0025 // Local includes
0026 
0027 #include "tableview_model.h"
0028 #include "tableview_shared.h"
0029 
0030 class QModelIndex;
0031 class QStyleOptionViewItem;
0032 
0033 class KConfigGroup;
0034 
0035 namespace Digikam
0036 {
0037 
0038 class ItemInfo;
0039 class ThumbnailSize;
0040 
0041 class TableViewColumnConfiguration
0042 {
0043 public:
0044 
0045     explicit TableViewColumnConfiguration(const QString& id = QString())
0046       : columnId(id),
0047         columnSettings()
0048     {
0049     }
0050 
0051     QString getSetting(const QString& key, const QString& defaultValue = QString()) const
0052     {
0053         if (!columnSettings.contains(key))
0054         {
0055             return defaultValue;
0056         }
0057 
0058         return columnSettings.value(key);
0059     }
0060 
0061     void loadSettings(const KConfigGroup& configGroup);
0062     void saveSettings(KConfigGroup& configGroup) const;
0063 
0064 public:
0065 
0066     QString                 columnId;
0067     QHash<QString, QString> columnSettings;
0068 };
0069 
0070 // ----------------------------------------------------------------------------
0071 
0072 class TableViewColumnDescription
0073 {
0074 public:
0075 
0076     explicit TableViewColumnDescription()
0077       : columnId(),
0078         columnTitle(),
0079         columnIcon(),
0080         columnSettings(),
0081         subColumns()
0082     {
0083     }
0084 
0085     explicit TableViewColumnDescription(const QString& id, const QString& title,
0086                                         const QString& settingKey = QString(),
0087                                         const QString& settingValue = QString())
0088       : columnId(id),
0089         columnTitle(title),
0090         columnIcon(),
0091         columnSettings(),
0092         subColumns()
0093     {
0094         if (!settingKey.isEmpty())
0095         {
0096             addSetting(settingKey, settingValue);
0097         }
0098     }
0099 
0100 public:
0101 
0102     typedef QList<TableViewColumnDescription> List;
0103 
0104     QString                                   columnId;
0105     QString                                   columnTitle;
0106     QString                                   columnIcon;
0107     QHash<QString, QString>                   columnSettings;
0108     QList<TableViewColumnDescription>         subColumns;
0109 
0110 public:
0111 
0112     void addSubColumn(const TableViewColumnDescription& subColumnDescription)
0113     {
0114         subColumns << subColumnDescription;
0115     }
0116 
0117     void addSetting(const QString& key, const QString& value)
0118     {
0119         columnSettings.insert(key, value);
0120     }
0121 
0122     TableViewColumnConfiguration toConfiguration() const
0123     {
0124         TableViewColumnConfiguration configuration;
0125 
0126         configuration.columnId       = columnId;
0127         configuration.columnSettings = columnSettings;
0128 
0129         return configuration;
0130     }
0131 
0132     TableViewColumnDescription setIcon(const QString& iconName)
0133     {
0134         columnIcon = iconName;
0135 
0136         return *this;
0137     }
0138 
0139     static bool FindInListById(const TableViewColumnDescription::List& listToSearch, const QString& targetId, TableViewColumnDescription* const resultDescription)
0140     {
0141         TableViewColumnDescription::List leftToSearch = listToSearch;
0142 
0143         while (!leftToSearch.isEmpty())
0144         {
0145             const TableViewColumnDescription desc = leftToSearch.takeFirst();
0146 
0147             if (desc.columnId == targetId)
0148             {
0149                 *resultDescription = desc;
0150 
0151                 return true;
0152             }
0153 
0154             if (!desc.subColumns.isEmpty())
0155             {
0156                 leftToSearch << desc.subColumns;
0157             }
0158         }
0159 
0160         return false;
0161     }
0162 };
0163 
0164 // ----------------------------------------------------------------------------
0165 
0166 class TableViewColumnConfigurationWidget : public QWidget
0167 {
0168     Q_OBJECT
0169 
0170 public:
0171 
0172     explicit TableViewColumnConfigurationWidget(TableViewShared* const sharedObject,
0173                                                 const TableViewColumnConfiguration& currentConfiguration,
0174                                                 QWidget* const parent = nullptr);
0175     ~TableViewColumnConfigurationWidget() override;
0176 
0177     virtual TableViewColumnConfiguration getNewConfiguration() = 0;
0178 
0179 public:
0180 
0181     TableViewShared* const       s;
0182     TableViewColumnConfiguration configuration;
0183 };
0184 
0185 // ----------------------------------------------------------------------------
0186 
0187 class TableViewColumn : public QObject
0188 {
0189     Q_OBJECT
0190 
0191 protected:
0192 
0193     TableViewShared* const       s;
0194     TableViewColumnConfiguration configuration;
0195 
0196 public:
0197 
0198     enum ColumnFlag
0199     {
0200         ColumnNoFlags                = 0,
0201         ColumnCustomPainting         = 1,
0202         ColumnCustomSorting          = 2,
0203         ColumnHasConfigurationWidget = 4
0204     };
0205     Q_DECLARE_FLAGS(ColumnFlags, ColumnFlag)
0206 
0207     enum ColumnCompareResult
0208     {
0209         CmpEqual                     = 0,
0210         CmpABiggerB                  = 1,
0211         CmpALessB                    = 2
0212     };
0213 
0214 public:
0215 
0216     explicit TableViewColumn(TableViewShared* const tableViewShared,
0217                              const TableViewColumnConfiguration& pConfiguration,
0218                              QObject* const parent = nullptr);
0219     ~TableViewColumn() override;
0220 
0221 public:
0222 
0223     virtual TableViewColumnConfiguration getConfiguration()                                                             const;
0224     virtual void setConfiguration(const TableViewColumnConfiguration& newConfiguration);
0225     virtual TableViewColumnConfigurationWidget* getConfigurationWidget(QWidget* const parentWidget)                     const;
0226     virtual ColumnFlags getColumnFlags()                                                                                const;
0227     virtual QString getTitle()                                                                                          const = 0;
0228 
0229     virtual QVariant data(TableViewModel::Item* const item, const int role)                                             const;
0230     virtual ColumnCompareResult compare(TableViewModel::Item* const itemA, TableViewModel::Item* const itemB)           const;
0231     virtual bool columnAffectedByChangeset(const ImageChangeset& imageChangeset)                                        const;
0232     virtual bool paint(QPainter* const painter, const QStyleOptionViewItem& option, TableViewModel::Item* const item)   const;
0233     virtual QSize sizeHint(const QStyleOptionViewItem& option, TableViewModel::Item* const item)                        const;
0234     virtual void updateThumbnailSize();
0235 
0236 public:
0237 
0238     static TableViewColumnDescription getDescription();
0239     static bool compareHelperBoolFailCheck(const bool okA, const bool okB, ColumnCompareResult* const result);
0240 
0241     template<class MyType>
0242     static ColumnCompareResult compareHelper(const MyType& A, const MyType& B)
0243     {
0244         if      (A == B)
0245         {
0246             return CmpEqual;
0247         }
0248         else if (A > B)
0249         {
0250             return CmpABiggerB;
0251         }
0252 
0253         return CmpALessB;
0254     }
0255 
0256     template<typename columnClass>
0257     static bool getSubColumnIndex(const QString& subColumnId, typename columnClass::SubColumn* const subColumn)
0258     {
0259         const int index = columnClass::getSubColumns().indexOf(subColumnId);
0260 
0261         if (index < 0)
0262         {
0263             return false;
0264         }
0265 
0266         *subColumn = typename columnClass::SubColumn(index);
0267 
0268         return true;
0269     }
0270 
0271     template<typename columnClass>
0272     static bool CreateFromConfiguration(TableViewShared* const tableViewShared,
0273                                         const TableViewColumnConfiguration& pConfiguration,
0274                                         TableViewColumn** const pNewColumn,
0275                                         QObject* const parent)
0276     {
0277         typename columnClass::SubColumn subColumn;
0278 
0279         if (!getSubColumnIndex<columnClass>(pConfiguration.columnId, &subColumn))
0280         {
0281             return false;
0282         }
0283 
0284         *pNewColumn = new columnClass(tableViewShared, pConfiguration, subColumn, parent);
0285 
0286         return true;
0287     }
0288 
0289 Q_SIGNALS:
0290 
0291     void signalDataChanged(const qlonglong imageId);
0292     void signalAllDataChanged();
0293 };
0294 
0295 // ----------------------------------------------------------------------------
0296 
0297 class TableViewColumnFactory : public QObject
0298 {
0299     Q_OBJECT
0300 
0301 public:
0302 
0303     explicit TableViewColumnFactory(TableViewShared* const tableViewShared, QWidget* const parent);
0304 
0305     static QList<TableViewColumnDescription> getColumnDescriptionList();
0306     TableViewColumn* getColumn(const TableViewColumnConfiguration& columnConfiguration);
0307 
0308 private:
0309 
0310     QWidget*         m_displayWidget;
0311     TableViewShared* const s;
0312 };
0313 
0314 // ----------------------------------------------------------------------------
0315 
0316 class TableViewColumnProfile
0317 {
0318 public:
0319 
0320     TableViewColumnProfile();
0321     ~TableViewColumnProfile();
0322 
0323     void loadSettings(const KConfigGroup& configGroup);
0324     void saveSettings(KConfigGroup& configGroup);
0325 
0326 public:
0327 
0328     QList<TableViewColumnConfiguration> columnConfigurationList;
0329     QString                             name;
0330     QByteArray                          headerState;
0331 };
0332 
0333 } // namespace Digikam
0334 
0335 Q_DECLARE_METATYPE(Digikam::TableViewColumnDescription)
0336 
0337 Q_DECLARE_OPERATORS_FOR_FLAGS(Digikam::TableViewColumn::ColumnFlags)
0338 
0339 #endif // DIGIKAM_TABLE_VIEW_COLUMNFACTORY_H