digiKam
tableview_columnfactory.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 : Table view column helpers
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_COLUMNFACTORY_H
25 #define DIGIKAM_TABLE_VIEW_COLUMNFACTORY_H
26 
27 // Qt includes
28 
29 #include <QObject>
30 #include <QHash>
31 #include <QWidget>
32 
33 // Local includes
34 
35 #include "tableview_model.h"
36 #include "tableview_shared.h"
37 
38 class QModelIndex;
39 class QStyleOptionViewItem;
40 
41 class KConfigGroup;
42 
43 namespace Digikam
44 {
45 
46 class ItemInfo;
47 class ThumbnailSize;
48 
50 {
51 public:
52 
53  explicit TableViewColumnConfiguration(const QString& id = QString())
54  : columnId(id),
56  {
57  }
58 
59  QString getSetting(const QString& key, const QString& defaultValue = QString()) const
60  {
61  if (!columnSettings.contains(key))
62  {
63  return defaultValue;
64  }
65 
66  return columnSettings.value(key);
67  }
68 
69  void loadSettings(const KConfigGroup& configGroup);
70  void saveSettings(KConfigGroup& configGroup) const;
71 
72 public:
73 
74  QString columnId;
75  QHash<QString, QString> columnSettings;
76 };
77 
78 // ----------------------------------------------------------------------------
79 
81 {
82 public:
83 
85  : columnId(),
86  columnTitle(),
87  columnIcon(),
89  subColumns()
90  {
91  }
92 
93  explicit TableViewColumnDescription(const QString& id, const QString& title,
94  const QString& settingKey = QString(),
95  const QString& settingValue = QString())
96  : columnId(id),
97  columnTitle(title),
98  columnIcon(),
100  subColumns()
101  {
102  if (!settingKey.isEmpty())
103  {
104  addSetting(settingKey, settingValue);
105  }
106  }
107 
108 public:
109 
111 
112  QString columnId;
113  QString columnTitle;
114  QString columnIcon;
115  QHash<QString, QString> columnSettings;
117 
118 public:
119 
120  void addSubColumn(const TableViewColumnDescription& subColumnDescription)
121  {
122  subColumns << subColumnDescription;
123  }
124 
125  void addSetting(const QString& key, const QString& value)
126  {
127  columnSettings.insert(key, value);
128  }
129 
131  {
132  TableViewColumnConfiguration configuration;
133 
134  configuration.columnId = columnId;
135  configuration.columnSettings = columnSettings;
136 
137  return configuration;
138  }
139 
140  TableViewColumnDescription setIcon(const QString& iconName)
141  {
142  columnIcon = iconName;
143 
144  return *this;
145  }
146 
147  static bool FindInListById(const TableViewColumnDescription::List& listToSearch, const QString& targetId, TableViewColumnDescription* const resultDescription)
148  {
149  TableViewColumnDescription::List leftToSearch = listToSearch;
150 
151  while (!leftToSearch.isEmpty())
152  {
153  const TableViewColumnDescription desc = leftToSearch.takeFirst();
154 
155  if (desc.columnId == targetId)
156  {
157  *resultDescription = desc;
158 
159  return true;
160  }
161 
162  if (!desc.subColumns.isEmpty())
163  {
164  leftToSearch << desc.subColumns;
165  }
166  }
167 
168  return false;
169  }
170 };
171 
172 // ----------------------------------------------------------------------------
173 
175 {
176  Q_OBJECT
177 
178 public:
179 
180  explicit TableViewColumnConfigurationWidget(TableViewShared* const sharedObject,
181  const TableViewColumnConfiguration& currentConfiguration,
182  QWidget* const parent = nullptr);
184 
186 
187 public:
188 
191 };
192 
193 // ----------------------------------------------------------------------------
194 
195 class TableViewColumn : public QObject
196 {
197  Q_OBJECT
198 
199 protected:
200 
203 
204 public:
205 
207  {
212  };
213  Q_DECLARE_FLAGS(ColumnFlags, ColumnFlag)
214 
216  {
217  CmpEqual = 0,
219  CmpALessB = 2
220  };
221 
222 public:
223 
224  explicit TableViewColumn(TableViewShared* const tableViewShared,
225  const TableViewColumnConfiguration& pConfiguration,
226  QObject* const parent = nullptr);
227  ~TableViewColumn() override;
228 
229 public:
230 
232  virtual void setConfiguration(const TableViewColumnConfiguration& newConfiguration);
233  virtual TableViewColumnConfigurationWidget* getConfigurationWidget(QWidget* const parentWidget) const;
234  virtual ColumnFlags getColumnFlags() const;
235  virtual QString getTitle() const = 0;
236 
237  virtual QVariant data(TableViewModel::Item* const item, const int role) const;
238  virtual ColumnCompareResult compare(TableViewModel::Item* const itemA, TableViewModel::Item* const itemB) const;
239  virtual bool columnAffectedByChangeset(const ImageChangeset& imageChangeset) const;
240  virtual bool paint(QPainter* const painter, const QStyleOptionViewItem& option, TableViewModel::Item* const item) const;
241  virtual QSize sizeHint(const QStyleOptionViewItem& option, TableViewModel::Item* const item) const;
242  virtual void updateThumbnailSize();
243 
244 public:
245 
247  static bool compareHelperBoolFailCheck(const bool okA, const bool okB, ColumnCompareResult* const result);
248 
249  template<class MyType>
250  static ColumnCompareResult compareHelper(const MyType& A, const MyType& B)
251  {
252  if (A == B)
253  {
254  return CmpEqual;
255  }
256  else if (A > B)
257  {
258  return CmpABiggerB;
259  }
260 
261  return CmpALessB;
262  }
263 
264  template<typename columnClass>
265  static bool getSubColumnIndex(const QString& subColumnId, typename columnClass::SubColumn* const subColumn)
266  {
267  const int index = columnClass::getSubColumns().indexOf(subColumnId);
268 
269  if (index < 0)
270  {
271  return false;
272  }
273 
274  *subColumn = typename columnClass::SubColumn(index);
275 
276  return true;
277  }
278 
279  template<typename columnClass>
280  static bool CreateFromConfiguration(TableViewShared* const tableViewShared,
281  const TableViewColumnConfiguration& pConfiguration,
282  TableViewColumn** const pNewColumn,
283  QObject* const parent)
284  {
285  typename columnClass::SubColumn subColumn;
286 
287  if (!getSubColumnIndex<columnClass>(pConfiguration.columnId, &subColumn))
288  {
289  return false;
290  }
291 
292  *pNewColumn = new columnClass(tableViewShared, pConfiguration, subColumn, parent);
293 
294  return true;
295  }
296 
297 Q_SIGNALS:
298 
299  void signalDataChanged(const qlonglong imageId);
301 };
302 
303 // ----------------------------------------------------------------------------
304 
305 class TableViewColumnFactory : public QObject
306 {
307  Q_OBJECT
308 
309 public:
310 
311  explicit TableViewColumnFactory(TableViewShared* const tableViewShared, QObject* const parent = nullptr);
312 
314  TableViewColumn* getColumn(const TableViewColumnConfiguration& columnConfiguration);
315 
316 private:
317 
318  TableViewShared* const s;
319 };
320 
321 // ----------------------------------------------------------------------------
322 
324 {
325 public:
326 
329 
330  void loadSettings(const KConfigGroup& configGroup);
331  void saveSettings(KConfigGroup& configGroup);
332 
333 public:
334 
336  QString name;
337  QByteArray headerState;
338 };
339 
340 } // namespace Digikam
341 
342 Q_DECLARE_METATYPE(Digikam::TableViewColumnDescription)
343 
344 Q_DECLARE_OPERATORS_FOR_FLAGS(Digikam::TableViewColumn::ColumnFlags)
345 
346 #endif // DIGIKAM_TABLE_VIEW_COLUMNFACTORY_H
Definition: coredbchangesets.h:48
Definition: tableview_columnfactory.h:175
~TableViewColumnConfigurationWidget() override
Definition: tableview_columnfactory.cpp:353
TableViewColumnConfigurationWidget(TableViewShared *const sharedObject, const TableViewColumnConfiguration &currentConfiguration, QWidget *const parent=nullptr)
Definition: tableview_columnfactory.cpp:344
virtual TableViewColumnConfiguration getNewConfiguration()=0
TableViewShared *const s
Definition: tableview_columnfactory.h:189
TableViewColumnConfiguration configuration
Definition: tableview_columnfactory.h:190
Definition: tableview_columnfactory.h:50
QString getSetting(const QString &key, const QString &defaultValue=QString()) const
Definition: tableview_columnfactory.h:59
QHash< QString, QString > columnSettings
Definition: tableview_columnfactory.h:75
void saveSettings(KConfigGroup &configGroup) const
Definition: tableview_columnfactory.cpp:325
void loadSettings(const KConfigGroup &configGroup)
Definition: tableview_columnfactory.cpp:308
QString columnId
Definition: tableview_columnfactory.h:74
TableViewColumnConfiguration(const QString &id=QString())
Definition: tableview_columnfactory.h:53
Definition: tableview_columnfactory.h:81
void addSetting(const QString &key, const QString &value)
Definition: tableview_columnfactory.h:125
QString columnIcon
Definition: tableview_columnfactory.h:114
TableViewColumnConfiguration toConfiguration() const
Definition: tableview_columnfactory.h:130
static bool FindInListById(const TableViewColumnDescription::List &listToSearch, const QString &targetId, TableViewColumnDescription *const resultDescription)
Definition: tableview_columnfactory.h:147
TableViewColumnDescription()
Definition: tableview_columnfactory.h:84
QList< TableViewColumnDescription > subColumns
Definition: tableview_columnfactory.h:116
TableViewColumnDescription setIcon(const QString &iconName)
Definition: tableview_columnfactory.h:140
TableViewColumnDescription(const QString &id, const QString &title, const QString &settingKey=QString(), const QString &settingValue=QString())
Definition: tableview_columnfactory.h:93
QHash< QString, QString > columnSettings
Definition: tableview_columnfactory.h:115
QList< TableViewColumnDescription > List
Definition: tableview_columnfactory.h:110
void addSubColumn(const TableViewColumnDescription &subColumnDescription)
Definition: tableview_columnfactory.h:120
QString columnTitle
Definition: tableview_columnfactory.h:113
QString columnId
Definition: tableview_columnfactory.h:112
Definition: tableview_columnfactory.h:306
TableViewColumn * getColumn(const TableViewColumnConfiguration &columnConfiguration)
Definition: tableview_columnfactory.cpp:62
static QList< TableViewColumnDescription > getColumnDescriptionList()
Definition: tableview_columnfactory.cpp:104
TableViewColumnFactory(TableViewShared *const tableViewShared, QObject *const parent=nullptr)
Definition: tableview_columnfactory.cpp:55
Definition: tableview_columnfactory.h:324
QByteArray headerState
Definition: tableview_columnfactory.h:337
void saveSettings(KConfigGroup &configGroup)
Definition: tableview_columnfactory.cpp:290
~TableViewColumnProfile()
Definition: tableview_columnfactory.cpp:223
QList< TableViewColumnConfiguration > columnConfigurationList
Definition: tableview_columnfactory.h:335
QString name
Definition: tableview_columnfactory.h:336
TableViewColumnProfile()
Definition: tableview_columnfactory.cpp:219
void loadSettings(const KConfigGroup &configGroup)
Definition: tableview_columnfactory.cpp:227
Definition: tableview_columnfactory.h:196
ColumnCompareResult
Definition: tableview_columnfactory.h:216
@ CmpABiggerB
Definition: tableview_columnfactory.h:218
@ CmpEqual
Definition: tableview_columnfactory.h:217
@ CmpALessB
Definition: tableview_columnfactory.h:219
virtual QVariant data(TableViewModel::Item *const item, const int role) const
Definition: tableview_columnfactory.cpp:138
TableViewColumnConfiguration configuration
Definition: tableview_columnfactory.h:202
static bool getSubColumnIndex(const QString &subColumnId, typename columnClass::SubColumn *const subColumn)
Definition: tableview_columnfactory.h:265
virtual QSize sizeHint(const QStyleOptionViewItem &option, TableViewModel::Item *const item) const
Definition: tableview_columnfactory.cpp:130
static bool compareHelperBoolFailCheck(const bool okA, const bool okB, ColumnCompareResult *const result)
Definition: tableview_columnfactory.cpp:186
static bool CreateFromConfiguration(TableViewShared *const tableViewShared, const TableViewColumnConfiguration &pConfiguration, TableViewColumn **const pNewColumn, QObject *const parent)
Definition: tableview_columnfactory.h:280
virtual ColumnCompareResult compare(TableViewModel::Item *const itemA, TableViewModel::Item *const itemB) const
Definition: tableview_columnfactory.cpp:160
virtual TableViewColumnConfigurationWidget * getConfigurationWidget(QWidget *const parentWidget) const
Definition: tableview_columnfactory.cpp:170
static TableViewColumnDescription getDescription()
static ColumnCompareResult compareHelper(const MyType &A, const MyType &B)
Definition: tableview_columnfactory.h:250
TableViewShared *const s
Definition: tableview_columnfactory.h:201
~TableViewColumn() override
Definition: tableview_columnfactory.cpp:49
virtual void setConfiguration(const TableViewColumnConfiguration &newConfiguration)
Definition: tableview_columnfactory.cpp:177
TableViewColumn(TableViewShared *const tableViewShared, const TableViewColumnConfiguration &pConfiguration, QObject *const parent=nullptr)
Definition: tableview_columnfactory.cpp:40
virtual bool columnAffectedByChangeset(const ImageChangeset &imageChangeset) const
Definition: tableview_columnfactory.cpp:212
virtual void updateThumbnailSize()
Definition: tableview_columnfactory.cpp:182
void signalDataChanged(const qlonglong imageId)
virtual QString getTitle() const =0
virtual TableViewColumnConfiguration getConfiguration() const
Definition: tableview_columnfactory.cpp:151
ColumnFlag
Definition: tableview_columnfactory.h:207
@ ColumnHasConfigurationWidget
Definition: tableview_columnfactory.h:211
@ ColumnCustomPainting
Definition: tableview_columnfactory.h:209
@ ColumnNoFlags
Definition: tableview_columnfactory.h:208
@ ColumnCustomSorting
Definition: tableview_columnfactory.h:210
virtual bool paint(QPainter *const painter, const QStyleOptionViewItem &option, TableViewModel::Item *const item) const
Definition: tableview_columnfactory.cpp:121
virtual ColumnFlags getColumnFlags() const
Definition: tableview_columnfactory.cpp:146
Definition: tableview_model.h:71
Definition: tableview_shared.h:44
Definition: piwigotalker.h:48
qulonglong value
Definition: itemviewutilities.cpp:592
Definition: datefolderview.cpp:43