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 QFont biggerFont = FontUtils::big();
52 QFont smallerFont = FontUtils::small();
54 #if defined(APP_MAC) | defined(APP_WIN)
55 // speedup painting since we'll paint the whole background
56 // by ourselves anyway in paintEvent()
57 setAttribute(Qt::WA_OpaquePaintEvent);
60 setAutoFillBackground(true);
62 QBoxLayout *mainLayout = new QVBoxLayout(this);
63 mainLayout->setMargin(PADDING);
64 mainLayout->setSpacing(0);
66 // hidden message widget
67 message = new QLabel(this);
69 mainLayout->addWidget(message);
71 mainLayout->addStretch();
73 QBoxLayout *hLayout = new QHBoxLayout();
74 hLayout->setAlignment(Qt::AlignCenter);
75 mainLayout->addLayout(hLayout);
77 QLabel *logo = new QLabel(this);
78 logo->setPixmap(QPixmap(":/images/app.png"));
79 hLayout->addWidget(logo, 0, Qt::AlignTop);
80 hLayout->addSpacing(PADDING);
82 QVBoxLayout *layout = new QVBoxLayout();
83 layout->setAlignment(Qt::AlignCenter);
84 hLayout->addLayout(layout);
86 QLabel *welcomeLabel =
87 new QLabel("<h1 style='font-weight:100'>" +
88 tr("Welcome to <a href='%1'>%2</a>,")
89 // .replace("<a ", "<a style='color:palette(text)'")
90 .replace("<a ", "<a style='text-decoration:none; color:palette(text);font-weight:normal' ")
91 .arg(Constants::WEBSITE, Constants::NAME)
93 welcomeLabel->setOpenExternalLinks(true);
94 welcomeLabel->setProperty("heading", true);
96 QFont f = welcomeLabel->font();
97 f.setFamily("Helvetica Neue");
98 f.setStyleName("Thin");
99 welcomeLabel->setFont(f);
101 QFont f = welcomeLabel->font();
102 f.setFamily("Segoe UI Light");
103 welcomeLabel->setFont(f);
105 layout->addWidget(welcomeLabel);
107 layout->addSpacing(PADDING / 2);
109 QBoxLayout *tipLayout = new QHBoxLayout();
110 tipLayout->setSpacing(10);
112 //: "Enter", as in "type". The whole phrase says: "Enter a keyword to start watching videos"
113 QLabel *tipLabel = new QLabel(tr("Enter"), this);
115 tipLabel->setFont(biggerFont);
117 tipLayout->addWidget(tipLabel);
119 typeCombo = new QComboBox(this);
120 typeCombo->addItem(tr("a keyword"));
121 typeCombo->addItem(tr("a channel"));
123 typeCombo->setFont(biggerFont);
125 connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
126 tipLayout->addWidget(typeCombo);
128 tipLabel = new QLabel(tr("to start watching videos."), this);
130 tipLabel->setFont(biggerFont);
132 tipLayout->addWidget(tipLabel);
133 layout->addLayout(tipLayout);
135 layout->addSpacing(PADDING / 2);
137 QHBoxLayout *searchLayout = new QHBoxLayout();
138 searchLayout->setAlignment(Qt::AlignVCenter);
140 queryEdit = new SearchLineEdit(this);
141 #ifndef APP_MAC_SEARCHFIELD
142 queryEdit->setFont(biggerFont);
144 connect(queryEdit, SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
145 connect(queryEdit, SIGNAL(textEdited(const QString &)), SLOT(textChanged(const QString &)));
146 connect(queryEdit, SIGNAL(suggestionAccepted(Suggestion*)), SLOT(suggestionAccepted(Suggestion*)));
148 youtubeSuggest = new YTSuggester(this);
149 channelSuggest = new ChannelSuggest(this);
150 searchTypeChanged(0);
152 searchLayout->addWidget(queryEdit);
153 searchLayout->addSpacing(10);
155 watchButton = new QPushButton(tr("Watch"), this);
156 watchButton->setDefault(true);
157 watchButton->setEnabled(false);
158 watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
159 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
160 searchLayout->addWidget(watchButton);
162 layout->addItem(searchLayout);
164 layout->addSpacing(PADDING / 2);
166 QHBoxLayout *otherLayout = new QHBoxLayout();
167 otherLayout->setMargin(0);
168 otherLayout->setSpacing(10);
170 recentKeywordsLayout = new QVBoxLayout();
171 recentKeywordsLayout->setSpacing(5);
172 recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
173 recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
174 recentKeywordsLabel->setProperty("recentHeader", true);
175 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
176 recentKeywordsLabel->hide();
177 recentKeywordsLabel->setFont(smallerFont);
178 recentKeywordsLayout->addWidget(recentKeywordsLabel);
180 otherLayout->addLayout(recentKeywordsLayout);
183 recentChannelsLayout = new QVBoxLayout();
184 recentChannelsLayout->setSpacing(5);
185 recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
186 recentChannelsLabel = new QLabel(tr("Recent channels").toUpper(), this);
187 recentChannelsLabel->setProperty("recentHeader", true);
188 recentChannelsLabel->setForegroundRole(QPalette::Dark);
189 recentChannelsLabel->hide();
190 recentChannelsLabel->setFont(smallerFont);
191 recentChannelsLayout->addWidget(recentChannelsLabel);
193 otherLayout->addLayout(recentChannelsLayout);
195 layout->addLayout(otherLayout);
197 mainLayout->addStretch();
199 #ifdef APP_ACTIVATION
200 if (!Activation::instance().isActivated())
201 mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignRight);
205 void SearchView::appear() {
206 setUpdatesEnabled(false);
207 updateRecentKeywords();
208 updateRecentChannels();
209 queryEdit->selectAll();
210 queryEdit->enableSuggest();
211 setUpdatesEnabled(true);
213 MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), true);
215 queryEdit->setFocus();
216 QTimer::singleShot(100, queryEdit, SLOT(setFocus()));
219 void SearchView::disappear() {
220 MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), false);
223 void SearchView::updateRecentKeywords() {
227 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
228 item->widget()->close();
234 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
235 recentKeywordsLabel->setVisible(!keywords.isEmpty());
236 The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
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);
248 bool needStatusTip = false;
249 if (display.length() > 24) {
250 display.truncate(24);
251 display.append("...");
252 needStatusTip = true;
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->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);
264 itemLabel->setStatusTip(link);
265 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
266 recentKeywordsLayout->addWidget(itemLabel);
271 void SearchView::updateRecentChannels() {
275 while ((item = recentChannelsLayout->takeAt(1)) != 0) {
276 item->widget()->close();
282 QStringList keywords = settings.value(recentChannelsKey).toStringList();
283 recentChannelsLabel->setVisible(!keywords.isEmpty());
284 // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
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);
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->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);
304 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
305 recentChannelsLayout->addWidget(itemLabel);
310 void SearchView::watch() {
311 QString query = queryEdit->text();
315 void SearchView::textChanged(const QString &text) {
316 watchButton->setEnabled(!text.simplified().isEmpty());
319 void SearchView::watch(const QString &query) {
320 QString q = query.simplified();
322 // check for empty query
323 if (q.length() == 0) {
324 queryEdit->setFocus(Qt::OtherFocusReason);
328 SearchParams *searchParams = new SearchParams();
329 if (typeCombo->currentIndex() == 0)
330 searchParams->setKeywords(q);
332 // remove spaces from channel name
334 searchParams->setChannelId(q);
335 searchParams->setSortBy(SearchParams::SortByNewest);
339 emit search(searchParams);
342 void SearchView::watchChannel(const QString &channelId) {
343 if (channelId.length() == 0) {
344 queryEdit->setFocus(Qt::OtherFocusReason);
348 QString id = channelId;
351 if (!id.startsWith("UC")) id = "UC" + id;
353 SearchParams *searchParams = new SearchParams();
354 searchParams->setChannelId(id);
355 searchParams->setSortBy(SearchParams::SortByNewest);
358 emit search(searchParams);
361 void SearchView::watchKeywords(const QString &query) {
362 QString q = query.simplified();
364 // check for empty query
365 if (query.length() == 0) {
366 queryEdit->setFocus(Qt::OtherFocusReason);
370 if (typeCombo->currentIndex() == 0) {
371 queryEdit->setText(q);
372 watchButton->setEnabled(true);
375 SearchParams *searchParams = new SearchParams();
376 searchParams->setKeywords(q);
379 emit search(searchParams);
382 void SearchView::paintEvent(QPaintEvent *event) {
383 QWidget::paintEvent(event);
384 #if defined(APP_MAC) | defined(APP_WIN)
386 if (window()->isActiveWindow()) {
387 // brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
390 brush = palette().window();
392 QPainter painter(this);
393 painter.fillRect(0, 0, width(), height(), brush);
400 style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
402 // PainterUtils::topShadow(this);
405 void SearchView::searchTypeChanged(int index) {
407 queryEdit->setSuggester(youtubeSuggest);
409 queryEdit->setSuggester(channelSuggest);
411 queryEdit->selectAll();
412 queryEdit->setFocus();
415 void SearchView::suggestionAccepted(Suggestion *suggestion) {
416 if (suggestion->type == QLatin1String("channel")) {
417 watchChannel(suggestion->userData);
418 } else watch(suggestion->value);