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