1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 * **************************************************************************/
20 #include "ratingsearchfilterwidget.h"
23 #include <nepomuk/comparisonterm.h>
24 #include <nepomuk/literalterm.h>
25 #include <nepomuk/orterm.h>
26 #include <nepomuk/kratingpainter.h>
27 #include <nepomuk/property.h>
28 #include <nepomuk/query.h>
30 #include <Soprano/LiteralValue>
31 #include <Soprano/Vocabulary/NAO>
32 #include <QFontMetrics>
36 #include <QPushButton>
37 #include <QHBoxLayout>
40 // Only show the ratings 0, 2, 4, ... 10
41 const int RatingInc
= 2;
44 RatingSearchFilterWidget::RatingSearchFilterWidget(QWidget
* parent
) :
45 AbstractSearchFilterWidget(parent
),
48 QHBoxLayout
* layout
= new QHBoxLayout(this);
49 layout
->setSpacing(0);
51 QFontMetrics
fontMetrics(font());
52 const int iconHeight
= fontMetrics
.height();
54 KRatingPainter ratingPainter
;
55 const int maxRating
= ratingPainter
.maxRating();
56 const QSize
iconSize(iconHeight
* (maxRating
/ 2), iconHeight
);
57 const QRect
paintRect(QPoint(0, 0), iconSize
);
59 for (int rating
= 0; rating
<= ratingPainter
.maxRating(); rating
+= RatingInc
) {
60 // Create pixmap that represents the rating
61 QPixmap
pixmap(iconSize
);
62 pixmap
.fill(Qt::transparent
);
63 QPainter
painter(&pixmap
);
64 ratingPainter
.paint(&painter
, paintRect
, rating
);
66 // Create button with the rating pixmap as icon
67 QPushButton
* button
= createButton();
68 button
->setIconSize(iconSize
);
69 button
->setIcon(QIcon(pixmap
));
71 layout
->addWidget(button
);
72 m_ratingButtons
.append(button
);
75 layout
->addStretch(1);
78 RatingSearchFilterWidget::~RatingSearchFilterWidget()
82 QString
RatingSearchFilterWidget::filterLabel() const
84 return i18nc("@title:group", "Rating");
87 Nepomuk::Query::Term
RatingSearchFilterWidget::queryTerm() const
89 Nepomuk::Query::OrTerm orTerm
;
92 foreach (const QPushButton
* ratingButton
, m_ratingButtons
) {
93 if (ratingButton
->isChecked()) {
94 const Nepomuk::Query::LiteralTerm
term(rating
);
95 const Nepomuk::Query::ComparisonTerm
compTerm(Soprano::Vocabulary::NAO::numericRating(),
97 Nepomuk::Query::ComparisonTerm::Equal
);
98 orTerm
.addSubTerm(compTerm
);
106 #include "ratingsearchfilterwidget.moc"