1 /***************************************************************************
2 * Copyright (C) 2009 by Adam Kidder <thekidder@gmail.com> *
3 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "searchcriterionselector.h"
23 #include "searchcriterionvalue.h"
26 #include <QHBoxLayout>
28 #include <QPushButton>
33 SearchCriterionSelector::SearchCriterionSelector(Type type
, QWidget
* parent
) :
42 m_descriptionsBox
= new QComboBox(this);
44 m_comparatorBox
= new QComboBox(this);
45 m_comparatorBox
->setSizeAdjustPolicy(QComboBox::AdjustToContents
);
48 const int index
= static_cast<int>(type
);
49 m_descriptionsBox
->setCurrentIndex(index
);
51 QWidget
* filler
= new QWidget(this);
52 filler
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
54 m_removeButton
= new QPushButton(this);
55 m_removeButton
->setIcon(KIcon("list-remove"));
56 m_removeButton
->setToolTip(i18nc("@info", "Remove search option"));
57 m_removeButton
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
58 connect(m_removeButton
, SIGNAL(clicked()), this, SIGNAL(removeCriterion()));
60 m_layout
= new QHBoxLayout(this);
61 m_layout
->setMargin(0);
62 m_layout
->addWidget(m_descriptionsBox
);
63 m_layout
->addWidget(m_comparatorBox
);
64 m_layout
->addWidget(filler
);
65 m_layout
->addWidget(m_removeButton
);
69 slotDescriptionChanged(index
);
70 connect(m_descriptionsBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDescriptionChanged(int)));
73 SearchCriterionSelector::~SearchCriterionSelector()
77 QString
SearchCriterionSelector::toString() const
79 if (m_valueWidget
== 0) {
83 const int descIndex
= m_descriptionsBox
->currentIndex();
84 const SearchCriterionDescription
& descr
= m_descriptions
[descIndex
];
86 const int compIndex
= m_comparatorBox
->currentIndex();
87 const SearchCriterionDescription::Comparator
& comp
= descr
.comparators()[compIndex
];
88 if (comp
.operation
.isEmpty()) {
92 QString criterion
= comp
.prefix
+ descr
.identifier() + comp
.operation
;
93 if (!m_valueWidget
->value().isEmpty()) {
94 criterion
+= '"' + m_valueWidget
->value() + '"';
99 void SearchCriterionSelector::slotDescriptionChanged(int index
)
101 if (m_valueWidget
!= 0) {
102 m_valueWidget
->hide();
103 m_layout
->removeWidget(m_valueWidget
);
105 // the value widget is obtained by the Search Criterion
106 // Selector instance and may not get deleted
109 // add comparator items
110 disconnect(m_comparatorBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
111 m_comparatorBox
->clear();
113 const SearchCriterionDescription
& description
= m_descriptions
[index
];
114 foreach (const SearchCriterionDescription::Comparator
& comp
, description
.comparators()) {
115 m_comparatorBox
->addItem(comp
.name
);
119 m_valueWidget
= description
.valueWidget();
120 m_layout
->insertWidget(2, m_valueWidget
);
122 m_comparatorBox
->setCurrentIndex(0);
123 slotComparatorChanged(0);
124 connect(m_comparatorBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
127 void SearchCriterionSelector::slotComparatorChanged(int index
)
129 Q_ASSERT(index
>= 0);
131 // only show the value widget if an operation is defined by the comparator
132 const int descIndex
= m_descriptionsBox
->currentIndex();
133 const SearchCriterionDescription
& descr
= m_descriptions
[descIndex
];
134 const SearchCriterionDescription::Comparator
& comp
= descr
.comparators()[index
];
135 m_valueWidget
->setVisible(!comp
.operation
.isEmpty());
137 emit
criterionChanged();
140 void SearchCriterionSelector::createDescriptions()
142 Q_ASSERT(m_descriptionsBox
!= 0);
143 Q_ASSERT(m_comparatorBox
!= 0);
145 // TODO: maybe this creation should be forwarded to a factory if
146 // the number of items increases in future
147 QList
<SearchCriterionDescription::Comparator
> defaultComps
;
148 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than"), ">", "+"));
149 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than or Equal to"), ">=", "+"));
150 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than"), "<", "+"));
151 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than or Equal to"), "<=", "+"));
153 // add "Date" description
154 QList
<SearchCriterionDescription::Comparator
> dateComps
;
155 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Anytime"))); // TODO
156 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Today"))); // TODO
157 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "This week"))); // TODO
158 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "This month"))); // TODO
159 foreach (const SearchCriterionDescription::Comparator
& comp
, defaultComps
) {
160 dateComps
.append(comp
);
163 DateValue
* dateValue
= new DateValue(this);
165 SearchCriterionDescription
date(i18nc("@label", "Date:"),
170 // add "Size" description
171 QList
<SearchCriterionDescription::Comparator
> sizeComps
= defaultComps
;
172 sizeComps
.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (file size)", "Any")));
174 SizeValue
* sizeValue
= new SizeValue(this);
176 SearchCriterionDescription
size(i18nc("@label", "Size:"),
181 // add "Tag" description
182 QList
<SearchCriterionDescription::Comparator
> tagComps
;
183 tagComps
.append(SearchCriterionDescription::Comparator(i18nc("@label All (tags)", "All")));
184 tagComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Equal to"), ":"));
186 TagValue
* tagValue
= new TagValue(this);
188 SearchCriterionDescription
tag(i18nc("@label", "Tag:"),
193 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Date
) == 0);
194 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Size
) == 1);
195 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Tag
) == 2);
196 m_descriptions
.append(date
);
197 m_descriptions
.append(size
);
198 m_descriptions
.append(tag
);
200 // add all descriptions to the combo box and connect the value widgets
202 foreach (const SearchCriterionDescription
& desc
, m_descriptions
) {
203 m_descriptionsBox
->addItem(desc
.name(), i
);
204 connect(desc
.valueWidget(), SIGNAL(valueChanged(QString
)), this, SIGNAL(criterionChanged()));
209 #include "searchcriterionselector.moc"