From 184448e767ccdeae66fe4d795757cace98033b0e Mon Sep 17 00:00:00 2001 From: Arno Date: Thu, 4 Oct 2012 06:59:44 +0200 Subject: Fix Mappings in PictureViewer2 Another fix for the new MappingTreeModel database layout: Make the mappings display correctly when showing an archived picture. For performance reasons I had to make PicFilesModel global and put it into a separate file. Also clean up MappingTreeModel: remove comments and unused functions. --- mappingtreemodel.cpp | 105 +++++++++++---------------------------------------- 1 file changed, 22 insertions(+), 83 deletions(-) (limited to 'mappingtreemodel.cpp') diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index f2af964..c32574a 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -115,11 +115,12 @@ SmTreeItem *MappingTreeModel::treeFromPaths(const QStringList &paths){ }else{ //create a new item, save old root SmTreeItem *oldRoot = pItem; - pItem = new SmTreeItem(QList() << parts.at(j), oldRoot); + pItem = new SmTreeItem(QList() << parts.at(j), partsHash.value(parts.at(j))); oldRoot->appendChild(pItem); partsHash.insert(parts.at(j), pItem); } } + pItem = root; } return root; } @@ -141,11 +142,6 @@ QVariant MappingTreeModel::data(const QModelIndex &index, int role) const{ if(role == MapParentIdRole){ return item->data(MapParentId); } - if(role == Qt::ForegroundRole){ - if(mSelectedMappings.contains(item)){ - return QColor(Qt::blue); - } - } return SmTreeModel::data(index, role); } @@ -292,13 +288,6 @@ 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; @@ -324,34 +313,8 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &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); - mSelectChildQ->bindValue(":type", mType); - mSelectChildQ->exec(); - while(mSelectChildQ->next()){ - int where = lowerBound(pItem, mSelectChildQ->value(1), Name); - if(insertRows(where, 1, parent)){ - QModelIndex newIdx = index(where, 0, parent); - setData(newIdx, mSelectChildQ->value(1), NameRole); - setData(newIdx, mSelectChildQ->value(0), IdRole); - setData(newIdx, mSelectChildQ->value(2), AddedRole); - } - if(pItem->parent() != root()){ - mAddParentQ->bindValue(":id", mSelectChildQ->value(0)); //Id - mAddParentQ->bindValue(":parentid", pItem->data(Id)); - mAddParentQ->exec(); - } - //mValidMappings.clear(); - //mValidMappings = mappingData(root()); - return true; - } - }*/ return false; } @@ -366,8 +329,6 @@ bool MappingTreeModel::deleteChild(const QModelIndex &idx){ mDeleteChildQ->bindValue(":id", item->data(Id)); if(mDeleteChildQ->exec()){ removeRows(idx.row(), 1, idx.parent()); - mValidMappings.clear(); - mValidMappings = mappingData(root()); return true; } return false; @@ -382,36 +343,13 @@ int MappingTreeModel::childCount(const QModelIndex &idx) const{ } MappingData MappingTreeModel::mappingDataFromIndex(QModelIndex &idx) const{ - MappingData retval = { -1, QString(), QStringList() }; + MappingData retval = { -1, QString(), QList() }; if(!idx.isValid()){ return retval; } retval.id = idx.data(MappingTreeModel::MapParentIdRole).toInt(); retval.name = idx.data(MappingTreeModel::NameRole).toString(); - retval.path = path(idx); - return retval; -} - -//continue here! -MappingData MappingTreeModel::mappingDataFromId(int mappingId) const{ - MappingData retval = { -1, QString(), QStringList() }; - QModelIndex mapIdx = findRecursive(mappingId, MappingTreeModel::Id, rootIndex()); - //REMOVE ME - Q_ASSERT(mapIdx.isValid()); - retval.id = mappingId; - retval.name = mapIdx.data(MappingTreeModel::NameRole).toString(); - qDebug() << "mapping" << retval.id << retval.name; - SmTreeItem *mapItem = static_cast(mapIdx.internalPointer()); - //REMOVE ME - Q_ASSERT(mapItem); - //retval.path = path(mapItem); - - /*foreach(MappingData d, mValidMappings){ - if(d.id == mappingId){ - retval = d; - break; - } - }*/ + retval.path << path(idx); return retval; } @@ -438,8 +376,6 @@ void MappingTreeModel::populate(){ getChildrenRecursive(childItem); } setRoot(rootItem); - //mValidMappings.clear(); - //mValidMappings = mappingData(root()); emit needExpansion(); } } @@ -571,7 +507,7 @@ QList MappingTreeModel::mappingData(SmTreeItem *item){ retval << mappingData(item->child(i)); } } - MappingData mapItem = { item->data(Id).toInt(), item->data(Name).toString(), QStringList() }; + MappingData mapItem = { item->data(Id).toInt(), item->data(Name).toString(), QList() }; QStringList path; SmTreeItem *p = item; while(p->parent()){ @@ -579,7 +515,7 @@ QList MappingTreeModel::mappingData(SmTreeItem *item){ p = p->parent(); } std::reverse(path.begin(), path.end()); - mapItem.path = path; + mapItem.path << path; retval << mapItem; return retval; } @@ -606,20 +542,23 @@ bool MappingTreeResultModel::setData(const QModelIndex &index, const QVariant &v void MappingTreeResultModel::addItem(const MappingData &data){ SmTreeItem *curItem = root(); - for(int i = 0; i < data.path.count(); ++i){ - int childPos = hasChild(curItem, data.path.at(i)); - if(childPos != -1){ - //child already exists - curItem = curItem->child(childPos); - continue; - }else{ - //insert child - int id = -1; - if(i == data.path.count() - 1){ - id = data.id; + QList paths = data.path; + foreach(QStringList p, paths){ + for(int i = 0; i < p.count(); ++i){ + int childPos = hasChild(curItem, p.at(i)); + if(childPos != -1){ + //child already exists + curItem = curItem->child(childPos); + continue; + }else{ + //insert child + int id = -1; + if(i == p.count() - 1){ + id = data.id; + } + QModelIndex curIdx = insertChild(p.at(i), id, curItem); + curItem = itemAt(curIdx); } - QModelIndex curIdx = insertChild(data.path.at(i), id, curItem); - curItem = itemAt(curIdx); } } } -- cgit v1.2.3-70-g09d2