kexi

macroitem.cpp

00001 /***************************************************************************
00002  * This file is part of the KDE project
00003  * copyright (C) 2006 by Sebastian Sauer (mail@dipe.org)
00004  * copyright (C) 2006 by Sascha Kupper (kusato@kfnv.de)
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 "macroitem.h"
00021 
00022 #include <kdebug.h>
00023 
00024 using namespace KoMacro;
00025 
00026 namespace KoMacro {
00027 
00032     class MacroItem::Private
00033     {
00034         public:
00038             KSharedPtr<Action> action;
00039             
00043             QString comment;
00044             
00048             Variable::Map variables;
00049 
00055             inline const QVariant cast(const QVariant& variant, QVariant::Type type) const
00056             {
00057                 // If ok is true the QVariant v holds our new and to the correct type
00058                 // casted variant value. If ok is false the as argument passed variant
00059                 // QVariant contains the (maybe uncasted string to prevent data-loosing
00060                 // what would happen if we e.g. would expect an integer and cast it to
00061                 // an incompatible non-int string) value.
00062                 bool ok = false;
00063                 QVariant v;
00064 
00065                 // Try to cast the passed variant to the expected variant-type.
00066                 switch(type) {
00067                     case QVariant::Bool: {
00068                         const QString s = variant.toString();
00069                         ok = (s == "true" || s == "false" || s == "0" || s == "1" || s == "-1");
00070                         v = QVariant( variant.toBool(), 0 );
00071                     } break;
00072                     case QVariant::Int: {
00073                         v = variant.toInt(&ok);
00074                         // Check if the cast is correct.
00075                         Q_ASSERT(!ok || v.toString() == variant.toString());
00076                     } break;
00077                     case QVariant::UInt: {
00078                         v = variant.toUInt(&ok); 
00079                         Q_ASSERT(!ok || v.toString() == variant.toString());
00080                     } break;
00081                     case QVariant::LongLong: {
00082                         v = variant.toLongLong(&ok); 
00083                         Q_ASSERT(!ok || v.toString() == variant.toString());
00084                     } break;
00085                     case QVariant::ULongLong: {
00086                         v = variant.toULongLong(&ok); 
00087                         Q_ASSERT(!ok || v.toString() == variant.toString());
00088                     } break;
00089                     case QVariant::Double: {
00090                         v = variant.toDouble(&ok);
00091                         Q_ASSERT(!ok || v.toString() == variant.toString());
00092                     } break;
00093                     case QVariant::String: {
00094                         ok = true; // cast will always be successfully
00095                         v = variant.toString();
00096                     } break;
00097                     default: {
00098                         // If we got another type we try to let Qt handle it...
00099                         ok = v.cast(type);
00100                         kdWarning()<<"MacroItem::Private::cast() Unhandled ok="<<ok<<" type="<<type<<" value="<<v<<endl;
00101                     } break;
00102                 }
00103 
00104                 return ok ? v : variant;
00105             }
00106 
00107     };
00108 
00109 }
00110 
00111 MacroItem::MacroItem()
00112     : KShared()
00113     , d( new Private() )
00114 {
00115 }
00116 
00117 MacroItem::~MacroItem()
00118 {
00119     delete d;
00120 }
00121 
00122 QString MacroItem::comment() const
00123 {
00124     return d->comment;
00125 }
00126 
00127 void MacroItem::setComment(const QString& comment)
00128 {
00129     d->comment = comment;
00130 }
00131 
00132 KSharedPtr<Action> MacroItem::action() const
00133 {
00134     return d->action;
00135 }
00136 
00137 void MacroItem::setAction(KSharedPtr<Action> action)
00138 {
00139     d->action = action;
00140 }
00141 
00142 QVariant MacroItem::variant(const QString& name, bool checkaction) const
00143 {
00144     KSharedPtr<Variable> v = variable(name, checkaction);
00145     return v.data() ? v->variant() : QVariant();
00146 }
00147 
00148 KSharedPtr<Variable> MacroItem::variable(const QString& name, bool checkaction) const
00149 {
00150     if(d->variables.contains(name))
00151         return d->variables[name];
00152     if(checkaction && d->action.data())
00153         return d->action->variable(name);
00154     return KSharedPtr<Variable>(0);
00155 }
00156 
00157 Variable::Map MacroItem::variables() const
00158 {
00159     return d->variables;
00160 }
00161 
00162 bool MacroItem::setVariant(const QString& name, const QVariant& variant)
00163 {
00164     // Let's look if there is an action defined for the variable. If that's
00165     // the case, we try to use that action to preserve the type of the variant.
00166     KSharedPtr<Variable> actionvariable = d->action ? d->action->variable(name) : KSharedPtr<Variable>(0);
00167 
00168     // If we know the expected type, we try to cast the variant to the expected
00169     // type else the variant stays untouched (so, it will stay a string).
00170     const QVariant v = actionvariable.data()
00171         ? d->cast(variant, actionvariable->variant().type()) // try to cast the variant
00172         : variant; // don't cast anything, just leave the string-type...
00173 
00174     // Now let's try to determinate the variable which should be changed.
00175     KSharedPtr<Variable> variable = d->variables[name];
00176     if(! variable.data()) {
00177         // if there exists no such variable yet, create one.
00178         kdDebug() << "MacroItem::setVariable() Creating new variable name=" << name << endl;
00179 
00180         variable = KSharedPtr<Variable>( new Variable() );
00181         variable->setName(name);
00182         d->variables.replace(name, variable);
00183     }
00184 
00185     // Remember the previous value for the case we like to restore it.
00186     const QVariant oldvar = variable->variant();
00187 
00188     // Set the variable.
00189     variable->setVariant(v);
00190 
00191     // Now we inform the referenced action that a variable changed. If
00192     // notifyUpdated() returns false, the action rejects the new variable
00193     // and we need to restore the previous value.
00194     if(d->action && ! d->action->notifyUpdated(this, name)) {
00195         kdWarning() << "MacroItem::setVariable() Notify failed for variable name=" << name << endl;
00196         variable->setVariant(oldvar);
00197         return false; // the action rejected the changed variable whyever...
00198     }
00199 
00200     // Job done successfully. The variable is changed to the new value.
00201     return true;
00202 }
00203 
00204 KSharedPtr<Variable> MacroItem::addVariable(const QString& name, const QVariant& variant)
00205 {
00206     Q_ASSERT(! d->variables.contains(name) );
00207     // Create a new Variable.
00208     KSharedPtr<Variable> variable = KSharedPtr<Variable>( new Variable() );
00209     variable->setName(name);
00210     
00211     // Put it into the Variable-map.
00212     d->variables.replace(name, variable);
00213     
00214     // Set the variant of the Variable.
00215     this->setVariant(name, variant);
00216     return variable;
00217 }
KDE Home | KDE Accessibility Home | Description of Access Keys