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"
42 QHash<QString, QAction*>* globalActions();
45 static const QString recentKeywordsKey = "recentKeywords";
46 static const QString recentChannelsKey = "recentChannels";
47 static const int PADDING = 30;
49 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
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);
57 QBoxLayout *mainLayout = new QVBoxLayout(this);
58 mainLayout->setMargin(PADDING);
59 mainLayout->setSpacing(0);
61 // hidden message widget
62 message = new QLabel(this);
64 mainLayout->addWidget(message);
66 mainLayout->addStretch();
68 QBoxLayout *hLayout = new QHBoxLayout();
69 hLayout->setAlignment(Qt::AlignCenter);
70 mainLayout->addLayout(hLayout);
72 QLabel *logo = new QLabel(this);
73 logo->setPixmap(QPixmap(":/images/app.png"));
74 hLayout->addWidget(logo, 0, Qt::AlignTop);
75 hLayout->addSpacing(PADDING);
77 QVBoxLayout *layout = new QVBoxLayout();
78 layout->setAlignment(Qt::AlignCenter);
79 hLayout->addLayout(layout);
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)
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);
107 //: "Enter", as in "type". The whole phrase says: "Enter a keyword to start watching videos"
108 QLabel *tipLabel = new QLabel(tr("Enter"), this);
110 const QFont &biggerFont = FontUtils::big();
111 tipLabel->setFont(biggerFont);
113 tipLayout->addWidget(tipLabel);
115 typeCombo = new QComboBox(this);
116 typeCombo->addItem(tr("a keyword"));
117 typeCombo->addItem(tr("a channel"));
119 typeCombo->setFont(biggerFont);
121 connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
122 tipLayout->addWidget(typeCombo);
124 tipLabel = new QLabel(tr("to start watching videos."), this);
126 tipLabel->setFont(biggerFont);
128 tipLayout->addWidget(tipLabel);
129 layout->addLayout(tipLayout);
131 layout->addSpacing(PADDING / 2);
133 QHBoxLayout *searchLayout = new QHBoxLayout();
134 searchLayout->setAlignment(Qt::AlignVCenter);
136 queryEdit = new SearchLineEdit(this);
137 #ifndef APP_MAC_SEARCHFIELD
138 queryEdit->setFont(biggerFont);
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*)));
144 youtubeSuggest = new YTSuggester(this);
145 channelSuggest = new ChannelSuggest(this);
146 searchTypeChanged(0);
148 searchLayout->addWidget(queryEdit);
149 searchLayout->addSpacing(10);
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);
158 layout->addItem(searchLayout);
160 layout->addSpacing(PADDING / 2);
162 QHBoxLayout *otherLayout = new QHBoxLayout();
163 otherLayout->setMargin(0);
164 otherLayout->setSpacing(10);
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);
175 otherLayout->addLayout(recentKeywordsLayout);
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);
187 otherLayout->addLayout(recentChannelsLayout);
189 layout->addLayout(otherLayout);
191 mainLayout->addStretch();
193 #ifdef APP_ACTIVATION
194 if (!Activation::instance().isActivated())
195 mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignRight);
199 void SearchView::appear() {
200 MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), true);
202 updateRecentKeywords();
203 updateRecentChannels();
204 queryEdit->selectAll();
205 queryEdit->enableSuggest();
207 if (!queryEdit->hasFocus()) queryEdit->setFocus();
210 void SearchView::disappear() {
211 MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), false);
214 void SearchView::updateRecentKeywords() {
217 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
218 if (keywords == recentKeywords) return;
219 recentKeywords = keywords;
223 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
224 item->widget()->close();
228 recentKeywordsLabel->setVisible(!keywords.isEmpty());
229 The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
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);
241 bool needStatusTip = false;
242 if (display.length() > 24) {
243 display.truncate(24);
244 display.append("...");
245 needStatusTip = true;
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);
257 itemLabel->setStatusTip(link);
258 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
259 recentKeywordsLayout->addWidget(itemLabel);
264 void SearchView::updateRecentChannels() {
267 QStringList keywords = settings.value(recentChannelsKey).toStringList();
268 if (keywords == recentChannels) return;
269 recentChannels = keywords;
273 while ((item = recentChannelsLayout->takeAt(1)) != 0) {
274 item->widget()->close();
278 recentChannelsLabel->setVisible(!keywords.isEmpty());
279 // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
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);
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);
299 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
300 recentChannelsLayout->addWidget(itemLabel);
305 void SearchView::watch() {
306 QString query = queryEdit->text();
310 void SearchView::textChanged(const QString &text) {
311 watchButton->setEnabled(!text.simplified().isEmpty());
314 void SearchView::watch(const QString &query) {
315 QString q = query.simplified();
317 // check for empty query
318 if (q.length() == 0) {
319 queryEdit->setFocus(Qt::OtherFocusReason);
323 SearchParams *searchParams = new SearchParams();
324 if (typeCombo->currentIndex() == 0)
325 searchParams->setKeywords(q);
327 // remove spaces from channel name
329 searchParams->setChannelId(q);
330 searchParams->setSortBy(SearchParams::SortByNewest);
334 emit search(searchParams);
337 void SearchView::watchChannel(const QString &channelId) {
338 if (channelId.length() == 0) {
339 queryEdit->setFocus(Qt::OtherFocusReason);
343 QString id = channelId;
346 if (!id.startsWith("UC")) id = "UC" + id;
348 SearchParams *searchParams = new SearchParams();
349 searchParams->setChannelId(id);
350 searchParams->setSortBy(SearchParams::SortByNewest);
353 emit search(searchParams);
356 void SearchView::watchKeywords(const QString &query) {
357 QString q = query.simplified();
359 // check for empty query
360 if (query.length() == 0) {
361 queryEdit->setFocus(Qt::OtherFocusReason);
365 if (typeCombo->currentIndex() == 0) {
366 queryEdit->setText(q);
367 watchButton->setEnabled(true);
370 SearchParams *searchParams = new SearchParams();
371 searchParams->setKeywords(q);
374 emit search(searchParams);
377 void SearchView::paintEvent(QPaintEvent *event) {
378 QWidget::paintEvent(event);
379 #if defined(APP_MAC) | defined(APP_WIN)
381 if (window()->isActiveWindow()) {
384 brush = palette().window();
386 QPainter painter(this);
387 painter.fillRect(0, 0, width(), height(), brush);
394 style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
398 void SearchView::searchTypeChanged(int index) {
400 queryEdit->setSuggester(youtubeSuggest);
402 queryEdit->setSuggester(channelSuggest);
404 queryEdit->selectAll();
405 queryEdit->setFocus();
408 void SearchView::suggestionAccepted(Suggestion *suggestion) {
409 if (suggestion->type == QLatin1String("channel")) {
410 watchChannel(suggestion->userData);
411 } else watch(suggestion->value);