]> git.sur5r.net Git - minitube/blob - src/searchview.cpp
d09c51e3ba749f194c1522c91992acd20ded9373
[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) : QWidget(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     //: "Enter", as in "type". The whole phrase says: "Enter a keyword to start watching videos"
108     QLabel *tipLabel = new QLabel(tr("Enter"), this);
109 #ifndef APP_MAC
110     const QFont &biggerFont = FontUtils::big();
111     tipLabel->setFont(biggerFont);
112 #endif
113     tipLayout->addWidget(tipLabel);
114
115     typeCombo = new QComboBox(this);
116     typeCombo->addItem(tr("a keyword"));
117     typeCombo->addItem(tr("a channel"));
118 #ifndef APP_MAC
119     typeCombo->setFont(biggerFont);
120 #endif
121     connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
122     tipLayout->addWidget(typeCombo);
123
124     tipLabel = new QLabel(tr("to start watching videos."), this);
125 #ifndef APP_MAC
126     tipLabel->setFont(biggerFont);
127 #endif
128     tipLayout->addWidget(tipLabel);
129     layout->addLayout(tipLayout);
130
131     layout->addSpacing(PADDING / 2);
132
133     QHBoxLayout *searchLayout = new QHBoxLayout();
134     searchLayout->setAlignment(Qt::AlignVCenter);
135
136     queryEdit = new SearchLineEdit(this);
137 #ifndef APP_MAC_SEARCHFIELD
138     queryEdit->setFont(biggerFont);
139 #endif
140     connect(queryEdit, SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
141     connect(queryEdit, SIGNAL(textEdited(const QString &)), SLOT(textChanged(const QString &)));
142     connect(queryEdit, SIGNAL(suggestionAccepted(Suggestion*)), SLOT(suggestionAccepted(Suggestion*)));
143
144     youtubeSuggest = new YTSuggester(this);
145     channelSuggest = new ChannelSuggest(this);
146     searchTypeChanged(0);
147
148     searchLayout->addWidget(queryEdit);
149     searchLayout->addSpacing(10);
150
151     watchButton = new QPushButton(tr("Watch"), this);
152     watchButton->setDefault(true);
153     watchButton->setEnabled(false);
154     watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
155     connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
156     searchLayout->addWidget(watchButton);
157
158     layout->addItem(searchLayout);
159
160     layout->addSpacing(PADDING / 2);
161
162     QHBoxLayout *otherLayout = new QHBoxLayout();
163     otherLayout->setMargin(0);
164     otherLayout->setSpacing(10);
165
166     recentKeywordsLayout = new QVBoxLayout();
167     recentKeywordsLayout->setSpacing(5);
168     recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
169     recentKeywordsLabel = new QLabel(tr("Recent keywords"), this);
170     recentKeywordsLabel->setProperty("recentHeader", true);
171     recentKeywordsLabel->setForegroundRole(QPalette::Dark);
172     recentKeywordsLabel->hide();
173     recentKeywordsLayout->addWidget(recentKeywordsLabel);
174
175     otherLayout->addLayout(recentKeywordsLayout);
176
177     // recent channels
178     recentChannelsLayout = new QVBoxLayout();
179     recentChannelsLayout->setSpacing(5);
180     recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
181     recentChannelsLabel = new QLabel(tr("Recent channels"), this);
182     recentChannelsLabel->setProperty("recentHeader", true);
183     recentChannelsLabel->setForegroundRole(QPalette::Dark);
184     recentChannelsLabel->hide();
185     recentChannelsLayout->addWidget(recentChannelsLabel);
186
187     otherLayout->addLayout(recentChannelsLayout);
188
189     layout->addLayout(otherLayout);
190
191     mainLayout->addStretch();
192
193 #ifdef APP_ACTIVATION
194     if (!Activation::instance().isActivated())
195         mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignRight);
196 #endif
197 }
198
199 void SearchView::appear() {
200     MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), true);
201
202     updateRecentKeywords();
203     updateRecentChannels();
204     queryEdit->selectAll();
205     queryEdit->enableSuggest();
206
207     if (!queryEdit->hasFocus()) queryEdit->setFocus();
208 }
209
210 void SearchView::disappear() {
211     MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), false);
212 }
213
214 void SearchView::updateRecentKeywords() {
215     // load
216     QSettings settings;
217     QStringList keywords = settings.value(recentKeywordsKey).toStringList();
218     if (keywords == recentKeywords) return;
219     recentKeywords = keywords;
220
221     // cleanup
222     QLayoutItem *item;
223     while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
224         item->widget()->close();
225         delete item;
226     }
227
228     recentKeywordsLabel->setVisible(!keywords.isEmpty());
229     The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
230
231     foreach (const QString &keyword, keywords) {
232         QString link = keyword;
233         QString display = keyword;
234         if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
235             int separator = keyword.indexOf("|");
236             if (separator > 0 && separator + 1 < keyword.length()) {
237                 link = keyword.left(separator);
238                 display = keyword.mid(separator+1);
239             }
240         }
241         bool needStatusTip = false;
242         if (display.length() > 24) {
243             display.truncate(24);
244             display.append("...");
245             needStatusTip = true;
246         }
247         QLabel *itemLabel = new QLabel("<a href=\"" + link
248                                        + "\" style=\"color:palette(text); text-decoration:none\">"
249                                        + display + "</a>", this);
250         itemLabel->setAttribute(Qt::WA_DeleteOnClose);
251         itemLabel->setProperty("recentItem", true);
252         itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
253         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
254         // Make links navigable with the keyboard too
255         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
256         if (needStatusTip)
257             itemLabel->setStatusTip(link);
258         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
259         recentKeywordsLayout->addWidget(itemLabel);
260     }
261
262 }
263
264 void SearchView::updateRecentChannels() {
265     // load
266     QSettings settings;
267     QStringList keywords = settings.value(recentChannelsKey).toStringList();
268     if (keywords == recentChannels) return;
269     recentChannels = keywords;
270
271     // cleanup
272     QLayoutItem *item;
273     while ((item = recentChannelsLayout->takeAt(1)) != 0) {
274         item->widget()->close();
275         delete item;
276     }
277
278     recentChannelsLabel->setVisible(!keywords.isEmpty());
279     // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
280
281     foreach (const QString &keyword, keywords) {
282         QString link = keyword;
283         QString display = keyword;
284         int separator = keyword.indexOf('|');
285         if (separator > 0 && separator + 1 < keyword.length()) {
286             link = keyword.left(separator);
287             display = keyword.mid(separator+1);
288         }
289         QLabel *itemLabel = new QLabel("<a href=\"" + link
290                                        + "\" style=\"color:palette(text); text-decoration:none\">"
291                                        + display + "</a>", this);
292         itemLabel->setAttribute(Qt::WA_DeleteOnClose);
293         itemLabel->setProperty("recentItem", true);
294         itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
295         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
296         // Make links navigable with the keyboard too
297         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
298
299         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
300         recentChannelsLayout->addWidget(itemLabel);
301     }
302
303 }
304
305 void SearchView::watch() {
306     QString query = queryEdit->text();
307     watch(query);
308 }
309
310 void SearchView::textChanged(const QString &text) {
311     watchButton->setEnabled(!text.simplified().isEmpty());
312 }
313
314 void SearchView::watch(const QString &query) {
315     QString q = query.simplified();
316
317     // check for empty query
318     if (q.length() == 0) {
319         queryEdit->setFocus(Qt::OtherFocusReason);
320         return;
321     }
322
323     SearchParams *searchParams = new SearchParams();
324     if (typeCombo->currentIndex() == 0)
325         searchParams->setKeywords(q);
326     else {
327         // remove spaces from channel name
328         q.remove(' ');
329         searchParams->setChannelId(q);
330         searchParams->setSortBy(SearchParams::SortByNewest);
331     }
332
333     // go!
334     emit search(searchParams);
335 }
336
337 void SearchView::watchChannel(const QString &channelId) {
338     if (channelId.length() == 0) {
339         queryEdit->setFocus(Qt::OtherFocusReason);
340         return;
341     }
342
343     QString id = channelId;
344
345     // Fix old settings
346     if (!id.startsWith("UC")) id = "UC" + id;
347
348     SearchParams *searchParams = new SearchParams();
349     searchParams->setChannelId(id);
350     searchParams->setSortBy(SearchParams::SortByNewest);
351
352     // go!
353     emit search(searchParams);
354 }
355
356 void SearchView::watchKeywords(const QString &query) {
357     QString q = query.simplified();
358
359     // check for empty query
360     if (query.length() == 0) {
361         queryEdit->setFocus(Qt::OtherFocusReason);
362         return;
363     }
364
365     if (typeCombo->currentIndex() == 0) {
366         queryEdit->setText(q);
367         watchButton->setEnabled(true);
368     }
369
370     SearchParams *searchParams = new SearchParams();
371     searchParams->setKeywords(q);
372
373     // go!
374     emit search(searchParams);
375 }
376
377 void SearchView::paintEvent(QPaintEvent *event) {
378     QWidget::paintEvent(event);
379 #if defined(APP_MAC) | defined(APP_WIN)
380     QBrush brush;
381     if (window()->isActiveWindow()) {
382         brush = Qt::white;
383     } else {
384         brush = palette().window();
385     }
386     QPainter painter(this);
387     painter.fillRect(0, 0, width(), height(), brush);
388     painter.end();
389 #endif
390 #ifdef APP_UBUNTU
391     QStyleOption o;
392     o.initFrom(this);
393     QPainter p(this);
394     style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
395 #endif
396 }
397
398 void SearchView::searchTypeChanged(int index) {
399     if (index == 0) {
400         queryEdit->setSuggester(youtubeSuggest);
401     } else {
402         queryEdit->setSuggester(channelSuggest);
403     }
404     queryEdit->selectAll();
405     queryEdit->setFocus();
406 }
407
408 void SearchView::suggestionAccepted(Suggestion *suggestion) {
409     if (suggestion->type == QLatin1String("channel")) {
410         watchChannel(suggestion->userData);
411     } else watch(suggestion->value);
412 }