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 // 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.
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/>.
21 #include <QSqlTableModel>
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"
30 SetupEditDialog::SetupEditDialog(QWidget *parent, EmuFrontObject* obj)
31 : DataObjectEditDialog(parent, obj)
33 dbSetup = 0; //new DbSetup(this);
34 dbPlatform = new DbPlatform(this);
35 dbMediaType = new DbMediaType(this);
37 qDebug() << "Connecting signals";
42 void SetupEditDialog::initWidgets()
44 mediaTypeComBox = new EFFileObjectComboBox(dbMediaType, this); //new QComboBox;
45 platformComBox = new EFFileObjectComboBox(dbPlatform, this); //new QComboBox;
46 supportedFileTypesList = new StringListWidget;
47 //populateMediaTypeComBox();
48 //populatePlatformComBox();
51 /*void SetupEditDialog::populateMediaTypeComBox()
53 qDebug() << "populating media types combo box";
54 if (!dbMediaType) dbMediaType = new DbMediaType(this);
55 qDebug() << "media type database manager created";
56 QSqlQueryModel *model = dbMediaType->getDataModel();
58 qDebug() << "We have a media type data model";
59 mediaTypeComBox->setModel(model);
60 mediaTypeComBox->setModelColumn(DbMediaType::EmuFrontFileObject_Name);
63 void SetupEditDialog::populatePlatformComBox()
65 qDebug() << "populating platform combo box";
66 if (!dbPlatform) dbPlatform = new DbPlatform(this);
67 QSqlQueryModel *model = dbPlatform->getDataModel();
69 platformComBox->setModel(model);
70 platformComBox->setModelColumn(DbPlatform::EmuFrontFileObject_Name);
71 qDebug() << "platform combo box populated";
74 void SetupEditDialog::layout()
76 qDebug() << "SetupEditDialog::layout";
77 QLabel *platformLabel = new QLabel(tr("&Platform"));
78 platformLabel->setBuddy(platformComBox);
79 QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
80 mediaTypeLabel->setBuddy(mediaTypeComBox);
81 QGridLayout *gridLayout = new QGridLayout;
82 gridLayout->addWidget(platformLabel, 0, 0);
83 gridLayout->addWidget(platformComBox, 0, 1);
84 gridLayout->addWidget(mediaTypeLabel, 1, 0);
85 gridLayout->addWidget(mediaTypeComBox, 1, 1);
86 gridLayout->addWidget(supportedFileTypesList, 2, 0, 2, 2);
87 QVBoxLayout *mainLayout = new QVBoxLayout;
88 mainLayout->addLayout(gridLayout);
89 mainLayout->addWidget(buttonBox);
90 setLayout(mainLayout);
91 qDebug() << "SetupEditDialog::layout done";
93 setWindowTitle(tr("Edit setup"));
96 void SetupEditDialog::acceptChanges()
98 Setup *sup = dynamic_cast<Setup*>(efObject);
99 Platform *plf = getSelectedPlatform();
102 QMessageBox::information(this, tr("Platform"), tr("Platform not selected"), QMessageBox::Ok);
105 qDebug() << "Platform selected " << plf->getName();
106 MediaType *mt = getSelectedMediaType();
109 QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
112 qDebug() << "Media type selected " << mt->getName();
115 Platform *ptmp = sup->getPlatform();
119 sup->setPlatform(plf);
121 MediaType *mtmp = sup->getMediaType();
125 sup->setMediaType(mt);
127 sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
128 emit dataObjectUpdated();
130 qDebug() << "Closing setup edit dialog";
134 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
138 Setup *sup= dynamic_cast<Setup*>(ob);
139 if (sup->getPlatform()) setSelectedPlatform(sup->getPlatform());
140 if (sup->getMediaType()) setSelectedMediaType(sup->getMediaType());
141 supportedFileTypesList->setItems(sup->getSupportedFileTypeExtensions());
144 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
146 //setSelected(platformComBox, plf, DbPlatform::EmuFrontFileObject_Id);
147 platformComBox->setSelected(plf);
150 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
152 //setSelected(mediaTypeComBox, plf, DbMediaType::EmuFrontFileObject_Id);
153 mediaTypeComBox->setSelected(plf);
156 Platform* SetupEditDialog::getSelectedPlatform() const
159 int index = platformComBox->currentIndex();
160 qDebug() << "Platform from index " << index << " selected";
163 QSqlQueryModel* platformModel
164 = dynamic_cast<QSqlQueryModel*>(platformComBox->model());
167 qDebug() << "No platform model";
170 QSqlRecord rec = platformModel->record(index);
173 qDebug() << "Trying to fetch platform with id "
174 << rec.value(DbPlatform::EmuFrontFileObject_Id).toInt() ;
176 = dbPlatform->getDataObject(rec.value(DbPlatform::EmuFrontFileObject_Id).toInt());
177 plf = dynamic_cast<Platform*>(o);
180 EmuFrontObject *o = platformComBox->getSelected();
182 Platform *plf = dynamic_cast<Platform*>(o);
186 MediaType* SetupEditDialog::getSelectedMediaType() const
189 int index = mediaTypeComBox->currentIndex();
190 if (index < 0) return mt;
191 QSqlQueryModel* mediaTypeModel = dynamic_cast<QSqlQueryModel*>(mediaTypeComBox->model());
194 qDebug("Media type data model missing");
197 QSqlRecord rec = mediaTypeModel->record(index);
200 EmuFrontObject *o = dbMediaType->getDataObject(rec.value(DbMediaType::EmuFrontFileObject_Id).toInt());
201 mt = dynamic_cast<MediaType*>(o);
204 EmuFrontObject *o = mediaTypeComBox->getSelected();
206 MediaType *mt = dynamic_cast<MediaType*>(o);
210 void SetupEditDialog::updateData()
212 qDebug() << "Updating combobox data";
213 platformComBox->updateDataModel();
214 mediaTypeComBox->updateDataModel();