Name dialog functionality
[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     // actually we need to create here a (deep) copy of the original object
8     // if the user clicks cancel the original remains unchanged
9     // if the user clicks or we set the pointer to modified object OR we update the original object:
10     //        - the copy object could be a stack object
11         nameLabel = new QLabel(tr("&Name: "));  
12         nameEdit = new QLineEdit;
13         nameLabel->setBuddy(nameEdit);
14     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Abort, Qt::Horizontal);
15     /*saveButton = new QPushButton(tr("&Save"));
16         saveButton->setDefault(true);
17         saveButton->setEnabled(false);
18     closeButton = new QPushButton(tr("Close"));*/
19     connectSignals();
20         layout();
21         setWindowTitle(tr("Set names"));
22 }
23
24 NameDialog::~NameDialog()
25 {
26     delete efObject;
27
28         /* deleting objects in heap is not needed here
29          * because when deleting a parent widget
30          * the child widgets will be also deleted:
31          * delete nameLabel;
32          * delete nameEdit;
33          * delete saveButton;
34          * delete closeButton;
35          */
36 }
37
38 void NameDialog::connectSignals()
39 {
40     connect(nameEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableSaveButton(const QString &)));
41     connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptChanges()));
42     connect(buttonBox, SIGNAL(rejected()), this, SLOT(close()));
43
44     /*connect(saveButton, SIGNAL(clicked()), this, SLOT(saveButtonClicked()));
45     connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));*/
46 }
47
48 void NameDialog::layout()
49 {
50         QHBoxLayout *topLayout = new QHBoxLayout;
51         topLayout->addWidget(nameLabel);
52         topLayout->addWidget(nameEdit);
53
54         QHBoxLayout *bottomLayout = new QHBoxLayout;
55     bottomLayout->addWidget(buttonBox);
56     /*bottomLayout->addStretch();
57         bottomLayout->addWidget(saveButton);
58     bottomLayout->addWidget(closeButton);*/
59
60         QVBoxLayout *mainLayout = new QVBoxLayout;
61         mainLayout->addLayout(topLayout);
62         mainLayout->addLayout(bottomLayout);
63         setLayout(mainLayout);
64 }
65
66 void NameDialog::saveButtonClicked()
67 {
68     if (nameEdit->text() == 0 || nameEdit->text().trimmed().isEmpty())
69                 return;
70
71         QString name = nameEdit->text().simplified();
72     setDataObject(name);
73     /*
74
75
76     efObject->setName(name);
77     efObject->setFilename("");
78
79     emit accept();*/
80 }
81
82 void NameDialog::enableSaveButton(const QString &text)
83 {
84     //saveButton->setEnabled(!text.isEmpty());
85 }
86
87 void NameDialog::close(bool save)
88 {
89     if (!save)
90     {
91         // restore original instance
92     }
93 }