2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
21 #include <QSqlTableModel>
23 #include "setupeditdialog.h"
24 #include "../widgets/stringlistwidget.h"
25 #include "../db/dbmediatype.h"
26 #include "../db/dbplatform.h"
27 #include "../db/dbsetup.h"
29 SetupEditDialog::SetupEditDialog(QWidget *parent, EmuFrontObject* obj)
30 : DataObjectEditDialog(parent, obj)
32 dbSetup = 0; //new DbSetup(this);
33 dbPlatform = 0; //new DbPlatform(this);
34 dbMediaType = 0; //new DbMediaType(this);
36 qDebug() << "Connecting signals";
41 void SetupEditDialog::initWidgets()
43 mediaTypeComBox = new QComboBox;
44 platformComBox = new QComboBox;
45 supportedFileTypesList = new StringListWidget;
46 populateMediaTypeComBox();
47 populatePlatformComBox();
50 void SetupEditDialog::populateMediaTypeComBox()
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();
57 qDebug() << "We have a media type data model";
58 mediaTypeComBox->setModel(model);
59 mediaTypeComBox->setModelColumn(DbMediaType::MediaType_Name);
62 void SetupEditDialog::populatePlatformComBox()
64 qDebug() << "populating platform combo box";
65 if (!dbPlatform) dbPlatform = new DbPlatform(this);
66 QSqlQueryModel *model = dbPlatform->getDataModel();
68 platformComBox->setModel(model);
69 platformComBox->setModelColumn(DbPlatform::Platform_Name);
70 qDebug() << "platform combo box populated";
73 void SetupEditDialog::layout()
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 gridLayout->addWidget(supportedFileTypesList, 2, 0, 2, 2);
86 QVBoxLayout *mainLayout = new QVBoxLayout;
87 mainLayout->addLayout(gridLayout);
88 mainLayout->addWidget(buttonBox);
89 setLayout(mainLayout);
90 qDebug() << "SetupEditDialog::layout done";
92 setWindowTitle(tr("Edit setup"));
95 void SetupEditDialog::acceptChanges()
97 Setup *sup = dynamic_cast<Setup*>(efObject);
98 Platform *plf = getSelectedPlatform();
101 QMessageBox::information(this, tr("Platform"), tr("Platform not selected"), QMessageBox::Ok);
104 qDebug() << "Platform selected " << plf->getName();
105 MediaType *mt = getSelectedMediaType();
108 QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
111 qDebug() << "Media type selected " << mt->getName();
114 Platform *ptmp = sup->getPlatform();
118 sup->setPlatform(plf);
120 MediaType *mtmp = sup->getMediaType();
124 sup->setMediaType(mt);
126 sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
127 emit dataObjectUpdated();
129 qDebug() << "Closing setup edit dialog";
133 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
137 Setup *sup= dynamic_cast<Setup*>(ob);
138 if (sup->getPlatform()) setSelectedPlatform(sup->getPlatform());
139 if (sup->getMediaType()) setSelectedMediaType(sup->getMediaType());
140 supportedFileTypesList->setItems(sup->getSupportedFileTypeExtensions());
143 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
145 setSelected(platformComBox, plf, DbPlatform::Platform_Id);
148 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
150 setSelected(mediaTypeComBox, plf, DbMediaType::MediaType_Id);
153 Platform* SetupEditDialog::getSelectedPlatform() const
155 qDebug() << "MediaImagePathDialog Selecting platform";
157 int index = platformComBox->currentIndex();
158 qDebug() << "Current index " << index;
159 if (index < 0) return plf;
160 QSqlTableModel* platformModel = dynamic_cast<QSqlTableModel*>(platformComBox->model());
163 qDebug() << "Data model missing";
166 QSqlRecord rec = platformModel->record(index);
169 qDebug() << "We have a record";
170 plf = new Platform(rec.value(DbPlatform::Platform_Id).toInt(),
171 rec.value(DbPlatform::Platform_Name).toString(),
172 rec.value(DbPlatform::Platform_Filename).toString());
174 else qDebug() << "Record missing";
178 MediaType* SetupEditDialog::getSelectedMediaType() const
181 int index = mediaTypeComBox->currentIndex();
182 if (index < 0) return mt;
183 QSqlTableModel* mediaTypeModel = dynamic_cast<QSqlTableModel*>(mediaTypeComBox->model());
186 qDebug("Media type data model missing");
189 QSqlRecord rec = mediaTypeModel->record(index);
191 mt = new MediaType(rec.value(DbMediaType::MediaType_Id).toInt(),
192 rec.value(DbMediaType::MediaType_Name).toString(),
193 rec.value(DbMediaType::MediaType_Filename).toString());