1 #include "SearchView.h"
4 static const QString recentKeywordsKey = "recentKeywords";
5 static const int PADDING = 40;
7 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
10 biggerFont.setPointSize(biggerFont.pointSize()*1.5);
13 smallerFont.setPointSize(smallerFont.pointSize()*.85);
14 smallerFont.setBold(true);
16 QVBoxLayout *mainLayout = new QVBoxLayout();
17 mainLayout->setMargin(0);
19 // hidden message widget
20 message = new QLabel(this);
22 mainLayout->addWidget(message);
24 QVBoxLayout *layout = new QVBoxLayout();
25 layout->setMargin(PADDING);
26 mainLayout->addLayout(layout);
28 QLabel *welcomeLabel =
30 tr("Welcome to <a href='%1'>%2</a>,").arg(Constants::WEBSITE, Constants::APP_NAME)
32 welcomeLabel->setOpenExternalLinks(true);
33 layout->addWidget(welcomeLabel);
35 layout->addSpacing(PADDING);
37 QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this);
38 tipLabel->setFont(biggerFont);
39 layout->addWidget(tipLabel);
41 QHBoxLayout *searchLayout = new QHBoxLayout();
43 queryEdit = new SearchLineEdit(this);
44 queryEdit->setFont(biggerFont);
45 queryEdit->setMinimumWidth(300);
46 queryEdit->sizeHint();
47 queryEdit->setFocus(Qt::OtherFocusReason);
48 // connect(queryEdit, SIGNAL(returnPressed()), this, SLOT(watch()));
49 connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&)));
50 connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
51 searchLayout->addWidget(queryEdit);
53 searchLayout->addSpacing(10);
55 watchButton = new QPushButton(tr("Watch"), this);
56 watchButton->setDefault(true);
57 watchButton->setEnabled(false);
58 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
59 searchLayout->addWidget(watchButton);
61 searchLayout->addStretch();
63 layout->addItem(searchLayout);
65 layout->addSpacing(PADDING);
67 QHBoxLayout *otherLayout = new QHBoxLayout();
69 recentKeywordsLayout = new QVBoxLayout();
70 recentKeywordsLayout->setAlignment(Qt::AlignTop);
71 recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
72 recentKeywordsLabel->hide();
73 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
74 recentKeywordsLabel->setFont(smallerFont);
75 recentKeywordsLayout->addWidget(recentKeywordsLabel);
77 otherLayout->addLayout(recentKeywordsLayout);
79 layout->addLayout(otherLayout);
83 setLayout(mainLayout);
85 // watchButton->setMinimumHeight(queryEdit->height());
91 void SearchView::paintEvent(QPaintEvent * /*event*/) {
93 QPainter painter(this);
96 QLinearGradient linearGrad(0, 0, 0, height());
98 linearGrad.setColorAt(0, palette.color(QPalette::Light));
99 linearGrad.setColorAt(1, palette.color(QPalette::Midlight));
100 painter.fillRect(0, 0, width(), height(), QBrush(linearGrad));
103 QPixmap watermark = QPixmap(":/images/app.png");
104 painter.setOpacity(.25);
105 painter.drawPixmap(width() - watermark.width() - PADDING,
106 height() - watermark.height() - PADDING,
112 void SearchView::updateRecentKeywords() {
116 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
117 item->widget()->close();
123 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
124 recentKeywordsLabel->setVisible(!keywords.isEmpty());
125 foreach (QString keyword, keywords) {
126 QLabel *itemLabel = new QLabel("<a href=\"" + keyword
127 + "\" style=\"color:palette(text); text-decoration:none\">"
128 + keyword + "</a>", this);
130 // Make links navigable with the keyboard
131 // this makes links nonclickable so it's disabled
132 // itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard);
133 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
134 recentKeywordsLayout->addWidget(itemLabel);
139 void SearchView::watch() {
140 QString query = queryEdit->text().trimmed();
144 void SearchView::textChanged(const QString &text) {
145 watchButton->setEnabled(!text.trimmed().isEmpty());
148 void SearchView::watch(QString query) {
150 // check for empty query
151 if (query.length() == 0) {
152 queryEdit->setFocus(Qt::OtherFocusReason);
158 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
159 keywords.removeAll(query);
160 keywords.prepend(query);
161 while (keywords.size() > 10)
162 keywords.removeLast();
163 settings.setValue(recentKeywordsKey, keywords);
169 void SearchView::checkForUpdate() {
170 static const QString updateCheckKey = "updateCheck";
174 uint unixTime = QDateTime::currentDateTime().toTime_t();
175 int lastCheck = settings.value(updateCheckKey).toInt();
176 int secondsSinceLastCheck = unixTime - lastCheck;
177 // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
178 if (secondsSinceLastCheck < 86400) return;
181 if (updateChecker) delete updateChecker;
182 updateChecker = new UpdateChecker();
183 connect(updateChecker, SIGNAL(newVersion(QString)),
184 this, SLOT(gotNewVersion(QString)));
185 updateChecker->checkForUpdate();
186 settings.setValue(updateCheckKey, unixTime);
190 void SearchView::gotNewVersion(QString version) {
192 tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
195 QString(Constants::WEBSITE).append("#download"),
198 message->setOpenExternalLinks(true);
199 message->setMargin(10);
200 message->setBackgroundRole(QPalette::ToolTipBase);
201 message->setForegroundRole(QPalette::ToolTipText);
202 message->setAutoFillBackground(true);
204 if (updateChecker) delete updateChecker;