]> git.sur5r.net Git - minitube/blob - src/sidebarwidget.cpp
27283d7e17cd60a6db6145a1c7841b4a6a0f0c7b
[minitube] / src / sidebarwidget.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "sidebarwidget.h"
22 #include "refinesearchbutton.h"
23 #include "refinesearchwidget.h"
24 #include "sidebarheader.h"
25 #include "mainwindow.h"
26 #ifdef APP_EXTRA
27 #include "extra.h"
28 #endif
29
30 SidebarWidget::SidebarWidget(QWidget *parent) :
31     QWidget(parent), playlistWidth(0) {
32     playlist = 0;
33
34     QBoxLayout *layout = new QVBoxLayout(this);
35     layout->setSpacing(0);
36     layout->setMargin(0);
37
38     sidebarHeader = new SidebarHeader();
39     layout->addWidget(sidebarHeader);
40
41     // hidden message widget
42     messageLabel = new QLabel(this);
43     messageLabel->setMargin(10);
44     messageLabel->setBackgroundRole(QPalette::ToolTipBase);
45     messageLabel->setForegroundRole(QPalette::ToolTipText);
46     messageLabel->setAutoFillBackground(true);
47     messageLabel->setWordWrap(true);
48     messageLabel->setTextFormat(Qt::RichText);
49     messageLabel->setTextInteractionFlags(
50                 Qt::LinksAccessibleByKeyboard |
51                 Qt::LinksAccessibleByMouse);
52     connect(messageLabel, SIGNAL(linkActivated(QString)),
53             SIGNAL(suggestionAccepted(QString)));
54     messageLabel->hide();
55     layout->addWidget(messageLabel);
56
57     stackedWidget = new QStackedWidget(this);
58     layout->addWidget(stackedWidget);
59
60     setup();
61 }
62
63 void SidebarWidget::setup() {
64     refineSearchButton = new RefineSearchButton(this);
65     refineSearchButton->setStatusTip(tr("Refine Search")
66                                      + " (" + QKeySequence(Qt::CTRL + Qt::Key_R).toString(QKeySequence::NativeText) + ")");
67     refineSearchButton->hide();
68     connect(refineSearchButton, SIGNAL(clicked()), SLOT(showRefineSearchWidget()));
69
70     refineSearchWidget = new RefineSearchWidget(this);
71     connect(refineSearchWidget, SIGNAL(done()), SLOT(hideRefineSearchWidget()));
72     stackedWidget->addWidget(refineSearchWidget);
73
74     setMouseTracking(true);
75     mouseTimer = new QTimer(this);
76     mouseTimer->setInterval(5000);
77     mouseTimer->setSingleShot(true);
78     connect(mouseTimer, SIGNAL(timeout()), refineSearchButton, SLOT(hide()));
79 }
80
81 void SidebarWidget::setPlaylist(QListView *playlist) {
82     this->playlist = playlist;
83     playlist->installEventFilter(this);
84     stackedWidget->addWidget(playlist);
85 }
86
87 void SidebarWidget::showPlaylist() {
88     stackedWidget->setCurrentWidget(playlist);
89     MainWindow::instance()->getAction("refineSearch")->setChecked(false);
90 }
91
92 void SidebarWidget::showRefineSearchWidget() {
93     if (!refineSearchWidget->isEnabled()) return;
94     playlistWidth = playlist->width();
95     refineSearchWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
96     refineSearchWidget->setDirty(false);
97     stackedWidget->setCurrentWidget(refineSearchWidget);
98     // refineSearchWidget->setFocus();
99 #ifdef APP_EXTRA
100     Extra::fadeInWidget(playlist, refineSearchWidget);
101 #endif
102     refineSearchButton->hide();
103     MainWindow::instance()->getAction("refineSearch")->setChecked(true);
104 }
105
106 void SidebarWidget::hideRefineSearchWidget() {
107     refineSearchWidget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
108     playlist->resize(playlistWidth, playlist->height());
109     stackedWidget->setCurrentWidget(playlist);
110     playlist->setFocus();
111 #ifdef APP_EXTRA
112     Extra::fadeInWidget(refineSearchWidget, playlist);
113 #endif
114     MainWindow::instance()->getAction("refineSearch")->setChecked(false);
115 }
116
117 void SidebarWidget::toggleRefineSearch(bool show) {
118     if (show) showRefineSearchWidget();
119     else hideRefineSearchWidget();
120 }
121
122 void SidebarWidget::resizeEvent(QResizeEvent *event) {
123     QWidget::resizeEvent(event);
124     refineSearchButton->move(
125                 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
126                 height() - refineSearchButton->minimumHeight());
127 }
128
129 void SidebarWidget::enterEvent(QEvent *) {
130     if (stackedWidget->currentWidget() != refineSearchWidget)
131         showRefineSearchButton();
132 }
133
134 void SidebarWidget::leaveEvent(QEvent *) {
135     refineSearchButton->hide();
136 }
137
138 void SidebarWidget::mouseMoveEvent(QMouseEvent *event) {
139     QWidget::mouseMoveEvent(event);
140     handleMouseMove();
141 }
142
143 bool SidebarWidget::eventFilter(QObject *obj, QEvent *event) {
144     if (event->type() == QEvent::MouseMove) handleMouseMove();
145     return QWidget::eventFilter(obj, event);
146 }
147
148 void SidebarWidget::handleMouseMove() {
149     if (stackedWidget->currentWidget() != refineSearchWidget) {
150         showRefineSearchButton();
151         mouseTimer->start();
152     }
153 }
154
155 void SidebarWidget::showRefineSearchButton() {
156     if (!refineSearchWidget->isEnabled()) return;
157     refineSearchButton->move(
158                 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
159                 height() - refineSearchButton->minimumHeight());
160     refineSearchButton->show();
161 }
162
163 void SidebarWidget::showSuggestions(const QStringList &suggestions) {
164     QString message = tr("Did you mean: %1");
165
166     QString suggestionLinks;
167     for (const QString &suggestion : suggestions) {
168         suggestionLinks += "<a href='" + suggestion + "'>" + suggestion + "</a> ";
169     }
170     message = message.arg(suggestionLinks);
171
172     QString html =
173             "<html>"
174             "<style>"
175             "a { color: palette(text); text-decoration: none; font-weight: bold }"
176             "</style>"
177             "<body>%1</body>"
178             "</html>";
179     html = html.arg(message);
180     messageLabel->setText(html);
181     messageLabel->show();
182 }
183
184 void SidebarWidget::hideSuggestions() {
185     messageLabel->hide();
186     messageLabel->clear();
187 }