1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/*
propertyaggregator.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Volker Krause <volker.krause@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "propertyaggregator.h"
#include "propertydata.h"
#include "objectinstance.h"
#include <compat/qasconst.h>
#include <QDebug>
#include <algorithm>
#include <numeric>
using namespace GammaRay;
PropertyAggregator::PropertyAggregator(QObject *parent)
: PropertyAdaptor(parent)
{
}
PropertyAggregator::~PropertyAggregator() = default;
void PropertyAggregator::doSetObject(const ObjectInstance &oi)
{
std::for_each(m_propertyAdaptors.begin(), m_propertyAdaptors.end(), [&oi](PropertyAdaptor *pa) {
pa->setObject(oi);
});
}
int PropertyAggregator::count() const
{
if (!object().isValid())
return 0;
return std::accumulate(m_propertyAdaptors.constBegin(), m_propertyAdaptors.constEnd(), 0,
[](int lhs, PropertyAdaptor *rhs) {
return lhs + rhs->count();
});
}
PropertyData PropertyAggregator::propertyData(int index) const
{
if (!object().isValid())
return PropertyData();
int offset = 0;
for (const auto adaptor : m_propertyAdaptors) {
if (index < offset + adaptor->count())
return adaptor->propertyData(index - offset);
offset += adaptor->count();
}
Q_ASSERT(false);
return PropertyData();
}
void PropertyAggregator::writeProperty(int index, const QVariant &value)
{
if (!object().isValid())
return;
int offset = 0;
for (const auto adaptor : qAsConst(m_propertyAdaptors)) {
if (index < offset + adaptor->count()) {
QPointer<PropertyAggregator> guard(this);
adaptor->writeProperty(index - offset, value);
if (guard) {
m_oi = adaptor->object(); // propagate changes back to us, particularly matters for value types
}
return;
}
offset += adaptor->count();
}
Q_ASSERT(false);
}
bool PropertyAggregator::canAddProperty() const
{
auto count = std::count_if(m_propertyAdaptors.constBegin(), m_propertyAdaptors.constEnd(),
[](PropertyAdaptor *pa) {
return pa->canAddProperty();
});
return count == 1;
}
void PropertyAggregator::addProperty(const PropertyData &data)
{
if (!object().isValid())
return;
Q_ASSERT(canAddProperty());
for (const auto adaptor : qAsConst(m_propertyAdaptors)) {
if (adaptor->canAddProperty()) {
adaptor->addProperty(data);
return;
}
}
Q_ASSERT(false);
}
void PropertyAggregator::resetProperty(int index)
{
if (!object().isValid())
return;
int offset = 0;
for (const auto adaptor : qAsConst(m_propertyAdaptors)) {
if (index < offset + adaptor->count()) {
adaptor->resetProperty(index - offset);
return;
}
offset += adaptor->count();
}
Q_ASSERT(false);
}
void PropertyAggregator::addPropertyAdaptor(PropertyAdaptor *adaptor)
{
m_propertyAdaptors.push_back(adaptor);
connect(adaptor, &PropertyAdaptor::propertyChanged, this, &PropertyAggregator::slotPropertyChanged);
connect(adaptor, &PropertyAdaptor::propertyAdded, this, &PropertyAggregator::slotPropertyAdded);
connect(adaptor, &PropertyAdaptor::propertyRemoved, this, &PropertyAggregator::slotPropertyRemoved);
connect(adaptor, &PropertyAdaptor::objectInvalidated, this, &PropertyAdaptor::objectInvalidated);
}
void PropertyAggregator::slotPropertyChanged(int first, int last)
{
auto source = sender();
Q_ASSERT(source);
int offset = 0;
for (auto pa : qAsConst(m_propertyAdaptors)) {
if (pa == source) {
emit propertyChanged(first + offset, last + offset);
return;
}
offset += pa->count();
}
}
void PropertyAggregator::slotPropertyAdded(int first, int last)
{
auto source = sender();
Q_ASSERT(source);
int offset = 0;
for (auto pa : qAsConst(m_propertyAdaptors)) {
if (pa == source) {
emit propertyAdded(first + offset, last + offset);
return;
}
offset += pa->count();
}
}
void PropertyAggregator::slotPropertyRemoved(int first, int last)
{
auto source = sender();
Q_ASSERT(source);
int offset = 0;
for (auto pa : qAsConst(m_propertyAdaptors)) {
if (pa == source) {
emit propertyRemoved(first + offset, last + offset);
return;
}
offset += pa->count();
}
}
|