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