| 1 | /*
|
|---|
| 2 | * alerticon.cpp - class for handling animating alert icons
|
|---|
| 3 | * Copyright (C) 2003 Michail Pishchagin
|
|---|
| 4 | *
|
|---|
| 5 | * This program is free software; you can redistribute it and/or
|
|---|
| 6 | * modify it under the terms of the GNU General Public License
|
|---|
| 7 | * as published by the Free Software Foundation; either version 2
|
|---|
| 8 | * of the License, or (at your option) any later version.
|
|---|
| 9 | *
|
|---|
| 10 | * This program is distributed in the hope that it will be useful,
|
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | * GNU General Public License for more details.
|
|---|
| 14 | *
|
|---|
| 15 | * You should have received a copy of the GNU General Public License
|
|---|
| 16 | * along with this library; if not, write to the Free Software
|
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 18 | *
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "alerticon.h"
|
|---|
| 22 | #include "common.h"
|
|---|
| 23 | #include <qtimer.h>
|
|---|
| 24 | #include <qapplication.h>
|
|---|
| 25 |
|
|---|
| 26 | //----------------------------------------------------------------------------
|
|---|
| 27 | // MetaAlertIcon
|
|---|
| 28 | //----------------------------------------------------------------------------
|
|---|
| 29 |
|
|---|
| 30 | class MetaAlertIcon : public QObject
|
|---|
| 31 | {
|
|---|
| 32 | Q_OBJECT
|
|---|
| 33 | public:
|
|---|
| 34 | MetaAlertIcon();
|
|---|
| 35 | ~MetaAlertIcon();
|
|---|
| 36 |
|
|---|
| 37 | Impix blank16() const;
|
|---|
| 38 | int framenumber() const;
|
|---|
| 39 |
|
|---|
| 40 | signals:
|
|---|
| 41 | void updateFrame(int frame);
|
|---|
| 42 | void update();
|
|---|
| 43 |
|
|---|
| 44 | public slots:
|
|---|
| 45 | void updateAlertStyle();
|
|---|
| 46 |
|
|---|
| 47 | private slots:
|
|---|
| 48 | void animTimeout();
|
|---|
| 49 |
|
|---|
| 50 | private:
|
|---|
| 51 | QTimer *animTimer;
|
|---|
| 52 | int frame;
|
|---|
| 53 | Impix _blank16;
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | static MetaAlertIcon *metaAlertIcon = 0;
|
|---|
| 57 |
|
|---|
| 58 | MetaAlertIcon::MetaAlertIcon()
|
|---|
| 59 | : QObject(qApp)
|
|---|
| 60 | {
|
|---|
| 61 | animTimer = new QTimer(this);
|
|---|
| 62 | connect(animTimer, SIGNAL(timeout()), SLOT(animTimeout()));
|
|---|
| 63 | animTimer->start(120 * 5);
|
|---|
| 64 | frame = 0;
|
|---|
| 65 |
|
|---|
| 66 | // blank icon
|
|---|
| 67 | QImage blankImg(16, 16, 32);
|
|---|
| 68 | blankImg.fill(0);
|
|---|
| 69 | blankImg.setAlphaBuffer(true);
|
|---|
| 70 | _blank16.setImage(blankImg);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | MetaAlertIcon::~MetaAlertIcon()
|
|---|
| 74 | {
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | Impix MetaAlertIcon::blank16() const
|
|---|
| 78 | {
|
|---|
| 79 | return _blank16;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void MetaAlertIcon::animTimeout()
|
|---|
| 83 | {
|
|---|
| 84 | frame = !frame;
|
|---|
| 85 | emit updateFrame(frame);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void MetaAlertIcon::updateAlertStyle()
|
|---|
| 89 | {
|
|---|
| 90 | emit update();
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | int MetaAlertIcon::framenumber() const
|
|---|
| 94 | {
|
|---|
| 95 | return frame;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | //----------------------------------------------------------------------------
|
|---|
| 99 | // AlertIcon::Private
|
|---|
| 100 | //----------------------------------------------------------------------------
|
|---|
| 101 |
|
|---|
| 102 | class AlertIcon::Private : public QObject
|
|---|
| 103 | {
|
|---|
| 104 | Q_OBJECT
|
|---|
| 105 | public:
|
|---|
| 106 | Private(AlertIcon *_ai);
|
|---|
| 107 | ~Private();
|
|---|
| 108 |
|
|---|
| 109 | void init();
|
|---|
| 110 |
|
|---|
| 111 | public slots:
|
|---|
| 112 | void update();
|
|---|
| 113 | void activated(bool playSound);
|
|---|
| 114 | void stop();
|
|---|
| 115 |
|
|---|
| 116 | void updateFrame(int frame);
|
|---|
| 117 | void pixmapChanged(const QPixmap &);
|
|---|
| 118 |
|
|---|
| 119 | public:
|
|---|
| 120 | AlertIcon *ai;
|
|---|
| 121 | Icon *real;
|
|---|
| 122 | bool isActivated;
|
|---|
| 123 | Impix impix;
|
|---|
| 124 | };
|
|---|
| 125 |
|
|---|
| 126 | AlertIcon::Private::Private(AlertIcon *_ai)
|
|---|
| 127 | {
|
|---|
| 128 | if ( !metaAlertIcon )
|
|---|
| 129 | metaAlertIcon = new MetaAlertIcon();
|
|---|
| 130 |
|
|---|
| 131 | ai = _ai;
|
|---|
| 132 | real = 0;
|
|---|
| 133 | isActivated = false;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | AlertIcon::Private::~Private()
|
|---|
| 137 | {
|
|---|
| 138 | if ( isActivated )
|
|---|
| 139 | stop();
|
|---|
| 140 |
|
|---|
| 141 | if ( real )
|
|---|
| 142 | delete real;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | void AlertIcon::Private::init()
|
|---|
| 146 | {
|
|---|
| 147 | connect(metaAlertIcon, SIGNAL(update()), SLOT(update()));
|
|---|
| 148 | connect(real, SIGNAL(iconModified(const QPixmap &)), SLOT(pixmapChanged(const QPixmap &)));
|
|---|
| 149 |
|
|---|
| 150 | if ( option.alertStyle == 2 && real->isAnimated() )
|
|---|
| 151 | impix = real->frameImpix();
|
|---|
| 152 | else
|
|---|
| 153 | impix = real->impix();
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | void AlertIcon::Private::update()
|
|---|
| 157 | {
|
|---|
| 158 | stop();
|
|---|
| 159 | activated(false);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | void AlertIcon::Private::activated(bool playSound)
|
|---|
| 163 | {
|
|---|
| 164 | if ( option.alertStyle == 2 && real->isAnimated() ) {
|
|---|
| 165 | if ( !isActivated ) {
|
|---|
| 166 | connect(real, SIGNAL(pixmapChanged(const QPixmap &)), SLOT(pixmapChanged(const QPixmap &)));
|
|---|
| 167 | real->activated(playSound);
|
|---|
| 168 | isActivated = true;
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 | else if ( option.alertStyle == 1 || (option.alertStyle == 2 && !real->isAnimated()) ) {
|
|---|
| 172 | connect(metaAlertIcon, SIGNAL(updateFrame(int)), SLOT(updateFrame(int)));
|
|---|
| 173 | }
|
|---|
| 174 | else {
|
|---|
| 175 | impix = real->impix();
|
|---|
| 176 | emit ai->pixmapChanged(impix.pixmap());
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | void AlertIcon::Private::stop()
|
|---|
| 181 | {
|
|---|
| 182 | disconnect(metaAlertIcon, SIGNAL(updateFrame(int)), this, SLOT(updateFrame(int)));
|
|---|
| 183 |
|
|---|
| 184 | if ( isActivated ) {
|
|---|
| 185 | disconnect(real, SIGNAL(pixmapChanged(const QPixmap &)), this, SLOT(pixmapChanged(const QPixmap &)));
|
|---|
| 186 | real->stop();
|
|---|
| 187 | isActivated = false;
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | void AlertIcon::Private::updateFrame(int frame)
|
|---|
| 192 | {
|
|---|
| 193 | if ( !metaAlertIcon ) // just in case
|
|---|
| 194 | metaAlertIcon = new MetaAlertIcon();
|
|---|
| 195 |
|
|---|
| 196 | if ( frame )
|
|---|
| 197 | impix = real->impix();
|
|---|
| 198 | else
|
|---|
| 199 | impix = metaAlertIcon->blank16();
|
|---|
| 200 |
|
|---|
| 201 | emit ai->pixmapChanged(impix.pixmap());
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | void AlertIcon::Private::pixmapChanged(const QPixmap &p)
|
|---|
| 205 | {
|
|---|
| 206 | impix = real->frameImpix();
|
|---|
| 207 |
|
|---|
| 208 | emit ai->pixmapChanged(p);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | //----------------------------------------------------------------------------
|
|---|
| 212 | // AlertIcon
|
|---|
| 213 | //----------------------------------------------------------------------------
|
|---|
| 214 |
|
|---|
| 215 | AlertIcon::AlertIcon(const Icon *icon)
|
|---|
| 216 | {
|
|---|
| 217 | d = new Private(this);
|
|---|
| 218 | if ( icon )
|
|---|
| 219 | d->real = new Icon(*icon);
|
|---|
| 220 | else
|
|---|
| 221 | d->real = new Icon();
|
|---|
| 222 |
|
|---|
| 223 | d->init();
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | AlertIcon::~AlertIcon()
|
|---|
| 227 | {
|
|---|
| 228 | delete d;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | bool AlertIcon::isAnimated() const
|
|---|
| 232 | {
|
|---|
| 233 | return d->real->isAnimated();
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | const QPixmap &AlertIcon::pixmap() const
|
|---|
| 237 | {
|
|---|
|
|---|