diff options
Diffstat (limited to 'mappingtreemodel.cpp')
-rw-r--r-- | mappingtreemodel.cpp | 55 |
1 files changed, 17 insertions, 38 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); |