diff options
Diffstat (limited to 'mappingtreemodel.cpp')
-rw-r--r-- | mappingtreemodel.cpp | 117 |
1 files changed, 91 insertions, 26 deletions
diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index ce96ff4..d63ef63 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -22,8 +22,7 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr getMappingTypes(); //prepare Queries - mTypeParentsQ = new QSqlQuery(mDb); - mTypeParentsQ->prepare("SELECT imapping_id, tmapping_name, tscreated FROM mappings WHERE imapping_id NOT IN (SELECT imapping_id FROM mappings_parents) AND imapping_type = :type ORDER BY tmapping_name"); + 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"; mUpdateTypeQ = new QSqlQuery(mDb); mUpdateTypeQ->prepare("UPDATE mappings_type SET tmappings_type_name = :value WHERE imappings_type_id = :id"); mUpdateChildQ = new QSqlQuery(mDb); @@ -46,11 +45,12 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr mDeleteMappingParentQ->prepare("DELETE FROM mappings_parents WHERE imapping_id = :id"); mMappingsForFileIdQ = new QSqlQuery(mDb); mMappingsForFileIdQ->prepare("SELECT DISTINCT(imapping_id) FROM pics_mappings WHERE ipics_id = :pid"); + mMappingsQ = new QSqlQuery(mDb); + mMappingsQ->prepare("SELECT tmapping_name, imapping_id FROM mappings WHERE imapping_type = :type"); } MappingTreeModel::~MappingTreeModel(){ delete mTypesQ; - delete mTypeParentsQ; delete mUpdateTypeQ; delete mUpdateChildQ; delete mAddMappingTypeQ; @@ -61,6 +61,7 @@ MappingTreeModel::~MappingTreeModel(){ delete mDeleteChildQ; delete mUpdateParentQ; delete mMappingsForFileIdQ; + delete mMappingsQ; mDb = QSqlDatabase(); } @@ -146,8 +147,10 @@ QVariant MappingTreeModel::data(const QModelIndex &index, int role) const{ QList<QVariant> MappingTreeModel::childList(const QVariant &value, int column) const{ QModelIndex itemIdx = findRecursive(value, column, createIndex(0, 0, root())); SmTreeItem *item = static_cast<SmTreeItem*>(itemIdx.internalPointer()); - if(item->childCount()){ - return getChildListRecursive(item, column); + if(item){ + if(item->childCount()){ + return getChildListRecursive(item, column); + } } return QList<QVariant>() << value; } @@ -264,6 +267,13 @@ bool MappingTreeModel::deleteMappingType(int typeId){ return false; } +/* + * FIXME! + * this is total crap. The model has to be redesigned. + * Like this children can never have the same name as parents. + * Too drunk to think about it now. + */ + bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent){ if(!parent.isValid()){ return false; @@ -271,8 +281,29 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent) if(name.toString().contains(mForbidden)){ return false; } + //check if we already have a mapping with this name + int id = mMappings.value(name.toString(), -1); + if(id == -1){ + id = addChild(name.toString(), mType); + } + //now check if we already have a mapping with the same parent + mAddParentQ->bindValue(":id", id); + mAddParentQ->bindValue(":parentid", parent.data(IdRole)); + if(!mAddParentQ->exec()){ + return false; + } + //we're good now... db makes sure we're unique SmTreeItem *pItem = itemAt(parent); - mAddChildQ->bindValue(":name", name); + int where = lowerBound(pItem, name, Name); + if(insertRows(where, 1, parent)){ + QModelIndex newIdx = index(where, 0, parent); + setData(newIdx, name, NameRole); + setData(newIdx, id, IdRole); + //setData(newIdx, AddedRole); + return true; + } + + /*mAddChildQ->bindValue(":name", name); mAddChildQ->bindValue(":type", mType); if(mAddChildQ->exec()){ mSelectChildQ->bindValue(":name", name); @@ -291,11 +322,11 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent) mAddParentQ->bindValue(":parentid", pItem->data(Id)); mAddParentQ->exec(); } - mValidMappings.clear(); - mValidMappings = mappingData(root()); + //mValidMappings.clear(); + //mValidMappings = mappingData(root()); return true; } - } + }*/ return false; } @@ -345,29 +376,60 @@ void MappingTreeModel::populate(){ return; } // get nodes with root as parent - mTypeParentsQ->bindValue(":type", mType); - if(mTypeParentsQ->exec()){ + QSqlQuery rootQ(mDb); + rootQ.prepare(mSParentsQ); + rootQ.bindValue(":type", mType); + rootQ.bindValue(":pid", -1); + if(rootQ.exec()){ SmTreeItem *rootItem = new SmTreeItem(NumFields); - SmTreeItem *firstChild = new SmTreeItem(NumFields, rootItem); - firstChild->setData(Name, mappingTypeNameFromId(mType)); - //no real id needed... conflicts with mapping ids! - firstChild->setData(Id, 0); - rootItem->appendChild(firstChild); - //collect children recursive - while(mTypeParentsQ->next()){ + while(rootQ.next()){ QList<QVariant> childData; - childData << mTypeParentsQ->value(1) << mTypeParentsQ->value(0) << mTypeParentsQ->value(2); - SmTreeItem *childItem = new SmTreeItem(childData, firstChild); - firstChild->appendChild(childItem); + childData << rootQ.value(2) << rootQ.value(1) << rootQ.value(3) << rootQ.value(0); + SmTreeItem *childItem = new SmTreeItem(childData, rootItem); + rootItem->appendChild(childItem); getChildrenRecursive(childItem); } setRoot(rootItem); - mValidMappings.clear(); - mValidMappings = mappingData(root()); + //mValidMappings.clear(); + //mValidMappings = mappingData(root()); emit needExpansion(); } } +void MappingTreeModel::setType(int type){ + mType = type; + getMappings(); +} + +void MappingTreeModel::getMappings(){ + mMappingsQ->bindValue(":type", mType); + if(mMappingsQ->exec()){ + mMappings.clear(); + while(mMappingsQ->next()){ + mMappings.insert(mMappingsQ->value(0).toString(), mMappingsQ->value(1).toInt()); + } + } +} + +int MappingTreeModel::addChild(const QString &name, int type){ + mAddChildQ->bindValue(":name", name); + mAddChildQ->bindValue(":type", type); + if(mAddChildQ->exec()){ + mSelectChildQ->bindValue(":name", name); + mSelectChildQ->bindValue(":type", type); + int c = 0; + mSelectChildQ->exec(); + while(mSelectChildQ->next()){ + ++c; + mMappings.insert(mSelectChildQ->value(1).toString(), mSelectChildQ->value(0).toInt()); + } + if(c > 1){ + qWarning("addChild yielded more than 1 result!"); + } + } + return mMappings.value(name); +} + void MappingTreeModel::getMappingTypes(){ bool qRes = mTypesQ->exec(); if(qRes){ @@ -384,12 +446,15 @@ void MappingTreeModel::getMappingTypes(){ void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){ QSqlQuery cq(mDb); - cq.prepare("SELECT mappings.imapping_id, mappings.tmapping_name, mappings.tscreated FROM mappings, mappings_parents WHERE mappings_parents.iparent_id = :id AND mappings.imapping_id = mappings_parents.imapping_id ORDER BY mappings.tmapping_name"); - cq.bindValue(":id", item->data(Id)); + cq.prepare(mSParentsQ); + cq.bindValue(":type", mType); + cq.bindValue(":pid", item->data(Id)); + //cq.prepare("SELECT mappings.imapping_id, mappings.tmapping_name, mappings.tscreated FROM mappings, mappings_parents WHERE mappings_parents.iparent_id = :id AND mappings.imapping_id = mappings_parents.imapping_id ORDER BY mappings.tmapping_name"); + //cq.bindValue(":id", item->data(Id)); if(cq.exec()){ while(cq.next()){ QList<QVariant> childData; - childData << cq.value(1) << cq.value(0) << cq.value(2); + childData << cq.value(2) << cq.value(1) << cq.value(3) << cq.value(0); SmTreeItem *child = new SmTreeItem(childData, item); item->appendChild(child); getChildrenRecursive(child); |