summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArno <am@disconnect.de>2012-09-22 07:11:55 +0200
committerArno <am@disconnect.de>2012-09-22 07:11:55 +0200
commitf2b80db9c4dd18e59f8b606e113d9f15cd891c5d (patch)
treeb3eddefe7ae6dddc7821db30892a33f133a50325
parent7cab312d14f79136b8c40507e24523ca5b6dc1e7 (diff)
downloadSheMov-f2b80db9c4dd18e59f8b606e113d9f15cd891c5d.tar.gz
SheMov-f2b80db9c4dd18e59f8b606e113d9f15cd891c5d.tar.bz2
SheMov-f2b80db9c4dd18e59f8b606e113d9f15cd891c5d.zip
MappingTreeModel Fix: repair parent <-> child relationship
Warning: this commit seems to work, but it doesn't! Introduce two new tables: mappings_parents2 and pics_mappings2 to create a real parent-child relationship. The pics_mappings need to reference a unique mappings_parents_id so the same mapping can be a child of different parents. For now only the MappingTree references the new tables. Everything else uses the old ones. Hence the warning!
-rw-r--r--mappingtreemodel.cpp117
-rw-r--r--mappingtreemodel.h14
-rw-r--r--mappingtreewidget.cpp1
-rw-r--r--smglobals.cpp2
4 files changed, 102 insertions, 32 deletions
diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp
index ce96ff4..d63ef63 100644
--- a/mappingtreemodel.cpp
+++ b/mappingtreemodel.cpp
@@ -22,8 +22,7 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr
getMappingTypes();
//prepare Queries
- mTypeParentsQ = new QSqlQuery(mDb);
- mTypeParentsQ->prepare("SELECT imapping_id, tmapping_name, tscreated FROM mappings WHERE imapping_id NOT IN (SELECT imapping_id FROM mappings_parents) AND imapping_type = :type ORDER BY tmapping_name");
+ mSParentsQ = "SELECT mappings_parents2.imappings_parents_id, mappings_parents2.imapping_id, mappings.tmapping_name, mappings.tscreated FROM mappings_parents2, mappings WHERE mappings_parents2.iparent_id = :pid AND mappings.imapping_type = :type AND mappings_parents2.imapping_id = mappings.imapping_id ORDER BY mappings.tmapping_name";
mUpdateTypeQ = new QSqlQuery(mDb);
mUpdateTypeQ->prepare("UPDATE mappings_type SET tmappings_type_name = :value WHERE imappings_type_id = :id");
mUpdateChildQ = new QSqlQuery(mDb);
@@ -46,11 +45,12 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr
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");
+ mMappingsQ = new QSqlQuery(mDb);
+ mMappingsQ->prepare("SELECT tmapping_name, imapping_id FROM mappings WHERE imapping_type = :type");
}
MappingTreeModel::~MappingTreeModel(){
delete mTypesQ;
- delete mTypeParentsQ;
delete mUpdateTypeQ;
delete mUpdateChildQ;
delete mAddMappingTypeQ;
@@ -61,6 +61,7 @@ MappingTreeModel::~MappingTreeModel(){
delete mDeleteChildQ;
delete mUpdateParentQ;
delete mMappingsForFileIdQ;
+ delete mMappingsQ;
mDb = QSqlDatabase();
}
@@ -146,8 +147,10 @@ QVariant MappingTreeModel::data(const QModelIndex &index, int role) const{
QList<QVariant> MappingTreeModel::childList(const QVariant &value, int column) const{
QModelIndex itemIdx = findRecursive(value, column, createIndex(0, 0, root()));
SmTreeItem *item = static_cast<SmTreeItem*>(itemIdx.internalPointer());
- if(item->childCount()){
- return getChildListRecursive(item, column);
+ if(item){
+ if(item->childCount()){
+ return getChildListRecursive(item, column);
+ }
}
return QList<QVariant>() << value;
}
@@ -264,6 +267,13 @@ bool MappingTreeModel::deleteMappingType(int typeId){
return false;
}
+/*
+ * FIXME!
+ * this is total crap. The model has to be redesigned.
+ * Like this children can never have the same name as parents.
+ * Too drunk to think about it now.
+ */
+
bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent){
if(!parent.isValid()){
return false;
@@ -271,8 +281,29 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent)
if(name.toString().contains(mForbidden)){
return false;
}
+ //check if we already have a mapping with this name
+ int id = mMappings.value(name.toString(), -1);
+ if(id == -1){
+ id = addChild(name.toString(), mType);
+ }
+ //now check if we already have a mapping with the same parent
+ mAddParentQ->bindValue(":id", id);
+ mAddParentQ->bindValue(":parentid", parent.data(IdRole));
+ if(!mAddParentQ->exec()){
+ return false;
+ }
+ //we're good now... db makes sure we're unique
SmTreeItem *pItem = itemAt(parent);
- mAddChildQ->bindValue(":name", name);
+ int where = lowerBound(pItem, name, Name);
+ if(insertRows(where, 1, parent)){
+ QModelIndex newIdx = index(where, 0, parent);
+ setData(newIdx, name, NameRole);
+ setData(newIdx, id, IdRole);
+ //setData(newIdx, AddedRole);
+ return true;
+ }
+
+ /*mAddChildQ->bindValue(":name", name);
mAddChildQ->bindValue(":type", mType);
if(mAddChildQ->exec()){
mSelectChildQ->bindValue(":name", name);
@@ -291,11 +322,11 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent)
mAddParentQ->bindValue(":parentid", pItem->data(Id));
mAddParentQ->exec();
}
- mValidMappings.clear();
- mValidMappings = mappingData(root());
+ //mValidMappings.clear();
+ //mValidMappings = mappingData(root());
return true;
}
- }
+ }*/
return false;
}
@@ -345,29 +376,60 @@ void MappingTreeModel::populate(){
return;
}
// get nodes with root as parent
- mTypeParentsQ->bindValue(":type", mType);
- if(mTypeParentsQ->exec()){
+ QSqlQuery rootQ(mDb);
+ rootQ.prepare(mSParentsQ);
+ rootQ.bindValue(":type", mType);
+ rootQ.bindValue(":pid", -1);
+ if(rootQ.exec()){
SmTreeItem *rootItem = new SmTreeItem(NumFields);
- SmTreeItem *firstChild = new SmTreeItem(NumFields, rootItem);
- firstChild->setData(Name, mappingTypeNameFromId(mType));
- //no real id needed... conflicts with mapping ids!
- firstChild->setData(Id, 0);
- rootItem->appendChild(firstChild);
- //collect children recursive
- while(mTypeParentsQ->next()){
+ while(rootQ.next()){
QList<QVariant> childData;
- childData << mTypeParentsQ->value(1) << mTypeParentsQ->value(0) << mTypeParentsQ->value(2);
- SmTreeItem *childItem = new SmTreeItem(childData, firstChild);
- firstChild->appendChild(childItem);
+ childData << rootQ.value(2) << rootQ.value(1) << rootQ.value(3) << rootQ.value(0);
+ SmTreeItem *childItem = new SmTreeItem(childData, rootItem);
+ rootItem->appendChild(childItem);
getChildrenRecursive(childItem);
}
setRoot(rootItem);
- mValidMappings.clear();
- mValidMappings = mappingData(root());
+ //mValidMappings.clear();
+ //mValidMappings = mappingData(root());
emit needExpansion();
}
}
+void MappingTreeModel::setType(int type){
+ mType = type;
+ getMappings();
+}
+
+void MappingTreeModel::getMappings(){
+ mMappingsQ->bindValue(":type", mType);
+ if(mMappingsQ->exec()){
+ mMappings.clear();
+ while(mMappingsQ->next()){
+ mMappings.insert(mMappingsQ->value(0).toString(), mMappingsQ->value(1).toInt());
+ }
+ }
+}
+
+int MappingTreeModel::addChild(const QString &name, int type){
+ mAddChildQ->bindValue(":name", name);
+ mAddChildQ->bindValue(":type", type);
+ if(mAddChildQ->exec()){
+ mSelectChildQ->bindValue(":name", name);
+ mSelectChildQ->bindValue(":type", type);
+ int c = 0;
+ mSelectChildQ->exec();
+ while(mSelectChildQ->next()){
+ ++c;
+ mMappings.insert(mSelectChildQ->value(1).toString(), mSelectChildQ->value(0).toInt());
+ }
+ if(c > 1){
+ qWarning("addChild yielded more than 1 result!");
+ }
+ }
+ return mMappings.value(name);
+}
+
void MappingTreeModel::getMappingTypes(){
bool qRes = mTypesQ->exec();
if(qRes){
@@ -384,12 +446,15 @@ void MappingTreeModel::getMappingTypes(){
void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){
QSqlQuery cq(mDb);
- cq.prepare("SELECT mappings.imapping_id, mappings.tmapping_name, mappings.tscreated FROM mappings, mappings_parents WHERE mappings_parents.iparent_id = :id AND mappings.imapping_id = mappings_parents.imapping_id ORDER BY mappings.tmapping_name");
- cq.bindValue(":id", item->data(Id));
+ cq.prepare(mSParentsQ);
+ cq.bindValue(":type", mType);
+ cq.bindValue(":pid", item->data(Id));
+ //cq.prepare("SELECT mappings.imapping_id, mappings.tmapping_name, mappings.tscreated FROM mappings, mappings_parents WHERE mappings_parents.iparent_id = :id AND mappings.imapping_id = mappings_parents.imapping_id ORDER BY mappings.tmapping_name");
+ //cq.bindValue(":id", item->data(Id));
if(cq.exec()){
while(cq.next()){
QList<QVariant> childData;
- childData << cq.value(1) << cq.value(0) << cq.value(2);
+ childData << cq.value(2) << cq.value(1) << cq.value(3) << cq.value(0);
SmTreeItem *child = new SmTreeItem(childData, item);
item->appendChild(child);
getChildrenRecursive(child);
diff --git a/mappingtreemodel.h b/mappingtreemodel.h
index 396484a..ce64ba6 100644
--- a/mappingtreemodel.h
+++ b/mappingtreemodel.h
@@ -20,9 +20,9 @@ class SmTreeItem;
class MappingTreeModel : public SmTreeModel {
Q_OBJECT
public:
- enum Roles { NameRole = Qt::UserRole + 1, IdRole = Qt::UserRole + 2, AddedRole = Qt::UserRole + 3 };
- enum Fields { Name = 0, Id = 1, Added = 2 };
- enum { NumFields = 3 };
+ enum Roles { NameRole = Qt::UserRole + 1, IdRole = Qt::UserRole + 2, AddedRole = Qt::UserRole + 3, ParentRole = Qt::UserRole + 4 };
+ enum Fields { Name = 0, Id = 1, Added = 2, Parent = 3 };
+ enum { NumFields = 4 };
MappingTreeModel(QStringList &headers, QObject *parent = 0);
~MappingTreeModel();
@@ -54,7 +54,7 @@ class MappingTreeModel : public SmTreeModel {
public slots:
void populate();
- void setType(int type) { mType = type; }
+ void setType(int type); // { mType = type; }
signals:
void mappingTypesChanged();
@@ -65,6 +65,8 @@ class MappingTreeModel : public SmTreeModel {
QVariant id;
QVariant name;
};
+ void getMappings();
+ int addChild(const QString &name, int type);
void getMappingTypes();
void getChildrenRecursive(SmTreeItem *item);
QStringList getPathsRecursive(SmTreeItem *parent) const;
@@ -74,7 +76,7 @@ class MappingTreeModel : public SmTreeModel {
QList<MappingData> mappingData(SmTreeItem *item);
QSqlDatabase mDb;
QSqlQuery *mTypesQ;
- QSqlQuery *mTypeParentsQ;
+ QString mSParentsQ;
QSqlQuery *mUpdateTypeQ;
QSqlQuery *mUpdateChildQ;
QSqlQuery *mAddMappingTypeQ;
@@ -86,9 +88,11 @@ class MappingTreeModel : public SmTreeModel {
QSqlQuery *mUpdateParentQ;
QSqlQuery *mDeleteMappingParentQ;
QSqlQuery *mMappingsForFileIdQ;
+ QSqlQuery *mMappingsQ;
QList<mappingType> mMappingTypes;
QList<MappingData> mValidMappings;
QList<SmTreeItem*> mSelectedMappings;
+ QMap<QString, int> mMappings;
const QString mForbidden;
int mType;
};
diff --git a/mappingtreewidget.cpp b/mappingtreewidget.cpp
index 27d14d3..cd157a8 100644
--- a/mappingtreewidget.cpp
+++ b/mappingtreewidget.cpp
@@ -38,6 +38,7 @@ MappingTreeWidget::MappingTreeWidget(QWidget *parent) : QWidget(parent){
mTree->setModel(mProxy);
mTree->setColumnHidden(1, true);
mTree->setColumnHidden(2, true);
+ mTree->setColumnHidden(3, true);
mTree->setAlternatingRowColors(true);
mTree->expandAll();
connect(mModel, SIGNAL(needExpansion()), mTree, SLOT(expandAll()));
diff --git a/smglobals.cpp b/smglobals.cpp
index b73c89b..93ec72e 100644
--- a/smglobals.cpp
+++ b/smglobals.cpp
@@ -94,7 +94,7 @@ QAbstractItemModel *SmGlobals::model(const QString &which){
}
}else if(which == "MappingTree"){
if(!mModels.contains("MappingTree")){
- QStringList headers = QStringList() << tr("Name") << tr("Id") << tr("Date");
+ QStringList headers = QStringList() << tr("Name") << tr("Id") << tr("Date") << tr("Parent");
MappingTreeModel *model = new MappingTreeModel(headers);
mModels.insert(which, model);
}