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
|
/***************************************************************************
* Copyright (C) 2009 by Adam Kidder <[email protected]> *
* Copyright (C) 2009 by Peter Penz <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
#include "searchcriterionselector.h"
#define DISABLE_NEPOMUK_LEGACY
#include <nepomuk/comparisonterm.h>
#include <nepomuk/literalterm.h>
#include <nepomuk/query.h>
#include "nie.h"
#include "searchcriterionvalue.h"
#include <Soprano/LiteralValue>
#include <Soprano/Vocabulary/NAO>
#include <QComboBox>
#include <QHBoxLayout>
#include <QList>
#include <QPushButton>
#include <kicon.h>
#include <klocale.h>
SearchCriterionSelector::SearchCriterionSelector(Type type, QWidget* parent) :
QWidget(parent),
m_layout(0),
m_descriptionsBox(0),
m_comparatorBox(0),
m_valueWidget(0),
m_removeButton(0),
m_descriptions()
{
m_descriptionsBox = new QComboBox(this);
m_comparatorBox = new QComboBox(this);
m_comparatorBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
createDescriptions();
const int index = static_cast<int>(type);
m_descriptionsBox->setCurrentIndex(index);
QWidget* filler = new QWidget(this);
filler->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_removeButton = new QPushButton(this);
m_removeButton->setIcon(KIcon("list-remove"));
m_removeButton->setToolTip(i18nc("@info", "Remove search option"));
m_removeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(m_removeButton, SIGNAL(clicked()), this, SIGNAL(removeCriterion()));
m_layout = new QHBoxLayout(this);
m_layout->setMargin(0);
m_layout->addWidget(m_removeButton);
m_layout->addWidget(m_descriptionsBox);
m_layout->addWidget(m_comparatorBox);
m_layout->addWidget(filler);
setLayout(m_layout);
slotDescriptionChanged(index);
connect(m_descriptionsBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDescriptionChanged(int)));
}
SearchCriterionSelector::~SearchCriterionSelector()
{
}
Nepomuk::Query::Term SearchCriterionSelector::queryTerm() const
{
if (m_valueWidget == 0) {
return Nepomuk::Query::Term();
}
const int descIndex = m_descriptionsBox->currentIndex();
const SearchCriterionDescription& descr = m_descriptions[descIndex];
const int compIndex = m_comparatorBox->currentIndex();
const SearchCriterionDescription::Comparator& comp = descr.comparators()[compIndex];
if (!comp.isActive) {
return Nepomuk::Query::Term();
}
const Nepomuk::Query::ComparisonTerm term(descr.identifier(),
m_valueWidget->value(),
comp.value);
return term;
}
SearchCriterionSelector::Type SearchCriterionSelector::type() const
{
return static_cast<Type>(m_descriptionsBox->currentIndex());
}
void SearchCriterionSelector::slotDescriptionChanged(int index)
{
if (m_valueWidget != 0) {
m_valueWidget->hide();
m_layout->removeWidget(m_valueWidget);
m_valueWidget = 0;
// the value widget is obtained by the Search Criterion
// Selector instance and may not get deleted
}
// add comparator items
disconnect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
m_comparatorBox->clear();
const SearchCriterionDescription& description = m_descriptions[index];
foreach (const SearchCriterionDescription::Comparator& comp, description.comparators()) {
m_comparatorBox->addItem(comp.name);
}
// add value widget
m_valueWidget = description.valueWidget();
m_layout->insertWidget(3, m_valueWidget);
m_comparatorBox->setCurrentIndex(0);
slotComparatorChanged(0);
connect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
}
void SearchCriterionSelector::slotComparatorChanged(int index)
{
Q_ASSERT(index >= 0);
// only show the value widget if an operation is defined by the comparator
const int descIndex = m_descriptionsBox->currentIndex();
const SearchCriterionDescription& descr = m_descriptions[descIndex];
const SearchCriterionDescription::Comparator& comp = descr.comparators()[index];
m_valueWidget->initializeValue(comp.autoValueType);
// only show the value widget, if an operation is defined
// and no automatic calculation is provided
m_valueWidget->setVisible(comp.isActive && comp.autoValueType.isEmpty());
emit criterionChanged();
}
void SearchCriterionSelector::createDescriptions()
{
Q_ASSERT(m_descriptionsBox != 0);
Q_ASSERT(m_comparatorBox != 0);
// TODO: maybe this creation should be forwarded to a factory if
// the number of items increases in future
QList<SearchCriterionDescription::Comparator> defaultComps;
defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than"), Nepomuk::Query::ComparisonTerm::Greater));
defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than or Equal to"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual));
defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than"), Nepomuk::Query::ComparisonTerm::Smaller));
defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than or Equal to"), Nepomuk::Query::ComparisonTerm::SmallerOrEqual));
// add "Date" description
QList<SearchCriterionDescription::Comparator> dateComps;
dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Anytime")));
dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Today"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual, "today"));
dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Week"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual, "thisWeek"));
dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Month"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual, "thisMonth"));
dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Year"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual, "thisYear"));
foreach (const SearchCriterionDescription::Comparator& comp, defaultComps) {
dateComps.append(comp);
}
DateValue* dateValue = new DateValue(this);
dateValue->hide();
SearchCriterionDescription date(i18nc("@label", "Date:"),
Nepomuk::Vocabulary::NIE::lastModified(),
dateComps,
dateValue);
Q_ASSERT(static_cast<int>(SearchCriterionSelector::Date) == 0);
m_descriptions.append(date);
// add "Size" description
QList<SearchCriterionDescription::Comparator> sizeComps = defaultComps;
sizeComps.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (file size)", "Any")));
SizeValue* sizeValue = new SizeValue(this);
sizeValue->hide();
SearchCriterionDescription size(i18nc("@label", "Size:"),
Soprano::Vocabulary::NAO::lastModified(), // TODO
sizeComps,
sizeValue);
Q_ASSERT(static_cast<int>(SearchCriterionSelector::Size) == 1);
m_descriptions.append(size);
// add "Tag" description
QList<SearchCriterionDescription::Comparator> tagComps;
tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label All (tags)", "All")));
tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Equal to"), Nepomuk::Query::ComparisonTerm::Equal));
// It is unclear yet how to express != for tags with the new Nepomuk query API. Disable it for KDE 4.4,
// but leave the translation string there to be able to enable this functionality for KDE 4.4.x:
const QString dummy = i18nc("@label", "Not Equal to");
Q_UNUSED(dummy);
//tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Not Equal to"), Nepomuk::Query::ComparisonTerm::Equal)); // TODO
TagValue* tagValue = new TagValue(this);
tagValue->hide();
SearchCriterionDescription tag(i18nc("@label", "Tag:"),
Soprano::Vocabulary::NAO::hasTag(),
tagComps,
tagValue);
Q_ASSERT(static_cast<int>(SearchCriterionSelector::Tag) == 2);
m_descriptions.append(tag);
// add "Rating" description
QList<SearchCriterionDescription::Comparator> ratingComps = defaultComps;
ratingComps.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (rating)", "Any")));
RatingValue* ratingValue = new RatingValue(this);
ratingValue->hide();
SearchCriterionDescription rating(i18nc("@label", "Rating:"),
Soprano::Vocabulary::NAO::numericRating(),
ratingComps,
ratingValue);
Q_ASSERT(static_cast<int>(SearchCriterionSelector::Rating) == 3);
m_descriptions.append(rating);
// add all descriptions to the combo box and connect the value widgets
int i = 0;
foreach (const SearchCriterionDescription& desc, m_descriptions) {
m_descriptionsBox->addItem(desc.name(), i);
connect(desc.valueWidget(), SIGNAL(valueChanged(Nepomuk::Query::LiteralTerm)), this, SIGNAL(criterionChanged()));
++i;
}
}
#include "searchcriterionselector.moc"
|