]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/filters/ratingsearchfilterwidget.cpp
showMenuBar is deprecated, use showHideMenuBar instead.
[dolphin.git] / src / search / filters / ratingsearchfilterwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 * **************************************************************************/
19
20 #include "ratingsearchfilterwidget.h"
21
22 #include <klocale.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>
29 #include "nie.h"
30 #include <Soprano/LiteralValue>
31 #include <Soprano/Vocabulary/NAO>
32 #include <QFontMetrics>
33 #include <QIcon>
34 #include <QLabel>
35 #include <QPainter>
36 #include <QPushButton>
37 #include <QHBoxLayout>
38
39 namespace {
40 // Only show the ratings 0, 2, 4, ... 10
41 const int RatingInc = 2;
42 };
43
44 RatingSearchFilterWidget::RatingSearchFilterWidget(QWidget* parent) :
45 AbstractSearchFilterWidget(parent),
46 m_ratingButtons()
47 {
48 QHBoxLayout* layout = new QHBoxLayout(this);
49 layout->setSpacing(0);
50
51 QFontMetrics fontMetrics(font());
52 const int iconHeight = fontMetrics.height();
53
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);
58
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);
65
66 // Create button with the rating pixmap as icon
67 QPushButton* button = createButton();
68 button->setIconSize(iconSize);
69 button->setIcon(QIcon(pixmap));
70
71 layout->addWidget(button);
72 m_ratingButtons.append(button);
73 }
74
75 layout->addStretch(1);
76 }
77
78 RatingSearchFilterWidget::~RatingSearchFilterWidget()
79 {
80 }
81
82 QString RatingSearchFilterWidget::filterLabel() const
83 {
84 return i18nc("@title:group", "Rating");
85 }
86
87 Nepomuk::Query::Term RatingSearchFilterWidget::queryTerm() const
88 {
89 Nepomuk::Query::OrTerm orTerm;
90
91 int rating = 0;
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(),
96 term,
97 Nepomuk::Query::ComparisonTerm::Equal);
98 orTerm.addSubTerm(compTerm);
99 }
100 rating += RatingInc;
101 }
102
103 return orTerm;
104 }
105
106 #include "ratingsearchfilterwidget.moc"