]> git.sur5r.net Git - minitube/blob - src/SearchView.cpp
Imported Upstream version 1.6
[minitube] / src / SearchView.cpp
1 #include "SearchView.h"
2 #include "constants.h"
3 #include "fontutils.h"
4 #include "searchparams.h"
5 #include "youtubesuggest.h"
6 #include "channelsuggest.h"
7
8 namespace The {
9     QMap<QString, QAction*>* globalActions();
10 }
11
12 static const QString recentKeywordsKey = "recentKeywords";
13 static const QString recentChannelsKey = "recentChannels";
14 static const int PADDING = 30;
15
16 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
17
18     QFont biggerFont = FontUtils::big();
19     QFont smallerFont = FontUtils::smallBold();
20
21 #if defined(APP_MAC) | defined(APP_WIN)
22     // speedup painting since we'll paint the whole background
23     // by ourselves anyway in paintEvent()
24     setAttribute(Qt::WA_OpaquePaintEvent);
25 #endif
26
27     QBoxLayout *mainLayout = new QVBoxLayout();
28     mainLayout->setMargin(0);
29     mainLayout->setSpacing(0);
30
31     // hidden message widget
32     message = new QLabel(this);
33     message->hide();
34     mainLayout->addWidget(message);
35
36 #ifdef APP_DEMO
37     QLabel *buy = new QLabel(this);
38     buy->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
39     buy->setText(QString("<a style='color:palette(text);text-decoration:none' href='%1'>%2</a>").arg(
40             QString(Constants::WEBSITE) + "#download",
41             tr("Get the full version").toUpper()
42             ));
43     buy->setOpenExternalLinks(true);
44     buy->setMargin(7);
45     buy->setAlignment(Qt::AlignRight);
46     buy->setStyleSheet("QLabel {"
47                        "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #CCD6E0, stop: 1 #ADBCCC);"
48                        "border-bottom-left-radius: 8px;"
49                        "border-bottom-right-radius: 8px;"
50                        "font-size: 10px;"
51                        "margin-right: 50px;"
52                        "}");
53     mainLayout->addWidget(buy, 0, Qt::AlignRight);
54 #endif
55
56     mainLayout->addStretch();
57     mainLayout->addSpacing(PADDING);
58
59     QBoxLayout *hLayout = new QHBoxLayout();
60     hLayout->setAlignment(Qt::AlignCenter);
61     mainLayout->addLayout(hLayout);
62
63     QLabel *logo = new QLabel(this);
64     logo->setPixmap(QPixmap(":/images/app.png"));
65     hLayout->addWidget(logo, 0, Qt::AlignTop);
66     hLayout->addSpacing(PADDING);
67
68     QVBoxLayout *layout = new QVBoxLayout();
69     layout->setAlignment(Qt::AlignCenter);
70     hLayout->addLayout(layout);
71
72     QLabel *welcomeLabel =
73             new QLabel("<h1 style='font-weight:normal'>" +
74                        tr("Welcome to <a href='%1'>%2</a>,")
75                        // .replace("<a ", "<a style='color:palette(text)'")
76                        .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
77                        .arg(Constants::WEBSITE, Constants::NAME)
78                        + "</h1>", this);
79     welcomeLabel->setOpenExternalLinks(true);
80     layout->addWidget(welcomeLabel);
81
82     layout->addSpacing(PADDING / 2);
83
84     QBoxLayout *tipLayout = new QHBoxLayout();
85     tipLayout->setSpacing(10);
86
87     //: "Enter", as in "type". The whole frase says: "Enter a keyword to start watching videos"
88     QLabel *tipLabel = new QLabel(tr("Enter"), this);
89     tipLabel->setFont(biggerFont);
90     tipLayout->addWidget(tipLabel);
91
92     typeCombo = new QComboBox(this);
93     typeCombo->addItem(tr("a keyword"));
94     typeCombo->addItem(tr("a channel"));
95     typeCombo->setFont(biggerFont);
96     connect(typeCombo, SIGNAL(currentIndexChanged(int)), SLOT(searchTypeChanged(int)));
97     tipLayout->addWidget(typeCombo);
98
99     tipLabel = new QLabel(tr("to start watching videos."), this);
100     tipLabel->setFont(biggerFont);
101     tipLayout->addWidget(tipLabel);
102     layout->addLayout(tipLayout);
103
104     layout->addSpacing(PADDING / 2);
105
106     QHBoxLayout *searchLayout = new QHBoxLayout();
107     searchLayout->setAlignment(Qt::AlignVCenter);
108
109     queryEdit = new SearchLineEdit(this);
110     queryEdit->setFont(biggerFont);
111     queryEdit->setMinimumWidth(queryEdit->fontInfo().pixelSize()*15);
112     queryEdit->sizeHint();
113     queryEdit->setFocus(Qt::OtherFocusReason);
114     connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&)));
115     connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
116
117     youtubeSuggest = new YouTubeSuggest(this);
118     channelSuggest = new ChannelSuggest(this);
119     searchTypeChanged(0);
120
121     searchLayout->addWidget(queryEdit);
122     searchLayout->addSpacing(10);
123
124     watchButton = new QPushButton(tr("Watch"), this);
125     watchButton->setDefault(true);
126     watchButton->setEnabled(false);
127     watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
128     connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
129     searchLayout->addWidget(watchButton);
130
131     layout->addItem(searchLayout);
132
133     layout->addSpacing(PADDING / 2);
134
135     QHBoxLayout *otherLayout = new QHBoxLayout();
136     otherLayout->setMargin(0);
137     otherLayout->setSpacing(10);
138
139     recentKeywordsLayout = new QVBoxLayout();
140     recentKeywordsLayout->setSpacing(5);
141     recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
142     recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
143 #if defined(APP_MAC) | defined(APP_WIN)
144     QPalette palette = recentKeywordsLabel->palette();
145     palette.setColor(QPalette::WindowText, QColor(0x65, 0x71, 0x80));
146     recentKeywordsLabel->setPalette(palette);
147 #else
148     recentKeywordsLabel->setForegroundRole(QPalette::Dark);
149 #endif
150     recentKeywordsLabel->hide();
151     recentKeywordsLabel->setFont(smallerFont);
152     recentKeywordsLayout->addWidget(recentKeywordsLabel);
153
154     otherLayout->addLayout(recentKeywordsLayout);
155
156     // recent channels
157     recentChannelsLayout = new QVBoxLayout();
158     recentChannelsLayout->setSpacing(5);
159     recentChannelsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
160     recentChannelsLabel = new QLabel(tr("Recent channels").toUpper(), this);
161 #if defined(APP_MAC) | defined(APP_WIN)
162     palette = recentChannelsLabel->palette();
163     palette.setColor(QPalette::WindowText, QColor(0x65, 0x71, 0x80));
164     recentChannelsLabel->setPalette(palette);
165 #else
166     recentChannelsLabel->setForegroundRole(QPalette::Dark);
167 #endif
168     recentChannelsLabel->hide();
169     recentChannelsLabel->setFont(smallerFont);
170     recentChannelsLayout->addWidget(recentChannelsLabel);
171
172     otherLayout->addLayout(recentChannelsLayout);
173
174     layout->addLayout(otherLayout);
175
176     mainLayout->addSpacing(PADDING);
177     mainLayout->addStretch();
178
179     setLayout(mainLayout);
180
181 }
182
183 void SearchView::updateRecentKeywords() {
184
185     // cleanup
186     QLayoutItem *item;
187     while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
188         item->widget()->close();
189         delete item;
190     }
191
192     // load
193     QSettings settings;
194     QStringList keywords = settings.value(recentKeywordsKey).toStringList();
195     recentKeywordsLabel->setVisible(!keywords.isEmpty());
196     The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
197
198     foreach (QString keyword, keywords) {
199         QString link = keyword;
200         QString display = keyword;
201         if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
202             int separator = keyword.indexOf("|");
203             if (separator > 0 && separator + 1 < keyword.length()) {
204                 link = keyword.left(separator);
205                 display = keyword.mid(separator+1);
206             }
207         }
208         bool needStatusTip = false;
209         if (display.length() > 24) {
210             display.truncate(24);
211             display.append("...");
212             needStatusTip = true;
213         }
214         QLabel *itemLabel = new QLabel("<a href=\"" + link
215                                        + "\" style=\"color:palette(text); text-decoration:none\">"
216                                        + display + "</a>", this);
217         itemLabel->setAttribute(Qt::WA_DeleteOnClose);
218         itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
219         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
220         // Make links navigable with the keyboard too
221         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
222         if (needStatusTip)
223             itemLabel->setStatusTip(link);
224         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchKeywords(QString)));
225         recentKeywordsLayout->addWidget(itemLabel);
226     }
227
228 }
229
230 void SearchView::updateRecentChannels() {
231
232     // cleanup
233     QLayoutItem *item;
234     while ((item = recentChannelsLayout->takeAt(1)) != 0) {
235         item->widget()->close();
236         delete item;
237     }
238
239     // load
240     QSettings settings;
241     QStringList keywords = settings.value(recentChannelsKey).toStringList();
242     recentChannelsLabel->setVisible(!keywords.isEmpty());
243     // TODO The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
244
245     foreach (QString keyword, keywords) {
246         QString link = keyword;
247         QString display = keyword;
248         if (keyword.startsWith("http://") || keyword.startsWith("https://")) {
249             int separator = keyword.indexOf("|");
250             if (separator > 0 && separator + 1 < keyword.length()) {
251                 link = keyword.left(separator);
252                 display = keyword.mid(separator+1);
253             }
254         }
255         QLabel *itemLabel = new QLabel("<a href=\"" + link
256                                        + "\" style=\"color:palette(text); text-decoration:none\">"
257                                        + display + "</a>", this);
258         itemLabel->setAttribute(Qt::WA_DeleteOnClose);
259         itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
260         // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
261         // Make links navigable with the keyboard too
262         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
263
264         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watchChannel(QString)));
265         recentChannelsLayout->addWidget(itemLabel);
266     }
267
268 }
269
270 void SearchView::watch() {
271     QString query = queryEdit->text();
272     watch(query);
273 }
274
275 void SearchView::textChanged(const QString &text) {
276     watchButton->setEnabled(!text.simplified().isEmpty());
277 }
278
279 void SearchView::watch(QString query) {
280
281     query = query.simplified();
282
283     // check for empty query
284     if (query.length() == 0) {
285         queryEdit->setFocus(Qt::OtherFocusReason);
286         return;
287     }
288
289     SearchParams *searchParams = new SearchParams();
290     if (typeCombo->currentIndex() == 0)
291         searchParams->setKeywords(query);
292     else {
293         // remove spaces from channel name
294         query = query.replace(" ", "");
295         searchParams->setAuthor(query);
296         searchParams->setSortBy(SearchParams::SortByNewest);
297     }
298
299     // go!
300     emit search(searchParams);
301 }
302
303 void SearchView::watchChannel(QString channel) {
304
305     channel = channel.simplified();
306
307     // check for empty query
308     if (channel.length() == 0) {
309         queryEdit->setFocus(Qt::OtherFocusReason);
310         return;
311     }
312
313     // remove spaces from channel name
314     channel = channel.replace(" ", "");
315
316     SearchParams *searchParams = new SearchParams();
317     searchParams->setAuthor(channel);
318     searchParams->setSortBy(SearchParams::SortByNewest);
319
320     // go!
321     emit search(searchParams);
322 }
323
324 void SearchView::watchKeywords(QString query) {
325
326     query = query.simplified();
327
328     // check for empty query
329     if (query.length() == 0) {
330         queryEdit->setFocus(Qt::OtherFocusReason);
331         return;
332     }
333
334     SearchParams *searchParams = new SearchParams();
335     searchParams->setKeywords(query);
336
337     // go!
338     emit search(searchParams);
339 }
340
341 void SearchView::paintEvent(QPaintEvent * /*event*/) {
342 #if defined(APP_MAC) | defined(APP_WIN)
343     QBrush brush;
344     if (window()->isActiveWindow()) {
345         brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
346     } else {
347         brush = palette().window();
348     }
349     QPainter painter(this);
350     painter.fillRect(0, 0, width(), height(), brush);
351 #endif
352 }
353
354 void SearchView::searchTypeChanged(int index) {
355     if (index == 0) {
356         queryEdit->setSuggester(youtubeSuggest);
357     } else {
358         queryEdit->setSuggester(channelSuggest);
359     }
360     queryEdit->setFocus();
361 }