]> git.sur5r.net Git - minitube/blob - src/refinesearchwidget.cpp
Imported Upstream version 2.0
[minitube] / src / refinesearchwidget.cpp
1 #include "refinesearchwidget.h"
2 #include "fontutils.h"
3 #include "searchparams.h"
4 #ifndef Q_WS_X11
5 #include "extra.h"
6 #endif
7
8 namespace The {
9 QHash<QString, QAction*>* globalActions();
10 }
11
12 RefineSearchWidget::RefineSearchWidget(QWidget *parent) :
13     QWidget(parent) {
14     dirty = false;
15 }
16
17 void RefineSearchWidget::setup() {
18     static bool isSetup = false;
19     if (isSetup) return;
20     isSetup = true;
21
22     static const int spacing = 15;
23     setFont(FontUtils::medium());
24
25     QBoxLayout *layout = new QVBoxLayout(this);
26     layout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
27     layout->setMargin(spacing*2);
28     layout->setSpacing(spacing);
29
30     QString paramName = "sortBy";
31     setupLabel(tr("Sort by"), layout, paramName);
32     QToolBar *sortBar = setupBar(paramName);
33     QActionGroup* sortGroup = new QActionGroup(this);
34     QStringList sortOptions = QStringList()
35             << tr("Relevance")
36             << tr("Date")
37             << tr("View Count")
38             << tr("Rating");
39     int i = 0;
40     foreach (QString actionName, sortOptions) {
41         QAction *action = new QAction(actionName, sortBar);
42         action->setCheckable(true);
43         action->setFont(FontUtils::medium());
44         action->setProperty("paramValue", i);
45         sortGroup->addAction(action);
46         sortBar->addAction(action);
47         i++;
48     }
49
50     paramName = "time";
51     layout->addSpacing(spacing);
52     setupLabel(tr("Date"), layout, paramName);
53     QToolBar *timeBar = setupBar(paramName);
54     QActionGroup* timeGroup = new QActionGroup(this);
55     QStringList timeSpans = QStringList()
56             << tr("Anytime")
57             << tr("Today")
58             << tr("7 Days")
59             << tr("30 Days");
60     i = 0;
61     foreach (QString actionName, timeSpans) {
62         QAction *action = new QAction(actionName, timeBar);
63         action->setCheckable(true);
64         action->setFont(FontUtils::medium());
65         action->setProperty("paramValue", i);
66         timeGroup->addAction(action);
67         timeBar->addAction(action);
68         i++;
69     }
70
71     paramName = "duration";
72     layout->addSpacing(spacing);
73     setupLabel(tr("Duration"), layout, paramName);
74     QToolBar *lengthBar = setupBar(paramName);
75     QActionGroup* lengthGroup = new QActionGroup(this);
76     QStringList lengthOptions = QStringList()
77             << tr("All")
78             << tr("Short")
79             << tr("Medium")
80             << tr("Long");
81     QStringList tips = QStringList()
82             << ""
83             << tr("Less than 4 minutes")
84             << tr("Between 4 and 20 minutes")
85             << tr("Longer than 20 minutes");
86     i = 0;
87     foreach (QString actionName, lengthOptions) {
88         QAction *action = new QAction(actionName, timeBar);
89         action->setStatusTip(tips.at(i));
90         action->setCheckable(true);
91         action->setFont(FontUtils::medium());
92         action->setProperty("paramValue", i);
93         lengthGroup->addAction(action);
94         lengthBar->addAction(action);
95         i++;
96     }
97
98     paramName = "quality";
99     layout->addSpacing(spacing);
100     setupLabel(tr("Quality"), layout, paramName);
101     QToolBar *qualityBar = setupBar(paramName);
102     QActionGroup* qualityGroup = new QActionGroup(this);
103     QStringList qualityOptions = QStringList()
104             << tr("All")
105             << tr("High Definition");
106     tips = QStringList()
107             << ""
108             << tr("720p or higher");
109     i = 0;
110     foreach (QString actionName, qualityOptions) {
111         QAction *action = new QAction(actionName, timeBar);
112         action->setStatusTip(tips.at(i));
113         action->setCheckable(true);
114         action->setFont(FontUtils::medium());
115         action->setProperty("paramValue", i);
116         qualityGroup->addAction(action);
117         qualityBar->addAction(action);
118         i++;
119     }
120
121     layout->addSpacing(spacing);
122     QPushButton *doneButton = new QPushButton(tr("Done"), this);
123     doneButton->setDefault(true);
124     doneButton->setFont(FontUtils::medium());
125     doneButton->setProperty("custom", true);
126     doneButton->setProperty("important", true);
127     doneButton->setProperty("big", true);
128     connect(doneButton, SIGNAL(clicked()), SLOT(doneClicked()));
129     layout->addWidget(doneButton, 0, Qt::AlignLeft);
130 }
131
132 void RefineSearchWidget::setupLabel(QString text, QBoxLayout *layout, QString paramName) {
133     QBoxLayout* hLayout = new QHBoxLayout();
134     hLayout->setSpacing(8);
135     hLayout->setMargin(0);
136     hLayout->setAlignment(Qt::AlignVCenter);
137
138     QLabel *icon = new QLabel(this);
139     icon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
140     QString resource = paramName;
141 #ifndef Q_WS_X11
142     resource = Extra::resourceName(resource);
143 #endif
144     QPixmap pixmap = QPixmap(":/images/search-" + resource + ".png");
145     QPixmap translucentPixmap(pixmap.size());
146     translucentPixmap.fill(Qt::transparent);
147     QPainter painter;
148     painter.begin(&translucentPixmap);
149     painter.setOpacity(0.5);
150     painter.drawPixmap(0, 0, pixmap);
151     painter.end();
152     icon->setPixmap(translucentPixmap);
153     hLayout->addWidget(icon);
154
155     QLabel *label = new QLabel(text.toUpper(), this);
156     label->setFont(FontUtils::mediumBold());
157     label->setStyleSheet("color: rgba(0, 0, 0, 128);");
158     hLayout->addWidget(label);
159
160     icon->setMaximumHeight(label->height());
161
162     layout->addLayout(hLayout);
163 }
164
165 QToolBar* RefineSearchWidget::setupBar(QString paramName) {
166     QToolBar* bar = new QToolBar(this);
167     bar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
168     // bar->setProperty("segmented", true);
169     bar->setFont(FontUtils::medium());
170     bar->setProperty("paramName", paramName);
171     connect(bar, SIGNAL(actionTriggered(QAction*)),
172             SLOT(actionTriggered(QAction*)));
173     bars.insert(paramName, bar);
174     layout()->addWidget(bar);
175     return bar;
176 }
177
178 void RefineSearchWidget::paintEvent(QPaintEvent * /*event*/) {
179 #if defined(APP_MAC) | defined(APP_WIN)
180     QBrush brush;
181     if (window()->isActiveWindow()) {
182         brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
183     } else {
184         brush = palette().window();
185     }
186     QPainter painter(this);
187     painter.fillRect(0, 0, width(), height(), brush);
188 #endif
189 }
190
191 void RefineSearchWidget::actionTriggered(QAction *action) {
192     QToolBar *bar = static_cast<QToolBar *>(sender());
193     if (!bar) {
194         qDebug() << __PRETTY_FUNCTION__ << "Cannot get sender";
195         return;
196     }
197
198     QString paramName = bar->property("paramName").toString();
199     QVariant paramValue = action->property("paramValue");
200
201     // qDebug() << "param changed" << paramName << paramValue;
202     emit paramChanged(paramName, paramValue);
203
204     dirty = true;
205 }
206
207 void RefineSearchWidget::setSearchParams(SearchParams *params) {
208     setup();
209
210     qDebug() << (params != 0);
211
212     The::globalActions()->value("refine-search")->setEnabled(params);
213     setEnabled(params);
214
215     if (!params) return;
216
217     QToolBar* bar;
218     QAction* action;
219
220     bar = bars.value("sortBy");
221     action = bar->actions().at(params->sortBy());
222     if (action) action->setChecked(true);
223
224     bar = bars.value("duration");
225     action = bar->actions().at(params->duration());
226     if (action) action->setChecked(true);
227
228     bar = bars.value("time");
229     action = bar->actions().at(params->time());
230     if (action) action->setChecked(true);
231
232     bar = bars.value("quality");
233     action = bar->actions().at(params->quality());
234     if (action) action->setChecked(true);
235
236     disconnect(SIGNAL(paramChanged(QString,QVariant)));
237     connect(this, SIGNAL(paramChanged(QString,QVariant)),
238             params, SLOT(setParam(QString,QVariant)),
239             Qt::UniqueConnection);
240
241     dirty = false;
242 }
243
244 void RefineSearchWidget::doneClicked() {
245     if (dirty) emit searchRefined();
246     emit done();
247 }