3ec89dc3b9916ba9a9d5f2154b69860a19585869
[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 new EmuFrontException(tr("Setup not selected"));
87         }
88         QString name = nameEdit->text().trimmed();
89         if (name.isEmpty()){
90             throw new EmuFrontException(tr("Name is not set"));
91         }
92         QString exec = execEdit->text().trimmed();
93         if (exec.isEmpty()){
94             throw new EmuFrontException(tr("Executable is not set"));
95         }
96         QString opts = optEdit->text().trimmed();
97         if (opts.isEmpty()) {
98             throw new EmuFrontException(tr("Options not set"));
99         }
100         Setup *supTmp = ex->getSetup();
101         if (supTmp != su) {
102             delete supTmp;
103             ex->setSetup(su);
104         }
105         ex->setName(name);
106         ex->setExecutable(exec);
107         ex->setOptions(opts);
108         emit dataObjectUpdated();
109         efObject = 0;
110         close();
111     } catch(EmuFrontException x) {
112             QMessageBox::information(this,
113                                      tr("Updating emulator"),
114                                      x.what(), QMessageBox::Ok);
115             return;
116     }
117 }
118
119 void ExecutableEditDialog::setDataObject(EmuFrontObject *ob)
120 {
121     if (!ob) return;
122     efObject = ob;
123     Executable *ex = dynamic_cast<Executable*>(ob);
124     if (!ex) {
125         qDebug("No executable");
126         return;
127     }
128     if (!ex->getSetup()) {
129         qDebug() << "No setup";
130         return;
131     }
132     if (ex->getSetup() && ex->getSetup()->getId() >= 0)
133         setSelectedSetup(ex->getSetup());
134     nameEdit->setText(ex->getName());
135     execEdit->setText(ex->getExecutable());
136     optEdit->setText(ex->getOptions());
137 }
138
139 void ExecutableEditDialog::setSelectedSetup(const Setup *su)
140 {
141     setupComBox->setSelected(su);
142 }
143
144 Setup* ExecutableEditDialog::getSelectedSetup() const
145 {
146     EmuFrontObject *o = setupComBox->getSelected();
147     if (!o) return 0;
148     Setup *ex = dynamic_cast<Setup*>(o);
149     return ex;
150 }
151
152 void ExecutableEditDialog::updateData()
153 {
154     setupComBox->updateDataModel();
155 }