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 "fontutils.h"
23 #include "searchparams.h"
29 QHash<QString, QAction*>* globalActions();
32 RefineSearchWidget::RefineSearchWidget(QWidget *parent) :
37 void RefineSearchWidget::setup() {
38 static bool isSetup = false;
42 static const int spacing = 15;
43 setFont(FontUtils::medium());
45 QBoxLayout *layout = new QVBoxLayout(this);
46 layout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
47 layout->setMargin(spacing*2);
48 layout->setSpacing(spacing);
50 QString paramName = "sortBy";
51 setupLabel(tr("Sort by"), layout, paramName);
52 QToolBar *sortBar = setupBar(paramName);
53 QActionGroup* sortGroup = new QActionGroup(this);
54 QStringList sortOptions = QStringList()
60 foreach (const QString &actionName, sortOptions) {
61 QAction *action = new QAction(actionName, sortBar);
62 action->setCheckable(true);
63 action->setFont(FontUtils::medium());
64 action->setProperty("paramValue", i);
65 sortGroup->addAction(action);
66 sortBar->addAction(action);
71 layout->addSpacing(spacing);
72 setupLabel(tr("Date"), layout, paramName);
73 QToolBar *timeBar = setupBar(paramName);
74 QActionGroup* timeGroup = new QActionGroup(this);
75 QStringList timeSpans = QStringList()
81 foreach (const QString &actionName, timeSpans) {
82 QAction *action = new QAction(actionName, timeBar);
83 action->setCheckable(true);
84 action->setFont(FontUtils::medium());
85 action->setProperty("paramValue", i);
86 timeGroup->addAction(action);
87 timeBar->addAction(action);
91 paramName = "duration";
92 layout->addSpacing(spacing);
93 setupLabel(tr("Duration"), layout, paramName);
94 QToolBar *lengthBar = setupBar(paramName);
95 QActionGroup* lengthGroup = new QActionGroup(this);
96 QStringList lengthOptions = QStringList()
101 QStringList tips = QStringList()
103 << tr("Less than 4 minutes")
104 << tr("Between 4 and 20 minutes")
105 << tr("Longer than 20 minutes");
107 foreach (const QString &actionName, lengthOptions) {
108 QAction *action = new QAction(actionName, timeBar);
109 action->setStatusTip(tips.at(i));
110 action->setCheckable(true);
111 action->setFont(FontUtils::medium());
112 action->setProperty("paramValue", i);
113 lengthGroup->addAction(action);
114 lengthBar->addAction(action);
118 paramName = "quality";
119 layout->addSpacing(spacing);
120 setupLabel(tr("Quality"), layout, paramName);
121 QToolBar *qualityBar = setupBar(paramName);
122 QActionGroup* qualityGroup = new QActionGroup(this);
123 QStringList qualityOptions = QStringList()
125 << tr("High Definition");
128 << tr("720p or higher");
130 foreach (const QString &actionName, qualityOptions) {
131 QAction *action = new QAction(actionName, timeBar);
132 action->setStatusTip(tips.at(i));
133 action->setCheckable(true);
134 action->setFont(FontUtils::medium());
135 action->setProperty("paramValue", i);
136 qualityGroup->addAction(action);
137 qualityBar->addAction(action);
141 layout->addSpacing(spacing);
142 doneButton = new QPushButton(tr("Done"), this);
143 doneButton->setDefault(true);
144 doneButton->setAutoDefault(true);
145 doneButton->setFocusPolicy(Qt::StrongFocus);
146 doneButton->setFont(FontUtils::medium());
147 doneButton->setProperty("custom", true);
148 doneButton->setProperty("important", true);
149 doneButton->setProperty("big", true);
150 connect(doneButton, SIGNAL(clicked()), SLOT(doneClicked()));
151 layout->addWidget(doneButton, 0, Qt::AlignLeft);
154 void RefineSearchWidget::setupLabel(const QString &text, QBoxLayout *layout, const QString ¶mName) {
155 QBoxLayout* hLayout = new QHBoxLayout();
156 hLayout->setSpacing(8);
157 hLayout->setMargin(0);
158 hLayout->setAlignment(Qt::AlignVCenter);
160 QLabel *icon = new QLabel(this);
161 icon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
162 QString resource = paramName;
163 QPixmap pixmap = QPixmap(":/images/search-" + resource + ".png");
164 QPixmap translucentPixmap(pixmap.size());
165 translucentPixmap.fill(Qt::transparent);
167 painter.begin(&translucentPixmap);
168 painter.setOpacity(0.5);
169 painter.drawPixmap(0, 0, pixmap);
171 icon->setPixmap(translucentPixmap);
172 hLayout->addWidget(icon);
174 QLabel *label = new QLabel(text.toUpper(), this);
175 label->setFont(FontUtils::mediumBold());
176 label->setStyleSheet("color: rgba(0, 0, 0, 128);");
177 hLayout->addWidget(label);
179 icon->setMaximumHeight(label->height());
181 layout->addLayout(hLayout);
184 QToolBar* RefineSearchWidget::setupBar(const QString ¶mName) {
185 QToolBar* bar = new QToolBar(this);
186 bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
187 // bar->setProperty("segmented", true);
188 bar->setFont(FontUtils::medium());
189 bar->setProperty("paramName", paramName);
190 connect(bar, SIGNAL(actionTriggered(QAction*)),
191 SLOT(actionTriggered(QAction*)));
192 bars.insert(paramName, bar);
193 layout()->addWidget(bar);
197 void RefineSearchWidget::paintEvent(QPaintEvent * /*event*/) {
198 #if defined(APP_MAC) | defined(APP_WIN)
200 if (window()->isActiveWindow()) {
201 brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
203 brush = palette().window();
205 QPainter painter(this);
206 painter.fillRect(0, 0, width(), height(), brush);
210 void RefineSearchWidget::actionTriggered(QAction *action) {
211 QToolBar *bar = static_cast<QToolBar *>(sender());
213 qDebug() << __PRETTY_FUNCTION__ << "Cannot get sender";
217 QString paramName = bar->property("paramName").toString();
218 QVariant paramValue = action->property("paramValue");
220 // qDebug() << "param changed" << paramName << paramValue;
221 emit paramChanged(paramName, paramValue);
226 void RefineSearchWidget::setSearchParams(SearchParams *params) {
229 The::globalActions()->value("refine-search")->setEnabled(params);
237 bar = bars.value("sortBy");
238 action = bar->actions().at(params->sortBy());
239 if (action) action->setChecked(true);
241 bar = bars.value("duration");
242 action = bar->actions().at(params->duration());
243 if (action) action->setChecked(true);
245 bar = bars.value("time");
246 action = bar->actions().at(params->time());
247 if (action) action->setChecked(true);
249 bar = bars.value("quality");
250 action = bar->actions().at(params->quality());
251 if (action) action->setChecked(true);
253 disconnect(SIGNAL(paramChanged(QString,QVariant)));
254 connect(this, SIGNAL(paramChanged(QString,QVariant)),
255 params, SLOT(setParam(QString,QVariant)),
256 Qt::UniqueConnection);
260 doneButton->setFocus();
263 void RefineSearchWidget::doneClicked() {
264 if (dirty) emit searchRefined();