diff options
author | Arno <am@disconnect.de> | 2012-10-06 12:39:54 +0200 |
---|---|---|
committer | Arno <am@disconnect.de> | 2012-10-06 12:41:09 +0200 |
commit | d562d4a3527044aaeb36534bc2d4b65b587c9bb3 (patch) | |
tree | f3d8e3d3c94f1c44aec26ae77136cb0771573320 | |
parent | e2d4843586d84da62e325d1cb0025a795b162c1c (diff) | |
download | SheMov-d562d4a3527044aaeb36534bc2d4b65b587c9bb3.tar.gz SheMov-d562d4a3527044aaeb36534bc2d4b65b587c9bb3.tar.bz2 SheMov-d562d4a3527044aaeb36534bc2d4b65b587c9bb3.zip |
Fix MappingTreeModel
* Don't recurse ad infinitum when having nodes with the same name.
* Add all relevant data to the model.
There's still one quirk left: you can't do something like this blub ->
blub2 if bla -> blub ->blub2 already exists, but that's something I can
live with.
-rw-r--r-- | mappingtreemodel.cpp | 55 | ||||
-rw-r--r-- | mappingtreemodel.h | 7 |
2 files changed, 21 insertions, 41 deletions
diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index 50586e8..7817c96 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -22,7 +22,7 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr getMappingTypes(); //prepare Queries - mSParentsQ = "SELECT mappings_parents2.imappings_parents_id, mappings_parents2.imapping_id, mappings.tmapping_name, mappings.tscreated FROM mappings_parents2, mappings WHERE mappings_parents2.iparent_id = :pid AND mappings.imapping_type = :type AND mappings_parents2.imapping_id = mappings.imapping_id ORDER BY mappings.tmapping_name"; + mSParentsQ = "SELECT mappings_parents2.imappings_parents_id, mappings_parents2.imapping_id, mappings.tmapping_name, mappings.tscreated, mappings_parents2.iparent_id FROM mappings_parents2, mappings WHERE mappings_parents2.iparent_id = :pid AND mappings.imapping_type = :type AND mappings_parents2.imapping_id = mappings.imapping_id ORDER BY mappings.tmapping_name"; mUpdateTypeQ = new QSqlQuery(mDb); mUpdateTypeQ->prepare("UPDATE mappings_type SET tmappings_type_name = :value WHERE imappings_type_id = :id"); mUpdateChildQ = new QSqlQuery(mDb); @@ -117,8 +117,8 @@ QVariant MappingTreeModel::data(const QModelIndex &index, int role) const{ if(role == NameRole){ return item->data(Name); } - if(role == ParentIdRole){ - return item->data(ParentId); + if(role == MappingIdRole){ + return item->data(MappingId); } if(role == AddedRole){ return item->data(Added); @@ -191,7 +191,7 @@ bool MappingTreeModel::setData(const QModelIndex &index, const QVariant &value, q = mUpdateChildQ; } q->bindValue(":value", value); - q->bindValue(":id", item->data(ParentId)); + q->bindValue(":id", item->data(MappingId)); if(q->exec()){ item->setData(Name, value); emit dataChanged(index, index); @@ -206,8 +206,8 @@ bool MappingTreeModel::setData(const QModelIndex &index, const QVariant &value, } item->setData(Name, value); } - if(role == ParentIdRole){ - item->setData(ParentId, value); + if(role == MappingIdRole){ + item->setData(MappingId, value); } if(role == AddedRole){ item->setData(Added, value); @@ -220,7 +220,7 @@ bool MappingTreeModel::setData(const QModelIndex &index, const QVariant &value, bool MappingTreeModel::move(const QModelIndex &source, const QModelIndex &dest){ QVariant sourceId = source.data(MyIdRole); - QVariant destId = dest.data(ParentIdRole); + QVariant destId = dest.data(MappingIdRole); mDb.transaction(); mUpdateParentQ->bindValue(":id", destId); mUpdateParentQ->bindValue(":sid", sourceId); @@ -276,9 +276,7 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent) //now check if we already have a mapping with the same parent mDb.transaction(); mAddParentQ->bindValue(":id", id); - // seems we're a child of root - //QVariant ppId = parent.data(ParentIdRole).isValid() ? parent.data(ParentIdRole) : -1; - mAddParentQ->bindValue(":parentid", parent.data(ParentIdRole)); + mAddParentQ->bindValue(":parentid", parent.data(MappingIdRole)); if(mAddParentQ->exec()){ mDb.commit(); populate(); @@ -287,23 +285,6 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent) mLastError = mAddParentQ->lastError(); mDb.rollback(); return false; - //we're good now... db makes sure we're unique - SmTreeItem *pItem = itemAt(parent); - int where = lowerBound(pItem, name, Name); - if(insertRows(where, 1, parent)){ - QSqlQuery pIdQuery = QSqlQuery("SELECT currval('mappings_parents_id__seq')", mDb); - int parentId = -1; - while(pIdQuery.next()){ - parentId = pIdQuery.value(0).toInt(); - } - QModelIndex newIdx = index(where, 0, parent); - setData(newIdx, name, NameRole); - setData(newIdx, id, ParentIdRole); - setData(newIdx, parentId, MyIdRole); - mDb.commit(); - return true; - } - return false; } bool MappingTreeModel::deleteChild(const QModelIndex &idx){ @@ -352,10 +333,11 @@ void MappingTreeModel::populate(){ if(rootQ.exec()){ SmTreeItem *rootItem = new SmTreeItem(NumFields); rootItem->setData(MyId, -1); - rootItem->setData(ParentId, -1); + rootItem->setData(MappingId, -1); + mSeen.clear(); while(rootQ.next()){ QList<QVariant> childData; - childData << rootQ.value(2) << rootQ.value(1) << rootQ.value(3) << rootQ.value(0); + childData << rootQ.value(2) << rootQ.value(1) << rootQ.value(3) << rootQ.value(0) << rootQ.value(4); SmTreeItem *childItem = new SmTreeItem(childData, rootItem); rootItem->appendChild(childItem); getChildrenRecursive(childItem); @@ -413,23 +395,20 @@ void MappingTreeModel::getMappingTypes(){ } } -#include <QDebug> - void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){ - static QList<QVariant> seenIds; + //static QList<QVariant> seenIds; QSqlQuery cq(mDb); cq.prepare(mSParentsQ); cq.bindValue(":type", mType); - cq.bindValue(":pid", item->data(ParentId)); + cq.bindValue(":pid", item->data(MappingId)); if(cq.exec()){ while(cq.next()){ QList<QVariant> childData; - childData << cq.value(2) << cq.value(1) << cq.value(3) << cq.value(0); - if(seenIds.contains(item->data(MyId))){ - qDebug() << "already seen" << childData.value(0); - continue; + childData << cq.value(2) << cq.value(1) << cq.value(3) << cq.value(0) << cq.value(4); + if(mSeen.contains(childData.at((MyId)))){ + return; } - seenIds << item->data(MyId); + mSeen << childData.at(MyId); SmTreeItem *child = new SmTreeItem(childData, item); item->appendChild(child); getChildrenRecursive(child); diff --git a/mappingtreemodel.h b/mappingtreemodel.h index beb7d7f..38c0c7b 100644 --- a/mappingtreemodel.h +++ b/mappingtreemodel.h @@ -21,9 +21,9 @@ class SmTreeItem; class MappingTreeModel : public SmTreeModel { Q_OBJECT public: - enum Roles { NameRole = Qt::UserRole + 1, ParentIdRole = Qt::UserRole + 2, AddedRole = Qt::UserRole + 3, MyIdRole = Qt::UserRole + 4 }; - enum Fields { Name = 0, ParentId = 1, Added = 2, MyId = 3 }; - enum { NumFields = 4 }; + enum Roles { NameRole = Qt::UserRole + 1, MappingIdRole = Qt::UserRole + 2, AddedRole = Qt::UserRole + 3, MyIdRole = Qt::UserRole + 4, ParentIdRole = Qt::UserRole + 5 }; + enum Fields { Name = 0, MappingId = 1, Added = 2, MyId = 3, ParentId = 4 }; + enum { NumFields = 5 }; MappingTreeModel(QStringList &headers, QObject *parent = 0); ~MappingTreeModel(); @@ -90,6 +90,7 @@ class MappingTreeModel : public SmTreeModel { const QString mForbidden; int mType; QSqlError mLastError; + QList<QVariant> mSeen; }; class MappingTreeResultModel : public SmTreeModel { |