]> git.sur5r.net Git - minitube/blob - src/refinesearchwidget.cpp
SearchLineEdit refactoring
[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
27 namespace The {
28 QHash<QString, QAction*>* globalActions();
29 }
30
31 RefineSearchWidget::RefineSearchWidget(QWidget *parent) :
32     QWidget(parent) {
33     dirty = false;
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*2);
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     QStringList sortOptions = QStringList()
53             << tr("Relevance")
54             << tr("Date")
55             << tr("View Count")
56             << tr("Rating");
57     int i = 0;
58     foreach (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     QStringList timeSpans = QStringList()
73             << tr("Anytime")
74             << tr("Today")
75             << tr("7 Days")
76             << tr("30 Days");
77     i = 0;
78     foreach (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     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     foreach (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     QStringList qualityOptions = QStringList()
119             << tr("All")
120             << tr("High Definition");
121     tips = QStringList()
122             << ""
123             << tr("720p or higher");
124     i = 0;
125     foreach (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 = QPixmap(":/images/search-" + resource + ".png");
159     QPixmap translucentPixmap(pixmap.size());
160     translucentPixmap.fill(Qt::transparent);
161     QPainter painter;
162     painter.begin(&translucentPixmap);
163     painter.setOpacity(0.5);
164     painter.drawPixmap(0, 0, pixmap);
165     painter.end();
166     icon->setPixmap(translucentPixmap);
167     hLayout->addWidget(icon);
168
169     QLabel *label = new QLabel(text, this);
170     label->setStyleSheet("color: rgba(0, 0, 0, 128);");
171     hLayout->addWidget(label);
172
173     icon->setMaximumHeight(label->height());
174
175     layout->addLayout(hLayout);
176 }
177
178 QToolBar* RefineSearchWidget::setupBar(const QString &paramName) {
179     QToolBar* bar = new QToolBar(this);
180     bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
181     // bar->setProperty("segmented", true);
182     bar->setProperty("paramName", paramName);
183     connect(bar, SIGNAL(actionTriggered(QAction*)), SLOT(actionTriggered(QAction*)));
184     bars.insert(paramName, bar);
185     layout()->addWidget(bar);
186     return bar;
187 }
188
189 void RefineSearchWidget::actionTriggered(QAction *action) {
190     QToolBar *bar = static_cast<QToolBar *>(sender());
191     if (!bar) {
192         qDebug() << __PRETTY_FUNCTION__ << "Cannot get sender";
193         return;
194     }
195
196     QString paramName = bar->property("paramName").toString();
197     QVariant paramValue = action->property("paramValue");
198
199     // qDebug() << "param changed" << paramName << paramValue;
200     emit paramChanged(paramName, paramValue);
201
202     dirty = true;
203 }
204
205 void RefineSearchWidget::setSearchParams(SearchParams *params) {
206     setup();
207
208     The::globalActions()->value("refine-search")->setEnabled(params);
209     setEnabled(params);
210
211     if (!params) return;
212
213     QToolBar* bar;
214     QAction* action;
215
216     bar = bars.value("sortBy");
217     action = bar->actions().at(params->sortBy());
218     if (action) action->setChecked(true);
219
220     bar = bars.value("duration");
221     action = bar->actions().at(params->duration());
222     if (action) action->setChecked(true);
223
224     bar = bars.value("time");
225     action = bar->actions().at(params->time());
226     if (action) action->setChecked(true);
227
228     bar = bars.value("quality");
229     action = bar->actions().at(params->quality());
230     if (action) action->setChecked(true);
231
232     disconnect(SIGNAL(paramChanged(QString,QVariant)));
233     connect(this, SIGNAL(paramChanged(QString,QVariant)),
234             params, SLOT(setParam(QString,QVariant)),
235             Qt::UniqueConnection);
236
237     dirty = false;
238
239     doneButton->setFocus();
240 }
241
242 void RefineSearchWidget::doneClicked() {
243     if (dirty) emit searchRefined();
244     emit done();
245 }