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 const QFont &biggerFont = FontUtils::big();
109 //: "Enter", as in "type". The whole phrase says: "Enter a keyword to start watching videos"
110 QLabel *tipLabel = new QLabel(tr("Enter"), this);
112 tipLabel->setFont(biggerFont);
114 tipLayout->addWidget(tipLabel);
116 typeCombo = new QComboBox(this);
117 typeCombo->addItem(tr("a keyword"));
118 typeCombo->addItem(tr("a channel"));
120 typeCombo->setFont(biggerFont);
122 connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
123 tipLayout->addWidget(typeCombo);
125 tipLabel = new QLabel(tr("to start watching videos."), this);
127 tipLabel->setFont(biggerFont);
129 tipLayout->addWidget(tipLabel);
130 layout->addLayout(tipLayout);
132 layout->addSpacing(PADDING / 2);
134 QHBoxLayout *searchLayout = new QHBoxLayout();
135 searchLayout->setAlignment(Qt::AlignVCenter);
137 #ifdef APP_MAC_SEARCHFIELD
138 queryEdit = new SearchLineEditMac(this);
140 queryEdit = new SearchLineEdit(this);
141 queryEdit->toWidget()->setFont(biggerFont);
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*)));
149 youtubeSuggest = new YTSuggester(this);
150 channelSuggest = new ChannelSuggest(this);
151 searchTypeChanged(0);
153 searchLayout->addWidget(queryEdit->toWidget());
154 searchLayout->addSpacing(10);
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);
163 layout->addItem(searchLayout);
165 layout->addSpacing(PADDING / 2);
167 QHBoxLayout *otherLayout = new QHBoxLayout();
168 otherLayout->setMargin(0);
169 otherLayout->setSpacing(10);
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);
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"), this);
187 recentChannelsLabel->setProperty("recentHeader", true);
188 recentChannelsLabel->setForegroundRole(QPalette::Dark);
189 recentChannelsLabel->hide();
190 recentChannelsLayout->addWidget(recentChannelsLabel);
192 otherLayout->addLayout(recentChannelsLayout);
194 layout->addLayout(otherLayout);
196 mainLayout->addStretch();
198 #ifdef APP_ACTIVATION
199 if (!Activation::instance().isActivated())
200 mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignRight);
204 void SearchView::appear() {
205 MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), true);
207 updateRecentKeywords();
208 updateRecentChannels();
209 queryEdit->selectAll();
210 queryEdit->enableSuggest();
212 if (!queryEdit->toWidget()->hasFocus()) queryEdit->toWidget()->setFocus();
215 void SearchView::disappear() {
216 MainWindow::instance()->showActionInStatusBar(The::globalActions()->value("definition"), false);
219 void SearchView::updateRecentKeywords() {
222 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
223 if (keywords == recentKeywords) return;
224 recentKeywords = keywords;
228 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
229 item->widget()->close();
233 recentKeywordsLabel->setVisible(!keywords.isEmpty());
234 The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
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);
246 bool needStatusTip = false;
247 if (display.length() > 24) {
248 display.truncate(24);
249 display.append("...");
250 needStatusTip = true;
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);
262 itemLabel->setStatusTip(link);
263 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
264 recentKeywordsLayout->addWidget(itemLabel);
269 void SearchView::updateRecentChannels() {
272 QStringList keywords = settings.value(recentChannelsKey).toStringList();
273 if (keywords == recentChannels) return;
274 recentChannels = keywords;
278 while ((item = recentChannelsLayout->takeAt(1)) != 0) {
279 item->widget()->close();
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->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);
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->toWidget()->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->toWidget()->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->toWidget()->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()) {
389 brush = palette().window();
391 QPainter painter(this);
392 painter.fillRect(0, 0, width(), height(), brush);
399 style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
403 void SearchView::searchTypeChanged(int index) {
405 queryEdit->setSuggester(youtubeSuggest);
407 queryEdit->setSuggester(channelSuggest);
409 queryEdit->selectAll();
410 queryEdit->toWidget()->setFocus();
413 void SearchView::suggestionAccepted(Suggestion *suggestion) {
414 if (suggestion->type == QLatin1String("channel")) {
415 watchChannel(suggestion->userData);
416 } else watch(suggestion->value);