summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArno <am@disconnect.de>2012-10-06 11:02:42 +0200
committerArno <am@disconnect.de>2012-10-06 11:02:42 +0200
commite2d4843586d84da62e325d1cb0025a795b162c1c (patch)
tree9dd94bf487c4f1d55845854706387512d7279ea2
parentc7caac3459461f93fbae544a501fa491f84e5ce4 (diff)
downloadSheMov-e2d4843586d84da62e325d1cb0025a795b162c1c.tar.gz
SheMov-e2d4843586d84da62e325d1cb0025a795b162c1c.tar.bz2
SheMov-e2d4843586d84da62e325d1cb0025a795b162c1c.zip
More fixes to MappingTreeModel
Fix move, addChild and such. Rename Fields and Roles to more speaking names, but that revealed a much deeper bug: the ParentID isn't really the parent_id, but the mapping_id. That could explain a lot. Nevertheless, it's still faster to repopulate the model after moving oder adding children instead of calling removeRows and insertRows. Another non-working commit... :(
-rw-r--r--mappingtreemodel.cpp98
-rw-r--r--mappingtreemodel.h6
-rw-r--r--mappingtreewidget.cpp2
-rw-r--r--picfilesmodel.cpp6
-rw-r--r--smtreemodel.cpp18
5 files changed, 73 insertions, 57 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);
diff --git a/mappingtreemodel.h b/mappingtreemodel.h
index b2fa9eb..beb7d7f 100644
--- a/mappingtreemodel.h
+++ b/mappingtreemodel.h
@@ -21,8 +21,8 @@ class SmTreeItem;
class MappingTreeModel : public SmTreeModel {
Q_OBJECT
public:
- enum Roles { NameRole = Qt::UserRole + 1, IdRole = Qt::UserRole + 2, AddedRole = Qt::UserRole + 3, MapParentIdRole = Qt::UserRole + 4 };
- enum Fields { Name = 0, Id = 1, Added = 2, MapParentId = 3 };
+ enum Roles { NameRole = Qt::UserRole + 1, ParentIdRole = Qt::UserRole + 2, AddedRole = Qt::UserRole + 3, MyIdRole = Qt::UserRole + 4 };
+ enum Fields { Name = 0, ParentId = 1, Added = 2, MyId = 3 };
enum { NumFields = 4 };
MappingTreeModel(QStringList &headers, QObject *parent = 0);
~MappingTreeModel();
@@ -41,7 +41,7 @@ class MappingTreeModel : public SmTreeModel {
QStringList path(QModelIndex &idx) const;
QModelIndex indexFromPath(const QString &path, int column = 0) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
- void move(const QModelIndex &source, const QModelIndex &dest);
+ bool move(const QModelIndex &source, const QModelIndex &dest);
bool addMappingType(const QString &type);
bool deleteMappingType(int typeId);
bool addChild(const QVariant &name, const QModelIndex &parent);
diff --git a/mappingtreewidget.cpp b/mappingtreewidget.cpp
index 1897cf1..0951312 100644
--- a/mappingtreewidget.cpp
+++ b/mappingtreewidget.cpp
@@ -221,7 +221,7 @@ void MappingTreeWidget::editChild(){
void MappingTreeWidget::selectionChanged(){
QModelIndex sel = selected();
QModelIndex real = mProxy->mapToSource(sel);
- emit mappingChanged(real.data(MappingTreeModel::MapParentIdRole).toInt());
+ emit mappingChanged(real.data(MappingTreeModel::MyIdRole).toInt());
}
void MappingTreeWidget::moveChild(){
diff --git a/picfilesmodel.cpp b/picfilesmodel.cpp
index 651ce32..cab7235 100644
--- a/picfilesmodel.cpp
+++ b/picfilesmodel.cpp
@@ -40,7 +40,7 @@ PicFilesModel::~PicFilesModel(){
}
void PicFilesModel::setMapping(int pMapId){
- QList<QVariant> ids = mMappingTreeModel->childList(pMapId, MappingTreeModel::MapParentId);
+ QList<QVariant> ids = mMappingTreeModel->childList(pMapId, MappingTreeModel::MyId);
mMappingIds.clear();
foreach(QVariant i, ids){
mMappingIds << i.toInt();
@@ -170,9 +170,9 @@ QList<MappingData> PicFilesModel::mappingDataFromFiles(const QList<int> fileIds)
}
QList<MappingData> retval;
foreach(int pId, parentIds){
- QModelIndex curIdx = mMappingTreeModel->findRecursive(pId, MappingTreeModel::MapParentId, mMappingTreeModel->rootIndex());
+ QModelIndex curIdx = mMappingTreeModel->findRecursive(pId, MappingTreeModel::MyId, mMappingTreeModel->rootIndex());
MappingData curData;
- curData.id = curIdx.data(MappingTreeModel::MapParentIdRole).toInt();
+ curData.id = curIdx.data(MappingTreeModel::MyIdRole).toInt();
curData.name = curIdx.data(MappingTreeModel::NameRole).toString();
curData.path << mMappingTreeModel->path(curIdx); //.join("/");
retval << curData;
diff --git a/smtreemodel.cpp b/smtreemodel.cpp
index 4596a5b..71c815e 100644
--- a/smtreemodel.cpp
+++ b/smtreemodel.cpp
@@ -143,7 +143,7 @@ QModelIndex SmTreeModel::find(const QVariant &value, int column, const QModelInd
for(int i = 0; i < parentItem->childCount(); ++i){
SmTreeItem *child = parentItem->child(i);
if(child->data(column) == value){
- return index(i, column, parent);
+ return index(i, column, parent);
}
}
return QModelIndex();
@@ -200,11 +200,11 @@ void SmTreeModel::reparent(const QModelIndex &idx, const QModelIndex &newParent,
if(!idx.isValid()){
return;
}
- SmTreeItem *item = static_cast<SmTreeItem*>(idx.internalPointer());
- QList<QVariant> data;
- for(int i = 0; i < item->columnCount(); ++i){
- data << item->data(i);
- }
+ SmTreeItem *item = static_cast<SmTreeItem*>(idx.internalPointer());
+ QList<QVariant> data;
+ for(int i = 0; i < item->columnCount(); ++i){
+ data << item->data(i);
+ }
QPersistentModelIndex pNewParent = newParent;
removeRows(idx.row(), 1, idx.parent());
addRow(data, pNewParent, sorted);
@@ -218,7 +218,7 @@ bool SmTreeModel::insertRows(int row, int count, const QModelIndex &parent){
return false;
}
- beginInsertRows(parent, row, row + count - 1);
+ beginInsertRows(parent, row, row + count - 1);
for(int i = row; i < row + count; ++i){
SmTreeItem *newItem = new SmTreeItem(mRootItem->columnCount(), parentItem);
retval = parentItem->insertChild(i, newItem);
@@ -264,8 +264,8 @@ bool SmTreeModel::addRow(const QList<QVariant> &data, const QModelIndex &parent,
for(int i = 0; i < data.count(); ++i){
child->setData(i, data.at(i));
}
- QModelIndex start = index(parentItem->childCount() - 1, 0, parent);
- QModelIndex end = index(parentItem->childCount() - 1, parentItem->columnCount() - 1, parent);
+ QModelIndex start = index(parentItem->childCount() - 1, 0, parent);
+ QModelIndex end = index(parentItem->childCount() - 1, parentItem->columnCount() - 1, parent);
emit dataChanged(start, end);
return true;
}