diff options
author | Adriano Rezende <adriano.rezende@openbossa.org> | 2009-11-16 10:49:55 -0300 |
---|---|---|
committer | Adriano Rezende <adriano.rezende@openbossa.org> | 2009-11-16 10:51:00 -0300 |
commit | 25e43f30060da3973250bf7b94ff5fe61ddead89 (patch) | |
tree | e2c1c5363cba75a0b91594264cc0134ac810ae3b /shared/button.cpp | |
parent | 21975c6542ec1e6676f5aa6a656fc3041a4faf63 (diff) |
Shared: Added Button widget
This is a simple button implementation that provides
the basic capabilities.
Signed-off-by: Adriano Rezende <adriano.rezende@openbossa.org>
Diffstat (limited to 'shared/button.cpp')
-rw-r--r-- | shared/button.cpp | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/shared/button.cpp b/shared/button.cpp new file mode 100644 index 0000000..e6e8d24 --- /dev/null +++ b/shared/button.cpp @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: openBossa - INdT (renato.chencarek@openbossa.org) +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** the openBossa stream from INdT (renato.chencarek@openbossa.org). +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QPixmap> +#include <QPainter> +#include <QStyleOptionGraphicsItem> +#include <QGraphicsSceneMouseEvent> + +#include "button.h" + + +class ButtonPrivate +{ +public: + ButtonPrivate(Button *qptr); + + void init(); + + Button *q; + QString text; + bool isPressed; + QPixmap normalPixmap; + QPixmap pressedPixmap; + QPixmap disabledPixmap; +}; + +ButtonPrivate::ButtonPrivate(Button *qptr) + : q(qptr), isPressed(false) +{ + +} + +void ButtonPrivate::init() +{ + q->setMinimumSize(normalPixmap.size()); + q->setMaximumSize(normalPixmap.size()); +} + + +Button::Button(const QPixmap &normal, QGraphicsItem *parent) + : QGraphicsWidget(parent), + d(new ButtonPrivate(this)) +{ + d->normalPixmap = normal; + d->init(); +} + +Button::Button(const QPixmap &normal, const QPixmap &pressed, + QGraphicsItem *parent) + : QGraphicsWidget(parent), + d(new ButtonPrivate(this)) +{ + d->normalPixmap = normal; + d->pressedPixmap = pressed; + d->init(); +} + +Button::Button(const QPixmap &normal, const QPixmap &pressed, + const QPixmap &disabled, QGraphicsItem *parent) + : QGraphicsWidget(parent), + d(new ButtonPrivate(this)) +{ + d->normalPixmap = normal; + d->pressedPixmap = pressed; + d->disabledPixmap = disabled; + d->init(); +} + +Button::~Button() +{ + delete d; +} + +void Button::setPixmap(State state, const QPixmap &pixmap) +{ + bool dirty = false; + + if (state == NormalState) { + d->normalPixmap = pixmap; + dirty = (!d->isPressed && isEnabled()); + setMinimumSize(d->normalPixmap.size()); + setMaximumSize(d->normalPixmap.size()); + } else if (state == PressedState) { + dirty = d->isPressed; + d->pressedPixmap = pixmap; + } else if (state == DisabledState) { + dirty = !isEnabled(); + d->disabledPixmap = pixmap; + } + + if (dirty) + update(); +} + +QString Button::text() const +{ + return d->text; +} + +void Button::setText(const QString &value) +{ + if (d->text != value) { + d->text = value; + update(); + } +} + +void Button::mousePressEvent(QGraphicsSceneMouseEvent *e) +{ + if (e->button() == Qt::LeftButton) { + d->isPressed = true; + update(); + emit pressed(); + } +} + +void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) +{ + if (e->button() == Qt::LeftButton) { + const bool isClick = d->isPressed; + + d->isPressed = false; + update(); + emit released(); + + if (isClick && contains(e->pos())) + emit clicked(); + } +} + +void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, + QWidget *widget) +{ + Q_UNUSED(widget); + + if (!(option->state & QStyle::State_Enabled)) { + const bool support = !d->disabledPixmap.isNull(); + painter->drawPixmap(0, 0, support ? d->disabledPixmap : d->normalPixmap); + } else if (d->isPressed && !d->pressedPixmap.isNull()) + painter->drawPixmap(0, 0, d->pressedPixmap); + else if (!d->normalPixmap.isNull()) + painter->drawPixmap(0, 0, d->normalPixmap); + + + if (!d->text.isEmpty()) { + const QRect &textRect = boundingRect().toRect(); + + QFontMetrics metrics(font()); + const QString &elidedText = metrics.elidedText(d->text, Qt::ElideRight, + textRect.width()); + + painter->setFont(font()); + painter->setPen(Qt::white); + painter->drawText(textRect, Qt::TextSingleLine | Qt::AlignCenter, elidedText); + } +} |