[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfconfigbase.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * file klfconfigbase.cpp
3 * This file is part of the KLatexFormula Project.
4 * Copyright (C) 2011 by Philippe Faist
5 * philippe.faist at bluewin.ch
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22/* $Id$ */
23
24#include "klfconfigbase.h"
25
26
33
34// ---------
35
36
37bool KLFConfigBase::okChangeProperty(KLFConfigPropBase */*property*/, const QVariant& /*oldValue*/,
38 const QVariant& /*newValue*/)
39{
40 return true;
41}
42
43void KLFConfigBase::propertyChanged(KLFConfigPropBase *property, const QVariant& /*oldValue*/,
44 const QVariant& newValue)
45{
46 KLF_ASSERT_NOT_NULL(property, "property is NULL!!", return; ) ;
47
48 const QString pname = property->propName();
49 if (pObjConnections.contains(pname)) {
50 // set connected QObject properties
51 const QList<ObjConnection> clist = pObjConnections[pname];
52 for (QList<ObjConnection>::const_iterator it = clist.begin(); it != clist.end(); ++it) {
53 if ((*it).target == Property) {
54 (*it).object->setProperty((*it).targetName, newValue);
55 } else if ((*it).target == Slot) {
56 QMetaObject::invokeMethod((*it).object, (*it).targetName,
57 QGenericArgument(newValue.typeName(), newValue.data()));
58 } else {
59 qWarning()<<KLF_FUNC_NAME<<": Unknown target type "<<(*it).target<<" !";
60 }
61 }
62 }
63}
64
68
69
70void KLFConfigBase::connectQObjectProperty(const QString& configPropertyName, QObject *object,
71 const QByteArray& objPropName)
72{
73 connectQObject(configPropertyName, object, Property, objPropName);
74}
75void KLFConfigBase::connectQObjectSlot(const QString& configPropertyName, QObject *object,
76 const QByteArray& slotName)
77{
78 connectQObject(configPropertyName, object, Slot, slotName);
79}
80void KLFConfigBase::disconnectQObjectProperty(const QString& configPropertyName, QObject *object,
81 const QByteArray& objPropName)
82{
83 disconnectQObject(configPropertyName, object, Property, objPropName);
84}
85void KLFConfigBase::disconnectQObjectSlot(const QString& configPropertyName, QObject *object,
86 const QByteArray& slotName)
87{
88 disconnectQObject(configPropertyName, object, Slot, slotName);
89}
90
91void KLFConfigBase::connectQObject(const QString& configPropertyName, QObject *object,
92 ConnectionTarget target, const QByteArray& targetName)
93{
94 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
95 klfDbg("Connecting prop "<<configPropertyName<<" to object "<<object<<", targettype="<<target
96 <<", targetName="<<targetName ) ;
97
98 // check that configPropertyName is valid
99 KLFConfigPropBase *p = NULL;
101 if ((*it)->propName() == configPropertyName) {
102 p = (*it);
103 break;
104 }
105 }
106 KLF_ASSERT_NOT_NULL(p, "Invalid config property name: "<<configPropertyName<<".", return; ) ;
107
109 c.target = target;
110 c.object = object;
111 c.targetName = targetName;
112
113 QList<ObjConnection> clist = pObjConnections[configPropertyName];
114
115 for (QList<ObjConnection>::const_iterator it = clist.begin(); it != clist.end(); ++it) {
116 if (*it == c) {
117 qWarning()<<KLF_FUNC_NAME<<": "<<configPropertyName<<" already connected to "<<object<<"/"<<targetName;
118 return;
119 }
120 }
121
122 pObjConnections[configPropertyName].append(c);
123
124 // and initialize the QObject property/slot to the current value of that config property
125 QVariant value = p->toVariant();
126 if (c.target == Property) {
127 object->setProperty(targetName, value);
128 } else if (c.target == Slot) {
129 QMetaObject::invokeMethod(object, targetName, QGenericArgument(value.typeName(), value.data()));
130 }
131}
132
133void KLFConfigBase::disconnectQObject(const QString& configPropertyName, QObject *object,
134 ConnectionTarget target, const QByteArray& targetName)
135{
137 c.target = target;
138 c.object = object;
139 c.targetName = targetName;
140
141 QList<ObjConnection> & clistref = pObjConnections[configPropertyName];
142
143 for (QList<ObjConnection>::iterator it = clistref.begin(); it != clistref.end(); ++it) {
144 if (*it == c) {
145 klfDbg("removed QObject-connection target-type "<<(*it).target<<", target-name "<<(*it).targetName) ;
146 clistref.erase(it);
147 return;
148 }
149 }
150
151 qWarning()<<KLF_FUNC_NAME<<": "<<configPropertyName<<" is not connected to "<<object<<"/"<<targetName;
152}
153
155{
157 for (pit = pObjConnections.begin(); pit != pObjConnections.end(); ++pit) {
158 const QString& pname = pit.key();
159 Q_UNUSED(pname) ;
160 QList<ObjConnection> & clistref = pit.value();
161 for (QList<ObjConnection>::iterator it = clistref.begin(); it != clistref.end(); ++it) {
162 if ((*it).object == object) {
163 klfDbg("Removing connection between object "<<object<<" and config property "<<pname) ;
164 clistref.erase(it);
165 }
166 }
167 }
168}
169
170
172{
173 QStringList list;
174 foreach (KLFConfigPropBase *p, pProperties) {
175 list << p->propName();
176 }
177 return list;
178}
179
181{
182 if (name.isEmpty()) {
183 klfWarning("Requesting property instance for empty name !?! Program might crash!") ;
184 return NULL;
185 }
186 foreach (KLFConfigPropBase *p, pProperties) {
187 if (p->propName() == name)
188 return p;
189 }
190 qWarning()<<KLF_FUNC_NAME<<": Can't find config property name "<<name<<" !";
191 return NULL;
192}
193
virtual void propertyChanged(KLFConfigPropBase *property, const QVariant &oldValue, const QVariant &newValue)
virtual void connectQObjectProperty(const QString &configPropertyName, QObject *object, const QByteArray &objPropName)
virtual QStringList propertyList() const
KLFConfigPropBase * property(const QString &name)
virtual void propertyValueRequested(const KLFConfigPropBase *property)
QList< KLFConfigPropBase * > pProperties
virtual void connectQObjectSlot(const QString &configPropertyName, QObject *object, const QByteArray &slotMethodName)
virtual void disconnectQObjectSlot(const QString &configPropertyName, QObject *object, const QByteArray &slotMethodName)
virtual void disconnectQObject(QObject *object)
virtual bool okChangeProperty(KLFConfigPropBase *property, const QVariant &oldValue, const QVariant &newValue)
QHash< QString, QList< ObjConnection > > pObjConnections
virtual void disconnectQObjectProperty(const QString &configPropertyName, QObject *object, const QByteArray &objPropName)
void connectQObject(const QString &configPropertyName, QObject *object, ConnectionTarget target, const QByteArray &targetName)
virtual ~KLFConfigPropBase()
virtual QString propName() const
virtual QVariant toVariant() const =0
#define klfWarning(streamableItems)
Definition klfdebug.h:171
#define KLF_DEBUG_BLOCK(msg)
Utility to debug the execution of a block.
Definition klfdebug.h:152
#define KLF_ASSERT_NOT_NULL(ptr, msg, failaction)
Asserting Non-NULL pointers (NON-FATAL)
Definition klfdebug.h:210
#define klfDbg(streamableItems)
print debug stream items
Definition klfdebug.h:158
QByteArray & append(char ch)
iterator begin()
iterator end()
const Key key(const T &value) const
const T value(const Key &key) const
iterator begin()
iterator end()
iterator erase(iterator pos)
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
bool isEmpty() const
const char * typeName() const

Generated by doxygen 1.11.0