~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/widgets/didyoumean.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2010, David Sansome <me@davidsansome.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#include "didyoumean.h"
 
19
 
 
20
#include <QEvent>
 
21
#include <QKeyEvent>
 
22
#include <QPainter>
 
23
#include <QToolButton>
 
24
 
 
25
const int DidYouMean::kPadding = 3;
 
26
 
 
27
DidYouMean::DidYouMean(QWidget* buddy, QWidget* parent)
 
28
  : QWidget(parent),
 
29
    buddy_(buddy),
 
30
    close_(new QToolButton(this)),
 
31
    normal_font_(font()),
 
32
    correction_font_(font()),
 
33
    press_enter_font_(font()) {
 
34
  // Close icon
 
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()));
 
39
 
 
40
  // Cursors
 
41
  setCursor(Qt::PointingHandCursor);
 
42
  close_->setCursor(Qt::ArrowCursor);
 
43
 
 
44
  // Fonts
 
45
  correction_font_.setBold(true);
 
46
  press_enter_font_.setBold(true);
 
47
  press_enter_font_.setPointSizeF(7.5);
 
48
 
 
49
  hide();
 
50
  buddy_->installEventFilter(this);
 
51
}
 
52
 
 
53
bool DidYouMean::eventFilter(QObject* object, QEvent* event) {
 
54
  if (object != buddy_) {
 
55
    return QObject::eventFilter(object, event);
 
56
  }
 
57
 
 
58
  switch (event->type()) {
 
59
    case QEvent::Move:
 
60
    case QEvent::Resize:
 
61
      if (isVisible()) {
 
62
        UpdateGeometry();
 
63
      }
 
64
      break;
 
65
 
 
66
    case QEvent::KeyPress:
 
67
      if (!isVisible()) {
 
68
        break;
 
69
      }
 
70
 
 
71
      switch (static_cast<QKeyEvent*>(event)->key()) {
 
72
        case Qt::Key_Return:
 
73
        case Qt::Key_Enter:
 
74
          emit Accepted(correction_);
 
75
          // fallthrough
 
76
        case Qt::Key_Escape:
 
77
          hide();
 
78
          return true;
 
79
 
 
80
        default:
 
81
          break;
 
82
      }
 
83
 
 
84
      break;
 
85
 
 
86
    case QEvent::FocusOut:
 
87
      hide();
 
88
      break;
 
89
 
 
90
    default:
 
91
      break;
 
92
  }
 
93
 
 
94
  return QObject::eventFilter(object, event);
 
95
}
 
96
 
 
97
void DidYouMean::showEvent(QShowEvent*) {
 
98
  UpdateGeometry();
 
99
}
 
100
 
 
101
void DidYouMean::UpdateGeometry() {
 
102
  const int text_height = fontMetrics().height();
 
103
  const int height = text_height + kPadding * 2;
 
104
 
 
105
  move(buddy_->mapTo(parentWidget(), buddy_->rect().bottomLeft()));
 
106
  resize(QSize(buddy_->width(), height));
 
107
 
 
108
  close_->move(kPadding, kPadding);
 
109
  close_->resize(text_height, text_height);
 
110
}
 
111
 
 
112
void DidYouMean::paintEvent(QPaintEvent*) {
 
113
  QPainter p(this);
 
114
 
 
115
  // Draw the background
 
116
  QColor bg(palette().color(QPalette::Inactive, QPalette::ToolTipBase));
 
117
  p.fillRect(0, 0, width()-1, height()-1, bg);
 
118
 
 
119
  // Border
 
120
  p.setPen(Qt::black);
 
121
  p.drawRect(0, 0, width()-1, height()-1);
 
122
 
 
123
  // Text rectangle
 
124
  QRect text_rect(kPadding + close_->width() + kPadding,
 
125
                  kPadding,
 
126
                  rect().width() - kPadding,
 
127
                  rect().height() - kPadding);
 
128
  const QString did_you_mean(tr("Did you mean") + ":  ");
 
129
 
 
130
  // Text
 
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));
 
134
 
 
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_ + "  "));
 
138
 
 
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") + ")");
 
142
}
 
143
 
 
144
void DidYouMean::SetCorrection(const QString& correction) {
 
145
  correction_ = correction;
 
146
  update();
 
147
}
 
148
 
 
149
void DidYouMean::Show(const QString& correction) {
 
150
  SetCorrection(correction);
 
151
  show();
 
152
}
 
153
 
 
154
void DidYouMean::mouseReleaseEvent(QMouseEvent* e) {
 
155
  emit Accepted(correction_);
 
156
  hide();
 
157
}