diff options
Diffstat (limited to 'mappingtreemodel.cpp')
-rw-r--r-- | mappingtreemodel.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp index 8872d45..2dec7fa 100644 --- a/mappingtreemodel.cpp +++ b/mappingtreemodel.cpp @@ -8,6 +8,8 @@ #include <QSqlDatabase> #include <QSqlQuery> +#include <algorithm> + #include "mappingtreemodel.h" #include "smtreeitem.h" @@ -37,6 +39,10 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr mAddParentQ->prepare("INSERT INTO mappings_parents (imapping_id, iparent_id) VALUES(:id, :parentid)"); mDeleteChildQ = new QSqlQuery(mDb); mDeleteChildQ->prepare("DELETE FROM mappings WHERE imapping_id = :id"); + mUpdateParentQ = new QSqlQuery(mDb); + 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"); } MappingTreeModel::~MappingTreeModel(){ @@ -50,6 +56,7 @@ MappingTreeModel::~MappingTreeModel(){ delete mSelectChildQ; delete mAddParentQ; delete mDeleteChildQ; + delete mUpdateParentQ; mDb = QSqlDatabase(); } @@ -103,6 +110,18 @@ QVariant MappingTreeModel::data(const QModelIndex &index, int role) const{ return SmTreeModel::data(index, role); } +QModelIndex MappingTreeModel::indexFromPath(QString &path, int column) const{ + QStringList items = path.split("/"); + if(items.isEmpty() || column >= NumFields){ + return QModelIndex(); + } + QModelIndex retval; + for(int i = 0; i < items.count(); ++i){ + retval = find(items.at(i), column, retval); + } + return retval; +} + bool MappingTreeModel::setData(const QModelIndex &index, const QVariant &value, int role){ if(!index.isValid()){ return false; @@ -138,6 +157,28 @@ bool MappingTreeModel::setData(const QModelIndex &index, const QVariant &value, return true; } +void MappingTreeModel::move(const QModelIndex &source, const QModelIndex &dest){ + SmTreeItem *sItem = itemAt(source); + SmTreeItem *dItem = itemAt(dest); + int sourceId = sItem->data(Id).toInt(); + if(dItem->parent() == root()){ + mDeleteMappingParentQ->bindValue(":id", sourceId); + mDeleteMappingParentQ->exec(); + reparent(source, dest, true); + return; + } + + int newParentId = dItem->data(Id).toInt(); + mAddParentQ->bindValue(":id", sourceId); + mAddParentQ->bindValue(":parentid", newParentId); + if(!mAddParentQ->exec()){ + mUpdateParentQ->bindValue(":pid", newParentId); + mUpdateParentQ->bindValue(":id", sourceId); + mUpdateParentQ->exec(); + } + reparent(source, dest, true); +} + bool MappingTreeModel::addMappingType(const QString &type){ mAddMappingTypeQ->bindValue(":value", type); mDb.transaction(); @@ -244,6 +285,10 @@ void MappingTreeModel::setSelectedMappings(const QList<int> &mappingIds){ emit needExpansion(); } +QStringList MappingTreeModel::paths() const{ + return getPathsRecursive(root()); +} + void MappingTreeModel::populate(){ if(mType == -1){ return; @@ -300,6 +345,35 @@ void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){ } } +QStringList MappingTreeModel::getPathsRecursive(SmTreeItem *parent) const{ + QStringList retval; + if(!basePath(parent).isEmpty()){ + retval << basePath(parent); + } + for(int i = 0; i < parent->childCount(); ++i){ + if(parent->child(i)->childCount()){ + retval << getPathsRecursive(parent->child(i)); + }else{ + retval << QString("%1/%2").arg(basePath(parent)).arg(parent->child(i)->data(Name).toString()); + } + } + return retval; +} + +QString MappingTreeModel::basePath(SmTreeItem *item) const{ + QStringList pItems; + SmTreeItem *cur = item; + while(cur != root()){ + pItems << cur->data(Name).toString(); + cur = cur->parent(); + } + if(pItems.isEmpty()){ + return QString(); + } + std::reverse(pItems.begin(), pItems.end()); + return pItems.join("/"); +} + int MappingTreeModel::lowerBound(SmTreeItem *item, const QVariant &value, int column) const { for(int i = 0; i < item->childCount(); ++i){ SmTreeItem *child = item->child(i); |