ca640a8bf3ec390f166608411cc2c1e76a5b23b3
[emufront] / src / dialogs / executableeditdialog.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 "executableeditdialog.h"
22 #include "../db/dbexecutable.h"
23 #include "../db/dbsetup.h"
24 #include "../dataobjects/executable.h"
25 #include "../dataobjects/setup.h"
26 #include "../widgets/setupcombobox.h"
27 #include "../exceptions/emufrontexception.h"
28
29 ExecutableEditDialog::ExecutableEditDialog(QWidget *parent, EmuFrontObject *obj)
30     : DataObjectEditDialog(parent, obj)
31 {
32     dbExecutable = 0;
33     dbSetup = new DbSetup(this);
34     initWidgets();
35     connectSignals();
36     layout();
37 }
38
39 void ExecutableEditDialog::initWidgets()
40 {
41     setupComBox = new SetupComboBox(dbSetup, this);
42     nameEdit = new QLineEdit;
43     nameEdit->setToolTip(tr("Set an individual short "
44         "description for this emulator configuration."));
45     execEdit = new QLineEdit;
46     execEdit->setToolTip(tr("Emulator executable in $PATH "
47         "or absolute path to emulator executable."));
48     optEdit = new QLineEdit;
49     optEdit->setToolTip(tr("Command line parameters for the "
50         "emulator executable including $1 as file 1 to be "
51         "assigned to emulator, $2 as file 2, etc."));
52 }
53
54 void ExecutableEditDialog::layout()
55 {
56     QLabel *nameLabel = new QLabel(tr("&Description"));
57     nameLabel->setBuddy(nameEdit);
58     QLabel *execLabel = new QLabel(tr("&Executable"));
59     execLabel->setBuddy(execEdit);
60     QLabel *optLabel = new QLabel(tr("&Parameters"));
61     optLabel->setBuddy(optEdit);
62     QLabel *setupLabel = new QLabel(tr("&Setup"));
63     setupLabel->setBuddy(setupComBox);
64     QGridLayout *gridLayout = new QGridLayout;
65     gridLayout->addWidget(nameLabel, 0, 0);
66     gridLayout->addWidget(nameEdit, 0, 1);
67     gridLayout->addWidget(execLabel, 1, 0);
68     gridLayout->addWidget(execEdit, 1, 1);
69     gridLayout->addWidget(optLabel, 2, 0);
70     gridLayout->addWidget(optEdit, 2, 1);
71     gridLayout->addWidget(setupLabel, 3, 0);
72     gridLayout->addWidget(setupComBox, 3, 1);
73     QVBoxLayout *mainLayout = new QVBoxLayout;
74     mainLayout->addLayout(gridLayout);
75     mainLayout->addWidget(buttonBox);
76     setLayout(mainLayout);
77     setWindowTitle(tr("Edit emulator configuration"));
78 }
79
80 void ExecutableEditDialog::acceptChanges()
81 {
82     Executable *ex = dynamic_cast<Executable*>(efObject);
83     try {
84         Setup *su = getSelectedSetup();
85         if (!su) {
86             throw EmuFrontException(tr("Setup not selected"));
87         }
88         QString name = nameEdit->text().trimmed();
89         if (name.isEmpty()){
90             throw EmuFrontException(tr("Name is not set"));
91         }
92         QString exec = execEdit->text().trimmed();
93         if (exec.isEmpty()){
94             throw EmuFrontException(tr("Executable is not set"));
95         }
96         QString opts = optEdit->text().trimmed();
97         if (opts.isEmpty()) {
98             throw EmuFrontException(tr("Options not set"));
99         }
100         bool change = false;
101         Setup *supTmp = ex->getSetup();
102         if (!supTmp || *supTmp != *su) {
103             delete supTmp;
104             ex->setSetup(su);
105             change = true;
106         }
107
108         if (name != ex->getName()) {
109             ex->setName(name);
110             change = true;
111         }
112
113         if (exec != ex->getExecutable()) {
114             ex->setExecutable(exec);
115             change = true;
116         }
117         if (opts != ex->getOptions()) {
118             ex->setOptions(opts);
119             change = true;
120         }
121         if (change) emit dataObjectUpdated();
122         efObject = 0;
123         close();
124     } catch(EmuFrontException x) {
125             QMessageBox::information(this,
126                                      tr("Updating emulator"),
127                                      x.what(), QMessageBox::Ok);
128             return;
129     }
130 }
131
132 void ExecutableEditDialog::setDataObject(EmuFrontObject *ob)
133 {
134     if (!ob) return;
135     efObject = ob;
136     Executable *ex = dynamic_cast<Executable*>(ob);
137     if (!ex) {
138         qDebug("No executable");
139         return;
140     }
141     if (!ex->getSetup()) {
142         qDebug() << "No setup";
143         return;
144     }
145     if (ex->getSetup() && ex->getSetup()->getId() >= 0)
146         setSelectedSetup(ex->getSetup());
147     nameEdit->setText(ex->getName());
148     execEdit->setText(ex->getExecutable());
149     optEdit->setText(ex->getOptions());
150 }
151
152 void ExecutableEditDialog::setSelectedSetup(const Setup *su)
153 {
154     setupComBox->setSelected(su);
155 }
156
157 Setup* ExecutableEditDialog::getSelectedSetup()
158 {
159     EmuFrontObject *o = 0;
160     try { o = setupComBox->getSelected(); }
161     catch(EmuFrontException &e){ errorMessage->showMessage(e.what()); }
162
163     if (!o) return 0;
164     Setup *ex = dynamic_cast<Setup*>(o);
165     return ex;
166 }
167
168 void ExecutableEditDialog::updateData()
169 {
170     setupComBox->updateDataModel();
171 }