kivio

kivio_shape.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 #include <stdio.h>
00020 #include <kdebug.h>
00021 #include "kivio_common.h"
00022 #include "kivio_fill_style.h"
00023 #include "kivio_line_style.h"
00024 #include "kivio_point.h"
00025 #include "kivio_shape.h"
00026 #include "kivio_text_style.h"
00027 
00033 KivioShape::KivioShape()
00034 {
00035 }
00036 
00037 
00045 KivioShape::KivioShape( const KivioShape &source )
00046 {
00047     source.m_shapeData.copyInto( &m_shapeData );
00048 }
00049 
00050 
00054 KivioShape::~KivioShape()
00055 {
00056 }
00057 
00058 
00066 void KivioShape::copyInto( KivioShape *pTarget ) const
00067 {
00068     if( !pTarget )
00069         return;
00070 
00071     m_shapeData.copyInto( &(pTarget->m_shapeData) );
00072 }
00073 
00074 
00081 bool KivioShape::loadXML( const QDomElement &e )
00082 {
00083     QDomElement ele;
00084     QDomNode node = e.firstChild();
00085 
00086     QString name = XmlReadString( e, "name", "" );
00087     m_shapeData.setName( name );
00088     m_shapeData.setShapeType( (KivioShapeData::KivioShapeType)XmlReadInt( e, "shapeType", -1 ));
00089 
00090     if( m_shapeData.name().isEmpty() ||
00091         m_shapeData.shapeType() == -1 )
00092     {
00093        kdWarning(43000) << "-LOAD KivioShape::loadXML() - Unknown shape or bad name read. Shape load aborted." << endl;
00094        return false;
00095     }
00096 
00097     while( !node.isNull() )
00098     {
00099         QString nodeName = node.nodeName();
00100         ele = node.toElement();
00101 
00102         if( nodeName == "KivioShapeData" )
00103         {
00104             m_shapeData.loadXML( ele );
00105         }
00106 
00107         node = node.nextSibling();
00108     }
00109     return true;
00110 }
00111 
00112 
00119 QDomElement KivioShape::saveXML( QDomDocument &doc )
00120 {
00121     QDomElement e = doc.createElement("KivioShape");
00122 
00123     XmlWriteString( e, "name", m_shapeData.m_name );
00124     XmlWriteInt( e, "shapeType", m_shapeData.m_shapeType );
00125 
00126     e.appendChild( m_shapeData.saveXML( doc ) );
00127 
00128     return e;
00129 }
00130 
00131 
00140 KivioShape *KivioShape::loadShapeArc( const QDomElement & )
00141 {
00142     return NULL;
00143 }
00144 
00145 
00152 KivioShape *KivioShape::loadShapeClosedPath( const QDomElement &e )
00153 {
00154     KivioShape *pShape = NULL;
00155     KivioPoint *pPoint = NULL;
00156     QDomNode node;
00157     QString nodeName;
00158     
00159 
00160     // Create the new shape to load into
00161     pShape = new KivioShape();
00162     
00163     // Load the type, name, and lineWidth properties
00164     pShape->m_shapeData.m_shapeType = KivioShapeData::kstClosedPath;
00165     pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );
00166 
00167     // Iterate through the nodes loading the various items
00168     node = e.firstChild();
00169     while( !node.isNull() )
00170     {
00171         nodeName = node.nodeName();
00172         
00173         // The line array  is made up of pairs of points
00174         if( nodeName == "KivioPoint" )
00175         {
00176             // Allocate a new point, load it, and store it in the list
00177             pPoint = new KivioPoint(0.0f, 0.0f, KivioPoint::kptBezier);
00178             pPoint->loadXML( node.toElement() );
00179             pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
00180             pPoint = NULL;
00181         }
00182         else if( nodeName == "KivioFillStyle" )
00183         {
00184             pShape->m_shapeData.m_pFillStyle->loadXML( node.toElement() );
00185         }
00186         else if( nodeName == "KivioLineStyle" )
00187         {
00188             pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
00189         }
00190     
00191         node = node.nextSibling();
00192     }
00193         
00194     return pShape;
00195 }
00196 
00197 
00204 KivioShape *KivioShape::loadShapeBezier( const QDomElement &e )
00205 {
00206     KivioShape *pShape = NULL;
00207     KivioPoint *pPoint = NULL;
00208     QDomNode node;
00209     QString nodeName;
00210     
00211 
00212     // Create the new shape to load into
00213     pShape = new KivioShape();
00214     
00215     // Load the type, name, and lineWidth properties
00216     pShape->m_shapeData.m_shapeType = KivioShapeData::kstBezier;
00217     pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );
00218 
00219     // Iterate through the nodes loading the various items
00220     node = e.firstChild();
00221     while( !node.isNull() )
00222     {
00223         nodeName = node.nodeName();
00224         
00225         // The line array  is made up of pairs of points
00226         if( nodeName == "KivioPoint" )
00227         {
00228             // Allocate a new point, load it, and store it in the list
00229             pPoint = new KivioPoint(0.0f, 0.0f, KivioPoint::kptBezier);
00230             pPoint->loadXML( node.toElement() );
00231             if( pPoint->pointType() != KivioPoint::kptBezier )
00232             {
00233            kdDebug(43000) << "KivioShape::loadShapeBezier() - Non-bezier point found.  Aborting shape." << endl;
00234                 delete pPoint;
00235                 delete pShape;
00236                 return NULL;
00237             }
00238             pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
00239             pPoint = NULL;
00240         }
00241         else if( nodeName == "KivioLineStyle" )
00242         {
00243             pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
00244         }
00245 
00246         node = node.nextSibling();
00247     }
00248     
00249     if( pShape->m_shapeData.m_pOriginalPointList->count() != 4 )
00250     {
00251        kdDebug(43000) << "KivioShape::loadShapeBezier() - Wrong number of points loaded, should be 4, shape aborted" << endl;
00252         delete pShape;
00253         return NULL;
00254     }
00255     
00256     return pShape;
00257 }
00258 
00259 
00266 KivioShape *KivioShape::loadShapeEllipse( const QDomElement &e )
00267 {
00268     KivioShape *pShape = NULL;
00269     QDomNode node;
00270     QString nodeName;
00271     
00272 
00273     // Create the new shape to load into
00274     pShape = new KivioShape();
00275     
00276     // Load the type, name, and lineWidth properties
00277     pShape->m_shapeData.m_shapeType = KivioShapeData::kstEllipse;
00278     pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );
00279 
00280     pShape->m_shapeData.m_position.set( XmlReadFloat( e, "x", 0.0f ), XmlReadFloat( e, "y", 0.0f ) );
00281     pShape->m_shapeData.m_dimensions.set( XmlReadFloat( e, "w", 0.0f ), XmlReadFloat( e, "h", 0.0f ) );
00282         
00283     // Iterate through the nodes loading the various items
00284     node = e.firstChild();
00285     while( !node.isNull() )
00286     {
00287         nodeName = node.nodeName();
00288         
00289         if( nodeName == "KivioFillStyle" )
00290         {
00291             pShape->m_shapeData.m_pFillStyle->loadXML( node.toElement() );
00292         }
00293         else if( nodeName == "KivioLineStyle" )
00294         {
00295             pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
00296         }
00297 
00298         node = node.nextSibling();
00299     }
00300     
00301     return pShape;
00302 }
00303 
00304 
00311 KivioShape *KivioShape::loadShapeLineArray( const QDomElement &e )
00312 {
00313     KivioShape *pShape = NULL;
00314     KivioPoint *pPoint = NULL;
00315     QDomNode node;
00316     QString nodeName;
00317     QDomElement lineElement;
00318     
00319 
00320     // Create the new shape to load into
00321     pShape = new KivioShape();
00322     
00323     // Load the type, name, and lineWidth properties
00324     pShape->m_shapeData.m_shapeType = KivioShapeData::kstLineArray;
00325     pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );
00326 
00327     // Iterate through the nodes loading the various items
00328     node = e.firstChild();
00329     while( !node.isNull() )
00330     {
00331         nodeName = node.nodeName();
00332         
00333         // The line array  is made up of pairs of points
00334         if( nodeName == "Line" )
00335         {
00336             lineElement = node.toElement();
00337 
00338             // Allocate a new point, load it, and store it in the list
00339             pPoint = new KivioPoint( XmlReadFloat( lineElement, "x1", 0.0f ), XmlReadFloat( lineElement, "y1", 0.0f ) );
00340             pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
00341             pPoint = new KivioPoint( XmlReadFloat( lineElement, "x2", 0.0f ), XmlReadFloat( lineElement, "y2", 0.0f ) );
00342             pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
00343             pPoint = NULL;
00344         }
00345         else if( nodeName == "KivioLineStyle" )
00346         {
00347             pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
00348         }
00349 
00350         node = node.nextSibling();
00351     }
00352 
00353     return pShape;
00354 }
00355 
00356 
00363 KivioShape *KivioShape::loadShapeOpenPath( const QDomElement &e )
00364 {
00365     KivioShape *pShape = NULL;
00366     KivioPoint *pPoint = NULL;
00367     QDomNode node;
00368     QString nodeName;
00369     
00370 
00371     // Create the new shape to load into
00372     pShape = new KivioShape();
00373     
00374     // Load the type, name, and lineWidth properties
00375     pShape->m_shapeData.m_shapeType = KivioShapeData::kstOpenPath;
00376     pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );
00377 
00378     // Iterate through the nodes loading the various items
00379     node = e.firstChild();
00380     while( !node.isNull() )
00381     {
00382         nodeName = node.nodeName();
00383         
00384         // The line array  is made up of pairs of points
00385         if( nodeName == "KivioPoint" )
00386         {
00387             // Allocate a new point, load it, and store it in the list
00388             pPoint = new KivioPoint(0.0f, 0.0f, KivioPoint::kptBezier);
00389             pPoint->loadXML( node.toElement() );
00390             pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
00391             pPoint = NULL;
00392         }
00393         else if( nodeName == "KivioLineStyle" )
00394         {
00395             pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
00396         }
00397 
00398         node = node.nextSibling();
00399     }
00400         
00401     return pShape;
00402 }
00403 
00404 
00413 KivioShape *KivioShape::loadShapePie( const QDomElement & )
00414 {
00415     return NULL;
00416 }
00417 
00418 
00425 KivioShape *KivioShape::loadShapePolygon( const QDomElement &e )
00426 {
00427     KivioShape *pShape = NULL;
00428     KivioPoint *pPoint = NULL;
00429     QDomNode node;
00430     QString nodeName;
00431     
00432 
00433     // Create the new shape to load into
00434     pShape = new KivioShape();
00435     
00436     // Load the type, name, and lineWidth properties
00437     pShape->m_shapeData.m_shapeType = KivioShapeData::kstPolygon;
00438     pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );
00439 
00440     // Iterate through the nodes loading the various items
00441     node = e.firstChild();
00442     while( !node.isNull() )
00443     {
00444         nodeName = node.nodeName();
00445         
00446         // The polygon is made up of a series of points
00447         if( nodeName == "KivioPoint" )
00448         {
00449             // Allocate a new point, load it, and store it in the list
00450             pPoint = new KivioPoint();
00451             pPoint->loadXML( node.toElement() );
00452             pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
00453             pPoint = NULL;
00454         }
00455         else if( nodeName == "KivioFillStyle" )
00456         {
00457             pShape->m_shapeData.m_pFillStyle->loadXML( node.toElement() );
00458         }
00459         else if( nodeName == "KivioLineStyle" )
00460         {
00461             pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
00462         }
00463 
00464         node = node.nextSibling();
00465     }
00466     
00467     return pShape;
00468 }
00469 
00470 
00477 KivioShape *KivioShape::loadShapePolyline( const QDomElement &e )
00478 {
00479     KivioShape *pShape = NULL;
00480     KivioPoint *pPoint = NULL;
00481     QDomNode node;
00482     QString nodeName;
00483     
00484 
00485     // Create the new shape to load into
00486     pShape = new KivioShape();
00487     
00488     // Load the type, name, and lineWidth properties
00489     pShape->m_shapeData.m_shapeType = KivioShapeData::kstPolyline;
00490     pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );
00491 
00492     // Iterate through the nodes loading the various items
00493     node = e.firstChild();
00494     while( !node.isNull() )
00495     {
00496         nodeName = node.nodeName();
00497         
00498         // The polygon is made up of a series of points
00499         if( nodeName == "KivioPoint" )
00500         {
00501             // Allocate a new point, load it, and store it in the list
00502             pPoint = new KivioPoint();
00503             pPoint->loadXML( node.toElement() );
00504             pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
00505             pPoint = NULL;
00506         }
00507         else if( nodeName == "KivioLineStyle" )
00508         {
00509             pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
00510         }
00511 
00512         node = node.nextSibling();
00513     }
00514     
00515     return pShape;
00516 }
00517 
00518 
00525 KivioShape *KivioShape::loadShapeRectangle( const QDomElement &e )
00526 {
00527     KivioShape *pShape = NULL;
00528     QDomNode node;
00529     QString nodeName;
00530     
00531 
00532     // Create the new shape to load into
00533     pShape = new KivioShape();
00534     
00535     // Load the type, name, and lineWidth properties
00536     pShape->m_shapeData.m_shapeType = KivioShapeData::kstRectangle;
00537     pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );
00538 
00539     pShape->m_shapeData.m_position.set( XmlReadFloat( e, "x", 0.0f ), XmlReadFloat( e, "y", 0.0f ) );
00540     pShape->m_shapeData.m_dimensions.set( XmlReadFloat( e, "w", 72.0f ), XmlReadFloat( e, "h", 72.0f ) );
00541     
00542     // Iterate through the nodes loading the various items
00543     node = e.firstChild();
00544     while( !node.isNull() )
00545     {
00546         nodeName = node.nodeName();
00547         
00548         // Read the fill style
00549         if( nodeName == "KivioFillStyle" )
00550         {
00551             pShape->m_shapeData.m_pFillStyle->loadXML( node.toElement() );
00552         }
00553         else if( nodeName == "KivioLineStyle" )
00554         {
00555             pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
00556         }
00557 
00558         node = node.nextSibling();
00559     }
00560     
00561     return pShape;
00562 }
00563 
00564 
00565 
00572 KivioShape *KivioShape::loadShapeRoundRectangle( const QDomElement &e )
00573 {
00574     KivioShape *pShape = NULL;
00575     KivioPoint *pPoint = NULL;
00576     QDomNode node;
00577     QString nodeName;
00578     
00579 
00580     // Create the new shape to load into
00581     pShape = new KivioShape();
00582     
00583     // Load the type, name, and lineWidth properties
00584     pShape->m_shapeData.m_shapeType = KivioShapeData::kstRoundRectangle;
00585     pShape->m_shapeData.m_name = XmlReadString( e, "name", "" );
00586 
00587     pShape->m_shapeData.m_position.set( XmlReadFloat( e, "x", 0.0f ), XmlReadFloat( e, "y", 0.0f ) );
00588     pShape->m_shapeData.m_dimensions.set( XmlReadFloat( e, "w", 72.0f ), XmlReadFloat( e, "h", 72.0f ) );
00589     
00590     // Read and store the radii of the curves
00591     pPoint = new KivioPoint(0.0f, 0.0f);
00592     pPoint->set( XmlReadFloat( e, "r1", 1.0f ), XmlReadFloat( e, "r2", 1.0f ) );
00593     pShape->m_shapeData.m_pOriginalPointList->append( pPoint );
00594     
00595     // Iterate through the nodes loading the various items
00596     node = e.firstChild();
00597     while( !node.isNull() )
00598     {
00599         nodeName = node.nodeName();
00600         
00601         // Read the fill style
00602         if( nodeName == "KivioFillStyle" )
00603         {
00604             pShape->m_shapeData.m_pFillStyle->loadXML( node.toElement() );
00605         }
00606         else if( nodeName == "KivioLineStyle" )
00607         {
00608             pShape->m_shapeData.m_pLineStyle->loadXML( node.toElement() );
00609         }
00610 
00611         node = node.nextSibling();
00612     }
00613     
00614     return pShape;
00615 }
00616 
00617 
00624 KivioShape *KivioShape::loadShapeTextBox( const QDomElement &e )
00625 {
00626     KivioShape *pShape = NULL;
00627     QDomNode node;
00628     QString nodeName;
00629     KivioTextStyle ts;
00630     QString name;
00631 
00632 
00633     // Create the new shape to load into
00634     pShape = new KivioShape();
00635 
00636     // Load the type and name
00637     pShape->m_shapeData.setShapeType(KivioShapeData::kstTextBox);
00638     pShape->m_shapeData.setName( XmlReadString( e, "name", "" ) );
00639     pShape->m_shapeData.setTextColor(XmlReadColor(e,"color",QColor(0,0,0)));
00640 
00641     pShape->m_shapeData.m_position.set( 
00642        XmlReadFloat( e, "x", 0.0f ), XmlReadFloat( e, "y", 0.0f ) );
00643     pShape->m_shapeData.m_dimensions.set( 
00644        XmlReadFloat( e, "w", 72.0f ), XmlReadFloat( e, "h", 72.0f ) );
00645 
00646     // Now look for the KivioTextStyle
00647     node = e.firstChild();
00648     while( !node.isNull() )
00649     {
00650        nodeName = node.nodeName();
00651        if( nodeName == "KivioTextStyle" )
00652        {
00653       ts.loadXML( node.toElement() );
00654       pShape->m_shapeData.setTextStyle( &ts );
00655        }
00656 
00657        node = node.nextSibling();
00658     }
00659 //    QString text = XmlReadString( e, "text", "" );
00660 //    pShape->m_shapeData.setText( text );
00661 
00662 
00663     return pShape;
00664 }
KDE Home | KDE Accessibility Home | Description of Access Keys