kexi

variable.cpp

00001 /***************************************************************************
00002  * This file is part of the KDE project
00003  * copyright (C) 2005 by Sebastian Sauer (mail@dipe.org)
00004  * copyright (C) 2005 by Tobi Krebs (tobi.krebs@gmail.com)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
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 GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #include "variable.h"
00021 #include "exception.h"
00022 
00023 #include <kdebug.h>
00024 
00025 using namespace KoMacro;
00026 
00027 namespace KoMacro {
00028 
00033     class Variable::Private
00034     {
00035         public:
00036 
00040             QString name;
00041 
00046             QString text;
00047 
00052             QVariant variant;
00053 
00058             const QObject* object;
00059 
00063             // TODO Dow we use this or is it for the future??
00064             Variable::List children;
00065 
00069             bool enabled;
00070 
00071             explicit Private()
00072                 : enabled(true)
00073             {
00074             }
00075     };
00076 
00077 }
00078 
00079 Variable::Variable()
00080     : MetaParameter()
00081     , d( new Private() ) // create the private d-pointer instance.
00082 {
00083     setType(TypeNone);
00084     d->object = 0;
00085 }
00086 
00087 Variable::Variable(const QVariant& variant, const QString& name, const QString& text)
00088     : MetaParameter()
00089     , d( new Private() ) // create the private d-pointer instance.
00090 {
00091     setVariantType(variant.type());
00092     d->variant = variant;
00093     d->object = 0;
00094     d->name = name;
00095     d->text = text;
00096 }
00097 
00098 Variable::Variable(const QObject* object)
00099     : MetaParameter()
00100     , d( new Private() ) // create the private d-pointer instance.
00101 {
00102     setType(TypeObject);
00103     d->object = object;
00104 }
00105 
00106 Variable::Variable(const QDomElement& element)
00107     : MetaParameter()
00108     , d( new Private() ) // create the private d-pointer instance.
00109 {
00110 
00111     QString typesignature = element.attribute("type", "const QString&");
00112     QString value = element.text();
00113 
00114     setSignatureArgument( typesignature );
00115 
00116     switch( type() ) {
00117         case KoMacro::MetaParameter::TypeVariant: {
00118             //kdDebug() << QString("KoMacro::Variable(QDomElement) KoMacro::MetaParameter::TypeVariant") << endl;
00119             // Set the variant without overwritting the previously detected varianttype.
00120             setVariant( QVariant(value), false );
00121         } break;
00122         case KoMacro::MetaParameter::TypeObject: {
00123             //kdDebug() << QString("KoMacro::Variable(QDomElement) KoMacro::MetaParameter::TypeObject") << endl;
00124             //TODO setObject();
00125         } break;
00126         default: {
00127             kdWarning() << QString("KoMacro::Variable(QDomElement) KoMacro::MetaParameter::TypeNone") << endl;
00128         } break;
00129     }
00130 }
00131 
00132 Variable::~Variable()
00133 {
00134     delete d;
00135 }
00136 
00137 QString Variable::name() const
00138 {
00139     return d->name;
00140 }
00141 
00142 void Variable::setName(const QString& name)
00143 {
00144     d->name = name;
00145 }
00146 
00147 QString Variable::text() const
00148 {
00149     return d->text;
00150 }
00151 
00152 void Variable::setText(const QString& text)
00153 {
00154     d->text = text;
00155 }
00156 
00157 const QVariant Variable::variant() const
00158 {
00159     //Q_ASSERT( type() == MetaParameter::TypeVariant );
00160     //Q_ASSERT( variantType() != QVariant::Invalid );
00161     //if(variantType() == QVariant::Invalid) return QVariant();
00162     return d->variant;
00163 }
00164 
00165 void Variable::setVariant(const QVariant& variant, bool detecttype)
00166 {
00167     if(detecttype) {
00168         setVariantType( variant.type() );
00169     }
00170     d->variant = variant;
00171 }
00172 
00173 const QObject* Variable::object() const
00174 {
00175     Q_ASSERT( ! d->object );
00176     return d->object;
00177 }
00178 
00179 void Variable::setObject(const QObject* object)
00180 {
00181     setType(TypeObject);
00182     d->object = object;
00183 }
00184 
00185 Variable::operator QVariant () const
00186 {
00187     return variant();
00188 }
00189 
00190 Variable::operator const QObject* () const
00191 {
00192     return object();
00193 }
00194 
00195 const QString Variable::toString() const
00196 {
00197     switch( type() ) {
00198         case KoMacro::MetaParameter::TypeVariant: {
00199             return variant().toString();
00200         } break;
00201         case KoMacro::MetaParameter::TypeObject: {
00202             return QString("[%1]").arg( object()->name() );
00203         } break;
00204         default: {
00205             throw Exception("Type is undefined.");
00206         } break;
00207     }
00208     return QString::null;
00209 }
00210 
00211 int Variable::toInt() const
00212 {
00213     return variant().toInt();
00214 }
00215 
00216 Variable::List Variable::children() const
00217 {
00218     return d->children;
00219 }
00220 
00221 void Variable::appendChild(KSharedPtr<Variable> variable)
00222 {
00223     d->children.append(variable);
00224 }
00225 
00226 void Variable::clearChildren()
00227 {
00228     d->children.clear();
00229 }
00230 
00231 void Variable::setChildren(const Variable::List& children)
00232 {
00233     d->children = children;
00234 }
00235 
00236 /*
00237 bool Variable::isEnabled() const
00238 {
00239     return d->enabled;
00240 }
00241 
00242 void Variable::setEnabled(const bool enabled)
00243 {
00244     d->enabled = enabled;
00245 }
00246 */
KDE Home | KDE Accessibility Home | Description of Access Keys