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 criterionsString
;
166 foreach(const SearchCriterionSelector
* criterion
, m_criterions
) {
167 if (!criterionsString
.isEmpty()) {
168 criterionsString
+= ',';
170 const int index
= static_cast<int>(criterion
->type());
171 criterionsString
+= g_criterionItems
[index
].settingsName
;
173 SearchSettings::setCriterions(criterionsString
);
175 SearchSettings::self()->writeConfig();
178 KUrl
DolphinSearchOptionsConfigurator::nepomukUrl() const
180 Nepomuk::Query::AndTerm andTerm
;
181 foreach (const SearchCriterionSelector
* criterion
, m_criterions
) {
182 const Nepomuk::Query::Term term
= criterion
->queryTerm();
183 andTerm
.addSubTerm(term
);
186 // TODO: respect m_customSearchQuery
188 Nepomuk::Query::Query query
;
189 query
.setTerm(andTerm
);
190 return query
.toSearchUrl();
192 /*QString searchOptions;
193 QString searchString = m_customSearchQuery;
194 if (!searchString.isEmpty() && !searchOptions.isEmpty()) {
195 searchString += ' ' + searchOptions;
196 } else if (!searchOptions.isEmpty()) {
197 searchString += searchOptions;
200 searchString.insert(0, QLatin1String("nepomuksearch:/"));
201 return KUrl(searchString);*/
204 void DolphinSearchOptionsConfigurator::setCustomSearchQuery(const QString
& searchQuery
)
206 m_customSearchQuery
= searchQuery
.simplified();
208 const bool enabled
= hasSearchParameters();
209 m_searchButton
->setEnabled(enabled
);
210 m_saveButton
->setEnabled(enabled
);
213 void DolphinSearchOptionsConfigurator::showEvent(QShowEvent
* event
)
215 if (!event
->spontaneous() && !m_initialized
) {
216 // restore the UI layout of the last session
217 const QString location
= SearchSettings::location();
218 for (unsigned int i
= 0; i
< sizeof(g_locationItems
) / sizeof(SettingsItem
); ++i
) {
219 if (g_locationItems
[i
].settingsName
== location
) {
220 m_locationBox
->setCurrentIndex(i
);
225 const QString what
= SearchSettings::what();
226 for (unsigned int i
= 0; i
< sizeof(g_whatItems
) / sizeof(SettingsItem
); ++i
) {
227 if (g_whatItems
[i
].settingsName
== what
) {
228 m_whatBox
->setCurrentIndex(i
);
233 const QString criterions
= SearchSettings::criterions();
234 QStringList criterionsList
= criterions
.split(',');
235 foreach (const QString
& criterionName
, criterionsList
) {
236 for (unsigned int i
= 0; i
< sizeof(g_criterionItems
) / sizeof(CriterionItem
); ++i
) {
237 if (g_criterionItems
[i
].settingsName
== criterionName
) {
238 const SearchCriterionSelector::Type type
= g_criterionItems
[i
].type
;
239 addCriterion(new SearchCriterionSelector(type
, this));
245 m_initialized
= true;
247 QWidget::showEvent(event
);
250 void DolphinSearchOptionsConfigurator::slotAddSelectorButtonClicked()
252 SearchCriterionSelector
* selector
= new SearchCriterionSelector(SearchCriterionSelector::Date
, this);
253 addCriterion(selector
);
256 void DolphinSearchOptionsConfigurator::slotCriterionChanged()
258 const bool enabled
= hasSearchParameters();
259 m_searchButton
->setEnabled(enabled
);
260 m_saveButton
->setEnabled(enabled
);
263 void DolphinSearchOptionsConfigurator::removeCriterion()
265 SearchCriterionSelector
* criterion
= qobject_cast
<SearchCriterionSelector
*>(sender());
266 Q_ASSERT(criterion
!= 0);
267 m_vBoxLayout
->removeWidget(criterion
);
269 const int index
= m_criterions
.indexOf(criterion
);
270 m_criterions
.removeAt(index
);
272 criterion
->deleteLater();
274 updateSelectorButton();
277 void DolphinSearchOptionsConfigurator::updateSelectorButton()
279 const int selectors
= m_vBoxLayout
->count() - 1;
280 m_addSelectorButton
->setEnabled(selectors
< 10);
283 void DolphinSearchOptionsConfigurator::saveQuery()
285 KDialog
dialog(0, Qt::Dialog
);
287 QWidget
* container
= new QWidget(&dialog
);
289 QLabel
* label
= new QLabel(i18nc("@label", "Name:"), container
);
290 KLineEdit
* lineEdit
= new KLineEdit(container
);
291 lineEdit
->setMinimumWidth(250);
293 QHBoxLayout
* layout
= new QHBoxLayout(container
);
294 layout
->addWidget(label
, Qt::AlignRight
);
295 layout
->addWidget(lineEdit
);
297 dialog
.setMainWidget(container
);
298 dialog
.setCaption(i18nc("@title:window", "Save Search Options"));
299 dialog
.setButtons(KDialog::Ok
| KDialog::Cancel
);
300 dialog
.setDefaultButton(KDialog::Ok
);
301 dialog
.setButtonText(KDialog::Ok
, i18nc("@action:button", "Save"));
303 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
304 "SaveSearchOptionsDialog");
305 dialog
.restoreDialogSize(dialogConfig
);
306 dialog
.exec(); // TODO...
309 void DolphinSearchOptionsConfigurator::addCriterion(SearchCriterionSelector
* criterion
)
311 connect(criterion
, SIGNAL(removeCriterion()), this, SLOT(removeCriterion()));
312 connect(criterion
, SIGNAL(criterionChanged()), this, SLOT(slotCriterionChanged()));
314 // insert the new selector before the KSeparator at the bottom
315 const int index
= m_vBoxLayout
->count() - 1;
316 m_vBoxLayout
->insertWidget(index
, criterion
);
317 updateSelectorButton();
319 m_criterions
.append(criterion
);
322 bool DolphinSearchOptionsConfigurator::hasSearchParameters() const
324 if (!m_customSearchQuery
.isEmpty()) {
325 // performance optimization: if a custom search query is defined,
326 // there is no need to call the (quite expensive) method nepomukUrl()
329 return nepomukUrl().path() != QLatin1String("/");
332 #include "dolphinsearchoptionsconfigurator.moc"