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(QWidget
* parent
) :
42 m_descriptionsBox
= new QComboBox(this);
43 m_descriptionsBox
->addItem(i18nc("@label", "Select..."), -1);
45 connect(m_descriptionsBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDescriptionChanged(int)));
47 m_comparatorBox
= new QComboBox(this);
48 m_comparatorBox
->hide();
49 connect(m_comparatorBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(updateQuery()));
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
);
69 SearchCriterionSelector::~SearchCriterionSelector()
73 void SearchCriterionSelector::createDescriptions()
75 // TODO: maybe this creation should be forwarded to a factory if
76 // the number of items increases in future
78 QList
<SearchCriterionDescription::Comparator
> comparators
;
79 comparators
.append(SearchCriterionDescription::Comparator(i18nc("@label", "greater than"), ">", "+"));
80 comparators
.append(SearchCriterionDescription::Comparator(i18nc("@label", "greater than or equal to"), ">=", "+"));
81 comparators
.append(SearchCriterionDescription::Comparator(i18nc("@label", "less than"), "<", "+"));
82 comparators
.append(SearchCriterionDescription::Comparator(i18nc("@label", "less than or equal to"), "<=", "+"));
84 // add "Date" description
85 DateValue
* dateValue
= new DateValue(this);
87 SearchCriterionDescription
date(i18nc("@label", "Date Modified"),
92 // add "File Size" description
93 FileSizeValue
* fileSizeValue
= new FileSizeValue(this);
94 fileSizeValue
->hide();
95 SearchCriterionDescription
size(i18nc("@label", "File Size"),
100 m_descriptions
.append(date
);
101 m_descriptions
.append(size
);
103 // add all descriptions to the combo box
104 const int count
= m_descriptions
.count();
105 for (int i
= 0; i
< count
; ++i
) {
106 m_descriptionsBox
->addItem(m_descriptions
[i
].name(), i
);
110 void SearchCriterionSelector::slotDescriptionChanged(int index
)
112 m_comparatorBox
->clear();
113 m_comparatorBox
->show();
114 if (m_valueWidget
!= 0) {
115 m_layout
->removeWidget(m_valueWidget
);
116 // the value widget is obtained by the Search Criterion
117 // Selector instance and may not get deleted
120 // adjust the comparator box and the value widget dependent from the selected description
121 m_comparatorBox
->addItem(i18nc("@label", "Select..."), -1);
122 const int descrIndex
= m_descriptionsBox
->itemData(index
).toInt();
123 if (descrIndex
>= 0) {
124 // add comparator items
125 const SearchCriterionDescription
& description
= m_descriptions
[descrIndex
];
126 foreach (const SearchCriterionDescription::Comparator
& comp
, description
.comparators()) {
127 m_comparatorBox
->addItem(comp
.name
);
131 m_valueWidget
= description
.valueWidget();
132 const int layoutIndex
= m_layout
->count() - 2;
133 m_layout
->insertWidget(layoutIndex
, m_valueWidget
);
134 m_valueWidget
->show();
138 void SearchCriterionSelector::updateQuery()
140 const SearchCriterionDescription
* descr
= description();
142 // no description has been selected
146 // get selected comparator related to the description
147 const int compBoxIndex
= m_comparatorBox
->currentIndex();
148 const int compIndex
= m_comparatorBox
->itemData(compBoxIndex
).toInt();
150 // no comparator has been selected
154 // create query string
155 const SearchCriterionDescription::Comparator
& comp
= descr
->comparators()[compIndex
];
156 const QString queryString
= comp
.prefix
+ descr
->identifier() + comp
.operation
+ m_valueWidget
->value();
157 emit
criterionChanged(queryString
);
160 const SearchCriterionDescription
* SearchCriterionSelector::description() const
162 const int descrBoxIndex
= m_descriptionsBox
->currentIndex();
163 const int descrIndex
= m_descriptionsBox
->itemData(descrBoxIndex
).toInt();
164 return (descrIndex
< 0) ? 0 : &m_descriptions
[descrIndex
];
167 #include "searchcriterionselector.moc"