]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/filters/datesearchfilterwidget.cpp
The &-shortcut from another action is not set until the action has been shown at...
[dolphin.git] / src / search / filters / datesearchfilterwidget.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 "datesearchfilterwidget.h"
21
22 #include <klocale.h>
23 #include <nepomuk/comparisonterm.h>
24 #include <nepomuk/literalterm.h>
25 #include <nepomuk/orterm.h>
26 #include <nepomuk/property.h>
27 #include <nepomuk/query.h>
28 #include "nie.h"
29 #include <QDate>
30 #include <QDateTime>
31 #include <QLabel>
32 #include <QPushButton>
33 #include <QHBoxLayout>
34
35 DateSearchFilterWidget::DateSearchFilterWidget(QWidget* parent) :
36 AbstractSearchFilterWidget(parent),
37 m_dateButtons()
38 {
39 QHBoxLayout* layout = new QHBoxLayout(this);
40 layout->setSpacing(0);
41
42 for (int i = Today; i <= ThisYear; ++i) {
43 QPushButton* button = createButton();
44 switch (i) {
45 case Today: button->setText(i18nc("@action:button", "Today")); break;
46 case Yesterday: button->setText(i18nc("@action:button", "Yesterday")); break;
47 case ThisWeek: button->setText(i18nc("@action:button", "This Week")); break;
48 case ThisMonth: button->setText(i18nc("@action:button", "This Month")); break;
49 case ThisYear: button->setText(i18nc("@action:button", "This Year")); break;
50 default: Q_ASSERT(false);
51 }
52
53 layout->addWidget(button);
54 m_dateButtons.append(button);
55 }
56 layout->addStretch(1);
57 }
58
59 DateSearchFilterWidget::~DateSearchFilterWidget()
60 {
61 }
62
63
64 QString DateSearchFilterWidget::filterLabel() const
65 {
66 return i18nc("@title:group", "Date");
67 }
68
69 Nepomuk::Query::Term DateSearchFilterWidget::queryTerm() const
70 {
71 Nepomuk::Query::OrTerm orTerm;
72
73 int index = 0;
74 foreach (const QPushButton* button, m_dateButtons) {
75 if (button->isChecked()) {
76 QDate today = QDate::currentDate();
77 QDate date;
78 switch (index) {
79 case Today:
80 // Current date is already set
81 break;
82 case Yesterday:
83 date.addDays(-1);
84 break;
85 case ThisWeek:
86 date.addDays(-today.dayOfWeek());
87 break;
88 case ThisMonth:
89 date = QDate(today.year(), today.month(), 1);
90 break;
91 case ThisYear:
92 date = QDate(today.year(), 1, 1);
93 break;
94 default:
95 Q_ASSERT(false);
96 }
97
98 const QDateTime dateTime(date);
99 const Nepomuk::Query::LiteralTerm term(dateTime);
100 const Nepomuk::Query::ComparisonTerm compTerm(Nepomuk::Vocabulary::NIE::lastModified(),
101 term,
102 Nepomuk::Query::ComparisonTerm::GreaterOrEqual);
103 orTerm.addSubTerm(compTerm);
104 }
105 ++index;
106 }
107
108 return orTerm;
109 }
110
111 #include "datesearchfilterwidget.moc"