summaryrefslogtreecommitdiffstats
path: root/mappingtreemodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'mappingtreemodel.cpp')
-rw-r--r--mappingtreemodel.cpp98
1 files changed, 57 insertions, 41 deletions
diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp
index 48a4c67..50586e8 100644
--- a/mappingtreemodel.cpp
+++ b/mappingtreemodel.cpp
@@ -38,7 +38,8 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr
mAddParentQ = new QSqlQuery(mDb);
mAddParentQ->prepare("INSERT INTO mappings_parents2 (imapping_id, iparent_id) VALUES(:id, :parentid)");
mUpdateParentQ = new QSqlQuery(mDb);
- mUpdateParentQ->prepare("UPDATE mappings_parents SET iparent_id = :pid WHERE imapping_id = :id");
+ //mUpdateParentQ->prepare("UPDATE mappings_parents SET iparent_id = :pid WHERE imapping_id = :id");
+ mUpdateParentQ->prepare("UPDATE mappings_parents2 SET iparent_id = :id where imappings_parents_id = :sid");
mDeleteMappingParentQ = new QSqlQuery(mDb);
mDeleteMappingParentQ->prepare("DELETE FROM mappings_parents2 WHERE imappings_parents_id = :id");
mMappingsQ = new QSqlQuery(mDb);
@@ -116,14 +117,14 @@ QVariant MappingTreeModel::data(const QModelIndex &index, int role) const{
if(role == NameRole){
return item->data(Name);
}
- if(role == IdRole){
- return item->data(Id);
+ if(role == ParentIdRole){
+ return item->data(ParentId);
}
if(role == AddedRole){
return item->data(Added);
}
- if(role == MapParentIdRole){
- return item->data(MapParentId);
+ if(role == MyIdRole){
+ return item->data(MyId);
}
return SmTreeModel::data(index, role);
}
@@ -157,6 +158,9 @@ QStringList MappingTreeModel::path(QModelIndex &idx) const{
}
QModelIndex MappingTreeModel::indexFromPath(const QString &path, int column) const{
+ if(path == "/"){
+ return rootIndex();
+ }
QStringList items = path.split("/");
if(items.isEmpty() || column >= NumFields){
return QModelIndex();
@@ -187,7 +191,7 @@ bool MappingTreeModel::setData(const QModelIndex &index, const QVariant &value,
q = mUpdateChildQ;
}
q->bindValue(":value", value);
- q->bindValue(":id", item->data(Id));
+ q->bindValue(":id", item->data(ParentId));
if(q->exec()){
item->setData(Name, value);
emit dataChanged(index, index);
@@ -202,38 +206,35 @@ bool MappingTreeModel::setData(const QModelIndex &index, const QVariant &value,
}
item->setData(Name, value);
}
- if(role == IdRole){
- item->setData(Id, value);
+ if(role == ParentIdRole){
+ item->setData(ParentId, value);
}
if(role == AddedRole){
item->setData(Added, value);
}
- if(role == MapParentIdRole){
- item->setData(MapParentId, value);
+ if(role == MyIdRole){
+ item->setData(MyId, 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();
+bool MappingTreeModel::move(const QModelIndex &source, const QModelIndex &dest){
+ QVariant sourceId = source.data(MyIdRole);
+ QVariant destId = dest.data(ParentIdRole);
+ mDb.transaction();
+ mUpdateParentQ->bindValue(":id", destId);
+ mUpdateParentQ->bindValue(":sid", sourceId);
+ if(mUpdateParentQ->exec()){
+ // well, well, I tried to make this work with removeRows and insertRows,
+ // but qt has a mind of its own there. So take the easy way out. Also,
+ // it's way faster!
+ mDb.commit();
+ populate();
+ return true;
}
- reparent(source, dest, true);
+ mLastError = mUpdateParentQ->lastError();
+ mDb.rollback();
+ return false;
}
bool MappingTreeModel::addMappingType(const QString &type){
@@ -276,13 +277,16 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent)
mDb.transaction();
mAddParentQ->bindValue(":id", id);
// seems we're a child of root
- QVariant ppId = parent.data(IdRole).isValid() ? parent.data(IdRole) : -1;
- mAddParentQ->bindValue(":parentid", ppId);
- if(!mAddParentQ->exec()){
- mLastError = mAddParentQ->lastError();
- mDb.rollback();
- return false;
+ //QVariant ppId = parent.data(ParentIdRole).isValid() ? parent.data(ParentIdRole) : -1;
+ mAddParentQ->bindValue(":parentid", parent.data(ParentIdRole));
+ if(mAddParentQ->exec()){
+ mDb.commit();
+ populate();
+ return true;
}
+ mLastError = mAddParentQ->lastError();
+ mDb.rollback();
+ return false;
//we're good now... db makes sure we're unique
SmTreeItem *pItem = itemAt(parent);
int where = lowerBound(pItem, name, Name);
@@ -294,8 +298,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, parentId, MapParentIdRole);
+ setData(newIdx, id, ParentIdRole);
+ setData(newIdx, parentId, MyIdRole);
mDb.commit();
return true;
}
@@ -310,7 +314,7 @@ bool MappingTreeModel::deleteChild(const QModelIndex &idx){
if(item->childCount() > 0){
return false;
}
- mDeleteMappingParentQ->bindValue(":id", item->data(MapParentId));
+ mDeleteMappingParentQ->bindValue(":id", item->data(MyId));
if(mDeleteMappingParentQ->exec()){
removeRows(idx.row(), 1, idx.parent());
return true;
@@ -324,14 +328,16 @@ MappingData MappingTreeModel::mappingDataFromIndex(QModelIndex &idx) const{
if(!idx.isValid()){
return retval;
}
- retval.id = idx.data(MappingTreeModel::MapParentIdRole).toInt();
+ retval.id = idx.data(MappingTreeModel::MyIdRole).toInt();
retval.name = idx.data(MappingTreeModel::NameRole).toString();
retval.path << path(idx);
return retval;
}
QStringList MappingTreeModel::paths() const{
- return getPathsRecursive(root());
+ QStringList retval = QStringList() << "/";
+ retval << getPathsRecursive(root());
+ return retval;
}
void MappingTreeModel::populate(){
@@ -345,6 +351,8 @@ void MappingTreeModel::populate(){
rootQ.bindValue(":pid", -1);
if(rootQ.exec()){
SmTreeItem *rootItem = new SmTreeItem(NumFields);
+ rootItem->setData(MyId, -1);
+ rootItem->setData(ParentId, -1);
while(rootQ.next()){
QList<QVariant> childData;
childData << rootQ.value(2) << rootQ.value(1) << rootQ.value(3) << rootQ.value(0);
@@ -405,15 +413,23 @@ void MappingTreeModel::getMappingTypes(){
}
}
+#include <QDebug>
+
void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){
+ static QList<QVariant> seenIds;
QSqlQuery cq(mDb);
cq.prepare(mSParentsQ);
cq.bindValue(":type", mType);
- cq.bindValue(":pid", item->data(Id));
+ cq.bindValue(":pid", item->data(ParentId));
if(cq.exec()){
while(cq.next()){
QList<QVariant> childData;
childData << cq.value(2) << cq.value(1) << cq.value(3) << cq.value(0);
+ if(seenIds.contains(item->data(MyId))){
+ qDebug() << "already seen" << childData.value(0);
+ continue;
+ }
+ seenIds << item->data(MyId);
SmTreeItem *child = new SmTreeItem(childData, item);
item->appendChild(child);
getChildrenRecursive(child);