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) : View(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);
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 queryEdit = new SearchLineEdit(this);
143 queryEdit->toWidget()->setFont(biggerFont);
146 qDebug() << "queryEdit->toWidget()" << (queryEdit->toWidget() == 0) << queryEdit->toWidget();
147 connect(queryEdit->toWidget(), SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
148 connect(queryEdit->toWidget(), SIGNAL(textEdited(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);
159 watchButton->setDefault(true);
160 watchButton->setEnabled(false);
161 watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
162 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
163 searchLayout->addWidget(watchButton);
165 layout->addItem(searchLayout);
167 layout->addSpacing(PADDING / 2);
169 QHBoxLayout *otherLayout = new QHBoxLayout();
170 otherLayout->setMargin(0);
171 otherLayout->setSpacing(10);
173 recentKeywordsLayout = new QVBoxLayout();
174 recentKeywordsLayout->setSpacing(5);
175 recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
176 recentKeywordsLabel = new QLabel(tr("Recent keywords"), this);
177 recentKeywordsLabel->setProperty("recentHeader", true);
178 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
179 recentKeywordsLabel->hide();
180 recentKeywordsLayout->addWidget(recentKeywordsLabel);
182 otherLayout->addLayout(recentKeywordsLayout);
185 recentChannelsLayout = new QVBoxLayout();
186 recentChannelsLayout->setSpacing(5);
187 recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
188 recentChannelsLabel = new QLabel(tr("Recent channels"), this);
189 recentChannelsLabel->setProperty("recentHeader", true);
190 recentChannelsLabel->setForegroundRole(QPalette::Dark);
191 recentChannelsLabel->hide();
192 recentChannelsLayout->addWidget(recentChannelsLabel);
194 otherLayout->addLayout(recentChannelsLayout);
196 layout->addLayout(otherLayout);
198 mainLayout->addStretch();
200 #ifdef APP_ACTIVATION
201 if (!Activation::instance().isActivated())
202 mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignRight);
206 void SearchView::appear() {
207 MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), true);
209 updateRecentKeywords();
210 updateRecentChannels();
211 queryEdit->selectAll();
212 queryEdit->enableSuggest();
214 if (!queryEdit->toWidget()->hasFocus()) queryEdit->toWidget()->setFocus();
217 void SearchView::disappear() {
218 MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), false);
221 void SearchView::updateRecentKeywords() {
224 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
225 if (keywords == recentKeywords) return;
226 recentKeywords = keywords;
230 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
231 item->widget()->close();
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->toWidget()->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() {
274 QStringList keywords = settings.value(recentChannelsKey).toStringList();
275 if (keywords == recentChannels) return;
276 recentChannels = keywords;
280 while ((item = recentChannelsLayout->takeAt(1)) != 0) {
281 item->widget()->close();
285 recentChannelsLabel->setVisible(!keywords.isEmpty());
286 // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
288 foreach (const QString &keyword, keywords) {
289 QString link = keyword;
290 QString display = keyword;
291 int separator = keyword.indexOf('|');
292 if (separator > 0 && separator + 1 < keyword.length()) {
293 link = keyword.left(separator);
294 display = keyword.mid(separator+1);
296 QLabel *itemLabel = new QLabel("<a href=\"" + link
297 + "\" style=\"color:palette(text); text-decoration:none\">"
298 + display + "</a>", this);
299 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
300 itemLabel->setProperty("recentItem", true);
301 itemLabel->setMaximumWidth(queryEdit->toWidget()->width() + watchButton->width());
302 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
303 // Make links navigable with the keyboard too
304 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
306 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
307 recentChannelsLayout->addWidget(itemLabel);
312 void SearchView::watch() {
313 QString query = queryEdit->text();
317 void SearchView::textChanged(const QString &text) {
318 watchButton->setEnabled(!text.simplified().isEmpty());
321 void SearchView::watch(const QString &query) {
322 QString q = query.simplified();
324 // check for empty query
325 if (q.length() == 0) {
326 queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
330 SearchParams *searchParams = new SearchParams();
331 if (typeCombo->currentIndex() == 0)
332 searchParams->setKeywords(q);
334 // remove spaces from channel name
336 searchParams->setChannelId(q);
337 searchParams->setSortBy(SearchParams::SortByNewest);
341 emit search(searchParams);
344 void SearchView::watchChannel(const QString &channelId) {
345 if (channelId.length() == 0) {
346 queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
350 QString id = channelId;
353 if (!id.startsWith("UC")) id = "UC" + id;
355 SearchParams *searchParams = new SearchParams();
356 searchParams->setChannelId(id);
357 searchParams->setSortBy(SearchParams::SortByNewest);
360 emit search(searchParams);
363 void SearchView::watchKeywords(const QString &query) {
364 QString q = query.simplified();
366 // check for empty query
367 if (query.length() == 0) {
368 queryEdit->toWidget()->setFocus(Qt::OtherFocusReason);
372 if (typeCombo->currentIndex() == 0) {
373 queryEdit->setText(q);
374 watchButton->setEnabled(true);
377 SearchParams *searchParams = new SearchParams();
378 searchParams->setKeywords(q);
381 emit search(searchParams);
384 void SearchView::paintEvent(QPaintEvent *event) {
385 QWidget::paintEvent(event);
386 #if defined(APP_MAC) | defined(APP_WIN)
388 if (window()->isActiveWindow()) {
391 brush = palette().window();
393 QPainter painter(this);
394 painter.fillRect(0, 0, width(), height(), brush);
401 style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
405 void SearchView::searchTypeChanged(int index) {
407 queryEdit->setSuggester(youtubeSuggest);
409 queryEdit->setSuggester(channelSuggest);
411 queryEdit->selectAll();
412 queryEdit->toWidget()->setFocus();
415 void SearchView::suggestionAccepted(Suggestion *suggestion) {
416 if (suggestion->type == QLatin1String("channel")) {
417 watchChannel(suggestion->userData);
418 } else watch(suggestion->value);