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 | Qt::AlignLeft);
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);
89 void SearchView::paintEvent(QPaintEvent * /*event*/) {
91 QPainter painter(this);
94 QLinearGradient linearGrad(0, 0, 0, height());
96 linearGrad.setColorAt(0, palette.color(QPalette::Light));
97 linearGrad.setColorAt(1, palette.color(QPalette::Midlight));
98 painter.fillRect(0, 0, width(), height(), QBrush(linearGrad));
101 QPixmap watermark = QPixmap(":/images/app.png");
102 painter.setOpacity(.25);
103 painter.drawPixmap(width() - watermark.width() - PADDING,
104 height() - watermark.height() - PADDING,
110 void SearchView::updateRecentKeywords() {
114 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
115 item->widget()->close();
121 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
122 recentKeywordsLabel->setVisible(!keywords.isEmpty());
123 foreach (QString keyword, keywords) {
124 QLabel *itemLabel = new QLabel("<a href=\"" + keyword
125 + "\" style=\"color:palette(text); text-decoration:none\">"
126 + keyword + "</a>", this);
128 itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
129 // Make links navigable with the keyboard too
130 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
132 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
133 recentKeywordsLayout->addWidget(itemLabel);
138 void SearchView::watch() {
139 QString query = queryEdit->text().simplified();
143 void SearchView::textChanged(const QString &text) {
144 watchButton->setEnabled(!text.simplified().isEmpty());
147 void SearchView::watch(QString query) {
149 // check for empty query
150 if (query.length() == 0) {
151 queryEdit->setFocus(Qt::OtherFocusReason);
155 queryEdit->preventSuggest();
161 void SearchView::checkForUpdate() {
162 static const QString updateCheckKey = "updateCheck";
166 uint unixTime = QDateTime::currentDateTime().toTime_t();
167 int lastCheck = settings.value(updateCheckKey).toInt();
168 int secondsSinceLastCheck = unixTime - lastCheck;
169 // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
170 if (secondsSinceLastCheck < 86400) return;
173 if (updateChecker) delete updateChecker;
174 updateChecker = new UpdateChecker();
175 connect(updateChecker, SIGNAL(newVersion(QString)),
176 this, SLOT(gotNewVersion(QString)));
177 updateChecker->checkForUpdate();
178 settings.setValue(updateCheckKey, unixTime);
182 void SearchView::gotNewVersion(QString version) {
184 tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
187 QString(Constants::WEBSITE).append("#download"),
190 message->setOpenExternalLinks(true);
191 message->setMargin(10);
192 message->setBackgroundRole(QPalette::ToolTipBase);
193 message->setForegroundRole(QPalette::ToolTipText);
194 message->setAutoFillBackground(true);
196 if (updateChecker) delete updateChecker;