2011-04-28 22:48:53 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "didyoumean.h"
|
|
|
|
|
|
|
|
#include <QEvent>
|
2011-04-29 16:01:59 +02:00
|
|
|
#include <QKeyEvent>
|
2011-04-28 22:48:53 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QToolButton>
|
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "core/logging.h"
|
|
|
|
|
2011-04-28 22:48:53 +02:00
|
|
|
const int DidYouMean::kPadding = 3;
|
|
|
|
|
|
|
|
DidYouMean::DidYouMean(QWidget* buddy, QWidget* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QWidget(parent, Qt::ToolTip),
|
|
|
|
buddy_(buddy),
|
|
|
|
close_(new QToolButton(this)),
|
|
|
|
normal_font_(font()),
|
|
|
|
correction_font_(font()),
|
|
|
|
press_enter_font_(font()) {
|
2011-04-29 16:01:59 +02:00
|
|
|
// Close icon
|
2011-04-28 22:48:53 +02:00
|
|
|
close_->setToolTip(tr("Close"));
|
|
|
|
close_->setIcon(QIcon(":/trolltech/styles/macstyle/images/closedock-16.png"));
|
|
|
|
close_->setIconSize(QSize(16, 16));
|
|
|
|
connect(close_, SIGNAL(clicked()), SLOT(hide()));
|
|
|
|
|
2011-04-29 16:01:59 +02:00
|
|
|
// Cursors
|
2011-04-28 22:48:53 +02:00
|
|
|
setCursor(Qt::PointingHandCursor);
|
|
|
|
close_->setCursor(Qt::ArrowCursor);
|
|
|
|
|
2011-04-29 16:01:59 +02:00
|
|
|
// Fonts
|
|
|
|
correction_font_.setBold(true);
|
|
|
|
press_enter_font_.setBold(true);
|
|
|
|
press_enter_font_.setPointSizeF(7.5);
|
|
|
|
|
2011-04-28 22:48:53 +02:00
|
|
|
hide();
|
|
|
|
buddy_->installEventFilter(this);
|
2012-07-01 23:55:54 +02:00
|
|
|
|
|
|
|
// 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_);
|
2011-04-28 22:48:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DidYouMean::eventFilter(QObject* object, QEvent* event) {
|
|
|
|
if (object != buddy_) {
|
|
|
|
return QObject::eventFilter(object, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (event->type()) {
|
|
|
|
case QEvent::Move:
|
|
|
|
case QEvent::Resize:
|
|
|
|
if (isVisible()) {
|
|
|
|
UpdateGeometry();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-04-29 16:01:59 +02:00
|
|
|
case QEvent::KeyPress:
|
|
|
|
if (!isVisible()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (static_cast<QKeyEvent*>(event)->key()) {
|
|
|
|
case Qt::Key_Return:
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
emit Accepted(correction_);
|
2014-02-07 16:34:20 +01:00
|
|
|
// fallthrough
|
2011-04-29 16:01:59 +02:00
|
|
|
case Qt::Key_Escape:
|
|
|
|
hide();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2011-04-29 13:24:58 +02:00
|
|
|
case QEvent::FocusOut:
|
2012-07-01 23:55:54 +02:00
|
|
|
case QEvent::WindowDeactivate:
|
2011-04-29 13:24:58 +02:00
|
|
|
hide();
|
|
|
|
break;
|
|
|
|
|
2011-04-28 22:48:53 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QObject::eventFilter(object, event);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DidYouMean::showEvent(QShowEvent*) { UpdateGeometry(); }
|
2011-04-28 22:48:53 +02:00
|
|
|
|
|
|
|
void DidYouMean::UpdateGeometry() {
|
|
|
|
const int text_height = fontMetrics().height();
|
|
|
|
const int height = text_height + kPadding * 2;
|
|
|
|
|
2012-07-01 23:55:54 +02:00
|
|
|
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_ +
|
2014-02-07 16:34:20 +01:00
|
|
|
QFontMetrics(correction_font_).width(correction_ + " ") +
|
|
|
|
press_enter_size_ + kPadding * 6 + close_->width(),
|
2012-07-01 23:55:54 +02:00
|
|
|
height));
|
2011-04-28 22:48:53 +02:00
|
|
|
|
|
|
|
close_->move(kPadding, kPadding);
|
|
|
|
close_->resize(text_height, text_height);
|
|
|
|
}
|
|
|
|
|
2011-04-29 16:01:59 +02:00
|
|
|
void DidYouMean::paintEvent(QPaintEvent*) {
|
2011-04-28 22:48:53 +02:00
|
|
|
QPainter p(this);
|
|
|
|
|
|
|
|
// Draw the background
|
|
|
|
QColor bg(palette().color(QPalette::Inactive, QPalette::ToolTipBase));
|
2014-02-07 16:34:20 +01:00
|
|
|
p.fillRect(0, 0, width() - 1, height() - 1, bg);
|
2011-04-28 22:48:53 +02:00
|
|
|
|
|
|
|
// Border
|
|
|
|
p.setPen(Qt::black);
|
2014-02-07 16:34:20 +01:00
|
|
|
p.drawRect(0, 0, width() - 1, height() - 1);
|
2011-04-28 22:48:53 +02:00
|
|
|
|
|
|
|
// Text rectangle
|
2014-02-07 16:34:20 +01:00
|
|
|
QRect text_rect(kPadding + close_->width() + kPadding, kPadding,
|
|
|
|
rect().width() - kPadding, rect().height() - kPadding);
|
2011-04-28 22:48:53 +02:00
|
|
|
|
2011-04-29 16:01:59 +02:00
|
|
|
// Text
|
|
|
|
p.setFont(normal_font_);
|
2012-07-01 23:55:54 +02:00
|
|
|
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, did_you_mean_);
|
|
|
|
text_rect.setLeft(text_rect.left() + p.fontMetrics().width(did_you_mean_));
|
2011-04-28 22:48:53 +02:00
|
|
|
|
2011-04-29 16:01:59 +02:00
|
|
|
p.setFont(correction_font_);
|
|
|
|
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, correction_);
|
2014-02-07 16:34:20 +01:00
|
|
|
text_rect.setLeft(text_rect.left() +
|
|
|
|
p.fontMetrics().width(correction_ + " "));
|
2011-04-28 22:48:53 +02:00
|
|
|
|
2011-04-29 16:01:59 +02:00
|
|
|
p.setPen(palette().color(QPalette::Disabled, QPalette::Text));
|
|
|
|
p.setFont(press_enter_font_);
|
2012-07-01 23:55:54 +02:00
|
|
|
p.drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, press_enter_);
|
2011-04-28 22:48:53 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 16:01:59 +02:00
|
|
|
void DidYouMean::SetCorrection(const QString& correction) {
|
|
|
|
correction_ = correction;
|
2012-07-01 23:55:54 +02:00
|
|
|
UpdateGeometry();
|
2011-04-28 22:48:53 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2011-04-29 16:01:59 +02:00
|
|
|
void DidYouMean::Show(const QString& correction) {
|
|
|
|
SetCorrection(correction);
|
2011-04-28 22:48:53 +02:00
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DidYouMean::mouseReleaseEvent(QMouseEvent* e) {
|
2011-04-29 16:01:59 +02:00
|
|
|
emit Accepted(correction_);
|
2011-04-28 22:48:53 +02:00
|
|
|
hide();
|
|
|
|
}
|