Data object passing.
[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         saveButton = new QPushButton(tr("&Save"));
11         saveButton->setDefault(true);
12         saveButton->setEnabled(false);
13         closeButton = new QPushButton(tr("Close"));
14         connectSignals();
15         layout();
16         setWindowTitle(tr("Set names"));
17 }
18
19 NameDialog::~NameDialog()
20 {
21     delete efObject;
22
23         /* deleting objects in heap is not needed here
24          * because when deleting a parent widget
25          * the child widgets will be also deleted:
26          * delete nameLabel;
27          * delete nameEdit;
28          * delete saveButton;
29          * delete closeButton;
30          */
31 }
32
33 void NameDialog::connectSignals()
34 {
35         connect(nameEdit, SIGNAL(textChanged(const QString &)), 
36                         this, SLOT(enableSaveButton(const QString &)));
37         connect(saveButton, SIGNAL(clicked()), this, SLOT(saveButtonClicked()));
38         connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
39 }
40
41 void NameDialog::layout()
42 {
43         QHBoxLayout *topLayout = new QHBoxLayout;
44         topLayout->addWidget(nameLabel);
45         topLayout->addWidget(nameEdit);
46
47         QHBoxLayout *bottomLayout = new QHBoxLayout;
48         bottomLayout->addStretch();
49         bottomLayout->addWidget(saveButton);
50         bottomLayout->addWidget(closeButton);
51
52         QVBoxLayout *mainLayout = new QVBoxLayout;
53         mainLayout->addLayout(topLayout);
54         mainLayout->addLayout(bottomLayout);
55         setLayout(mainLayout);
56 }
57
58 void NameDialog::saveButtonClicked()
59 {
60         if (nameEdit->text() == 0 || nameEdit->text().trimmed().isEmpty())
61                 return;
62
63         QString name = nameEdit->text().simplified();
64         if (save(name)) emit dbUpdated();
65         else errorMessage->showMessage("Database update failed!");
66         /*if (edit) updateDb(name);
67         else insertDb(name);*/
68 }
69
70 void NameDialog::enableSaveButton(const QString &text)
71 {
72         saveButton->setEnabled(!text.isEmpty());
73 }