From b58baaef6befbcb249d982e831a8dbdaa9b6e123 Mon Sep 17 00:00:00 2001 From: Andre Duffeck Date: Sun, 22 Jul 2007 11:02:34 +0000 Subject: [PATCH] Import Flash widget. It allows to show text or an image in order to notify the user about some change. svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=690844 --- CMakeLists.txt | 2 + widgets/flash.cpp | 190 ++++++++++++++++++++++++++++++++++++++++++++++ widgets/flash.h | 72 ++++++++++++++++++ 3 files changed, 264 insertions(+) create mode 100644 widgets/flash.cpp create mode 100644 widgets/flash.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8613c480c..489075c35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ set(plasma_LIB_SRCS widgets/layout.cpp widgets/layoutitem.cpp widgets/vboxlayout.cpp + widgets/flash.cpp ) kde4_add_library(plasma SHARED ${plasma_LIB_SRCS}) @@ -80,6 +81,7 @@ install(FILES widgets/radiobutton.h widgets/vboxlayout.h widgets/widget.h + widgets/flash.h DESTINATION ${INCLUDE_INSTALL_DIR}/plasma/widgets) install(FILES diff --git a/widgets/flash.cpp b/widgets/flash.cpp new file mode 100644 index 000000000..6ce73dcee --- /dev/null +++ b/widgets/flash.cpp @@ -0,0 +1,190 @@ +/* + * Copyright (C) 2007 by André Duffeck + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License version 2 as + * published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Stre + * et, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "flash.h" + + +#include +#include +#include +#include +#include +#include + +#include + +#include + +using namespace Plasma; + +class Flash::Private +{ + public: + enum FlashType { Text, Pixmap }; + + Private() { } + ~Private() { } + int height; + int width; + + QString text; + QColor color; + QFont font; + QPixmap pixmap; + int duration; + int defaultDuration; + FlashType type; + + Plasma::Phase::AnimId animId; + QPixmap renderedPixmap; +}; + + +Flash::Flash(QGraphicsItem *parent) + : QObject(), + QGraphicsItem(parent), + d(new Private) +{ + d->duration = 2000; + d->type = Private::Text; + d->color = Qt::black; + d->height = 40; + d->width = 100 ; + d->animId = 0; +} + +Flash::~Flash() +{ + delete d; +} + +QRectF Flash::boundingRect() const +{ + return QRectF(0,0,d->width,d->height); +} + +int Flash::height() const +{ + return d->height; +} + +int Flash::width() const +{ + return d->width; +} + +void Flash::setHeight(int h) +{ + prepareGeometryChange (); + d->height = h; + update(); +} + +void Flash::setWidth(int w) +{ + prepareGeometryChange (); + d->width = w; + update(); +} + +void Flash::setDuration( int duration ) +{ + d->defaultDuration = duration; +} + +QSize Flash::size() const +{ + return QSize(d->width,d->height); +} + +void Flash::setSize(const QSize &s) +{ + prepareGeometryChange (); + d->width = s.width(); + d->height = s.height(); + update(); +} + +void Flash::setColor( const QColor &color ) +{ + d->color = color; +} + +void Flash::setFont( const QFont &font ) +{ + d->font = font; +} + +void Flash::flash( const QString &text, int duration) +{ + d->type = Private::Text; + d->duration = duration > 0 ? duration : d->defaultDuration; + d->text = text; + QTimer::singleShot( 0, this, SLOT(fadeIn()) ); +} + +void Flash::flash( const QPixmap &pixmap, int duration ) +{ + d->type = Private::Pixmap; + d->duration = duration > 0 ? duration : d->defaultDuration; + d->pixmap = pixmap; + QTimer::singleShot( 0, this, SLOT(fadeIn()) ); +} + +void Flash::fadeIn() +{ + d->renderedPixmap = renderPixmap(); + d->animId = Plasma::Phase::self()->animateElement(this, Plasma::Phase::ElementAppear); + Plasma::Phase::self()->setAnimationPixmap( d->animId, d->renderedPixmap ); + QTimer::singleShot( d->duration, this, SLOT(fadeOut()) ); +} + +void Flash::fadeOut() +{ + d->animId = Plasma::Phase::self()->animateElement(this, Plasma::Phase::ElementDisappear); + Plasma::Phase::self()->setAnimationPixmap( d->animId, d->renderedPixmap ); +} + +QPixmap Flash::renderPixmap() +{ + QPixmap pm( width(), height() ); + pm.fill(Qt::transparent); + + QPainter painter( &pm ); + if( d->type == Private::Text ) { + painter.setPen( d->color ); + painter.setFont( d->font ); + painter.drawText( QRect( 0, 0, pm.width(), pm.height() ), d->text, Qt::AlignHCenter | Qt::AlignVCenter); + } else if( d->type == Private::Pixmap ) { + painter.drawPixmap( QPoint( 0, 0 ), d->pixmap ); + } + return pm; +} + +void Flash::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option) + Q_UNUSED(widget) + + if( d->animId ) { + painter->drawPixmap(0, 0, Plasma::Phase::self()->animationResult(d->animId)); + } +} + +#include "flash.moc" diff --git a/widgets/flash.h b/widgets/flash.h new file mode 100644 index 000000000..a82198952 --- /dev/null +++ b/widgets/flash.h @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2007 by André Duffeck + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License version 2 as + * published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef FLASH_H_ +#define FLASH_H_ + + +#include +#include + +#include + + +namespace Plasma +{ + +/** + * Class that allows to flash text or icons inside plasma + */ +class PLASMA_EXPORT Flash : public QObject, public QGraphicsItem +{ + Q_OBJECT + public: + Flash(QGraphicsItem *parent = 0); + virtual ~Flash(); + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + QRectF boundingRect() const; + QSize size() const; + int height() const; + int width() const; + void setSize(const QSize &size); + void setWidth(int width); + void setHeight(int height); + + void setFont( const QFont & ); + void setColor( const QColor & ); + void setDuration( int duration ); + + void flash( const QString &text, int duration = 0 ); + void flash( const QPixmap &pixmap, int duration = 0 ); + + protected Q_SLOTS: + void fadeIn(); + void fadeOut(); + + protected: + QPixmap renderPixmap(); + + private: + class Private; + Private * const d; +}; + +} + +#endif