]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/searchcriterionselector.cpp
get_servicemenu added to dolphinpart
[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(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 m_descriptionsBox->addItem(i18nc("@label", "Select..."), -1);
44 createDescriptions();
45 connect(m_descriptionsBox, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDescriptionChanged(int)));
46
47 m_comparatorBox = new QComboBox(this);
48 m_comparatorBox->hide();
49 connect(m_comparatorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateQuery()));
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
69 SearchCriterionSelector::~SearchCriterionSelector()
70 {
71 }
72
73 void SearchCriterionSelector::createDescriptions()
74 {
75 // TODO: maybe this creation should be forwarded to a factory if
76 // the number of items increases in future
77
78 QList<SearchCriterionDescription::Comparator> comparators;
79 comparators.append(SearchCriterionDescription::Comparator(i18nc("@label", "greater than"), ">", "+"));
80 comparators.append(SearchCriterionDescription::Comparator(i18nc("@label", "greater than or equal to"), ">=", "+"));
81 comparators.append(SearchCriterionDescription::Comparator(i18nc("@label", "less than"), "<", "+"));
82 comparators.append(SearchCriterionDescription::Comparator(i18nc("@label", "less than or equal to"), "<=", "+"));
83
84 // add "Date" description
85 DateValue* dateValue = new DateValue(this);
86 dateValue->hide();
87 SearchCriterionDescription date(i18nc("@label", "Date Modified"),
88 "sourceModified",
89 comparators,
90 dateValue);
91
92 // add "File Size" description
93 FileSizeValue* fileSizeValue = new FileSizeValue(this);
94 fileSizeValue->hide();
95 SearchCriterionDescription size(i18nc("@label", "File Size"),
96 "contentSize",
97 comparators,
98 fileSizeValue);
99
100 m_descriptions.append(date);
101 m_descriptions.append(size);
102
103 // add all descriptions to the combo box
104 const int count = m_descriptions.count();
105 for (int i = 0; i < count; ++i) {
106 m_descriptionsBox->addItem(m_descriptions[i].name(), i);
107 }
108 }
109
110 void SearchCriterionSelector::slotDescriptionChanged(int index)
111 {
112 m_comparatorBox->clear();
113 m_comparatorBox->show();
114 if (m_valueWidget != 0) {
115 m_layout->removeWidget(m_valueWidget);
116 // the value widget is obtained by the Search Criterion
117 // Selector instance and may not get deleted
118 }
119
120 // adjust the comparator box and the value widget dependent from the selected description
121 m_comparatorBox->addItem(i18nc("@label", "Select..."), -1);
122 const int descrIndex = m_descriptionsBox->itemData(index).toInt();
123 if (descrIndex >= 0) {
124 // add comparator items
125 const SearchCriterionDescription& description = m_descriptions[descrIndex];
126 foreach (const SearchCriterionDescription::Comparator& comp, description.comparators()) {
127 m_comparatorBox->addItem(comp.name);
128 }
129
130 // add value widget
131 m_valueWidget = description.valueWidget();
132 const int layoutIndex = m_layout->count() - 2;
133 m_layout->insertWidget(layoutIndex, m_valueWidget);
134 m_valueWidget->show();
135 }
136 }
137
138 void SearchCriterionSelector::updateQuery()
139 {
140 const SearchCriterionDescription* descr = description();
141 if (descr == 0) {
142 // no description has been selected
143 return;
144 }
145
146 // get selected comparator related to the description
147 const int compBoxIndex = m_comparatorBox->currentIndex();
148 const int compIndex = m_comparatorBox->itemData(compBoxIndex).toInt();
149 if (compIndex < 0) {
150 // no comparator has been selected
151 return;
152 }
153
154 // create query string
155 const SearchCriterionDescription::Comparator& comp = descr->comparators()[compIndex];
156 const QString queryString = comp.prefix + descr->identifier() + comp.operation + m_valueWidget->value();
157 emit criterionChanged(queryString);
158 }
159
160 const SearchCriterionDescription* SearchCriterionSelector::description() const
161 {
162 const int descrBoxIndex = m_descriptionsBox->currentIndex();
163 const int descrIndex = m_descriptionsBox->itemData(descrBoxIndex).toInt();
164 return (descrIndex < 0) ? 0 : &m_descriptions[descrIndex];
165 }
166
167 #include "searchcriterionselector.moc"