kivio

kivio_sml_stencil_spawner.cpp
1/*
2 * Kivio - Visual Modelling and Flowcharting
3 * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
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 program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19#include "kivio_common.h"
20#include "kivio_connector_target.h"
21#include "kivio_shape.h"
22#include "kivio_shape_data.h"
23#include "kivio_sml_stencil.h"
24#include "kivio_sml_stencil_spawner.h"
25#include "kivio_stencil_spawner_set.h"
26#include "kivio_stencil_spawner.h"
27#include "kivio_stencil_spawner_info.h"
28
29#include <tqdom.h>
30#include <tqfile.h>
31#include <tqiodevice.h>
32#include <tqpainter.h>
33#include <tqpoint.h>
34#include <tqpixmap.h>
35#include <tqrect.h>
36#include <tqfileinfo.h>
37
38#include <kdebug.h>
39
40KivioSMLStencilSpawner::KivioSMLStencilSpawner( KivioStencilSpawnerSet *p )
41 : KivioStencilSpawner( p ),
42 m_pStencil(NULL)
43{
44 m_pStencil = new KivioSMLStencil();
45
46 m_pTargets = new TQPtrList<KivioConnectorTarget>;
47 m_pTargets->setAutoDelete(true);
48}
49
50KivioSMLStencilSpawner::~KivioSMLStencilSpawner()
51{
52 if( m_pStencil )
53 {
54 delete m_pStencil;
55 m_pStencil = NULL;
56 }
57
58 if( m_pTargets )
59 {
60 delete m_pTargets;
61 m_pTargets = NULL;
62 }
63
64 kdDebug(43000) << "* SMLStencilSpawner "<< m_pInfo->id() << " deleted" << endl;
65}
66
67TQDomElement KivioSMLStencilSpawner::saveXML( TQDomDocument &doc )
68{
69 TQDomElement spawnE = doc.createElement("KivioSMLStencilSpawner");
70
71 XmlWriteString( spawnE, "id", m_pInfo->id() );
72
73 return spawnE;
74}
75
76bool KivioSMLStencilSpawner::load( const TQString &file )
77{
78 TQDomDocument d("test");
79
80 m_filename = TQString(file);
81 TQFile f(file);
82
83 if( f.open( IO_ReadOnly )==false )
84 {
85 kdDebug(43000) << "KivioSMLStencilSpawner::load() - Error opening stencil: " << file << endl;
86 return false;
87 }
88 d.setContent(&f);
89
90 if(loadXML(file, d))
91 {
92 f.close();
93 return true;
94 }
95 else
96 {
97 f.close();
98 return false;
99 }
100}
101
102bool KivioSMLStencilSpawner::loadXML( const TQString &file, TQDomDocument &d )
103{
104 KivioConnectorTarget *pTarget;
105
106 TQDomElement root = d.documentElement();
107 TQDomElement e;
108 TQDomNode node = root.firstChild();
109 TQString nodeName;
110
111 while( !node.isNull() )
112 {
113 nodeName = node.nodeName();
114
115 if( nodeName.compare("KivioSMLStencilSpawnerInfo")==0 )
116 {
117 m_pInfo->loadXML( (const TQDomElement)node.toElement() );
118 }
119 else if( nodeName.compare("KivioShape")==0 )
120 {
121 loadShape( node );
122 }
123 else if( nodeName.compare("Dimensions")==0 )
124 {
125 e = node.toElement();
126
127 m_defWidth = XmlReadFloat( e, "w", 72.0f );
128 m_defHeight = XmlReadFloat( e, "h", 72.0f );
129 }
130 else if( nodeName.compare("KivioConnectorTarget")==0 )
131 {
132 pTarget = new KivioConnectorTarget();
133 pTarget->loadXML( (const TQDomElement)node.toElement() );
134
135 m_pStencil->m_pConnectorTargets->append( pTarget );
136 }
137 else
138 {
139 kdDebug(43000) << "KivioSMLStencilSpawner::load() - Unknown node " << nodeName << " while loading " << file << endl;
140 }
141
142 node = node.nextSibling();
143 }
144
145 pTarget = m_pStencil->m_pConnectorTargets->first();
146
147 while(pTarget) {
148 pTarget->setOffsets(pTarget->x() / m_defWidth, pTarget->y() / m_defHeight);
149 m_pTargets->append(pTarget->duplicate());
150 pTarget = m_pStencil->m_pConnectorTargets->next();
151 }
152
153 // Now load the xpm
154 TQFileInfo finfo(file);
155 TQString pixFile = finfo.dirPath(true) + "/" + finfo.baseName() + ".xpm";
156
157 if(!m_icon.load( pixFile )) {
158 TQString pixFile = finfo.dirPath(true) + "/" + finfo.baseName() + ".png";
159 m_icon.load( pixFile );
160 }
161
162 return true;
163}
164
165
169void KivioSMLStencilSpawner::loadShape( TQDomNode &shapeNode )
170{
172 KivioShape *pShape = NULL;
173 TQDomElement shapeElement = shapeNode.toElement();
174
175 t = KivioShapeData::shapeTypeFromString( XmlReadString( shapeElement, "type", "None" ) );
176
177 switch( t )
178 {
179 case KivioShapeData::kstNone:
180 break;
181
182 case KivioShapeData::kstArc:
183 pShape = KivioShape::loadShapeArc( shapeElement );
184 break;
185
186 case KivioShapeData::kstPie:
187 pShape = KivioShape::loadShapePie( shapeElement );
188 break;
189
190 case KivioShapeData::kstLineArray:
191 pShape = KivioShape::loadShapeLineArray( shapeElement );
192 break;
193
194 case KivioShapeData::kstPolyline:
195 pShape = KivioShape::loadShapePolyline( shapeElement );
196 break;
197
198 case KivioShapeData::kstPolygon:
199 pShape = KivioShape::loadShapePolygon( shapeElement );
200 break;
201
202 case KivioShapeData::kstBezier:
203 pShape = KivioShape::loadShapeBezier( shapeElement );
204 break;
205
206 case KivioShapeData::kstRectangle:
207 pShape = KivioShape::loadShapeRectangle( shapeElement );
208 break;
209
210 case KivioShapeData::kstRoundRectangle:
211 pShape = KivioShape::loadShapeRoundRectangle( shapeElement );
212 break;
213
214 case KivioShapeData::kstEllipse:
215 pShape = KivioShape::loadShapeEllipse( shapeElement );
216 break;
217
218 case KivioShapeData::kstOpenPath:
219 pShape = KivioShape::loadShapeOpenPath( shapeElement );
220 break;
221
222 case KivioShapeData::kstClosedPath:
223 pShape = KivioShape::loadShapeClosedPath( shapeElement );
224 break;
225
226 case KivioShapeData::kstTextBox:
227 pShape = KivioShape::loadShapeTextBox( shapeElement );
228 break;
229
230 default:
231 break;
232 }
233
234 if( pShape )
235 {
236 m_pStencil->m_pShapeList->append( pShape );
237 }
238
239
240}
241
242
246KivioStencil *KivioSMLStencilSpawner::newStencil()
247{
248 KivioStencil *pNewStencil = m_pStencil->duplicate();
249
250 pNewStencil->setSpawner(this);
251
252 pNewStencil->setDimensions( m_defWidth, m_defHeight );
253
254 return pNewStencil;
255}
KivioShapeType
The possible shape types.