1
/* This file is part of Clementine.
2
Copyright 2010, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "didyoumean.h"
23
#include <QToolButton>
25
const int DidYouMean::kPadding = 3;
27
DidYouMean::DidYouMean(QWidget* buddy, QWidget* parent)
30
close_(new QToolButton(this)),
32
correction_font_(font()),
33
press_enter_font_(font()) {
35
close_->setToolTip(tr("Close"));
36
close_->setIcon(QIcon(":/trolltech/styles/macstyle/images/closedock-16.png"));
37
close_->setIconSize(QSize(16, 16));
38
connect(close_, SIGNAL(clicked()), SLOT(hide()));
41
setCursor(Qt::PointingHandCursor);
42
close_->setCursor(Qt::ArrowCursor);
45
correction_font_.setBold(true);
46
press_enter_font_.setBold(true);
47
press_enter_font_.setPointSizeF(7.5);
50
buddy_->installEventFilter(this);
53
bool DidYouMean::eventFilter(QObject* object, QEvent* event) {
54
if (object != buddy_) {
55
return QObject::eventFilter(object, event);
58
switch (event->type()) {
66
case QEvent::KeyPress:
71
switch (static_cast<QKeyEvent*>(event)->key()) {
74
emit Accepted(correction_);
86
case QEvent::FocusOut:
94
return QObject::eventFilter(object, event);
97
void DidYouMean::showEvent(QShowEvent*) {
101
void DidYouMean::UpdateGeometry() {
102
const int text_height = fontMetrics().height();
103
const int height = text_height + kPadding * 2;
105
move(buddy_->mapTo(parentWidget(), buddy_->rect().bottomLeft()));
106
resize(QSize(buddy_->width(), height));
108
close_->move(kPadding, kPadding);
109
close_->resize(text_height, text_height);
112
void DidYouMean::paintEvent(QPaintEvent*) {
115
// Draw the background
116
QColor bg(palette().color(QPalette::Inactive, QPalette::ToolTipBase));
117
p.fillRect(0, 0, width()-1, height()-1, bg);
121
p.drawRect(0, 0, width()-1, height()-1);
124
QRect text_rect(kPadding + close_->width() + kPadding,
126
rect().width() - kPadding,
127
rect().height() - kPadding);
128
const QString did_you_mean(tr("Did you mean") + ": ");
131
p.setFont(normal_font_);
132
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, did_you_mean);
133
text_rect.setLeft(text_rect.left() + p.fontMetrics().width(did_you_mean));
135
p.setFont(correction_font_);
136
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, correction_);
137
text_rect.setLeft(text_rect.left() + p.fontMetrics().width(correction_ + " "));
139
p.setPen(palette().color(QPalette::Disabled, QPalette::Text));
140
p.setFont(press_enter_font_);
141
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, "(" + tr("press enter") + ")");
144
void DidYouMean::SetCorrection(const QString& correction) {
145
correction_ = correction;
149
void DidYouMean::Show(const QString& correction) {
150
SetCorrection(correction);
154
void DidYouMean::mouseReleaseEvent(QMouseEvent* e) {
155
emit Accepted(correction_);