2
Copyright (c) 2007 Riccardo Iaconelli <ruphy@fsfe.org>
3
(c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
15
#include <math.h> // fabs
19
Button::Button(WelcomeScreen* parent, const QIcon& icon,
20
const QFont& font, const QString& text)
22
, KGameCanvasPixmap(parent)
26
, m_fixed_width(false)
29
, m_brightness(BRIGHTNESS_NORMAL)
44
void Button::setWidth(int width)
46
m_fixed_width = width != -1;
47
m_size.setWidth(width);
51
void Button::computeSize()
53
QFontMetrics fm(m_font);
54
m_text_width = fm.width(m_text);
60
m_size = QSize(m_text_width, h);
61
m_size.rwidth() += 10 + 32 + 10 + 10;
67
m_size.rheight() += 10 + 10;
70
void Button::repaint()
72
QImage tmp(m_size, QImage::Format_ARGB32_Premultiplied);
76
p.setRenderHint(QPainter::Antialiasing);
77
QPen pen(QColor(200, 200, 220, 255));
80
p.setBrush(QBrush(QColor(
81
static_cast<int>(m_brightness),
82
static_cast<int>(m_brightness),
83
static_cast<int>(m_brightness), 100)));
84
p.drawRoundRect(1, 1, m_size.width() - 2, m_size.height() -2,
85
2000 / m_size.width(), 2000 / m_size.height());
87
m_size.height() / 2 - 16,
90
m_icon.pixmap(32, 32));
94
p.drawText(textPos(), m_text);
98
setPixmap(QPixmap::fromImage(tmp));
102
QSize Button::size() const
107
QPoint Button::textPos() const
109
return QPoint(32 + 10 + (m_size.width() - 32 - 10 - m_text_width) / 2,
110
m_size.height() / 2 + 6);
113
void Button::onMousePress(const QPoint&)
115
if (!m_editor && !m_down) {
118
m_animation->abort();
120
m_brightness = BRIGHTNESS_DOWN;
126
void Button::onMouseRelease(const QPoint&)
128
if (!m_editor && m_down) {
131
m_animation->abort();
133
m_brightness = BRIGHTNESS_NORMAL;
138
void Button::onMouseMove(const QPoint&)
140
if (!m_editor && !m_hover) {
145
m_animation->abort();
147
m_brightness = BRIGHTNESS_HOVER;
149
else if (m_animation) {
150
m_animation->setBrightness(BRIGHTNESS_HOVER);
153
m_animation = new ButtonAnimation(this, BRIGHTNESS_HOVER);
154
Animator::instance()->add(m_animation);
161
void Button::onMouseLeave()
168
m_animation->abort();
170
m_brightness = BRIGHTNESS_NORMAL;
172
else if (m_animation) {
173
m_animation->setBrightness(BRIGHTNESS_NORMAL);
176
m_animation = new ButtonAnimation(this, BRIGHTNESS_NORMAL);
177
Animator::instance()->add(m_animation);
184
bool Button::onClicked()
187
kDebug() << "clicked";
196
void Button::setText(const QString& text)
202
double Button::brightness() const
207
void Button::setBrightness(double value)
209
m_brightness = value;
213
KGameCanvasPixmap* Button::extractIcon()
215
KGameCanvasPixmap* res = new KGameCanvasPixmap(
216
KGameCanvasPixmap::canvas());
217
res->moveTo(pos() + QPoint(10, 10));
218
res->setPixmap(m_icon.pixmap(32, 32));
226
void Button::setEditor(EditorFactory& factory)
228
// remove old editor if existent
231
// create a new editor
232
m_editor = factory.createEditor(topLevelCanvas());
235
m_size.setWidth(32 * 6);
240
m_editor->setFocus();
243
void Button::updateEditor()
247
m_editor->setGeometry(QRect(absolutePosition() + QPoint(textPos().x(), SPACE),
248
QSize(m_size.width() - 10 - 10 - 32 - 10, m_size.height() - SPACE * 2)));
252
void Button::removeEditor()
255
m_editor->deleteLater();
265
double ButtonAnimation::m_speed = 0.46;
267
ButtonAnimation::ButtonAnimation(Button* button, int brightness)
269
, m_brightness(brightness)
274
void ButtonAnimation::start(int t)
279
bool ButtonAnimation::step(int t)
285
int sign = (m_button->brightness() > m_brightness) ? -1 : 1;
286
double delta = (t - m_last) * m_speed;
287
// kDebug() << "button step t =" << t << "sign =" << sign << "delta =" << delta;
289
if (fabs(m_button->brightness() - m_brightness) <= delta) {
290
m_button->setBrightness(m_brightness);
294
m_button->setBrightness(m_button->brightness() + sign * delta);
299
void ButtonAnimation::abort()
304
void ButtonAnimation::setBrightness(int value)
306
m_brightness = value;
309
ButtonAnimation::~ButtonAnimation()
314
#include "button.moc"