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