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 #define DISABLE_NEPOMUK_LEGACY
24 #include <nepomuk/comparisonterm.h>
25 #include <nepomuk/literalterm.h>
26 #include <nepomuk/query.h>
29 #include "searchcriterionvalue.h"
31 #include <Soprano/LiteralValue>
32 #include <Soprano/Vocabulary/NAO>
35 #include <QHBoxLayout>
37 #include <QPushButton>
42 SearchCriterionSelector::SearchCriterionSelector(Type type
, QWidget
* parent
) :
51 m_descriptionsBox
= new QComboBox(this);
53 m_comparatorBox
= new QComboBox(this);
54 m_comparatorBox
->setSizeAdjustPolicy(QComboBox::AdjustToContents
);
57 const int index
= static_cast<int>(type
);
58 m_descriptionsBox
->setCurrentIndex(index
);
60 QWidget
* filler
= new QWidget(this);
61 filler
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
63 m_removeButton
= new QPushButton(this);
64 m_removeButton
->setIcon(KIcon("list-remove"));
65 m_removeButton
->setToolTip(i18nc("@info", "Remove search option"));
66 m_removeButton
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
67 connect(m_removeButton
, SIGNAL(clicked()), this, SIGNAL(removeCriterion()));
69 m_layout
= new QHBoxLayout(this);
70 m_layout
->setMargin(0);
71 m_layout
->addWidget(m_removeButton
);
72 m_layout
->addWidget(m_descriptionsBox
);
73 m_layout
->addWidget(m_comparatorBox
);
74 m_layout
->addWidget(filler
);
78 slotDescriptionChanged(index
);
79 connect(m_descriptionsBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDescriptionChanged(int)));
82 SearchCriterionSelector::~SearchCriterionSelector()
86 Nepomuk::Query::Term
SearchCriterionSelector::queryTerm() const
88 if (m_valueWidget
== 0) {
89 return Nepomuk::Query::Term();
92 const int descIndex
= m_descriptionsBox
->currentIndex();
93 const SearchCriterionDescription
& descr
= m_descriptions
[descIndex
];
95 const int compIndex
= m_comparatorBox
->currentIndex();
96 const SearchCriterionDescription::Comparator
& comp
= descr
.comparators()[compIndex
];
98 return Nepomuk::Query::Term();
101 const Nepomuk::Query::ComparisonTerm
term(descr
.identifier(),
102 m_valueWidget
->value(),
107 SearchCriterionSelector::Type
SearchCriterionSelector::type() const
109 return static_cast<Type
>(m_descriptionsBox
->currentIndex());
112 void SearchCriterionSelector::slotDescriptionChanged(int index
)
114 if (m_valueWidget
!= 0) {
115 m_valueWidget
->hide();
116 m_layout
->removeWidget(m_valueWidget
);
118 // the value widget is obtained by the Search Criterion
119 // Selector instance and may not get deleted
122 // add comparator items
123 disconnect(m_comparatorBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
124 m_comparatorBox
->clear();
126 const SearchCriterionDescription
& description
= m_descriptions
[index
];
127 foreach (const SearchCriterionDescription::Comparator
& comp
, description
.comparators()) {
128 m_comparatorBox
->addItem(comp
.name
);
132 m_valueWidget
= description
.valueWidget();
133 m_layout
->insertWidget(3, m_valueWidget
);
135 m_comparatorBox
->setCurrentIndex(0);
136 slotComparatorChanged(0);
137 connect(m_comparatorBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
140 void SearchCriterionSelector::slotComparatorChanged(int index
)
142 Q_ASSERT(index
>= 0);
144 // only show the value widget if an operation is defined by the comparator
145 const int descIndex
= m_descriptionsBox
->currentIndex();
146 const SearchCriterionDescription
& descr
= m_descriptions
[descIndex
];
147 const SearchCriterionDescription::Comparator
& comp
= descr
.comparators()[index
];
149 m_valueWidget
->initializeValue(comp
.autoValueType
);
150 // only show the value widget, if an operation is defined
151 // and no automatic calculation is provided
152 m_valueWidget
->setVisible(comp
.isActive
&& comp
.autoValueType
.isEmpty());
154 emit
criterionChanged();
157 void SearchCriterionSelector::createDescriptions()
159 Q_ASSERT(m_descriptionsBox
!= 0);
160 Q_ASSERT(m_comparatorBox
!= 0);
162 // TODO: maybe this creation should be forwarded to a factory if
163 // the number of items increases in future
164 QList
<SearchCriterionDescription::Comparator
> defaultComps
;
165 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than"), Nepomuk::Query::ComparisonTerm::Greater
));
166 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than or Equal to"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual
));
167 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than"), Nepomuk::Query::ComparisonTerm::Smaller
));
168 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than or Equal to"), Nepomuk::Query::ComparisonTerm::SmallerOrEqual
));
170 // add "Date" description
171 QList
<SearchCriterionDescription::Comparator
> dateComps
;
172 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Anytime")));
173 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Today"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual
, "today"));
174 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Week"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual
, "thisWeek"));
175 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Month"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual
, "thisMonth"));
176 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Year"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual
, "thisYear"));
177 foreach (const SearchCriterionDescription::Comparator
& comp
, defaultComps
) {
178 dateComps
.append(comp
);
181 DateValue
* dateValue
= new DateValue(this);
183 SearchCriterionDescription
date(i18nc("@label", "Date:"),
184 Nepomuk::Vocabulary::NIE::lastModified(),
187 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Date
) == 0);
188 m_descriptions
.append(date
);
190 // add "Size" description
191 QList
<SearchCriterionDescription::Comparator
> sizeComps
= defaultComps
;
192 sizeComps
.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (file size)", "Any")));
194 SizeValue
* sizeValue
= new SizeValue(this);
196 SearchCriterionDescription
size(i18nc("@label", "Size:"),
197 Soprano::Vocabulary::NAO::lastModified(), // TODO
200 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Size
) == 1);
201 m_descriptions
.append(size
);
203 // add "Tag" description
204 QList
<SearchCriterionDescription::Comparator
> tagComps
;
205 tagComps
.append(SearchCriterionDescription::Comparator(i18nc("@label All (tags)", "All")));
206 tagComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Equal to"), Nepomuk::Query::ComparisonTerm::Equal
));
207 // It is unclear yet how to express != for tags with the new Nepomuk query API. Disable it for KDE 4.4,
208 // but leave the translation string there to be able to enable this functionality for KDE 4.4.x:
209 const QString dummy
= i18nc("@label", "Not Equal to");
211 //tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Not Equal to"), Nepomuk::Query::ComparisonTerm::Equal)); // TODO
213 TagValue
* tagValue
= new TagValue(this);
215 SearchCriterionDescription
tag(i18nc("@label", "Tag:"),
216 Soprano::Vocabulary::NAO::hasTag(),
219 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Tag
) == 2);
220 m_descriptions
.append(tag
);
222 // add "Rating" description
223 QList
<SearchCriterionDescription::Comparator
> ratingComps
= defaultComps
;
224 ratingComps
.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (rating)", "Any")));
226 RatingValue
* ratingValue
= new RatingValue(this);
228 SearchCriterionDescription
rating(i18nc("@label", "Rating:"),
229 Soprano::Vocabulary::NAO::numericRating(),
232 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Rating
) == 3);
233 m_descriptions
.append(rating
);
235 // add all descriptions to the combo box and connect the value widgets
237 foreach (const SearchCriterionDescription
& desc
, m_descriptions
) {
238 m_descriptionsBox
->addItem(desc
.name(), i
);
239 connect(desc
.valueWidget(), SIGNAL(valueChanged(Nepomuk::Query::LiteralTerm
)), this, SIGNAL(criterionChanged()));
244 #include "searchcriterionselector.moc"