Inserting platform data enabled
[emufront] / src / dialogs / namedialog.cpp
1 #include <QtGui>
2 #include "namedialog.h"
3
4 NameDialog::NameDialog(QWidget *parent, EmuFrontObject *efObj)
5         : EmuFrontDialog(parent), efObject(efObj)
6 {
7         nameLabel = new QLabel(tr("&Name: "));  
8         nameEdit = new QLineEdit;
9         nameLabel->setBuddy(nameEdit);
10     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Abort, Qt::Horizontal);
11     connectSignals();
12         layout();
13         setWindowTitle(tr("Set names"));
14 }
15
16 NameDialog::~NameDialog()
17 {
18     delete efObject;
19
20     /* deleting parenteed QT-objects in heap is not needed here
21          * because when deleting a parent widget
22      * the child widgets will be also deleted
23          */
24 }
25
26 void NameDialog::connectSignals()
27 {
28     connect(nameEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableSaveButton(const QString &)));
29     connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptChanges()));
30     connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
31 }
32
33 void NameDialog::layout()
34 {
35         QHBoxLayout *topLayout = new QHBoxLayout;
36         topLayout->addWidget(nameLabel);
37         topLayout->addWidget(nameEdit);
38
39         QHBoxLayout *bottomLayout = new QHBoxLayout;
40     bottomLayout->addWidget(buttonBox);
41
42         QVBoxLayout *mainLayout = new QVBoxLayout;
43         mainLayout->addLayout(topLayout);
44         mainLayout->addLayout(bottomLayout);
45         setLayout(mainLayout);
46 }
47
48 void NameDialog::acceptChanges()
49 {
50     if (nameEdit->text() == 0 || nameEdit->text().trimmed().isEmpty())
51     {
52         QMessageBox::warning(this, tr("Invalid input"), tr("Empty string is not accepted as name!"));
53                 return;
54     }
55
56         QString name = nameEdit->text().simplified();
57     setDataObject(name);
58     emit dataObjectUpdated();
59     efObject = 0; // TODO we should also se efObject to null when user clicks abort
60     close();
61 }
62
63 void NameDialog::enableSaveButton(const QString &text)
64 {
65     //saveButton->setEnabled(!text.isEmpty());
66 }
67
68 void NameDialog::setDataObject(EmuFrontObject *ob)
69 {
70     // delete efObject; -> we should not delete the previously referenced data object here, it may be still used in the parent widget
71     // the parent widget will take of destruction
72     // we'll just refresh the name dialog pointer to a new object
73     efObject = ob;
74 }