┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/search/searchcriterionvalue.cpp
blob: 065100e4ed626905ffb7708c55265094b3170576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/***************************************************************************
 *   Copyright (C) 2009 by Adam Kidder <[email protected]>               *
 *   Copyright (C) 2009 by Peter Penz <[email protected]>                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
 ***************************************************************************/

#include "searchcriterionvalue.h"

#include <kdatewidget.h>
#include <klineedit.h>
#include <klocale.h>

#include <nepomuk/kratingwidget.h>
#include <nepomuk/tag.h>

#include <QComboBox>
#include <QDate>
#include <QIntValidator>
#include <QLabel>
#include <QHBoxLayout>
#include <QShowEvent>

SearchCriterionValue::SearchCriterionValue(QWidget* parent) :
    QWidget(parent)
{
}

SearchCriterionValue::~SearchCriterionValue()
{
}

void SearchCriterionValue::initializeValue(const QString& valueType)
{
    Q_UNUSED(valueType);
}

// -------------------------------------------------------------------------

DateValue::DateValue(QWidget* parent) :
    SearchCriterionValue(parent),
    m_dateWidget(0)
{
    m_dateWidget = new KDateWidget(QDate::currentDate(), this);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setMargin(0);
    layout->addWidget(m_dateWidget);
}

DateValue::~DateValue()
{
}

Nepomuk::Query::LiteralTerm DateValue::value() const
{
    const QDateTime dateTime(m_dateWidget->date());
    return Nepomuk::Query::LiteralTerm(dateTime);
}

void DateValue::initializeValue(const QString& valueType)
{
    QDate date;    
    if (valueType.isEmpty() || (valueType == "today")) {
        date = QDate::currentDate();
    } else if (valueType == "thisWeek") {
        const QDate today = QDate::currentDate();
        const int dayOfWeek = today.dayOfWeek();
        date = today.addDays(-dayOfWeek);
    } else if (valueType == "thisMonth") {
        const QDate today = QDate::currentDate();
        date = QDate(today.year(), today.month(), 1);
    } else if (valueType == "thisYear") {
        date = QDate(QDate::currentDate().year(), 1, 1);
    } else {
        // unknown value-type
        Q_ASSERT(false);
    }
    m_dateWidget->setDate(date);
}

// -------------------------------------------------------------------------

TagValue::TagValue(QWidget* parent) :
    SearchCriterionValue(parent),
    m_tags(0)
{
    m_tags = new QComboBox(this);
    m_tags->setInsertPolicy(QComboBox::InsertAlphabetically);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setMargin(0);
    layout->addWidget(m_tags);

    connect(m_tags, SIGNAL(activated(QString)),
            this, SIGNAL(valueChanged(QString)));
}

TagValue::~TagValue()
{
}

Nepomuk::Query::LiteralTerm TagValue::value() const
{
    return Nepomuk::Query::LiteralTerm(m_tags->currentText());
}

void TagValue::showEvent(QShowEvent* event)
{
    if (!event->spontaneous() && (m_tags->count() == 0)) {
        const QList<Nepomuk::Tag> tags = Nepomuk::Tag::allTags();
        foreach (const Nepomuk::Tag& tag, tags) {
            m_tags->addItem(tag.label());
        }

        if (tags.count() == 0) {
            m_tags->addItem(i18nc("@label", "No Tags Available"));
        }
    }
    SearchCriterionValue::showEvent(event);
}

// -------------------------------------------------------------------------

SizeValue::SizeValue(QWidget* parent) :
    SearchCriterionValue(parent),
    m_lineEdit(0),
    m_units(0)
{
    m_lineEdit = new KLineEdit(this);
    m_lineEdit->setClearButtonShown(true);
    m_lineEdit->setValidator(new QIntValidator(this));
    m_lineEdit->setAlignment(Qt::AlignRight);

    m_units = new QComboBox(this);
    // TODO: check the KByte vs. KiByte dilemma :-/
    m_units->addItem(i18nc("@label", "Byte"));
    m_units->addItem(i18nc("@label", "KByte"));
    m_units->addItem(i18nc("@label", "MByte"));
    m_units->addItem(i18nc("@label", "GByte"));

    // set 1 MByte as default
    m_lineEdit->setText("1");
    m_units->setCurrentIndex(2);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setMargin(0);
    layout->addWidget(m_lineEdit);
    layout->addWidget(m_units);
}

SizeValue::~SizeValue()
{
}

Nepomuk::Query::LiteralTerm SizeValue::value() const
{
    return Nepomuk::Query::LiteralTerm(); // TODO
}

// -------------------------------------------------------------------------

RatingValue::RatingValue(QWidget* parent) :
    SearchCriterionValue(parent),
    m_ratingWidget(0)
{
    m_ratingWidget = new KRatingWidget(this);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setMargin(0);
    layout->addWidget(m_ratingWidget);
}

RatingValue::~RatingValue()
{
}

Nepomuk::Query::LiteralTerm RatingValue::value() const
{
    return Nepomuk::Query::LiteralTerm(m_ratingWidget->rating());
}

#include "searchcriterionvalue.moc"