Fixed a bug: the edit dialog fields were not cleared if an
[emufront] / src / dialogs / namedialog.cpp
index 8cb5cd2..0bbffbf 100644 (file)
@@ -1,44 +1,52 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// 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.
+//
+// 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 EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
 #include <QtGui>
 #include "namedialog.h"
 
 NameDialog::NameDialog(QWidget *parent, EmuFrontObject *efObj)
-        : EmuFrontDialog(parent), efObject(efObj)
+        : DataObjectEditDialog(parent, efObj)
 {
        nameLabel = new QLabel(tr("&Name: "));  
        nameEdit = new QLineEdit;
        nameLabel->setBuddy(nameEdit);
-    buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Abort, Qt::Horizontal);
-    /*saveButton = new QPushButton(tr("&Save"));
-       saveButton->setDefault(true);
-       saveButton->setEnabled(false);
-    closeButton = new QPushButton(tr("Close"));*/
     connectSignals();
        layout();
+    emit test();
        setWindowTitle(tr("Set names"));
 }
 
 NameDialog::~NameDialog()
 {
-    delete efObject;
+    // should be deleted in implementing classes
+    // delete efObject;
 
-       /* deleting objects in heap is not needed here
+    /* no need to delete parented QT-objects in heap here
         * because when deleting a parent widget
-        * the child widgets will be also deleted:
-        * delete nameLabel;
-        * delete nameEdit;
-        * delete saveButton;
-        * delete closeButton;
+     * the child widgets will be also deleted
         */
 }
 
 void NameDialog::connectSignals()
 {
+    DataObjectEditDialog::connectSignals();
     connect(nameEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableSaveButton(const QString &)));
-    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptChanges()));
-    connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
-
-    /*connect(saveButton, SIGNAL(clicked()), this, SLOT(saveButtonClicked()));
-    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));*/
 }
 
 void NameDialog::layout()
@@ -49,9 +57,6 @@ void NameDialog::layout()
 
        QHBoxLayout *bottomLayout = new QHBoxLayout;
     bottomLayout->addWidget(buttonBox);
-    /*bottomLayout->addStretch();
-       bottomLayout->addWidget(saveButton);
-    bottomLayout->addWidget(closeButton);*/
 
        QVBoxLayout *mainLayout = new QVBoxLayout;
        mainLayout->addLayout(topLayout);
@@ -68,14 +73,32 @@ void NameDialog::acceptChanges()
     }
 
        QString name = nameEdit->text().simplified();
-    setDataObject(name);
-    emit dataObjectUpdated();
+    qDebug() << "We have a name " << name << ".";
+    if (name != efObject->getName()) {
+        setDataObject(name);
+        emit dataObjectUpdated();
+    }
+    qDebug() << "Signal emitted.";
+    efObject = 0; // TODO we should also set efObject to null when user clicks abort
     close();
 }
 
-void NameDialog::enableSaveButton(const QString &text)
+void NameDialog::enableSaveButton(const QString &/*text*/)
 {
     //saveButton->setEnabled(!text.isEmpty());
 }
 
+void NameDialog::setDataObject(EmuFrontObject *ob)
+{
+    if (!ob) return;
+    // delete efObject; -> we should not delete the previously referenced data object here, it
+    // may be still used in the parent widget
+    // the parent widget will take care of destruction
+    // we'll just refresh the name dialog pointer to a new object
+    efObject = ob;
+}
 
+void NameDialog::clear()
+{
+    nameEdit->clear();
+}