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) :
34 QWidget(parent), playlistWidth(0) {
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 playlistWidth = playlist->width();
98 refineSearchWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
99 refineSearchWidget->setDirty(false);
100 stackedWidget->setCurrentWidget(refineSearchWidget);
101 // refineSearchWidget->setFocus();
103 Extra::fadeInWidget(playlist, refineSearchWidget);
105 refineSearchButton->hide();
106 The::globalActions()->value("refine-search")->setChecked(true);
109 void SidebarWidget::hideRefineSearchWidget() {
110 refineSearchWidget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
111 playlist->resize(playlistWidth, playlist->height());
112 stackedWidget->setCurrentWidget(playlist);
113 playlist->setFocus();
115 Extra::fadeInWidget(refineSearchWidget, playlist);
117 The::globalActions()->value("refine-search")->setChecked(false);
120 void SidebarWidget::toggleRefineSearch(bool show) {
121 if (show) showRefineSearchWidget();
122 else hideRefineSearchWidget();
125 void SidebarWidget::resizeEvent(QResizeEvent *event) {
126 QWidget::resizeEvent(event);
127 refineSearchButton->move(
128 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
129 height() - refineSearchButton->minimumHeight());
132 void SidebarWidget::enterEvent(QEvent *) {
133 if (stackedWidget->currentWidget() != refineSearchWidget)
134 showRefineSearchButton();
137 void SidebarWidget::leaveEvent(QEvent *) {
138 refineSearchButton->hide();
141 void SidebarWidget::mouseMoveEvent(QMouseEvent *event) {
142 QWidget::mouseMoveEvent(event);
146 bool SidebarWidget::eventFilter(QObject *obj, QEvent *event) {
147 if (event->type() == QEvent::MouseMove) handleMouseMove();
148 return QWidget::eventFilter(obj, event);
151 void SidebarWidget::handleMouseMove() {
152 if (stackedWidget->currentWidget() != refineSearchWidget) {
153 showRefineSearchButton();
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();
166 void SidebarWidget::showSuggestions(const QStringList &suggestions) {
167 QString message = tr("Did you mean: %1");
169 QString suggestionLinks;
170 foreach (const QString &suggestion, suggestions) {
171 suggestionLinks += "<a href='" + suggestion + "'>" + suggestion + "</a> ";
173 message = message.arg(suggestionLinks);
178 "a { color: palette(text); text-decoration: none; font-weight: bold }"
182 html = html.arg(message);
183 messageLabel->setText(html);
184 messageLabel->show();
187 void SidebarWidget::hideSuggestions() {
188 messageLabel->hide();
189 messageLabel->clear();