]> git.sur5r.net Git - minitube/blob - src/SearchView.cpp
Fixed fullscreen keyboard shortcut
[minitube] / src / SearchView.cpp
1 #include "SearchView.h"
2 #include "Constants.h"
3
4 static const QString recentKeywordsKey = "recentKeywords";
5 static const int PADDING = 40;
6
7 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
8
9     QFont biggerFont;
10     biggerFont.setPointSize(biggerFont.pointSize()*1.5);
11
12     QFont smallerFont;
13     smallerFont.setPointSize(smallerFont.pointSize()*.85);
14     smallerFont.setBold(true);
15
16     QVBoxLayout *mainLayout = new QVBoxLayout();
17     mainLayout->setMargin(0);
18
19     // hidden message widget
20     message = new QLabel(this);
21     message->hide();
22     mainLayout->addWidget(message);
23
24     QVBoxLayout *layout = new QVBoxLayout();
25     layout->setMargin(PADDING);
26     mainLayout->addLayout(layout);
27
28     QLabel *welcomeLabel =
29             new QLabel("<h1>" +
30                        tr("Welcome to <a href='%1'>%2</a>,").arg(Constants::WEBSITE, Constants::APP_NAME)
31                        + "</h1>", this);
32     welcomeLabel->setOpenExternalLinks(true);
33     layout->addWidget(welcomeLabel);
34
35     layout->addSpacing(PADDING);
36
37     QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this);
38     tipLabel->setFont(biggerFont);
39     layout->addWidget(tipLabel);
40
41     QHBoxLayout *searchLayout = new QHBoxLayout();
42
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);
52
53     searchLayout->addSpacing(10);
54
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);
60
61     searchLayout->addStretch();
62
63     layout->addItem(searchLayout);
64
65     layout->addSpacing(PADDING);
66
67     QHBoxLayout *otherLayout = new QHBoxLayout();
68
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);
76
77     otherLayout->addLayout(recentKeywordsLayout);
78
79     layout->addLayout(otherLayout);
80
81     layout->addStretch();
82
83     setLayout(mainLayout);
84
85     // watchButton->setMinimumHeight(queryEdit->height());
86
87     updateChecker = 0;
88     checkForUpdate();
89 }
90
91 void SearchView::paintEvent(QPaintEvent * /*event*/) {
92
93     QPainter painter(this);
94
95 #ifdef Q_WS_MAC
96     QLinearGradient linearGrad(0, 0, 0, height());
97     QPalette palette;
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));
101 #endif
102
103     QPixmap watermark = QPixmap(":/images/app.png");
104     painter.setOpacity(.25);
105     painter.drawPixmap(width() - watermark.width() - PADDING,
106                        height() - watermark.height() - PADDING,
107                        watermark.width(),
108                        watermark.height(),
109                        watermark);
110 }
111
112 void SearchView::updateRecentKeywords() {
113
114     // cleanup
115     QLayoutItem *item;
116     while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
117         item->widget()->close();
118         delete item;
119     }
120
121     // load
122     QSettings settings;
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);
129
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);
135     }
136
137 }
138
139 void SearchView::watch() {
140     QString query = queryEdit->text().simplified();
141     watch(query);
142 }
143
144 void SearchView::textChanged(const QString &text) {
145     watchButton->setEnabled(!text.simplified().isEmpty());
146 }
147
148 void SearchView::watch(QString query) {
149
150     // check for empty query
151     if (query.length() == 0) {
152         queryEdit->setFocus(Qt::OtherFocusReason);
153         return;
154     }
155
156     // save keyword
157     QSettings settings;
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);
164
165     // go!
166     emit search(query);
167 }
168
169 void SearchView::checkForUpdate() {
170     static const QString updateCheckKey = "updateCheck";
171
172     // check every 24h
173     QSettings settings;
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;
179
180     // check it out
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);
187
188 }
189
190 void SearchView::gotNewVersion(QString version) {
191     message->setText(
192             tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
193             .arg(
194                     Constants::APP_NAME,
195                     QString(Constants::WEBSITE).append("#download"),
196                     version)
197             );
198     message->setOpenExternalLinks(true);
199     message->setMargin(10);
200     message->setBackgroundRole(QPalette::ToolTipBase);
201     message->setForegroundRole(QPalette::ToolTipText);
202     message->setAutoFillBackground(true);
203     message->show();
204     if (updateChecker) delete updateChecker;
205 }