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 "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"
30 #include "searchlineedit.h"
36 #include "activation.h"
38 #include "mainwindow.h"
39 #include "painterutils.h"
40 #include "iconutils.h"
43 QHash<QString, QAction*>* globalActions();
46 static const QString recentKeywordsKey = "recentKeywords";
47 static const QString recentChannelsKey = "recentChannels";
48 static const int PADDING = 30;
50 SearchView::SearchView(QWidget *parent) : View(parent) {
52 #if defined(APP_MAC) | defined(APP_WIN)
53 // speedup painting since we'll paint the whole background
54 // by ourselves anyway in paintEvent()
55 setAttribute(Qt::WA_OpaquePaintEvent);
58 QBoxLayout *mainLayout = new QVBoxLayout(this);
59 mainLayout->setMargin(PADDING);
60 mainLayout->setSpacing(0);
62 // hidden message widget
63 message = new QLabel(this);
65 mainLayout->addWidget(message);
67 mainLayout->addStretch();
69 QBoxLayout *hLayout = new QHBoxLayout();
70 hLayout->setAlignment(Qt::AlignCenter);
71 mainLayout->addLayout(hLayout);
73 QLabel *logo = new QLabel(this);
74 logo->setPixmap(IconUtils::pixmap(":/images/app.png"));
75 hLayout->addWidget(logo, 0, Qt::AlignTop);
76 hLayout->addSpacing(PADDING);
78 QVBoxLayout *layout = new QVBoxLayout();
79 layout->setAlignment(Qt::AlignCenter);
80 hLayout->addLayout(layout);
82 QLabel *welcomeLabel =
83 new QLabel("<h1 style='font-weight:100'>" +
84 tr("Welcome to <a href='%1'>%2</a>,")
85 .replace("<a ", "<a style='text-decoration:none; color:palette(text);font-weight:normal' ")
86 .arg(Constants::WEBSITE, Constants::NAME)
88 welcomeLabel->setOpenExternalLinks(true);
89 welcomeLabel->setProperty("heading", true);
91 QFont f = welcomeLabel->font();
92 f.setFamily("Helvetica Neue");
93 f.setStyleName("Thin");
94 welcomeLabel->setFont(f);
96 QFont f = welcomeLabel->font();
97 f.setFamily("Segoe UI Light");
98 welcomeLabel->setFont(f);
100 layout->addWidget(welcomeLabel);
102 layout->addSpacing(PADDING / 2);
104 QBoxLayout *tipLayout = new QHBoxLayout();
105 tipLayout->setSpacing(10);
108 const QFont &biggerFont = FontUtils::big();
111 //: "Enter", as in "type". The whole phrase says: "Enter a keyword to start watching videos"
112 QLabel *tipLabel = new QLabel(tr("Enter"), this);
114 tipLabel->setFont(biggerFont);
116 tipLayout->addWidget(tipLabel);
118 typeCombo = new QComboBox(this);
119 typeCombo->addItem(tr("a keyword"));
120 typeCombo->addItem(tr("a channel"));
122 typeCombo->setFont(biggerFont);
124 connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
125 tipLayout->addWidget(typeCombo);
127 tipLabel = new QLabel(tr("to start watching videos."), this);
129 tipLabel->setFont(biggerFont);
131 tipLayout->addWidget(tipLabel);
132 layout->addLayout(tipLayout);
134 layout->addSpacing(PADDING / 2);
136 QHBoxLayout *searchLayout = new QHBoxLayout();
137 searchLayout->setAlignment(Qt::AlignVCenter);
139 #ifdef APP_MAC_SEARCHFIELD
140 queryEdit = new SearchLineEditMac(this);
142 SearchLineEdit *sle = new SearchLineEdit(this);
143 sle->setFont(biggerFont);
147 connect(queryEdit->toWidget(), SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
148 connect(queryEdit->toWidget(), SIGNAL(textChanged(const QString &)), SLOT(textChanged(const QString &)));
149 connect(queryEdit->toWidget(), SIGNAL(suggestionAccepted(Suggestion*)), SLOT(suggestionAccepted(Suggestion*)));
151 youtubeSuggest = new YTSuggester(this);
152 channelSuggest = new ChannelSuggest(this);
153 searchTypeChanged(0);
155 searchLayout->addWidget(queryEdit->toWidget());
156 searchLayout->addSpacing(10);
158 watchButton = new QPushButton(tr("Watch"), this);
160 watchButton->setFont(biggerFont);
162 watchButton->setDefault(true);
163 watchButton->setEnabled(false);
164 watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
165 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
166 searchLayout->addWidget(watchButton);
168 layout->addItem(searchLayout);
170 layout->addSpacing(PADDING / 2);
172 QHBoxLayout *otherLayout = new QHBoxLayout();
173 otherLayout->setMargin(0);
174 otherLayout->setSpacing(10);
176 recentKeywordsLayout = new QVBoxLayout();
177 recentKeywordsLayout->setSpacing(5);
178 recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
179 recentKeywordsLabel = new QLabel(tr("Recent keywords"), this);
180 recentKeywordsLabel->setProperty("recentHeader", true);
181 recentKeywordsLabel->hide();
182 recentKeywordsLayout->addWidget(recentKeywordsLabel);
184 otherLayout->addLayout(recentKeywordsLayout);
187 recentChannelsLayout = new QVBoxLayout();
188 recentChannelsLayout->setSpacing(5);
189 recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
190 recentChannelsLabel = new QLabel(tr("Recent channels"), this);
191 recentChannelsLabel->setProperty("recentHeader", true);
192 recentChannelsLabel->setForegroundRole(QPalette::Dark);
193 recentChannelsLabel->hide();
194 recentChannelsLayout->addWidget(recentChannelsLabel);
196 otherLayout->addLayout(recentChannelsLayout);
198 layout->addLayout(otherLayout);
200 mainLayout->addStretch();
202 #ifdef APP_ACTIVATION
203 if (!Activation::instance().isActivated())
204 mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignRight);
208 void SearchView::appear() {
209 MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), true);
211 updateRecentKeywords();
212 updateRecentChannels();
213 queryEdit->selectAll();
214 queryEdit->enableSuggest();
216 if (!queryEdit->toWidget()->hasFocus()) queryEdit->toWidget()->setFocus();
219 void SearchView::disappear() {
220 MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), false);
223 void SearchView::updateRecentKeywords() {
226 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
227 if (keywords == recentKeywords) return;
228 recentKeywords = keywords;
232 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
233 item->widget()->close();
237 recentKeywordsLabel->setVisible(!keywords.isEmpty());
238 The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
240 foreach (const QString &keyword, keywords) {
241 QString link = keyword;
242 QString display = keyword;
243 if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
244 int separator = keyword.indexOf("|");
245 if (separator > 0 && separator + 1 < keyword.length()) {
246 link = keyword.left(separator);
247 display = keyword.mid(separator+1);
250 bool needStatusTip = false;
251 if (display.length() > 24) {
252 display.truncate(24);
253 display.append("...");
254 needStatusTip = true;
256 QLabel *itemLabel = new QLabel("<a href=\"" + link
257 + "\" style=\"color:palette(text); text-decoration:none\">"
258 + display + "</a>", this);
259 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
260 itemLabel->setProperty("recentItem", true);
261 itemLabel->setMaximumWidth(queryEdit->toWidget()->width() + watchButton->width());
262 itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
263 // Make links navigable with the keyboard too
264 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
266 itemLabel->setStatusTip(link);
267 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
268 recentKeywordsLayout->addWidget(itemLabel);
273 void SearchView::updateRecentChannels() {
276 QStringList keywords = settings.value(recentChannelsKey).toStringList();
277 if (keywords == recentChannels) return;
278 recentChannels = keywords;
282 while ((item = recentChannelsLayout->takeAt(1)) != 0) {
283 item->widget()->close();
287 recentChannelsLabel->setVisible(!keywords.isEmpty());
288 // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
290 foreach (const QString &keyword, keywords) {
291 QString link = keyword;
292 QString display = keyword;
293 int separator = keyword.indexOf('|');
294 if (separator > 0 && separator + 1 < keyword.length()) {
295 link = keyword.left(separator);
296 display = keyword.mid(separator+1);
298 QLabel *itemLabel = new QLabel("<a href=\"" + link
299 + "\" style=\"color:palette(text); text-decoration:none\">"
300 + display + "</a>", this);
301 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
302 itemLabel->setProperty("recentItem", true);
303 itemLabel->setMaximumWidth(queryEdit->toWidget()->width() + watchButton->width());
304 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
305 // Make links navigable with the keyboard too
306 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
308 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
309 recentChannelsLayout->addWidget(itemLabel);
314 void SearchView::watch() {
315 QString query = queryEdit->text();
319 void SearchView::textChanged(const QString &text) {
320 watchButton->setEnabled(!text.simplified().isEmpty());
323 void SearchView::watch(const QString &query) {
324 QString q = query.simplified();
326 // check for empty query
327 if (q.length() == 0) {
328 queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
332 SearchParams *searchParams = new SearchParams();
333 if (typeCombo->currentIndex() == 0)
334 searchParams->setKeywords(q);
336 // remove spaces from channel name
338 searchParams->setChannelId(q);
339 searchParams->setSortBy(SearchParams::SortByNewest);
343 emit search(searchParams);
346 void SearchView::watchChannel(const QString &channelId) {
347 if (channelId.length() == 0) {
348 queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
352 QString id = channelId;
355 if (!id.startsWith("UC")) id = "UC" + id;
357 SearchParams *searchParams = new SearchParams();
358 searchParams->setChannelId(id);
359 searchParams->setSortBy(SearchParams::SortByNewest);
362 emit search(searchParams);
365 void SearchView::watchKeywords(const QString &query) {
366 QString q = query.simplified();
368 // check for empty query
369 if (query.length() == 0) {
370 queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
374 if (typeCombo->currentIndex() == 0) {
375 queryEdit->setText(q);
376 watchButton->setEnabled(true);
379 SearchParams *searchParams = new SearchParams();
380 searchParams->setKeywords(q);
383 emit search(searchParams);
386 void SearchView::paintEvent(QPaintEvent *event) {
387 QWidget::paintEvent(event);
388 #if defined(APP_MAC) | defined(APP_WIN)
390 if (window()->isActiveWindow()) {
393 brush = palette().window();
395 QPainter painter(this);
396 painter.fillRect(0, 0, width(), height(), brush);
403 style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
407 void SearchView::searchTypeChanged(int index) {
409 queryEdit->setSuggester(youtubeSuggest);
411 queryEdit->setSuggester(channelSuggest);
413 queryEdit->selectAll();
414 queryEdit->toWidget()->setFocus();
417 void SearchView::suggestionAccepted(Suggestion *suggestion) {
418 if (suggestion->type == QLatin1String("channel")) {
419 watchChannel(suggestion->userData);
420 } else watch(suggestion->value);