19 #include "bitmapimage.h"
20 #include "layerbitmap.h"
25 mName = QString( tr(
"Bitmap Layer" ) );
29 LayerBitmap::~LayerBitmap()
33 BitmapImage* LayerBitmap::getBitmapImageAtFrame(
int frameNumber )
35 Q_ASSERT( frameNumber >= 1 );
36 return static_cast< BitmapImage*
>( getKeyFrameAt( frameNumber ) );
39 BitmapImage* LayerBitmap::getLastBitmapImageAtFrame(
int frameNumber,
int increment )
41 Q_ASSERT( frameNumber >= 1 );
42 return static_cast< BitmapImage*
>( getLastKeyFrameAtPosition( frameNumber + increment ) );
45 void LayerBitmap::loadImageAtFrame( QString path, QPoint topLeft,
int frameNumber )
48 pKeyFrame->setPos( frameNumber );
52 Status LayerBitmap::saveKeyFrame(
KeyFrame* pKeyFrame, QString path )
54 QStringList debugInfo = QStringList() <<
"LayerBitmap::saveKeyFrame" << QString(
"pKeyFrame.pos() = %1" ).arg( pKeyFrame->pos() ) << QString(
"path = %1" ).arg( path );
57 QString theFileName = fileName( pKeyFrame->pos() );
58 QString strFilePath = QDir( path ).filePath( theFileName );
59 debugInfo << QString(
"strFilePath = " ).arg( strFilePath );
60 if ( !pBitmapImage->image()->save( strFilePath ) && !pBitmapImage->image()->isNull() )
62 return Status( Status::FAIL, debugInfo << QString(
"pBitmapImage could not be saved" ) );
68 QString LayerBitmap::fileName(
int frame )
70 QString layerNumberString = QString::number(
id() );
71 QString frameNumberString = QString::number( frame );
72 while ( layerNumberString.length() < 3 ) layerNumberString.prepend(
"0" );
73 while ( frameNumberString.length() < 3 ) frameNumberString.prepend(
"0" );
74 return layerNumberString +
"." + frameNumberString +
".png";
77 QDomElement LayerBitmap::createDomElement( QDomDocument& doc )
79 QDomElement layerTag = doc.createElement(
"layer" );
80 layerTag.setAttribute(
"id",
id() );
81 layerTag.setAttribute(
"name", mName );
82 layerTag.setAttribute(
"visibility", mVisible );
83 layerTag.setAttribute(
"type", type() );
85 foreachKeyFrame( [&](
KeyFrame* pKeyFrame )
89 QDomElement imageTag = doc.createElement(
"image" );
90 imageTag.setAttribute(
"frame", pKeyFrame->pos() );
91 imageTag.setAttribute(
"src", fileName( pKeyFrame->pos() ) );
92 imageTag.setAttribute(
"topLeftX", pImg->topLeft().x() );
93 imageTag.setAttribute(
"topLeftY", pImg->topLeft().y() );
94 layerTag.appendChild( imageTag );
100 void LayerBitmap::loadDomElement( QDomElement element, QString dataDirPath )
102 if ( !element.attribute(
"id" ).isNull() )
104 int id = element.attribute(
"id" ).toInt();
107 mName = element.attribute(
"name" );
108 mVisible = ( element.attribute(
"visibility" ).toInt() == 1 );
110 QDomNode imageTag = element.firstChild();
111 while ( !imageTag.isNull() )
113 QDomElement imageElement = imageTag.toElement();
114 if ( !imageElement.isNull() )
116 if ( imageElement.tagName() ==
"image" )
118 QString path = dataDirPath +
"/" + imageElement.attribute(
"src" );
120 QFileInfo fi( path );
121 if ( !fi.exists() ) path = imageElement.attribute(
"src" );
122 int position = imageElement.attribute(
"frame" ).toInt();
123 int x = imageElement.attribute(
"topLeftX" ).toInt();
124 int y = imageElement.attribute(
"topLeftY" ).toInt();
125 loadImageAtFrame( path, QPoint( x, y ), position );
128 imageTag = imageTag.nextSibling();