From 2defc44300bd18917e6e5e2327ebb72f951ff7b1 Mon Sep 17 00:00:00 2001 From: Arno Date: Sat, 17 Nov 2012 05:14:56 +0100 Subject: Finally { fix MappingTreeModel }!!! I think I did it! Fixed MappingTreeModel to make it usable, avoid all the traps with the insane column and variable names! Now we can actually have the same mapping with different parents! Yay! Hope it won't come back and bite me! --- mappingtreemodel.cpp | 23 +++++++++-------------- mappingtreemodel.h | 3 +-- mappingtreewidget.cpp | 2 +- picfilesmodel.cpp | 7 +++---- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index ddfd5e2..cf8bfc0 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -219,7 +219,7 @@ bool MappingTreeModel::setData(const QModelIndex &index, const QVariant &value, } bool MappingTreeModel::move(const QModelIndex &source, const QModelIndex &dest){ - QVariant sourceId = source.data(DescIdRole); + QVariant sourceId = source.data(MappingIdRole); QVariant destId = dest.data(MappingIdRole); mDb.transaction(); mUpdateParentQ->bindValue(":id", destId); @@ -295,7 +295,7 @@ bool MappingTreeModel::deleteChild(const QModelIndex &idx){ if(item->childCount() > 0){ return false; } - mDeleteMappingParentQ->bindValue(":id", item->data(DescId)); + mDeleteMappingParentQ->bindValue(":id", item->data(MappingId)); if(mDeleteMappingParentQ->exec()){ removeRows(idx.row(), 1, idx.parent()); return true; @@ -311,7 +311,7 @@ MappingData MappingTreeModel::mappingDataFromIndex(QModelIndex &idx) const{ } retval.mappingId = idx.data(MappingTreeModel::MappingIdRole).toInt(); retval.parentId = idx.data(MappingTreeModel::MappingParentIdRole).toInt(); - retval.myId = idx.data(MappingTreeModel::DescIdRole).toInt(); + retval.descId = idx.data(MappingTreeModel::DescIdRole).toInt(); retval.name = idx.data(MappingTreeModel::NameRole).toString(); retval.path << path(idx); return retval; @@ -336,10 +336,10 @@ void MappingTreeModel::populate(){ SmTreeItem *rootItem = new SmTreeItem(NumFields); rootItem->setData(DescId, -1); rootItem->setData(MappingId, -1); - mSeen.clear(); + rootItem->setData(MappingParentId, -1); while(rootQ.next()){ QList childData; - childData << rootQ.value(2) << rootQ.value(1) << rootQ.value(3) << rootQ.value(0) << rootQ.value(4); + childData << rootQ.value(2) << rootQ.value(0) << rootQ.value(3) << rootQ.value(1) << rootQ.value(4); SmTreeItem *childItem = new SmTreeItem(childData, rootItem); rootItem->appendChild(childItem); getChildrenRecursive(childItem); @@ -398,7 +398,6 @@ void MappingTreeModel::getMappingTypes(){ } void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){ - //static QList seenIds; QSqlQuery cq(mDb); cq.prepare(mSParentsQ); cq.bindValue(":type", mType); @@ -406,11 +405,7 @@ void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){ if(cq.exec()){ while(cq.next()){ QList childData; - childData << cq.value(2) << cq.value(1) << cq.value(3) << cq.value(0) << cq.value(4); - if(mSeen.contains(childData.at((DescId)))){ - return; - } - mSeen << childData.at(DescId); + childData << cq.value(2) << cq.value(0) << cq.value(3) << cq.value(1) << cq.value(4); SmTreeItem *child = new SmTreeItem(childData, item); item->appendChild(child); getChildrenRecursive(child); @@ -518,7 +513,7 @@ void MappingTreeResultModel::addItem(const MappingData &data){ if(i == p.count() - 1){ mappingId = data.mappingId; parentId = data.parentId; - myId = data.myId; + myId = data.descId; } QModelIndex curIdx = insertChild(p.at(i), mappingId, parentId, myId, curItem); curItem = itemAt(curIdx); @@ -580,8 +575,8 @@ QList MappingTreeResultModel::columnValuesRecursive(SmTreeItem *parent return retval; } -MappingData::MappingData() : mappingId(-1), parentId(-1), myId(-1) {} +MappingData::MappingData() : mappingId(-1), parentId(-1), descId(-1) {} bool MappingData::isValid(){ - return !(mappingId == -1 && parentId == -1 && myId == -1); + return !(mappingId == -1 && parentId == -1 && descId == -1); } diff --git a/mappingtreemodel.h b/mappingtreemodel.h index ad4ff85..90eca8c 100644 --- a/mappingtreemodel.h +++ b/mappingtreemodel.h @@ -90,7 +90,6 @@ class MappingTreeModel : public SmTreeModel { const QString mForbidden; int mType; QSqlError mLastError; - QList mSeen; }; class MappingTreeResultModel : public SmTreeModel { @@ -121,7 +120,7 @@ struct MappingData { bool isValid(); int mappingId; int parentId; - int myId; + int descId; QString name; QList path; }; diff --git a/mappingtreewidget.cpp b/mappingtreewidget.cpp index 0881e3c..4cde673 100644 --- a/mappingtreewidget.cpp +++ b/mappingtreewidget.cpp @@ -227,7 +227,7 @@ void MappingTreeWidget::editChild(){ void MappingTreeWidget::selectionChanged(){ QModelIndex sel = selected(); QModelIndex real = mProxy->mapToSource(sel); - emit mappingChanged(real.data(MappingTreeModel::DescIdRole).toInt()); + emit mappingChanged(real.data(MappingTreeModel::MappingIdRole).toInt()); } void MappingTreeWidget::moveChild(){ diff --git a/picfilesmodel.cpp b/picfilesmodel.cpp index 4114eb5..6b8153f 100644 --- a/picfilesmodel.cpp +++ b/picfilesmodel.cpp @@ -40,7 +40,7 @@ PicFilesModel::~PicFilesModel(){ } void PicFilesModel::setMapping(int pMapId){ - QList ids = mMappingTreeModel->childList(pMapId, MappingTreeModel::DescId); + QList ids = mMappingTreeModel->childList(pMapId, MappingTreeModel::MappingId); mMappingIds.clear(); foreach(QVariant i, ids){ mMappingIds << i.toInt(); @@ -170,13 +170,12 @@ QList PicFilesModel::mappingDataFromFiles(const QList fileIds) } QList retval; foreach(int pId, parentIds){ - QModelIndex curIdx = mMappingTreeModel->findRecursive(pId, MappingTreeModel::DescId, mMappingTreeModel->rootIndex()); + QModelIndex curIdx = mMappingTreeModel->findRecursive(pId, MappingTreeModel::MappingId, mMappingTreeModel->rootIndex()); MappingData curData; - //curData.id = curIdx.data(MappingTreeModel::MyIdRole).toInt(); curData.mappingId = curIdx.data(MappingTreeModel::MappingIdRole).toInt(); curData.parentId = curIdx.data(MappingTreeModel::MappingParentIdRole).toInt(); curData.name = curIdx.data(MappingTreeModel::NameRole).toString(); - curData.path << mMappingTreeModel->path(curIdx); //.join("/"); + curData.path << mMappingTreeModel->path(curIdx); retval << curData; } return retval; -- cgit v1.2.3-70-g09d2