[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: klfconfigbase.cpp 983 2016-12-31 21:03:44Z phfaist $ */
23 
24 #include "klfconfigbase.h"
25 
26 
28 {
29 }
31 {
32 }
33 
34 // ---------
35 
36 
37 bool KLFConfigBase::okChangeProperty(KLFConfigPropBase */*property*/, const QVariant& /*oldValue*/,
38  const QVariant& /*newValue*/)
39 {
40  return true;
41 }
42 
43 void 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 
66 {
67 }
68 
69 
70 void KLFConfigBase::connectQObjectProperty(const QString& configPropertyName, QObject *object,
71  const QByteArray& objPropName)
72 {
73  connectQObject(configPropertyName, object, Property, objPropName);
74 }
75 void KLFConfigBase::connectQObjectSlot(const QString& configPropertyName, QObject *object,
76  const QByteArray& slotName)
77 {
78  connectQObject(configPropertyName, object, Slot, slotName);
79 }
80 void KLFConfigBase::disconnectQObjectProperty(const QString& configPropertyName, QObject *object,
81  const QByteArray& objPropName)
82 {
83  disconnectQObject(configPropertyName, object, Property, objPropName);
84 }
85 void KLFConfigBase::disconnectQObjectSlot(const QString& configPropertyName, QObject *object,
86  const QByteArray& slotName)
87 {
88  disconnectQObject(configPropertyName, object, Slot, slotName);
89 }
90 
91 void KLFConfigBase::connectQObject(const QString& configPropertyName, QObject *object,
92  ConnectionTarget target, const QByteArray& targetName)
93 {
95  klfDbg("Connecting prop "<<configPropertyName<<" to object "<<object<<", targettype="<<target
96  <<", targetName="<<targetName ) ;
97 
98  // check that configPropertyName is valid
99  KLFConfigPropBase *p = NULL;
100  for (QList<KLFConfigPropBase*>::const_iterator it = pProperties.begin(); it != pProperties.end(); ++it) {
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 
108  ObjConnection c;
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 
133 void KLFConfigBase::disconnectQObject(const QString& configPropertyName, QObject *object,
134  ConnectionTarget target, const QByteArray& targetName)
135 {
136  ObjConnection c;
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 {
156  QHash<QString,QList<ObjConnection> >::iterator pit;
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 
const Key key(const T &value) const
virtual void disconnectQObjectSlot(const QString &configPropertyName, QObject *object, const QByteArray &slotMethodName)
#define klfDbg(streamableItems)
print debug stream items
#define KLF_DEBUG_BLOCK(msg)
Utility to debug the execution of a block.
iterator erase(iterator pos)
void connectQObject(const QString &configPropertyName, QObject *object, ConnectionTarget target, const QByteArray &targetName)
virtual void disconnectQObject(QObject *object)
#define KLF_ASSERT_NOT_NULL(ptr, msg, failaction)
Asserting Non-NULL pointers (NON-FATAL)
virtual void connectQObjectSlot(const QString &configPropertyName, QObject *object, const QByteArray &slotMethodName)
#define klfWarning(streamableItems)
bool isEmpty() const
virtual ~KLFConfigPropBase()
iterator begin()
virtual void propertyChanged(KLFConfigPropBase *property, const QVariant &oldValue, const QVariant &newValue)
virtual QVariant toVariant() const =0
virtual void connectQObjectProperty(const QString &configPropertyName, QObject *object, const QByteArray &objPropName)
QByteArray & append(char ch)
iterator end()
const T value(const Key &key) const
#define KLF_FUNC_NAME
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)
virtual bool okChangeProperty(KLFConfigPropBase *property, const QVariant &oldValue, const QVariant &newValue)
virtual void propertyValueRequested(const KLFConfigPropBase *property)
const char * typeName() const
virtual QStringList propertyList() const
virtual void disconnectQObjectProperty(const QString &configPropertyName, QObject *object, const QByteArray &objPropName)
iterator end()
virtual QString propName() const
Definition: klfconfigbase.h:41
iterator begin()
KLFConfigPropBase * property(const QString &name)

Generated by doxygen 1.8.13