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()
69 << tr("Anytime") << tr("Today") << tr("7 Days") << tr("30 Days");
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() << tr("All") << tr("High Definition");
107 tips = QStringList() << "" << tr("720p or higher");
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);
119 layout->addSpacing(spacing);
120 doneButton = new QPushButton(tr("Done"), this);
121 doneButton->setDefault(true);
122 doneButton->setAutoDefault(true);
123 doneButton->setFocusPolicy(Qt::StrongFocus);
125 doneButton->setProperty("custom", true);
126 doneButton->setProperty("important", true);
127 doneButton->setProperty("big", true);
129 connect(doneButton, SIGNAL(clicked()), SLOT(doneClicked()));
130 layout->addWidget(doneButton, 0, Qt::AlignLeft);
133 void RefineSearchWidget::setupLabel(const QString &text,
135 const QString ¶mName) {
136 QBoxLayout *hLayout = new QHBoxLayout();
137 hLayout->setSpacing(8);
138 hLayout->setMargin(0);
139 hLayout->setAlignment(Qt::AlignVCenter);
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());
149 QPixmap translucentPixmap(pixmap.size());
150 translucentPixmap.fill(Qt::transparent);
152 painter.begin(&translucentPixmap);
153 painter.setOpacity(0.5);
154 painter.drawPixmap(0, 0, pixmap);
157 icon->setPixmap(pixmap);
158 hLayout->addWidget(icon);
160 QLabel *label = new QLabel(text, this);
161 hLayout->addWidget(label);
163 icon->setMaximumHeight(label->height());
165 layout->addLayout(hLayout);
168 QToolBar *RefineSearchWidget::setupBar(const QString ¶mName) {
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);
179 void RefineSearchWidget::actionTriggered(QAction *action) {
180 QToolBar *bar = static_cast<QToolBar *>(sender());
182 qDebug() << __PRETTY_FUNCTION__ << "Cannot get sender";
186 QString paramName = bar->property("paramName").toString();
187 QVariant paramValue = action->property("paramValue");
189 // qDebug() << "param changed" << paramName << paramValue;
190 emit paramChanged(paramName, paramValue);
195 void RefineSearchWidget::setSearchParams(SearchParams *params) {
198 MainWindow::instance()->getAction("refineSearch")->setEnabled(params);
206 bar = bars.value("sortBy");
207 action = bar->actions().at(params->sortBy());
208 if (action) action->setChecked(true);
210 bar = bars.value("duration");
211 action = bar->actions().at(params->duration());
212 if (action) action->setChecked(true);
214 bar = bars.value("time");
215 action = bar->actions().at(params->time());
216 if (action) action->setChecked(true);
218 bar = bars.value("quality");
219 action = bar->actions().at(params->quality());
220 if (action) action->setChecked(true);
222 disconnect(SIGNAL(paramChanged(QString, QVariant)));
223 connect(this, SIGNAL(paramChanged(QString, QVariant)), params,
224 SLOT(setParam(QString, QVariant)), Qt::UniqueConnection);
228 doneButton->setFocus();
231 void RefineSearchWidget::doneClicked() {
232 if (dirty) emit searchRefined();