]> git.sur5r.net Git - minitube/blob - src/searchview.cpp
No alternating colors
[minitube] / src / searchview.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "searchview.h"
22 #include "constants.h"
23 #include "fontutils.h"
24 #include "searchparams.h"
25 #include "ytsuggester.h"
26 #include "channelsuggest.h"
27 #ifdef APP_MAC_SEARCHFIELD
28 #include "searchlineedit_mac.h"
29 #else
30 #include "searchlineedit.h"
31 #endif
32 #ifdef APP_EXTRA
33 #include "extra.h"
34 #endif
35 #ifdef APP_ACTIVATION
36 #include "activation.h"
37 #endif
38 #include "mainwindow.h"
39 #include "painterutils.h"
40
41 namespace The {
42 QHash<QString, QAction*>* globalActions();
43 }
44
45 static const QString recentKeywordsKey = "recentKeywords";
46 static const QString recentChannelsKey = "recentChannels";
47 static const int PADDING = 30;
48
49 SearchView::SearchView(QWidget *parent) : View(parent) {
50
51 #if defined(APP_MAC) | defined(APP_WIN)
52     // speedup painting since we'll paint the whole background
53     // by ourselves anyway in paintEvent()
54     setAttribute(Qt::WA_OpaquePaintEvent);
55 #endif
56
57     QBoxLayout *mainLayout = new QVBoxLayout(this);
58     mainLayout->setMargin(PADDING);
59     mainLayout->setSpacing(0);
60
61     // hidden message widget
62     message = new QLabel(this);
63     message->hide();
64     mainLayout->addWidget(message);
65
66     mainLayout->addStretch();
67
68     QBoxLayout *hLayout = new QHBoxLayout();
69     hLayout->setAlignment(Qt::AlignCenter);
70     mainLayout->addLayout(hLayout);
71
72     QLabel *logo = new QLabel(this);
73     logo->setPixmap(QPixmap(":/images/app.png"));
74     hLayout->addWidget(logo, 0, Qt::AlignTop);
75     hLayout->addSpacing(PADDING);
76
77     QVBoxLayout *layout = new QVBoxLayout();
78     layout->setAlignment(Qt::AlignCenter);
79     hLayout->addLayout(layout);
80
81     QLabel *welcomeLabel =
82             new QLabel("<h1 style='font-weight:100'>" +
83                        tr("Welcome to <a href='%1'>%2</a>,")
84                        // .replace("<a ", "<a style='color:palette(text)'")
85                        .replace("<a ", "<a style='text-decoration:none; color:palette(text);font-weight:normal' ")
86                        .arg(Constants::WEBSITE, Constants::NAME)
87                        + "</h1>", this);
88     welcomeLabel->setOpenExternalLinks(true);
89     welcomeLabel->setProperty("heading", true);
90 #ifdef APP_MAC
91     QFont f = welcomeLabel->font();
92     f.setFamily("Helvetica Neue");
93     f.setStyleName("Thin");
94     welcomeLabel->setFont(f);
95 #elif APP_WIN
96     QFont f = welcomeLabel->font();
97     f.setFamily("Segoe UI Light");
98     welcomeLabel->setFont(f);
99 #endif
100     layout->addWidget(welcomeLabel);
101
102     layout->addSpacing(PADDING / 2);
103
104     QBoxLayout *tipLayout = new QHBoxLayout();
105     tipLayout->setSpacing(10);
106
107 #ifndef APP_MAC
108     const QFont &biggerFont = FontUtils::big();
109 #endif
110
111     //: "Enter", as in "type". The whole phrase says: "Enter a keyword to start watching videos"
112     QLabel *tipLabel = new QLabel(tr("Enter"), this);
113 #ifndef APP_MAC
114     tipLabel->setFont(biggerFont);
115 #endif
116     tipLayout->addWidget(tipLabel);
117
118     typeCombo = new QComboBox(this);
119     typeCombo->addItem(tr("a keyword"));
120     typeCombo->addItem(tr("a channel"));
121 #ifndef APP_MAC
122     typeCombo->setFont(biggerFont);
123 #endif
124     connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
125     tipLayout->addWidget(typeCombo);
126
127     tipLabel = new QLabel(tr("to start watching videos."), this);
128 #ifndef APP_MAC
129     tipLabel->setFont(biggerFont);
130 #endif
131     tipLayout->addWidget(tipLabel);
132     layout->addLayout(tipLayout);
133
134     layout->addSpacing(PADDING / 2);
135
136     QHBoxLayout *searchLayout = new QHBoxLayout();
137     searchLayout->setAlignment(Qt::AlignVCenter);
138
139 #ifdef APP_MAC_SEARCHFIELD
140     queryEdit = new SearchLineEditMac(this);
141 #else
142     queryEdit = new SearchLineEdit(this);
143     queryEdit->toWidget()->setFont(biggerFont);
144 #endif
145
146     qDebug() << "queryEdit->toWidget()" << (queryEdit->toWidget() == 0) << queryEdit->toWidget();
147     connect(queryEdit->toWidget(), SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
148     connect(queryEdit->toWidget(), SIGNAL(textEdited(const QString &)), SLOT(textChanged(const QString &)));
149     connect(queryEdit->toWidget(), SIGNAL(suggestionAccepted(Suggestion*)), SLOT(suggestionAccepted(Suggestion*)));
150
151     youtubeSuggest = new YTSuggester(this);
152     channelSuggest = new ChannelSuggest(this);
153     searchTypeChanged(0);
154
155     searchLayout->addWidget(queryEdit->toWidget());
156     searchLayout->addSpacing(10);
157
158     watchButton = new QPushButton(tr("Watch"), this);
159     watchButton->setDefault(true);
160     watchButton->setEnabled(false);
161     watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
162     connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
163     searchLayout->addWidget(watchButton);
164
165     layout->addItem(searchLayout);
166
167     layout->addSpacing(PADDING / 2);
168
169     QHBoxLayout *otherLayout = new QHBoxLayout();
170     otherLayout->setMargin(0);
171     otherLayout->setSpacing(10);
172
173     recentKeywordsLayout = new QVBoxLayout();
174     recentKeywordsLayout->setSpacing(5);
175     recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
176     recentKeywordsLabel = new QLabel(tr("Recent keywords"), this);
177     recentKeywordsLabel->setProperty("recentHeader", true);
178     recentKeywordsLabel->setForegroundRole(QPalette::Dark);
179     recentKeywordsLabel->hide();
180     recentKeywordsLayout->addWidget(recentKeywordsLabel);
181
182     otherLayout->addLayout(recentKeywordsLayout);
183
184     // recent channels
185     recentChannelsLayout = new QVBoxLayout();
186     recentChannelsLayout->setSpacing(5);
187     recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
188     recentChannelsLabel = new QLabel(tr("Recent channels"), this);
189     recentChannelsLabel->setProperty("recentHeader", true);
190     recentChannelsLabel->setForegroundRole(QPalette::Dark);
191     recentChannelsLabel->hide();
192     recentChannelsLayout->addWidget(recentChannelsLabel);
193
194     otherLayout->addLayout(recentChannelsLayout);
195
196     layout->addLayout(otherLayout);
197
198     mainLayout->addStretch();
199
200 #ifdef APP_ACTIVATION
201     if (!Activation::instance().isActivated())
202         mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignRight);
203 #endif
204 }
205
206 void SearchView::appear() {
207     MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), true);
208
209     updateRecentKeywords();
210     updateRecentChannels();
211     queryEdit->selectAll();
212     queryEdit->enableSuggest();
213
214     if (!queryEdit->toWidget()->hasFocus()) queryEdit->toWidget()->setFocus();
215 }
216
217 void SearchView::disappear() {
218     MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), false);
219 }
220
221 void SearchView::updateRecentKeywords() {
222     // load
223     QSettings settings;
224     QStringList keywords = settings.value(recentKeywordsKey).toStringList();
225     if (keywords == recentKeywords) return;
226     recentKeywords = keywords;
227
228     // cleanup
229     QLayoutItem *item;
230     while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
231         item->widget()->close();
232         delete item;
233     }
234
235     recentKeywordsLabel->setVisible(!keywords.isEmpty());
236     The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
237
238     foreach (const QString &keyword, keywords) {
239         QString link = keyword;
240         QString display = keyword;
241         if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
242             int separator = keyword.indexOf("|");
243             if (separator > 0 && separator + 1 < keyword.length()) {
244                 link = keyword.left(separator);
245                 display = keyword.mid(separator+1);
246             }
247         }
248         bool needStatusTip = false;
249         if (display.length() > 24) {
250             display.truncate(24);
251             display.append("...");
252             needStatusTip = true;
253         }
254         QLabel *itemLabel = new QLabel("<a href=\"" + link
255                                        + "\" style=\"color:palette(text); text-decoration:none\">"
256                                        + display + "</a>", this);
257         itemLabel->setAttribute(Qt::WA_DeleteOnClose);
258         itemLabel->setProperty("recentItem", true);
259         itemLabel->setMaximumWidth(queryEdit->toWidget()->width() + watchButton->width());
260         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
261         // Make links navigable with the keyboard too
262         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
263         if (needStatusTip)
264             itemLabel->setStatusTip(link);
265         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
266         recentKeywordsLayout->addWidget(itemLabel);
267     }
268
269 }
270
271 void SearchView::updateRecentChannels() {
272     // load
273     QSettings settings;
274     QStringList keywords = settings.value(recentChannelsKey).toStringList();
275     if (keywords == recentChannels) return;
276     recentChannels = keywords;
277
278     // cleanup
279     QLayoutItem *item;
280     while ((item = recentChannelsLayout->takeAt(1)) != 0) {
281         item->widget()->close();
282         delete item;
283     }
284
285     recentChannelsLabel->setVisible(!keywords.isEmpty());
286     // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
287
288     foreach (const QString &keyword, keywords) {
289         QString link = keyword;
290         QString display = keyword;
291         int separator = keyword.indexOf('|');
292         if (separator > 0 && separator + 1 < keyword.length()) {
293             link = keyword.left(separator);
294             display = keyword.mid(separator+1);
295         }
296         QLabel *itemLabel = new QLabel("<a href=\"" + link
297                                        + "\" style=\"color:palette(text); text-decoration:none\">"
298                                        + display + "</a>", this);
299         itemLabel->setAttribute(Qt::WA_DeleteOnClose);
300         itemLabel->setProperty("recentItem", true);
301         itemLabel->setMaximumWidth(queryEdit->toWidget()->width() + watchButton->width());
302         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
303         // Make links navigable with the keyboard too
304         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
305
306         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
307         recentChannelsLayout->addWidget(itemLabel);
308     }
309
310 }
311
312 void SearchView::watch() {
313     QString query = queryEdit->text();
314     watch(query);
315 }
316
317 void SearchView::textChanged(const QString &text) {
318     watchButton->setEnabled(!text.simplified().isEmpty());
319 }
320
321 void SearchView::watch(const QString &query) {
322     QString q = query.simplified();
323
324     // check for empty query
325     if (q.length() == 0) {
326         queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
327         return;
328     }
329
330     SearchParams *searchParams = new SearchParams();
331     if (typeCombo->currentIndex() == 0)
332         searchParams->setKeywords(q);
333     else {
334         // remove spaces from channel name
335         q.remove(' ');
336         searchParams->setChannelId(q);
337         searchParams->setSortBy(SearchParams::SortByNewest);
338     }
339
340     // go!
341     emit search(searchParams);
342 }
343
344 void SearchView::watchChannel(const QString &channelId) {
345     if (channelId.length() == 0) {
346         queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
347         return;
348     }
349
350     QString id = channelId;
351
352     // Fix old settings
353     if (!id.startsWith("UC")) id = "UC" + id;
354
355     SearchParams *searchParams = new SearchParams();
356     searchParams->setChannelId(id);
357     searchParams->setSortBy(SearchParams::SortByNewest);
358
359     // go!
360     emit search(searchParams);
361 }
362
363 void SearchView::watchKeywords(const QString &query) {
364     QString q = query.simplified();
365
366     // check for empty query
367     if (query.length() == 0) {
368         queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
369         return;
370     }
371
372     if (typeCombo->currentIndex() == 0) {
373         queryEdit->setText(q);
374         watchButton->setEnabled(true);
375     }
376
377     SearchParams *searchParams = new SearchParams();
378     searchParams->setKeywords(q);
379
380     // go!
381     emit search(searchParams);
382 }
383
384 void SearchView::paintEvent(QPaintEvent *event) {
385     QWidget::paintEvent(event);
386 #if defined(APP_MAC) | defined(APP_WIN)
387     QBrush brush;
388     if (window()->isActiveWindow()) {
389         brush = Qt::white;
390     } else {
391         brush = palette().window();
392     }
393     QPainter painter(this);
394     painter.fillRect(0, 0, width(), height(), brush);
395     painter.end();
396 #endif
397 #ifdef APP_UBUNTU
398     QStyleOption o;
399     o.initFrom(this);
400     QPainter p(this);
401     style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
402 #endif
403 }
404
405 void SearchView::searchTypeChanged(int index) {
406     if (index == 0) {
407         queryEdit->setSuggester(youtubeSuggest);
408     } else {
409         queryEdit->setSuggester(channelSuggest);
410     }
411     queryEdit->selectAll();
412     queryEdit->toWidget()->setFocus();
413 }
414
415 void SearchView::suggestionAccepted(Suggestion *suggestion) {
416     if (suggestion->type == QLatin1String("channel")) {
417         watchChannel(suggestion->userData);
418     } else watch(suggestion->value);
419 }