]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/searchcriterionselector.cpp
Patches to make kdebase compile with current animation code.
[dolphin.git] / src / search / searchcriterionselector.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Adam Kidder <thekidder@gmail.com> *
3 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "searchcriterionselector.h"
22
23 #define DISABLE_NEPOMUK_LEGACY
24 #include <nepomuk/comparisonterm.h>
25 #include <nepomuk/literalterm.h>
26 #include <nepomuk/query.h>
27
28 #include "searchcriterionvalue.h"
29
30 #include <Soprano/LiteralValue>
31 #include <Soprano/Vocabulary/NAO>
32
33 #include <QComboBox>
34 #include <QHBoxLayout>
35 #include <QList>
36 #include <QPushButton>
37
38 #include <kicon.h>
39 #include <klocale.h>
40
41 #include <kdebug.h>
42
43 SearchCriterionSelector::SearchCriterionSelector(Type type, QWidget* parent) :
44 QWidget(parent),
45 m_layout(0),
46 m_descriptionsBox(0),
47 m_comparatorBox(0),
48 m_valueWidget(0),
49 m_removeButton(0),
50 m_descriptions()
51 {
52 m_descriptionsBox = new QComboBox(this);
53
54 m_comparatorBox = new QComboBox(this);
55 m_comparatorBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
56
57 createDescriptions();
58 const int index = static_cast<int>(type);
59 m_descriptionsBox->setCurrentIndex(index);
60
61 QWidget* filler = new QWidget(this);
62 filler->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
63
64 m_removeButton = new QPushButton(this);
65 m_removeButton->setIcon(KIcon("list-remove"));
66 m_removeButton->setToolTip(i18nc("@info", "Remove search option"));
67 m_removeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
68 connect(m_removeButton, SIGNAL(clicked()), this, SIGNAL(removeCriterion()));
69
70 m_layout = new QHBoxLayout(this);
71 m_layout->setMargin(0);
72 m_layout->addWidget(m_removeButton);
73 m_layout->addWidget(m_descriptionsBox);
74 m_layout->addWidget(m_comparatorBox);
75 m_layout->addWidget(filler);
76
77 setLayout(m_layout);
78
79 slotDescriptionChanged(index);
80 connect(m_descriptionsBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDescriptionChanged(int)));
81 }
82
83 SearchCriterionSelector::~SearchCriterionSelector()
84 {
85 }
86
87 Nepomuk::Query::Term SearchCriterionSelector::queryTerm() const
88 {
89 if (m_valueWidget == 0) {
90 return Nepomuk::Query::Term();
91 }
92
93 const int descIndex = m_descriptionsBox->currentIndex();
94 const SearchCriterionDescription& descr = m_descriptions[descIndex];
95
96 const int compIndex = m_comparatorBox->currentIndex();
97 const SearchCriterionDescription::Comparator& comp = descr.comparators()[compIndex];
98 if (!comp.isActive) {
99 return Nepomuk::Query::Term();
100 }
101
102 kDebug() << "identifier:" << descr.identifier();
103 kDebug() << "value:" << m_valueWidget->value();
104 kDebug() << "comp:" << comp.value;
105
106 const Nepomuk::Query::ComparisonTerm term(descr.identifier(),
107 m_valueWidget->value(),
108 comp.value);
109 kDebug() << "term: " << term;
110 return term;
111 }
112
113 SearchCriterionSelector::Type SearchCriterionSelector::type() const
114 {
115 return static_cast<Type>(m_descriptionsBox->currentIndex());
116 }
117
118 void SearchCriterionSelector::slotDescriptionChanged(int index)
119 {
120 if (m_valueWidget != 0) {
121 m_valueWidget->hide();
122 m_layout->removeWidget(m_valueWidget);
123 m_valueWidget = 0;
124 // the value widget is obtained by the Search Criterion
125 // Selector instance and may not get deleted
126 }
127
128 // add comparator items
129 disconnect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
130 m_comparatorBox->clear();
131
132 const SearchCriterionDescription& description = m_descriptions[index];
133 foreach (const SearchCriterionDescription::Comparator& comp, description.comparators()) {
134 m_comparatorBox->addItem(comp.name);
135 }
136
137 // add value widget
138 m_valueWidget = description.valueWidget();
139 m_layout->insertWidget(3, m_valueWidget);
140
141 m_comparatorBox->setCurrentIndex(0);
142 slotComparatorChanged(0);
143 connect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
144 }
145
146 void SearchCriterionSelector::slotComparatorChanged(int index)
147 {
148 Q_ASSERT(index >= 0);
149
150 // only show the value widget if an operation is defined by the comparator
151 const int descIndex = m_descriptionsBox->currentIndex();
152 const SearchCriterionDescription& descr = m_descriptions[descIndex];
153 const SearchCriterionDescription::Comparator& comp = descr.comparators()[index];
154
155 m_valueWidget->initializeValue(comp.autoValueType);
156 // only show the value widget, if an operation is defined
157 // and no automatic calculation is provided
158 m_valueWidget->setVisible(comp.isActive && comp.autoValueType.isEmpty());
159
160 emit criterionChanged();
161 }
162
163 void SearchCriterionSelector::createDescriptions()
164 {
165 Q_ASSERT(m_descriptionsBox != 0);
166 Q_ASSERT(m_comparatorBox != 0);
167
168 // TODO: maybe this creation should be forwarded to a factory if
169 // the number of items increases in future
170 QList<SearchCriterionDescription::Comparator> defaultComps;
171 defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than"), Nepomuk::Query::ComparisonTerm::Greater));
172 defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than or Equal to"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual));
173 defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than"), Nepomuk::Query::ComparisonTerm::Smaller));
174 defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than or Equal to"), Nepomuk::Query::ComparisonTerm::SmallerOrEqual));
175
176 // add "Date" description
177 QList<SearchCriterionDescription::Comparator> dateComps;
178 dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Anytime")));
179 dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Today"), Nepomuk::Query::ComparisonTerm::Equal, "today"));
180 dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Week"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual, "thisWeek"));
181 dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Month"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual, "thisMonth"));
182 dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Year"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual, "thisYear"));
183 foreach (const SearchCriterionDescription::Comparator& comp, defaultComps) {
184 dateComps.append(comp);
185 }
186
187 DateValue* dateValue = new DateValue(this);
188 dateValue->hide();
189 SearchCriterionDescription date(i18nc("@label", "Date:"),
190 Soprano::Vocabulary::NAO::lastModified(),
191 dateComps,
192 dateValue);
193 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Date) == 0);
194 m_descriptions.append(date);
195
196 // add "Size" description
197 QList<SearchCriterionDescription::Comparator> sizeComps = defaultComps;
198 sizeComps.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (file size)", "Any")));
199
200 SizeValue* sizeValue = new SizeValue(this);
201 sizeValue->hide();
202 SearchCriterionDescription size(i18nc("@label", "Size:"),
203 Soprano::Vocabulary::NAO::lastModified(), // TODO
204 sizeComps,
205 sizeValue);
206 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Size) == 1);
207 m_descriptions.append(size);
208
209 // add "Tag" description
210 QList<SearchCriterionDescription::Comparator> tagComps;
211 tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label All (tags)", "All")));
212 tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Equal to"), Nepomuk::Query::ComparisonTerm::Equal));
213 tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Not Equal to"), Nepomuk::Query::ComparisonTerm::Equal)); // TODO
214
215 TagValue* tagValue = new TagValue(this);
216 tagValue->hide();
217 SearchCriterionDescription tag(i18nc("@label", "Tag:"),
218 Soprano::Vocabulary::NAO::Tag(),
219 tagComps,
220 tagValue);
221 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Tag) == 2);
222 m_descriptions.append(tag);
223
224 // add "Rating" description
225 QList<SearchCriterionDescription::Comparator> ratingComps = defaultComps;
226 ratingComps.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (rating)", "Any")));
227
228 RatingValue* ratingValue = new RatingValue(this);
229 ratingValue->hide();
230 SearchCriterionDescription rating(i18nc("@label", "Rating:"),
231 Soprano::Vocabulary::NAO::rating(),
232 ratingComps,
233 ratingValue);
234 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Rating) == 3);
235 m_descriptions.append(rating);
236
237 // add all descriptions to the combo box and connect the value widgets
238 int i = 0;
239 foreach (const SearchCriterionDescription& desc, m_descriptions) {
240 m_descriptionsBox->addItem(desc.name(), i);
241 connect(desc.valueWidget(), SIGNAL(valueChanged(Nepomuk::Query::LiteralTerm)), this, SIGNAL(criterionChanged()));
242 ++i;
243 }
244 }
245
246 #include "searchcriterionselector.moc"