0a86b1816145f57f49d4b46fb2ace3207f48b55c
[emufront] / src / dialogs / namedialog.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
11 //
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QtGui>
21 #include "namedialog.h"
22
23 NameDialog::NameDialog(QWidget *parent, EmuFrontObject *efObj)
24         : DataObjectEditDialog(parent, efObj)
25 {
26         nameLabel = new QLabel(tr("&Name: "));  
27     nameEdit = new QLineEdit;
28     nameLabel->setBuddy(nameEdit);
29     connectSignals();
30         layout();
31     emit test();
32     setWindowTitle(tr("Set names"));
33     setFocusProxy(nameEdit);
34 }
35
36
37
38 NameDialog::~NameDialog()
39 {
40     // should be deleted in implementing classes
41     // delete efObject;
42
43     /* no need to delete parented QT-objects in heap here
44          * because when deleting a parent widget
45      * the child widgets will be also deleted
46          */
47 }
48
49 void NameDialog::connectSignals()
50 {
51     DataObjectEditDialog::connectSignals();
52     connect(nameEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableSaveButton(const QString &)));
53 }
54
55 void NameDialog::layout()
56 {
57         QHBoxLayout *topLayout = new QHBoxLayout;
58         topLayout->addWidget(nameLabel);
59         topLayout->addWidget(nameEdit);
60
61         QHBoxLayout *bottomLayout = new QHBoxLayout;
62     bottomLayout->addWidget(buttonBox);
63     buttonBox->setFocusPolicy(Qt::NoFocus);
64
65         QVBoxLayout *mainLayout = new QVBoxLayout;
66         mainLayout->addLayout(topLayout);
67         mainLayout->addLayout(bottomLayout);
68     setLayout(mainLayout);
69 }
70
71 void NameDialog::acceptChanges()
72 {
73     if (nameEdit->text() == 0 || nameEdit->text().trimmed().isEmpty())
74     {
75         QMessageBox::warning(this, tr("Invalid input"), tr("Empty string is not accepted as name!"));
76                 return;
77     }
78
79         QString name = nameEdit->text().simplified();
80     qDebug() << "We have a name " << name << ".";
81     if (name != efObject->getName()) {
82         setDataObject(name);
83         emit dataObjectUpdated();
84     }
85     qDebug() << "Signal emitted.";
86     efObject = 0; // TODO we should also set efObject to null when user clicks abort
87     close();
88 }
89
90 void NameDialog::enableSaveButton(const QString &/*text*/)
91 {
92     //saveButton->setEnabled(!text.isEmpty());
93 }
94
95 void NameDialog::setDataObject(EmuFrontObject *ob)
96 {
97     if (!ob) return;
98     // delete efObject; -> we should not delete the previously referenced data object here, it
99     // may be still used in the parent widget
100     // the parent widget will take care of destruction
101     // we'll just refresh the name dialog pointer to a new object
102     efObject = ob;
103 }
104
105 void NameDialog::clear()
106 {
107     nameEdit->clear();
108 }