1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2008-04-18
* Description : User interface for searches
*
* SPDX-FileCopyrightText: 2008-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "choicesearchutilities.h"
// Qt includes
#include <QTreeView>
// Local includes
#include "digikam_debug.h"
#include "searchutilities.h"
namespace Digikam
{
ChoiceSearchModel::Entry::Entry(const QVariant& key, const QString& userDisplay)
: m_key (key),
m_display(userDisplay)
{
}
bool ChoiceSearchModel::Entry::operator==(const Entry& other) const
{
return (m_key == other.m_key);
}
ChoiceSearchModel::ChoiceSearchModel(QObject* const parent)
: QAbstractListModel(parent)
{
}
void ChoiceSearchModel::setChoice(const QMap<int, QString>& data)<--- Shadow argument
{
if (m_entries.size())
{
beginResetModel();
m_entries.clear();
endResetModel();
}
for (QMap<int, QString>::const_iterator it = data.constBegin() ; it != data.constEnd() ; ++it)
{
m_entries << Entry(it.key(), it.value());
}
}
void ChoiceSearchModel::setChoice(const QVariantList& data)<--- Shadow argument
{
if (m_entries.size())
{
beginResetModel();
m_entries.clear();
endResetModel();
}
Q_ASSERT(data.size() % 2 == 0);
for (QVariantList::const_iterator it = data.constBegin() ; it != data.constEnd() ; )
{
QVariant key = *it;
++it;
QString value = (*it).toString();
++it;
m_entries << Entry(key, value);
}
}
void ChoiceSearchModel::setChoice(const QStringList& data)<--- Shadow argument
{
if (m_entries.size())
{
beginResetModel();
m_entries.clear();
endResetModel();
}
Q_ASSERT(data.size() % 2 == 0);
for (QStringList::const_iterator it = data.constBegin() ; it != data.constEnd() ; )
{
QVariant key = *it;
++it;
QString value = *it;
++it;
m_entries << Entry(key, value);
}
}
QVariantList ChoiceSearchModel::checkedKeys() const
{
QVariantList list;
for (QList<Entry>::const_iterator it = m_entries.constBegin() ; it != m_entries.constEnd() ; ++it)
{
if ((*it).m_checkState)
{
list << (*it).m_key;
}
}
return list;
}
QStringList ChoiceSearchModel::checkedDisplayTexts() const
{
QStringList list;
for (QList<Entry>::const_iterator it = m_entries.constBegin() ; it != m_entries.constEnd() ; ++it)
{
if ((*it).m_checkState)
{
list << (*it).m_display;
}
}
return list;
}
void ChoiceSearchModel::setChecked(int i, bool checked)
{
m_entries[i].m_checkState = checked;
QModelIndex modelIndex = index(i);
Q_EMIT dataChanged(modelIndex, modelIndex);
Q_EMIT checkStateChanged(m_entries.at(i).m_key, checked);
}
void ChoiceSearchModel::resetChecked()
{
for (int i = 0 ; i < m_entries.size() ; ++i)
{
if (m_entries.at(i).m_checkState)
{
setChecked(i, false);
}
}
}
int ChoiceSearchModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid())
{
return 0;
}
return m_entries.count();
}
QVariant ChoiceSearchModel::data(const QModelIndex& index, int role) const<--- Shadow argument
{
if (index.isValid())
{
if (role == Qt::DisplayRole)
{
return m_entries.at(index.row()).m_display;
}
else if (role == Qt::CheckStateRole)
{
return m_entries.at(index.row()).m_checkState ? Qt::Checked : Qt::Unchecked;
}
else if (role == IdRole)
{
return m_entries.at(index.row()).m_key;
}
}
return QVariant();
}
QModelIndex ChoiceSearchModel::index(int row, int column, const QModelIndex& parent) const
{
if (parent.isValid() || (column != 0) || (row >= m_entries.size()))
{
return QModelIndex();
}
return createIndex(row, 0);
}
Qt::ItemFlags ChoiceSearchModel::flags(const QModelIndex& index) const<--- Shadow argument
{
return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable;
}
bool ChoiceSearchModel::setData(const QModelIndex& index, const QVariant& value, int role)<--- Shadow argument
{
if (role == Qt::CheckStateRole)
{
Qt::CheckState state = (Qt::CheckState)value.toInt();
setChecked(index.row(), state == Qt::Checked);
return true;
}
else
{
return QAbstractListModel::setData(index, value, role);
}
}
// --------------------------------------------------------------------------------------
ChoiceSearchComboBox::ChoiceSearchComboBox(QWidget* const parent)
: ListViewComboBox(parent)
{
}
void ChoiceSearchComboBox::setSearchModel(ChoiceSearchModel* model)<--- Shadow argument
{
ModelIndexBasedComboBox::setModel(model);
installView();
}
ChoiceSearchModel* ChoiceSearchComboBox::model() const
{
return static_cast<ChoiceSearchModel*>(ListViewComboBox::model());
}
DSqueezedClickLabel* ChoiceSearchComboBox::label() const
{
return m_label;
}
void ChoiceSearchComboBox::setLabelText(const QString& text)
{
m_label->setAdjustedText(text);
}
void ChoiceSearchComboBox::labelClicked()
{
qCDebug(DIGIKAM_GENERAL_LOG) << "labelClicked";
showPopup();
}
void ChoiceSearchComboBox::installView(QAbstractItemView* v)
{
// make protected again
ListViewComboBox::installView(v);
/*
view()->setHeaderHidden(true);
*/
view()->setAlternatingRowColors(true);
// create the label
m_label = new DSqueezedClickLabel;
if (layoutDirection() == Qt::RightToLeft)
{
m_label->setElideMode(Qt::ElideRight);
}
else
{
m_label->setElideMode(Qt::ElideLeft);
}
// set a line edit that carries the label
ProxyClickLineEdit* const lineEdit = new ProxyClickLineEdit;
lineEdit->setCursor(m_label->cursor());
lineEdit->setWidget(m_label);
setLineEdit(lineEdit);
// connect clicks on upper area (both line edit and widget within) to showPopup
connect(lineEdit, SIGNAL(leftClicked()),
this, SLOT(labelClicked()));
connect(m_label, SIGNAL(activated()),
this, SLOT(labelClicked()));
}
} // namespace Digikam
#include "moc_choicesearchutilities.cpp"
|