kivio

kivio_py_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
20#include "kivio_py_stencil_spawner.h"
21
22#ifdef HAVE_PYTHON
23
24#include "kivio_common.h"
25#include "kivio_connector_target.h"
26#include "kivio_py_stencil.h"
27#include "kivio_stencil_spawner_set.h"
28#include "kivio_stencil_spawner.h"
29#include "kivio_stencil_spawner_info.h"
30
31#include <iostream>
32#include <tqdom.h>
33#include <tqfile.h>
34#include <tqiodevice.h>
35#include <tqpainter.h>
36#include <tqpoint.h>
37#include <tqpixmap.h>
38#include <tqrect.h>
39#include <tqfileinfo.h>
40
41#include <kdebug.h>
42
43KivioPyStencilSpawner::KivioPyStencilSpawner( KivioStencilSpawnerSet *p )
44 : KivioStencilSpawner( p ),
45 m_pStencil(NULL)
46{
47 m_pStencil = new KivioPyStencil();
48 m_pStencil->setSpawner(this);
49
50 m_pTargets = new TQPtrList<KivioConnectorTarget>;
51 m_pTargets->setAutoDelete(true);
52}
53
54KivioPyStencilSpawner::~KivioPyStencilSpawner()
55{
56 if( m_pStencil )
57 {
58 delete m_pStencil;
59 m_pStencil = NULL;
60 }
61
62 if( m_pTargets )
63 {
64 delete m_pTargets;
65 m_pTargets = NULL;
66 }
67
68 kdDebug(43000) << "* PyStencilSpawner "<< m_pInfo->id() << " deleted" << endl;
69}
70
71TQDomElement KivioPyStencilSpawner::saveXML( TQDomDocument &doc )
72{
73 TQDomElement spawnE = doc.createElement("KivioPyStencilSpawner");
74
75 XmlWriteString( spawnE, "id", m_pInfo->id() );
76
77 return spawnE;
78}
79
80bool KivioPyStencilSpawner::load( const TQString &file )
81{
82 KivioConnectorTarget *pTarget;
83 TQDomDocument d("test");
84
85 m_filename = TQString(file);
86 TQFile f(file);
87
88 if( f.open( IO_ReadOnly )==false )
89 {
90 kdDebug(43000) << "KivioPyStencilSpawner::load() - Error opening stencil: " << file << endl;
91 return false;
92 }
93
94 d.setContent(&f);
95
96 TQDomElement root = d.documentElement();
97 TQDomElement e;
98 TQDomNode node = root.firstChild();
99 TQString nodeName;
100
101 while( !node.isNull() )
102 {
103 nodeName = node.nodeName();
104
105 if( nodeName.compare("KivioPyStencilSpawnerInfo")==0 )
106 {
107 m_pInfo->loadXML( (const TQDomElement)node.toElement() );
108 }
109 else if( nodeName.compare("init")==0 )
110 {
111 m_pStencil->setSpawner(this);
112 if ( ! m_pStencil->init( node.toElement().text() ) ) {
113 return false;
114 }
115 // init connector targets list
116 PyObject *targets = PyDict_GetItemString(m_pStencil->vars,"connector_targets");
117 int size = PyList_Size( targets );
118 for ( int i=0; i<size; i++ ) {
119
120 PyObject *target = PyList_GetItem( targets, i );
121 float x = m_pStencil->getDoubleFromDict( target,"x");
122 float y = m_pStencil->getDoubleFromDict( target,"y");
123 pTarget = new KivioConnectorTarget(x,y);
124 m_pStencil->m_pConnectorTargets->append( pTarget );
125 m_pTargets->append(pTarget->duplicate());
126 }
127 m_defWidth = m_pStencil->getDoubleFromDict( m_pStencil->vars, "w");
128 m_defHeight = m_pStencil->getDoubleFromDict( m_pStencil->vars, "h");
129
130 }
131
132 else if( nodeName.compare("resize")==0 )
133 {
134 e = node.toElement();
135 m_pStencil->resizeCode = node.toElement().text();
136 }
137 else
138 {
139 kdDebug(43000) << "KivioPyStencilSpawner::load() - Unknown node " << nodeName << " while loading " << file << endl;
140 }
141
142 node = node.nextSibling();
143 }
144
145 // Now load the xpm
146 TQFileInfo finfo(file);
147 TQString pixFile = finfo.dirPath(true) + "/" + finfo.baseName() + ".xpm";
148
149 if(!m_icon.load( pixFile )) {
150 TQString pixFile = finfo.dirPath(true) + "/" + finfo.baseName() + ".png";
151 m_icon.load( pixFile );
152 }
153
154 f.close();
155
156 return true;
157}
158
159
160
164KivioStencil *KivioPyStencilSpawner::newStencil()
165{
166
167 KivioStencil *pNewStencil = m_pStencil->duplicate();
168
169 return pNewStencil;
170}
171
172#endif // HAVE_PYTHON