Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
layerbitmap.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2017 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 #include <QtDebug>
18 #include "keyframe.h"
19 #include "bitmapimage.h"
20 #include "layerbitmap.h"
21 
22 
23 LayerBitmap::LayerBitmap( Object* object ) : Layer( object, Layer::BITMAP )
24 {
25  mName = QString( tr( "Bitmap Layer" ) );
26  mOpacity = 1.0;
27 }
28 
29 LayerBitmap::~LayerBitmap()
30 {
31 }
32 
33 BitmapImage* LayerBitmap::getBitmapImageAtFrame( int frameNumber )
34 {
35  Q_ASSERT( frameNumber >= 1 );
36  return static_cast< BitmapImage* >( getKeyFrameAt( frameNumber ) );
37 }
38 
39 BitmapImage* LayerBitmap::getLastBitmapImageAtFrame( int frameNumber, int increment )
40 {
41  Q_ASSERT( frameNumber >= 1 );
42  return static_cast< BitmapImage* >( getLastKeyFrameAtPosition( frameNumber + increment ) );
43 }
44 
45 void LayerBitmap::loadImageAtFrame( QString path, QPoint topLeft, int frameNumber )
46 {
47  BitmapImage* pKeyFrame = new BitmapImage( path, topLeft );
48  pKeyFrame->setPos( frameNumber );
49  loadKey( pKeyFrame );
50 }
51 
52 Status LayerBitmap::saveKeyFrame( KeyFrame* pKeyFrame, QString path )
53 {
54  QStringList debugInfo = QStringList() << "LayerBitmap::saveKeyFrame" << QString( "pKeyFrame.pos() = %1" ).arg( pKeyFrame->pos() ) << QString( "path = %1" ).arg( path );
55  BitmapImage* pBitmapImage = static_cast< BitmapImage* >( pKeyFrame );
56 
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() )
61  {
62  return Status( Status::FAIL, debugInfo << QString( "pBitmapImage could not be saved" ) );
63  }
64 
65  return Status::OK;
66 }
67 
68 QString LayerBitmap::fileName( int frame )
69 {
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";
75 }
76 
77 QDomElement LayerBitmap::createDomElement( QDomDocument& doc )
78 {
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() );
84 
85  foreachKeyFrame( [&]( KeyFrame* pKeyFrame )
86  {
87  BitmapImage* pImg = static_cast< BitmapImage* >( pKeyFrame );
88 
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 );
95  } );
96 
97  return layerTag;
98 }
99 
100 void LayerBitmap::loadDomElement( QDomElement element, QString dataDirPath )
101 {
102  if ( !element.attribute( "id" ).isNull() )
103  {
104  int id = element.attribute( "id" ).toInt();
105  setId( id );
106  }
107  mName = element.attribute( "name" );
108  mVisible = ( element.attribute( "visibility" ).toInt() == 1 );
109 
110  QDomNode imageTag = element.firstChild();
111  while ( !imageTag.isNull() )
112  {
113  QDomElement imageElement = imageTag.toElement();
114  if ( !imageElement.isNull() )
115  {
116  if ( imageElement.tagName() == "image" )
117  {
118  QString path = dataDirPath + "/" + imageElement.attribute( "src" ); // the file is supposed to be in the data directory
119  //qDebug() << "LAY_BITMAP dataDirPath=" << dataDirPath << " ;path=" << path; //added for debugging puproses
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 );
126  }
127  }
128  imageTag = imageTag.nextSibling();
129  }
130 }
Definition: layer.h:32
Definition: object.h:71