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:normal'>" +
88 tr("Welcome to <a href='%1'>%2</a>,")
89 // .replace("<a ", "<a style='color:palette(text)'")
90 .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
91 .arg(Constants::WEBSITE, Constants::NAME)
93 welcomeLabel->setOpenExternalLinks(true);
95 QFont f = welcomeLabel->font();
96 f.setHintingPreference(QFont::PreferNoHinting);
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 //: "Enter", as in "type". The whole phrase says: "Enter a keyword to start watching videos"
108 QLabel *tipLabel = new QLabel(tr("Enter"), this);
109 tipLabel->setFont(biggerFont);
110 tipLayout->addWidget(tipLabel);
112 typeCombo = new QComboBox(this);
113 typeCombo->addItem(tr("a keyword"));
114 typeCombo->addItem(tr("a channel"));
115 typeCombo->setFont(biggerFont);
116 connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
117 tipLayout->addWidget(typeCombo);
119 tipLabel = new QLabel(tr("to start watching videos."), this);
120 tipLabel->setFont(biggerFont);
121 tipLayout->addWidget(tipLabel);
122 layout->addLayout(tipLayout);
124 layout->addSpacing(PADDING / 2);
126 QHBoxLayout *searchLayout = new QHBoxLayout();
127 searchLayout->setAlignment(Qt::AlignVCenter);
129 queryEdit = new SearchLineEdit(this);
130 queryEdit->setFont(biggerFont);
131 queryEdit->setMinimumWidth(queryEdit->fontInfo().pixelSize()*15);
132 connect(queryEdit, SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
133 connect(queryEdit, SIGNAL(textChanged(const QString &)), SLOT(textChanged(const QString &)));
134 connect(queryEdit, SIGNAL(suggestionAccepted(const QString&)), SLOT(watch(const QString&)));
136 youtubeSuggest = new YTSuggester(this);
137 channelSuggest = new ChannelSuggest(this);
138 searchTypeChanged(0);
140 searchLayout->addWidget(queryEdit);
141 searchLayout->addSpacing(10);
143 watchButton = new QPushButton(tr("Watch"), this);
144 watchButton->setDefault(true);
145 watchButton->setEnabled(false);
146 watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
147 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
148 searchLayout->addWidget(watchButton);
150 layout->addItem(searchLayout);
152 layout->addSpacing(PADDING / 2);
154 QHBoxLayout *otherLayout = new QHBoxLayout();
155 otherLayout->setMargin(0);
156 otherLayout->setSpacing(10);
158 recentKeywordsLayout = new QVBoxLayout();
159 recentKeywordsLayout->setSpacing(5);
160 recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
161 recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
162 recentKeywordsLabel->setProperty("recentHeader", true);
163 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
164 recentKeywordsLabel->hide();
165 recentKeywordsLabel->setFont(smallerFont);
166 recentKeywordsLayout->addWidget(recentKeywordsLabel);
168 otherLayout->addLayout(recentKeywordsLayout);
171 recentChannelsLayout = new QVBoxLayout();
172 recentChannelsLayout->setSpacing(5);
173 recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
174 recentChannelsLabel = new QLabel(tr("Recent channels").toUpper(), this);
175 recentChannelsLabel->setProperty("recentHeader", true);
176 recentChannelsLabel->setForegroundRole(QPalette::Dark);
177 recentChannelsLabel->hide();
178 recentChannelsLabel->setFont(smallerFont);
179 recentChannelsLayout->addWidget(recentChannelsLabel);
181 otherLayout->addLayout(recentChannelsLayout);
183 layout->addLayout(otherLayout);
185 mainLayout->addStretch();
187 #ifdef APP_ACTIVATION
188 if (!Activation::instance().isActivated())
189 mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignRight);
193 void SearchView::appear() {
194 updateRecentKeywords();
195 updateRecentChannels();
196 queryEdit->selectAll();
197 queryEdit->enableSuggest();
198 QTimer::singleShot(0, queryEdit, SLOT(setFocus()));
201 void SearchView::updateRecentKeywords() {
205 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
206 item->widget()->close();
212 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
213 recentKeywordsLabel->setVisible(!keywords.isEmpty());
214 The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
216 foreach (QString keyword, keywords) {
217 QString link = keyword;
218 QString display = keyword;
219 if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
220 int separator = keyword.indexOf("|");
221 if (separator > 0 && separator + 1 < keyword.length()) {
222 link = keyword.left(separator);
223 display = keyword.mid(separator+1);
226 bool needStatusTip = false;
227 if (display.length() > 24) {
228 display.truncate(24);
229 display.append("...");
230 needStatusTip = true;
232 QLabel *itemLabel = new QLabel("<a href=\"" + link
233 + "\" style=\"color:palette(text); text-decoration:none\">"
234 + display + "</a>", this);
235 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
236 itemLabel->setProperty("recentItem", true);
237 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
238 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
239 // Make links navigable with the keyboard too
240 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
242 itemLabel->setStatusTip(link);
243 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
244 recentKeywordsLayout->addWidget(itemLabel);
249 void SearchView::updateRecentChannels() {
253 while ((item = recentChannelsLayout->takeAt(1)) != 0) {
254 item->widget()->close();
260 QStringList keywords = settings.value(recentChannelsKey).toStringList();
261 recentChannelsLabel->setVisible(!keywords.isEmpty());
262 // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
264 foreach (QString keyword, keywords) {
265 QString link = keyword;
266 QString display = keyword;
267 int separator = keyword.indexOf('|');
268 if (separator > 0 && separator + 1 < keyword.length()) {
269 link = keyword.left(separator);
270 display = keyword.mid(separator+1);
272 QLabel *itemLabel = new QLabel("<a href=\"" + link
273 + "\" style=\"color:palette(text); text-decoration:none\">"
274 + display + "</a>", this);
275 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
276 itemLabel->setProperty("recentItem", true);
277 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
278 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
279 // Make links navigable with the keyboard too
280 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
282 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
283 recentChannelsLayout->addWidget(itemLabel);
288 void SearchView::watch() {
289 QString query = queryEdit->text();
293 void SearchView::textChanged(const QString &text) {
294 watchButton->setEnabled(!text.simplified().isEmpty());
297 void SearchView::watch(QString query) {
299 query = query.simplified();
301 // check for empty query
302 if (query.length() == 0) {
303 queryEdit->setFocus(Qt::OtherFocusReason);
307 SearchParams *searchParams = new SearchParams();
308 if (typeCombo->currentIndex() == 0)
309 searchParams->setKeywords(query);
311 // remove spaces from channel name
312 query = query.simplified();
313 searchParams->setAuthor(query);
314 searchParams->setSortBy(SearchParams::SortByNewest);
318 emit search(searchParams);
321 void SearchView::watchChannel(QString channel) {
323 channel = channel.simplified();
325 // check for empty query
326 if (channel.length() == 0) {
327 queryEdit->setFocus(Qt::OtherFocusReason);
331 // remove spaces from channel name
332 channel = channel.remove(" ");
334 SearchParams *searchParams = new SearchParams();
335 searchParams->setAuthor(channel);
336 searchParams->setSortBy(SearchParams::SortByNewest);
339 emit search(searchParams);
342 void SearchView::watchKeywords(QString query) {
344 query = query.simplified();
346 // check for empty query
347 if (query.length() == 0) {
348 queryEdit->setFocus(Qt::OtherFocusReason);
352 if (typeCombo->currentIndex() == 0) {
353 queryEdit->setText(query);
354 watchButton->setEnabled(true);
357 SearchParams *searchParams = new SearchParams();
358 searchParams->setKeywords(query);
361 emit search(searchParams);
364 void SearchView::paintEvent(QPaintEvent *event) {
365 QWidget::paintEvent(event);
366 #if defined(APP_MAC) | defined(APP_WIN)
368 if (window()->isActiveWindow()) {
369 brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
371 brush = palette().window();
373 QPainter painter(this);
374 painter.fillRect(0, 0, width(), height(), brush);
381 style()->drawPrimitive(QStyle::PE_Widget, &o, &p, this);
383 // PainterUtils::topShadow(this);
386 void SearchView::searchTypeChanged(int index) {
388 queryEdit->setSuggester(youtubeSuggest);
390 queryEdit->setSuggester(channelSuggest);
392 queryEdit->selectAll();
393 queryEdit->setFocus();