]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/searchcriterionselector.cpp
initial code to provide a Nepomuk query string out of the search criterions
[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 #include "searchcriterionvalue.h"
24
25 #include <QComboBox>
26 #include <QHBoxLayout>
27 #include <QList>
28 #include <QPushButton>
29
30 #include <kicon.h>
31 #include <klocale.h>
32
33 SearchCriterionSelector::SearchCriterionSelector(Type type, QWidget* parent) :
34 QWidget(parent),
35 m_layout(0),
36 m_descriptionsBox(0),
37 m_comparatorBox(0),
38 m_valueWidget(0),
39 m_removeButton(0),
40 m_descriptions()
41 {
42 m_descriptionsBox = new QComboBox(this);
43
44 m_comparatorBox = new QComboBox(this);
45 m_comparatorBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
46
47 createDescriptions();
48 const int index = static_cast<int>(type);
49 m_descriptionsBox->setCurrentIndex(index);
50
51 QWidget* filler = new QWidget(this);
52 filler->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
53
54 m_removeButton = new QPushButton(this);
55 m_removeButton->setIcon(KIcon("list-remove"));
56 m_removeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
57 connect(m_removeButton, SIGNAL(clicked()), this, SIGNAL(removeCriterion()));
58
59 m_layout = new QHBoxLayout(this);
60 m_layout->setMargin(0);
61 m_layout->addWidget(m_descriptionsBox);
62 m_layout->addWidget(m_comparatorBox);
63 m_layout->addWidget(filler);
64 m_layout->addWidget(m_removeButton);
65
66 setLayout(m_layout);
67
68 slotDescriptionChanged(index);
69 connect(m_descriptionsBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDescriptionChanged(int)));
70 }
71
72 SearchCriterionSelector::~SearchCriterionSelector()
73 {
74 }
75
76 QString SearchCriterionSelector::queryString() const
77 {
78 if (m_valueWidget == 0) {
79 return QString();
80 }
81
82 const int descIndex = m_descriptionsBox->currentIndex();
83 const SearchCriterionDescription& descr = m_descriptions[descIndex];
84
85 const int compIndex = m_comparatorBox->currentIndex();
86 const SearchCriterionDescription::Comparator& comp = descr.comparators()[compIndex];
87
88 return comp.prefix + descr.identifier() + comp.operation +
89 '"' + m_valueWidget->value() + '"';
90 }
91
92 void SearchCriterionSelector::slotDescriptionChanged(int index)
93 {
94 if (m_valueWidget != 0) {
95 m_valueWidget->hide();
96 m_layout->removeWidget(m_valueWidget);
97 m_valueWidget = 0;
98 // the value widget is obtained by the Search Criterion
99 // Selector instance and may not get deleted
100 }
101
102 // add comparator items
103 disconnect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
104 m_comparatorBox->clear();
105
106 const SearchCriterionDescription& description = m_descriptions[index];
107 foreach (const SearchCriterionDescription::Comparator& comp, description.comparators()) {
108 m_comparatorBox->addItem(comp.name);
109 }
110
111 // add value widget
112 m_valueWidget = description.valueWidget();
113 m_layout->insertWidget(2, m_valueWidget);
114
115 m_comparatorBox->setCurrentIndex(0);
116 slotComparatorChanged(0);
117 connect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
118 }
119
120 void SearchCriterionSelector::slotComparatorChanged(int index)
121 {
122 Q_ASSERT(index >= 0);
123
124 // only show the value widget if an operation is defined by the comparator
125 const int descIndex = m_descriptionsBox->currentIndex();
126 const SearchCriterionDescription& descr = m_descriptions[descIndex];
127 const SearchCriterionDescription::Comparator& comp = descr.comparators()[index];
128 m_valueWidget->setVisible(!comp.operation.isEmpty());
129
130 emit criterionChanged();
131 }
132
133 void SearchCriterionSelector::createDescriptions()
134 {
135 Q_ASSERT(m_descriptionsBox != 0);
136 Q_ASSERT(m_comparatorBox != 0);
137
138 // TODO: maybe this creation should be forwarded to a factory if
139 // the number of items increases in future
140 QList<SearchCriterionDescription::Comparator> defaultComps;
141 defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than"), ">", "+"));
142 defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than or Equal to"), ">=", "+"));
143 defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than"), "<", "+"));
144 defaultComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than or Equal to"), "<=", "+"));
145
146 // add "Date" description
147 QList<SearchCriterionDescription::Comparator> dateComps;
148 dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Anytime"))); // TODO
149 dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Today"))); // TODO
150 dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "This week"))); // TODO
151 dateComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "This month"))); // TODO
152 foreach (const SearchCriterionDescription::Comparator& comp, defaultComps) {
153 dateComps.append(comp);
154 }
155
156 DateValue* dateValue = new DateValue(this);
157 dateValue->hide();
158 SearchCriterionDescription date(i18nc("@label", "Date:"),
159 "lastModified",
160 dateComps,
161 dateValue);
162
163 // add "Size" description
164 QList<SearchCriterionDescription::Comparator> sizeComps = defaultComps;
165 sizeComps.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (file size)", "Any")));
166
167 SizeValue* sizeValue = new SizeValue(this);
168 sizeValue->hide();
169 SearchCriterionDescription size(i18nc("@label", "Size:"),
170 "contentSize",
171 sizeComps,
172 sizeValue);
173
174 // add "Tag" description
175 QList<SearchCriterionDescription::Comparator> tagComps;
176 tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label All (tags)", "All")));
177 tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Equal to"), ":"));
178
179 TagValue* tagValue = new TagValue(this);
180 tagValue->hide();
181 SearchCriterionDescription tag(i18nc("@label", "Tag:"),
182 "tag",
183 tagComps,
184 tagValue);
185
186 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Date) == 0);
187 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Size) == 1);
188 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Tag) == 2);
189 m_descriptions.append(date);
190 m_descriptions.append(size);
191 m_descriptions.append(tag);
192
193 // add all descriptions to the combo box and connect the value widgets
194 int i = 0;
195 foreach (const SearchCriterionDescription& desc, m_descriptions) {
196 m_descriptionsBox->addItem(desc.name(), i);
197 connect(desc.valueWidget(), SIGNAL(valueChanged(QString)), this, SIGNAL(criterionChanged()));
198 ++i;
199 }
200 }
201
202 #include "searchcriterionselector.moc"