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