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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-02-12
0007  * Description : Wrapper model for table view
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_MODEL_H
0017 #define DIGIKAM_TABLE_VIEW_MODEL_H
0018 
0019 // Qt includes
0020 
0021 #include <QAbstractItemModel>
0022 #include <QUrl>
0023 
0024 // Local includes
0025 
0026 #include "coredbchangesets.h"
0027 #include "tableview_shared.h"
0028 
0029 class QMimeData;
0030 
0031 namespace Digikam
0032 {
0033 
0034 class ImageChangeset;
0035 class ItemFilterModel;
0036 class ItemFilterSettings;
0037 class ItemInfo;
0038 class ItemInfoList;
0039 class TableViewColumn;
0040 class TableViewColumnConfiguration;
0041 class TableViewColumnDescription;
0042 class TableViewColumnFactory;
0043 class TableViewColumnProfile;
0044 
0045 class TableViewModel : public QAbstractItemModel
0046 {
0047     Q_OBJECT
0048 
0049 public:
0050 
0051     enum GroupingMode
0052     {
0053         GroupingHideGrouped    = 0,
0054         GroupingIgnoreGrouping = 1,
0055         GroupingShowSubItems   = 2
0056     };
0057 
0058     typedef DatabaseFields::Hash<QVariant> DatabaseFieldsHashRaw;
0059 
0060 public:
0061 
0062     class Item
0063     {
0064     public:
0065 
0066         explicit Item();
0067         ~Item();
0068 
0069         void  addChild(Item* const newChild);
0070         void  insertChild(const int pos, Item* const newChild);
0071         void  takeChild(Item* const oldChild);
0072         Item* findChildWithImageId(const qlonglong searchImageId);
0073 
0074     public:
0075 
0076         qlonglong    imageId;
0077         Item*        parent;
0078         QList<Item*> children;
0079     };
0080 
0081 public:
0082 
0083     explicit TableViewModel(TableViewShared* const sharedObject, QObject* const parent = nullptr);
0084     ~TableViewModel()                                                                                                 override;
0085 
0086     void                    addColumnAt(const TableViewColumnDescription& description,
0087                                         const int targetColumn = -1);
0088     void                    addColumnAt(const TableViewColumnConfiguration& cpp,
0089                                         const int targetColumn = -1);
0090     void                    removeColumnAt(const int columnIndex);
0091     TableViewColumn*        getColumnObject(const int columnIndex);
0092     QList<TableViewColumn*> getColumnObjects();
0093     QModelIndex             fromItemFilterModelIndex(const QModelIndex& imageFilterModelIndex);
0094     QModelIndex             fromItemModelIndex(const QModelIndex& imageModelIndex);
0095     QModelIndex             toItemFilterModelIndex(const QModelIndex& i)                                        const;
0096     QModelIndex             toItemModelIndex(const QModelIndex& i)                                              const;
0097     void                    loadColumnProfile(const TableViewColumnProfile& columnProfile);
0098     TableViewColumnProfile  getColumnProfile()                                                                  const;
0099 
0100     QModelIndex deepRowIndex(const int rowNumber)                                                               const;
0101     int indexToDeepRowNumber(const QModelIndex& index)                                                          const;
0102     int deepRowCount()                                                                                          const;
0103     int firstDeepRowNotInList(const QList<QModelIndex>& needleList);
0104     QModelIndex toCol0(const QModelIndex& anIndex)                                                              const;
0105 
0106     QModelIndex   itemIndex(Item* const item)                                                                   const;
0107     QModelIndex   indexFromImageId(const qlonglong imageId, const int columnIndex)                              const;
0108     Item*         itemFromImageId(const qlonglong imageId)                                                      const;
0109     Item*         itemFromIndex(const QModelIndex& i)                                                           const;
0110     ItemInfo     infoFromItem(Item* const item)                                                                 const;
0111     ItemInfoList infosFromItems(const QList<Item*>& items)                                                      const;
0112 
0113     QVariant              itemDatabaseFieldRaw(Item* const item, const DatabaseFields::Set& requestedField);
0114     DatabaseFieldsHashRaw itemDatabaseFieldsRaw(Item* const item, const DatabaseFields::Set& requestedSet);
0115 
0116     qlonglong        imageId(const QModelIndex& anIndex)                                                        const;
0117     QList<qlonglong> imageIds(const QModelIndexList& indexList)                                                 const;
0118     QList<ItemInfo> imageInfos(const QModelIndexList& indexList)                                                const;
0119     ItemInfo        imageInfo(const QModelIndex& index)                                                         const;
0120     QList<ItemInfo> allItemInfo()                                                                               const;
0121 
0122     QList<Item*> sortItems(const QList<Item*>& itemList);
0123     class LessThan;
0124     bool lessThan(Item* const itemA, Item* const itemB);
0125     int findChildSortedPosition(Item* const parentItem, Item* const childItem);
0126 
0127     void scheduleResort();
0128     GroupingMode groupingMode()                                                                                 const;
0129     void setGroupingMode(const GroupingMode newGroupingMode);
0130 
0131 public:
0132 
0133     QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex())                   const override;
0134     QModelIndex parent(const QModelIndex& childIndex)                                                   const override;
0135     int rowCount(const QModelIndex& parent)                                                             const override;
0136     int columnCount(const QModelIndex& i)                                                               const override;
0137     QVariant data(const QModelIndex& i, int role)                                                       const override;
0138     QVariant headerData(int section, Qt::Orientation orientation, int role)                             const override;
0139     Qt::ItemFlags flags(const QModelIndex& index)                                                       const override;
0140     bool hasChildren(const QModelIndex& parent = QModelIndex())                                         const override;
0141 
0142     /// drag-and-drop related functions
0143     Qt::DropActions supportedDropActions()                                                              const override;
0144     QStringList mimeTypes()                                                                             const override;
0145     bool dropMimeData(const QMimeData* data,
0146                               Qt::DropAction action,
0147                               int row,
0148                               int column,
0149                               const QModelIndex& parent) override;
0150     QMimeData* mimeData(const QModelIndexList& indexes)                                                 const override;
0151 
0152 protected:
0153 
0154     void sort(int column, Qt::SortOrder order = Qt::AscendingOrder)                                           override;
0155 
0156 private Q_SLOTS:
0157 
0158     void slotPopulateModelWithNotifications();
0159     void slotPopulateModel(const bool sendNotifications);
0160 
0161     void slotColumnDataChanged(const qlonglong imageId);
0162     void slotColumnAllDataChanged();
0163 
0164     void slotSourceModelAboutToBeReset();
0165     void slotSourceModelReset();
0166     void slotSourceRowsAboutToBeInserted(const QModelIndex& parent, int start, int end);
0167     void slotSourceRowsInserted(const QModelIndex& parent, int start, int end);
0168     void slotSourceRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
0169     void slotSourceRowsRemoved(const QModelIndex& parent, int start, int end);
0170     void slotSourceRowsAboutToBeMoved(const QModelIndex& sourceParent, int sourceStart, int sourceEnd,
0171                                       const QModelIndex& destinationParent, int destinationRow);
0172     void slotSourceRowsMoved(const QModelIndex& sourceParent, int sourceStart, int sourceEnd,
0173                              const QModelIndex& destinationParent, int destinationRow);
0174     void slotSourceLayoutAboutToBeChanged();
0175     void slotSourceLayoutChanged();
0176 
0177     void slotDatabaseImageChanged(const ImageChangeset& imageChangeset);
0178 
0179     void slotFilterSettingsChanged(const ItemFilterSettings& settings);
0180     void slotResortModel();
0181     void slotClearModel(const bool sendNotifications);
0182 
0183 public Q_SLOTS:
0184 
0185     void slotSetActive(const bool isActive);
0186 
0187 Q_SIGNALS:
0188 
0189     void signalGroupingModeChanged();
0190 
0191 private:
0192 
0193     Item* createItemFromSourceIndex(const QModelIndex& imageFilterModelIndex);
0194     void addSourceModelIndex(const QModelIndex& imageModelIndex, const bool sendNotifications);
0195 
0196 private:
0197 
0198     TableViewShared* const s;
0199     class Private;
0200     const QScopedPointer<Private> d;
0201 };
0202 
0203 } // namespace Digikam
0204 
0205 Q_DECLARE_METATYPE(Digikam::TableViewModel::GroupingMode)
0206 
0207 #endif // DIGIKAM_TABLE_VIEW_MODEL_H