Always display "did you mean" widget in top of all windows.

Also, update its width to the text it should displayed
This commit is contained in:
Arnaud Bienner 2012-07-01 23:55:54 +02:00
parent 905ef90882
commit 124d4e6a59
4 changed files with 39 additions and 10 deletions

View File

@ -45,9 +45,6 @@ SearchBoxWidget::SearchBoxWidget(InternetService* service)
ui_->filter->setPlaceholderText(QString("Search on %1").arg(service_->name()));
connect(ui_->filter, SIGNAL(textChanged(QString)), SIGNAL(TextChanged(QString)));
// FIXME: the "Did you mean" suggestion is displayed above the search box,
// but below the internet services tree, which makes it fairly unusuable for
// now :(
did_you_mean_ = new DidYouMean(ui_->filter, this);
connect(did_you_mean_, SIGNAL(Accepted(QString)),
ui_->filter, SLOT(setText(QString)));

View File

@ -609,6 +609,9 @@ void SpotifyService::SearchResults(const pb::spotify::SearchResponse& response)
qLog(Debug) << "Did you mean suggestion: " << did_you_mean_suggestion;
if (!did_you_mean_suggestion.isEmpty()) {
search_box_->did_you_mean()->Show(did_you_mean_suggestion);
} else {
// In case something else was previously displayed
search_box_->did_you_mean()->hide();
}
QModelIndex index = model()->merged_model()->mapFromSource(search_->index());

View File

@ -17,6 +17,8 @@
#include "didyoumean.h"
#include "core/logging.h"
#include <QEvent>
#include <QKeyEvent>
#include <QPainter>
@ -25,7 +27,7 @@
const int DidYouMean::kPadding = 3;
DidYouMean::DidYouMean(QWidget* buddy, QWidget* parent)
: QWidget(parent),
: QWidget(parent, Qt::ToolTip),
buddy_(buddy),
close_(new QToolButton(this)),
normal_font_(font()),
@ -48,6 +50,14 @@ DidYouMean::DidYouMean(QWidget* buddy, QWidget* parent)
hide();
buddy_->installEventFilter(this);
// Texts
did_you_mean_ = tr("Did you mean") + ": ";
press_enter_ = "(" + tr("press enter") + ")";
// Texts' sizes
did_you_mean_size_ = QFontMetrics(normal_font_).width(did_you_mean_);
press_enter_size_ = QFontMetrics(press_enter_font_).width(press_enter_);
}
bool DidYouMean::eventFilter(QObject* object, QEvent* event) {
@ -83,7 +93,10 @@ bool DidYouMean::eventFilter(QObject* object, QEvent* event) {
break;
// FIXME: FocusOut doesn't work with QSearchField, which is used as buddy
// in Spotify Service :(
case QEvent::FocusOut:
case QEvent::WindowDeactivate:
hide();
break;
@ -102,8 +115,16 @@ void DidYouMean::UpdateGeometry() {
const int text_height = fontMetrics().height();
const int height = text_height + kPadding * 2;
move(buddy_->mapTo(parentWidget(), buddy_->rect().bottomLeft()));
resize(QSize(buddy_->width(), height));
move(buddy_->mapToGlobal(buddy_->rect().bottomLeft()));
// Resize to len(text to display) + total number of padding added +
// size(close button), so the "Did you mean" widget is always fully displayed
resize(QSize(did_you_mean_size_ +
QFontMetrics(correction_font_).width(correction_ + " ") +
press_enter_size_ +
kPadding * 6 +
close_->width(),
height));
close_->move(kPadding, kPadding);
close_->resize(text_height, text_height);
@ -125,12 +146,11 @@ void DidYouMean::paintEvent(QPaintEvent*) {
kPadding,
rect().width() - kPadding,
rect().height() - kPadding);
const QString did_you_mean(tr("Did you mean") + ": ");
// Text
p.setFont(normal_font_);
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, did_you_mean);
text_rect.setLeft(text_rect.left() + p.fontMetrics().width(did_you_mean));
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, did_you_mean_);
text_rect.setLeft(text_rect.left() + p.fontMetrics().width(did_you_mean_));
p.setFont(correction_font_);
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, correction_);
@ -138,11 +158,12 @@ void DidYouMean::paintEvent(QPaintEvent*) {
p.setPen(palette().color(QPalette::Disabled, QPalette::Text));
p.setFont(press_enter_font_);
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, "(" + tr("press enter") + ")");
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, press_enter_);
}
void DidYouMean::SetCorrection(const QString& correction) {
correction_ = correction;
UpdateGeometry();
update();
}

View File

@ -55,6 +55,14 @@ private:
QFont normal_font_;
QFont correction_font_;
QFont press_enter_font_;
QString did_you_mean_;
QString press_enter_;
// Size of the text to display, according to QFont above
// Stored here to avoid to recompute them each time
int did_you_mean_size_;
int press_enter_size_;
};
#endif // DIDYOUMEAN_H