]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/dolphinsearchoptionsconfigurator.cpp
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 "searchcriterionselector.h"
24 #include <kcombobox.h>
27 #include <klineedit.h>
29 #include <kseparator.h>
31 #include <QButtonGroup>
32 #include <QHBoxLayout>
34 #include <QPushButton>
36 #include <QVBoxLayout>
40 DolphinSearchOptionsConfigurator::DolphinSearchOptionsConfigurator(QWidget
* parent
) :
45 m_addSelectorButton(0),
49 m_vBoxLayout
= new QVBoxLayout(this);
51 // add "search" configuration
52 QLabel
* searchLabel
= new QLabel(i18nc("@label", "Search:"));
54 m_searchFromBox
= new KComboBox(this);
55 m_searchFromBox
->addItem(i18nc("@label", "Everywhere"));
56 m_searchFromBox
->addItem(i18nc("@label", "From Here"));
58 // add "what" configuration
59 QLabel
* whatLabel
= new QLabel(i18nc("@label", "What:"));
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"));
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()));
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()));
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()));
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
);
96 QHBoxLayout
* lastLineLayout
= new QHBoxLayout();
97 lastLineLayout
->addWidget(new QWidget(this), 1); // filler
98 lastLineLayout
->addWidget(saveButton
);
99 lastLineLayout
->addWidget(closeButton
);
101 m_vBoxLayout
->addWidget(new KSeparator(this));
102 m_vBoxLayout
->addLayout(firstLineLayout
);
103 m_vBoxLayout
->addLayout(lastLineLayout
);
104 m_vBoxLayout
->addWidget(new KSeparator(this));
107 DolphinSearchOptionsConfigurator::~DolphinSearchOptionsConfigurator()
111 void DolphinSearchOptionsConfigurator::showEvent(QShowEvent
* event
)
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);
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
);
128 m_initialized
= true;
130 QWidget::showEvent(event
);
133 void DolphinSearchOptionsConfigurator::slotAddSelectorButtonClicked()
135 SearchCriterionSelector
* selector
= new SearchCriterionSelector(SearchCriterionSelector::Tag
, this);
136 addCriterion(selector
);
139 void DolphinSearchOptionsConfigurator::slotCriterionChanged()
141 QString searchOptions
;
142 foreach (const SearchCriterionSelector
* criterion
, m_criterions
) {
143 searchOptions
+= criterion
->queryString() + ' ';
145 kDebug() << "Search option string:" << searchOptions
;
146 emit
searchOptionsChanged(searchOptions
);
149 void DolphinSearchOptionsConfigurator::removeCriterion()
151 SearchCriterionSelector
* criterion
= qobject_cast
<SearchCriterionSelector
*>(sender());
152 Q_ASSERT(criterion
!= 0);
153 m_vBoxLayout
->removeWidget(criterion
);
155 const int index
= m_criterions
.indexOf(criterion
);
156 m_criterions
.removeAt(index
);
158 criterion
->deleteLater();
160 updateSelectorButton();
163 void DolphinSearchOptionsConfigurator::updateSelectorButton()
165 const int selectors
= m_vBoxLayout
->count() - 1;
166 m_addSelectorButton
->setEnabled(selectors
< 10);
169 void DolphinSearchOptionsConfigurator::saveQuery()
171 KDialog
dialog(0, Qt::Dialog
);
173 QWidget
* container
= new QWidget(&dialog
);
175 QLabel
* label
= new QLabel(i18nc("@label", "Name:"), container
);
176 KLineEdit
* lineEdit
= new KLineEdit(container
);
177 lineEdit
->setMinimumWidth(250);
179 QHBoxLayout
* layout
= new QHBoxLayout(container
);
180 layout
->addWidget(label
, Qt::AlignRight
);
181 layout
->addWidget(lineEdit
);
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"));
189 KConfigGroup
dialogConfig(KSharedConfig::openConfig("dolphinrc"),
190 "SaveSearchOptionsDialog");
191 dialog
.restoreDialogSize(dialogConfig
);
192 dialog
.exec(); // TODO...
195 void DolphinSearchOptionsConfigurator::addCriterion(SearchCriterionSelector
* criterion
)
197 connect(criterion
, SIGNAL(removeCriterion()), this, SLOT(removeCriterion()));
198 connect(criterion
, SIGNAL(criterionChanged()), this, SLOT(slotCriterionChanged()));
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();
205 m_criterions
.append(criterion
);
208 #include "dolphinsearchoptionsconfigurator.moc"