]> git.sur5r.net Git - minitube/blob - src/sidebarwidget.cpp
Merge tag 'upstream/2.5.1'
[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 #ifdef APP_EXTRA
26 #include "extra.h"
27 #endif
28
29 namespace The {
30 QHash<QString, QAction*>* globalActions();
31 }
32
33 SidebarWidget::SidebarWidget(QWidget *parent) :
34     QWidget(parent), playlistWidth(0) {
35     playlist = 0;
36
37     QBoxLayout *layout = new QVBoxLayout(this);
38     layout->setSpacing(0);
39     layout->setMargin(0);
40
41     sidebarHeader = new SidebarHeader();
42     layout->addWidget(sidebarHeader);
43
44     // hidden message widget
45     messageLabel = new QLabel(this);
46     messageLabel->setMargin(10);
47     messageLabel->setBackgroundRole(QPalette::ToolTipBase);
48     messageLabel->setForegroundRole(QPalette::ToolTipText);
49     messageLabel->setAutoFillBackground(true);
50     messageLabel->setWordWrap(true);
51     messageLabel->setTextFormat(Qt::RichText);
52     messageLabel->setTextInteractionFlags(
53                 Qt::LinksAccessibleByKeyboard |
54                 Qt::LinksAccessibleByMouse);
55     connect(messageLabel, SIGNAL(linkActivated(QString)),
56             SIGNAL(suggestionAccepted(QString)));
57     messageLabel->hide();
58     layout->addWidget(messageLabel);
59
60     stackedWidget = new QStackedWidget(this);
61     layout->addWidget(stackedWidget);
62
63     setup();
64 }
65
66 void SidebarWidget::setup() {
67     refineSearchButton = new RefineSearchButton(this);
68     refineSearchButton->setStatusTip(tr("Refine Search")
69                                      + " (" + QKeySequence(Qt::CTRL + Qt::Key_R).toString(QKeySequence::NativeText) + ")");
70     refineSearchButton->hide();
71     connect(refineSearchButton, SIGNAL(clicked()), SLOT(showRefineSearchWidget()));
72
73     refineSearchWidget = new RefineSearchWidget(this);
74     connect(refineSearchWidget, SIGNAL(done()), SLOT(hideRefineSearchWidget()));
75     stackedWidget->addWidget(refineSearchWidget);
76
77     setMouseTracking(true);
78     mouseTimer = new QTimer(this);
79     mouseTimer->setInterval(5000);
80     mouseTimer->setSingleShot(true);
81     connect(mouseTimer, SIGNAL(timeout()), refineSearchButton, SLOT(hide()));
82 }
83
84 void SidebarWidget::setPlaylist(QListView *playlist) {
85     this->playlist = playlist;
86     playlist->installEventFilter(this);
87     stackedWidget->addWidget(playlist);
88 }
89
90 void SidebarWidget::showPlaylist() {
91     stackedWidget->setCurrentWidget(playlist);
92     The::globalActions()->value("refine-search")->setChecked(false);
93 }
94
95 void SidebarWidget::showRefineSearchWidget() {
96     if (!refineSearchWidget->isEnabled()) return;
97     playlistWidth = playlist->width();
98     refineSearchWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
99     refineSearchWidget->setDirty(false);
100     stackedWidget->setCurrentWidget(refineSearchWidget);
101     // refineSearchWidget->setFocus();
102 #ifdef APP_EXTRA
103     Extra::fadeInWidget(playlist, refineSearchWidget);
104 #endif
105     refineSearchButton->hide();
106     The::globalActions()->value("refine-search")->setChecked(true);
107 }
108
109 void SidebarWidget::hideRefineSearchWidget() {
110     refineSearchWidget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
111     playlist->resize(playlistWidth, playlist->height());
112     stackedWidget->setCurrentWidget(playlist);
113     playlist->setFocus();
114 #ifdef APP_EXTRA
115     Extra::fadeInWidget(refineSearchWidget, playlist);
116 #endif
117     The::globalActions()->value("refine-search")->setChecked(false);
118 }
119
120 void SidebarWidget::toggleRefineSearch(bool show) {
121     if (show) showRefineSearchWidget();
122     else hideRefineSearchWidget();
123 }
124
125 void SidebarWidget::resizeEvent(QResizeEvent *event) {
126     QWidget::resizeEvent(event);
127     refineSearchButton->move(
128                 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
129                 height() - refineSearchButton->minimumHeight());
130 }
131
132 void SidebarWidget::enterEvent(QEvent *) {
133     if (stackedWidget->currentWidget() != refineSearchWidget)
134         showRefineSearchButton();
135 }
136
137 void SidebarWidget::leaveEvent(QEvent *) {
138     refineSearchButton->hide();
139 }
140
141 void SidebarWidget::mouseMoveEvent(QMouseEvent *event) {
142     QWidget::mouseMoveEvent(event);
143     handleMouseMove();
144 }
145
146 bool SidebarWidget::eventFilter(QObject *obj, QEvent *event) {
147     if (event->type() == QEvent::MouseMove) handleMouseMove();
148     return QWidget::eventFilter(obj, event);
149 }
150
151 void SidebarWidget::handleMouseMove() {
152     if (stackedWidget->currentWidget() != refineSearchWidget) {
153         showRefineSearchButton();
154         mouseTimer->start();
155     }
156 }
157
158 void SidebarWidget::showRefineSearchButton() {
159     if (!refineSearchWidget->isEnabled()) return;
160     refineSearchButton->move(
161                 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
162                 height() - refineSearchButton->minimumHeight());
163     refineSearchButton->show();
164 }
165
166 void SidebarWidget::showSuggestions(const QStringList &suggestions) {
167     QString message = tr("Did you mean: %1");
168
169     QString suggestionLinks;
170     foreach (const QString &suggestion, suggestions) {
171         suggestionLinks += "<a href='" + suggestion + "'>" + suggestion + "</a> ";
172     }
173     message = message.arg(suggestionLinks);
174
175     QString html =
176             "<html>"
177             "<style>"
178             "a { color: palette(text); text-decoration: none; font-weight: bold }"
179             "</style>"
180             "<body>%1</body>"
181             "</html>";
182     html = html.arg(message);
183     messageLabel->setText(html);
184     messageLabel->show();
185 }
186
187 void SidebarWidget::hideSuggestions() {
188     messageLabel->hide();
189     messageLabel->clear();
190 }