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