]> git.sur5r.net Git - minitube/blob - src/refinesearchwidget.cpp
6723b5a282bb5457e2670d6b3f889b1fe0d92e82
[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 "searchparams.h"
23 #ifdef APP_EXTRA
24 #include "extra.h"
25 #endif
26 #include "iconutils.h"
27 #include "mainwindow.h"
28
29 RefineSearchWidget::RefineSearchWidget(QWidget *parent) :
30     QWidget(parent) {
31     dirty = false;
32     // Fixes background painting in fullscreen
33     setAutoFillBackground(true);
34 }
35
36 void RefineSearchWidget::setup() {
37     static bool isSetup = false;
38     if (isSetup) return;
39     isSetup = true;
40
41     const int spacing = 15;
42
43     QBoxLayout *layout = new QVBoxLayout(this);
44     layout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
45     layout->setMargin(spacing);
46     layout->setSpacing(spacing);
47
48     QString paramName = "sortBy";
49     setupLabel(tr("Sort by"), layout, paramName);
50     QToolBar *sortBar = setupBar(paramName);
51     QActionGroup* sortGroup = new QActionGroup(this);
52     const QStringList sortOptions = QStringList()
53             << tr("Relevance")
54             << tr("Date")
55             << tr("View Count")
56             << tr("Rating");
57     int i = 0;
58     for (const QString &actionName : sortOptions) {
59         QAction *action = new QAction(actionName, sortBar);
60         action->setCheckable(true);
61         action->setProperty("paramValue", i);
62         sortGroup->addAction(action);
63         sortBar->addAction(action);
64         i++;
65     }
66
67     paramName = "time";
68     layout->addSpacing(spacing);
69     setupLabel(tr("Date"), layout, paramName);
70     QToolBar *timeBar = setupBar(paramName);
71     QActionGroup* timeGroup = new QActionGroup(this);
72     const QStringList timeSpans = QStringList()
73             << tr("Anytime")
74             << tr("Today")
75             << tr("7 Days")
76             << tr("30 Days");
77     i = 0;
78     for (const QString &actionName : timeSpans) {
79         QAction *action = new QAction(actionName, timeBar);
80         action->setCheckable(true);
81         action->setProperty("paramValue", i);
82         timeGroup->addAction(action);
83         timeBar->addAction(action);
84         i++;
85     }
86
87     paramName = "duration";
88     layout->addSpacing(spacing);
89     setupLabel(tr("Duration"), layout, paramName);
90     QToolBar *lengthBar = setupBar(paramName);
91     QActionGroup* lengthGroup = new QActionGroup(this);
92     const QStringList lengthOptions = QStringList()
93             << tr("All")
94             << tr("Short")
95             << tr("Medium")
96             << tr("Long");
97     QStringList tips = QStringList()
98             << ""
99             << tr("Less than 4 minutes")
100             << tr("Between 4 and 20 minutes")
101             << tr("Longer than 20 minutes");
102     i = 0;
103     for (const QString &actionName : lengthOptions) {
104         QAction *action = new QAction(actionName, timeBar);
105         action->setStatusTip(tips.at(i));
106         action->setCheckable(true);
107         action->setProperty("paramValue", i);
108         lengthGroup->addAction(action);
109         lengthBar->addAction(action);
110         i++;
111     }
112
113     paramName = "quality";
114     layout->addSpacing(spacing);
115     setupLabel(tr("Quality"), layout, paramName);
116     QToolBar *qualityBar = setupBar(paramName);
117     QActionGroup* qualityGroup = new QActionGroup(this);
118     const QStringList qualityOptions = QStringList()
119             << tr("All")
120             << tr("High Definition");
121     tips = QStringList()
122             << ""
123             << tr("720p or higher");
124     i = 0;
125     for (const QString &actionName : qualityOptions) {
126         QAction *action = new QAction(actionName, timeBar);
127         action->setStatusTip(tips.at(i));
128         action->setCheckable(true);
129         action->setProperty("paramValue", i);
130         qualityGroup->addAction(action);
131         qualityBar->addAction(action);
132         i++;
133     }
134
135     layout->addSpacing(spacing);
136     doneButton = new QPushButton(tr("Done"), this);
137     doneButton->setDefault(true);
138     doneButton->setAutoDefault(true);
139     doneButton->setFocusPolicy(Qt::StrongFocus);
140 #ifndef APP_MAC
141     doneButton->setProperty("custom", true);
142     doneButton->setProperty("important", true);
143     doneButton->setProperty("big", true);
144 #endif
145     connect(doneButton, SIGNAL(clicked()), SLOT(doneClicked()));
146     layout->addWidget(doneButton, 0, Qt::AlignLeft);
147 }
148
149 void RefineSearchWidget::setupLabel(const QString &text, QBoxLayout *layout, const QString &paramName) {
150     QBoxLayout* hLayout = new QHBoxLayout();
151     hLayout->setSpacing(8);
152     hLayout->setMargin(0);
153     hLayout->setAlignment(Qt::AlignVCenter);
154
155     QLabel *icon = new QLabel(this);
156     icon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
157     QString resource = paramName;
158     QPixmap pixmap = IconUtils::pixmap(":/images/search-" + resource + ".png");
159     /*
160     QPixmap translucentPixmap(pixmap.size());
161     translucentPixmap.fill(Qt::transparent);
162     QPainter painter;
163     painter.begin(&translucentPixmap);
164     painter.setOpacity(0.5);
165     painter.drawPixmap(0, 0, pixmap);
166     painter.end();
167     */
168     icon->setPixmap(pixmap);
169     hLayout->addWidget(icon);
170
171     QLabel *label = new QLabel(text, this);
172     hLayout->addWidget(label);
173
174     icon->setMaximumHeight(label->height());
175
176     layout->addLayout(hLayout);
177 }
178
179 QToolBar* RefineSearchWidget::setupBar(const QString &paramName) {
180     QToolBar* bar = new QToolBar(this);
181     bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
182     // bar->setProperty("segmented", true);
183     bar->setProperty("paramName", paramName);
184     connect(bar, SIGNAL(actionTriggered(QAction*)), SLOT(actionTriggered(QAction*)));
185     bars.insert(paramName, bar);
186     layout()->addWidget(bar);
187     return bar;
188 }
189
190 void RefineSearchWidget::actionTriggered(QAction *action) {
191     QToolBar *bar = static_cast<QToolBar *>(sender());
192     if (!bar) {
193         qDebug() << __PRETTY_FUNCTION__ << "Cannot get sender";
194         return;
195     }
196
197     QString paramName = bar->property("paramName").toString();
198     QVariant paramValue = action->property("paramValue");
199
200     // qDebug() << "param changed" << paramName << paramValue;
201     emit paramChanged(paramName, paramValue);
202
203     dirty = true;
204 }
205
206 void RefineSearchWidget::setSearchParams(SearchParams *params) {
207     setup();
208
209     MainWindow::instance()->getAction("refineSearch")->setEnabled(params);
210     setEnabled(params);
211
212     if (!params) return;
213
214     QToolBar* bar;
215     QAction* action;
216
217     bar = bars.value("sortBy");
218     action = bar->actions().at(params->sortBy());
219     if (action) action->setChecked(true);
220
221     bar = bars.value("duration");
222     action = bar->actions().at(params->duration());
223     if (action) action->setChecked(true);
224
225     bar = bars.value("time");
226     action = bar->actions().at(params->time());
227     if (action) action->setChecked(true);
228
229     bar = bars.value("quality");
230     action = bar->actions().at(params->quality());
231     if (action) action->setChecked(true);
232
233     disconnect(SIGNAL(paramChanged(QString,QVariant)));
234     connect(this, SIGNAL(paramChanged(QString,QVariant)),
235             params, SLOT(setParam(QString,QVariant)),
236             Qt::UniqueConnection);
237
238     dirty = false;
239
240     doneButton->setFocus();
241 }
242
243 void RefineSearchWidget::doneClicked() {
244     if (dirty) emit searchRefined();
245     emit done();
246 }