3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "refinesearchwidget.h"
22 #include "searchparams.h"
26 #include "iconutils.h"
27 #include "mainwindow.h"
29 RefineSearchWidget::RefineSearchWidget(QWidget *parent) : QWidget(parent) {
31 // Fixes background painting in fullscreen
32 setAutoFillBackground(true);
35 void RefineSearchWidget::setup() {
36 static bool isSetup = false;
40 const int spacing = 15;
42 QBoxLayout *layout = new QVBoxLayout(this);
43 layout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
44 layout->setMargin(spacing);
45 layout->setSpacing(spacing);
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");
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);
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");
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);
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");
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);
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");
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);
118 layout->addSpacing(spacing);
119 doneButton = new QPushButton(tr("Done"), this);
120 doneButton->setDefault(true);
121 doneButton->setAutoDefault(true);
122 doneButton->setFocusPolicy(Qt::StrongFocus);
124 doneButton->setProperty("custom", true);
125 doneButton->setProperty("important", true);
126 doneButton->setProperty("big", true);
128 connect(doneButton, SIGNAL(clicked()), SLOT(doneClicked()));
129 layout->addWidget(doneButton, 0, Qt::AlignLeft);
132 void RefineSearchWidget::setupLabel(const QString &text,
134 const QString ¶mName) {
135 QBoxLayout *hLayout = new QHBoxLayout();
136 hLayout->setSpacing(8);
137 hLayout->setMargin(0);
138 hLayout->setAlignment(Qt::AlignVCenter);
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());
148 QPixmap translucentPixmap(pixmap.size());
149 translucentPixmap.fill(Qt::transparent);
151 painter.begin(&translucentPixmap);
152 painter.setOpacity(0.5);
153 painter.drawPixmap(0, 0, pixmap);
156 icon->setPixmap(pixmap);
157 hLayout->addWidget(icon);
159 QLabel *label = new QLabel(text, this);
160 hLayout->addWidget(label);
162 icon->setMaximumHeight(label->height());
164 layout->addLayout(hLayout);
167 QToolBar *RefineSearchWidget::setupBar(const QString ¶mName) {
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);
178 void RefineSearchWidget::actionTriggered(QAction *action) {
179 QToolBar *bar = static_cast<QToolBar *>(sender());
181 qDebug() << __PRETTY_FUNCTION__ << "Cannot get sender";
185 QString paramName = bar->property("paramName").toString();
186 QVariant paramValue = action->property("paramValue");
188 // qDebug() << "param changed" << paramName << paramValue;
189 emit paramChanged(paramName, paramValue);
194 void RefineSearchWidget::setSearchParams(SearchParams *params) {
197 MainWindow::instance()->getAction("refineSearch")->setEnabled(params);
205 bar = bars.value("sortBy");
206 action = bar->actions().at(params->sortBy());
207 if (action) action->setChecked(true);
209 bar = bars.value("duration");
210 action = bar->actions().at(params->duration());
211 if (action) action->setChecked(true);
213 bar = bars.value("time");
214 action = bar->actions().at(params->time());
215 if (action) action->setChecked(true);
217 bar = bars.value("quality");
218 action = bar->actions().at(params->quality());
219 if (action) action->setChecked(true);
221 disconnect(SIGNAL(paramChanged(QString, QVariant)));
222 connect(this, SIGNAL(paramChanged(QString, QVariant)), params,
223 SLOT(setParam(QString, QVariant)), Qt::UniqueConnection);
227 doneButton->setFocus();
230 void RefineSearchWidget::doneClicked() {
231 if (dirty) emit searchRefined();