diff options
author | Arno <am@disconnect.de> | 2013-04-11 09:31:11 +0200 |
---|---|---|
committer | Arno <am@disconnect.de> | 2013-04-11 09:31:11 +0200 |
commit | ecc8d0f8d6b1266d214ec1fad5733fcad4d2bf33 (patch) | |
tree | e3f496eaa30a41615dfeb6162bc4796982aec55e | |
parent | a39b9bb410593edf105be3ce808b6bfd94f95cde (diff) | |
download | SheMov-ecc8d0f8d6b1266d214ec1fad5733fcad4d2bf33.tar.gz SheMov-ecc8d0f8d6b1266d214ec1fad5733fcad4d2bf33.tar.bz2 SheMov-ecc8d0f8d6b1266d214ec1fad5733fcad4d2bf33.zip |
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.
-rw-r--r-- | mappingtreemodel.cpp | 74 | ||||
-rw-r--r-- | mappingtreemodel.h | 4 |
2 files changed, 60 insertions, 18 deletions
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()){ diff --git a/mappingtreemodel.h b/mappingtreemodel.h index 228adee..247c96d 100644 --- a/mappingtreemodel.h +++ b/mappingtreemodel.h @@ -40,7 +40,7 @@ class MappingTreeModel : public SmTreeModel { QList<QVariant> childList(const QVariant &value, int column = 0) const; QStringList path(QModelIndex &idx) const; QModelIndex indexFromPath(const QString &path, int column = 0) const; - bool setData(const QModelIndex &index, const QVariant &value, int role); + bool setData(const QModelIndex &idx, const QVariant &value, int role); bool move(const QModelIndex &source, const QModelIndex &dest); bool addMappingType(const QString &type); bool deleteMappingType(int typeId); @@ -68,6 +68,8 @@ class MappingTreeModel : public SmTreeModel { int addChild(const QString &name, int type); void getMappingTypes(); void getChildrenRecursive(SmTreeItem *item); + bool updateType(SmTreeItem *item, const QVariant &value); + bool updateChild(SmTreeItem *item, const QVariant &value); QStringList getPathsRecursive(SmTreeItem *parent) const; QList<QVariant> getChildListRecursive(SmTreeItem *item, int column) const; QString basePath(SmTreeItem *item) const; |