]> git.sur5r.net Git - minitube/blob - src/SearchView.cpp
Imported Upstream version 1.2
[minitube] / src / SearchView.cpp
1 #include "SearchView.h"
2 #include "constants.h"
3 #include "fontutils.h"
4
5 namespace The {
6     QMap<QString, QAction*>* globalActions();
7 }
8
9 static const QString recentKeywordsKey = "recentKeywords";
10 static const int PADDING = 30;
11
12 SearchView::SearchView(QWidget *parent) : QWidget(parent) {
13
14     QFont biggerFont = FontUtils::big();
15     QFont smallerFont = FontUtils::smallBold();
16
17 #ifdef APP_MAC
18     // speedup painting since we'll paint the whole background
19     // by ourselves anyway in paintEvent()
20     setAttribute(Qt::WA_OpaquePaintEvent);
21 #endif
22
23     QBoxLayout *mainLayout = new QVBoxLayout();
24     mainLayout->setMargin(0);
25     mainLayout->setSpacing(0);
26
27     // hidden message widget
28     message = new QLabel(this);
29     message->hide();
30     mainLayout->addWidget(message);
31
32     mainLayout->addStretch();
33     mainLayout->addSpacing(PADDING);
34
35     QBoxLayout *hLayout = new QHBoxLayout();
36     hLayout->setAlignment(Qt::AlignCenter);
37     mainLayout->addLayout(hLayout);
38
39     QLabel *logo = new QLabel(this);
40     logo->setPixmap(QPixmap(":/images/app.png"));
41     hLayout->addWidget(logo, 0, Qt::AlignTop);
42     hLayout->addSpacing(PADDING);
43
44     QVBoxLayout *layout = new QVBoxLayout();
45     layout->setAlignment(Qt::AlignCenter);
46     hLayout->addLayout(layout);
47
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)
54                        + "</h1>", this);
55     welcomeLabel->setOpenExternalLinks(true);
56     layout->addWidget(welcomeLabel);
57
58     layout->addSpacing(PADDING / 2);
59
60     QLabel *tipLabel = new QLabel(tr("Enter a keyword to start watching videos."), this);
61     tipLabel->setFont(biggerFont);
62     layout->addWidget(tipLabel);
63
64     layout->addSpacing(PADDING / 2);
65
66     QHBoxLayout *searchLayout = new QHBoxLayout();
67     searchLayout->setAlignment(Qt::AlignVCenter);
68
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);
77
78     searchLayout->addSpacing(10);
79
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);
86
87     layout->addItem(searchLayout);
88
89     layout->addSpacing(PADDING / 2);
90
91     QHBoxLayout *otherLayout = new QHBoxLayout();
92     otherLayout->setMargin(0);
93
94     recentKeywordsLayout = new QVBoxLayout();
95     recentKeywordsLayout->setSpacing(5);
96     recentKeywordsLayout->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
97     recentKeywordsLabel = new QLabel(tr("Recent keywords").toUpper(), this);
98 #ifdef APP_MAC
99     QPalette palette = recentKeywordsLabel->palette();
100     palette.setColor(QPalette::WindowText, QColor(0x65, 0x71, 0x80));
101     recentKeywordsLabel->setPalette(palette);
102 #else
103     recentKeywordsLabel->setForegroundRole(QPalette::Dark);
104 #endif
105     recentKeywordsLabel->hide();
106     recentKeywordsLabel->setFont(smallerFont);
107     recentKeywordsLayout->addWidget(recentKeywordsLabel);
108
109     otherLayout->addLayout(recentKeywordsLayout);
110
111     layout->addLayout(otherLayout);
112
113     mainLayout->addSpacing(PADDING);
114     mainLayout->addStretch();
115
116     setLayout(mainLayout);
117
118     updateChecker = 0;
119     checkForUpdate();
120 }
121
122 void SearchView::updateRecentKeywords() {
123
124     // cleanup
125     QLayoutItem *item;
126     while ((item = recentKeywordsLayout->takeAt(1)) != 0) {
127         item->widget()->close();
128         delete item;
129     }
130
131     // load
132     QSettings settings;
133     QStringList keywords = settings.value(recentKeywordsKey).toStringList();
134     recentKeywordsLabel->setVisible(!keywords.isEmpty());
135     The::globalActions()->value("clearRecentKeywords")->setEnabled(!keywords.isEmpty());
136
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->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);
146
147         connect(itemLabel, SIGNAL(linkActivated(QString)), this, SLOT(watch(QString)));
148         recentKeywordsLayout->addWidget(itemLabel);
149     }
150
151 }
152
153
154
155 void SearchView::watch() {
156     QString query = queryEdit->text().simplified();
157     watch(query);
158 }
159
160 void SearchView::textChanged(const QString &text) {
161     watchButton->setEnabled(!text.simplified().isEmpty());
162 }
163
164 void SearchView::watch(QString query) {
165
166     // check for empty query
167     if (query.length() == 0) {
168         queryEdit->setFocus(Qt::OtherFocusReason);
169         return;
170     }
171
172     // go!
173     emit search(query);
174 }
175
176 void SearchView::checkForUpdate() {
177     static const QString updateCheckKey = "updateCheck";
178
179     // check every 24h
180     QSettings settings;
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;
186
187     // check it out
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);
194
195 }
196
197 void SearchView::gotNewVersion(QString version) {
198     message->setText(
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")
201             .arg(
202                     Constants::APP_NAME,
203                     QString(Constants::WEBSITE).append("#download"),
204                     version)
205             );
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); }");
213     message->show();
214     if (updateChecker) delete updateChecker;
215 }
216
217 void SearchView::paintEvent(QPaintEvent * /*event*/) {
218 #ifdef APP_MAC
219     QBrush brush;
220     if (window()->isActiveWindow()) {
221         brush = QBrush(QColor(0xdd, 0xe4, 0xeb));
222     } else {
223         brush = palette().window();
224     }
225     QPainter painter(this);
226     painter.fillRect(0, 0, width(), height(), brush);
227 #endif
228 }