kivio
kivio_sml_stencil_spawner.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kivio_common.h"
00020 #include "kivio_connector_target.h"
00021 #include "kivio_shape.h"
00022 #include "kivio_shape_data.h"
00023 #include "kivio_sml_stencil.h"
00024 #include "kivio_sml_stencil_spawner.h"
00025 #include "kivio_stencil_spawner_set.h"
00026 #include "kivio_stencil_spawner.h"
00027 #include "kivio_stencil_spawner_info.h"
00028
00029 #include <qdom.h>
00030 #include <qfile.h>
00031 #include <qiodevice.h>
00032 #include <qpainter.h>
00033 #include <qpoint.h>
00034 #include <qpixmap.h>
00035 #include <qrect.h>
00036 #include <qfileinfo.h>
00037
00038 #include <kdebug.h>
00039
00040 KivioSMLStencilSpawner::KivioSMLStencilSpawner( KivioStencilSpawnerSet *p )
00041 : KivioStencilSpawner( p ),
00042 m_pStencil(NULL)
00043 {
00044 m_pStencil = new KivioSMLStencil();
00045
00046 m_pTargets = new QPtrList<KivioConnectorTarget>;
00047 m_pTargets->setAutoDelete(true);
00048 }
00049
00050 KivioSMLStencilSpawner::~KivioSMLStencilSpawner()
00051 {
00052 if( m_pStencil )
00053 {
00054 delete m_pStencil;
00055 m_pStencil = NULL;
00056 }
00057
00058 if( m_pTargets )
00059 {
00060 delete m_pTargets;
00061 m_pTargets = NULL;
00062 }
00063
00064 kdDebug(43000) << "* SMLStencilSpawner "<< m_pInfo->id() << " deleted" << endl;
00065 }
00066
00067 QDomElement KivioSMLStencilSpawner::saveXML( QDomDocument &doc )
00068 {
00069 QDomElement spawnE = doc.createElement("KivioSMLStencilSpawner");
00070
00071 XmlWriteString( spawnE, "id", m_pInfo->id() );
00072
00073 return spawnE;
00074 }
00075
00076 bool KivioSMLStencilSpawner::load( const QString &file )
00077 {
00078 QDomDocument d("test");
00079
00080 m_filename = QString(file);
00081 QFile f(file);
00082
00083 if( f.open( IO_ReadOnly )==false )
00084 {
00085 kdDebug(43000) << "KivioSMLStencilSpawner::load() - Error opening stencil: " << file << endl;
00086 return false;
00087 }
00088 d.setContent(&f);
00089
00090 if(loadXML(file, d))
00091 {
00092 f.close();
00093 return true;
00094 }
00095 else
00096 {
00097 f.close();
00098 return false;
00099 }
00100 }
00101
00102 bool KivioSMLStencilSpawner::loadXML( const QString &file, QDomDocument &d )
00103 {
00104 KivioConnectorTarget *pTarget;
00105
00106 QDomElement root = d.documentElement();
00107 QDomElement e;
00108 QDomNode node = root.firstChild();
00109 QString nodeName;
00110
00111 while( !node.isNull() )
00112 {
00113 nodeName = node.nodeName();
00114
00115 if( nodeName.compare("KivioSMLStencilSpawnerInfo")==0 )
00116 {
00117 m_pInfo->loadXML( (const QDomElement)node.toElement() );
00118 }
00119 else if( nodeName.compare("KivioShape")==0 )
00120 {
00121 loadShape( node );
00122 }
00123 else if( nodeName.compare("Dimensions")==0 )
00124 {
00125 e = node.toElement();
00126
00127 m_defWidth = XmlReadFloat( e, "w", 72.0f );
00128 m_defHeight = XmlReadFloat( e, "h", 72.0f );
00129 }
00130 else if( nodeName.compare("KivioConnectorTarget")==0 )
00131 {
00132 pTarget = new KivioConnectorTarget();
00133 pTarget->loadXML( (const QDomElement)node.toElement() );
00134
00135 m_pStencil->m_pConnectorTargets->append( pTarget );
00136 }
00137 else
00138 {
00139 kdDebug(43000) << "KivioSMLStencilSpawner::load() - Unknown node " << nodeName << " while loading " << file << endl;
00140 }
00141
00142 node = node.nextSibling();
00143 }
00144
00145 pTarget = m_pStencil->m_pConnectorTargets->first();
00146
00147 while(pTarget) {
00148 pTarget->setOffsets(pTarget->x() / m_defWidth, pTarget->y() / m_defHeight);
00149 m_pTargets->append(pTarget->duplicate());
00150 pTarget = m_pStencil->m_pConnectorTargets->next();
00151 }
00152
00153
00154 QFileInfo finfo(file);
00155 QString pixFile = finfo.dirPath(true) + "/" + finfo.baseName() + ".xpm";
00156
00157 if(!m_icon.load( pixFile )) {
00158 QString pixFile = finfo.dirPath(true) + "/" + finfo.baseName() + ".png";
00159 m_icon.load( pixFile );
00160 }
00161
00162 return true;
00163 }
00164
00165
00169 void KivioSMLStencilSpawner::loadShape( QDomNode &shapeNode )
00170 {
00171 KivioShapeData::KivioShapeType t;
00172 KivioShape *pShape = NULL;
00173 QDomElement shapeElement = shapeNode.toElement();
00174
00175 t = KivioShapeData::shapeTypeFromString( XmlReadString( shapeElement, "type", "None" ) );
00176
00177 switch( t )
00178 {
00179 case KivioShapeData::kstNone:
00180 break;
00181
00182 case KivioShapeData::kstArc:
00183 pShape = KivioShape::loadShapeArc( shapeElement );
00184 break;
00185
00186 case KivioShapeData::kstPie:
00187 pShape = KivioShape::loadShapePie( shapeElement );
00188 break;
00189
00190 case KivioShapeData::kstLineArray:
00191 pShape = KivioShape::loadShapeLineArray( shapeElement );
00192 break;
00193
00194 case KivioShapeData::kstPolyline:
00195 pShape = KivioShape::loadShapePolyline( shapeElement );
00196 break;
00197
00198 case KivioShapeData::kstPolygon:
00199 pShape = KivioShape::loadShapePolygon( shapeElement );
00200 break;
00201
00202 case KivioShapeData::kstBezier:
00203 pShape = KivioShape::loadShapeBezier( shapeElement );
00204 break;
00205
00206 case KivioShapeData::kstRectangle:
00207 pShape = KivioShape::loadShapeRectangle( shapeElement );
00208 break;
00209
00210 case KivioShapeData::kstRoundRectangle:
00211 pShape = KivioShape::loadShapeRoundRectangle( shapeElement );
00212 break;
00213
00214 case KivioShapeData::kstEllipse:
00215 pShape = KivioShape::loadShapeEllipse( shapeElement );
00216 break;
00217
00218 case KivioShapeData::kstOpenPath:
00219 pShape = KivioShape::loadShapeOpenPath( shapeElement );
00220 break;
00221
00222 case KivioShapeData::kstClosedPath:
00223 pShape = KivioShape::loadShapeClosedPath( shapeElement );
00224 break;
00225
00226 case KivioShapeData::kstTextBox:
00227 pShape = KivioShape::loadShapeTextBox( shapeElement );
00228 break;
00229
00230 default:
00231 break;
00232 }
00233
00234 if( pShape )
00235 {
00236 m_pStencil->m_pShapeList->append( pShape );
00237 }
00238
00239
00240 }
00241
00242
00246 KivioStencil *KivioSMLStencilSpawner::newStencil()
00247 {
00248 KivioStencil *pNewStencil = m_pStencil->duplicate();
00249
00250 pNewStencil->setSpawner(this);
00251
00252 pNewStencil->setDimensions( m_defWidth, m_defHeight );
00253
00254 return pNewStencil;
00255 }
|