EmuFront ... NOT Foobar :D
[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 // 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.
16 //
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/>.
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::EmuFrontFileObject_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::EmuFrontFileObject_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     qDebug() << "Closing setup edit dialog";
130     close();
131 }
132
133 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
134 {
135     if (!ob) return;
136     efObject = 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());
141 }
142
143 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
144 {
145     setSelected(platformComBox, plf, DbPlatform::EmuFrontFileObject_Id);
146 }
147
148 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
149 {
150     setSelected(mediaTypeComBox, plf, DbMediaType::EmuFrontFileObject_Id);
151 }
152
153 Platform* SetupEditDialog::getSelectedPlatform() const
154 {
155     Platform *plf = 0;
156     int index = platformComBox->currentIndex();
157     if (index < 0) return plf;
158     QSqlTableModel* platformModel = dynamic_cast<QSqlTableModel*>(platformComBox->model());
159     if (!platformModel)
160     {
161         return plf;
162     }
163     QSqlRecord rec = platformModel->record(index);
164     if (!rec.isEmpty())
165     {
166         EmuFrontObject *o = dbPlatform->getDataObject(rec.value(DbPlatform::EmuFrontFileObject_Id).toInt());
167         plf = dynamic_cast<Platform*>(o);
168     }
169     return plf;
170 }
171
172 MediaType* SetupEditDialog::getSelectedMediaType() const
173 {
174     MediaType *mt = 0;
175     int index = mediaTypeComBox->currentIndex();
176     if (index < 0) return mt;
177     QSqlTableModel* mediaTypeModel = dynamic_cast<QSqlTableModel*>(mediaTypeComBox->model());
178     if (!mediaTypeModel)
179     {
180         qDebug("Media type data model missing");
181         return mt;
182     }
183     QSqlRecord rec = mediaTypeModel->record(index);
184     if (!rec.isEmpty())
185     {
186         EmuFrontObject *o = dbMediaType->getDataObject(rec.value(DbMediaType::EmuFrontFileObject_Id).toInt());
187         mt = dynamic_cast<MediaType*>(o);
188     }
189     return mt;
190 }