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"
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::smallBold();
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:"
91 #if defined(APP_UBUNTU) || defined(APP_WIN)
97 .arg(Constants::WEBSITE, Constants::NAME)
99 welcomeLabel->setOpenExternalLinks(true);
101 QFont f = welcomeLabel->font();
102 f.setHintingPreference(QFont::PreferNoHinting);
103 f.setFamily("Segoe UI Light");
104 welcomeLabel->setFont(f);
106 layout->addWidget(welcomeLabel);
108 layout->addSpacing(PADDING / 2);
110 QBoxLayout *tipLayout = new QHBoxLayout();
111 tipLayout->setSpacing(10);
113 //: "Enter", as in "type". The whole phrase says: "Enter a keyword to start watching videos"
114 QLabel *tipLabel = new QLabel(tr("Enter"), this);
115 tipLabel->setFont(biggerFont);
116 tipLayout->addWidget(tipLabel);
118 typeCombo = new QComboBox(this);
119 typeCombo->addItem(tr("a keyword"));
120 typeCombo->addItem(tr("a channel"));
121 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);
126 tipLabel->setFont(biggerFont);
127 tipLayout->addWidget(tipLabel);
128 layout->addLayout(tipLayout);
130 layout->addSpacing(PADDING / 2);
132 QHBoxLayout *searchLayout = new QHBoxLayout();
133 searchLayout->setAlignment(Qt::AlignVCenter);
135 queryEdit = new SearchLineEdit(this);
136 queryEdit->setFont(biggerFont);
137 connect(queryEdit, SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
138 connect(queryEdit, SIGNAL(textEdited(const QString &)), SLOT(textChanged(const QString &)));
139 connect(queryEdit, SIGNAL(suggestionAccepted(Suggestion*)), SLOT(suggestionAccepted(Suggestion*)));
141 youtubeSuggest = new YTSuggester(this);
142 channelSuggest = new ChannelSuggest(this);
143 searchTypeChanged(0);
145 searchLayout->addWidget(queryEdit);
146 searchLayout->addSpacing(10);
148 watchButton = new QPushButton(tr("Watch"), this);
149 watchButton->setDefault(true);
150 watchButton->setEnabled(false);
151 watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
152 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
153 searchLayout->addWidget(watchButton);
155 layout->addItem(searchLayout);
157 layout->addSpacing(PADDING / 2);
159 QHBoxLayout *otherLayout = new QHBoxLayout();
160 otherLayout->setMargin(0);
161 otherLayout->setSpacing(10);
163 recentKeywordsLayout = new QVBoxLayout();
164 recentKeywordsLayout->setSpacing(5);
165 recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
166 recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
167 recentKeywordsLabel->setProperty("recentHeader", true);
168 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
169 recentKeywordsLabel->hide();
170 recentKeywordsLabel->setFont(smallerFont);
171 recentKeywordsLayout->addWidget(recentKeywordsLabel);
173 otherLayout->addLayout(recentKeywordsLayout);
176 recentChannelsLayout = new QVBoxLayout();
177 recentChannelsLayout->setSpacing(5);
178 recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
179 recentChannelsLabel = new QLabel(tr("Recent channels").toUpper(), this);
180 recentChannelsLabel->setProperty("recentHeader", true);
181 recentChannelsLabel->setForegroundRole(QPalette::Dark);
182 recentChannelsLabel->hide();
183 recentChannelsLabel->setFont(smallerFont);
184 recentChannelsLayout->addWidget(recentChannelsLabel);
186 otherLayout->addLayout(recentChannelsLayout);
188 layout->addLayout(otherLayout);
190 mainLayout->addStretch();
192 #ifdef APP_ACTIVATION
193 if (!Activation::instance().isActivated())
194 mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignRight);
198 void SearchView::appear() {
199 updateRecentKeywords();
200 updateRecentChannels();
201 queryEdit->selectAll();
202 queryEdit->enableSuggest();
203 QTimer::singleShot(0, queryEdit, SLOT(setFocus()));
206 void SearchView::updateRecentKeywords() {
210 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
211 item->widget()->close();
217 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
218 recentKeywordsLabel->setVisible(!keywords.isEmpty());
219 The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
221 foreach (QString keyword, keywords) {
222 QString link = keyword;
223 QString display = keyword;
224 if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
225 int separator = keyword.indexOf("|");
226 if (separator > 0 && separator + 1 < keyword.length()) {
227 link = keyword.left(separator);
228 display = keyword.mid(separator+1);
231 bool needStatusTip = false;
232 if (display.length() > 24) {
233 display.truncate(24);
234 display.append("...");
235 needStatusTip = true;
237 QLabel *itemLabel = new QLabel("<a href=\"" + link
238 + "\" style=\"color:palette(text); text-decoration:none\">"
239 + display + "</a>", this);
240 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
241 itemLabel->setProperty("recentItem", true);
242 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
243 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
244 // Make links navigable with the keyboard too
245 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
247 itemLabel->setStatusTip(link);
248 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
249 recentKeywordsLayout->addWidget(itemLabel);
254 void SearchView::updateRecentChannels() {
258 while ((item = recentChannelsLayout->takeAt(1)) != 0) {
259 item->widget()->close();
265 QStringList keywords = settings.value(recentChannelsKey).toStringList();
266 recentChannelsLabel->setVisible(!keywords.isEmpty());
267 // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
269 foreach (QString keyword, keywords) {
270 QString link = keyword;
271 QString display = keyword;
272 int separator = keyword.indexOf('|');
273 if (separator > 0 && separator + 1 < keyword.length()) {
274 link = keyword.left(separator);
275 display = keyword.mid(separator+1);
277 QLabel *itemLabel = new QLabel("<a href=\"" + link
278 + "\" style=\"color:palette(text); text-decoration:none\">"
279 + display + "</a>", this);
280 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
281 itemLabel->setProperty("recentItem", true);
282 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
283 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
284 // Make links navigable with the keyboard too
285 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
287 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
288 recentChannelsLayout->addWidget(itemLabel);
293 void SearchView::watch() {
294 QString query = queryEdit->text();
298 void SearchView::textChanged(const QString &text) {
299 watchButton->setEnabled(!text.simplified().isEmpty());
302 void SearchView::watch(QString query) {
304 query = query.simplified();
306 // check for empty query
307 if (query.length() == 0) {
308 queryEdit->setFocus(Qt::OtherFocusReason);
312 SearchParams *searchParams = new SearchParams();
313 if (typeCombo->currentIndex() == 0)
314 searchParams->setKeywords(query);
316 // remove spaces from channel name
317 query = query.simplified();
318 searchParams->setAuthor(query);
319 searchParams->setSortBy(SearchParams::SortByNewest);
323 emit search(searchParams);
326 void SearchView::watchChannel(QString channel) {
328 channel = channel.simplified();
330 // check for empty query
331 if (channel.length() == 0) {
332 queryEdit->setFocus(Qt::OtherFocusReason);
336 // remove spaces from channel name
337 channel = channel.remove(" ");
339 SearchParams *searchParams = new SearchParams();
340 searchParams->setAuthor(channel);
341 searchParams->setSortBy(SearchParams::SortByNewest);
344 emit search(searchParams);
347 void SearchView::watchKeywords(QString query) {
349 query = query.simplified();
351 // check for empty query
352 if (query.length() == 0) {
353 queryEdit->setFocus(Qt::OtherFocusReason);
357 if (typeCombo->currentIndex() == 0) {
358 queryEdit->setText(query);
359 watchButton->setEnabled(true);
362 SearchParams *searchParams = new SearchParams();
363 searchParams->setKeywords(query);
366 emit search(searchParams);
369 void SearchView::paintEvent(QPaintEvent *event) {
370 QWidget::paintEvent(event);
371 #if defined(APP_MAC) | defined(APP_WIN)
373 if (window()->isActiveWindow()) {
374 brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
376 brush = palette().window();
378 QPainter painter(this);
379 painter.fillRect(0, 0, width(), height(), brush);
386 style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
388 PainterUtils::topShadow(this);
391 void SearchView::searchTypeChanged(int index) {
393 queryEdit->setSuggester(youtubeSuggest);
395 queryEdit->setSuggester(channelSuggest);
397 queryEdit->selectAll();
398 queryEdit->setFocus();
401 void SearchView::suggestionAccepted(Suggestion *suggestion) {
402 watch(suggestion->value);