Fixed a bug: the edit dialog fields were not cleared if an
[emufront] / src / dialogs / dbobjectdialog.cpp
index 03085d3..46e0ca1 100644 (file)
@@ -5,17 +5,17 @@
 //
 //
 // EmuFront is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
 //
-// Foobar is distributed in the hope that it will be useful,
+// EmuFront is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 // GNU General Public License for more details.
 //
 // You should have received a copy of the GNU General Public License
-// along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <QtGui>
 #include <QSqlTableModel>
@@ -45,27 +45,33 @@ DbObjectDialog::DbObjectDialog(QWidget *parent)
 
 DbObjectDialog::~DbObjectDialog()
 {
-    // no need to explicitically delete widgets within a parented layout
+    // no need to explicitly delete widgets within a parented layout
     // they are automatically parented and will be deleted
-    // dbManager is also parented and will be implicitically deleted
+    // dbManager is also parented and will be implicitly deleted
     // this must be deleted in an implementing class
     //delete dbObject;
+    // hiddenColumn QList will be deleted implicitly, since it
+    // implements implicit data sharing
 }
 
 void DbObjectDialog::connectSignals()
 {
-    qDebug() << "DbObjectDialog connecting signals";
     connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
         this, SLOT(listObjectClicked(const QModelIndex &)));
     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
+}
+
+void DbObjectDialog::connectNameDialogSignals()
+{
     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
     connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
     connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
+    connect(nameDialog, SIGNAL(dialogClosed()), this, SLOT(enableUi()));
 }
-
+    
 void DbObjectDialog::testSlot()
 {
     qDebug() << "TEST SIGNAL RECEIVED!";
@@ -73,12 +79,23 @@ void DbObjectDialog::testSlot()
 
 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
 {
-    dbManager->insertDataObjectToModel(ob);
+    if (!dbManager->insertDataObjectToModel(ob))
+        errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
+}
+
+void DbObjectDialog::createEditDialog()
+{
+    initEditDialog();
+    // call this from implementing classes:
+    //connectNameDialogSignals();
 }
 
 void DbObjectDialog::addObject()
 {
-    if (!nameDialog) initEditDialog();
+    setUIEnabled(false);
+    if (!nameDialog) {
+       createEditDialog();
+    }
     deleteCurrentObject();
     dbObject = createObject();
     nameDialog->setDataObject(dbObject);
@@ -90,50 +107,72 @@ void DbObjectDialog::editObject()
     QModelIndex index = objectList->currentIndex();
     if (!index.isValid())
         return;
-    if (!nameDialog) initEditDialog();
+    if (!nameDialog) {
+        createEditDialog();
+    }
     deleteCurrentObject();
-    dbObject = dbManager->getDataObjectFromModel(&index);
-    nameDialog->setDataObject(dbObject);
+    try {
+        dbObject = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
+    } catch (EmuFrontException &e) {
+        errorMessage->showMessage(e.what());
+        return;
+    }
     activateNameDialog();
+    nameDialog->setDataObject(dbObject);
 }
 
 bool DbObjectDialog::deleteItem()
 {
+    qDebug() << "deleteItem called";
     QModelIndex index = objectList->currentIndex();
     if (!index.isValid()) return false;
+    try
+    {
+        EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
 
-    EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
-    if (!ob) return false;
+        qDebug() << "Trying to delete " << ob->getName();
 
-    int numBindings = dbManager->countDataObjectRefs(ob->getId());
-    if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
-    {
+        if (!ob)
+        {
+            errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
+            return false;
+        }
+
+        int numBindings = dbManager->countDataObjectRefs(ob->getId());
+
+        if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
+        { return false; }
+        deleteCurrentObject();
+
+        bool delOk = dbManager->deleteDataObject(ob->getId());
+        if (!delOk)
+        {
+            errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
             return false;
+        }
+        updateList();
+        objectList->setFocus();
+        setUIEnabled(true);
     }
-    deleteCurrentObject();
-    bool delOk = dbManager->deleteDataObjectFromModel(&index);
-    if (!delOk)
+    catch(EmuFrontException e)
     {
-        qDebug() << "delete failed";
-        return false;
+        errorMessage->showMessage(e.what());
     }
-    updateList();
-    objectList->setFocus();
     return false;
 }
 
 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
 {
     if (!ob) return;
-    qDebug() << "Updating platform " << ob->getName();
-    dbManager->updateDataObjectToModel(ob);
+    if ( !dbManager->updateDataObjectToModel(ob) )
+    { errorMessage->showMessage("Database update failed!"); }
 }
 
 void DbObjectDialog::updateList() const
 {
     if (!dbManager) return;
-    qDebug() << "Going to reset the data model";
     dbManager->resetModel();
+    objectList->resizeColumnsToContents();
 }
 
 void DbObjectDialog::addButtonClicked()
@@ -163,7 +202,6 @@ void DbObjectDialog::deleteButtonClicked()
     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
     if (yn == QMessageBox::Yes)
     {
-        qDebug() << "Deleting item..." << name << ".";
         deleteItem();
     }
 }
@@ -186,22 +224,37 @@ void DbObjectDialog::listObjectClicked(const QModelIndex &index)
        return;
 }
 
+void DbObjectDialog::enableUi()
+{
+    setUIEnabled(true);
+}
+
 void DbObjectDialog::setButtonsEnabled(bool enabled)
 {
+    addButton->setEnabled(enabled);
     editButton->setEnabled(enabled);
     deleteButton->setEnabled(enabled);
 }
 
+void DbObjectDialog::setUIEnabled(bool enabled)
+{
+    buttonBox->setEnabled(enabled);
+    objectList->setEnabled(enabled);
+}
+
 void DbObjectDialog::disableSelection()
 {
-    setButtonsEnabled(false);
+    setUIEnabled(false);
+    //setButtonsEnabled(false);
 }
 
-void DbObjectDialog::activateNameDialog()
+void DbObjectDialog::activateNameDialog(bool updateData)
 {
     if (!nameDialog) return;
     nameDialog->show();
     nameDialog->raise();
+    if (updateData)
+        nameDialog->updateData();
     nameDialog->activateWindow();
 }
 
@@ -214,23 +267,17 @@ void DbObjectDialog::initDataTable()
 
 void DbObjectDialog::updateReject()
 {
-    qDebug() << "Update rejected ... going to delete current object.";
+    addButton->setEnabled(true);
+    setUIEnabled(true);
     // we don't want to keep this in memory
     deleteCurrentObject();
 }
 
 void DbObjectDialog::updateData()
 {
-    qDebug() << "Update accepted.";
     // update data model
     if (!dbObject) return;
 
-    qDebug() << "dbObject is not 0";
-
-    qDebug() << "We have a " + dbObject->getName();
-
-    qDebug() << "Data will be inserted/updated...";
-
     // if data object id > -1 we are updating the data otherwise we are inserting new data
     if (dbObject->getId() > -1) updateDb(dbObject);
     else insertDb(dbObject);
@@ -239,12 +286,14 @@ void DbObjectDialog::updateData()
     deleteCurrentObject();
     dbObject = 0;
     updateList();
+    setUIEnabled(true);
 }
 
+/* Implementation specific delete must be used!
 void DbObjectDialog::deleteCurrentObject()
 {
     delete dbObject;
-}
+}*/
 
 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
 {
@@ -256,3 +305,22 @@ bool DbObjectDialog::confirmDelete(QString name, int numRefs)
         return false;
     return true;
 }
+
+void DbObjectDialog::refreshDataModel()
+{
+    dbManager->resetModel();
+}
+
+void DbObjectDialog::hideColumns()
+{
+    foreach(int c, hiddenColumns)
+        objectList->hideColumn(c);
+}
+
+/* Enables UI. Deletes nameDialog object and current data object */
+void DbObjectDialog::closeEvent(QCloseEvent *ev)
+{
+    qDebug() << "DbObjectDialog closing!";
+    setUIEnabled(true);
+    cleanUp();
+}