]> git.sur5r.net Git - minitube/blob - src/refinesearchwidget.cpp
Imported Upstream version 2.1.3
[minitube] / src / refinesearchwidget.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "refinesearchwidget.h"
22 #include "fontutils.h"
23 #include "searchparams.h"
24 #ifdef APP_EXTRA
25 #include "extra.h"
26 #endif
27
28 namespace The {
29 QHash<QString, QAction*>* globalActions();
30 }
31
32 RefineSearchWidget::RefineSearchWidget(QWidget *parent) :
33     QWidget(parent) {
34     dirty = false;
35 }
36
37 void RefineSearchWidget::setup() {
38     static bool isSetup = false;
39     if (isSetup) return;
40     isSetup = true;
41
42     static const int spacing = 15;
43     setFont(FontUtils::medium());
44
45     QBoxLayout *layout = new QVBoxLayout(this);
46     layout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
47     layout->setMargin(spacing*2);
48     layout->setSpacing(spacing);
49
50     QString paramName = "sortBy";
51     setupLabel(tr("Sort by"), layout, paramName);
52     QToolBar *sortBar = setupBar(paramName);
53     QActionGroup* sortGroup = new QActionGroup(this);
54     QStringList sortOptions = QStringList()
55             << tr("Relevance")
56             << tr("Date")
57             << tr("View Count")
58             << tr("Rating");
59     int i = 0;
60     foreach (QString actionName, sortOptions) {
61         QAction *action = new QAction(actionName, sortBar);
62         action->setCheckable(true);
63         action->setFont(FontUtils::medium());
64         action->setProperty("paramValue", i);
65         sortGroup->addAction(action);
66         sortBar->addAction(action);
67         i++;
68     }
69
70     paramName = "time";
71     layout->addSpacing(spacing);
72     setupLabel(tr("Date"), layout, paramName);
73     QToolBar *timeBar = setupBar(paramName);
74     QActionGroup* timeGroup = new QActionGroup(this);
75     QStringList timeSpans = QStringList()
76             << tr("Anytime")
77             << tr("Today")
78             << tr("7 Days")
79             << tr("30 Days");
80     i = 0;
81     foreach (QString actionName, timeSpans) {
82         QAction *action = new QAction(actionName, timeBar);
83         action->setCheckable(true);
84         action->setFont(FontUtils::medium());
85         action->setProperty("paramValue", i);
86         timeGroup->addAction(action);
87         timeBar->addAction(action);
88         i++;
89     }
90
91     paramName = "duration";
92     layout->addSpacing(spacing);
93     setupLabel(tr("Duration"), layout, paramName);
94     QToolBar *lengthBar = setupBar(paramName);
95     QActionGroup* lengthGroup = new QActionGroup(this);
96     QStringList lengthOptions = QStringList()
97             << tr("All")
98             << tr("Short")
99             << tr("Medium")
100             << tr("Long");
101     QStringList tips = QStringList()
102             << ""
103             << tr("Less than 4 minutes")
104             << tr("Between 4 and 20 minutes")
105             << tr("Longer than 20 minutes");
106     i = 0;
107     foreach (QString actionName, lengthOptions) {
108         QAction *action = new QAction(actionName, timeBar);
109         action->setStatusTip(tips.at(i));
110         action->setCheckable(true);
111         action->setFont(FontUtils::medium());
112         action->setProperty("paramValue", i);
113         lengthGroup->addAction(action);
114         lengthBar->addAction(action);
115         i++;
116     }
117
118     paramName = "quality";
119     layout->addSpacing(spacing);
120     setupLabel(tr("Quality"), layout, paramName);
121     QToolBar *qualityBar = setupBar(paramName);
122     QActionGroup* qualityGroup = new QActionGroup(this);
123     QStringList qualityOptions = QStringList()
124             << tr("All")
125             << tr("High Definition");
126     tips = QStringList()
127             << ""
128             << tr("720p or higher");
129     i = 0;
130     foreach (QString actionName, qualityOptions) {
131         QAction *action = new QAction(actionName, timeBar);
132         action->setStatusTip(tips.at(i));
133         action->setCheckable(true);
134         action->setFont(FontUtils::medium());
135         action->setProperty("paramValue", i);
136         qualityGroup->addAction(action);
137         qualityBar->addAction(action);
138         i++;
139     }
140
141     layout->addSpacing(spacing);
142     doneButton = new QPushButton(tr("Done"), this);
143     doneButton->setDefault(true);
144     doneButton->setAutoDefault(true);
145     doneButton->setFocusPolicy(Qt::StrongFocus);
146     doneButton->setFont(FontUtils::medium());
147     doneButton->setProperty("custom", true);
148     doneButton->setProperty("important", true);
149     doneButton->setProperty("big", true);
150     connect(doneButton, SIGNAL(clicked()), SLOT(doneClicked()));
151     layout->addWidget(doneButton, 0, Qt::AlignLeft);
152 }
153
154 void RefineSearchWidget::setupLabel(QString text, QBoxLayout *layout, QString paramName) {
155     QBoxLayout* hLayout = new QHBoxLayout();
156     hLayout->setSpacing(8);
157     hLayout->setMargin(0);
158     hLayout->setAlignment(Qt::AlignVCenter);
159
160     QLabel *icon = new QLabel(this);
161     icon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
162     QString resource = paramName;
163 #ifdef APP_EXTRA
164     resource = Extra::resourceName(resource);
165 #endif
166     QPixmap pixmap = QPixmap(":/images/search-" + resource + ".png");
167     QPixmap translucentPixmap(pixmap.size());
168     translucentPixmap.fill(Qt::transparent);
169     QPainter painter;
170     painter.begin(&translucentPixmap);
171     painter.setOpacity(0.5);
172     painter.drawPixmap(0, 0, pixmap);
173     painter.end();
174     icon->setPixmap(translucentPixmap);
175     hLayout->addWidget(icon);
176
177     QLabel *label = new QLabel(text.toUpper(), this);
178     label->setFont(FontUtils::mediumBold());
179     label->setStyleSheet("color: rgba(0, 0, 0, 128);");
180     hLayout->addWidget(label);
181
182     icon->setMaximumHeight(label->height());
183
184     layout->addLayout(hLayout);
185 }
186
187 QToolBar* RefineSearchWidget::setupBar(QString paramName) {
188     QToolBar* bar = new QToolBar(this);
189     bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
190     // bar->setProperty("segmented", true);
191     bar->setFont(FontUtils::medium());
192     bar->setProperty("paramName", paramName);
193     connect(bar, SIGNAL(actionTriggered(QAction*)),
194             SLOT(actionTriggered(QAction*)));
195     bars.insert(paramName, bar);
196     layout()->addWidget(bar);
197     return bar;
198 }
199
200 void RefineSearchWidget::paintEvent(QPaintEvent * /*event*/) {
201 #if defined(APP_MAC) | defined(APP_WIN)
202     QBrush brush;
203     if (window()->isActiveWindow()) {
204         brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
205     } else {
206         brush = palette().window();
207     }
208     QPainter painter(this);
209     painter.fillRect(0, 0, width(), height(), brush);
210 #endif
211 }
212
213 void RefineSearchWidget::actionTriggered(QAction *action) {
214     QToolBar *bar = static_cast<QToolBar *>(sender());
215     if (!bar) {
216         qDebug() << __PRETTY_FUNCTION__ << "Cannot get sender";
217         return;
218     }
219
220     QString paramName = bar->property("paramName").toString();
221     QVariant paramValue = action->property("paramValue");
222
223     // qDebug() << "param changed" << paramName << paramValue;
224     emit paramChanged(paramName, paramValue);
225
226     dirty = true;
227 }
228
229 void RefineSearchWidget::setSearchParams(SearchParams *params) {
230     setup();
231
232     The::globalActions()->value("refine-search")->setEnabled(params);
233     setEnabled(params);
234
235     if (!params) return;
236
237     QToolBar* bar;
238     QAction* action;
239
240     bar = bars.value("sortBy");
241     action = bar->actions().at(params->sortBy());
242     if (action) action->setChecked(true);
243
244     bar = bars.value("duration");
245     action = bar->actions().at(params->duration());
246     if (action) action->setChecked(true);
247
248     bar = bars.value("time");
249     action = bar->actions().at(params->time());
250     if (action) action->setChecked(true);
251
252     bar = bars.value("quality");
253     action = bar->actions().at(params->quality());
254     if (action) action->setChecked(true);
255
256     disconnect(SIGNAL(paramChanged(QString,QVariant)));
257     connect(this, SIGNAL(paramChanged(QString,QVariant)),
258             params, SLOT(setParam(QString,QVariant)),
259             Qt::UniqueConnection);
260
261     dirty = false;
262
263     doneButton->setFocus();
264 }
265
266 void RefineSearchWidget::doneClicked() {
267     if (dirty) emit searchRefined();
268     emit done();
269 }