valentina_old/src/libs/vpropertyexplorer/vpropertymodel.cpp

365 lines
9.1 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"
using namespace VPE;
#include "vpropertymodel_p.h"
VPropertyModel::VPropertyModel(VPropertyModelPrivate *d, QObject *parent)
: QAbstractItemModel(parent), d_ptr(d)
{
}
VPropertyModel::VPropertyModel(QObject * parent) :
QAbstractItemModel(parent), d_ptr(new VPropertyModelPrivate())
{
}
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
bool VPropertyModel::addProperty(VProperty* property, const QString& id, const QString &parentid, bool emitsignals)
{
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);
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)
{
endInsertRows();
2014-09-10 19:57:08 +02:00
}
return true;
}
//! Creates a property and adds it to the model
2014-09-10 19:57:08 +02:00
VProperty* VPropertyModel::createProperty(const QString& id, const QString& name, const QString& parentid,
const QVariant& data)
{
VProperty* 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
VProperty* VPropertyModel::getProperty(const QString& id)
{
return d_ptr->Properties != nullptr ? d_ptr->Properties->getProperty(id) : nullptr;
}
//! Returns the model index at row/column
QModelIndex VPropertyModel::index(int row, int column, const QModelIndex& parent) const
{
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
QModelIndex VPropertyModel::parent ( const QModelIndex & index ) const
{
if (!index.isValid())
2014-09-10 19:57:08 +02:00
{
return QModelIndex();
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();
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 19:57:08 +02:00
if (parents_row >= 0)
{
return createIndex(parents_row, 0, parentItem);
2014-09-10 19:57:08 +02:00
}
}
}
return QModelIndex();
}
//! Returns the item flags for the given index
Qt::ItemFlags VPropertyModel::flags (const QModelIndex& index) const
{
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
bool VPropertyModel::setData (const QModelIndex& index, const QVariant& value, int role)
{
VProperty* tmpProperty = getProperty(index);
2014-09-10 19:57:08 +02:00
if (index.column() == 1 && tmpProperty)
{
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
QModelIndex 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
QVariant VPropertyModel::data ( const QModelIndex & index, int role ) const
{
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);
}
QVariant VPropertyModel::headerData (int section, Qt::Orientation orientation, int role) const
{
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
int VPropertyModel::rowCount ( const QModelIndex & parent ) const
{
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 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 d_ptr->Properties->getRootPropertyCount();
2014-09-10 19:57:08 +02:00
}
return 0;
}
//! Returns the number of columns
int VPropertyModel::columnCount ( const QModelIndex & parent) const
{
Q_UNUSED(parent)
return 2;
}
//! Gets a property by its ModelIndex
VProperty* VPropertyModel::getProperty(const QModelIndex &index) const
{
2014-09-10 19:57:08 +02:00
if (index.isValid())
{
VProperty* 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;
}
2014-09-14 11:16:59 +02:00
QString VPropertyModel::getPropertyID(const VProperty *prop) const
{
return d_ptr->Properties != nullptr ? d_ptr->Properties->getPropertyID(prop) : QString();
}
QModelIndex VPropertyModel::getIndexFromProperty(VProperty* property, int column) const
{
if (!property || column > columnCount() || column < 0)
2014-09-10 19:57:08 +02:00
{
return QModelIndex();
}
VProperty* parentItem = property->getParent();
int row = 0;
2014-09-10 19:57:08 +02:00
if (parentItem)
{
row = parentItem->getChildRow(property);
}
return createIndex(row, column, property);
}
void VPropertyModel::onDataChangedByModel(VProperty* property)
{
QModelIndex tmpIndex = getIndexFromProperty(property, 1);
2014-09-10 19:57:08 +02:00
if (tmpIndex.isValid())
{
emit dataChanged(tmpIndex, tmpIndex);
emit onDataChangedByEditor(property);
}
}
const VPropertySet *VPropertyModel::getPropertySet() const
{
return d_ptr->Properties;
}
void VPropertyModel::clear(bool emit_signals)
{
setPropertySet(nullptr, emit_signals);
}
VPropertySet *VPropertyModel::takePropertySet(VPropertySet *new_property_set, bool emit_signals)
{
VPropertySet* tmpOldPropertySet = d_ptr->Properties;
2014-09-10 19:57:08 +02:00
if (emit_signals)
{
emit beginResetModel();
}
d_ptr->Properties = new_property_set;
2014-09-10 19:57:08 +02:00
if (emit_signals)
{
emit endResetModel();
}
return tmpOldPropertySet;
}
void 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;
}
VProperty *VPropertyModel::takeProperty(const QString &id)
{
QModelIndex 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 VPropertyModel::removeProperty(const QString &id)
{
QModelIndex 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();
}
}