00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <math.h>
00023
00024
00025 #include <QSizeF>
00026
00027
00028 #include <kdebug.h>
00029 #include <kconfig.h>
00030
00031
00032 #include "pixmapsprite.h"
00033
00034
00035
00036 PixmapSprite::PixmapSprite(QString id, ThemeManager* theme, int advancePeriod, int no, QGraphicsScene* canvas)
00037 : Themable(id, theme), QGraphicsPixmapItem(0, canvas)
00038 {
00039 hide();
00040
00041 mAnimationState = Idle;
00042 mAdvancePeriod = advancePeriod;
00043 mNo = no;
00044 mCurrentFrame = 0;
00045
00046 if (theme) theme->updateTheme(this);
00047 }
00048
00049
00050
00051 PixmapSprite::PixmapSprite(int advancePeriod, int no, QGraphicsScene* canvas)
00052 : Themable(), QGraphicsPixmapItem(0, canvas)
00053 {
00054 hide();
00055
00056 mAnimationState = Idle;
00057 mAdvancePeriod = advancePeriod;
00058 mNo = no;
00059 mCurrentFrame = 0;
00060 }
00061
00062
00063
00064
00065 void PixmapSprite::changeTheme()
00066 {
00067
00068 mFrames.clear();
00069 mHotspots.clear();
00070
00071
00072 double oldscale = this->getScale();
00073 double scale = thememanager()->getScale();
00074 setScale(scale);
00075
00076
00077 KConfigGroup config = thememanager()->config(id());
00078 double width = config.readEntry("width", 1.0);
00079 double height = config.readEntry("height", 0.0);
00080 width *= scale;
00081 height *= scale;
00082 QPointF pos = config.readEntry("pos", QPointF(1.0,1.0));
00083 pos *= scale;
00084
00085 if (config.hasKey("zValue"))
00086 {
00087 double zValue = config.readEntry("zValue", 0.0);
00088 setZValue(zValue);
00089 }
00090
00091
00092 bool center = config.readEntry("center", false);
00093
00094
00095 mStartFrame = config.readEntry("start-frame", 0);
00096 mEndFrame = config.readEntry("end-frame", 0);
00097 mDelay = config.readEntry("animation-delay", 0);
00098 QString refframe = config.readEntry("ref-frame", QString());
00099
00100
00101 if (config.hasKey("pos"))
00102 {
00103 setPos(pos.x(), pos.y());
00104 }
00105 else
00106 {
00107 setPos(x()*scale/oldscale, y()*scale/oldscale);
00108 }
00109
00110
00111 QString svgid = config.readEntry("svgid");
00112
00113 QPixmap pixmap;
00114 if (svgid == "auto")
00115 {
00116 for (int i=mStartFrame;i<=mEndFrame;i++)
00117 {
00118 QString name = QString("frame%1").arg(i);
00119 svgid = config.readEntry(name.toUtf8());
00120 if (!refframe.isNull())
00121 {
00122 pixmap = thememanager()->getPixmap(svgid, refframe, width);
00123 }
00124 else if (config.hasKey("height"))
00125 {
00126 pixmap = thememanager()->getPixmap(svgid, QSize(int(width), int(height)));
00127 }
00128 else
00129 {
00130 pixmap = thememanager()->getPixmap(svgid, width);
00131 }
00132 mFrames.append(pixmap);
00133 if (center) mHotspots.append(QPointF(pixmap.width()/2,pixmap.height()/2));
00134 else mHotspots.append(QPointF(0.0,0.0));
00135 }
00136 }
00137
00138 else
00139 {
00140 if (config.hasKey("height"))
00141 {
00142 pixmap = thememanager()->getPixmap(svgid, QSize(int(width), int(height)));
00143 }
00144 else
00145 {
00146 pixmap = thememanager()->getPixmap(svgid, width);
00147 }
00148 mFrames.append(pixmap);
00149 if (center) mHotspots.append(QPointF(pixmap.width()/2,pixmap.height()/2));
00150 else mHotspots.append(QPointF(0.0,0.0));
00151 }
00152
00153
00154 setFrame(mCurrentFrame, true);
00155 update();
00156 }
00157
00158
00159
00160 double PixmapSprite::getDoubleValue(QString item)
00161 {
00162 KConfigGroup config = thememanager()->config(id());
00163 return config.readEntry(item, 0.0);
00164 }
00165
00166
00167
00168 void PixmapSprite::setPosition(QPointF pos)
00169 {
00170 mStart = pos;
00171 setPos(mStart.x()*getScale(), mStart.y()*getScale());
00172 }
00173
00174
00175
00176 void PixmapSprite::setAnimation(bool status)
00177 {
00178 if (status) mAnimationState = Animated;
00179 else mAnimationState = Idle;
00180 mTime = 0;
00181 setFrame(mStartFrame);
00182 }
00183
00184
00185
00186 void PixmapSprite::setAnimation(int start, int end, int delay)
00187 {
00188 mDelay = delay;
00189 mStartFrame = start;
00190 mEndFrame = end;
00191 setAnimation(true);
00192 }
00193
00194
00195
00196
00197 void PixmapSprite::setFrame(int no, bool force)
00198 {
00199 if (!force && no == mCurrentFrame) return;
00200 if (no<0 || no >=mFrames.count()) return;
00201 setPixmap(mFrames.at(no));
00202 resetMatrix();
00203 translate(-mHotspots.at(no).x(), -mHotspots.at(no).y());
00204 mCurrentFrame = no;
00205 update();
00206 }
00207
00208
00209
00210 void PixmapSprite::advance(int phase)
00211 {
00212
00213 if (phase == 0)
00214 {
00215 QGraphicsItem::advance(phase);
00216 return ;
00217 }
00218
00219
00220 mTime += mAdvancePeriod;
00221
00222
00223 if (mAnimationState == Animated)
00224 {
00225
00226 if (mTime>mDelay)
00227 {
00228 mTime = 0;
00229 int frame = mCurrentFrame+1;
00230 if (frame > mEndFrame) setFrame(mStartFrame);
00231 else setFrame(frame);
00232 }
00233 }
00234
00235 QGraphicsItem::advance(phase);
00236 }
00237