[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfconfigbase.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfconfigbase.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00024 #include "klfconfigbase.h"
00025 
00026 
00027 KLFConfigPropBase::KLFConfigPropBase()
00028 {
00029 }
00030 KLFConfigPropBase::~KLFConfigPropBase()
00031 {
00032 }
00033 
00034 // ---------
00035 
00036 
00037 bool KLFConfigBase::okChangeProperty(KLFConfigPropBase */*property*/, const QVariant& /*oldValue*/,
00038                                      const QVariant& /*newValue*/)
00039 {
00040   return true;
00041 }
00042 
00043 void KLFConfigBase::propertyChanged(KLFConfigPropBase *property, const QVariant& /*oldValue*/,
00044                                     const QVariant& newValue)
00045 {
00046   KLF_ASSERT_NOT_NULL(property, "property is NULL!!", return; ) ;
00047 
00048   const QString pname = property->propName();
00049   if (pObjConnections.contains(pname)) {
00050     // set connected QObject properties
00051     const QList<ObjConnection> clist = pObjConnections[pname];
00052     for (QList<ObjConnection>::const_iterator it = clist.begin(); it != clist.end(); ++it) {
00053       if ((*it).target == Property) {
00054         (*it).object->setProperty((*it).targetName, newValue);
00055       } else if ((*it).target == Slot) {
00056         QMetaObject::invokeMethod((*it).object, (*it).targetName,
00057                                   QGenericArgument(newValue.typeName(), newValue.data()));
00058       } else {
00059         qWarning()<<KLF_FUNC_NAME<<": Unknown target type "<<(*it).target<<" !";
00060       }
00061     }
00062   }
00063 }
00064 
00065 void KLFConfigBase::propertyValueRequested(const KLFConfigPropBase */*property*/)
00066 {
00067 }
00068 
00069 
00070 void KLFConfigBase::connectQObjectProperty(const QString& configPropertyName, QObject *object,
00071                                            const QByteArray& objPropName)
00072 {
00073   connectQObject(configPropertyName, object, Property, objPropName);
00074 }
00075 void KLFConfigBase::connectQObjectSlot(const QString& configPropertyName, QObject *object,
00076                                            const QByteArray& slotName)
00077 {
00078   connectQObject(configPropertyName, object, Slot, slotName);
00079 }
00080 void KLFConfigBase::disconnectQObjectProperty(const QString& configPropertyName, QObject *object,
00081                                               const QByteArray& objPropName)
00082 {
00083   disconnectQObject(configPropertyName, object, Property, objPropName);
00084 }
00085 void KLFConfigBase::disconnectQObjectSlot(const QString& configPropertyName, QObject *object,
00086                                            const QByteArray& slotName)
00087 {
00088   disconnectQObject(configPropertyName, object, Slot, slotName);
00089 }
00090 
00091 void KLFConfigBase::connectQObject(const QString& configPropertyName, QObject *object,
00092                                    ConnectionTarget target, const QByteArray& targetName)
00093 {
00094   KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00095   klfDbg("Connecting prop "<<configPropertyName<<" to object "<<object<<", targettype="<<target
00096          <<", targetName="<<targetName ) ;
00097 
00098   // check that configPropertyName is valid
00099   KLFConfigPropBase *p = NULL;
00100   for (QList<KLFConfigPropBase*>::const_iterator it = pProperties.begin(); it != pProperties.end(); ++it) {
00101     if ((*it)->propName() == configPropertyName) {
00102       p = (*it);
00103       break;
00104     }
00105   }
00106   KLF_ASSERT_NOT_NULL(p, "Invalid config property name: "<<configPropertyName<<".", return; ) ;
00107 
00108   ObjConnection c;
00109   c.target = target;
00110   c.object = object;
00111   c.targetName = targetName;
00112 
00113   QList<ObjConnection> clist = pObjConnections[configPropertyName];
00114 
00115   for (QList<ObjConnection>::const_iterator it = clist.begin(); it != clist.end(); ++it) {
00116     if (*it == c) {
00117       qWarning()<<KLF_FUNC_NAME<<": "<<configPropertyName<<" already connected to "<<object<<"/"<<targetName;
00118       return;
00119     }
00120   }
00121 
00122   pObjConnections[configPropertyName].append(c);
00123 
00124   // and initialize the QObject property/slot to the current value of that config property
00125   QVariant value = p->toVariant();
00126   if (c.target == Property) {
00127     object->setProperty(targetName, value);
00128   } else if (c.target == Slot) {
00129     QMetaObject::invokeMethod(object, targetName, QGenericArgument(value.typeName(), value.data()));
00130   }
00131 }
00132 
00133 void KLFConfigBase::disconnectQObject(const QString& configPropertyName, QObject *object,
00134                                       ConnectionTarget target, const QByteArray& targetName)
00135 {
00136   ObjConnection c;
00137   c.target = target;
00138   c.object = object;
00139   c.targetName = targetName;
00140 
00141   QList<ObjConnection> & clistref = pObjConnections[configPropertyName];
00142 
00143   for (QList<ObjConnection>::iterator it = clistref.begin(); it != clistref.end(); ++it) {
00144     if (*it == c) {
00145       klfDbg("removed QObject-connection target-type "<<(*it).target<<", target-name "<<(*it).targetName) ;
00146       clistref.erase(it);
00147       return;
00148     }
00149   }
00150 
00151   qWarning()<<KLF_FUNC_NAME<<": "<<configPropertyName<<" is not connected to "<<object<<"/"<<targetName;
00152 }
00153 
00154 void KLFConfigBase::disconnectQObject(QObject * object)
00155 {
00156   QHash<QString,QList<ObjConnection> >::iterator pit;
00157   for (pit = pObjConnections.begin(); pit != pObjConnections.end(); ++pit) {
00158     const QString& pname = pit.key();
00159     Q_UNUSED(pname) ;
00160     QList<ObjConnection> & clistref = pit.value();
00161     for (QList<ObjConnection>::iterator it = clistref.begin(); it != clistref.end(); ++it) {
00162       if ((*it).object == object) {
00163         klfDbg("Removing connection between object "<<object<<" and config property "<<pname) ;
00164         clistref.erase(it);
00165       }
00166     }
00167   }
00168 }
00169 
00170 
00171 QStringList KLFConfigBase::propertyList() const
00172 {
00173   QStringList list;
00174   foreach (KLFConfigPropBase *p, pProperties) {
00175     list << p->propName();
00176   }
00177   return list;
00178 }
00179 
00180 KLFConfigPropBase * KLFConfigBase::property(const QString& name)
00181 {
00182   if (name.isEmpty()) {
00183     klfWarning("Requesting property instance for empty name !?! Program might crash!") ;
00184     return NULL;
00185   }
00186   foreach (KLFConfigPropBase *p, pProperties) {
00187     if (p->propName() == name)
00188       return p;
00189   }
00190   qWarning()<<KLF_FUNC_NAME<<": Can't find config property name "<<name<<" !";
00191   return NULL;
00192 }
00193 

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net