1 #include "SearchView.h"
4 static const QString recentKeywordsKey = "recentKeywords";
5 static const int PADDING = 30;
7 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
10 // speedup painting since we'll paint the whole background
11 // by ourselves anyway in paintEvent()
12 setAttribute(Qt::WA_OpaquePaintEvent);
16 biggerFont.setPointSize(biggerFont.pointSize()*1.5);
19 smallerFont.setPointSize(smallerFont.pointSize()*.85);
20 smallerFont.setBold(true);
22 QBoxLayout *mainLayout = new QHBoxLayout();
23 mainLayout->setAlignment(Qt::AlignCenter);
24 mainLayout->setMargin(PADDING);
26 QLabel *logo = new QLabel(this);
27 logo->setPixmap(QPixmap(":/images/app.png"));
28 mainLayout->addWidget(logo, 0, Qt::AlignTop);
29 mainLayout->addSpacing(PADDING);
31 // hidden message widget
32 message = new QLabel(this);
34 mainLayout->addWidget(message);
36 QVBoxLayout *layout = new QVBoxLayout();
37 layout->setAlignment(Qt::AlignCenter);
38 mainLayout->addLayout(layout);
40 QLabel *welcomeLabel =
42 tr("Welcome to <a href='%1'>%2</a>,")
43 .replace("<a ", "<a style='color:palette(text)'")
44 .arg(Constants::WEBSITE, Constants::APP_NAME)
46 welcomeLabel->setOpenExternalLinks(true);
47 layout->addWidget(welcomeLabel);
49 layout->addSpacing(PADDING);
51 QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this);
52 tipLabel->setFont(biggerFont);
53 layout->addWidget(tipLabel);
55 QHBoxLayout *searchLayout = new QHBoxLayout();
56 searchLayout->setAlignment(Qt::AlignCenter);
58 queryEdit = new SearchLineEdit(this);
59 queryEdit->setFont(biggerFont);
60 queryEdit->setMinimumWidth(300);
61 queryEdit->sizeHint();
62 queryEdit->setFocus(Qt::OtherFocusReason);
63 // connect(queryEdit, SIGNAL(returnPressed()), this, SLOT(watch()));
64 connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&)));
65 connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
66 searchLayout->addWidget(queryEdit);
68 searchLayout->addSpacing(10);
70 watchButton = new QPushButton(tr("Watch"), this);
71 watchButton->setDefault(true);
72 watchButton->setEnabled(false);
73 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
74 searchLayout->addWidget(watchButton);
77 layout->addItem(searchLayout);
79 layout->addSpacing(PADDING);
81 QHBoxLayout *otherLayout = new QHBoxLayout();
83 recentKeywordsLayout = new QVBoxLayout();
84 recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
85 recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
86 recentKeywordsLabel->hide();
87 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
88 recentKeywordsLabel->setFont(smallerFont);
89 recentKeywordsLayout->addWidget(recentKeywordsLabel);
91 otherLayout->addLayout(recentKeywordsLayout);
93 layout->addLayout(otherLayout);
95 setLayout(mainLayout);
101 void SearchView::paintEvent(QPaintEvent * /*event*/) {
103 QPainter painter(this);
106 QLinearGradient linearGrad(0, 0, 0, height());
108 linearGrad.setColorAt(0, palette.color(QPalette::Light));
109 linearGrad.setColorAt(1, palette.color(QPalette::Midlight));
110 painter.fillRect(0, 0, width(), height(), QBrush(linearGrad));
114 QPixmap watermark = QPixmap(":/images/app.png");
115 painter.setOpacity(.25);
116 painter.drawPixmap(width() - watermark.width() - PADDING,
117 height() - watermark.height() - PADDING,
124 void SearchView::updateRecentKeywords() {
128 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
129 item->widget()->close();
135 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
136 recentKeywordsLabel->setVisible(!keywords.isEmpty());
137 foreach (QString keyword, keywords) {
138 QLabel *itemLabel = new QLabel("<a href=\"" + keyword
139 + "\" style=\"color:palette(text); text-decoration:none\">"
140 + keyword + "</a>", this);
142 itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
143 // Make links navigable with the keyboard too
144 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
146 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
147 recentKeywordsLayout->addWidget(itemLabel);
152 void SearchView::watch() {
153 QString query = queryEdit->text().simplified();
157 void SearchView::textChanged(const QString &text) {
158 watchButton->setEnabled(!text.simplified().isEmpty());
161 void SearchView::watch(QString query) {
163 // check for empty query
164 if (query.length() == 0) {
165 queryEdit->setFocus(Qt::OtherFocusReason);
169 queryEdit->preventSuggest();
175 void SearchView::checkForUpdate() {
176 static const QString updateCheckKey = "updateCheck";
180 uint unixTime = QDateTime::currentDateTime().toTime_t();
181 int lastCheck = settings.value(updateCheckKey).toInt();
182 int secondsSinceLastCheck = unixTime - lastCheck;
183 // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
184 if (secondsSinceLastCheck < 86400) return;
187 if (updateChecker) delete updateChecker;
188 updateChecker = new UpdateChecker();
189 connect(updateChecker, SIGNAL(newVersion(QString)),
190 this, SLOT(gotNewVersion(QString)));
191 updateChecker->checkForUpdate();
192 settings.setValue(updateCheckKey, unixTime);
196 void SearchView::gotNewVersion(QString version) {
198 tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
201 QString(Constants::WEBSITE).append("#download"),
204 message->setOpenExternalLinks(true);
205 message->setMargin(10);
206 message->setBackgroundRole(QPalette::ToolTipBase);
207 message->setForegroundRole(QPalette::ToolTipText);
208 message->setAutoFillBackground(true);
210 if (updateChecker) delete updateChecker;