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 queryEdit->setMinimumWidth(queryEdit->fontInfo().pixelSize()*15);
138 connect(queryEdit, SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
139 connect(queryEdit, SIGNAL(textChanged(const QString &)), SLOT(textChanged(const QString &)));
140 connect(queryEdit, SIGNAL(suggestionAccepted(const QString&)), SLOT(watch(const QString&)));
142 youtubeSuggest = new YTSuggester(this);
143 channelSuggest = new ChannelSuggest(this);
144 searchTypeChanged(0);
146 searchLayout->addWidget(queryEdit);
147 searchLayout->addSpacing(10);
149 watchButton = new QPushButton(tr("Watch"), this);
150 watchButton->setDefault(true);
151 watchButton->setEnabled(false);
152 watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
153 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
154 searchLayout->addWidget(watchButton);
156 layout->addItem(searchLayout);
158 layout->addSpacing(PADDING / 2);
160 QHBoxLayout *otherLayout = new QHBoxLayout();
161 otherLayout->setMargin(0);
162 otherLayout->setSpacing(10);
164 recentKeywordsLayout = new QVBoxLayout();
165 recentKeywordsLayout->setSpacing(5);
166 recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
167 recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
168 recentKeywordsLabel->setProperty("recentHeader", true);
169 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
170 recentKeywordsLabel->hide();
171 recentKeywordsLabel->setFont(smallerFont);
172 recentKeywordsLayout->addWidget(recentKeywordsLabel);
174 otherLayout->addLayout(recentKeywordsLayout);
177 recentChannelsLayout = new QVBoxLayout();
178 recentChannelsLayout->setSpacing(5);
179 recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
180 recentChannelsLabel = new QLabel(tr("Recent channels").toUpper(), this);
181 recentChannelsLabel->setProperty("recentHeader", true);
182 recentChannelsLabel->setForegroundRole(QPalette::Dark);
183 recentChannelsLabel->hide();
184 recentChannelsLabel->setFont(smallerFont);
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 updateRecentKeywords();
201 updateRecentChannels();
202 queryEdit->selectAll();
203 queryEdit->enableSuggest();
204 QTimer::singleShot(0, queryEdit, SLOT(setFocus()));
207 void SearchView::updateRecentKeywords() {
211 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
212 item->widget()->close();
218 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
219 recentKeywordsLabel->setVisible(!keywords.isEmpty());
220 The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
222 foreach (QString keyword, keywords) {
223 QString link = keyword;
224 QString display = keyword;
225 if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
226 int separator = keyword.indexOf("|");
227 if (separator > 0 && separator + 1 < keyword.length()) {
228 link = keyword.left(separator);
229 display = keyword.mid(separator+1);
232 bool needStatusTip = false;
233 if (display.length() > 24) {
234 display.truncate(24);
235 display.append("...");
236 needStatusTip = true;
238 QLabel *itemLabel = new QLabel("<a href=\"" + link
239 + "\" style=\"color:palette(text); text-decoration:none\">"
240 + display + "</a>", this);
241 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
242 itemLabel->setProperty("recentItem", true);
243 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
244 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
245 // Make links navigable with the keyboard too
246 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
248 itemLabel->setStatusTip(link);
249 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
250 recentKeywordsLayout->addWidget(itemLabel);
255 void SearchView::updateRecentChannels() {
259 while ((item = recentChannelsLayout->takeAt(1)) != 0) {
260 item->widget()->close();
266 QStringList keywords = settings.value(recentChannelsKey).toStringList();
267 recentChannelsLabel->setVisible(!keywords.isEmpty());
268 // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
270 foreach (QString keyword, keywords) {
271 QString link = keyword;
272 QString display = keyword;
273 int separator = keyword.indexOf('|');
274 if (separator > 0 && separator + 1 < keyword.length()) {
275 link = keyword.left(separator);
276 display = keyword.mid(separator+1);
278 QLabel *itemLabel = new QLabel("<a href=\"" + link
279 + "\" style=\"color:palette(text); text-decoration:none\">"
280 + display + "</a>", this);
281 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
282 itemLabel->setProperty("recentItem", true);
283 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
284 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
285 // Make links navigable with the keyboard too
286 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
288 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
289 recentChannelsLayout->addWidget(itemLabel);
294 void SearchView::watch() {
295 QString query = queryEdit->text();
299 void SearchView::textChanged(const QString &text) {
300 watchButton->setEnabled(!text.simplified().isEmpty());
303 void SearchView::watch(QString query) {
305 query = query.simplified();
307 // check for empty query
308 if (query.length() == 0) {
309 queryEdit->setFocus(Qt::OtherFocusReason);
313 SearchParams *searchParams = new SearchParams();
314 if (typeCombo->currentIndex() == 0)
315 searchParams->setKeywords(query);
317 // remove spaces from channel name
318 query = query.simplified();
319 searchParams->setAuthor(query);
320 searchParams->setSortBy(SearchParams::SortByNewest);
324 emit search(searchParams);
327 void SearchView::watchChannel(QString channel) {
329 channel = channel.simplified();
331 // check for empty query
332 if (channel.length() == 0) {
333 queryEdit->setFocus(Qt::OtherFocusReason);
337 // remove spaces from channel name
338 channel = channel.remove(" ");
340 SearchParams *searchParams = new SearchParams();
341 searchParams->setAuthor(channel);
342 searchParams->setSortBy(SearchParams::SortByNewest);
345 emit search(searchParams);
348 void SearchView::watchKeywords(QString query) {
350 query = query.simplified();
352 // check for empty query
353 if (query.length() == 0) {
354 queryEdit->setFocus(Qt::OtherFocusReason);
358 if (typeCombo->currentIndex() == 0) {
359 queryEdit->setText(query);
360 watchButton->setEnabled(true);
363 SearchParams *searchParams = new SearchParams();
364 searchParams->setKeywords(query);
367 emit search(searchParams);
370 void SearchView::paintEvent(QPaintEvent *event) {
371 QWidget::paintEvent(event);
372 #if defined(APP_MAC) | defined(APP_WIN)
374 if (window()->isActiveWindow()) {
375 brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
377 brush = palette().window();
379 QPainter painter(this);
380 painter.fillRect(0, 0, width(), height(), brush);
387 style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
389 // PainterUtils::topShadow(this);
392 void SearchView::searchTypeChanged(int index) {
394 queryEdit->setSuggester(youtubeSuggest);
396 queryEdit->setSuggester(channelSuggest);
398 queryEdit->selectAll();
399 queryEdit->setFocus();