/*************************************************************************** * This file is part of the KDE project * copyright (C) 2006 by Sebastian Sauer (mail@dipe.org) * copyright (C) 2006 by Sascha Kupper (kusato@kfnv.de) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * You should have received a copy of the GNU Library General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. ***************************************************************************/ #include "macroitem.h" #include using namespace KoMacro; namespace KoMacro { /** * @internal d-pointer class to be more flexible on future extension of the * functionality without to much risk to break the binary compatibility. */ class MacroItem::Private { public: /** * The @a Action this @a MacroItem has. */ TDESharedPtr action; /** * The comment this @a MacroItem has. */ TQString comment; /** * The @a TQMap of @a Variable this @a MacroItem has. */ Variable::Map variables; /** * define a @a TQVariant -cast as inline for better performance * @return the casted @a TQVariant by passing a @param variant and its * expected TQVariant::Type @param type. */ inline const TQVariant cast(const TQVariant& variant, TQVariant::Type type) const { // If ok is true the TQVariant v holds our new and to the correct type // casted variant value. If ok is false the as argument passed variant // TQVariant contains the (maybe uncasted string to prevent data-loosing // what would happen if we e.g. would expect an integer and cast it to // an incompatible non-int string) value. bool ok = false; TQVariant v; // Try to cast the passed variant to the expected variant-type. switch(type) { case TQVariant::Bool: { const TQString s = variant.toString(); ok = (s == "true" || s == "false" || s == "0" || s == "1" || s == "-1"); v = TQVariant( variant.toBool(), 0 ); } break; case TQVariant::Int: { v = variant.toInt(&ok); // Check if the cast is correct. Q_ASSERT(!ok || v.toString() == variant.toString()); } break; case TQVariant::UInt: { v = variant.toUInt(&ok); Q_ASSERT(!ok || v.toString() == variant.toString()); } break; case TQVariant::LongLong: { v = variant.toLongLong(&ok); Q_ASSERT(!ok || v.toString() == variant.toString()); } break; case TQVariant::ULongLong: { v = variant.toULongLong(&ok); Q_ASSERT(!ok || v.toString() == variant.toString()); } break; case TQVariant::Double: { v = variant.toDouble(&ok); Q_ASSERT(!ok || v.toString() == variant.toString()); } break; case TQVariant::String: { ok = true; // cast will always be successfully v = variant.toString(); } break; default: { // If we got another type we try to let TQt handle it... ok = v.cast(type); kdWarning()<<"MacroItem::Private::cast() Unhandled ok="<