Fixed a bug: the edit dialog fields were not cleared if an
[emufront] / src / dialogs / dbobjectdialog.cpp
index a1a3c37..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,25 +45,31 @@ 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()
@@ -73,14 +79,23 @@ void DbObjectDialog::testSlot()
 
 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
 {
-    if ( dbManager->insertDataObjectToModel(ob) )
-        qDebug() << "Db insert ok";
-    else qDebug() << "Db insert failed";
+    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);
@@ -92,20 +107,30 @@ 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);
+        EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
+
+        qDebug() << "Trying to delete " << ob->getName();
 
         if (!ob)
         {
@@ -119,7 +144,7 @@ bool DbObjectDialog::deleteItem()
         { return false; }
         deleteCurrentObject();
 
-        bool delOk = dbManager->deleteDataObjectFromModel(&index);
+        bool delOk = dbManager->deleteDataObject(ob->getId());
         if (!delOk)
         {
             errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
@@ -127,6 +152,7 @@ bool DbObjectDialog::deleteItem()
         }
         updateList();
         objectList->setFocus();
+        setUIEnabled(true);
     }
     catch(EmuFrontException e)
     {
@@ -146,6 +172,7 @@ void DbObjectDialog::updateList() const
 {
     if (!dbManager) return;
     dbManager->resetModel();
+    objectList->resizeColumnsToContents();
 }
 
 void DbObjectDialog::addButtonClicked()
@@ -197,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();
 }
 
@@ -225,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);
@@ -250,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)
 {
@@ -272,3 +310,17 @@ 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();
+}