3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
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.
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.
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/>.
21 #include "sidebarwidget.h"
22 #include "refinesearchbutton.h"
23 #include "refinesearchwidget.h"
24 #include "sidebarheader.h"
30 QHash<QString, QAction*>* globalActions();
33 SidebarWidget::SidebarWidget(QWidget *parent) :
37 QBoxLayout *layout = new QVBoxLayout(this);
38 layout->setSpacing(0);
41 sidebarHeader = new SidebarHeader();
42 layout->addWidget(sidebarHeader);
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)));
58 layout->addWidget(messageLabel);
60 stackedWidget = new QStackedWidget(this);
61 layout->addWidget(stackedWidget);
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()));
73 refineSearchWidget = new RefineSearchWidget(this);
74 connect(refineSearchWidget, SIGNAL(done()), SLOT(hideRefineSearchWidget()));
75 stackedWidget->addWidget(refineSearchWidget);
77 setMouseTracking(true);
78 mouseTimer = new QTimer(this);
79 mouseTimer->setInterval(5000);
80 mouseTimer->setSingleShot(true);
81 connect(mouseTimer, SIGNAL(timeout()), refineSearchButton, SLOT(hide()));
84 void SidebarWidget::setPlaylist(QListView *playlist) {
85 this->playlist = playlist;
86 playlist->installEventFilter(this);
87 stackedWidget->addWidget(playlist);
90 void SidebarWidget::showPlaylist() {
91 stackedWidget->setCurrentWidget(playlist);
92 The::globalActions()->value("refine-search")->setChecked(false);
95 void SidebarWidget::showRefineSearchWidget() {
96 if (!refineSearchWidget->isEnabled()) return;
97 refineSearchWidget->setDirty(false);
98 stackedWidget->setCurrentWidget(refineSearchWidget);
99 // refineSearchWidget->setFocus();
101 Extra::fadeInWidget(playlist, refineSearchWidget);
103 refineSearchButton->hide();
104 The::globalActions()->value("refine-search")->setChecked(true);
107 void SidebarWidget::hideRefineSearchWidget() {
108 stackedWidget->setCurrentWidget(playlist);
109 playlist->setFocus();
111 Extra::fadeInWidget(refineSearchWidget, playlist);
113 The::globalActions()->value("refine-search")->setChecked(false);
116 void SidebarWidget::toggleRefineSearch(bool show) {
117 if (show) showRefineSearchWidget();
118 else hideRefineSearchWidget();
121 void SidebarWidget::resizeEvent(QResizeEvent *event) {
122 QWidget::resizeEvent(event);
123 refineSearchButton->move(
124 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
125 height() - refineSearchButton->minimumHeight());
128 void SidebarWidget::enterEvent(QEvent *) {
129 if (stackedWidget->currentWidget() != refineSearchWidget)
130 showRefineSearchButton();
133 void SidebarWidget::leaveEvent(QEvent *) {
134 refineSearchButton->hide();
137 void SidebarWidget::mouseMoveEvent(QMouseEvent *event) {
138 QWidget::mouseMoveEvent(event);
142 bool SidebarWidget::eventFilter(QObject *obj, QEvent *event) {
143 if (event->type() == QEvent::MouseMove) handleMouseMove();
144 return QWidget::eventFilter(obj, event);
147 void SidebarWidget::handleMouseMove() {
148 if (stackedWidget->currentWidget() != refineSearchWidget) {
149 showRefineSearchButton();
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();
162 void SidebarWidget::showSuggestions(const QStringList &suggestions) {
163 QString message = tr("Did you mean: %1");
165 QString suggestionLinks;
166 foreach (QString suggestion, suggestions) {
167 suggestionLinks += "<a href='" + suggestion + "'>" + suggestion + "</a> ";
169 message = message.arg(suggestionLinks);
174 "a { color: palette(text); text-decoration: none; font-weight: bold }"
178 html = html.arg(message);
179 messageLabel->setText(html);
180 messageLabel->show();
183 void SidebarWidget::hideSuggestions() {
184 messageLabel->hide();
185 messageLabel->clear();