Implemented Executable editor (for configurating emulators).
[emufront] / src / dialogs / setupeditdialog.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 // 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 <QSqlTableModel>
22 #include <QSqlRecord>
23 #include "setupeditdialog.h"
24 #include "../widgets/stringlistwidget.h"
25 #include "../widgets/effileobjectcombobox.h"
26 #include "../db/dbmediatype.h"
27 #include "../db/dbplatform.h"
28 #include "../db/dbsetup.h"
29
30 SetupEditDialog::SetupEditDialog(QWidget *parent, EmuFrontObject* obj)
31     : DataObjectEditDialog(parent, obj)
32 {
33     dbSetup = 0; //new DbSetup(this);
34     dbPlatform = new DbPlatform(this);
35     dbMediaType = new DbMediaType(this);
36     initWidgets();
37     qDebug() << "Connecting signals";
38     connectSignals();
39     layout();
40 }
41
42 void SetupEditDialog::initWidgets()
43 {
44     mediaTypeComBox = new EFFileObjectComboBox(dbMediaType, this); //new QComboBox;
45     platformComBox = new EFFileObjectComboBox(dbPlatform, this); //new QComboBox;
46     supportedFileTypesList = new StringListWidget;
47 }
48
49 void SetupEditDialog::layout()
50 {
51     qDebug() << "SetupEditDialog::layout";
52    QLabel *platformLabel = new QLabel(tr("&Platform"));
53    platformLabel->setBuddy(platformComBox);
54    QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
55    mediaTypeLabel->setBuddy(mediaTypeComBox);
56    QGridLayout *gridLayout = new QGridLayout;
57    gridLayout->addWidget(platformLabel, 0, 0);
58    gridLayout->addWidget(platformComBox, 0, 1);
59    gridLayout->addWidget(mediaTypeLabel, 1, 0);
60    gridLayout->addWidget(mediaTypeComBox, 1, 1);
61    gridLayout->addWidget(supportedFileTypesList, 2, 0, 2, 2);
62    QVBoxLayout *mainLayout = new QVBoxLayout;
63    mainLayout->addLayout(gridLayout);
64    mainLayout->addWidget(buttonBox);
65    setLayout(mainLayout);
66     qDebug() << "SetupEditDialog::layout done";
67
68    setWindowTitle(tr("Edit setup"));
69 }
70
71 void SetupEditDialog::acceptChanges()
72 {
73     Setup *sup = dynamic_cast<Setup*>(efObject);
74     Platform *plf = getSelectedPlatform();
75     if (!plf)
76     {
77         QMessageBox::information(this, tr("Platform"),
78             tr("Platform not selected"), QMessageBox::Ok);
79         return;
80     }
81     qDebug() << "Platform selected " << plf->getName();
82     MediaType *mt = getSelectedMediaType();
83     if (!mt)
84     {
85         QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
86         return;
87     }
88     qDebug() << "Media type selected " << mt->getName();
89
90
91     Platform *ptmp = sup->getPlatform();
92     if (plf != ptmp)
93     {
94         delete ptmp;
95         sup->setPlatform(plf);
96     }
97     MediaType *mtmp = sup->getMediaType();
98     if (mt != mtmp)
99     {
100         delete mtmp;
101         sup->setMediaType(mt);
102     }
103     sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
104     emit dataObjectUpdated();
105     efObject = 0;
106     qDebug() << "Closing setup edit dialog";
107     close();
108 }
109
110 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
111 {
112     if (!ob) return;
113     efObject = ob;
114     Setup *sup= dynamic_cast<Setup*>(ob);
115     if (sup->getPlatform()) setSelectedPlatform(sup->getPlatform());
116     if (sup->getMediaType()) setSelectedMediaType(sup->getMediaType());
117     supportedFileTypesList->setItems(sup->getSupportedFileTypeExtensions());
118 }
119
120 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
121 {
122     //setSelected(platformComBox, plf, DbPlatform::EmuFrontFileObject_Id);
123     platformComBox->setSelected(plf);
124 }
125
126 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
127 {
128     //setSelected(mediaTypeComBox, plf, DbMediaType::EmuFrontFileObject_Id);
129     mediaTypeComBox->setSelected(plf);
130 }
131
132 Platform* SetupEditDialog::getSelectedPlatform() const
133 {
134     /*Platform *plf = 0;
135     int index = platformComBox->currentIndex();
136     qDebug() << "Platform from index " << index << " selected";
137     if (index < 0)
138         return plf;
139     QSqlQueryModel* platformModel
140         = dynamic_cast<QSqlQueryModel*>(platformComBox->model());
141     if (!platformModel)
142     {
143         qDebug() << "No platform model";
144         return plf;
145     }
146     QSqlRecord rec = platformModel->record(index);
147     if (!rec.isEmpty())
148     {
149         qDebug() << "Trying to fetch platform with id "
150             << rec.value(DbPlatform::EmuFrontFileObject_Id).toInt() ;
151         EmuFrontObject *o
152             = dbPlatform->getDataObject(rec.value(DbPlatform::EmuFrontFileObject_Id).toInt());
153         plf = dynamic_cast<Platform*>(o);
154     }*/
155
156     EmuFrontObject *o = platformComBox->getSelected();
157     if (!o) return 0;
158     Platform *plf = dynamic_cast<Platform*>(o);
159     return plf;
160 }
161
162 MediaType* SetupEditDialog::getSelectedMediaType() const
163 {
164     /*MediaType *mt = 0;
165     int index = mediaTypeComBox->currentIndex();
166     if (index < 0) return mt;
167     QSqlQueryModel* mediaTypeModel = dynamic_cast<QSqlQueryModel*>(mediaTypeComBox->model());
168     if (!mediaTypeModel)
169     {
170         qDebug("Media type data model missing");
171         return mt;
172     }
173     QSqlRecord rec = mediaTypeModel->record(index);
174     if (!rec.isEmpty())
175     {
176         EmuFrontObject *o = dbMediaType->getDataObject(rec.value(DbMediaType::EmuFrontFileObject_Id).toInt());
177         mt = dynamic_cast<MediaType*>(o);
178     }
179     return mt;*/
180     EmuFrontObject *o = mediaTypeComBox->getSelected();
181     if (!o) return 0;
182     MediaType *mt = dynamic_cast<MediaType*>(o);
183     return mt;
184 }
185
186 void SetupEditDialog::updateData()
187 {
188     qDebug() << "Updating combobox data";
189     platformComBox->updateDataModel();
190     mediaTypeComBox->updateDataModel();
191 }