summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArno <am@disconnect.de>2012-10-05 12:18:14 +0200
committerArno <am@disconnect.de>2012-10-05 12:18:14 +0200
commita682fc799b99653b67f5bbd8f1bed371bcbe48f1 (patch)
tree32d79631b279296f28f31fe1dc1c36b03962c79c
parent4d2cc1f62de2e097600212f57ef17d222931bbe5 (diff)
downloadSheMov-a682fc799b99653b67f5bbd8f1bed371bcbe48f1.tar.gz
SheMov-a682fc799b99653b67f5bbd8f1bed371bcbe48f1.tar.bz2
SheMov-a682fc799b99653b67f5bbd8f1bed371bcbe48f1.zip
Fixed adding and deleting children from MappingTreeModel
Another fix to MappingTreeModel's new database layout. I think we're getting there... Insert the mappings into mapping_parents2 and add the MapParentId to the newly created index in the model. For now, the added date remains invalid. Make it possible (again?) to add root items to MappingTreeModel. For this I had to design a new QDialog with a checkbox. This one fixes another bug in SmTreeModel: Don't call parent() on a null pointer. Sometimes I'm getting random SIGBUS-Signals, but maybe that's because of the debug build of qt I'm using. Couldn't track it down yet...
-rw-r--r--mappingtreemodel.cpp16
-rw-r--r--mappingtreewidget.cpp59
-rw-r--r--mappingtreewidget.h16
-rw-r--r--smtreeitem.cpp6
-rw-r--r--smtreemodel.cpp5
5 files changed, 92 insertions, 10 deletions
diff --git a/mappingtreemodel.cpp b/mappingtreemodel.cpp
index 1babac1..eed686b 100644
--- a/mappingtreemodel.cpp
+++ b/mappingtreemodel.cpp
@@ -38,7 +38,7 @@ MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTr
mSelectChildQ = new QSqlQuery(mDb);
mSelectChildQ->prepare("SELECT imapping_id, tmapping_name, tscreated FROM mappings WHERE tmapping_name = :name AND imapping_type = :type");
mAddParentQ = new QSqlQuery(mDb);
- mAddParentQ->prepare("INSERT INTO mappings_parents (imapping_id, iparent_id) VALUES(:id, :parentid)");
+ mAddParentQ->prepare("INSERT INTO mappings_parents2 (imapping_id, iparent_id) VALUES(:id, :parentid)");
mDeleteChildQ = new QSqlQuery(mDb);
mDeleteChildQ->prepare("DELETE FROM mappings WHERE imapping_id = :id");
mUpdateParentQ = new QSqlQuery(mDb);
@@ -301,18 +301,30 @@ bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent)
id = addChild(name.toString(), mType);
}
//now check if we already have a mapping with the same parent
+ mDb.transaction();
mAddParentQ->bindValue(":id", id);
- mAddParentQ->bindValue(":parentid", parent.data(IdRole));
+ // 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;
}
//we're good now... db makes sure we're unique
SmTreeItem *pItem = itemAt(parent);
int where = lowerBound(pItem, name, Name);
if(insertRows(where, 1, parent)){
+ QSqlQuery pIdQuery = QSqlQuery("SELECT currval('mappings_parents_id__seq')", mDb);
+ int parentId = -1;
+ while(pIdQuery.next()){
+ parentId = pIdQuery.value(0).toInt();
+ }
QModelIndex newIdx = index(where, 0, parent);
setData(newIdx, name, NameRole);
setData(newIdx, id, IdRole);
+ setData(newIdx, parentId, MapParentIdRole);
+ mDb.commit();
return true;
}
return false;
diff --git a/mappingtreewidget.cpp b/mappingtreewidget.cpp
index 7e62ba3..1897cf1 100644
--- a/mappingtreewidget.cpp
+++ b/mappingtreewidget.cpp
@@ -12,6 +12,8 @@
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QInputDialog>
+#include <QCheckBox>
+#include <QLabel>
#include <QStringListModel>
#include <QMessageBox>
#include <QSettings>
@@ -103,8 +105,12 @@ MappingData MappingTreeWidget::selectedItem() const {
}
void MappingTreeWidget::addChild(){
- QModelIndex sel = selected();
- QString value = QInputDialog::getText(this, tr("Mapping name"), tr("Enter mapping name"));
+ MappingInputDialog dlg(this);
+ int retval = dlg.exec();
+ if(retval != QDialog::Accepted){
+ return;
+ }
+ QString value = dlg.mappingName();
if(value.isEmpty()){
return;
}
@@ -113,8 +119,16 @@ void MappingTreeWidget::addChild(){
QMessageBox::critical(this, tr("Error"), msg);
return;
}
- QModelIndex real = mProxy->mapToSource(sel);
- mModel->addChild(value, real);
+ QModelIndex sel = selected();
+ QModelIndex parent = mModel->rootIndex();
+ if(!dlg.createRoot()){
+ QModelIndex sel = selected();
+ parent = mProxy->mapToSource(sel);
+ }
+ if(!mModel->addChild(value, parent)){
+ QString err = QString(tr("Error: Database said: %1")).arg(mModel->lastError().text());
+ QMessageBox::critical(this, tr("Error"), err);
+ }
}
void MappingTreeWidget::addType(){
@@ -311,6 +325,43 @@ void MappingEditWidget::setMappings(const QList<MappingData> &mappings){
mMappingResult->expandAll();
}
+MappingInputDialog::MappingInputDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
+ mOk = new QPushButton(tr("Ok"));
+ connect(mOk, SIGNAL(clicked()), this, SLOT(accept()));
+ mCancel = new QPushButton(tr("Cancel"));
+ connect(mCancel, SIGNAL(clicked()), this, SLOT(reject()));
+ mIsRoot = new QCheckBox(tr("Create root item"));
+ mEditor = new QLineEdit;
+
+ QHBoxLayout *buttonLayout = new QHBoxLayout;
+ buttonLayout->addStretch();
+ buttonLayout->addWidget(mOk);
+ buttonLayout->addWidget(mCancel);
+
+ QVBoxLayout* inputLayout = new QVBoxLayout;
+ QLabel *caption = new QLabel(tr("Enter mapping name"));
+ inputLayout->addWidget(caption);
+ inputLayout->addWidget(mEditor);
+
+ QHBoxLayout *cbLayout = new QHBoxLayout;
+ cbLayout->addStretch();
+ cbLayout->addWidget(mIsRoot);
+
+ QVBoxLayout *mainLayout = new QVBoxLayout;
+ mainLayout->addLayout(inputLayout);
+ mainLayout->addLayout(cbLayout);
+ mainLayout->addLayout(buttonLayout);
+ setLayout(mainLayout);
+}
+
+QString MappingInputDialog::mappingName() const{
+ return mEditor->text();
+}
+
+bool MappingInputDialog::createRoot() const {
+ return mIsRoot->checkState() == Qt::Checked;
+}
+
MappingEditDialog::MappingEditDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
mEditWidget = new MappingEditWidget;
mOk = new QPushButton(tr("Ok"));
diff --git a/mappingtreewidget.h b/mappingtreewidget.h
index 60bbecc..600c6d5 100644
--- a/mappingtreewidget.h
+++ b/mappingtreewidget.h
@@ -17,6 +17,7 @@ class MappingTreeModel;
class QComboBox;
class QPushButton;
class QLabel;
+class QCheckBox;
class QSortFilterProxyModel;
class QStringListModel;
class QAction;
@@ -98,6 +99,21 @@ class MappingEditWidget : public QWidget {
QPushButton *mRemoveMapping;
};
+class MappingInputDialog : public QDialog {
+ Q_OBJECT
+ public:
+ explicit MappingInputDialog(QWidget *parent, Qt::WindowFlags f = 0);
+ QString mappingName() const;
+ bool createRoot() const;
+
+ private:
+ QPushButton *mOk;
+ QPushButton *mCancel;
+ QLineEdit *mEditor;
+ QCheckBox *mIsRoot;
+
+};
+
class MappingEditDialog : public QDialog {
Q_OBJECT
public:
diff --git a/smtreeitem.cpp b/smtreeitem.cpp
index ab5e636..77a75d5 100644
--- a/smtreeitem.cpp
+++ b/smtreeitem.cpp
@@ -44,12 +44,12 @@ SmTreeItem *SmTreeItem::child(int row) const{
SmTreeItem *SmTreeItem::next() const{
int pos = row();
- if(pos + 1 < parent()->childCount()){
- return parent()->child(pos + 1);
- }
if(parent() == 0){
return 0;
}
+ if(pos + 1 < parent()->childCount()){
+ return parent()->child(pos + 1);
+ }
return parent()->next();
}
diff --git a/smtreemodel.cpp b/smtreemodel.cpp
index 088d393..4596a5b 100644
--- a/smtreemodel.cpp
+++ b/smtreemodel.cpp
@@ -62,8 +62,11 @@ QModelIndex SmTreeModel::parent(const QModelIndex &child) const{
}
SmTreeItem *childItem = static_cast<SmTreeItem*>(child.internalPointer());
- SmTreeItem *parentItem = childItem->parent();
+ if(childItem == mRootItem){
+ return QModelIndex();
+ }
+ SmTreeItem *parentItem = childItem->parent();
if(parentItem == mRootItem){
return QModelIndex();
}