variable.cpp
1/***************************************************************************
2 * This file is part of the KDE project
3 * copyright (C) 2005 by Sebastian Sauer (mail@dipe.org)
4 * copyright (C) 2005 by Tobi Krebs (tobi.krebs@gmail.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
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 GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#include "variable.h"
21#include "exception.h"
22
23#include <kdebug.h>
24
25using namespace KoMacro;
26
27namespace KoMacro {
28
33 class Variable::Private
34 {
35 public:
36
40 TQString name;
41
46 TQString text;
47
52 TQVariant variant;
53
58 const TQObject* object;
59
63 // TODO Dow we use this or is it for the future??
64 Variable::List children;
65
69 bool enabled;
70
71 explicit Private()
72 : enabled(true)
73 {
74 }
75 };
76
77}
78
81 , d( new Private() ) // create the private d-pointer instance.
82{
83 setType(TypeNone);
84 d->object = 0;
85}
86
87Variable::Variable(const TQVariant& variant, const TQString& name, const TQString& text)
89 , d( new Private() ) // create the private d-pointer instance.
90{
91 setVariantType(variant.type());
92 d->variant = variant;
93 d->object = 0;
94 d->name = name;
95 d->text = text;
96}
97
98Variable::Variable(const TQObject* object)
100 , d( new Private() ) // create the private d-pointer instance.
101{
103 d->object = object;
104}
105
106Variable::Variable(const TQDomElement& element)
107 : MetaParameter()
108 , d( new Private() ) // create the private d-pointer instance.
109{
110
111 TQString typesignature = element.attribute("type", "const TQString&");
112 TQString value = element.text();
113
114 setSignatureArgument( typesignature );
115
116 switch( type() ) {
118 //kdDebug() << TQString("KoMacro::Variable(TQDomElement) KoMacro::MetaParameter::TypeVariant") << endl;
119 // Set the variant without overwritting the previously detected varianttype.
120 setVariant( TQVariant(value), false );
121 } break;
123 //kdDebug() << TQString("KoMacro::Variable(TQDomElement) KoMacro::MetaParameter::TypeObject") << endl;
124 //TODO setObject();
125 } break;
126 default: {
127 kdWarning() << TQString("KoMacro::Variable(TQDomElement) KoMacro::MetaParameter::TypeNone") << endl;
128 } break;
129 }
130}
131
133{
134 delete d;
135}
136
137TQString Variable::name() const
138{
139 return d->name;
140}
141
142void Variable::setName(const TQString& name)
143{
144 d->name = name;
145}
146
147TQString Variable::text() const
148{
149 return d->text;
150}
151
152void Variable::setText(const TQString& text)
153{
154 d->text = text;
155}
156
157const TQVariant Variable::variant() const
158{
159 //Q_ASSERT( type() == MetaParameter::TypeVariant );
160 //Q_ASSERT( variantType() != TQVariant::Invalid );
161 //if(variantType() == TQVariant::Invalid) return TQVariant();
162 return d->variant;
163}
164
165void Variable::setVariant(const TQVariant& variant, bool detecttype)
166{
167 if(detecttype) {
168 setVariantType( variant.type() );
169 }
170 d->variant = variant;
171}
172
173const TQObject* Variable::object() const
174{
175 Q_ASSERT( ! d->object );
176 return d->object;
177}
178
179void Variable::setObject(const TQObject* object)
180{
182 d->object = object;
183}
184
185Variable::operator TQVariant () const
186{
187 return variant();
188}
189
190Variable::operator const TQObject* () const
191{
192 return object();
193}
194
195const TQString Variable::toString() const
196{
197 switch( type() ) {
199 return variant().toString();
200 } break;
202 return TQString("[%1]").arg( object()->name() );
203 } break;
204 default: {
205 throw Exception("Type is undefined.");
206 } break;
207 }
208 return TQString();
209}
210
212{
213 return variant().toInt();
214}
215
217{
218 return d->children;
219}
220
221void Variable::appendChild(TDESharedPtr<Variable> variable)
222{
223 d->children.append(variable);
224}
225
227{
228 d->children.clear();
229}
230
232{
233 d->children = children;
234}
235
236/*
237bool Variable::isEnabled() const
238{
239 return d->enabled;
240}
241
242void Variable::setEnabled(const bool enabled)
243{
244 d->enabled = enabled;
245}
246*/
Base Exception class.
Definition exception.h:35
Class to provide abstract methods for the undocumented TQt3 TQUObject-API functionality.
void setType(Type type)
Set the MetaParameter::Type this variable is.
@ TypeObject
The MetaParameter is a TQVariant.
@ TypeVariant
None type, the MetaParameter is empty.
void setVariantType(TQVariant::Type varianttype)
Set the MetaParameter::Type this variable is.
void setText(const TQString &text)
Set the caption.
Definition variable.cpp:152
TQString text() const
Definition variable.cpp:147
void clearChildren()
Clear the list of children this Variable has.
Definition variable.cpp:226
TQString name() const
Definition variable.cpp:137
void appendChild(TDESharedPtr< Variable > variable)
Append a Variable to the list of children this Variable has.
Definition variable.cpp:221
Variable()
Default constructor.
Definition variable.cpp:79
void setVariant(const TQVariant &variant, bool detecttype=true)
Set the TQVariant.
Definition variable.cpp:165
const TQVariant variant() const
Definition variable.cpp:157
TQValueList< TDESharedPtr< Variable > > List
Property to get and set a TQVariant as variable.
Definition variable.h:61
int toInt() const
Definition variable.cpp:211
List children() const
Definition variable.cpp:216
void setChildren(const List &children)
Set the children this Variable has.
Definition variable.cpp:231
const TQString toString() const
Definition variable.cpp:195
void setName(const TQString &name)
Set the name.
Definition variable.cpp:142
void setObject(const TQObject *object)
Set the TQObject.
Definition variable.cpp:179
virtual ~Variable()
Destructor.
Definition variable.cpp:132
const TQObject * object() const
Definition variable.cpp:173