digiKam
tableview_model.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 2013-02-12
7  * Description : Wrapper model for table view
8  *
9  * Copyright (C) 2017-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  * Copyright (C) 2013 by Michael G. Hansen <mike at mghansen dot de>
11  *
12  * This program is free software; you can redistribute it
13  * and/or modify it under the terms of the GNU General
14  * Public License as published by the Free Software Foundation;
15  * either version 2, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #ifndef DIGIKAM_TABLE_VIEW_MODEL_H
25 #define DIGIKAM_TABLE_VIEW_MODEL_H
26 
27 // Qt includes
28 
29 #include <QAbstractItemModel>
30 #include <QUrl>
31 
32 // Local includes
33 
34 #include "coredbchangesets.h"
35 #include "tableview_shared.h"
36 
37 class QMimeData;
38 
39 namespace Digikam
40 {
41 
42 class ImageChangeset;
43 class ItemFilterModel;
44 class ItemFilterSettings;
45 class ItemInfo;
46 class ItemInfoList;
47 class TableViewColumn;
48 class TableViewColumnConfiguration;
49 class TableViewColumnDescription;
50 class TableViewColumnFactory;
51 class TableViewColumnProfile;
52 
53 class TableViewModel : public QAbstractItemModel
54 {
55  Q_OBJECT
56 
57 public:
58 
60  {
64  };
65 
67 
68 public:
69 
70  class Item
71  {
72  public:
73 
74  explicit Item();
75  ~Item();
76 
77  void addChild(Item* const newChild);
78  void insertChild(const int pos, Item* const newChild);
79  void takeChild(Item* const oldChild);
80  Item* findChildWithImageId(const qlonglong searchImageId);
81 
82  public:
83 
84  qlonglong imageId;
87  };
88 
89 public:
90 
91  explicit TableViewModel(TableViewShared* const sharedObject, QObject* const parent = nullptr);
92  ~TableViewModel() override;
93 
94  void addColumnAt(const TableViewColumnDescription& description,
95  const int targetColumn = -1);
97  const int targetColumn = -1);
98  void removeColumnAt(const int columnIndex);
99  TableViewColumn* getColumnObject(const int columnIndex);
101  QModelIndex fromItemFilterModelIndex(const QModelIndex& imageFilterModelIndex);
102  QModelIndex fromItemModelIndex(const QModelIndex& imageModelIndex);
103  QModelIndex toItemFilterModelIndex(const QModelIndex& i) const;
104  QModelIndex toItemModelIndex(const QModelIndex& i) const;
105  void loadColumnProfile(const TableViewColumnProfile& columnProfile);
107 
108  QModelIndex deepRowIndex(const int rowNumber) const;
109  int indexToDeepRowNumber(const QModelIndex& index) const;
110  int deepRowCount() const;
111  int firstDeepRowNotInList(const QList<QModelIndex>& needleList);
112  QModelIndex toCol0(const QModelIndex& anIndex) const;
113 
114  QModelIndex itemIndex(Item* const item) const;
115  QModelIndex indexFromImageId(const qlonglong imageId, const int columnIndex) const;
116  Item* itemFromImageId(const qlonglong imageId) const;
117  Item* itemFromIndex(const QModelIndex& i) const;
118  ItemInfo infoFromItem(Item* const item) const;
119  ItemInfoList infosFromItems(const QList<Item*>& items) const;
120 
121  QVariant itemDatabaseFieldRaw(Item* const item, const DatabaseFields::Set& requestedField);
122  DatabaseFieldsHashRaw itemDatabaseFieldsRaw(Item* const item, const DatabaseFields::Set& requestedSet);
123 
124  qlonglong imageId(const QModelIndex& anIndex) const;
125  QList<qlonglong> imageIds(const QModelIndexList& indexList) const;
126  QList<ItemInfo> imageInfos(const QModelIndexList& indexList) const;
127  ItemInfo imageInfo(const QModelIndex& index) const;
129 
130  QList<Item*> sortItems(const QList<Item*>& itemList);
131  class LessThan;
132  bool lessThan(Item* const itemA, Item* const itemB);
133  int findChildSortedPosition(Item* const parentItem, Item* const childItem);
134 
135  void scheduleResort();
136  GroupingMode groupingMode() const;
137  void setGroupingMode(const GroupingMode newGroupingMode);
138 
139 public:
140 
141  QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
142  QModelIndex parent(const QModelIndex& childIndex) const override;
143  int rowCount(const QModelIndex& parent) const override;
144  int columnCount(const QModelIndex& i) const override;
145  QVariant data(const QModelIndex& i, int role) const override;
146  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
147  Qt::ItemFlags flags(const QModelIndex& index) const override;
148  bool hasChildren(const QModelIndex& parent = QModelIndex()) const override;
149 
151  Qt::DropActions supportedDropActions() const override;
152  QStringList mimeTypes() const override;
153  bool dropMimeData(const QMimeData* data,
154  Qt::DropAction action,
155  int row,
156  int column,
157  const QModelIndex& parent) override;
158  QMimeData* mimeData(const QModelIndexList& indexes) const override;
159 
160 protected:
161 
162  void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
163 
164 private Q_SLOTS:
165 
166  void slotPopulateModelWithNotifications();
167  void slotPopulateModel(const bool sendNotifications);
168 
169  void slotColumnDataChanged(const qlonglong imageId);
170  void slotColumnAllDataChanged();
171 
172  void slotSourceModelAboutToBeReset();
173  void slotSourceModelReset();
174  void slotSourceRowsAboutToBeInserted(const QModelIndex& parent, int start, int end);
175  void slotSourceRowsInserted(const QModelIndex& parent, int start, int end);
176  void slotSourceRowsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
177  void slotSourceRowsRemoved(const QModelIndex& parent, int start, int end);
178  void slotSourceRowsAboutToBeMoved(const QModelIndex& sourceParent, int sourceStart, int sourceEnd,
179  const QModelIndex& destinationParent, int destinationRow);
180  void slotSourceRowsMoved(const QModelIndex& sourceParent, int sourceStart, int sourceEnd,
181  const QModelIndex& destinationParent, int destinationRow);
182  void slotSourceLayoutAboutToBeChanged();
183  void slotSourceLayoutChanged();
184 
185  void slotDatabaseImageChanged(const ImageChangeset& imageChangeset);
186 
187  void slotFilterSettingsChanged(const ItemFilterSettings& settings);
188  void slotResortModel();
189  void slotClearModel(const bool sendNotifications);
190 
191 public Q_SLOTS:
192 
193  void slotSetActive(const bool isActive);
194 
195 Q_SIGNALS:
196 
198 
199 private:
200 
201  Item* createItemFromSourceIndex(const QModelIndex& imageFilterModelIndex);
202  void addSourceModelIndex(const QModelIndex& imageModelIndex, const bool sendNotifications);
203 
204 private:
205 
206  TableViewShared* const s;
207  class Private;
208  const QScopedPointer<Private> d;
209 };
210 
211 } // namespace Digikam
212 
213 Q_DECLARE_METATYPE(Digikam::TableViewModel::GroupingMode)
214 
215 #endif // DIGIKAM_TABLE_VIEW_MODEL_H
Definition: coredbfields.h:389
Definition: coredbchangesets.h:48
Definition: itemfiltersettings.h:98
Definition: iteminfolist.h:47
Definition: iteminfo.h:78
Definition: tableview_columnfactory.h:50
Definition: tableview_columnfactory.h:81
Definition: tableview_columnfactory.h:324
Definition: tableview_columnfactory.h:196
Definition: tableview_model.h:71
void addChild(Item *const newChild)
Definition: tableview_model.cpp:71
Item * parent
Definition: tableview_model.h:85
Item()
Definition: tableview_model.cpp:59
Item * findChildWithImageId(const qlonglong searchImageId)
Definition: tableview_model.cpp:90
~Item()
Definition: tableview_model.cpp:66
QList< Item * > children
Definition: tableview_model.h:86
void takeChild(Item *const oldChild)
Definition: tableview_model.cpp:85
void insertChild(const int pos, Item *const newChild)
Definition: tableview_model.cpp:78
qlonglong imageId
Definition: tableview_model.h:84
Definition: tableview_model.h:54
QModelIndex fromItemFilterModelIndex(const QModelIndex &imageFilterModelIndex)
Definition: tableview_model.cpp:978
Qt::DropActions supportedDropActions() const override
drag-and-drop related functions
Definition: tableview_model.cpp:1284
QModelIndex toItemFilterModelIndex(const QModelIndex &i) const
Definition: tableview_model.cpp:218
DatabaseFields::Hash< QVariant > DatabaseFieldsHashRaw
Definition: tableview_model.h:66
QVariant data(const QModelIndex &i, int role) const override
Definition: tableview_model.cpp:242
DatabaseFieldsHashRaw itemDatabaseFieldsRaw(Item *const item, const DatabaseFields::Set &requestedSet)
Definition: tableview_model.cpp:1028
Item * itemFromImageId(const qlonglong imageId) const
Definition: tableview_model.cpp:959
QList< ItemInfo > allItemInfo() const
Definition: tableview_model.cpp:1386
QModelIndex toItemModelIndex(const QModelIndex &i) const
Definition: tableview_model.cpp:230
QList< Item * > sortItems(const QList< Item * > &itemList)
Definition: tableview_model.cpp:1178
Item * itemFromIndex(const QModelIndex &i) const
Definition: tableview_model.cpp:964
~TableViewModel() override
Definition: tableview_model.cpp:203
QVariant itemDatabaseFieldRaw(Item *const item, const DatabaseFields::Set &requestedField)
Definition: tableview_model.cpp:1036
QList< TableViewColumn * > getColumnObjects()
Definition: tableview_model.cpp:765
ItemInfo infoFromItem(Item *const item) const
Definition: tableview_model.cpp:1006
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: tableview_model.cpp:751
int deepRowCount() const
Definition: tableview_model.cpp:1494
int rowCount(const QModelIndex &parent) const override
Definition: tableview_model.cpp:313
QModelIndex indexFromImageId(const qlonglong imageId, const int columnIndex) const
Definition: tableview_model.cpp:1060
QList< ItemInfo > imageInfos(const QModelIndexList &indexList) const
Definition: tableview_model.cpp:1105
QModelIndex parent(const QModelIndex &childIndex) const override
Definition: tableview_model.cpp:290
TableViewColumn * getColumnObject(const int columnIndex)
Definition: tableview_model.cpp:458
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
Definition: tableview_model.cpp:1355
QModelIndex itemIndex(Item *const item) const
Definition: tableview_model.cpp:1343
GroupingMode groupingMode() const
Definition: tableview_model.cpp:1391
QModelIndex toCol0(const QModelIndex &anIndex) const
Definition: tableview_model.cpp:1530
QModelIndex deepRowIndex(const int rowNumber) const
Definition: tableview_model.cpp:1407
TableViewModel(TableViewShared *const sharedObject, QObject *const parent=nullptr)
Definition: tableview_model.cpp:138
QStringList mimeTypes() const override
Definition: tableview_model.cpp:1289
QMimeData * mimeData(const QModelIndexList &indexes) const override
Definition: tableview_model.cpp:1256
ItemInfo imageInfo(const QModelIndex &index) const
Definition: tableview_model.cpp:1131
GroupingMode
Definition: tableview_model.h:60
@ GroupingShowSubItems
Definition: tableview_model.h:63
@ GroupingHideGrouped
Definition: tableview_model.h:61
@ GroupingIgnoreGrouping
Definition: tableview_model.h:62
void slotSetActive(const bool isActive)
Definition: tableview_model.cpp:1589
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: tableview_model.cpp:257
void removeColumnAt(const int columnIndex)
Definition: tableview_model.cpp:443
int indexToDeepRowNumber(const QModelIndex &index) const
Definition: tableview_model.cpp:1447
void loadColumnProfile(const TableViewColumnProfile &columnProfile)
Definition: tableview_model.cpp:476
int columnCount(const QModelIndex &i) const override
Definition: tableview_model.cpp:208
ItemInfoList infosFromItems(const QList< Item * > &items) const
Definition: tableview_model.cpp:1016
QList< qlonglong > imageIds(const QModelIndexList &indexList) const
Definition: tableview_model.cpp:1079
void addColumnAt(const TableViewColumnDescription &description, const int targetColumn=-1)
Definition: tableview_model.cpp:349
int findChildSortedPosition(Item *const parentItem, Item *const childItem)
Definition: tableview_model.cpp:1602
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Definition: tableview_model.cpp:1301
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
Definition: tableview_model.cpp:1189
void setGroupingMode(const GroupingMode newGroupingMode)
Definition: tableview_model.cpp:1396
int firstDeepRowNotInList(const QList< QModelIndex > &needleList)
Definition: tableview_model.cpp:1535
QModelIndex fromItemModelIndex(const QModelIndex &imageModelIndex)
Definition: tableview_model.cpp:992
TableViewColumnProfile getColumnProfile() const
Definition: tableview_model.cpp:463
qlonglong imageId(const QModelIndex &anIndex) const
Definition: tableview_model.cpp:1374
bool lessThan(Item *const itemA, Item *const itemB)
Definition: tableview_model.cpp:1219
void scheduleResort()
Definition: tableview_model.cpp:1331
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition: tableview_model.cpp:330
Definition: tableview_shared.h:44
Definition: piwigotalker.h:48
@ Orientation
Definition: coredbfields.h:86
@ LessThan
Definition: coredbsearchxml.h:71
Definition: datefolderview.cpp:43
DropAction
Definition: itemdragdrop.cpp:60