]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchoptionsconfigurator.cpp
initial code to provide a Nepomuk query string out of the search criterions
[dolphin.git] / src / search / dolphinsearchoptionsconfigurator.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "dolphinsearchoptionsconfigurator.h"
21
22 #include "searchcriterionselector.h"
23
24 #include <kcombobox.h>
25 #include <kdialog.h>
26 #include <kicon.h>
27 #include <klineedit.h>
28 #include <klocale.h>
29 #include <kseparator.h>
30
31 #include <QButtonGroup>
32 #include <QHBoxLayout>
33 #include <QLabel>
34 #include <QPushButton>
35 #include <QShowEvent>
36 #include <QVBoxLayout>
37
38 #include <kdebug.h>
39
40 DolphinSearchOptionsConfigurator::DolphinSearchOptionsConfigurator(QWidget* parent) :
41 QWidget(parent),
42 m_initialized(false),
43 m_searchFromBox(0),
44 m_searchWhatBox(0),
45 m_addSelectorButton(0),
46 m_vBoxLayout(0),
47 m_criterions()
48 {
49 m_vBoxLayout = new QVBoxLayout(this);
50
51 // add "search" configuration
52 QLabel* searchLabel = new QLabel(i18nc("@label", "Search:"));
53
54 m_searchFromBox = new KComboBox(this);
55 m_searchFromBox->addItem(i18nc("@label", "Everywhere"));
56 m_searchFromBox->addItem(i18nc("@label", "From Here"));
57
58 // add "what" configuration
59 QLabel* whatLabel = new QLabel(i18nc("@label", "What:"));
60
61 m_searchWhatBox = new KComboBox(this);
62 m_searchWhatBox->addItem(i18nc("@label", "All"));
63 m_searchWhatBox->addItem(i18nc("@label", "Images"));
64 m_searchWhatBox->addItem(i18nc("@label", "Text"));
65 m_searchWhatBox->addItem(i18nc("@label", "Filenames"));
66
67 // add button "Save"
68 QPushButton* saveButton = new QPushButton(this);
69 saveButton->setIcon(KIcon("document-save"));
70 saveButton->setText(i18nc("@action:button", "Save"));
71 saveButton->setToolTip(i18nc("@info", "Save search options"));
72 connect(saveButton, SIGNAL(clicked()), this, SLOT(saveQuery()));
73
74 // add button "Close"
75 QPushButton* closeButton = new QPushButton(this);
76 closeButton->setIcon(KIcon("dialog-close"));
77 closeButton->setText(i18nc("@action:button", "Close"));
78 closeButton->setToolTip(i18nc("@info", "Close search options"));
79 connect(closeButton, SIGNAL(clicked()), this, SLOT(hide()));
80
81 // add "Add selector" button
82 m_addSelectorButton = new QPushButton(this);
83 m_addSelectorButton->setIcon(KIcon("list-add"));
84 m_addSelectorButton->setToolTip(i18nc("@info", "Add search option"));
85 m_addSelectorButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
86 connect(m_addSelectorButton, SIGNAL(clicked()), this, SLOT(slotAddSelectorButtonClicked()));
87
88 QHBoxLayout* firstLineLayout = new QHBoxLayout();
89 firstLineLayout->addWidget(searchLabel);
90 firstLineLayout->addWidget(m_searchFromBox);
91 firstLineLayout->addWidget(whatLabel);
92 firstLineLayout->addWidget(m_searchWhatBox);
93 firstLineLayout->addWidget(new QWidget(this), 1); // filler
94 firstLineLayout->addWidget(m_addSelectorButton);
95
96 QHBoxLayout* lastLineLayout = new QHBoxLayout();
97 lastLineLayout->addWidget(new QWidget(this), 1); // filler
98 lastLineLayout->addWidget(saveButton);
99 lastLineLayout->addWidget(closeButton);
100
101 m_vBoxLayout->addWidget(new KSeparator(this));
102 m_vBoxLayout->addLayout(firstLineLayout);
103 m_vBoxLayout->addLayout(lastLineLayout);
104 m_vBoxLayout->addWidget(new KSeparator(this));
105 }
106
107 DolphinSearchOptionsConfigurator::~DolphinSearchOptionsConfigurator()
108 {
109 }
110
111 void DolphinSearchOptionsConfigurator::showEvent(QShowEvent* event)
112 {
113 if (!event->spontaneous() && !m_initialized) {
114 // add default search criterions
115 SearchCriterionSelector* dateCriterion = new SearchCriterionSelector(SearchCriterionSelector::Date, this);
116 SearchCriterionSelector* sizeCriterion = new SearchCriterionSelector(SearchCriterionSelector::Size, this);
117 SearchCriterionSelector* tagCriterion = new SearchCriterionSelector(SearchCriterionSelector::Tag, this);
118
119 // Add the items in the same order as available in the description combo (verified by Q_ASSERTs). This
120 // is not mandatory from an implementation point of view, but preferable from a usability point of view.
121 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Date) == 0);
122 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Size) == 1);
123 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Tag) == 2);
124 addCriterion(dateCriterion);
125 addCriterion(sizeCriterion);
126 addCriterion(tagCriterion);
127
128 m_initialized = true;
129 }
130 QWidget::showEvent(event);
131 }
132
133 void DolphinSearchOptionsConfigurator::slotAddSelectorButtonClicked()
134 {
135 SearchCriterionSelector* selector = new SearchCriterionSelector(SearchCriterionSelector::Tag, this);
136 addCriterion(selector);
137 }
138
139 void DolphinSearchOptionsConfigurator::slotCriterionChanged()
140 {
141 QString searchOptions;
142 foreach (const SearchCriterionSelector* criterion, m_criterions) {
143 searchOptions += criterion->queryString() + ' ';
144 }
145 kDebug() << "Search option string:" << searchOptions;
146 emit searchOptionsChanged(searchOptions);
147 }
148
149 void DolphinSearchOptionsConfigurator::removeCriterion()
150 {
151 SearchCriterionSelector* criterion = qobject_cast<SearchCriterionSelector*>(sender());
152 Q_ASSERT(criterion != 0);
153 m_vBoxLayout->removeWidget(criterion);
154
155 const int index = m_criterions.indexOf(criterion);
156 m_criterions.removeAt(index);
157
158 criterion->deleteLater();
159
160 updateSelectorButton();
161 }
162
163 void DolphinSearchOptionsConfigurator::updateSelectorButton()
164 {
165 const int selectors = m_vBoxLayout->count() - 1;
166 m_addSelectorButton->setEnabled(selectors < 10);
167 }
168
169 void DolphinSearchOptionsConfigurator::saveQuery()
170 {
171 KDialog dialog(0, Qt::Dialog);
172
173 QWidget* container = new QWidget(&dialog);
174
175 QLabel* label = new QLabel(i18nc("@label", "Name:"), container);
176 KLineEdit* lineEdit = new KLineEdit(container);
177 lineEdit->setMinimumWidth(250);
178
179 QHBoxLayout* layout = new QHBoxLayout(container);
180 layout->addWidget(label, Qt::AlignRight);
181 layout->addWidget(lineEdit);
182
183 dialog.setMainWidget(container);
184 dialog.setCaption(i18nc("@title:window", "Save Search Options"));
185 dialog.setButtons(KDialog::Ok | KDialog::Cancel);
186 dialog.setDefaultButton(KDialog::Ok);
187 dialog.setButtonText(KDialog::Ok, i18nc("@action:button", "Save"));
188
189 KConfigGroup dialogConfig(KSharedConfig::openConfig("dolphinrc"),
190 "SaveSearchOptionsDialog");
191 dialog.restoreDialogSize(dialogConfig);
192 dialog.exec(); // TODO...
193 }
194
195 void DolphinSearchOptionsConfigurator::addCriterion(SearchCriterionSelector* criterion)
196 {
197 connect(criterion, SIGNAL(removeCriterion()), this, SLOT(removeCriterion()));
198 connect(criterion, SIGNAL(criterionChanged()), this, SLOT(slotCriterionChanged()));
199
200 // insert the new selector before the lastLineLayout and the KSeparator at the bottom
201 const int index = m_vBoxLayout->count() - 2;
202 m_vBoxLayout->insertWidget(index, criterion);
203 updateSelectorButton();
204
205 m_criterions.append(criterion);
206 }
207
208 #include "dolphinsearchoptionsconfigurator.moc"