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 QVBoxLayout *mainLayout = new QVBoxLayout;
86 mainLayout->addLayout(gridLayout);
87 mainLayout->addWidget(buttonBox);
88 setLayout(mainLayout);
89 qDebug() << "SetupEditDialog::layout done";
91 setWindowTitle(tr("Edit setup"));
94 void SetupEditDialog::acceptChanges()
96 Setup *sup = dynamic_cast<Setup*>(efObject);
97 Platform *plf = getSelectedPlatform();
100 QMessageBox::information(this, tr("Platform"), tr("Platform not selected"), QMessageBox::Ok);
103 qDebug() << "Platform selected " << plf->getName();
104 MediaType *mt = getSelectedMediaType();
107 QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
110 qDebug() << "Media type selected " << mt->getName();
113 Platform *ptmp = sup->getPlatform();
117 sup->setPlatform(plf);
119 MediaType *mtmp = sup->getMediaType();
123 sup->setMediaType(mt);
125 sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
126 emit dataObjectUpdated();
131 void SetupEditDialog::setDataObject(EmuFrontObject *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());
141 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
143 setSelected(platformComBox, plf, DbPlatform::Platform_Id);
146 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
148 setSelected(mediaTypeComBox, plf, DbMediaType::MediaType_Id);
151 Platform* SetupEditDialog::getSelectedPlatform() const
153 qDebug() << "MediaImagePathDialog Selecting platform";
155 int index = platformComBox->currentIndex();
156 qDebug() << "Current index " << index;
157 if (index < 0) return plf;
158 QSqlTableModel* platformModel = dynamic_cast<QSqlTableModel*>(platformComBox->model());
161 qDebug() << "Data model missing";
164 QSqlRecord rec = platformModel->record(index);
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());
172 else qDebug() << "Record missing";
176 MediaType* SetupEditDialog::getSelectedMediaType() const
179 int index = mediaTypeComBox->currentIndex();
180 if (index < 0) return mt;
181 QSqlTableModel* mediaTypeModel = dynamic_cast<QSqlTableModel*>(mediaTypeComBox->model());
184 qDebug("Media type data model missing");
187 QSqlRecord rec = mediaTypeModel->record(index);
189 mt = new MediaType(rec.value(DbMediaType::MediaType_Id).toInt(),
190 rec.value(DbMediaType::MediaType_Name).toString(),
191 rec.value(DbMediaType::MediaType_Filename).toString());