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