]> git.sur5r.net Git - minitube/blob - src/SearchView.cpp
New search view look
[minitube] / src / SearchView.cpp
1 #include "SearchView.h"
2 #include "Constants.h"
3
4 static const QString recentKeywordsKey = "recentKeywords";
5 static const int PADDING = 30;
6
7 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
8
9 #ifdef Q_WS_MAC
10     // speedup painting since we'll paint the whole background
11     // by ourselves anyway in paintEvent()
12     setAttribute(Qt::WA_OpaquePaintEvent);
13 #endif
14
15     QFont biggerFont;
16     biggerFont.setPointSize(biggerFont.pointSize()*1.5);
17
18     QFont smallerFont;
19     smallerFont.setPointSize(smallerFont.pointSize()*.85);
20     smallerFont.setBold(true);
21
22     QBoxLayout *mainLayout = new QHBoxLayout();
23     mainLayout->setAlignment(Qt::AlignCenter);
24     mainLayout->setMargin(PADDING);
25
26     QLabel *logo = new QLabel(this);
27     logo->setPixmap(QPixmap(":/images/app.png"));
28     mainLayout->addWidget(logo, 0, Qt::AlignTop);
29     mainLayout->addSpacing(PADDING);
30
31     // hidden message widget
32     message = new QLabel(this);
33     message->hide();
34     mainLayout->addWidget(message);
35
36     QVBoxLayout *layout = new QVBoxLayout();
37     layout->setAlignment(Qt::AlignCenter);
38     mainLayout->addLayout(layout);
39
40     QLabel *welcomeLabel =
41             new QLabel("<h1>" +
42                        tr("Welcome to <a href='%1'>%2</a>,")
43                        .replace("<a ", "<a style='color:palette(text)'")
44                        .arg(Constants::WEBSITE, Constants::APP_NAME)
45                        + "</h1>", this);
46     welcomeLabel->setOpenExternalLinks(true);
47     layout->addWidget(welcomeLabel);
48
49     layout->addSpacing(PADDING);
50
51     QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this);
52     tipLabel->setFont(biggerFont);
53     layout->addWidget(tipLabel);
54
55     QHBoxLayout *searchLayout = new QHBoxLayout();
56     searchLayout->setAlignment(Qt::AlignCenter);
57
58     queryEdit = new SearchLineEdit(this);
59     queryEdit->setFont(biggerFont);
60     queryEdit->setMinimumWidth(300);
61     queryEdit->sizeHint();
62     queryEdit->setFocus(Qt::OtherFocusReason);
63     // connect(queryEdit, SIGNAL(returnPressed()), this, SLOT(watch()));
64     connect(queryEdit, SIGNAL(search(const QString&)), this, SLOT(watch(const QString&)));
65     connect(queryEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
66     searchLayout->addWidget(queryEdit);
67
68     searchLayout->addSpacing(10);
69
70     watchButton = new QPushButton(tr("Watch"), this);
71     watchButton->setDefault(true);
72     watchButton->setEnabled(false);
73     connect(watchButton, SIGNAL(clicked()), this, SLOT(watch()));
74     searchLayout->addWidget(watchButton);
75
76
77     layout->addItem(searchLayout);
78
79     layout->addSpacing(PADDING);
80
81     QHBoxLayout *otherLayout = new QHBoxLayout();
82
83     recentKeywordsLayout = new QVBoxLayout();
84     recentKeywordsLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
85     recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
86     recentKeywordsLabel->hide();
87     recentKeywordsLabel->setForegroundRole(QPalette::Dark);
88     recentKeywordsLabel->setFont(smallerFont);
89     recentKeywordsLayout->addWidget(recentKeywordsLabel);
90
91     otherLayout->addLayout(recentKeywordsLayout);
92
93     layout->addLayout(otherLayout);
94
95     setLayout(mainLayout);
96
97     updateChecker = 0;
98     checkForUpdate();
99 }
100
101 void SearchView::paintEvent(QPaintEvent * /*event*/) {
102
103     QPainter painter(this);
104
105 #ifdef Q_WS_MAC
106     QLinearGradient linearGrad(0, 0, 0, height());
107     QPalette palette;
108     linearGrad.setColorAt(0, palette.color(QPalette::Light));
109     linearGrad.setColorAt(1, palette.color(QPalette::Midlight));
110     painter.fillRect(0, 0, width(), height(), QBrush(linearGrad));
111 #endif
112
113     /*
114     QPixmap watermark = QPixmap(":/images/app.png");
115     painter.setOpacity(.25);
116     painter.drawPixmap(width() - watermark.width() - PADDING,
117                        height() - watermark.height() - PADDING,
118                        watermark.width(),
119                        watermark.height(),
120                        watermark);
121                        */
122 }
123
124 void SearchView::updateRecentKeywords() {
125
126     // cleanup
127     QLayoutItem *item;
128     while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
129         item->widget()->close();
130         delete item;
131     }
132
133     // load
134     QSettings settings;
135     QStringList keywords = settings.value(recentKeywordsKey).toStringList();
136     recentKeywordsLabel->setVisible(!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);
141
142         itemLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
143         // Make links navigable with the keyboard too
144         itemLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard | Qt::LinksAccessibleByMouse);
145
146         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
147         recentKeywordsLayout->addWidget(itemLabel);
148     }
149
150 }
151
152 void SearchView::watch() {
153     QString query = queryEdit->text().simplified();
154     watch(query);
155 }
156
157 void SearchView::textChanged(const QString &text) {
158     watchButton->setEnabled(!text.simplified().isEmpty());
159 }
160
161 void SearchView::watch(QString query) {
162
163     // check for empty query
164     if (query.length() == 0) {
165         queryEdit->setFocus(Qt::OtherFocusReason);
166         return;
167     }
168
169     queryEdit->preventSuggest();
170
171     // go!
172     emit search(query);
173 }
174
175 void SearchView::checkForUpdate() {
176     static const QString updateCheckKey = "updateCheck";
177
178     // check every 24h
179     QSettings settings;
180     uint unixTime = QDateTime::currentDateTime().toTime_t();
181     int lastCheck = settings.value(updateCheckKey).toInt();
182     int secondsSinceLastCheck = unixTime - lastCheck;
183     // qDebug() << "secondsSinceLastCheck" << unixTime << lastCheck << secondsSinceLastCheck;
184     if (secondsSinceLastCheck < 86400) return;
185
186     // check it out
187     if (updateChecker) delete updateChecker;
188     updateChecker = new UpdateChecker();
189     connect(updateChecker, SIGNAL(newVersion(QString)),
190             this, SLOT(gotNewVersion(QString)));
191     updateChecker->checkForUpdate();
192     settings.setValue(updateCheckKey, unixTime);
193
194 }
195
196 void SearchView::gotNewVersion(QString version) {
197     message->setText(
198             tr("A new version of %1 is available. Please <a href='%2'>update to version %3</a>")
199             .arg(
200                     Constants::APP_NAME,
201                     QString(Constants::WEBSITE).append("#download"),
202                     version)
203             );
204     message->setOpenExternalLinks(true);
205     message->setMargin(10);
206     message->setBackgroundRole(QPalette::ToolTipBase);
207     message->setForegroundRole(QPalette::ToolTipText);
208     message->setAutoFillBackground(true);
209     message->show();
210     if (updateChecker) delete updateChecker;
211 }