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"
29 QHash<QString, QAction*>* globalActions();
32 RefineSearchWidget::RefineSearchWidget(QWidget *parent) :
37 void RefineSearchWidget::setup() {
38 static bool isSetup = false;
42 const int spacing = 15;
44 QBoxLayout *layout = new QVBoxLayout(this);
45 layout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
46 layout->setMargin(spacing);
47 layout->setSpacing(spacing);
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()
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);
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()
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);
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()
98 QStringList tips = QStringList()
100 << tr("Less than 4 minutes")
101 << tr("Between 4 and 20 minutes")
102 << tr("Longer than 20 minutes");
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);
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()
121 << tr("High Definition");
124 << tr("720p or higher");
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);
136 layout->addSpacing(spacing);
137 doneButton = new QPushButton(tr("Done"), this);
138 doneButton->setDefault(true);
139 doneButton->setAutoDefault(true);
140 doneButton->setFocusPolicy(Qt::StrongFocus);
142 doneButton->setProperty("custom", true);
143 doneButton->setProperty("important", true);
144 doneButton->setProperty("big", true);
146 connect(doneButton, SIGNAL(clicked()), SLOT(doneClicked()));
147 layout->addWidget(doneButton, 0, Qt::AlignLeft);
150 void RefineSearchWidget::setupLabel(const QString &text, QBoxLayout *layout, const QString ¶mName) {
151 QBoxLayout* hLayout = new QHBoxLayout();
152 hLayout->setSpacing(8);
153 hLayout->setMargin(0);
154 hLayout->setAlignment(Qt::AlignVCenter);
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");
161 QPixmap translucentPixmap(pixmap.size());
162 translucentPixmap.fill(Qt::transparent);
164 painter.begin(&translucentPixmap);
165 painter.setOpacity(0.5);
166 painter.drawPixmap(0, 0, pixmap);
169 icon->setPixmap(pixmap);
170 hLayout->addWidget(icon);
172 QLabel *label = new QLabel(text, this);
173 hLayout->addWidget(label);
175 icon->setMaximumHeight(label->height());
177 layout->addLayout(hLayout);
180 QToolBar* RefineSearchWidget::setupBar(const QString ¶mName) {
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);
191 void RefineSearchWidget::actionTriggered(QAction *action) {
192 QToolBar *bar = static_cast<QToolBar *>(sender());
194 qDebug() << __PRETTY_FUNCTION__ << "Cannot get sender";
198 QString paramName = bar->property("paramName").toString();
199 QVariant paramValue = action->property("paramValue");
201 // qDebug() << "param changed" << paramName << paramValue;
202 emit paramChanged(paramName, paramValue);
207 void RefineSearchWidget::setSearchParams(SearchParams *params) {
210 The::globalActions()->value("refine-search")->setEnabled(params);
218 bar = bars.value("sortBy");
219 action = bar->actions().at(params->sortBy());
220 if (action) action->setChecked(true);
222 bar = bars.value("duration");
223 action = bar->actions().at(params->duration());
224 if (action) action->setChecked(true);
226 bar = bars.value("time");
227 action = bar->actions().at(params->time());
228 if (action) action->setChecked(true);
230 bar = bars.value("quality");
231 action = bar->actions().at(params->quality());
232 if (action) action->setChecked(true);
234 disconnect(SIGNAL(paramChanged(QString,QVariant)));
235 connect(this, SIGNAL(paramChanged(QString,QVariant)),
236 params, SLOT(setParam(QString,QVariant)),
237 Qt::UniqueConnection);
241 doneButton->setFocus();
244 void RefineSearchWidget::doneClicked() {
245 if (dirty) emit searchRefined();