]> git.sur5r.net Git - minitube/blob - src/sidebarwidget.cpp
Upload 3.9.3-2 to unstable
[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 "mainwindow.h"
23 #include "refinesearchbutton.h"
24 #include "refinesearchwidget.h"
25 #include "sidebarheader.h"
26 #ifdef APP_EXTRA
27 #include "extra.h"
28 #endif
29
30 SidebarWidget::SidebarWidget(QWidget *parent) : QWidget(parent), playlistWidth(0) {
31     playlist = nullptr;
32
33     QBoxLayout *layout = new QVBoxLayout(this);
34     layout->setSpacing(0);
35     layout->setMargin(0);
36
37     setBackgroundRole(QPalette::Base);
38     setAutoFillBackground(true);
39
40     sidebarHeader = new SidebarHeader();
41     layout->addWidget(sidebarHeader);
42
43     // hidden message widget
44     messageLabel = new QLabel(this);
45     messageLabel->setMargin(10);
46     messageLabel->setBackgroundRole(QPalette::ToolTipBase);
47     messageLabel->setForegroundRole(QPalette::ToolTipText);
48     messageLabel->setAutoFillBackground(true);
49     messageLabel->setWordWrap(true);
50     messageLabel->setTextFormat(Qt::RichText);
51     messageLabel->setTextInteractionFlags(Qt::LinksAccessibleByKeyboard |
52                                           Qt::LinksAccessibleByMouse);
53     connect(messageLabel, SIGNAL(linkActivated(QString)), 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(
66             tr("Refine Search") + " (" +
67             QKeySequence(Qt::CTRL + Qt::Key_R).toString(QKeySequence::NativeText) + ")");
68     refineSearchButton->hide();
69     connect(refineSearchButton, SIGNAL(clicked()), SLOT(showRefineSearchWidget()));
70
71     refineSearchWidget = new RefineSearchWidget(this);
72     connect(refineSearchWidget, SIGNAL(done()), SLOT(hideRefineSearchWidget()));
73     stackedWidget->addWidget(refineSearchWidget);
74
75     setMouseTracking(true);
76     mouseTimer = new QTimer(this);
77     mouseTimer->setInterval(5000);
78     mouseTimer->setSingleShot(true);
79     connect(mouseTimer, SIGNAL(timeout()), refineSearchButton, SLOT(hide()));
80 }
81
82 void SidebarWidget::setPlaylist(QListView *playlist) {
83     this->playlist = playlist;
84     playlist->installEventFilter(this);
85     stackedWidget->addWidget(playlist);
86 }
87
88 void SidebarWidget::showPlaylist() {
89     stackedWidget->setCurrentWidget(playlist);
90     MainWindow::instance()->getAction("refineSearch")->setChecked(false);
91 }
92
93 void SidebarWidget::showRefineSearchWidget() {
94     if (!refineSearchWidget->isEnabled()) return;
95     playlistWidth = playlist->width();
96     refineSearchWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
97     refineSearchWidget->setDirty(false);
98     stackedWidget->setCurrentWidget(refineSearchWidget);
99     // refineSearchWidget->setFocus();
100 #ifdef APP_EXTRA
101     Extra::fadeInWidget(playlist, refineSearchWidget);
102 #endif
103     refineSearchButton->hide();
104     MainWindow::instance()->getAction("refineSearch")->setChecked(true);
105 }
106
107 void SidebarWidget::hideRefineSearchWidget() {
108     refineSearchWidget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
109     playlist->resize(playlistWidth, playlist->height());
110     stackedWidget->setCurrentWidget(playlist);
111     playlist->setFocus();
112 #ifdef APP_EXTRA
113     Extra::fadeInWidget(refineSearchWidget, playlist);
114 #endif
115     MainWindow::instance()->getAction("refineSearch")->setChecked(false);
116 }
117
118 void SidebarWidget::toggleRefineSearch(bool show) {
119     if (show)
120         showRefineSearchWidget();
121     else
122         hideRefineSearchWidget();
123 }
124
125 void SidebarWidget::resizeEvent(QResizeEvent *event) {
126     QWidget::resizeEvent(event);
127     refineSearchButton->move(playlist->viewport()->width() - refineSearchButton->minimumWidth(),
128                              height() - refineSearchButton->minimumHeight());
129 }
130
131 void SidebarWidget::enterEvent(QEvent *) {
132     if (stackedWidget->currentWidget() != refineSearchWidget) showRefineSearchButton();
133 }
134
135 void SidebarWidget::leaveEvent(QEvent *) {
136     refineSearchButton->hide();
137 }
138
139 void SidebarWidget::mouseMoveEvent(QMouseEvent *event) {
140     QWidget::mouseMoveEvent(event);
141     handleMouseMove();
142 }
143
144 bool SidebarWidget::eventFilter(QObject *obj, QEvent *event) {
145     if (event->type() == QEvent::MouseMove) handleMouseMove();
146     return QWidget::eventFilter(obj, event);
147 }
148
149 void SidebarWidget::handleMouseMove() {
150     if (stackedWidget->currentWidget() != refineSearchWidget) {
151         showRefineSearchButton();
152         mouseTimer->start();
153     }
154 }
155
156 void SidebarWidget::showRefineSearchButton() {
157     if (!refineSearchWidget->isEnabled()) return;
158     refineSearchButton->move(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 = "<html>"
173                    "<style>"
174                    "a { color: palette(text); text-decoration: none; font-weight: bold }"
175                    "</style>"
176                    "<body>%1</body>"
177                    "</html>";
178     html = html.arg(message);
179     messageLabel->setText(html);
180     messageLabel->show();
181 }
182
183 void SidebarWidget::hideSuggestions() {
184     messageLabel->hide();
185     messageLabel->clear();
186 }