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

« back to all changes in this revision

Viewing changes to src/globalsearch/tooltipactionwidget.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 "tooltipactionwidget.h"
 
19
#include "core/logging.h"
 
20
 
 
21
#include <QAction>
 
22
#include <QMouseEvent>
 
23
#include <QPainter>
 
24
 
 
25
const int TooltipActionWidget::kBorder = 16;
 
26
const int TooltipActionWidget::kSpacing = 6;
 
27
const int TooltipActionWidget::kTopPadding = 3;
 
28
const int TooltipActionWidget::kFadeDurationMsec = 200;
 
29
 
 
30
TooltipActionWidget::TooltipActionWidget(QWidget* parent)
 
31
  : QWidget(parent),
 
32
    kTextHeight(fontMetrics().height()),
 
33
    shortcut_width_(0),
 
34
    description_width_(0)
 
35
{
 
36
  setMouseTracking(true);
 
37
}
 
38
 
 
39
void TooltipActionWidget::SetActions(QList<QAction*> actions) {
 
40
  actions_ = actions;
 
41
  action_opacities_.clear();
 
42
 
 
43
  int h = kTopPadding + kTextHeight * actions.count();
 
44
  shortcut_width_ = 0;
 
45
  description_width_ = 0;
 
46
 
 
47
  foreach (const QAction* action, actions) {
 
48
    shortcut_width_ =
 
49
        qMax(shortcut_width_,
 
50
             fontMetrics().width(action->shortcut().toString(QKeySequence::NativeText)));
 
51
    description_width_ =
 
52
        qMax(description_width_, fontMetrics().width(action->text()));
 
53
 
 
54
    QTimeLine* timeline = new QTimeLine(kFadeDurationMsec, this);
 
55
    connect(timeline, SIGNAL(valueChanged(qreal)), SLOT(update()));
 
56
    action_opacities_ << timeline;
 
57
  }
 
58
 
 
59
  size_hint_ = QSize(
 
60
      kBorder*2 + shortcut_width_ + kSpacing + description_width_, h);
 
61
 
 
62
  updateGeometry();
 
63
  update();
 
64
}
 
65
 
 
66
void TooltipActionWidget::paintEvent(QPaintEvent*) {
 
67
  int y = kTopPadding;
 
68
 
 
69
  QPainter p(this);
 
70
  p.setPen(palette().color(QPalette::Text));
 
71
 
 
72
  for (int i=0 ; i<actions_.count() ; ++i) {
 
73
    const QAction* action = actions_[i];
 
74
    const QTimeLine* timeline = action_opacities_[i];
 
75
 
 
76
    const QRect shortcut_rect(kBorder, y, shortcut_width_, kTextHeight);
 
77
    const QRect description_rect(shortcut_rect.right() + kSpacing, y,
 
78
                                 description_width_, kTextHeight);
 
79
 
 
80
    const qreal shortcut_opacity = 0.4 + 0.3 * timeline->currentValue();
 
81
    const qreal description_opacity = 0.7 + 0.3 * timeline->currentValue();
 
82
 
 
83
    p.setOpacity(shortcut_opacity);
 
84
    p.drawText(shortcut_rect, Qt::AlignRight | Qt::AlignVCenter,
 
85
               action->shortcut().toString(QKeySequence::NativeText));
 
86
 
 
87
    p.setOpacity(description_opacity);
 
88
    p.drawText(description_rect, Qt::AlignVCenter, action->text());
 
89
 
 
90
    y += kTextHeight;
 
91
  }
 
92
}
 
93
 
 
94
int TooltipActionWidget::ActionAt(const QPoint& pos) const {
 
95
  return (pos.y() - kTopPadding) / kTextHeight;
 
96
}
 
97
 
 
98
void TooltipActionWidget::mouseMoveEvent(QMouseEvent* e) {
 
99
  const int action = ActionAt(e->pos());
 
100
 
 
101
  for (int i=0 ; i<actions_.count() ; ++i) {
 
102
    if (i == action) {
 
103
      StartAnimation(i, QTimeLine::Forward);
 
104
    } else {
 
105
      StartAnimation(i, QTimeLine::Backward);
 
106
    }
 
107
  }
 
108
}
 
109
 
 
110
void TooltipActionWidget::leaveEvent(QEvent* e) {
 
111
  for (int i=0 ; i<actions_.count() ; ++i) {
 
112
    StartAnimation(i, QTimeLine::Backward);
 
113
  }
 
114
}
 
115
 
 
116
void TooltipActionWidget::StartAnimation(int i, QTimeLine::Direction direction) {
 
117
  QTimeLine* timeline = action_opacities_[i];
 
118
  if (timeline->direction() != direction) {
 
119
    timeline->setDirection(direction);
 
120
    if (timeline->state() != QTimeLine::Running)
 
121
      timeline->resume();
 
122
  }
 
123
}
 
124
 
 
125
void TooltipActionWidget::mousePressEvent(QMouseEvent* e) {
 
126
  const int action = ActionAt(e->pos());
 
127
  if (action >= 0 && action < actions_.count()) {
 
128
    actions_[action]->trigger();
 
129
  }
 
130
}