File indexing completed on 2024-04-21 05:42:43

0001 /*
0002  *   Copyright 2017 by Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU Library General Public License as
0006  *   published by the Free Software Foundation; either version 2, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details
0013  *
0014  *   You should have received a copy of the GNU Library General Public
0015  *   License along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0018  */
0019 
0020 #include "objectwatcher.h"
0021 
0022 #include <QMetaProperty>
0023 #include <QDebug>
0024 
0025 void ObjectWatcher::PropertyValue::integrateValue(QObject* object, const QMetaProperty& prop, const QVariant& value)
0026 {
0027 //     if (!QByteArray(object->metaObject()->className()).contains("ListView")) return;
0028 
0029     if (value == lastValue && !QByteArray(prop.name()).contains("hildren")) {
0030         times++;
0031         qDebug() << "repeated value" << object << prop.name() << value << times << "times";
0032     } else {
0033         times = 0;
0034         lastValue = value;
0035     }
0036 }
0037 
0038 ObjectWatcher::ObjectWatcher(QObject* object)
0039     : m_watched(object)
0040 {
0041     const auto &meta = ObjectWatcher::staticMetaObject;
0042     QMetaMethod propChange = meta.method(meta.indexOfSlot("propertyChanged()"));
0043     Q_ASSERT(propChange.isValid() && meta.indexOfSlot("propertyChanged()")>=0);
0044 
0045     const auto mo = object->metaObject();
0046     for(int i = 0;  i<mo->propertyCount(); ++i) {
0047         const auto prop = mo->property(i);
0048         const auto value = prop.read(object);
0049         m_values.insert(prop.name(), {value});
0050 
0051         if (prop.hasNotifySignal()) {
0052             connect(object, prop.notifySignal(), this, propChange);
0053         }
0054     }
0055 
0056     connect(object, &QObject::destroyed, this, &QObject::deleteLater);
0057 }
0058 
0059 void ObjectWatcher::propertyChanged()
0060 {
0061     Q_ASSERT(sender() == m_watched);
0062 
0063     const QMetaObject* mo = m_watched->metaObject();
0064 
0065     for (int i = 0, pc = mo->propertyCount(); i<pc; ++i) {
0066         const QMetaProperty prop = mo->property(i);
0067         if (prop.notifySignalIndex() == senderSignalIndex()) {
0068             const auto value = prop.read(m_watched);
0069             m_values[prop.name()].integrateValue(m_watched, prop, value);
0070         }
0071     }
0072 }