1 #include "SearchView.h"
4 static const QString recentKeywordsKey = "recentKeywords";
5 static const int PADDING = 40;
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 QVBoxLayout *mainLayout = new QVBoxLayout();
23 mainLayout->setMargin(0);
25 // hidden message widget
26 message = new QLabel(this);
28 mainLayout->addWidget(message);
30 QVBoxLayout *layout = new QVBoxLayout();
31 layout->setMargin(PADDING);
32 mainLayout->addLayout(layout);
34 QLabel *welcomeLabel =
36 tr("Welcome to <a href='%1'>%2</a>,").arg(Constants::WEBSITE, Constants::APP_NAME)
38 welcomeLabel->setOpenExternalLinks(true);
39 layout->addWidget(welcomeLabel);
41 layout->addSpacing(PADDING);
43 QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this);
44 tipLabel->setFont(biggerFont);
45 layout->addWidget(tipLabel);
47 QHBoxLayout *searchLayout = new QHBoxLayout();
49 queryEdit = new SearchLineEdit(this);
50 queryEdit->setFont(biggerFont);
51 queryEdit->setMinimumWidth(300);
52 queryEdit->sizeHint();
53 queryEdit->setFocus(Qt::OtherFocusReason);
54 // connect(queryEdit, SIGNAL(returnPressed()), this, SLOT(watch()));
55 connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&)));
56 connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
57 searchLayout->addWidget(queryEdit);
59 searchLayout->addSpacing(10);
61 watchButton = new QPushButton(tr("Watch"), this);
62 watchButton->setDefault(true);
63 watchButton->setEnabled(false);
64 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
65 searchLayout->addWidget(watchButton);
67 searchLayout->addStretch();
69 layout->addItem(searchLayout);
71 layout->addSpacing(PADDING);
73 QHBoxLayout *otherLayout = new QHBoxLayout();
75 recentKeywordsLayout = new QVBoxLayout();
76 recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
77 recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
78 recentKeywordsLabel->hide();
79 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
80 recentKeywordsLabel->setFont(smallerFont);
81 recentKeywordsLayout->addWidget(recentKeywordsLabel);
83 otherLayout->addLayout(recentKeywordsLayout);
85 layout->addLayout(otherLayout);
89 setLayout(mainLayout);
95 void SearchView::paintEvent(QPaintEvent * /*event*/) {
97 QPainter painter(this);
100 QLinearGradient linearGrad(0, 0, 0, height());
102 linearGrad.setColorAt(0, palette.color(QPalette::Light));
103 linearGrad.setColorAt(1, palette.color(QPalette::Midlight));
104 painter.fillRect(0, 0, width(), height(), QBrush(linearGrad));
107 QPixmap watermark = QPixmap(":/images/app.png");
108 painter.setOpacity(.25);
109 painter.drawPixmap(width() - watermark.width() - PADDING,
110 height() - watermark.height() - PADDING,
116 void SearchView::updateRecentKeywords() {
120 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
121 item->widget()->close();
127 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
128 recentKeywordsLabel->setVisible(!keywords.isEmpty());
129 foreach (QString keyword, keywords) {
130 QLabel *itemLabel = new QLabel("<a href=\"" + keyword
131 + "\" style=\"color:palette(text); text-decoration:none\">"
132 + keyword + "</a>", this);
134 itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
135 // Make links navigable with the keyboard too
136 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
138 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
139 recentKeywordsLayout->addWidget(itemLabel);
144 void SearchView::watch() {
145 QString query = queryEdit->text().simplified();
149 void SearchView::textChanged(const QString &text) {
150 watchButton->setEnabled(!text.simplified().isEmpty());
153 void SearchView::watch(QString query) {
155 // check for empty query
156 if (query.length() == 0) {
157 queryEdit->setFocus(Qt::OtherFocusReason);
161 queryEdit->preventSuggest();
167 void SearchView::checkForUpdate() {
168 static const QString updateCheckKey = "updateCheck";
172 uint unixTime = QDateTime::currentDateTime().toTime_t();
173 int lastCheck = settings.value(updateCheckKey).toInt();
174 int secondsSinceLastCheck = unixTime - lastCheck;
175 // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
176 if (secondsSinceLastCheck < 86400) return;
179 if (updateChecker) delete updateChecker;
180 updateChecker = new UpdateChecker();
181 connect(updateChecker, SIGNAL(newVersion(QString)),
182 this, SLOT(gotNewVersion(QString)));
183 updateChecker->checkForUpdate();
184 settings.setValue(updateCheckKey, unixTime);
188 void SearchView::gotNewVersion(QString version) {
190 tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
193 QString(Constants::WEBSITE).append("#download"),
196 message->setOpenExternalLinks(true);
197 message->setMargin(10);
198 message->setBackgroundRole(QPalette::ToolTipBase);
199 message->setForegroundRole(QPalette::ToolTipText);
200 message->setAutoFillBackground(true);
202 if (updateChecker) delete updateChecker;