1 #include "SearchView.h"
4 #include "searchparams.h"
5 #include "youtubesuggest.h"
6 #include "channelsuggest.h"
8 #include "searchlineedit_mac.h"
10 #include "searchlineedit.h"
16 #include "activation.h"
18 #include "MainWindow.h"
21 QMap<QString, QAction*>* globalActions();
24 static const QString recentKeywordsKey = "recentKeywords";
25 static const QString recentChannelsKey = "recentChannels";
26 static const int PADDING = 30;
28 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
30 QFont biggerFont = FontUtils::big();
31 QFont smallerFont = FontUtils::smallBold();
33 #if defined(APP_MAC) | defined(APP_WIN)
34 // speedup painting since we'll paint the whole background
35 // by ourselves anyway in paintEvent()
36 setAttribute(Qt::WA_OpaquePaintEvent);
39 QBoxLayout *mainLayout = new QVBoxLayout();
40 mainLayout->setMargin(0);
41 mainLayout->setSpacing(0);
43 // hidden message widget
44 message = new QLabel(this);
46 mainLayout->addWidget(message);
49 if (!Activation::instance().isActivated())
50 mainLayout->addWidget(Extra::buyButton(tr("Get the full version")), 0, Qt::AlignCenter);
53 mainLayout->addStretch();
54 mainLayout->addSpacing(PADDING);
56 QBoxLayout *hLayout = new QHBoxLayout();
57 hLayout->setAlignment(Qt::AlignCenter);
58 mainLayout->addLayout(hLayout);
60 QLabel *logo = new QLabel(this);
61 logo->setPixmap(QPixmap(":/images/app.png"));
62 hLayout->addWidget(logo, 0, Qt::AlignTop);
63 hLayout->addSpacing(PADDING);
65 QVBoxLayout *layout = new QVBoxLayout();
66 layout->setAlignment(Qt::AlignCenter);
67 hLayout->addLayout(layout);
69 QLabel *welcomeLabel =
70 new QLabel("<h1 style='font-weight:normal'>" +
71 tr("Welcome to <a href='%1'>%2</a>,")
72 // .replace("<a ", "<a style='color:palette(text)'")
73 .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
74 .arg(Constants::WEBSITE, Constants::NAME)
76 welcomeLabel->setOpenExternalLinks(true);
77 layout->addWidget(welcomeLabel);
79 layout->addSpacing(PADDING / 2);
81 QBoxLayout *tipLayout = new QHBoxLayout();
82 tipLayout->setSpacing(10);
84 //: "Enter", as in "type". The whole phrase says: "Enter a keyword to start watching videos"
85 QLabel *tipLabel = new QLabel(tr("Enter"), this);
86 tipLabel->setFont(biggerFont);
87 tipLayout->addWidget(tipLabel);
89 typeCombo = new QComboBox(this);
90 typeCombo->addItem(tr("a keyword"));
91 typeCombo->addItem(tr("a channel"));
92 typeCombo->setFont(biggerFont);
93 connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
94 tipLayout->addWidget(typeCombo);
96 tipLabel = new QLabel(tr("to start watching videos."), this);
97 tipLabel->setFont(biggerFont);
98 tipLayout->addWidget(tipLabel);
99 layout->addLayout(tipLayout);
101 layout->addSpacing(PADDING / 2);
103 QHBoxLayout *searchLayout = new QHBoxLayout();
104 searchLayout->setAlignment(Qt::AlignVCenter);
106 queryEdit = new SearchLineEdit(this);
107 queryEdit->setFont(biggerFont);
108 queryEdit->setMinimumWidth(queryEdit->fontInfo().pixelSize()*15);
109 connect(queryEdit, SIGNAL(search(const QString&)), SLOT(watch(const QString&)));
110 connect(queryEdit, SIGNAL(textChanged(const QString &)), SLOT(textChanged(const QString &)));
111 connect(queryEdit, SIGNAL(suggestionAccepted(const QString&)), SLOT(watch(const QString&)));
113 youtubeSuggest = new YouTubeSuggest(this);
114 channelSuggest = new ChannelSuggest(this);
115 searchTypeChanged(0);
117 searchLayout->addWidget(queryEdit);
118 searchLayout->addSpacing(10);
120 watchButton = new QPushButton(tr("Watch"), this);
121 watchButton->setDefault(true);
122 watchButton->setEnabled(false);
123 watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
124 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
125 searchLayout->addWidget(watchButton);
127 layout->addItem(searchLayout);
129 layout->addSpacing(PADDING / 2);
131 QHBoxLayout *otherLayout = new QHBoxLayout();
132 otherLayout->setMargin(0);
133 otherLayout->setSpacing(10);
135 recentKeywordsLayout = new QVBoxLayout();
136 recentKeywordsLayout->setSpacing(5);
137 recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
138 recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
139 recentKeywordsLabel->setProperty("recentHeader", true);
140 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
141 recentKeywordsLabel->hide();
142 recentKeywordsLabel->setFont(smallerFont);
143 recentKeywordsLayout->addWidget(recentKeywordsLabel);
145 otherLayout->addLayout(recentKeywordsLayout);
148 recentChannelsLayout = new QVBoxLayout();
149 recentChannelsLayout->setSpacing(5);
150 recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
151 recentChannelsLabel = new QLabel(tr("Recent channels").toUpper(), this);
152 recentChannelsLabel->setProperty("recentHeader", true);
153 recentChannelsLabel->setForegroundRole(QPalette::Dark);
154 recentChannelsLabel->hide();
155 recentChannelsLabel->setFont(smallerFont);
156 recentChannelsLayout->addWidget(recentChannelsLabel);
158 otherLayout->addLayout(recentChannelsLayout);
160 layout->addLayout(otherLayout);
162 mainLayout->addSpacing(PADDING);
163 mainLayout->addStretch();
165 setLayout(mainLayout);
169 void SearchView::appear() {
170 updateRecentKeywords();
171 updateRecentChannels();
172 queryEdit->selectAll();
173 queryEdit->enableSuggest();
174 QTimer::singleShot(0, queryEdit, SLOT(setFocus()));
177 void SearchView::updateRecentKeywords() {
181 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
182 item->widget()->close();
188 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
189 recentKeywordsLabel->setVisible(!keywords.isEmpty());
190 The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
192 foreach (QString keyword, keywords) {
193 QString link = keyword;
194 QString display = keyword;
195 if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
196 int separator = keyword.indexOf("|");
197 if (separator > 0 && separator + 1 < keyword.length()) {
198 link = keyword.left(separator);
199 display = keyword.mid(separator+1);
202 bool needStatusTip = false;
203 if (display.length() > 24) {
204 display.truncate(24);
205 display.append("...");
206 needStatusTip = true;
208 QLabel *itemLabel = new QLabel("<a href=\"" + link
209 + "\" style=\"color:palette(text); text-decoration:none\">"
210 + display + "</a>", this);
211 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
212 itemLabel->setProperty("recentItem", true);
213 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
214 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
215 // Make links navigable with the keyboard too
216 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
218 itemLabel->setStatusTip(link);
219 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
220 recentKeywordsLayout->addWidget(itemLabel);
225 void SearchView::updateRecentChannels() {
229 while ((item = recentChannelsLayout->takeAt(1)) != 0) {
230 item->widget()->close();
236 QStringList keywords = settings.value(recentChannelsKey).toStringList();
237 recentChannelsLabel->setVisible(!keywords.isEmpty());
238 // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
240 foreach (QString keyword, keywords) {
241 QString link = keyword;
242 QString display = keyword;
243 if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
244 int separator = keyword.indexOf("|");
245 if (separator > 0 && separator + 1 < keyword.length()) {
246 link = keyword.left(separator);
247 display = keyword.mid(separator+1);
250 QLabel *itemLabel = new QLabel("<a href=\"" + link
251 + "\" style=\"color:palette(text); text-decoration:none\">"
252 + display + "</a>", this);
253 itemLabel->setAttribute(Qt::WA_DeleteOnClose);
254 itemLabel->setProperty("recentItem", true);
255 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
256 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
257 // Make links navigable with the keyboard too
258 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
260 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
261 recentChannelsLayout->addWidget(itemLabel);
266 void SearchView::watch() {
267 QString query = queryEdit->text();
271 void SearchView::textChanged(const QString &text) {
272 watchButton->setEnabled(!text.simplified().isEmpty());
275 void SearchView::watch(QString query) {
277 query = query.simplified();
279 // check for empty query
280 if (query.length() == 0) {
281 queryEdit->setFocus(Qt::OtherFocusReason);
285 SearchParams *searchParams = new SearchParams();
286 if (typeCombo->currentIndex() == 0)
287 searchParams->setKeywords(query);
289 // remove spaces from channel name
290 query = query.replace(" ", "");
291 searchParams->setAuthor(query);
292 searchParams->setSortBy(SearchParams::SortByNewest);
296 emit search(searchParams);
299 void SearchView::watchChannel(QString channel) {
301 channel = channel.simplified();
303 // check for empty query
304 if (channel.length() == 0) {
305 queryEdit->setFocus(Qt::OtherFocusReason);
309 // remove spaces from channel name
310 channel = channel.remove(" ");
312 SearchParams *searchParams = new SearchParams();
313 searchParams->setAuthor(channel);
314 searchParams->setSortBy(SearchParams::SortByNewest);
317 emit search(searchParams);
320 void SearchView::watchKeywords(QString query) {
322 query = query.simplified();
324 // check for empty query
325 if (query.length() == 0) {
326 queryEdit->setFocus(Qt::OtherFocusReason);
330 if (typeCombo->currentIndex() == 0) {
331 queryEdit->setText(query);
332 watchButton->setEnabled(true);
335 SearchParams *searchParams = new SearchParams();
336 searchParams->setKeywords(query);
339 emit search(searchParams);
342 void SearchView::paintEvent(QPaintEvent * /*event*/) {
343 #if defined(APP_MAC) | defined(APP_WIN)
345 if (window()->isActiveWindow()) {
346 brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
348 brush = palette().window();
350 QPainter painter(this);
351 painter.fillRect(0, 0, width(), height(), brush);
355 void SearchView::searchTypeChanged(int index) {
357 queryEdit->setSuggester(youtubeSuggest);
359 queryEdit->setSuggester(channelSuggest);
361 queryEdit->selectAll();
362 queryEdit->setFocus();