From ecc8d0f8d6b1266d214ec1fad5733fcad4d2bf33 Mon Sep 17 00:00:00 2001 From: Arno Date: Thu, 11 Apr 2013 09:31:11 +0200 Subject: Fix editing of MappingTreeModel Renaming/Editing of an item didn't work, because database restrictions hadn't been taken into account. tdescription_name is unique, so update the description_id if it already exists. --- mappingtreemodel.cpp | 74 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 17 deletions(-) (limited to 'mappingtreemodel.cpp') diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index 3d595c1..9c6be13 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -172,33 +172,30 @@ QModelIndex MappingTreeModel::indexFromPath(const QString &path, int column) con return retval; } -bool MappingTreeModel::setData(const QModelIndex &index, const QVariant &value, int role){ - if(!index.isValid()){ +bool MappingTreeModel::setData(const QModelIndex &idx, const QVariant &value, int role){ + if(!idx.isValid()){ return false; } - SmTreeItem *item = itemAt(index); + SmTreeItem *item = itemAt(idx); if(role == Qt::EditRole){ - if(index.column() == Name){ + if(idx.column() == Name){ if(value.toString().contains(mForbidden)){ return false; } } - mDb.transaction(); - QSqlQuery *q = 0; if(item == root()){ - q = mUpdateTypeQ; + if(updateType(item, value)){ + emit dataChanged(idx, idx); + return true; + } }else{ - q = mUpdateChildQ; - } - q->bindValue(":value", value); - q->bindValue(":id", item->data(DescId)); - if(q->exec()){ - item->setData(Name, value); - emit dataChanged(index, index); - mDb.commit(); - return true; + if(updateChild(item, value)){ + QModelIndex cstart = index(idx.row(), 0, idx.parent()); + QModelIndex cend = index(idx.row(), NumFields - 1, idx.parent()); + emit dataChanged(cstart, cend); + return true; + } } - mDb.rollback(); } if(role == NameRole){ if(value.toString().contains(mForbidden)){ @@ -413,6 +410,49 @@ void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){ } } +bool MappingTreeModel::updateType(SmTreeItem *item, const QVariant &value){ + mDb.transaction(); + mUpdateTypeQ->bindValue(":id", item->data(DescId)); + mUpdateTypeQ->bindValue(":value", value); + if(mUpdateTypeQ->exec()){ + mDb.commit(); + item->setData(Name, value); + return true; + } + mDb.rollback(); + return false; +} + +bool MappingTreeModel::updateChild(SmTreeItem *item, const QVariant &value){ + QSqlQuery findValue(mDb); + QVariant descRes; + findValue.prepare("SELECT idescription_id FROM mapping_description WHERE tdescription_name = :name"); + findValue.bindValue(":name", value); + findValue.exec(); + while(findValue.next()){ + descRes = findValue.value(0); + } + if(!descRes.isValid()){ + mUpdateChildQ->bindValue(":id", item->data(DescId)); + mUpdateChildQ->bindValue(":value", value); + if(mUpdateChildQ->exec()){ + item->setData(Name, value); + return true; + } + }else{ + QSqlQuery updateChild(mDb); + updateChild.prepare("UPDATE mapping_parents SET idescription_id = :did WHERE imapping_parents_id = :id"); + updateChild.bindValue(":did", descRes); + updateChild.bindValue(":id", item->data(MappingId)); + if(updateChild.exec()){ + item->setData(Name, value); + item->setData(DescId, descRes); + return true; + } + } + return false; +} + QStringList MappingTreeModel::getPathsRecursive(SmTreeItem *parent) const{ QStringList retval; if(!basePath(parent).isEmpty()){ -- cgit v1.2.3-70-g09d2