valentina/src/libs/vpropertyexplorer/vpropertymodel.cpp

367 lines
9.5 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file vpropertymodel.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 "vpropertymodel.h"
#include <QList>
#include "vproperty.h"
#include "vpropertyset.h"
#include "vpropertydef.h"
#include "vpropertymodel_p.h"
VPE::VPropertyModel::VPropertyModel(VPropertyModelPrivate *d, QObject *parent)
: QAbstractItemModel(parent), d_ptr(d)
{
}
VPE::VPropertyModel::VPropertyModel(QObject * parent) :
QAbstractItemModel(parent), d_ptr(new VPropertyModelPrivate())
{
}
VPE::VPropertyModel::~VPropertyModel()
{
2014-09-14 11:16:59 +02:00
delete d_ptr->Properties;
delete d_ptr;
}
//! Adds the property to the model and attaches it to the parentid
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::addProperty(VProperty *property, const QString &id, const QString &parentid, bool emitsignals)
-> bool
{
2014-09-10 19:57:08 +02:00
if (!property)
{
return false;
2014-09-10 19:57:08 +02:00
}
2014-09-10 19:57:08 +02:00
if (!d_ptr->Properties) // If not existant, create property set
{
d_ptr->Properties = new VPropertySet();
2014-09-10 19:57:08 +02:00
}
2014-09-10 19:57:08 +02:00
if (emitsignals)
{
VProperty* tmpParent = getProperty(parentid);
2024-02-19 17:09:56 +01:00
vpesizetype const tmpRow =
tmpParent != nullptr ? tmpParent->getRowCount() : d_ptr->Properties->getRootPropertyCount();
beginInsertRows((tmpParent != nullptr ? getIndexFromProperty(tmpParent) : QModelIndex()),
static_cast<int>(tmpRow), static_cast<int>(tmpRow));
}
d_ptr->Properties->addProperty(property, id, parentid);
2014-09-10 19:57:08 +02:00
if (emitsignals)
{
endInsertRows();
2014-09-10 19:57:08 +02:00
}
return true;
}
//! Creates a property and adds it to the model
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::createProperty(const QString &id, const QString &name, const QString &parentid,
const QVariant &data) -> VPE::VProperty *
{
auto *tmpProp = new VProperty(name);
tmpProp->setValue(data);
if (addProperty(tmpProp, id, parentid))
2014-09-10 19:57:08 +02:00
{
return tmpProp;
2014-09-10 19:57:08 +02:00
}
else
return nullptr;
}
//! Gets a property by it's ID
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::getProperty(const QString &id) -> VPE::VProperty *
{
return d_ptr->Properties != nullptr ? d_ptr->Properties->getProperty(id) : nullptr;
}
//! Returns the model index at row/column
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::index(int row, int column, const QModelIndex &parent) const -> QModelIndex
{
if (d_ptr->Properties == nullptr || (parent.isValid() && parent.column() > 1))
2014-09-10 19:57:08 +02:00
{
return QModelIndex();
2014-09-10 19:57:08 +02:00
}
2014-09-10 19:57:08 +02:00
if (parent.isValid())
{
// Get the parent index
VProperty* parentItem = getProperty(parent);
2014-09-10 19:57:08 +02:00
if (parentItem)
{
VProperty* childItem = parentItem->getChild(row);
if (childItem)
2014-09-10 19:57:08 +02:00
{
return createIndex(row, column, childItem);
2014-09-10 19:57:08 +02:00
}
}
2014-09-10 19:57:08 +02:00
}
else if (row >= 0 && row < d_ptr->Properties->count())
{
return createIndex(row, column, d_ptr->Properties->getRootProperty(row));
}
return QModelIndex();
}
//! Returns the parent of one model index
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::parent(const QModelIndex &index) const -> QModelIndex
{
if (!index.isValid())
2014-09-10 19:57:08 +02:00
{
2023-02-09 14:48:28 +01:00
return {};
2014-09-10 19:57:08 +02:00
}
VProperty* childItem = getProperty(index);
2014-09-10 19:57:08 +02:00
if (childItem)
{
VProperty* parentItem = childItem->getParent();
2014-09-10 19:57:08 +02:00
if (parentItem)
{
VProperty* grandParentItem = parentItem->getParent();
2024-02-19 17:09:56 +01:00
vpesizetype const parents_row = grandParentItem != nullptr
? grandParentItem->getChildRow(parentItem)
: d_ptr->Properties->getRootProperties().indexOf(parentItem);
2014-09-10 19:57:08 +02:00
if (parents_row >= 0)
{
return createIndex(static_cast<int>(parents_row), 0, parentItem);
2014-09-10 19:57:08 +02:00
}
}
}
2023-02-09 14:48:28 +01:00
return {};
}
//! Returns the item flags for the given index
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::flags(const QModelIndex &index) const -> Qt::ItemFlags
{
VProperty* tmpProperty = getProperty(index);
2014-09-10 19:57:08 +02:00
if (!tmpProperty)
{
return Qt::NoItemFlags;
2014-09-10 19:57:08 +02:00
}
else
return tmpProperty->flags(index.column());
}
//! Sets the role data for the item at index to value
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::setData(const QModelIndex &index, const QVariant &value, int role) -> bool
{
VProperty* tmpProperty = getProperty(index);
2014-09-10 19:57:08 +02:00
if (index.column() == 1 && tmpProperty)
{
2024-02-19 17:09:56 +01:00
bool const tmpHasChanged = tmpProperty->setData(value, role);
2014-09-10 19:57:08 +02:00
if (tmpProperty->getUpdateParent() && tmpHasChanged)
{ // If neccessary, update the parent as well
2024-02-19 17:09:56 +01:00
QModelIndex const tmpParentIndex = parent(index);
emit dataChanged(tmpParentIndex, tmpParentIndex);
}
2014-09-10 19:57:08 +02:00
if (tmpHasChanged)
{
emit onDataChangedByEditor(tmpProperty);
2014-09-10 19:57:08 +02:00
}
}
return true;
}
//! Returns the data of an model index
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::data(const QModelIndex &index, int role) const -> QVariant
{
VProperty* tmpProperty = getProperty(index);
2014-09-10 19:57:08 +02:00
if (!tmpProperty)
{
return QVariant();
2014-09-10 19:57:08 +02:00
}
else
return tmpProperty->data(index.column(), role);
}
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::headerData(int section, Qt::Orientation orientation, int role) const -> QVariant
{
2014-09-10 19:57:08 +02:00
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
// Header data
2014-09-10 19:57:08 +02:00
if (section == 0)
{
return d_ptr->HeadlineProperty;
}
else if (section == 1)
{
return d_ptr->HeadlineValue;
}
}
2014-09-10 19:57:08 +02:00
else if (role == Qt::DisplayRole)
{
return QVariant(section);
2014-09-10 19:57:08 +02:00
}
return QVariant();
}
//! Returns the number of rows
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::rowCount(const QModelIndex &parent) const -> int
{
2014-09-10 19:57:08 +02:00
if (parent.isValid())
{
VProperty* tmpParent = getProperty(parent);
2014-09-10 19:57:08 +02:00
if (tmpParent)
{
return static_cast<int>(tmpParent->getRowCount());
2014-09-10 19:57:08 +02:00
}
}
// Return the root property count
2014-09-10 19:57:08 +02:00
if (d_ptr->Properties)
{
return static_cast<int>(d_ptr->Properties->getRootPropertyCount());
2014-09-10 19:57:08 +02:00
}
return 0;
}
//! Returns the number of columns
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::columnCount(const QModelIndex &parent) const -> int
{
Q_UNUSED(parent)
return 2;
}
//! Gets a property by its ModelIndex
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::getProperty(const QModelIndex &index) const -> VPE::VProperty *
{
2014-09-10 19:57:08 +02:00
if (index.isValid())
{
auto *prop = static_cast<VProperty *>(index.internalPointer());
if (prop)
2014-09-10 19:57:08 +02:00
{
return prop;
2014-09-10 19:57:08 +02:00
}
}
return nullptr;
}
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::getPropertyID(const VProperty *prop) const -> QString
{
return d_ptr->Properties != nullptr ? d_ptr->Properties->getPropertyID(prop) : QString();
}
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::getIndexFromProperty(VProperty *property, int column) const -> QModelIndex
{
if (!property || column > columnCount() || column < 0)
2014-09-10 19:57:08 +02:00
{
2023-02-09 14:48:28 +01:00
return {};
2014-09-10 19:57:08 +02:00
}
VProperty* parentItem = property->getParent();
vpesizetype row = 0;
2014-09-10 19:57:08 +02:00
if (parentItem)
{
row = parentItem->getChildRow(property);
}
return createIndex(static_cast<int>(row), column, property);
}
void VPE::VPropertyModel::onDataChangedByModel(VProperty* property)
{
2024-02-19 17:09:56 +01:00
QModelIndex const tmpIndex = getIndexFromProperty(property, 1);
2014-09-10 19:57:08 +02:00
if (tmpIndex.isValid())
{
emit dataChanged(tmpIndex, tmpIndex);
emit onDataChangedByEditor(property);
}
}
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::getPropertySet() const -> const VPE::VPropertySet *
{
return d_ptr->Properties;
}
void VPE::VPropertyModel::clear(bool emit_signals)
{
setPropertySet(nullptr, emit_signals);
}
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::takePropertySet(VPropertySet *new_property_set, bool emit_signals) -> VPE::VPropertySet *
{
VPropertySet* tmpOldPropertySet = d_ptr->Properties;
2014-09-10 19:57:08 +02:00
if (emit_signals)
{
beginResetModel();
2014-09-10 19:57:08 +02:00
}
d_ptr->Properties = new_property_set;
2014-09-10 19:57:08 +02:00
if (emit_signals)
{
endResetModel();
2014-09-10 19:57:08 +02:00
}
return tmpOldPropertySet;
}
void VPE::VPropertyModel::setPropertySet(VPropertySet *property_set, bool emit_signals)
{
VPropertySet* tmpOldPropertySet = takePropertySet(property_set, emit_signals);
2014-09-14 11:16:59 +02:00
delete tmpOldPropertySet;
}
2023-05-03 13:07:02 +02:00
auto VPE::VPropertyModel::takeProperty(const QString &id) -> VPE::VProperty *
{
2024-02-19 17:09:56 +01:00
QModelIndex const tmpIndex = getIndexFromProperty(getProperty(id));
2014-09-10 19:57:08 +02:00
if (d_ptr->Properties && tmpIndex.isValid())
{
beginRemoveRows(tmpIndex.parent(), tmpIndex.row(), tmpIndex.row());
VProperty* tmpProp = d_ptr->Properties->takeProperty(id);
endRemoveRows();
return tmpProp;
}
return nullptr;
}
void VPE::VPropertyModel::removeProperty(const QString &id)
{
2024-02-19 17:09:56 +01:00
QModelIndex const tmpIndex = getIndexFromProperty(getProperty(id));
2014-09-10 19:57:08 +02:00
if (d_ptr->Properties && tmpIndex.isValid())
{
beginRemoveRows(tmpIndex.parent(), tmpIndex.row(), tmpIndex.row());
d_ptr->Properties->removeProperty(id);
endRemoveRows();
}
}