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