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"
25 #include "mainwindow.h"
30 SidebarWidget::SidebarWidget(QWidget *parent) :
31 QWidget(parent), playlistWidth(0) {
34 QBoxLayout *layout = new QVBoxLayout(this);
35 layout->setSpacing(0);
38 sidebarHeader = new SidebarHeader();
39 layout->addWidget(sidebarHeader);
41 // hidden message widget
42 messageLabel = new QLabel(this);
43 messageLabel->setMargin(10);
44 messageLabel->setBackgroundRole(QPalette::ToolTipBase);
45 messageLabel->setForegroundRole(QPalette::ToolTipText);
46 messageLabel->setAutoFillBackground(true);
47 messageLabel->setWordWrap(true);
48 messageLabel->setTextFormat(Qt::RichText);
49 messageLabel->setTextInteractionFlags(
50 Qt::LinksAccessibleByKeyboard |
51 Qt::LinksAccessibleByMouse);
52 connect(messageLabel, SIGNAL(linkActivated(QString)),
53 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(tr("Refine Search")
66 + " (" + QKeySequence(Qt::CTRL + Qt::Key_R).toString(QKeySequence::NativeText) + ")");
67 refineSearchButton->hide();
68 connect(refineSearchButton, SIGNAL(clicked()), SLOT(showRefineSearchWidget()));
70 refineSearchWidget = new RefineSearchWidget(this);
71 connect(refineSearchWidget, SIGNAL(done()), SLOT(hideRefineSearchWidget()));
72 stackedWidget->addWidget(refineSearchWidget);
74 setMouseTracking(true);
75 mouseTimer = new QTimer(this);
76 mouseTimer->setInterval(5000);
77 mouseTimer->setSingleShot(true);
78 connect(mouseTimer, SIGNAL(timeout()), refineSearchButton, SLOT(hide()));
81 void SidebarWidget::setPlaylist(QListView *playlist) {
82 this->playlist = playlist;
83 playlist->installEventFilter(this);
84 stackedWidget->addWidget(playlist);
87 void SidebarWidget::showPlaylist() {
88 stackedWidget->setCurrentWidget(playlist);
89 MainWindow::instance()->getAction("refineSearch")->setChecked(false);
92 void SidebarWidget::showRefineSearchWidget() {
93 if (!refineSearchWidget->isEnabled()) return;
94 playlistWidth = playlist->width();
95 refineSearchWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
96 refineSearchWidget->setDirty(false);
97 stackedWidget->setCurrentWidget(refineSearchWidget);
98 // refineSearchWidget->setFocus();
100 Extra::fadeInWidget(playlist, refineSearchWidget);
102 refineSearchButton->hide();
103 MainWindow::instance()->getAction("refineSearch")->setChecked(true);
106 void SidebarWidget::hideRefineSearchWidget() {
107 refineSearchWidget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
108 playlist->resize(playlistWidth, playlist->height());
109 stackedWidget->setCurrentWidget(playlist);
110 playlist->setFocus();
112 Extra::fadeInWidget(refineSearchWidget, playlist);
114 MainWindow::instance()->getAction("refineSearch")->setChecked(false);
117 void SidebarWidget::toggleRefineSearch(bool show) {
118 if (show) showRefineSearchWidget();
119 else hideRefineSearchWidget();
122 void SidebarWidget::resizeEvent(QResizeEvent *event) {
123 QWidget::resizeEvent(event);
124 refineSearchButton->move(
125 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
126 height() - refineSearchButton->minimumHeight());
129 void SidebarWidget::enterEvent(QEvent *) {
130 if (stackedWidget->currentWidget() != refineSearchWidget)
131 showRefineSearchButton();
134 void SidebarWidget::leaveEvent(QEvent *) {
135 refineSearchButton->hide();
138 void SidebarWidget::mouseMoveEvent(QMouseEvent *event) {
139 QWidget::mouseMoveEvent(event);
143 bool SidebarWidget::eventFilter(QObject *obj, QEvent *event) {
144 if (event->type() == QEvent::MouseMove) handleMouseMove();
145 return QWidget::eventFilter(obj, event);
148 void SidebarWidget::handleMouseMove() {
149 if (stackedWidget->currentWidget() != refineSearchWidget) {
150 showRefineSearchButton();
155 void SidebarWidget::showRefineSearchButton() {
156 if (!refineSearchWidget->isEnabled()) return;
157 refineSearchButton->move(
158 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);
175 "a { color: palette(text); text-decoration: none; font-weight: bold }"
179 html = html.arg(message);
180 messageLabel->setText(html);
181 messageLabel->show();
184 void SidebarWidget::hideSuggestions() {
185 messageLabel->hide();
186 messageLabel->clear();