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>
28 #include "searchcriterionvalue.h"
30 #include <Soprano/LiteralValue>
31 #include <Soprano/Vocabulary/NAO>
34 #include <QHBoxLayout>
36 #include <QPushButton>
41 SearchCriterionSelector::SearchCriterionSelector(Type type
, QWidget
* parent
) :
50 m_descriptionsBox
= new QComboBox(this);
52 m_comparatorBox
= new QComboBox(this);
53 m_comparatorBox
->setSizeAdjustPolicy(QComboBox::AdjustToContents
);
56 const int index
= static_cast<int>(type
);
57 m_descriptionsBox
->setCurrentIndex(index
);
59 QWidget
* filler
= new QWidget(this);
60 filler
->setSizePolicy(QSizePolicy::Expanding
, QSizePolicy::Preferred
);
62 m_removeButton
= new QPushButton(this);
63 m_removeButton
->setIcon(KIcon("list-remove"));
64 m_removeButton
->setToolTip(i18nc("@info", "Remove search option"));
65 m_removeButton
->setSizePolicy(QSizePolicy::Fixed
, QSizePolicy::Fixed
);
66 connect(m_removeButton
, SIGNAL(clicked()), this, SIGNAL(removeCriterion()));
68 m_layout
= new QHBoxLayout(this);
69 m_layout
->setMargin(0);
70 m_layout
->addWidget(m_removeButton
);
71 m_layout
->addWidget(m_descriptionsBox
);
72 m_layout
->addWidget(m_comparatorBox
);
73 m_layout
->addWidget(filler
);
77 slotDescriptionChanged(index
);
78 connect(m_descriptionsBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDescriptionChanged(int)));
81 SearchCriterionSelector::~SearchCriterionSelector()
85 Nepomuk::Query::Term
SearchCriterionSelector::queryTerm() const
87 if (m_valueWidget
== 0) {
88 return Nepomuk::Query::Term();
91 const int descIndex
= m_descriptionsBox
->currentIndex();
92 const SearchCriterionDescription
& descr
= m_descriptions
[descIndex
];
94 const int compIndex
= m_comparatorBox
->currentIndex();
95 const SearchCriterionDescription::Comparator
& comp
= descr
.comparators()[compIndex
];
97 return Nepomuk::Query::Term();
100 const Nepomuk::Query::ComparisonTerm
term(descr
.identifier(),
101 m_valueWidget
->value(),
106 SearchCriterionSelector::Type
SearchCriterionSelector::type() const
108 return static_cast<Type
>(m_descriptionsBox
->currentIndex());
111 void SearchCriterionSelector::slotDescriptionChanged(int index
)
113 if (m_valueWidget
!= 0) {
114 m_valueWidget
->hide();
115 m_layout
->removeWidget(m_valueWidget
);
117 // the value widget is obtained by the Search Criterion
118 // Selector instance and may not get deleted
121 // add comparator items
122 disconnect(m_comparatorBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
123 m_comparatorBox
->clear();
125 const SearchCriterionDescription
& description
= m_descriptions
[index
];
126 foreach (const SearchCriterionDescription::Comparator
& comp
, description
.comparators()) {
127 m_comparatorBox
->addItem(comp
.name
);
131 m_valueWidget
= description
.valueWidget();
132 m_layout
->insertWidget(3, m_valueWidget
);
134 m_comparatorBox
->setCurrentIndex(0);
135 slotComparatorChanged(0);
136 connect(m_comparatorBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(slotComparatorChanged(int)));
139 void SearchCriterionSelector::slotComparatorChanged(int index
)
141 Q_ASSERT(index
>= 0);
143 // only show the value widget if an operation is defined by the comparator
144 const int descIndex
= m_descriptionsBox
->currentIndex();
145 const SearchCriterionDescription
& descr
= m_descriptions
[descIndex
];
146 const SearchCriterionDescription::Comparator
& comp
= descr
.comparators()[index
];
148 m_valueWidget
->initializeValue(comp
.autoValueType
);
149 // only show the value widget, if an operation is defined
150 // and no automatic calculation is provided
151 m_valueWidget
->setVisible(comp
.isActive
&& comp
.autoValueType
.isEmpty());
153 emit
criterionChanged();
156 void SearchCriterionSelector::createDescriptions()
158 Q_ASSERT(m_descriptionsBox
!= 0);
159 Q_ASSERT(m_comparatorBox
!= 0);
161 // TODO: maybe this creation should be forwarded to a factory if
162 // the number of items increases in future
163 QList
<SearchCriterionDescription::Comparator
> defaultComps
;
164 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than"), Nepomuk::Query::ComparisonTerm::Greater
));
165 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Greater Than or Equal to"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual
));
166 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than"), Nepomuk::Query::ComparisonTerm::Smaller
));
167 defaultComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Less Than or Equal to"), Nepomuk::Query::ComparisonTerm::SmallerOrEqual
));
169 // add "Date" description
170 QList
<SearchCriterionDescription::Comparator
> dateComps
;
171 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Anytime")));
172 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Today"), Nepomuk::Query::ComparisonTerm::Equal
, "today"));
173 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Week"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual
, "thisWeek"));
174 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Month"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual
, "thisMonth"));
175 dateComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "This Year"), Nepomuk::Query::ComparisonTerm::GreaterOrEqual
, "thisYear"));
176 foreach (const SearchCriterionDescription::Comparator
& comp
, defaultComps
) {
177 dateComps
.append(comp
);
180 DateValue
* dateValue
= new DateValue(this);
182 SearchCriterionDescription
date(i18nc("@label", "Date:"),
183 Soprano::Vocabulary::NAO::lastModified(),
186 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Date
) == 0);
187 m_descriptions
.append(date
);
189 // add "Size" description
190 QList
<SearchCriterionDescription::Comparator
> sizeComps
= defaultComps
;
191 sizeComps
.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (file size)", "Any")));
193 SizeValue
* sizeValue
= new SizeValue(this);
195 SearchCriterionDescription
size(i18nc("@label", "Size:"),
196 Soprano::Vocabulary::NAO::lastModified(), // TODO
199 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Size
) == 1);
200 m_descriptions
.append(size
);
202 // add "Tag" description
203 QList
<SearchCriterionDescription::Comparator
> tagComps
;
204 tagComps
.append(SearchCriterionDescription::Comparator(i18nc("@label All (tags)", "All")));
205 tagComps
.append(SearchCriterionDescription::Comparator(i18nc("@label", "Equal to"), Nepomuk::Query::ComparisonTerm::Equal
));
206 // It is unclear yet how to express != for tags with the new Nepomuk query API. Disable it for KDE 4.4,
207 // but leave the translation string there to be able to enable this functionality for KDE 4.4.x:
208 const QString dummy
= i18nc("@label", "Not Equal to");
210 //tagComps.append(SearchCriterionDescription::Comparator(i18nc("@label", "Not Equal to"), Nepomuk::Query::ComparisonTerm::Equal)); // TODO
212 TagValue
* tagValue
= new TagValue(this);
214 SearchCriterionDescription
tag(i18nc("@label", "Tag:"),
215 Soprano::Vocabulary::NAO::hasTag(),
218 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Tag
) == 2);
219 m_descriptions
.append(tag
);
221 // add "Rating" description
222 QList
<SearchCriterionDescription::Comparator
> ratingComps
= defaultComps
;
223 ratingComps
.insert(0, SearchCriterionDescription::Comparator(i18nc("@label Any (rating)", "Any")));
225 RatingValue
* ratingValue
= new RatingValue(this);
227 SearchCriterionDescription
rating(i18nc("@label", "Rating:"),
228 Soprano::Vocabulary::NAO::numericRating(),
231 Q_ASSERT(static_cast<int>(SearchCriterionSelector::Rating
) == 3);
232 m_descriptions
.append(rating
);
234 // add all descriptions to the combo box and connect the value widgets
236 foreach (const SearchCriterionDescription
& desc
, m_descriptions
) {
237 m_descriptionsBox
->addItem(desc
.name(), i
);
238 connect(desc
.valueWidget(), SIGNAL(valueChanged(Nepomuk::Query::LiteralTerm
)), this, SIGNAL(criterionChanged()));
243 #include "searchcriterionselector.moc"