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
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
57 connect(m_removeButton
, SIGNAL(clicked()), this, SIGNAL(removeCriterion()));
59 m_layout
= new QHBoxLayout(this);
60 m_layout
->setMargin(0);
61 m_layout
->addWidget(m_descriptionsBox
);
62 m_layout
->addWidget(m_comparatorBox
);
63 m_layout
->addWidget(filler
);
64 m_layout
->addWidget(m_removeButton
);
68 slotDescriptionChanged(index
);
69 connect(m_descriptionsBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDescriptionChanged(int)));
72 SearchCriterionSelector::~SearchCriterionSelector()
76 void SearchCriterionSelector::createDescriptions()
78 Q_ASSERT(m_descriptionsBox
!= 0);
79 Q_ASSERT(m_comparatorBox
!= 0);
81 // TODO: maybe this creation should be forwarded to a factory if
82 // the number of items increases in future
83 QList
<SearchCriterionDescription::Comparator
> defaultComps
;
84 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than"), ">", "+"));
85 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than or Equal to"), ">=", "+"));
86 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than"), "<", "+"));
87 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than or Equal to"), "<=", "+"));
89 // add "Date" description
90 QList
<SearchCriterionDescription::Comparator
> dateComps
;
91 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Anytime"))); // TODO
92 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Today"))); // TODO
93 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "This week"))); // TODO
94 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "This month"))); // TODO
95 foreach (const SearchCriterionDescription::Comparator
& comp
, defaultComps
) {
96 dateComps
.append(comp
);
99 DateValue
* dateValue
= new DateValue(this);
101 SearchCriterionDescription
date(i18nc("@label", "Date"),
106 // add "Size" description
107 QList
<SearchCriterionDescription::Comparator
> sizeComps
= defaultComps
;
108 sizeComps
.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (file size)", "Any")));
110 SizeValue
* sizeValue
= new SizeValue(this);
112 SearchCriterionDescription
size(i18nc("@label", "Size"),
117 // add "Tag" description
118 QList
<SearchCriterionDescription::Comparator
> tagComps
;
119 tagComps
.append(SearchCriterionDescription::Comparator(i18nc("@label All (tags)", "All")));
120 tagComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Equal to"), "=="));
122 TagValue
* tagValue
= new TagValue(this);
124 SearchCriterionDescription
tag(i18nc("@label", "Tag"),
129 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Date
) == 0);
130 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Size
) == 1);
131 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Tag
) == 2);
132 m_descriptions
.append(date
);
133 m_descriptions
.append(size
);
134 m_descriptions
.append(tag
);
136 // add all descriptions to the combo box
137 const int count
= m_descriptions
.count();
138 for (int i
= 0; i
< count
; ++i
) {
139 m_descriptionsBox
->addItem(m_descriptions
[i
].name(), i
);
143 void SearchCriterionSelector::slotDescriptionChanged(int index
)
145 if (m_valueWidget
!= 0) {
146 m_valueWidget
->hide();
147 m_layout
->removeWidget(m_valueWidget
);
149 // the value widget is obtained by the Search Criterion
150 // Selector instance and may not get deleted
153 // add comparator items
154 disconnect(m_comparatorBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
155 m_comparatorBox
->clear();
157 const SearchCriterionDescription
& description
= m_descriptions
[index
];
158 foreach (const SearchCriterionDescription::Comparator
& comp
, description
.comparators()) {
159 m_comparatorBox
->addItem(comp
.name
);
163 m_valueWidget
= description
.valueWidget();
164 m_layout
->insertWidget(2, m_valueWidget
);
166 m_comparatorBox
->setCurrentIndex(0);
167 slotComparatorChanged(0);
168 connect(m_comparatorBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
171 void SearchCriterionSelector::slotComparatorChanged(int index
)
173 Q_ASSERT(index
>= 0);
175 // only show the value widget if an operation is defined by the comparator
176 const int descIndex
= m_descriptionsBox
->currentIndex();
177 const SearchCriterionDescription
& descr
= m_descriptions
[descIndex
];
178 const SearchCriterionDescription::Comparator
& comp
= descr
.comparators()[index
];
179 m_valueWidget
->setVisible(!comp
.operation
.isEmpty());
181 // create query string
182 const QString queryString
= comp
.prefix
+ descr
.identifier() + comp
.operation
+ m_valueWidget
->value();
183 emit
criterionChanged(queryString
);
186 #include "searchcriterionselector.moc"