]> git.sur5r.net Git - minitube/blob - src/sidebarwidget.cpp
Imported Upstream version 2.0
[minitube] / src / sidebarwidget.cpp
1 #include "sidebarwidget.h"
2 #include "refinesearchbutton.h"
3 #include "refinesearchwidget.h"
4 #include "sidebarheader.h"
5 #ifndef Q_WS_X11
6 #include "extra.h"
7 #endif
8
9 namespace The {
10 QHash<QString, QAction*>* globalActions();
11 }
12
13 SidebarWidget::SidebarWidget(QWidget *parent) :
14     QWidget(parent) {
15     playlist = 0;
16
17     QBoxLayout *layout = new QVBoxLayout(this);
18     layout->setSpacing(0);
19     layout->setMargin(0);
20
21     sidebarHeader = new SidebarHeader();
22     layout->addWidget(sidebarHeader);
23
24     // hidden message widget
25     messageLabel = new QLabel(this);
26     messageLabel->setMargin(10);
27     messageLabel->setBackgroundRole(QPalette::ToolTipBase);
28     messageLabel->setForegroundRole(QPalette::ToolTipText);
29     messageLabel->setAutoFillBackground(true);
30     messageLabel->setWordWrap(true);
31     messageLabel->setTextFormat(Qt::RichText);
32     messageLabel->setTextInteractionFlags(
33                 Qt::LinksAccessibleByKeyboard |
34                 Qt::LinksAccessibleByMouse);
35     connect(messageLabel, SIGNAL(linkActivated(QString)),
36             SIGNAL(suggestionAccepted(QString)));
37     messageLabel->hide();
38     layout->addWidget(messageLabel);
39
40     stackedWidget = new QStackedWidget(this);
41     layout->addWidget(stackedWidget);
42
43     setup();
44 }
45
46 void SidebarWidget::setup() {
47     static bool isSetup = false;
48     if (isSetup) return;
49     isSetup = true;
50
51     refineSearchButton = new RefineSearchButton(this);
52     refineSearchButton->setStatusTip(tr("Refine Search")
53                                      + " (" + QKeySequence(Qt::CTRL + Qt::Key_R).toString(QKeySequence::NativeText) + ")");
54     refineSearchButton->hide();
55     connect(refineSearchButton, SIGNAL(clicked()), SLOT(showRefineSearchWidget()));
56
57     refineSearchWidget = new RefineSearchWidget(this);
58     connect(refineSearchWidget, SIGNAL(done()), SLOT(hideRefineSearchWidget()));
59     stackedWidget->addWidget(refineSearchWidget);
60
61     setMouseTracking(true);
62     mouseTimer = new QTimer(this);
63     mouseTimer->setInterval(5000);
64     mouseTimer->setSingleShot(true);
65     connect(mouseTimer, SIGNAL(timeout()), refineSearchButton, SLOT(hide()));
66 }
67
68 void SidebarWidget::setPlaylist(QListView *playlist) {
69     this->playlist = playlist;
70     playlist->installEventFilter(this);
71     stackedWidget->addWidget(playlist);
72 }
73
74 void SidebarWidget::showPlaylist() {
75     setup();
76     stackedWidget->setCurrentWidget(playlist);
77     The::globalActions()->value("refine-search")->setChecked(false);
78 }
79
80 void SidebarWidget::showRefineSearchWidget() {
81     if (!refineSearchWidget->isEnabled()) return;
82     refineSearchWidget->setDirty(false);
83     stackedWidget->setCurrentWidget(refineSearchWidget);
84     refineSearchWidget->setFocus();
85 #ifndef Q_WS_X11
86     Extra::fadeInWidget(playlist, refineSearchWidget);
87 #endif
88     refineSearchButton->hide();
89     The::globalActions()->value("refine-search")->setChecked(true);
90 }
91
92 void SidebarWidget::hideRefineSearchWidget() {
93     stackedWidget->setCurrentWidget(playlist);
94     playlist->setFocus();
95 #ifndef Q_WS_X11
96     Extra::fadeInWidget(refineSearchWidget, playlist);
97 #endif
98     The::globalActions()->value("refine-search")->setChecked(false);
99 }
100
101 void SidebarWidget::toggleRefineSearch(bool show) {
102     if (show) showRefineSearchWidget();
103     else hideRefineSearchWidget();
104 }
105
106 void SidebarWidget::resizeEvent(QResizeEvent *event) {
107     QWidget::resizeEvent(event);
108     refineSearchButton->move(
109                 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
110                 height() - refineSearchButton->minimumHeight());
111 }
112
113 void SidebarWidget::enterEvent(QEvent *) {
114     if (stackedWidget->currentWidget() != refineSearchWidget)
115         showRefineSearchButton();
116 }
117
118 void SidebarWidget::leaveEvent(QEvent *) {
119     refineSearchButton->hide();
120 }
121
122 void SidebarWidget::mouseMoveEvent(QMouseEvent *event) {
123     QWidget::mouseMoveEvent(event);
124     handleMouseMove();
125 }
126
127 bool SidebarWidget::eventFilter(QObject *obj, QEvent *event) {
128     if (event->type() == QEvent::MouseMove) handleMouseMove();
129     return QWidget::eventFilter(obj, event);
130 }
131
132 void SidebarWidget::handleMouseMove() {
133     if (stackedWidget->currentWidget() != refineSearchWidget) {
134         showRefineSearchButton();
135         mouseTimer->start();
136     }
137 }
138
139 void SidebarWidget::showRefineSearchButton() {
140     if (!refineSearchWidget->isEnabled()) return;
141     refineSearchButton->move(
142                 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
143                 height() - refineSearchButton->minimumHeight());
144     refineSearchButton->show();
145 }
146
147 void SidebarWidget::showSuggestions(const QStringList &suggestions) {
148     QString message = tr("Did you mean: %1");
149
150     QString suggestionLinks;
151     foreach (QString suggestion, suggestions) {
152         suggestionLinks += "<a href='" + suggestion + "'>" + suggestion + "</a> ";
153     }
154     message = message.arg(suggestionLinks);
155
156     QString html =
157             "<html>"
158             "<style>"
159             "a { color: palette(text); text-decoration: none; font-weight: bold }"
160             "</style>"
161             "<body>%1</body>"
162             "</html>";
163     html = html.arg(message);
164     messageLabel->setText(html);
165     messageLabel->show();
166 }
167
168 void SidebarWidget::hideSuggestions() {
169     messageLabel->hide();
170     messageLabel->clear();
171 }