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 QLabel *itemLabel = new QLabel("<a href=\"" + keyword
139 + "\" style=\"color:palette(text); text-decoration:none\">"
140 + keyword + "</a>", this);
142 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
143 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
144 // Make links navigable with the keyboard too
145 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
147 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
148 recentKeywordsLayout->addWidget(itemLabel);
155 void SearchView::watch() {
156 QString query = queryEdit->text().simplified();
160 void SearchView::textChanged(const QString &text) {
161 watchButton->setEnabled(!text.simplified().isEmpty());
164 void SearchView::watch(QString query) {
166 // check for empty query
167 if (query.length() == 0) {
168 queryEdit->setFocus(Qt::OtherFocusReason);
176 void SearchView::checkForUpdate() {
177 static const QString updateCheckKey = "updateCheck";
181 uint unixTime = QDateTime::currentDateTime().toTime_t();
182 int lastCheck = settings.value(updateCheckKey).toInt();
183 int secondsSinceLastCheck = unixTime - lastCheck;
184 // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
185 if (secondsSinceLastCheck < 86400) return;
188 if (updateChecker) delete updateChecker;
189 updateChecker = new UpdateChecker();
190 connect(updateChecker, SIGNAL(newVersion(QString)),
191 this, SLOT(gotNewVersion(QString)));
192 updateChecker->checkForUpdate();
193 settings.setValue(updateCheckKey, unixTime);
197 void SearchView::gotNewVersion(QString version) {
199 tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
200 .replace("<a href", "<a style='text-decoration:none; color:palette(text); font-weight:bold' href")
203 QString(Constants::WEBSITE).append("#download"),
206 message->setOpenExternalLinks(true);
207 message->setMargin(10);
208 message->setAlignment(Qt::AlignCenter);
209 // message->setBackgroundRole(QPalette::ToolTipBase);
210 // message->setForegroundRole(QPalette::ToolTipText);
211 // message->setAutoFillBackground(true);
212 message->setStyleSheet("QLabel { border-bottom: 1px solid palette(mid); }");
214 if (updateChecker) delete updateChecker;
217 void SearchView::paintEvent(QPaintEvent * /*event*/) {
220 if (window()->isActiveWindow()) {
221 brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
223 brush = palette().window();
225 QPainter painter(this);
226 painter.fillRect(0, 0, width(), height(), brush);