1 #include "sidebarwidget.h"
2 #include "refinesearchbutton.h"
3 #include "refinesearchwidget.h"
9 QMap<QString, QAction*>* globalActions();
12 SidebarWidget::SidebarWidget(QWidget *parent) :
16 QBoxLayout *layout = new QVBoxLayout(this);
17 layout->setSpacing(1);
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)));
34 layout->addWidget(messageLabel);
36 stackedWidget = new QStackedWidget(this);
37 layout->addWidget(stackedWidget);
42 void SidebarWidget::setup() {
43 static bool isSetup = false;
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()));
53 refineSearchWidget = new RefineSearchWidget(this);
54 connect(refineSearchWidget, SIGNAL(done()), SLOT(hideRefineSearchWidget()));
55 stackedWidget->addWidget(refineSearchWidget);
57 setMouseTracking(true);
58 mouseTimer = new QTimer(this);
59 mouseTimer->setInterval(5000);
60 mouseTimer->setSingleShot(true);
61 connect(mouseTimer, SIGNAL(timeout()), refineSearchButton, SLOT(hide()));
64 void SidebarWidget::setPlaylist(QListView *playlist) {
65 this->playlist = playlist;
66 playlist->installEventFilter(this);
67 stackedWidget->addWidget(playlist);
70 void SidebarWidget::showPlaylist() {
72 stackedWidget->setCurrentWidget(playlist);
75 void SidebarWidget::showRefineSearchWidget() {
76 refineSearchWidget->setDirty(false);
77 stackedWidget->setCurrentWidget(refineSearchWidget);
78 refineSearchWidget->setFocus();
80 Extra::fadeInWidget(playlist, refineSearchWidget);
82 refineSearchButton->hide();
83 The::globalActions()->value("refine-search")->setChecked(true);
86 void SidebarWidget::hideRefineSearchWidget() {
87 stackedWidget->setCurrentWidget(playlist);
90 Extra::fadeInWidget(refineSearchWidget, playlist);
92 The::globalActions()->value("refine-search")->setChecked(false);
95 void SidebarWidget::toggleRefineSearch(bool show) {
96 if (show) showRefineSearchWidget();
97 else hideRefineSearchWidget();
100 void SidebarWidget::resizeEvent(QResizeEvent *event) {
101 QWidget::resizeEvent(event);
102 refineSearchButton->move(
103 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
104 height() - refineSearchButton->minimumHeight());
107 void SidebarWidget::enterEvent(QEvent *) {
108 if (stackedWidget->currentWidget() != refineSearchWidget)
109 showRefineSearchButton();
112 void SidebarWidget::leaveEvent(QEvent *) {
113 refineSearchButton->hide();
116 void SidebarWidget::mouseMoveEvent(QMouseEvent *event) {
117 QWidget::mouseMoveEvent(event);
121 bool SidebarWidget::eventFilter(QObject *obj, QEvent *event) {
122 if (event->type() == QEvent::MouseMove) handleMouseMove();
123 return QWidget::eventFilter(obj, event);
126 void SidebarWidget::handleMouseMove() {
127 if (stackedWidget->currentWidget() != refineSearchWidget) {
128 showRefineSearchButton();
133 void SidebarWidget::showRefineSearchButton() {
134 refineSearchButton->move(
135 playlist->viewport()->width() - refineSearchButton->minimumWidth(),
136 height() - refineSearchButton->minimumHeight());
137 refineSearchButton->show();
140 void SidebarWidget::showSuggestions(const QStringList &suggestions) {
141 QString message = tr("Did you mean: %1");
143 QString suggestionLinks;
144 foreach (QString suggestion, suggestions) {
145 suggestionLinks += "<a href='" + suggestion + "'>" + suggestion + "</a> ";
147 message = message.arg(suggestionLinks);
152 "a { color: palette(text); text-decoration: none; font-weight: bold }"
156 html = html.arg(message);
157 messageLabel->setText(html);
158 messageLabel->show();
161 void SidebarWidget::hideSuggestions() {
162 messageLabel->hide();
163 messageLabel->clear();