Enabled supported file types widget.
[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    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";
91
92    setWindowTitle(tr("Edit setup"));
93 }
94
95 void SetupEditDialog::acceptChanges()
96 {
97     Setup *sup = dynamic_cast<Setup*>(efObject);
98     Platform *plf = getSelectedPlatform();
99     if (!plf)
100     {
101         QMessageBox::information(this, tr("Platform"), tr("Platform not selected"), QMessageBox::Ok);
102         return;
103     }
104     qDebug() << "Platform selected " << plf->getName();
105     MediaType *mt = getSelectedMediaType();
106     if (!mt)
107     {
108         QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
109         return;
110     }
111     qDebug() << "Media type selected " << mt->getName();
112
113
114     Platform *ptmp = sup->getPlatform();
115     if (plf != ptmp)
116     {
117         delete ptmp;
118         sup->setPlatform(plf);
119     }
120     MediaType *mtmp = sup->getMediaType();
121     if (mt != mtmp)
122     {
123         delete mtmp;
124         sup->setMediaType(mt);
125     }
126     sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
127     emit dataObjectUpdated();
128     efObject = 0;
129     close();
130 }
131
132 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
133 {
134     if (!ob) return;
135     efObject = ob;
136     Setup *sup= dynamic_cast<Setup*>(ob);
137     if (sup->getPlatform()) setSelectedPlatform(sup->getPlatform());
138     if (sup->getMediaType()) setSelectedMediaType(sup->getMediaType());
139     supportedFileTypesList->setItems(sup->getSupportedFileTypeExtensions());
140 }
141
142 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
143 {
144     setSelected(platformComBox, plf, DbPlatform::Platform_Id);
145 }
146
147 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
148 {
149     setSelected(mediaTypeComBox, plf, DbMediaType::MediaType_Id);
150 }
151
152 Platform* SetupEditDialog::getSelectedPlatform() const
153 {
154     qDebug() << "MediaImagePathDialog Selecting platform";
155     Platform *plf = 0;
156     int index = platformComBox->currentIndex();
157     qDebug() << "Current index " << index;
158     if (index < 0) return plf;
159     QSqlTableModel* platformModel = dynamic_cast<QSqlTableModel*>(platformComBox->model());
160     if (!platformModel)
161     {
162         qDebug() << "Data model missing";
163         return plf;
164     }
165     QSqlRecord rec = platformModel->record(index);
166     if (!rec.isEmpty())
167     {
168         qDebug() << "We have a record";
169         plf = new Platform(rec.value(DbPlatform::Platform_Id).toInt(),
170         rec.value(DbPlatform::Platform_Name).toString(),
171         rec.value(DbPlatform::Platform_Filename).toString());
172     }
173     else qDebug() << "Record missing";
174     return plf;
175 }
176
177 MediaType* SetupEditDialog::getSelectedMediaType() const
178 {
179     MediaType *mt = 0;
180     int index = mediaTypeComBox->currentIndex();
181     if (index < 0) return mt;
182     QSqlTableModel* mediaTypeModel = dynamic_cast<QSqlTableModel*>(mediaTypeComBox->model());
183     if (!mediaTypeModel)
184     {
185         qDebug("Media type data model missing");
186         return mt;
187     }
188     QSqlRecord rec = mediaTypeModel->record(index);
189     if (!rec.isEmpty())
190         mt = new MediaType(rec.value(DbMediaType::MediaType_Id).toInt(),
191                            rec.value(DbMediaType::MediaType_Name).toString(),
192                            rec.value(DbMediaType::MediaType_Filename).toString());
193     return mt;
194 }