digiKam
choicesearchutilities.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 : 2008-04-18
7  * Description : User interface for searches
8  *
9  * Copyright (C) 2008-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * 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_CHOICE_SEARCH_UTILITIES_H
25 #define DIGIKAM_CHOICE_SEARCH_UTILITIES_H
26 
27 // Qt includes
28 
29 #include <QAbstractListModel>
30 #include <QList>
31 #include <QVariant>
32 
33 // Local includes
34 
35 #include "dexpanderbox.h"
36 #include "coredbsearchxml.h"
37 #include "comboboxutilities.h"
38 
39 namespace Digikam
40 {
41 
42 class ChoiceSearchModel : public QAbstractListModel
43 {
44  Q_OBJECT
45 
46 public:
47 
49  {
50  IdRole = Qt::UserRole
51  };
52 
53 public:
54 
55  explicit ChoiceSearchModel(QObject* const parent = nullptr);
56 
60  void setChoice(const QMap<int, QString>& data);
61 
67  void setChoice(const QVariantList& data);
68 
73  void setChoice(const QStringList& data);
74 
78  QVariantList checkedKeys() const;
79 
84  template <typename T> QList<T> checkedKeys() const;
85 
89  QStringList checkedDisplayTexts() const;
90 
94  template <typename T> void setChecked(const T& key, bool checked = true);
95 
99  template <typename T> void setChecked(const QList<T>& keys, bool checked = true);
100 
107  template <typename T> void setChecked(const T& value, SearchXml::Relation relation);
108 
112  void resetChecked();
113 
114  int rowCount(const QModelIndex& parent) const override;
115  QVariant data(const QModelIndex& index, int role) const override;
116  QModelIndex index(int row, int column = 0, const QModelIndex& parent = QModelIndex()) const override;
117  Qt::ItemFlags flags(const QModelIndex& index) const override;
118  bool setData(const QModelIndex& index, const QVariant& value, int role) override;
119 
120 Q_SIGNALS:
121 
122  void checkStateChanged(const QVariant& key, bool isChecked);
123 
124 protected:
125 
126  void setChecked(int index, bool checked);
127 
128 protected:
129 
130  class Entry
131  {
132  public:
133 
134  Entry();
135  Entry(const QVariant& key, const QString& userDisplay);
136 
137  bool operator==(const Entry& other) const;
138 
140  bool operator==(const QVariant& other) const;
141 
142  public:
143 
144  QVariant m_key;
145  QString m_display;
147  };
148 
149 protected:
150 
152 };
153 
154 template <typename T> void ChoiceSearchModel::setChecked(const T& key, bool checked)
155 {
156  QVariant variantKey(key);
157 
158  for (int i = 0 ; i < m_entries.size() ; ++i)
159  {
160  if (m_entries[i].m_key == variantKey)
161  {
162  setChecked(i, checked);
163  }
164  }
165 }
166 
167 template <typename T> void ChoiceSearchModel::setChecked(const T& value, SearchXml::Relation relation)
168 {
169  for (int i = 0 ; i < m_entries.size() ; ++i)
170  {
171  setChecked(i, SearchXml::testRelation(m_entries.at(i).m_key.value<T>(), value, relation));
172  }
173 }
174 
175 template <typename T> void ChoiceSearchModel::setChecked(const QList<T>& keys, bool checked)
176 {
177  foreach (T key, keys)
178  {
179  setChecked(key, checked);
180  }
181 }
182 
183 template <typename T> QList<T> ChoiceSearchModel::checkedKeys() const
184 {
185  QList<T> list;
186 
187  for (QList<Entry>::const_iterator it = m_entries.begin() ; it != m_entries.end() ; ++it)
188  {
189  if ((*it).m_checkState)
190  {
191  list << (*it).m_key.value<T>();
192  }
193  }
194 
195  return list;
196 }
197 
198 // -------------------------------------------------------------------------------------
199 
201 {
202  Q_OBJECT
203 
204 public:
205 
212  explicit ChoiceSearchComboBox(QWidget* const parent = nullptr);
213 
219 
223  void setLabelText(const QString& text);
224 
225  ChoiceSearchModel* model() const;
226  DSqueezedClickLabel* label() const;
227 
228 Q_SIGNALS:
229 
231 
232 protected Q_SLOTS:
233 
234  void labelClicked();
235 
236 protected:
237 
238  void installView(QAbstractItemView* view = nullptr) override;
239 
240 protected:
241 
243 };
244 
245 } // namespace Digikam
246 
247 #endif // DIGIKAM_CHOICE_SEARCH_UTILITIES_H
Definition: choicesearchutilities.h:201
ChoiceSearchModel * model() const
Definition: choicesearchutilities.cpp:242
void setLabelText(const QString &text)
Definition: choicesearchutilities.cpp:252
DSqueezedClickLabel * m_label
Definition: choicesearchutilities.h:242
void setSearchModel(ChoiceSearchModel *model)
Definition: choicesearchutilities.cpp:236
ChoiceSearchComboBox(QWidget *const parent=nullptr)
Definition: choicesearchutilities.cpp:230
DSqueezedClickLabel * label() const
Definition: choicesearchutilities.cpp:247
void installView(QAbstractItemView *view=nullptr) override
Definition: choicesearchutilities.cpp:263
void labelClicked()
Definition: choicesearchutilities.cpp:257
Definition: choicesearchutilities.h:131
bool m_checkState
Definition: choicesearchutilities.h:146
bool operator==(const QVariant &other) const
Entry()
Definition: choicesearchutilities.cpp:38
bool operator==(const Entry &other) const
Definition: choicesearchutilities.cpp:50
QString m_display
Definition: choicesearchutilities.h:145
QVariant m_key
Definition: choicesearchutilities.h:144
Definition: choicesearchutilities.h:43
void setChoice(const QMap< int, QString > &data)
Definition: choicesearchutilities.cpp:60
void checkStateChanged(const QVariant &key, bool isChecked)
QVariant data(const QModelIndex &index, int role) const override
Definition: choicesearchutilities.cpp:177
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Definition: choicesearchutilities.cpp:213
QStringList checkedDisplayTexts() const
Definition: choicesearchutilities.cpp:132
QModelIndex index(int row, int column=0, const QModelIndex &parent=QModelIndex()) const override
Definition: choicesearchutilities.cpp:198
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: choicesearchutilities.cpp:208
ChoiceSearchModel(QObject *const parent=nullptr)
Definition: choicesearchutilities.cpp:55
void resetChecked()
Definition: choicesearchutilities.cpp:156
QVariantList checkedKeys() const
Definition: choicesearchutilities.cpp:117
void setChecked(const T &key, bool checked=true)
Definition: choicesearchutilities.h:154
CustomRoles
Definition: choicesearchutilities.h:49
@ IdRole
Definition: choicesearchutilities.h:50
QList< Entry > m_entries
Definition: choicesearchutilities.h:151
int rowCount(const QModelIndex &parent) const override
Definition: choicesearchutilities.cpp:167
Definition: dexpanderbox.h:132
Definition: comboboxutilities.h:238
QListView * view() const
Definition: comboboxutilities.cpp:439
Definition: piwigotalker.h:48
qulonglong value
Definition: itemviewutilities.cpp:592
#define T
Relation
Definition: coredbsearchxml.h:66
bool testRelation(T v1, T v2, Relation rel)
Definition: coredbsearchxml.h:86
Definition: datefolderview.cpp:43