]> git.sur5r.net Git - minitube/blob - refinesearchwidget.cpp
3518d7127f7dbd82d256c39987e9841578fc5e0b
[minitube] / 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 (const 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 (const 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 (const 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 (const 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(const QString &text, QBoxLayout *layout, const 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     QPixmap pixmap = QPixmap(":/images/search-" + resource + ".png");
164     QPixmap translucentPixmap(pixmap.size());
165     translucentPixmap.fill(Qt::transparent);
166     QPainter painter;
167     painter.begin(&translucentPixmap);
168     painter.setOpacity(0.5);
169     painter.drawPixmap(0, 0, pixmap);
170     painter.end();
171     icon->setPixmap(translucentPixmap);
172     hLayout->addWidget(icon);
173
174     QLabel *label = new QLabel(text.toUpper(), this);
175     label->setFont(FontUtils::mediumBold());
176     label->setStyleSheet("color: rgba(0, 0, 0, 128);");
177     hLayout->addWidget(label);
178
179     icon->setMaximumHeight(label->height());
180
181     layout->addLayout(hLayout);
182 }
183
184 QToolBar* RefineSearchWidget::setupBar(const QString &paramName) {
185     QToolBar* bar = new QToolBar(this);
186     bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
187     // bar->setProperty("segmented", true);
188     bar->setFont(FontUtils::medium());
189     bar->setProperty("paramName", paramName);
190     connect(bar, SIGNAL(actionTriggered(QAction*)),
191             SLOT(actionTriggered(QAction*)));
192     bars.insert(paramName, bar);
193     layout()->addWidget(bar);
194     return bar;
195 }
196
197 void RefineSearchWidget::paintEvent(QPaintEvent * /*event*/) {
198 #if defined(APP_MAC) | defined(APP_WIN)
199     QBrush brush;
200     if (window()->isActiveWindow()) {
201         brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
202     } else {
203         brush = palette().window();
204     }
205     QPainter painter(this);
206     painter.fillRect(0, 0, width(), height(), brush);
207 #endif
208 }
209
210 void RefineSearchWidget::actionTriggered(QAction *action) {
211     QToolBar *bar = static_cast<QToolBar *>(sender());
212     if (!bar) {
213         qDebug() << __PRETTY_FUNCTION__ << "Cannot get sender";
214         return;
215     }
216
217     QString paramName = bar->property("paramName").toString();
218     QVariant paramValue = action->property("paramValue");
219
220     // qDebug() << "param changed" << paramName << paramValue;
221     emit paramChanged(paramName, paramValue);
222
223     dirty = true;
224 }
225
226 void RefineSearchWidget::setSearchParams(SearchParams *params) {
227     setup();
228
229     The::globalActions()->value("refine-search")->setEnabled(params);
230     setEnabled(params);
231
232     if (!params) return;
233
234     QToolBar* bar;
235     QAction* action;
236
237     bar = bars.value("sortBy");
238     action = bar->actions().at(params->sortBy());
239     if (action) action->setChecked(true);
240
241     bar = bars.value("duration");
242     action = bar->actions().at(params->duration());
243     if (action) action->setChecked(true);
244
245     bar = bars.value("time");
246     action = bar->actions().at(params->time());
247     if (action) action->setChecked(true);
248
249     bar = bars.value("quality");
250     action = bar->actions().at(params->quality());
251     if (action) action->setChecked(true);
252
253     disconnect(SIGNAL(paramChanged(QString,QVariant)));
254     connect(this, SIGNAL(paramChanged(QString,QVariant)),
255             params, SLOT(setParam(QString,QVariant)),
256             Qt::UniqueConnection);
257
258     dirty = false;
259
260     doneButton->setFocus();
261 }
262
263 void RefineSearchWidget::doneClicked() {
264     if (dirty) emit searchRefined();
265     emit done();
266 }