valentina/src/libs/vpropertyexplorer/vproperty.cpp

444 lines
10 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file vproperty.cpp
** @author hedgeware <internal(at)hedgeware.net>
** @date
**
** @brief
** @copyright
** All rights reserved. This program and the accompanying materials
** are made available under the terms of the GNU Lesser General Public License
** (LGPL) version 2.1 which accompanies this distribution, and is available at
** http://www.gnu.org/licenses/lgpl-2.1.html
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
*************************************************************************/
#include "vproperty.h"
#include <QByteArray>
#include <QFlags>
#include <QItemEditorFactory>
#include <QLineEdit>
#include <QList>
#include <QMetaProperty>
#include <QObject>
#include <QStandardItemEditorCreator>
#include <QWidget>
#include "vproperty_p.h"
//! Standard constructor, takes a name and a parent property as argument
2023-02-09 15:09:10 +01:00
VPE::VProperty::VProperty(const QString& name,
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QMetaType::Type type)
#else
QVariant::Type type)
#endif
: QObject(), d_ptr(new VPropertyPrivate(name, type))
{
}
VPE::VProperty::VProperty(VPropertyPrivate *d)
: d_ptr(d)
{
}
VPE::VProperty::~VProperty()
{
2022-08-08 14:25:14 +02:00
VPE::VProperty::setParent(nullptr);
2014-09-10 19:57:08 +02:00
while (!d_ptr->Children.isEmpty())
{
VProperty* tmpChild = d_ptr->Children.takeLast();
delete tmpChild;
}
delete d_ptr;
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::type() const -> QString
{
return "string";
}
//! Get the data how it should be displayed
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::data(int column, int role) const -> QVariant
{
2014-09-10 19:57:08 +02:00
if (column == DPC_Name && Qt::DisplayRole == role)
{
return QVariant(d_ptr->Name);
2014-09-10 19:57:08 +02:00
}
else if (column == DPC_Data && (Qt::DisplayRole == role || Qt::EditRole == role))
{
return d_ptr->VariantValue;
2014-09-10 19:57:08 +02:00
}
else if (Qt::ToolTipRole == role)
{
return QVariant(d_ptr->Description);
2014-09-10 19:57:08 +02:00
}
else
return QVariant();
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::setData(const QVariant &data, int role) -> bool
{
bool tmpResult = false;
2014-09-10 19:57:08 +02:00
if (Qt::EditRole == role)
{
tmpResult = (d_ptr->VariantValue != data);
setValue(data);
}
return tmpResult;
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index,
const QAbstractItemDelegate *delegate) const -> bool
{
Q_UNUSED(painter)
Q_UNUSED(option)
Q_UNUSED(index)
Q_UNUSED(delegate)
return false;
}
//! Returns an editor widget, or NULL if it doesn't supply one
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::createEditor(QWidget *parent, const QStyleOptionViewItem &options,
const QAbstractItemDelegate *delegate) -> QWidget *
{
Q_UNUSED(options)
Q_UNUSED(delegate)
QItemEditorFactory *factory = new QItemEditorFactory;
QItemEditorCreatorBase *lineCreator = new QStandardItemEditorCreator<QLineEdit>();
2023-02-09 15:09:10 +01:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
factory->registerEditor(QMetaType::QString, lineCreator);
#else
factory->registerEditor(QVariant::String, lineCreator);
2023-02-09 15:09:10 +01:00
#endif
QItemEditorFactory::setDefaultFactory(factory);
d_ptr->editor = factory->createEditor(static_cast<int>(d_ptr->PropertyVariantType), parent);
return d_ptr->editor;
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::setEditorData(QWidget *editor) -> bool
{
2014-09-10 19:57:08 +02:00
if (!editor)
{
return false;
2014-09-10 19:57:08 +02:00
}
QByteArray n = editor->metaObject()->userProperty().name();
2014-09-10 19:57:08 +02:00
if (!n.isEmpty())
{
editor->blockSignals(true);
editor->setProperty(n, d_ptr->VariantValue);
editor->blockSignals(false);
return true;
}
return false;
}
//! Gets the data from the widget
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getEditorData(const QWidget *editor) const -> QVariant
{
2014-09-10 19:57:08 +02:00
if (!editor)
{
return QVariant();
2014-09-10 19:57:08 +02:00
}
QByteArray n = editor->metaObject()->userProperty().name();
if (!n.isEmpty())
2014-09-10 19:57:08 +02:00
{
return editor->property(n);
2014-09-10 19:57:08 +02:00
}
else
return QVariant();
}
//! Returns item flags
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::flags(int column) const -> Qt::ItemFlags
{
2014-09-10 19:57:08 +02:00
if (column == DPC_Name)
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
2014-09-10 19:57:08 +02:00
}
else if (column == DPC_Data)
{
return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
2014-09-10 19:57:08 +02:00
}
else
return Qt::NoItemFlags;
}
void VPE::VProperty::setValue(const QVariant &value)
{
d_ptr->VariantValue = value;
2023-02-09 15:09:10 +01:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
d_ptr->VariantValue.convert(QMetaType(d_ptr->PropertyVariantType));
#else
d_ptr->VariantValue.convert(static_cast<int>(d_ptr->PropertyVariantType));
2023-02-09 15:09:10 +01:00
#endif
if (d_ptr->editor != nullptr)
{
setEditorData(d_ptr->editor);
}
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getValue() const -> QVariant
{
return d_ptr->VariantValue;
}
2015-04-15 14:44:57 +02:00
// cppcheck-suppress unusedFunction
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::serialize() const -> QString
{
return getValue().toString();
}
void VPE::VProperty::deserialize(const QString& value)
{
setValue(QVariant(value));
}
void VPE::VProperty::setName(const QString& name)
{
d_ptr->Name = name;
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getName() const -> QString
{
return d_ptr->Name;
}
void VPE::VProperty::setDescription(const QString& desc)
{
d_ptr->Description = desc;
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getDescription() const -> QString
{
return d_ptr->Description;
}
//! Returns a reference to the list of children
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getChildren() -> QList<VPE::VProperty *> &
{
return d_ptr->Children;
}
//! Returns a reference to the list of children
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getChildren() const -> const QList<VPE::VProperty *> &
{
return d_ptr->Children;
}
//! Returns the child at a certain row
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getChild(int row) const -> VPE::VProperty *
{
2014-09-10 19:57:08 +02:00
if (row >= 0 && row < getRowCount())
{
return d_ptr->Children.at(row);
2014-09-10 19:57:08 +02:00
}
else
return nullptr;
}
//! Gets the number of children
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getRowCount() const -> vpesizetype
{
return d_ptr->Children.count();
}
//! Gets the parent of this property
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getParent() const -> VPE::VProperty *
{
return d_ptr->Parent;
}
//! Sets the parent of this property
void VPE::VProperty::setParent(VProperty* parent)
{
2014-09-10 19:57:08 +02:00
if (d_ptr->Parent == parent)
{
return;
2014-09-10 19:57:08 +02:00
}
VProperty* oldParent = d_ptr->Parent;
d_ptr->Parent = parent;
2014-09-10 19:57:08 +02:00
if (oldParent)
{
oldParent->removeChild(this);
2014-09-10 19:57:08 +02:00
}
2014-09-10 19:57:08 +02:00
if (d_ptr->Parent && d_ptr->Parent->getChildRow(this) == -1)
{
d_ptr->Parent->addChild(this);
2014-09-10 19:57:08 +02:00
}
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::addChild(VProperty *child) -> vpesizetype
{
2014-09-10 19:57:08 +02:00
if (child && child->getParent() != this)
{
child->setParent(this);
2014-09-10 19:57:08 +02:00
}
2014-09-10 19:57:08 +02:00
if (!d_ptr->Children.contains(child) && child != nullptr)
{
d_ptr->Children.push_back(child);
return d_ptr->Children.count()-1;
}
else
{
return d_ptr->Children.indexOf(child);
}
}
//! Removes a child from the children list
void VPE::VProperty::removeChild(VProperty* child)
{
d_ptr->Children.removeAll(child);
2014-09-10 19:57:08 +02:00
if (child && child->getParent() == this)
{
child->setParent(nullptr);
2014-09-10 19:57:08 +02:00
}
}
//! Returns the row the child has
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getChildRow(VProperty *child) const -> vpesizetype
{
return d_ptr->Children.indexOf(child);
}
//! Returns whether the views have to update the parent of this property if it changes
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getUpdateParent() const -> bool
{
return d_ptr->UpdateParent;
}
//! Returns whether the views have to update the children of this property if it changes
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getUpdateChildren() const -> bool
{
return d_ptr->UpdateChildren;
}
//! Sets whether the views should update Parents or children after this property changes
void VPE::VProperty::setUpdateBehaviour(bool update_parent, bool update_children)
{
d_ptr->UpdateParent = update_parent;
d_ptr->UpdateChildren = update_children;
}
void VPE::VProperty::setSettings(const QMap<QString, QVariant>& settings)
{
QMap<QString, QVariant>::const_iterator tmpIterator = settings.constBegin();
2014-09-10 19:57:08 +02:00
for (; tmpIterator != settings.constEnd(); ++tmpIterator)
{
setSetting(tmpIterator.key(), tmpIterator.value());
}
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getSettings() const -> QMap<QString, QVariant>
{
QMap<QString, QVariant> tmpResult;
const QStringList tmpKeyList = getSettingKeys();
2023-05-03 13:07:02 +02:00
for (const auto &tmpKey : tmpKeyList)
{
tmpResult.insert(tmpKey, getSetting(tmpKey));
}
return tmpResult;
}
void VPE::VProperty::setSetting(const QString& key, const QVariant& value)
{
Q_UNUSED(key)
Q_UNUSED(value)
// Not needed in the Standard property
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getSetting(const QString &key) const -> QVariant
{
// Not needed in the Standard property
Q_UNUSED(key)
return QVariant();
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::getSettingKeys() const -> QStringList
{
return QStringList();
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::clone(bool include_children, VProperty *container) const -> VPE::VProperty *
{
2014-09-10 19:57:08 +02:00
if (!container)
{
container = new VProperty(getName(), d_ptr->PropertyVariantType);
2014-09-10 19:57:08 +02:00
}
container->setName(getName());
container->setDescription(getDescription());
container->setValue(getValue());
container->setSettings(getSettings());
container->setUpdateBehaviour(getUpdateParent(), getUpdateChildren());
container->setPropertyType(propertyType());
2014-09-10 19:57:08 +02:00
if (include_children)
{
const QList<VProperty*> children = d_ptr->Children;
2023-05-03 13:07:02 +02:00
for (auto *tmpChild : children)
{
container->addChild(tmpChild->clone(true));
}
}
return container;
}
2023-05-03 13:07:02 +02:00
auto VPE::VProperty::propertyType() const -> VPE::Property
{
return d_ptr->type;
}
void VPE::VProperty::setPropertyType(const Property &type)
{
d_ptr->type = type;
}
void VPE::VProperty::UpdateParent(const QVariant &value)
{
Q_UNUSED(value)
}
void VPE::VProperty::ValueChildChanged(const QVariant &value, int typeForParent)
{
2014-09-11 16:15:49 +02:00
Q_UNUSED(value)
Q_UNUSED(typeForParent)
}
VPE::UserChangeEvent::~UserChangeEvent()
{}
VPE::VPropertyPrivate::~VPropertyPrivate()
{}