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