macroitem.cpp
1/***************************************************************************
2 * This file is part of the KDE project
3 * copyright (C) 2006 by Sebastian Sauer (mail@dipe.org)
4 * copyright (C) 2006 by Sascha Kupper (kusato@kfnv.de)
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 "macroitem.h"
21
22#include <kdebug.h>
23
24using namespace KoMacro;
25
26namespace KoMacro {
27
32 class MacroItem::Private
33 {
34 public:
38 TDESharedPtr<Action> action;
39
43 TQString comment;
44
48 Variable::Map variables;
49
55 inline const TQVariant cast(const TQVariant& variant, TQVariant::Type type) const
56 {
57 // If ok is true the TQVariant v holds our new and to the correct type
58 // casted variant value. If ok is false the as argument passed variant
59 // TQVariant contains the (maybe uncasted string to prevent data-loosing
60 // what would happen if we e.g. would expect an integer and cast it to
61 // an incompatible non-int string) value.
62 bool ok = false;
63 TQVariant v;
64
65 // Try to cast the passed variant to the expected variant-type.
66 switch(type) {
67 case TQVariant::Bool: {
68 const TQString s = variant.toString();
69 ok = (s == "true" || s == "false" || s == "0" || s == "1" || s == "-1");
70 v = TQVariant( variant.toBool(), 0 );
71 } break;
72 case TQVariant::Int: {
73 v = variant.toInt(&ok);
74 // Check if the cast is correct.
75 Q_ASSERT(!ok || v.toString() == variant.toString());
76 } break;
77 case TQVariant::UInt: {
78 v = variant.toUInt(&ok);
79 Q_ASSERT(!ok || v.toString() == variant.toString());
80 } break;
81 case TQVariant::LongLong: {
82 v = variant.toLongLong(&ok);
83 Q_ASSERT(!ok || v.toString() == variant.toString());
84 } break;
85 case TQVariant::ULongLong: {
86 v = variant.toULongLong(&ok);
87 Q_ASSERT(!ok || v.toString() == variant.toString());
88 } break;
89 case TQVariant::Double: {
90 v = variant.toDouble(&ok);
91 Q_ASSERT(!ok || v.toString() == variant.toString());
92 } break;
93 case TQVariant::String: {
94 ok = true; // cast will always be successfully
95 v = variant.toString();
96 } break;
97 default: {
98 // If we got another type we try to let TQt handle it...
99 ok = v.cast(type);
100 kdWarning()<<"MacroItem::Private::cast() Unhandled ok="<<ok<<" type="<<type<<" value="<<v<<endl;
101 } break;
102 }
103
104 return ok ? v : variant;
105 }
106
107 };
108
109}
110
112 : TDEShared()
113 , d( new Private() )
114{
115}
116
118{
119 delete d;
120}
121
122TQString MacroItem::comment() const
123{
124 return d->comment;
125}
126
127void MacroItem::setComment(const TQString& comment)
128{
129 d->comment = comment;
130}
131
132TDESharedPtr<Action> MacroItem::action() const
133{
134 return d->action;
135}
136
137void MacroItem::setAction(TDESharedPtr<Action> action)
138{
139 d->action = action;
140}
141
142TQVariant MacroItem::variant(const TQString& name, bool checkaction) const
143{
144 TDESharedPtr<Variable> v = variable(name, checkaction);
145 return v.data() ? v->variant() : TQVariant();
146}
147
148TDESharedPtr<Variable> MacroItem::variable(const TQString& name, bool checkaction) const
149{
150 if(d->variables.contains(name))
151 return d->variables[name];
152 if(checkaction && d->action.data())
153 return d->action->variable(name);
154 return TDESharedPtr<Variable>(0);
155}
156
158{
159 return d->variables;
160}
161
162bool MacroItem::setVariant(const TQString& name, const TQVariant& variant)
163{
164 // Let's look if there is an action defined for the variable. If that's
165 // the case, we try to use that action to preserve the type of the variant.
166 TDESharedPtr<Variable> actionvariable = d->action ? d->action->variable(name) : TDESharedPtr<Variable>(0);
167
168 // If we know the expected type, we try to cast the variant to the expected
169 // type else the variant stays untouched (so, it will stay a string).
170 const TQVariant v = actionvariable.data()
171 ? d->cast(variant, actionvariable->variant().type()) // try to cast the variant
172 : variant; // don't cast anything, just leave the string-type...
173
174 // Now let's try to determinate the variable which should be changed.
175 TDESharedPtr<Variable> variable = d->variables[name];
176 if(! variable.data()) {
177 // if there exists no such variable yet, create one.
178 kdDebug() << "MacroItem::setVariable() Creating new variable name=" << name << endl;
179
180 variable = TDESharedPtr<Variable>( new Variable() );
181 variable->setName(name);
182 d->variables.replace(name, variable);
183 }
184
185 // Remember the previous value for the case we like to restore it.
186 const TQVariant oldvar = variable->variant();
187
188 // Set the variable.
189 variable->setVariant(v);
190
191 // Now we inform the referenced action that a variable changed. If
192 // notifyUpdated() returns false, the action rejects the new variable
193 // and we need to restore the previous value.
194 if(d->action && ! d->action->notifyUpdated(this, name)) {
195 kdWarning() << "MacroItem::setVariable() Notify failed for variable name=" << name << endl;
196 variable->setVariant(oldvar);
197 return false; // the action rejected the changed variable whyever...
198 }
199
200 // Job done successfully. The variable is changed to the new value.
201 return true;
202}
203
204TDESharedPtr<Variable> MacroItem::addVariable(const TQString& name, const TQVariant& variant)
205{
206 Q_ASSERT(! d->variables.contains(name) );
207 // Create a new Variable.
208 TDESharedPtr<Variable> variable = TDESharedPtr<Variable>( new Variable() );
209 variable->setName(name);
210
211 // Put it into the Variable-map.
212 d->variables.replace(name, variable);
213
214 // Set the variant of the Variable.
215 this->setVariant(name, variant);
216 return variable;
217}
void setComment(const TQString &comment)
Set the comment.
TQString comment() const
TDESharedPtr< Variable > addVariable(const TQString &name, const TQVariant &variant)
Add a new variable with the vaiablename.
bool setVariant(const TQString &name, const TQVariant &variant)
Set the TQVariant.
~MacroItem()
Destructor.
TDESharedPtr< Action > action() const
void setAction(TDESharedPtr< Action > action)
Set the Action.
MacroItem()
Constructor.
TQMap< TQString, TDESharedPtr< Variable > > variables() const
TQVariant variant(const TQString &name, bool checkaction=false) const
TDESharedPtr< Variable > variable(const TQString &name, bool checkaction=false) const
A variable value used to provide abstract access to variables.
Definition variable.h:39
TQMap< TQString, TDESharedPtr< Variable > > Map
A map of variables.
Definition variable.h:66