]> git.sur5r.net Git - minitube/blob - src/refinesearchwidget.cpp
Upload 3.9.3-2 to unstable
[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() << tr("Anytime") << tr("Today") << tr("7 Days")
69                                                 << tr("30 Days") << tr("This year");
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()
107                                        << tr("All") << tr("HD") << tr("4K") << tr("HDR");
108     i = 0;
109     for (const QString &actionName : qualityOptions) {
110         QAction *action = new QAction(actionName, timeBar);
111         action->setCheckable(true);
112         action->setProperty("paramValue", i);
113         qualityGroup->addAction(action);
114         qualityBar->addAction(action);
115         i++;
116     }
117
118     layout->addSpacing(spacing);
119     doneButton = new QPushButton(tr("Done"), this);
120     doneButton->setDefault(true);
121     doneButton->setAutoDefault(true);
122     doneButton->setFocusPolicy(Qt::StrongFocus);
123 #ifndef APP_MAC
124     doneButton->setProperty("custom", true);
125     doneButton->setProperty("important", true);
126     doneButton->setProperty("big", true);
127 #endif
128     connect(doneButton, SIGNAL(clicked()), SLOT(doneClicked()));
129     layout->addWidget(doneButton, 0, Qt::AlignLeft);
130 }
131
132 void RefineSearchWidget::setupLabel(const QString &text,
133                                     QBoxLayout *layout,
134                                     const QString &paramName) {
135     QBoxLayout *hLayout = new QHBoxLayout();
136     hLayout->setSpacing(8);
137     hLayout->setMargin(0);
138     hLayout->setAlignment(Qt::AlignVCenter);
139
140     QLabel *icon = new QLabel(this);
141     icon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
142     QString resource = paramName;
143     QByteArray iconName = QByteArrayLiteral("search-") + resource.toLatin1();
144     QPixmap pixmap = IconUtils::iconPixmap(iconName.constData(), 16, palette().window().color(),
145                                            devicePixelRatioF());
146
147     /*
148     QPixmap translucentPixmap(pixmap.size());
149     translucentPixmap.fill(Qt::transparent);
150     QPainter painter;
151     painter.begin(&translucentPixmap);
152     painter.setOpacity(0.5);
153     painter.drawPixmap(0, 0, pixmap);
154     painter.end();
155     */
156     icon->setPixmap(pixmap);
157     hLayout->addWidget(icon);
158
159     QLabel *label = new QLabel(text, this);
160     hLayout->addWidget(label);
161
162     icon->setMaximumHeight(label->height());
163
164     layout->addLayout(hLayout);
165 }
166
167 QToolBar *RefineSearchWidget::setupBar(const QString &paramName) {
168     QToolBar *bar = new QToolBar(this);
169     bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
170     // bar->setProperty("segmented", true);
171     bar->setProperty("paramName", paramName);
172     connect(bar, SIGNAL(actionTriggered(QAction *)), SLOT(actionTriggered(QAction *)));
173     bars.insert(paramName, bar);
174     layout()->addWidget(bar);
175     return bar;
176 }
177
178 void RefineSearchWidget::actionTriggered(QAction *action) {
179     QToolBar *bar = static_cast<QToolBar *>(sender());
180     if (!bar) {
181         qDebug() << __PRETTY_FUNCTION__ << "Cannot get sender";
182         return;
183     }
184
185     QString paramName = bar->property("paramName").toString();
186     QVariant paramValue = action->property("paramValue");
187
188     // qDebug() << "param changed" << paramName << paramValue;
189     emit paramChanged(paramName, paramValue);
190
191     dirty = true;
192 }
193
194 void RefineSearchWidget::setSearchParams(SearchParams *params) {
195     setup();
196
197     MainWindow::instance()->getAction("refineSearch")->setEnabled(params);
198     setEnabled(params);
199
200     if (!params) return;
201
202     QToolBar *bar;
203     QAction *action;
204
205     bar = bars.value("sortBy");
206     action = bar->actions().at(params->sortBy());
207     if (action) action->setChecked(true);
208
209     bar = bars.value("duration");
210     action = bar->actions().at(params->duration());
211     if (action) action->setChecked(true);
212
213     bar = bars.value("time");
214     action = bar->actions().at(params->time());
215     if (action) action->setChecked(true);
216
217     bar = bars.value("quality");
218     action = bar->actions().at(params->quality());
219     if (action) action->setChecked(true);
220
221     disconnect(SIGNAL(paramChanged(QString, QVariant)));
222     connect(this, SIGNAL(paramChanged(QString, QVariant)), params,
223             SLOT(setParam(QString, QVariant)), Qt::UniqueConnection);
224
225     dirty = false;
226
227     doneButton->setFocus();
228 }
229
230 void RefineSearchWidget::doneClicked() {
231     if (dirty) emit searchRefined();
232     emit done();
233 }