Added new ComboBox child classes. Updated SetupEditDialog to use these.
[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 "../widgets/effileobjectcombobox.h"
26 #include "../db/dbmediatype.h"
27 #include "../db/dbplatform.h"
28 #include "../db/dbsetup.h"
29
30 SetupEditDialog::SetupEditDialog(QWidget *parent, EmuFrontObject* obj)
31     : DataObjectEditDialog(parent, obj)
32 {
33     dbSetup = 0; //new DbSetup(this);
34     dbPlatform = new DbPlatform(this);
35     dbMediaType = new DbMediaType(this);
36     initWidgets();
37     qDebug() << "Connecting signals";
38     connectSignals();
39     layout();
40 }
41
42 void SetupEditDialog::initWidgets()
43 {
44     mediaTypeComBox = new EFFileObjectComboBox(dbMediaType, this); //new QComboBox;
45     platformComBox = new EFFileObjectComboBox(dbPlatform, this); //new QComboBox;
46     supportedFileTypesList = new StringListWidget;
47     //populateMediaTypeComBox();
48     //populatePlatformComBox();
49 }
50
51 /*void SetupEditDialog::populateMediaTypeComBox()
52 {
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();
57     if (!model) return;
58     qDebug() << "We have a media type data model";
59     mediaTypeComBox->setModel(model);
60     mediaTypeComBox->setModelColumn(DbMediaType::EmuFrontFileObject_Name);
61 }
62
63 void SetupEditDialog::populatePlatformComBox()
64 {
65     qDebug() << "populating platform combo box";
66     if (!dbPlatform) dbPlatform = new DbPlatform(this);
67     QSqlQueryModel *model = dbPlatform->getDataModel();
68     if (!model) return;
69     platformComBox->setModel(model);
70     platformComBox->setModelColumn(DbPlatform::EmuFrontFileObject_Name);
71     qDebug() << "platform combo box populated";
72 }*/
73
74 void SetupEditDialog::layout()
75 {
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";
92
93    setWindowTitle(tr("Edit setup"));
94 }
95
96 void SetupEditDialog::acceptChanges()
97 {
98     Setup *sup = dynamic_cast<Setup*>(efObject);
99     Platform *plf = getSelectedPlatform();
100     if (!plf)
101     {
102         QMessageBox::information(this, tr("Platform"), tr("Platform not selected"), QMessageBox::Ok);
103         return;
104     }
105     qDebug() << "Platform selected " << plf->getName();
106     MediaType *mt = getSelectedMediaType();
107     if (!mt)
108     {
109         QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
110         return;
111     }
112     qDebug() << "Media type selected " << mt->getName();
113
114
115     Platform *ptmp = sup->getPlatform();
116     if (plf != ptmp)
117     {
118         delete ptmp;
119         sup->setPlatform(plf);
120     }
121     MediaType *mtmp = sup->getMediaType();
122     if (mt != mtmp)
123     {
124         delete mtmp;
125         sup->setMediaType(mt);
126     }
127     sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
128     emit dataObjectUpdated();
129     efObject = 0;
130     qDebug() << "Closing setup edit dialog";
131     close();
132 }
133
134 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
135 {
136     if (!ob) return;
137     efObject = 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());
142 }
143
144 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
145 {
146     //setSelected(platformComBox, plf, DbPlatform::EmuFrontFileObject_Id);
147     platformComBox->setSelected(plf);
148 }
149
150 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
151 {
152     //setSelected(mediaTypeComBox, plf, DbMediaType::EmuFrontFileObject_Id);
153     mediaTypeComBox->setSelected(plf);
154 }
155
156 Platform* SetupEditDialog::getSelectedPlatform() const
157 {
158     /*Platform *plf = 0;
159     int index = platformComBox->currentIndex();
160     qDebug() << "Platform from index " << index << " selected";
161     if (index < 0)
162         return plf;
163     QSqlQueryModel* platformModel
164         = dynamic_cast<QSqlQueryModel*>(platformComBox->model());
165     if (!platformModel)
166     {
167         qDebug() << "No platform model";
168         return plf;
169     }
170     QSqlRecord rec = platformModel->record(index);
171     if (!rec.isEmpty())
172     {
173         qDebug() << "Trying to fetch platform with id "
174             << rec.value(DbPlatform::EmuFrontFileObject_Id).toInt() ;
175         EmuFrontObject *o
176             = dbPlatform->getDataObject(rec.value(DbPlatform::EmuFrontFileObject_Id).toInt());
177         plf = dynamic_cast<Platform*>(o);
178     }*/
179
180     EmuFrontObject *o = platformComBox->getSelected();
181     if (!o) return 0;
182     Platform *plf = dynamic_cast<Platform*>(o);
183     return plf;
184 }
185
186 MediaType* SetupEditDialog::getSelectedMediaType() const
187 {
188     /*MediaType *mt = 0;
189     int index = mediaTypeComBox->currentIndex();
190     if (index < 0) return mt;
191     QSqlQueryModel* mediaTypeModel = dynamic_cast<QSqlQueryModel*>(mediaTypeComBox->model());
192     if (!mediaTypeModel)
193     {
194         qDebug("Media type data model missing");
195         return mt;
196     }
197     QSqlRecord rec = mediaTypeModel->record(index);
198     if (!rec.isEmpty())
199     {
200         EmuFrontObject *o = dbMediaType->getDataObject(rec.value(DbMediaType::EmuFrontFileObject_Id).toInt());
201         mt = dynamic_cast<MediaType*>(o);
202     }
203     return mt;*/
204     EmuFrontObject *o = mediaTypeComBox->getSelected();
205     if (!o) return 0;
206     MediaType *mt = dynamic_cast<MediaType*>(o);
207     return mt;
208 }
209
210 void SetupEditDialog::updateData()
211 {
212     qDebug() << "Updating combobox data";
213     platformComBox->updateDataModel();
214     mediaTypeComBox->updateDataModel();
215 }