Expanded Media image path editor functionality.
[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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Foobar 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 Foobar.  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         setWindowTitle(tr("Set names"));
32 }
33
34 NameDialog::~NameDialog()
35 {
36     // should be deleted in implementing classes
37     // delete efObject;
38
39     /* no need to delete parented QT-objects in heap here
40          * because when deleting a parent widget
41      * the child widgets will be also deleted
42          */
43 }
44
45 void NameDialog::connectSignals()
46 {
47     DataObjectEditDialog::connectSignals();
48     connect(nameEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableSaveButton(const QString &)));
49 }
50
51 void NameDialog::layout()
52 {
53         QHBoxLayout *topLayout = new QHBoxLayout;
54         topLayout->addWidget(nameLabel);
55         topLayout->addWidget(nameEdit);
56
57         QHBoxLayout *bottomLayout = new QHBoxLayout;
58     bottomLayout->addWidget(buttonBox);
59
60         QVBoxLayout *mainLayout = new QVBoxLayout;
61         mainLayout->addLayout(topLayout);
62         mainLayout->addLayout(bottomLayout);
63         setLayout(mainLayout);
64 }
65
66 void NameDialog::rejectChanges()
67 {
68     efObject = 0;
69     emit updateRejected();
70     close();
71 }
72
73 void NameDialog::acceptChanges()
74 {
75     if (nameEdit->text() == 0 || nameEdit->text().trimmed().isEmpty())
76     {
77         QMessageBox::warning(this, tr("Invalid input"), tr("Empty string is not accepted as name!"));
78                 return;
79     }
80
81         QString name = nameEdit->text().simplified();
82     setDataObject(name);
83     emit dataObjectUpdated();
84     efObject = 0; // TODO we should also set efObject to null when user clicks abort
85     close();
86 }
87
88 void NameDialog::enableSaveButton(const QString &/*text*/)
89 {
90     //saveButton->setEnabled(!text.isEmpty());
91 }
92
93 void NameDialog::setDataObject(EmuFrontObject *ob)
94 {
95     if (!ob) return;
96     // delete efObject; -> we should not delete the previously referenced data object here, it may be still used in the parent widget
97     // the parent widget will take of destruction
98     // we'll just refresh the name dialog pointer to a new object
99     efObject = ob;
100 }