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 "tooltipactionwidget.h"
19
#include "core/logging.h"
22
#include <QMouseEvent>
25
const int TooltipActionWidget::kBorder = 16;
26
const int TooltipActionWidget::kSpacing = 6;
27
const int TooltipActionWidget::kTopPadding = 3;
28
const int TooltipActionWidget::kFadeDurationMsec = 200;
30
TooltipActionWidget::TooltipActionWidget(QWidget* parent)
32
kTextHeight(fontMetrics().height()),
36
setMouseTracking(true);
39
void TooltipActionWidget::SetActions(QList<QAction*> actions) {
41
action_opacities_.clear();
43
int h = kTopPadding + kTextHeight * actions.count();
45
description_width_ = 0;
47
foreach (const QAction* action, actions) {
50
fontMetrics().width(action->shortcut().toString(QKeySequence::NativeText)));
52
qMax(description_width_, fontMetrics().width(action->text()));
54
QTimeLine* timeline = new QTimeLine(kFadeDurationMsec, this);
55
connect(timeline, SIGNAL(valueChanged(qreal)), SLOT(update()));
56
action_opacities_ << timeline;
60
kBorder*2 + shortcut_width_ + kSpacing + description_width_, h);
66
void TooltipActionWidget::paintEvent(QPaintEvent*) {
70
p.setPen(palette().color(QPalette::Text));
72
for (int i=0 ; i<actions_.count() ; ++i) {
73
const QAction* action = actions_[i];
74
const QTimeLine* timeline = action_opacities_[i];
76
const QRect shortcut_rect(kBorder, y, shortcut_width_, kTextHeight);
77
const QRect description_rect(shortcut_rect.right() + kSpacing, y,
78
description_width_, kTextHeight);
80
const qreal shortcut_opacity = 0.4 + 0.3 * timeline->currentValue();
81
const qreal description_opacity = 0.7 + 0.3 * timeline->currentValue();
83
p.setOpacity(shortcut_opacity);
84
p.drawText(shortcut_rect, Qt::AlignRight | Qt::AlignVCenter,
85
action->shortcut().toString(QKeySequence::NativeText));
87
p.setOpacity(description_opacity);
88
p.drawText(description_rect, Qt::AlignVCenter, action->text());
94
int TooltipActionWidget::ActionAt(const QPoint& pos) const {
95
return (pos.y() - kTopPadding) / kTextHeight;
98
void TooltipActionWidget::mouseMoveEvent(QMouseEvent* e) {
99
const int action = ActionAt(e->pos());
101
for (int i=0 ; i<actions_.count() ; ++i) {
103
StartAnimation(i, QTimeLine::Forward);
105
StartAnimation(i, QTimeLine::Backward);
110
void TooltipActionWidget::leaveEvent(QEvent* e) {
111
for (int i=0 ; i<actions_.count() ; ++i) {
112
StartAnimation(i, QTimeLine::Backward);
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)
125
void TooltipActionWidget::mousePressEvent(QMouseEvent* e) {
126
const int action = ActionAt(e->pos());
127
if (action >= 0 && action < actions_.count()) {
128
actions_[action]->trigger();