1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "dolphinsearchoptionsconfigurator.h"
22 #include "dolphin_searchsettings.h"
23 #include "searchcriterionselector.h"
25 #include <nepomuk/andterm.h>
26 #include <nepomuk/query.h>
27 #include <nepomuk/term.h>
29 #include <kcombobox.h>
32 #include <klineedit.h>
34 #include <kseparator.h>
36 #include <QButtonGroup>
37 #include <QHBoxLayout>
39 #include <QPushButton>
41 #include <QVBoxLayout>
45 const char* settingsName
;
49 // Contains the settings names and translated texts
50 // for each item of the location-combo-box.
51 static const SettingsItem g_locationItems
[] = {
52 {"Everywhere", I18N_NOOP2("@label", "Everywhere")},
53 {"From Here", I18N_NOOP2("@label", "From Here")}
56 // Contains the settings names and translated texts
57 // for each item of the what-combobox.
58 static const SettingsItem g_whatItems
[] = {
59 {"All", I18N_NOOP2("@label", "All")},
60 {"Images", I18N_NOOP2("@label", "Images")},
61 {"Text", I18N_NOOP2("@label", "Text")},
62 {"Filenames", I18N_NOOP2("@label", "Filenames")}
67 const char* settingsName
;
68 SearchCriterionSelector::Type type
;
71 // Contains the settings names for type
72 // of availabe search criterion.
73 static const CriterionItem g_criterionItems
[] = {
74 {"Date", SearchCriterionSelector::Date
},
75 {"Size", SearchCriterionSelector::Size
},
76 {"Tag", SearchCriterionSelector::Tag
},
77 {"Raging", SearchCriterionSelector::Rating
}
80 DolphinSearchOptionsConfigurator::DolphinSearchOptionsConfigurator(QWidget
* parent
) :
85 m_addSelectorButton(0),
92 m_vBoxLayout
= new QVBoxLayout(this);
94 // add "search" configuration
95 QLabel
* searchLabel
= new QLabel(i18nc("@label", "Search:"));
97 m_locationBox
= new KComboBox(this);
98 for (unsigned int i
= 0; i
< sizeof(g_locationItems
) / sizeof(SettingsItem
); ++i
) {
99 m_locationBox
->addItem(g_locationItems
[i
].text
);
102 // add "what" configuration
103 QLabel
* whatLabel
= new QLabel(i18nc("@label", "What:"));
105 m_whatBox
= new KComboBox(this);
106 for (unsigned int i
= 0; i
< sizeof(g_whatItems
) / sizeof(SettingsItem
); ++i
) {
107 m_whatBox
->addItem(g_whatItems
[i
].text
);
110 // add "Add selector" button
111 m_addSelectorButton
= new QPushButton(this);
112 m_addSelectorButton
->setIcon(KIcon("list-add"));
113 m_addSelectorButton
->setToolTip(i18nc("@info", "Add search option"));
114 m_addSelectorButton
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
115 connect(m_addSelectorButton
, SIGNAL(clicked()), this, SLOT(slotAddSelectorButtonClicked()));
117 // add button "Search"
118 m_searchButton
= new QPushButton(this);
119 m_searchButton
->setIcon(KIcon("edit-find"));
120 m_searchButton
->setText(i18nc("@action:button", "Search"));
121 m_searchButton
->setToolTip(i18nc("@info", "Start searching"));
122 m_searchButton
->setEnabled(false);
123 connect(m_searchButton
, SIGNAL(clicked()), this, SIGNAL(searchOptionsChanged()));
126 m_saveButton
= new QPushButton(this);
127 m_saveButton
->setIcon(KIcon("document-save"));
128 m_saveButton
->setText(i18nc("@action:button", "Save"));
129 m_saveButton
->setToolTip(i18nc("@info", "Save search options"));
130 m_saveButton
->setEnabled(false);
131 connect(m_saveButton
, SIGNAL(clicked()), this, SLOT(saveQuery()));
133 // add button "Close"
134 QPushButton
* closeButton
= new QPushButton(this);
135 closeButton
->setIcon(KIcon("dialog-close"));
136 closeButton
->setText(i18nc("@action:button", "Close"));
137 closeButton
->setToolTip(i18nc("@info", "Close search options"));
138 connect(closeButton
, SIGNAL(clicked()), this, SLOT(hide()));
140 QHBoxLayout
* topLineLayout
= new QHBoxLayout();
141 topLineLayout
->addWidget(m_addSelectorButton
);
142 topLineLayout
->addWidget(searchLabel
);
143 topLineLayout
->addWidget(m_locationBox
);
144 topLineLayout
->addWidget(whatLabel
);
145 topLineLayout
->addWidget(m_whatBox
);
146 topLineLayout
->addWidget(new QWidget(this), 1); // filler
147 topLineLayout
->addWidget(m_searchButton
);
148 topLineLayout
->addWidget(m_saveButton
);
149 topLineLayout
->addWidget(closeButton
);
151 m_vBoxLayout
->addWidget(new KSeparator(this));
152 m_vBoxLayout
->addLayout(topLineLayout
);
153 m_vBoxLayout
->addWidget(new KSeparator(this));
156 DolphinSearchOptionsConfigurator::~DolphinSearchOptionsConfigurator()
158 // store the UI configuration
159 const int locationIndex
= m_locationBox
->currentIndex();
160 SearchSettings::setLocation(g_locationItems
[locationIndex
].settingsName
);
162 const int whatIndex
= m_whatBox
->currentIndex();
163 SearchSettings::setWhat(g_whatItems
[whatIndex
].settingsName
);
165 QString criteriaString
;
166 foreach(const SearchCriterionSelector
* criterion
, m_criteria
) {
167 if (!criteriaString
.isEmpty()) {
168 criteriaString
+= ',';
170 const int index
= static_cast<int>(criterion
->type());
171 criteriaString
+= g_criterionItems
[index
].settingsName
;
173 SearchSettings::setCriteria(criteriaString
);
175 SearchSettings::self()->writeConfig();
178 KUrl
DolphinSearchOptionsConfigurator::nepomukUrl() const
180 Nepomuk::Query::Query query
;
181 if (m_criteria
.size() == 1) {
182 query
.setTerm(m_criteria
.first()->queryTerm());
184 Nepomuk::Query::AndTerm andTerm
;
185 foreach (const SearchCriterionSelector
* criterion
, m_criteria
) {
186 const Nepomuk::Query::Term term
= criterion
->queryTerm();
187 andTerm
.addSubTerm(term
);
189 query
.setTerm(andTerm
);
192 // TODO: respect m_customSearchQuery
195 //return query.toSearchUrl();
200 void DolphinSearchOptionsConfigurator::setCustomSearchQuery(const QString
& searchQuery
)
202 m_customSearchQuery
= searchQuery
.simplified();
204 const bool enabled
= hasSearchParameters();
205 m_searchButton
->setEnabled(enabled
);
206 m_saveButton
->setEnabled(enabled
);
209 void DolphinSearchOptionsConfigurator::showEvent(QShowEvent
* event
)
211 if (!event
->spontaneous() && !m_initialized
) {
212 // restore the UI layout of the last session
213 const QString location
= SearchSettings::location();
214 for (unsigned int i
= 0; i
< sizeof(g_locationItems
) / sizeof(SettingsItem
); ++i
) {
215 if (g_locationItems
[i
].settingsName
== location
) {
216 m_locationBox
->setCurrentIndex(i
);
221 const QString what
= SearchSettings::what();
222 for (unsigned int i
= 0; i
< sizeof(g_whatItems
) / sizeof(SettingsItem
); ++i
) {
223 if (g_whatItems
[i
].settingsName
== what
) {
224 m_whatBox
->setCurrentIndex(i
);
229 const QString criteria
= SearchSettings::criteria();
230 QStringList criteriaList
= criteria
.split(',');
231 foreach (const QString
& criterionName
, criteriaList
) {
232 for (unsigned int i
= 0; i
< sizeof(g_criterionItems
) / sizeof(CriterionItem
); ++i
) {
233 if (g_criterionItems
[i
].settingsName
== criterionName
) {
234 const SearchCriterionSelector::Type type
= g_criterionItems
[i
].type
;
235 addCriterion(new SearchCriterionSelector(type
, this));
241 m_initialized
= true;
243 QWidget::showEvent(event
);
246 void DolphinSearchOptionsConfigurator::slotAddSelectorButtonClicked()
248 SearchCriterionSelector
* selector
= new SearchCriterionSelector(SearchCriterionSelector::Date
, this);
249 addCriterion(selector
);
252 void DolphinSearchOptionsConfigurator::slotCriterionChanged()
254 const bool enabled
= hasSearchParameters();
255 m_searchButton
->setEnabled(enabled
);
256 m_saveButton
->setEnabled(enabled
);
259 void DolphinSearchOptionsConfigurator::removeCriterion()
261 SearchCriterionSelector
* criterion
= qobject_cast
<SearchCriterionSelector
*>(sender());
262 Q_ASSERT(criterion
!= 0);
263 m_vBoxLayout
->removeWidget(criterion
);
265 const int index
= m_criteria
.indexOf(criterion
);
266 m_criteria
.removeAt(index
);
268 criterion
->deleteLater();
270 updateSelectorButton();
273 void DolphinSearchOptionsConfigurator::updateSelectorButton()
275 const int selectors
= m_vBoxLayout
->count() - 1;
276 m_addSelectorButton
->setEnabled(selectors
< 10);
279 void DolphinSearchOptionsConfigurator::saveQuery()
281 KDialog
dialog(0, Qt::Dialog
);
283 QWidget
* container
= new QWidget(&dialog
);
285 QLabel
* label
= new QLabel(i18nc("@label", "Name:"), container
);
286 KLineEdit
* lineEdit
= new KLineEdit(container
);
287 lineEdit
->setMinimumWidth(250);
289 QHBoxLayout
* layout
= new QHBoxLayout(container
);
290 layout
->addWidget(label
, Qt::AlignRight
);
291 layout
->addWidget(lineEdit
);
293 dialog
.setMainWidget(container
);
294 dialog
.setCaption(i18nc("@title:window", "Save Search Options"));
295 dialog
.setButtons(KDialog::Ok
| KDialog::Cancel
);
296 dialog
.setDefaultButton(KDialog::Ok
);
297 dialog
.setButtonText(KDialog::Ok
, i18nc("@action:button", "Save"));
299 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
300 "SaveSearchOptionsDialog");
301 dialog
.restoreDialogSize(dialogConfig
);
302 dialog
.exec(); // TODO...
305 void DolphinSearchOptionsConfigurator::addCriterion(SearchCriterionSelector
* criterion
)
307 connect(criterion
, SIGNAL(removeCriterion()), this, SLOT(removeCriterion()));
308 connect(criterion
, SIGNAL(criterionChanged()), this, SLOT(slotCriterionChanged()));
310 // insert the new selector before the KSeparator at the bottom
311 const int index
= m_vBoxLayout
->count() - 1;
312 m_vBoxLayout
->insertWidget(index
, criterion
);
313 updateSelectorButton();
315 m_criteria
.append(criterion
);
318 bool DolphinSearchOptionsConfigurator::hasSearchParameters() const
320 if (!m_customSearchQuery
.isEmpty()) {
321 // performance optimization: if a custom search query is defined,
322 // there is no need to call the (quite expensive) method nepomukUrl()
325 return true; //nepomukUrl().path() != QLatin1String("/");
328 #include "dolphinsearchoptionsconfigurator.moc"