Fixed a bug: the edit dialog fields were not cleared if an
[emufront] / src / dialogs / namedialog.cpp
index 282a9fd..0bbffbf 100644 (file)
@@ -1,43 +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, bool edit)
-       : QDialog(parent), edit(edit)
+NameDialog::NameDialog(QWidget *parent, EmuFrontObject *efObj)
+        : DataObjectEditDialog(parent, efObj)
 {
        nameLabel = new QLabel(tr("&Name: "));  
        nameEdit = new QLineEdit;
        nameLabel->setBuddy(nameEdit);
-       saveButton = new QPushButton(tr("&Save"));
-       saveButton->setDefault(true);
-       saveButton->setEnabled(false);
-       closeButton = new QPushButton(tr("Close"));
-       connectSignals();
+    connectSignals();
        layout();
+    emit test();
        setWindowTitle(tr("Set names"));
 }
 
 NameDialog::~NameDialog()
 {
-       /* deleting objects in heap is not needed here
+    // should be deleted in implementing classes
+    // delete efObject;
+
+    /* 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::setEdit(bool edit)
-{
-       this->edit = edit;
-}
 void NameDialog::connectSignals()
 {
-       connect(nameEdit, SIGNAL(textChanged(const QString &)), 
-                       this, SLOT(enableSaveButton(const QString &)));
-       connect(saveButton, SIGNAL(clicked()), this, SLOT(saveButtonClicked()));
-       connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
+    DataObjectEditDialog::connectSignals();
+    connect(nameEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableSaveButton(const QString &)));
 }
 
 void NameDialog::layout()
@@ -47,9 +56,7 @@ void NameDialog::layout()
        topLayout->addWidget(nameEdit);
 
        QHBoxLayout *bottomLayout = new QHBoxLayout;
-       bottomLayout->addStretch();
-       bottomLayout->addWidget(saveButton);
-       bottomLayout->addWidget(closeButton);
+    bottomLayout->addWidget(buttonBox);
 
        QVBoxLayout *mainLayout = new QVBoxLayout;
        mainLayout->addLayout(topLayout);
@@ -57,17 +64,41 @@ void NameDialog::layout()
        setLayout(mainLayout);
 }
 
-void NameDialog::saveButtonClicked()
+void NameDialog::acceptChanges()
 {
-       if (nameEdit->text() == 0 || nameEdit->text().trimmed().isEmpty())
+    if (nameEdit->text() == 0 || nameEdit->text().trimmed().isEmpty())
+    {
+        QMessageBox::warning(this, tr("Invalid input"), tr("Empty string is not accepted as name!"));
                return;
+    }
 
        QString name = nameEdit->text().simplified();
-       /*if (edit) updateDb(name);
-       else insertDb(name);*/
+    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*/)
+{
+    //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::enableSaveButton(const QString &text)
+void NameDialog::clear()
 {
-       saveButton->setEnabled(!text.isEmpty());
+    nameEdit->clear();
 }