Add a "Switch provider" action when there is more than one provider for a search result

This commit is contained in:
David Sansome 2011-09-19 00:29:09 +01:00
parent 334ecd3c38
commit 847d90d117
4 changed files with 70 additions and 9 deletions

View File

@ -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<QAction*> 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<QKeyEvent*>(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 ; i<result_buttons_.count() ; ++i) {
if (result_buttons_[i]->isChecked()) {
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);
}

View File

@ -22,6 +22,7 @@
#include <QWidget>
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<QAction*> 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<QAbstractButton*> result_buttons_;
};
#endif // GLOBALSEARCHTOOLTIP_H

View File

@ -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;

View File

@ -20,9 +20,9 @@
#include "searchprovider.h"
#include <QWidget>
#include <QAbstractButton>
class TooltipResultWidget : public QWidget {
class TooltipResultWidget : public QAbstractButton {
Q_OBJECT
public: