]> git.sur5r.net Git - minitube/blob - src/sidebarwidget.cpp
New upstream version 2.1.3
[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) {
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     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     The::globalActions()->value("refine-search")->setChecked(true);
105 }
106
107 void SidebarWidget::hideRefineSearchWidget() {
108     stackedWidget->setCurrentWidget(playlist);
109     playlist->setFocus();
110 #ifdef APP_EXTRA
111     Extra::fadeInWidget(refineSearchWidget, playlist);
112 #endif
113     The::globalActions()->value("refine-search")->setChecked(false);
114 }
115
116 void SidebarWidget::toggleRefineSearch(bool show) {
117     if (show) showRefineSearchWidget();
118     else hideRefineSearchWidget();
119 }
120
121 void SidebarWidget::resizeEvent(QResizeEvent *event) {
122     QWidget::resizeEvent(event);
123     refineSearchButton->move(
124                 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
125                 height() - refineSearchButton->minimumHeight());
126 }
127
128 void SidebarWidget::enterEvent(QEvent *) {
129     if (stackedWidget->currentWidget() != refineSearchWidget)
130         showRefineSearchButton();
131 }
132
133 void SidebarWidget::leaveEvent(QEvent *) {
134     refineSearchButton->hide();
135 }
136
137 void SidebarWidget::mouseMoveEvent(QMouseEvent *event) {
138     QWidget::mouseMoveEvent(event);
139     handleMouseMove();
140 }
141
142 bool SidebarWidget::eventFilter(QObject *obj, QEvent *event) {
143     if (event->type() == QEvent::MouseMove) handleMouseMove();
144     return QWidget::eventFilter(obj, event);
145 }
146
147 void SidebarWidget::handleMouseMove() {
148     if (stackedWidget->currentWidget() != refineSearchWidget) {
149         showRefineSearchButton();
150         mouseTimer->start();
151     }
152 }
153
154 void SidebarWidget::showRefineSearchButton() {
155     if (!refineSearchWidget->isEnabled()) return;
156     refineSearchButton->move(
157                 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
158                 height() - refineSearchButton->minimumHeight());
159     refineSearchButton->show();
160 }
161
162 void SidebarWidget::showSuggestions(const QStringList &suggestions) {
163     QString message = tr("Did you mean: %1");
164
165     QString suggestionLinks;
166     foreach (QString suggestion, suggestions) {
167         suggestionLinks += "<a href='" + suggestion + "'>" + suggestion + "</a> ";
168     }
169     message = message.arg(suggestionLinks);
170
171     QString html =
172             "<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 }