1 #include "SearchView.h"
6 QMap<QString, QAction*>* globalActions();
9 static const QString recentKeywordsKey = "recentKeywords";
10 static const int PADDING = 30;
12 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
14 QFont biggerFont = FontUtils::big();
15 QFont smallerFont = FontUtils::smallBold();
18 // speedup painting since we'll paint the whole background
19 // by ourselves anyway in paintEvent()
20 setAttribute(Qt::WA_OpaquePaintEvent);
23 QBoxLayout *mainLayout = new QVBoxLayout();
24 mainLayout->setMargin(0);
25 mainLayout->setSpacing(0);
27 // hidden message widget
28 message = new QLabel(this);
30 mainLayout->addWidget(message);
32 mainLayout->addStretch();
33 mainLayout->addSpacing(PADDING);
35 QBoxLayout *hLayout = new QHBoxLayout();
36 hLayout->setAlignment(Qt::AlignCenter);
37 mainLayout->addLayout(hLayout);
39 QLabel *logo = new QLabel(this);
40 logo->setPixmap(QPixmap(":/images/app.png"));
41 hLayout->addWidget(logo, 0, Qt::AlignTop);
42 hLayout->addSpacing(PADDING);
44 QVBoxLayout *layout = new QVBoxLayout();
45 layout->setAlignment(Qt::AlignCenter);
46 hLayout->addLayout(layout);
48 QLabel *welcomeLabel =
49 new QLabel("<h1 style='font-weight:normal'>" +
50 tr("Welcome to <a href='%1'>%2</a>,")
51 // .replace("<a ", "<a style='color:palette(text)'")
52 .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
53 .arg(Constants::WEBSITE, Constants::APP_NAME)
55 welcomeLabel->setOpenExternalLinks(true);
56 layout->addWidget(welcomeLabel);
58 layout->addSpacing(PADDING / 2);
60 QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this);
61 tipLabel->setFont(biggerFont);
62 layout->addWidget(tipLabel);
64 layout->addSpacing(PADDING / 2);
66 QHBoxLayout *searchLayout = new QHBoxLayout();
67 searchLayout->setAlignment(Qt::AlignVCenter);
69 queryEdit = new SearchLineEdit(this);
70 queryEdit->setFont(biggerFont);
71 queryEdit->setMinimumWidth(queryEdit->fontInfo().pixelSize()*15);
72 queryEdit->sizeHint();
73 queryEdit->setFocus(Qt::OtherFocusReason);
74 connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&)));
75 connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
76 searchLayout->addWidget(queryEdit);
78 searchLayout->addSpacing(10);
80 watchButton = new QPushButton(tr("Watch"), this);
81 watchButton->setDefault(true);
82 watchButton->setEnabled(false);
83 watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
84 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
85 searchLayout->addWidget(watchButton);
87 layout->addItem(searchLayout);
89 layout->addSpacing(PADDING / 2);
91 QHBoxLayout *otherLayout = new QHBoxLayout();
92 otherLayout->setMargin(0);
94 recentKeywordsLayout = new QVBoxLayout();
95 recentKeywordsLayout->setSpacing(5);
96 recentKeywordsLayout->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
97 recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
99 QPalette palette = recentKeywordsLabel->palette();
100 palette.setColor(QPalette::WindowText, QColor(0x65, 0x71, 0x80));
101 recentKeywordsLabel->setPalette(palette);
103 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
105 recentKeywordsLabel->hide();
106 recentKeywordsLabel->setFont(smallerFont);
107 recentKeywordsLayout->addWidget(recentKeywordsLabel);
109 otherLayout->addLayout(recentKeywordsLayout);
111 layout->addLayout(otherLayout);
113 mainLayout->addSpacing(PADDING);
114 mainLayout->addStretch();
116 setLayout(mainLayout);
122 void SearchView::updateRecentKeywords() {
126 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
127 item->widget()->close();
133 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
134 recentKeywordsLabel->setVisible(!keywords.isEmpty());
135 The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
137 foreach (QString keyword, keywords) {
138 QString link = keyword;
139 QString display = keyword;
140 if (keyword.startsWith("http://")) {
141 int separator = keyword.indexOf("|");
142 if (separator > 0 && separator + 1 < keyword.length()) {
143 link = keyword.left(separator);
144 display = keyword.mid(separator+1);
147 QLabel *itemLabel = new QLabel("<a href=\"" + link
148 + "\" style=\"color:palette(text); text-decoration:none\">"
149 + display + "</a>", this);
151 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
152 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
153 // Make links navigable with the keyboard too
154 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
156 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
157 recentKeywordsLayout->addWidget(itemLabel);
164 void SearchView::watch() {
165 QString query = queryEdit->text().simplified();
169 void SearchView::textChanged(const QString &text) {
170 watchButton->setEnabled(!text.simplified().isEmpty());
173 void SearchView::watch(QString query) {
175 // check for empty query
176 if (query.length() == 0) {
177 queryEdit->setFocus(Qt::OtherFocusReason);
185 void SearchView::checkForUpdate() {
186 static const QString updateCheckKey = "updateCheck";
190 uint unixTime = QDateTime::currentDateTime().toTime_t();
191 int lastCheck = settings.value(updateCheckKey).toInt();
192 int secondsSinceLastCheck = unixTime - lastCheck;
193 // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
194 if (secondsSinceLastCheck < 86400) return;
197 if (updateChecker) delete updateChecker;
198 updateChecker = new UpdateChecker();
199 connect(updateChecker, SIGNAL(newVersion(QString)),
200 this, SLOT(gotNewVersion(QString)));
201 updateChecker->checkForUpdate();
202 settings.setValue(updateCheckKey, unixTime);
206 void SearchView::gotNewVersion(QString version) {
208 tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
209 .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
212 QString(Constants::WEBSITE).append("#download"),
215 message->setOpenExternalLinks(true);
216 message->setMargin(10);
217 message->setAlignment(Qt::AlignCenter);
218 // message->setBackgroundRole(QPalette::ToolTipBase);
219 // message->setForegroundRole(QPalette::ToolTipText);
220 // message->setAutoFillBackground(true);
221 message->setStyleSheet("QLabel { border-bottom: 1px solid palette(mid); }");
223 if (updateChecker) delete updateChecker;
226 void SearchView::paintEvent(QPaintEvent * /*event*/) {
229 if (window()->isActiveWindow()) {
230 brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
232 brush = palette().window();
234 QPainter painter(this);
235 painter.fillRect(0, 0, width(), height(), brush);