From f30d07e5acb83dd74bc6e6dc75b8b1c9e39e6e01 Mon Sep 17 00:00:00 2001 From: Arno Date: Sat, 24 Mar 2012 08:40:23 +0100 Subject: Show mappings in PictureViewer2 Hell, this was one heck of a bitch. Lots of changes just to show the mapping tree in PictureViewer2. Did I mention that I hate recursion? Added a function for fetching a SmTreeItem * from MappingTreeModel for paths, just to turn it into a QTextDocument in PictureViewer2. Of course everything is recursive. Did I say that I hate recursion? Well, as said in the comments, one recursion function seems fishy, but it works (tm). --- mappingtreemodel.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'mappingtreemodel.cpp') diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index 42c1380..ce96ff4 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -7,6 +7,7 @@ #include #include +#include #include @@ -43,6 +44,8 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr mUpdateParentQ->prepare("UPDATE mappings_parents SET iparent_id = :pid WHERE imapping_id = :id"); mDeleteMappingParentQ = new QSqlQuery(mDb); 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"); } MappingTreeModel::~MappingTreeModel(){ @@ -57,6 +60,7 @@ MappingTreeModel::~MappingTreeModel(){ delete mAddParentQ; delete mDeleteChildQ; delete mUpdateParentQ; + delete mMappingsForFileIdQ; mDb = QSqlDatabase(); } @@ -87,6 +91,36 @@ QString MappingTreeModel::mappingTypeNameFromId(int id) const{ return QString(); } +//caller has ownership of this item! +SmTreeItem *MappingTreeModel::treeFromPaths(const QStringList &paths){ + if(paths.isEmpty()){ + return 0; + } + QHash partsHash; + SmTreeItem *root = new SmTreeItem(1); + partsHash.insert(QString(), root); + SmTreeItem *pItem = root; + + // create tree + for(int i = 0; i < paths.count(); ++i){ + //split the paths + QStringList parts = paths.at(i).split(forbidden()); + //process path items + for(int j = 0; j < parts.count(); ++j){ + if(partsHash.contains(parts.at(j))){ + pItem = partsHash.value(parts.at(j)); //we've already seen this item, set it as new parent + }else{ + //create a new item, save old root + SmTreeItem *oldRoot = pItem; + pItem = new SmTreeItem(QList() << parts.at(j), oldRoot); + oldRoot->appendChild(pItem); + partsHash.insert(parts.at(j), pItem); + } + } + } + return root; +} + QVariant MappingTreeModel::data(const QModelIndex &index, int role) const{ if(!index.isValid()){ return QVariant(); @@ -118,6 +152,17 @@ QList MappingTreeModel::childList(const QVariant &value, int column) c return QList() << value; } +QList MappingTreeModel::mappingsForFile(const QVariant &fileId) const{ + QList retval; + mMappingsForFileIdQ->addBindValue(fileId); + if(mMappingsForFileIdQ->exec()){ + while(mMappingsForFileIdQ->next()){ + retval << mMappingsForFileIdQ->value(0); + } + } + return retval; +} + QModelIndex MappingTreeModel::indexFromPath(const QString &path, int column) const{ QStringList items = path.split("/"); if(items.isEmpty() || column >= NumFields){ -- cgit v1.2.3-70-g09d2