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
194 return query
.toSearchUrl();
197 void DolphinSearchOptionsConfigurator::setCustomSearchQuery(const QString
& searchQuery
)
199 m_customSearchQuery
= searchQuery
.simplified();
201 const bool enabled
= hasSearchParameters();
202 m_searchButton
->setEnabled(enabled
);
203 m_saveButton
->setEnabled(enabled
);
206 void DolphinSearchOptionsConfigurator::showEvent(QShowEvent
* event
)
208 if (!event
->spontaneous() && !m_initialized
) {
209 // restore the UI layout of the last session
210 const QString location
= SearchSettings::location();
211 for (unsigned int i
= 0; i
< sizeof(g_locationItems
) / sizeof(SettingsItem
); ++i
) {
212 if (g_locationItems
[i
].settingsName
== location
) {
213 m_locationBox
->setCurrentIndex(i
);
218 const QString what
= SearchSettings::what();
219 for (unsigned int i
= 0; i
< sizeof(g_whatItems
) / sizeof(SettingsItem
); ++i
) {
220 if (g_whatItems
[i
].settingsName
== what
) {
221 m_whatBox
->setCurrentIndex(i
);
226 const QString criteria
= SearchSettings::criteria();
227 QStringList criteriaList
= criteria
.split(',');
228 foreach (const QString
& criterionName
, criteriaList
) {
229 for (unsigned int i
= 0; i
< sizeof(g_criterionItems
) / sizeof(CriterionItem
); ++i
) {
230 if (g_criterionItems
[i
].settingsName
== criterionName
) {
231 const SearchCriterionSelector::Type type
= g_criterionItems
[i
].type
;
232 addCriterion(new SearchCriterionSelector(type
, this));
238 m_initialized
= true;
240 QWidget::showEvent(event
);
243 void DolphinSearchOptionsConfigurator::slotAddSelectorButtonClicked()
245 SearchCriterionSelector
* selector
= new SearchCriterionSelector(SearchCriterionSelector::Date
, this);
246 addCriterion(selector
);
249 void DolphinSearchOptionsConfigurator::slotCriterionChanged()
251 const bool enabled
= hasSearchParameters();
252 m_searchButton
->setEnabled(enabled
);
253 m_saveButton
->setEnabled(enabled
);
256 void DolphinSearchOptionsConfigurator::removeCriterion()
258 SearchCriterionSelector
* criterion
= qobject_cast
<SearchCriterionSelector
*>(sender());
259 Q_ASSERT(criterion
!= 0);
260 m_vBoxLayout
->removeWidget(criterion
);
262 const int index
= m_criteria
.indexOf(criterion
);
263 m_criteria
.removeAt(index
);
265 criterion
->deleteLater();
267 updateSelectorButton();
270 void DolphinSearchOptionsConfigurator::updateSelectorButton()
272 const int selectors
= m_vBoxLayout
->count() - 1;
273 m_addSelectorButton
->setEnabled(selectors
< 10);
276 void DolphinSearchOptionsConfigurator::saveQuery()
278 KDialog
dialog(0, Qt::Dialog
);
280 QWidget
* container
= new QWidget(&dialog
);
282 QLabel
* label
= new QLabel(i18nc("@label", "Name:"), container
);
283 KLineEdit
* lineEdit
= new KLineEdit(container
);
284 lineEdit
->setMinimumWidth(250);
286 QHBoxLayout
* layout
= new QHBoxLayout(container
);
287 layout
->addWidget(label
, Qt::AlignRight
);
288 layout
->addWidget(lineEdit
);
290 dialog
.setMainWidget(container
);
291 dialog
.setCaption(i18nc("@title:window", "Save Search Options"));
292 dialog
.setButtons(KDialog::Ok
| KDialog::Cancel
);
293 dialog
.setDefaultButton(KDialog::Ok
);
294 dialog
.setButtonText(KDialog::Ok
, i18nc("@action:button", "Save"));
296 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
297 "SaveSearchOptionsDialog");
298 dialog
.restoreDialogSize(dialogConfig
);
299 dialog
.exec(); // TODO...
302 void DolphinSearchOptionsConfigurator::addCriterion(SearchCriterionSelector
* criterion
)
304 connect(criterion
, SIGNAL(removeCriterion()), this, SLOT(removeCriterion()));
305 connect(criterion
, SIGNAL(criterionChanged()), this, SLOT(slotCriterionChanged()));
307 // insert the new selector before the KSeparator at the bottom
308 const int index
= m_vBoxLayout
->count() - 1;
309 m_vBoxLayout
->insertWidget(index
, criterion
);
310 updateSelectorButton();
312 m_criteria
.append(criterion
);
315 bool DolphinSearchOptionsConfigurator::hasSearchParameters() const
317 if (!m_customSearchQuery
.isEmpty()) {
318 // performance optimization: if a custom search query is defined,
319 // there is no need to call the (quite expensive) method nepomukUrl()
322 return true; //nepomukUrl().path() != QLatin1String("/");
325 #include "dolphinsearchoptionsconfigurator.moc"