Updated the git clone command.
[emufront] / src / dialogs / executableeditdialog.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <QtGui>
22 #include "executableeditdialog.h"
23 #include "dbexecutable.h"
24 #include "dbsetup.h"
25 #include "executable.h"
26 #include "setup.h"
27 #include "setupcombobox.h"
28 #include "emufrontexception.h"
29
30 ExecutableEditDialog::ExecutableEditDialog(QWidget *parent, EmuFrontObject *obj)
31     : DataObjectEditDialog(parent, obj)
32 {
33     dbExecutable = 0;
34     dbSetup = new DbSetup(this);
35     initWidgets();
36     connectSignals();
37     layout();
38 }
39
40 void ExecutableEditDialog::initWidgets()
41 {
42     setupComBox = new SetupComboBox(dbSetup, this);
43     nameEdit = new QLineEdit;
44     nameEdit->setToolTip(tr("Set an individual short "
45         "description for this emulator configuration."));
46     execEdit = new QLineEdit;
47     execEdit->setToolTip(tr("Emulator executable in $PATH "
48         "or absolute path to emulator executable."));
49     optEdit = new QLineEdit;
50     optEdit->setToolTip(tr("Command line parameters for the "
51         "emulator executable including $1 as file 1 to be "
52         "assigned to emulator, $2 as file 2, etc."));
53 }
54
55 void ExecutableEditDialog::layout()
56 {
57     QLabel *nameLabel = new QLabel(tr("&Description"));
58     nameLabel->setBuddy(nameEdit);
59     QLabel *execLabel = new QLabel(tr("&Executable"));
60     execLabel->setBuddy(execEdit);
61     QLabel *optLabel = new QLabel(tr("&Parameters"));
62     optLabel->setBuddy(optEdit);
63     QLabel *setupLabel = new QLabel(tr("&Setup"));
64     setupLabel->setBuddy(setupComBox);
65     QGridLayout *gridLayout = new QGridLayout;
66     gridLayout->addWidget(nameLabel, 0, 0);
67     gridLayout->addWidget(nameEdit, 0, 1);
68     gridLayout->addWidget(execLabel, 1, 0);
69     gridLayout->addWidget(execEdit, 1, 1);
70     gridLayout->addWidget(optLabel, 2, 0);
71     gridLayout->addWidget(optEdit, 2, 1);
72     gridLayout->addWidget(setupLabel, 3, 0);
73     gridLayout->addWidget(setupComBox, 3, 1);
74     QVBoxLayout *mainLayout = new QVBoxLayout;
75     mainLayout->addLayout(gridLayout);
76     mainLayout->addWidget(buttonBox);
77     setLayout(mainLayout);
78     setWindowTitle(tr("Edit emulator configuration"));
79 }
80
81 void ExecutableEditDialog::acceptChanges()
82 {
83     Executable *ex = dynamic_cast<Executable*>(efObject);
84     try {
85         Setup *su = getSelectedSetup();
86         if (!su) {
87             throw EmuFrontException(tr("Setup not selected"));
88         }
89         QString name = nameEdit->text().trimmed();
90         if (name.isEmpty()){
91             throw EmuFrontException(tr("Name is not set"));
92         }
93         QString exec = execEdit->text().trimmed();
94         if (exec.isEmpty()){
95             throw EmuFrontException(tr("Executable is not set"));
96         }
97         QString opts = optEdit->text().trimmed();
98         if (opts.isEmpty()) {
99             throw EmuFrontException(tr("Options not set"));
100         }
101         bool change = false;
102         Setup *supTmp = ex->getSetup();
103         if (!supTmp || *supTmp != *su) {
104             delete supTmp;
105             ex->setSetup(su);
106             change = true;
107         }
108
109         if (name != ex->getName()) {
110             ex->setName(name);
111             change = true;
112         }
113
114         if (exec != ex->getExecutable()) {
115             ex->setExecutable(exec);
116             change = true;
117         }
118         if (opts != ex->getOptions()) {
119             ex->setOptions(opts);
120             change = true;
121         }
122         if (change) emit dataObjectUpdated();
123         efObject = 0;
124         close();
125     } catch(EmuFrontException x) {
126             QMessageBox::information(this,
127                                      tr("Updating emulator"),
128                                      x.what(), QMessageBox::Ok);
129             return;
130     }
131 }
132
133 void ExecutableEditDialog::setDataObject(EmuFrontObject *ob)
134 {
135     if (!ob) return;
136     efObject = ob;
137     Executable *ex = dynamic_cast<Executable*>(ob);
138     if (!ex) {
139         qDebug("No executable");
140         return;
141     }
142     clear();
143     if (!ex->getSetup()) {
144         qDebug() << "No setup";
145         return;
146     }
147     if (ex->getSetup() && ex->getSetup()->getId() >= 0)
148         setSelectedSetup(ex->getSetup());
149     nameEdit->setText(ex->getName());
150     execEdit->setText(ex->getExecutable());
151     optEdit->setText(ex->getOptions());
152 }
153
154 void ExecutableEditDialog::clear()
155 {
156     nameEdit->clear();
157     execEdit->clear();
158     optEdit->clear();
159     setupComBox->setCurrentIndex(-1);
160 }
161
162 void ExecutableEditDialog::setSelectedSetup(const Setup *su)
163 {
164     setupComBox->setSelected(su);
165 }
166
167 /* Returns a pointer to a Setup object which must be deleted by calling code! */
168 Setup* ExecutableEditDialog::getSelectedSetup()
169 {
170     EmuFrontObject *o = 0;
171     try { o = setupComBox->getSelected(); }
172     catch(EmuFrontException &e){ errorMessage->showMessage(e.what()); }
173
174     if (!o) return 0;
175     Setup *ex = dynamic_cast<Setup*>(o);
176     return ex;
177 }
178
179 void ExecutableEditDialog::updateData()
180 {
181     setupComBox->updateDataModel();
182 }