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 "mainwindow.h"
23 #include "refinesearchbutton.h"
24 #include "refinesearchwidget.h"
25 #include "sidebarheader.h"
30 SidebarWidget::SidebarWidget(QWidget *parent) : QWidget(parent), playlistWidth(0) {
33 QBoxLayout *layout = new QVBoxLayout(this);
34 layout->setSpacing(0);
37 setBackgroundRole(QPalette::Base);
38 setAutoFillBackground(true);
40 sidebarHeader = new SidebarHeader();
41 layout->addWidget(sidebarHeader);
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)));
55 layout->addWidget(messageLabel);
57 stackedWidget = new QStackedWidget(this);
58 layout->addWidget(stackedWidget);
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()));
71 refineSearchWidget = new RefineSearchWidget(this);
72 connect(refineSearchWidget, SIGNAL(done()), SLOT(hideRefineSearchWidget()));
73 stackedWidget->addWidget(refineSearchWidget);
75 setMouseTracking(true);
76 mouseTimer = new QTimer(this);
77 mouseTimer->setInterval(5000);
78 mouseTimer->setSingleShot(true);
79 connect(mouseTimer, SIGNAL(timeout()), refineSearchButton, SLOT(hide()));
82 void SidebarWidget::setPlaylist(QListView *playlist) {
83 this->playlist = playlist;
84 playlist->installEventFilter(this);
85 stackedWidget->addWidget(playlist);
88 void SidebarWidget::showPlaylist() {
89 stackedWidget->setCurrentWidget(playlist);
90 MainWindow::instance()->getAction("refineSearch")->setChecked(false);
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();
101 Extra::fadeInWidget(playlist, refineSearchWidget);
103 refineSearchButton->hide();
104 MainWindow::instance()->getAction("refineSearch")->setChecked(true);
107 void SidebarWidget::hideRefineSearchWidget() {
108 refineSearchWidget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
109 playlist->resize(playlistWidth, playlist->height());
110 stackedWidget->setCurrentWidget(playlist);
111 playlist->setFocus();
113 Extra::fadeInWidget(refineSearchWidget, playlist);
115 MainWindow::instance()->getAction("refineSearch")->setChecked(false);
118 void SidebarWidget::toggleRefineSearch(bool show) {
120 showRefineSearchWidget();
122 hideRefineSearchWidget();
125 void SidebarWidget::resizeEvent(QResizeEvent *event) {
126 QWidget::resizeEvent(event);
127 refineSearchButton->move(playlist->viewport()->width() - refineSearchButton->minimumWidth(),
128 height() - refineSearchButton->minimumHeight());
131 void SidebarWidget::enterEvent(QEvent *) {
132 if (stackedWidget->currentWidget() != refineSearchWidget) showRefineSearchButton();
135 void SidebarWidget::leaveEvent(QEvent *) {
136 refineSearchButton->hide();
139 void SidebarWidget::mouseMoveEvent(QMouseEvent *event) {
140 QWidget::mouseMoveEvent(event);
144 bool SidebarWidget::eventFilter(QObject *obj, QEvent *event) {
145 if (event->type() == QEvent::MouseMove) handleMouseMove();
146 return QWidget::eventFilter(obj, event);
149 void SidebarWidget::handleMouseMove() {
150 if (stackedWidget->currentWidget() != refineSearchWidget) {
151 showRefineSearchButton();
156 void SidebarWidget::showRefineSearchButton() {
157 if (!refineSearchWidget->isEnabled()) return;
158 refineSearchButton->move(playlist->viewport()->width() - refineSearchButton->minimumWidth(),
159 height() - refineSearchButton->minimumHeight());
160 refineSearchButton->show();
163 void SidebarWidget::showSuggestions(const QStringList &suggestions) {
164 QString message = tr("Did you mean: %1");
166 QString suggestionLinks;
167 for (const QString &suggestion : suggestions) {
168 suggestionLinks += "<a href='" + suggestion + "'>" + suggestion + "</a> ";
170 message = message.arg(suggestionLinks);
172 QString html = "<html>"
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();