drawpart.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by David Saxton                                    *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "itemdocument.h"
00012 #include "itemdocumentdata.h"
00013 #include "drawpart.h"
00014 #include "variant.h"
00015 
00016 #include <klocale.h>
00017 #include <qbitarray.h>
00018 
00019 DrawPart::DrawPart( ItemDocument *itemDocument, bool newItem, const char *id )
00020         : Item( itemDocument, newItem, id )
00021 {
00022         itemDocument->registerItem(this);
00023 }
00024 
00025 
00026 DrawPart::~DrawPart()
00027 {
00028 }
00029 
00030 
00031 int DrawPart::rtti() const
00032 {
00033         return ItemDocument::RTTI::DrawPart;
00034 }
00035 
00036 
00037 Variant * DrawPart::createProperty( const QString & id, Variant::Type::Value type )
00038 {
00039         if ( type == Variant::Type::PenStyle )
00040         {
00041                 QStringList penStyles;
00042                 penStyles << DrawPart::penStyleToName(Qt::SolidLine) << DrawPart::penStyleToName(Qt::DashLine)
00043                                 << DrawPart::penStyleToName(Qt::DotLine) << DrawPart::penStyleToName(Qt::DashDotLine)
00044                                 << DrawPart::penStyleToName(Qt::DashDotDotLine);
00045         
00046                 Variant * v = createProperty( id, Variant::Type::String );
00047                 v->setType( Variant::Type::PenStyle );
00048                 v->setAllowed(penStyles);
00049                 return v;
00050         }
00051         
00052         if ( type == Variant::Type::PenCapStyle )
00053         {
00054                 QStringList penCapStyles;
00055                 penCapStyles << DrawPart::penCapStyleToName(Qt::FlatCap) << DrawPart::penCapStyleToName(Qt::SquareCap)
00056                                 << DrawPart::penCapStyleToName(Qt::RoundCap);
00057                 
00058                 Variant * v = createProperty( id, Variant::Type::String );
00059                 v->setType( Variant::Type::PenCapStyle );
00060                 v->setAllowed(penCapStyles);
00061                 return v;
00062         }
00063         
00064         return Item::createProperty( id, type );
00065 }
00066 
00067 
00068 Qt::PenStyle DrawPart::getDataPenStyle( const QString & id )
00069 {
00070         return nameToPenStyle( dataString(id) );
00071 }
00072 Qt::PenCapStyle DrawPart::getDataPenCapStyle( const QString & id )
00073 {
00074         return nameToPenCapStyle( dataString(id) );
00075 }
00076 void DrawPart::setDataPenStyle( const QString & id, Qt::PenStyle value )
00077 {
00078         property(id)->setValue( penStyleToName(value) );
00079 }
00080 void DrawPart::setDataPenCapStyle( const QString & id, Qt::PenCapStyle value )
00081 {
00082         property(id)->setValue( penCapStyleToName(value) );
00083 }
00084 
00085 
00086 ItemData DrawPart::itemData() const
00087 {
00088         ItemData itemData = Item::itemData();
00089         
00090         const VariantDataMap::const_iterator end = m_variantData.end();
00091         for ( VariantDataMap::const_iterator it = m_variantData.begin(); it != end; ++it )
00092         {
00093                 switch( it.data()->type() )
00094                 {
00095                         case Variant::Type::PenStyle:
00096                                 itemData.dataString[it.key()] = penStyleToID( nameToPenStyle( it.data()->value().toString() ) );
00097                                 break;
00098                         case Variant::Type::PenCapStyle:
00099                                 itemData.dataString[it.key()] = penCapStyleToID( nameToPenCapStyle( it.data()->value().toString() ) );
00100                                 break;
00101                         case Variant::Type::String:
00102                         case Variant::Type::FileName:
00103                         case Variant::Type::Port:
00104                         case Variant::Type::Pin:
00105                         case Variant::Type::VarName:
00106                         case Variant::Type::Combo:
00107                         case Variant::Type::Select:
00108                         case Variant::Type::Multiline:
00109                         case Variant::Type::Int:
00110                         case Variant::Type::Double:
00111                         case Variant::Type::Color:
00112                         case Variant::Type::Bool:
00113                         case Variant::Type::Raw:
00114                         case Variant::Type::SevenSegment:
00115                         case Variant::Type::KeyPad:
00116                         case Variant::Type::None:
00117                                 // All of these are handled by Item
00118                                 break;
00119                 }
00120         }
00121         
00122         return itemData;
00123 }
00124 
00125 
00126 void DrawPart::restoreFromItemData( const ItemData &itemData )
00127 {
00128         Item::restoreFromItemData(itemData);
00129         
00130         const QStringMap::const_iterator stringEnd = itemData.dataString.end();
00131         for ( QStringMap::const_iterator it = itemData.dataString.begin(); it != stringEnd; ++it )
00132         {
00133                 VariantDataMap::iterator vit = m_variantData.find(it.key());
00134                 if ( vit == m_variantData.end() )
00135                         continue;
00136                 
00137                 if ( vit.data()->type() == Variant::Type::PenStyle )
00138                         setDataPenStyle( it.key(), idToPenStyle( it.data() ) );
00139                 
00140                 else if ( vit.data()->type() == Variant::Type::PenCapStyle )
00141                         setDataPenCapStyle( it.key(), idToPenCapStyle( it.data() ) );
00142         }
00143 }
00144 
00145 
00146 
00147 QString DrawPart::penStyleToID( Qt::PenStyle style )
00148 {
00149         switch (style)
00150         {
00151                 case Qt::SolidLine:
00152                         return "SolidLine";
00153                 case Qt::NoPen:
00154                         return "NoPen";
00155                 case Qt::DashLine:
00156                         return "DashLine";
00157                 case Qt::DotLine:
00158                         return "DotLine";
00159                 case Qt::DashDotLine:
00160                         return "DashDotLine";
00161                 case Qt::DashDotDotLine:
00162                         return "DashDotDotLine";
00163                 case Qt::MPenStyle:
00164                 default:
00165                         return ""; // ?!
00166         }
00167 }
00168 Qt::PenStyle DrawPart::idToPenStyle( const QString & id )
00169 {
00170         if ( id == "NoPen" )
00171                 return Qt::NoPen;
00172         if ( id == "DashLine" )
00173                 return Qt::DashLine;
00174         if ( id == "DotLine" )
00175                 return Qt::DotLine;
00176         if ( id == "DashDotLine" )
00177                 return Qt::DashDotLine;
00178         if ( id == "DashDotDotLine" )
00179                 return Qt::DashDotDotLine;
00180         return Qt::SolidLine;
00181 }
00182 QString DrawPart::penCapStyleToID( Qt::PenCapStyle style )
00183 {
00184         switch (style)
00185         {
00186                 case Qt::FlatCap:
00187                         return "FlatCap";
00188                 case Qt::SquareCap:
00189                         return "SquareCap";
00190                 case Qt::RoundCap:
00191                         return "RoundCap";
00192                 case Qt::MPenCapStyle:
00193                 default:
00194                         return ""; // ?!
00195         }
00196 }
00197 Qt::PenCapStyle DrawPart::idToPenCapStyle( const QString & id )
00198 {
00199         if ( id == "SquareCap" )
00200                 return Qt::SquareCap;
00201         if ( id == "RoundCap" )
00202                 return Qt::RoundCap;
00203         return Qt::FlatCap;
00204 }
00205 
00206 QString DrawPart::penStyleToName( Qt::PenStyle style )
00207 {
00208         switch (style)
00209         {
00210                 case Qt::SolidLine:
00211                         return i18n("Solid");
00212                 case Qt::NoPen:
00213                         return i18n("None");
00214                 case Qt::DashLine:
00215                         return i18n("Dash");
00216                 case Qt::DotLine:
00217                         return i18n("Dot");
00218                 case Qt::DashDotLine:
00219                         return i18n("Dash Dot");
00220                 case Qt::DashDotDotLine:
00221                         return i18n("Dash Dot Dot");
00222                 case Qt::MPenStyle:
00223                 default:
00224                         return ""; // ?!
00225         }
00226 }
00227 Qt::PenStyle DrawPart::nameToPenStyle( const QString & name )
00228 {
00229         if ( name == i18n("None") )
00230                 return Qt::NoPen;
00231         if ( name == i18n("Dash") )
00232                 return Qt::DashLine;
00233         if ( name == i18n("Dot") )
00234                 return Qt::DotLine;
00235         if ( name == i18n("Dash Dot") )
00236                 return Qt::DashDotLine;
00237         if ( name == i18n("Dash Dot Dot") )
00238                 return Qt::DashDotDotLine;
00239         return Qt::SolidLine;
00240 }
00241 QString DrawPart::penCapStyleToName( Qt::PenCapStyle style )
00242 {
00243         switch (style)
00244         {
00245                 case Qt::FlatCap:
00246                         return i18n("Flat");
00247                 case Qt::SquareCap:
00248                         return i18n("Square");
00249                 case Qt::RoundCap:
00250                         return i18n("Round");
00251                 case Qt::MPenCapStyle:
00252                 default:
00253                         return ""; // ?!
00254         }
00255 }
00256 Qt::PenCapStyle DrawPart::nameToPenCapStyle( const QString & name )
00257 {
00258         if ( name == i18n("Square") )
00259                 return Qt::SquareCap;
00260         if ( name == i18n("Round") )
00261                 return Qt::RoundCap;
00262         return Qt::FlatCap;
00263 }
00264 

Generated on Tue May 8 17:05:29 2007 for KTechLab by  doxygen 1.5.1