1 #include "SearchView.h"
5 QMap<QString, QAction*>* globalActions();
8 static const QString recentKeywordsKey = "recentKeywords";
9 static const int PADDING = 30;
11 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
14 biggerFont.setPointSize(biggerFont.pointSize()*1.5);
17 smallerFont.setPointSize(smallerFont.pointSize()*.85);
18 smallerFont.setBold(true);
20 QBoxLayout *mainLayout = new QVBoxLayout();
21 mainLayout->setMargin(0);
22 mainLayout->setSpacing(0);
24 // hidden message widget
25 message = new QLabel(this);
27 mainLayout->addWidget(message);
29 mainLayout->addStretch();
31 QBoxLayout *hLayout = new QHBoxLayout();
32 hLayout->setAlignment(Qt::AlignCenter);
33 mainLayout->addLayout(hLayout);
35 QLabel *logo = new QLabel(this);
36 logo->setPixmap(QPixmap(":/images/app.png"));
37 hLayout->addWidget(logo, 0, Qt::AlignTop);
38 hLayout->addSpacing(PADDING);
40 QVBoxLayout *layout = new QVBoxLayout();
41 layout->setAlignment(Qt::AlignCenter);
42 hLayout->addLayout(layout);
44 QLabel *welcomeLabel =
46 tr("Welcome to <a href='%1'>%2</a>,")
47 .replace("<a ", "<a style='color:palette(text)'")
48 .arg(Constants::WEBSITE, Constants::APP_NAME)
50 welcomeLabel->setOpenExternalLinks(true);
51 layout->addWidget(welcomeLabel);
53 layout->addSpacing(PADDING);
55 QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this);
56 tipLabel->setFont(biggerFont);
57 layout->addWidget(tipLabel);
59 layout->addSpacing(10);
61 QHBoxLayout *searchLayout = new QHBoxLayout();
62 searchLayout->setAlignment(Qt::AlignVCenter);
64 queryEdit = new SearchLineEdit(this);
65 queryEdit->setFont(biggerFont);
66 queryEdit->setMinimumWidth(queryEdit->fontInfo().pixelSize()*15);
67 queryEdit->sizeHint();
68 queryEdit->setFocus(Qt::OtherFocusReason);
69 connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&)));
70 connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
71 searchLayout->addWidget(queryEdit);
73 searchLayout->addSpacing(10);
75 watchButton = new QPushButton(tr("Watch"), this);
76 watchButton->setDefault(true);
77 watchButton->setEnabled(false);
78 watchButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
79 connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
80 searchLayout->addWidget(watchButton);
82 layout->addItem(searchLayout);
84 layout->addSpacing(PADDING);
86 QHBoxLayout *otherLayout = new QHBoxLayout();
88 recentKeywordsLayout = new QVBoxLayout();
89 recentKeywordsLayout->setSpacing(5);
90 recentKeywordsLayout->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
91 recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
92 recentKeywordsLabel->hide();
93 recentKeywordsLabel->setForegroundRole(QPalette::Dark);
94 recentKeywordsLabel->setFont(smallerFont);
95 recentKeywordsLayout->addWidget(recentKeywordsLabel);
97 otherLayout->addLayout(recentKeywordsLayout);
99 layout->addLayout(otherLayout);
101 mainLayout->addStretch();
103 setLayout(mainLayout);
109 void SearchView::updateRecentKeywords() {
113 while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
114 item->widget()->close();
120 QStringList keywords = settings.value(recentKeywordsKey).toStringList();
121 recentKeywordsLabel->setVisible(!keywords.isEmpty());
122 The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
124 foreach (QString keyword, keywords) {
125 QLabel *itemLabel = new QLabel("<a href=\"" + keyword
126 + "\" style=\"color:palette(text); text-decoration:none\">"
127 + keyword + "</a>", this);
129 itemLabel->setMaximumWidth(queryEdit->width() + watchButton->width());
130 // itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
131 // Make links navigable with the keyboard too
132 itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
134 connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
135 recentKeywordsLayout->addWidget(itemLabel);
142 void SearchView::watch() {
143 QString query = queryEdit->text().simplified();
147 void SearchView::textChanged(const QString &text) {
148 watchButton->setEnabled(!text.simplified().isEmpty());
151 void SearchView::watch(QString query) {
153 // check for empty query
154 if (query.length() == 0) {
155 queryEdit->setFocus(Qt::OtherFocusReason);
163 void SearchView::checkForUpdate() {
164 static const QString updateCheckKey = "updateCheck";
168 uint unixTime = QDateTime::currentDateTime().toTime_t();
169 int lastCheck = settings.value(updateCheckKey).toInt();
170 int secondsSinceLastCheck = unixTime - lastCheck;
171 // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
172 if (secondsSinceLastCheck < 86400) return;
175 if (updateChecker) delete updateChecker;
176 updateChecker = new UpdateChecker();
177 connect(updateChecker, SIGNAL(newVersion(QString)),
178 this, SLOT(gotNewVersion(QString)));
179 updateChecker->checkForUpdate();
180 settings.setValue(updateCheckKey, unixTime);
184 void SearchView::gotNewVersion(QString version) {
186 tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
189 QString(Constants::WEBSITE).append("#download"),
192 message->setOpenExternalLinks(true);
193 message->setMargin(10);
194 message->setBackgroundRole(QPalette::ToolTipBase);
195 message->setForegroundRole(QPalette::ToolTipText);
196 message->setAutoFillBackground(true);
198 if (updateChecker) delete updateChecker;