kivio

kivio_py_stencil_spawner.cpp

00001 /*
00002  * Kivio - Visual Modelling and Flowcharting
00003  * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "kivio_py_stencil_spawner.h"
00021 
00022 #ifdef HAVE_PYTHON
00023 
00024 #include "kivio_common.h"
00025 #include "kivio_connector_target.h"
00026 #include "kivio_py_stencil.h"
00027 #include "kivio_stencil_spawner_set.h"
00028 #include "kivio_stencil_spawner.h"
00029 #include "kivio_stencil_spawner_info.h"
00030 
00031 #include <iostream>
00032 #include <qdom.h>
00033 #include <qfile.h>
00034 #include <qiodevice.h>
00035 #include <qpainter.h>
00036 #include <qpoint.h>
00037 #include <qpixmap.h>
00038 #include <qrect.h>
00039 #include <qfileinfo.h>
00040 
00041 #include <kdebug.h>
00042 
00043 KivioPyStencilSpawner::KivioPyStencilSpawner( KivioStencilSpawnerSet *p )
00044     : KivioStencilSpawner( p ),
00045       m_pStencil(NULL)
00046 {
00047     m_pStencil = new KivioPyStencil();
00048     m_pStencil->setSpawner(this);
00049 
00050     m_pTargets = new QPtrList<KivioConnectorTarget>;
00051     m_pTargets->setAutoDelete(true);
00052 }
00053 
00054 KivioPyStencilSpawner::~KivioPyStencilSpawner()
00055 {
00056     if( m_pStencil )
00057     {
00058         delete m_pStencil;
00059         m_pStencil = NULL;
00060     }
00061 
00062     if( m_pTargets )
00063     {
00064         delete m_pTargets;
00065         m_pTargets = NULL;
00066     }
00067 
00068     kdDebug(43000) << "* PyStencilSpawner "<< m_pInfo->id() << " deleted" << endl;
00069 }
00070 
00071 QDomElement KivioPyStencilSpawner::saveXML( QDomDocument &doc )
00072 {
00073     QDomElement spawnE = doc.createElement("KivioPyStencilSpawner");
00074 
00075     XmlWriteString( spawnE, "id", m_pInfo->id() );
00076 
00077     return spawnE;
00078 }
00079 
00080 bool KivioPyStencilSpawner::load( const QString &file )
00081 {
00082     KivioConnectorTarget *pTarget;
00083     QDomDocument d("test");
00084 
00085     m_filename = QString(file);
00086     QFile f(file);
00087 
00088     if( f.open( IO_ReadOnly )==false )
00089     {
00090        kdDebug(43000) << "KivioPyStencilSpawner::load() - Error opening stencil: " << file << endl;
00091         return false;
00092     }
00093 
00094     d.setContent(&f);
00095 
00096     QDomElement root = d.documentElement();
00097     QDomElement e;
00098     QDomNode node = root.firstChild();
00099     QString nodeName;
00100 
00101     while( !node.isNull() )
00102     {
00103         nodeName = node.nodeName();
00104 
00105         if( nodeName.compare("KivioPyStencilSpawnerInfo")==0 )
00106         {
00107             m_pInfo->loadXML( (const QDomElement)node.toElement() );
00108         }
00109         else if( nodeName.compare("init")==0 )
00110         {
00111            m_pStencil->setSpawner(this);
00112            if ( ! m_pStencil->init( node.toElement().text() ) ) {
00113               return false;
00114            }
00115            // init connector targets list
00116            PyObject *targets = PyDict_GetItemString(m_pStencil->vars,"connector_targets");
00117            int size = PyList_Size( targets );
00118            for ( int i=0; i<size; i++ ) {
00119 
00120                 PyObject *target = PyList_GetItem( targets, i );
00121                 float x = m_pStencil->getDoubleFromDict( target,"x");
00122                 float y = m_pStencil->getDoubleFromDict( target,"y");
00123                 pTarget = new KivioConnectorTarget(x,y);
00124                 m_pStencil->m_pConnectorTargets->append( pTarget );
00125                 m_pTargets->append(pTarget->duplicate());
00126            }
00127            m_defWidth  = m_pStencil->getDoubleFromDict( m_pStencil->vars, "w");
00128            m_defHeight = m_pStencil->getDoubleFromDict( m_pStencil->vars, "h");
00129 
00130         }
00131 
00132         else if( nodeName.compare("resize")==0 )
00133         {
00134             e = node.toElement();
00135             m_pStencil->resizeCode = node.toElement().text();
00136         }
00137         else
00138         {
00139            kdDebug(43000) << "KivioPyStencilSpawner::load() - Unknown node " << nodeName << " while loading " << file << endl;
00140         }
00141 
00142         node = node.nextSibling();
00143     }
00144 
00145     // Now load the xpm
00146     QFileInfo finfo(file);
00147     QString pixFile = finfo.dirPath(true) + "/" + finfo.baseName() + ".xpm";
00148 
00149     if(!m_icon.load( pixFile )) {
00150       QString pixFile = finfo.dirPath(true) + "/" + finfo.baseName() + ".png";
00151       m_icon.load( pixFile );
00152     }
00153 
00154     f.close();
00155 
00156     return true;
00157 }
00158 
00159 
00160 
00164 KivioStencil *KivioPyStencilSpawner::newStencil()
00165 {
00166 
00167     KivioStencil *pNewStencil = m_pStencil->duplicate();
00168 
00169     return pNewStencil;
00170 }
00171 
00172 #endif // HAVE_PYTHON
KDE Home | KDE Accessibility Home | Description of Access Keys