summaryrefslogtreecommitdiffstats
path: root/mappingtreemodel.cpp
diff options
context:
space:
mode:
authorArno <am@disconnect.de>2012-03-02 20:08:40 +0100
committerArno <am@disconnect.de>2012-03-02 20:08:40 +0100
commitee29bb41dc9c4d4dd6fc9bfd3fb9ad5cc3bd1569 (patch)
tree4801c4b23215259cec9620a32593c656fdfd8ede /mappingtreemodel.cpp
parent11bf52b6cf1c27a75715a8379e7893b8d1e16bf0 (diff)
downloadSheMov-ee29bb41dc9c4d4dd6fc9bfd3fb9ad5cc3bd1569.tar.gz
SheMov-ee29bb41dc9c4d4dd6fc9bfd3fb9ad5cc3bd1569.tar.bz2
SheMov-ee29bb41dc9c4d4dd6fc9bfd3fb9ad5cc3bd1569.zip
Make it possible to move mappings
Move mappings by context menu. Select new parent by QComboBox with available paths, items separated by "/". Note that hell will break loose if a mapping name contains "/". Will be fixed later. Since mapping views don't have setSortingEnabled(), make SmTreeModel::addRow() sort items. This fixes a long standing bug in SmTreeModel::reparent(): Since it alters the model, newParent has to be a QPersistentModelIndex to stay consistent.
Diffstat (limited to 'mappingtreemodel.cpp')
-rw-r--r--mappingtreemodel.cpp74
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);