diff --git a/src/globalsearch/globalsearchtooltip.cpp b/src/globalsearch/globalsearchtooltip.cpp index 7d2fcb1b2..c298649fe 100644 --- a/src/globalsearch/globalsearchtooltip.cpp +++ b/src/globalsearch/globalsearchtooltip.cpp @@ -44,6 +44,10 @@ GlobalSearchTooltip::GlobalSearchTooltip(QWidget* event_target) setFocusPolicy(Qt::NoFocus); setAttribute(Qt::WA_OpaquePaintEvent); setAttribute(Qt::WA_TranslucentBackground); + + switch_action_ = new QAction(tr("Switch provider"), this); + switch_action_->setShortcut(QKeySequence(Qt::Key_Tab)); + connect(switch_action_, SIGNAL(triggered()), SLOT(SwitchProvider())); } void GlobalSearchTooltip::SetResults(const SearchProvider::ResultList& results) { @@ -51,6 +55,7 @@ void GlobalSearchTooltip::SetResults(const SearchProvider::ResultList& results) qDeleteAll(widgets_); widgets_.clear(); + result_buttons_.clear(); // Using a QVBoxLayout here made some weird flickering that I couldn't figure // out how to fix, so do layout manually. @@ -59,11 +64,21 @@ void GlobalSearchTooltip::SetResults(const SearchProvider::ResultList& results) // Add a widget for each result foreach (const SearchProvider::Result& result, results) { - AddWidget(new TooltipResultWidget(result, this), &w, &y); + TooltipResultWidget* widget = new TooltipResultWidget(result, this); + if (widgets_.isEmpty()) { + // If this is the first widget then mark it as selected + widget->setChecked(true); + } + + AddWidget(widget, &w, &y); + result_buttons_ << widget; } // Add the action widget QList actions; + if (results_.count() > 1) { + actions.append(switch_action_); + } actions.append(common_actions_); action_widget_ = new TooltipActionWidget(this); @@ -113,7 +128,16 @@ void GlobalSearchTooltip::ShowAt(const QPoint& pointing_to) { bool GlobalSearchTooltip::event(QEvent* e) { switch (e->type()) { - case QEvent::KeyPress: + case QEvent::KeyPress: { + QKeyEvent* ke = static_cast(e); + if (ke->key() == Qt::Key_Tab && ke->modifiers() == Qt::NoModifier) { + SwitchProvider(); + e->accept(); + return true; + } + + // fallthrough + } case QEvent::KeyRelease: case QEvent::InputMethod: case QEvent::Shortcut: @@ -199,3 +223,25 @@ void GlobalSearchTooltip::paintEvent(QPaintEvent*) { p.setPen(inner_color); p.drawPolygon(arrow); } + +void GlobalSearchTooltip::SwitchProvider() { + // Find which one was checked before. + int old_index = -1; + for (int i=0 ; iisChecked()) { + old_index = i; + break; + } + } + + if (old_index == -1) + return; + + // Check the new one. The auto exclusive group will take care of unchecking + // the old one. + const int new_index = (old_index + 1) % result_buttons_.count(); + result_buttons_[new_index]->setChecked(true); + + emit ActiveResultChanged(new_index); +} + diff --git a/src/globalsearch/globalsearchtooltip.h b/src/globalsearch/globalsearchtooltip.h index 685f5a034..95f153092 100644 --- a/src/globalsearch/globalsearchtooltip.h +++ b/src/globalsearch/globalsearchtooltip.h @@ -22,6 +22,7 @@ #include +class QAbstractButton; class QDesktopWidget; class TooltipActionWidget; @@ -46,9 +47,15 @@ public: bool event(QEvent* e); +signals: + void ActiveResultChanged(int new_index); + protected: void paintEvent(QPaintEvent*); +private slots: + void SwitchProvider(); + private: void AddWidget(QWidget* widget, int* w, int* y); @@ -57,6 +64,8 @@ private: TooltipActionWidget* action_widget_; QList common_actions_; + QAction* switch_action_; + SearchProvider::ResultList results_; qreal arrow_offset_; QRect inner_rect_; @@ -64,6 +73,7 @@ private: QWidget* event_target_; QWidgetList widgets_; + QList result_buttons_; }; #endif // GLOBALSEARCHTOOLTIP_H diff --git a/src/globalsearch/tooltipresultwidget.cpp b/src/globalsearch/tooltipresultwidget.cpp index 5e7302f63..71743c121 100644 --- a/src/globalsearch/tooltipresultwidget.cpp +++ b/src/globalsearch/tooltipresultwidget.cpp @@ -28,7 +28,7 @@ const int TooltipResultWidget::kIconSize = 16; TooltipResultWidget::TooltipResultWidget(const SearchProvider::Result& result, QWidget* parent) - : QWidget(parent), + : QAbstractButton(parent), result_(result), kTextHeight(fontMetrics().height()), kTrackNoWidth(fontMetrics().width("0000")), @@ -39,6 +39,9 @@ TooltipResultWidget::TooltipResultWidget(const SearchProvider::Result& result, bold_metrics_ = QFontMetrics(bold_font_); size_hint_ = CalculateSizeHint(); + + setCheckable(true); + setAutoExclusive(true); } QSize TooltipResultWidget::sizeHint() const { @@ -86,11 +89,11 @@ QString TooltipResultWidget::TitleText() const { void TooltipResultWidget::paintEvent(QPaintEvent*) { QPainter p(this); - p.setPen(palette().color(QPalette::Text)); + const QColor text_color = palette().color(QPalette::Text); - const qreal line_opacity = 0.4; - const qreal track_opacity = 0.6; - const qreal text_opacity = 0.9; + const qreal line_opacity = 0.1 + (isChecked() ? 0.2 : 0.0); + const qreal track_opacity = 0.1 + (isChecked() ? 0.5 : 0.0); + const qreal text_opacity = 0.4 + (isChecked() ? 0.5 : 0.0); int y = kSpacing; @@ -98,6 +101,7 @@ void TooltipResultWidget::paintEvent(QPaintEvent*) { QRect text_rect(kBorder + kTrackNoWidth + kTrackNumSpacing, y, width() - kBorder*2 - kTrackNoWidth - kTrackNumSpacing, kIconSize); p.setFont(bold_font_); + p.setPen(text_color); p.setOpacity(text_opacity); p.drawText(text_rect, Qt::AlignVCenter, TitleText()); @@ -109,6 +113,7 @@ void TooltipResultWidget::paintEvent(QPaintEvent*) { // Line y += kIconSize + kSpacing; p.setOpacity(line_opacity); + p.setPen(text_color); p.drawLine(0, y, width(), y); y += kLineHeight; diff --git a/src/globalsearch/tooltipresultwidget.h b/src/globalsearch/tooltipresultwidget.h index a7c7b66ff..dd48aa5cb 100644 --- a/src/globalsearch/tooltipresultwidget.h +++ b/src/globalsearch/tooltipresultwidget.h @@ -20,9 +20,9 @@ #include "searchprovider.h" -#include +#include -class TooltipResultWidget : public QWidget { +class TooltipResultWidget : public QAbstractButton { Q_OBJECT public: