Fixing a bug with selection combo box not updating when editin an
[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 version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
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;
34     dbPlatform = new DbPlatform(this);
35     dbMediaType = new DbMediaType(this);
36     initWidgets();
37     connectSignals();
38     layout();
39 }
40
41 void SetupEditDialog::initWidgets()
42 {
43     mediaTypeComBox = new EFFileObjectComboBox(dbMediaType, this);
44     platformComBox = new EFFileObjectComboBox(dbPlatform, this);
45     supportedFileTypesList = new StringListWidget;
46 }
47
48 void SetupEditDialog::layout()
49 {
50    QLabel *platformLabel = new QLabel(tr("&Platform"));
51    platformLabel->setBuddy(platformComBox);
52    QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
53    mediaTypeLabel->setBuddy(mediaTypeComBox);
54    QGridLayout *gridLayout = new QGridLayout;
55    gridLayout->addWidget(platformLabel, 0, 0);
56    gridLayout->addWidget(platformComBox, 0, 1);
57    gridLayout->addWidget(mediaTypeLabel, 1, 0);
58    gridLayout->addWidget(mediaTypeComBox, 1, 1);
59    gridLayout->addWidget(supportedFileTypesList, 2, 0, 2, 2);
60    QVBoxLayout *mainLayout = new QVBoxLayout;
61    mainLayout->addLayout(gridLayout);
62    mainLayout->addWidget(buttonBox);
63    setLayout(mainLayout);
64    setWindowTitle(tr("Edit setup"));
65 }
66
67 void SetupEditDialog::acceptChanges()
68 {
69     Setup *sup = dynamic_cast<Setup*>(efObject);
70     Platform *plf = getSelectedPlatform();
71     if (!plf)
72     {
73         QMessageBox::information(this, tr("Platform"),
74             tr("Platform not selected"), QMessageBox::Ok);
75         return;
76     }
77     qDebug() << "Platform selected " << plf->getName();
78     MediaType *mt = getSelectedMediaType();
79     if (!mt)
80     {
81         QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
82         return;
83     }
84     qDebug() << "Media type selected " << mt->getName();
85
86
87     Platform *ptmp = sup->getPlatform();
88     if (plf != ptmp)
89     {
90         delete ptmp;
91         sup->setPlatform(plf);
92     }
93     MediaType *mtmp = sup->getMediaType();
94     if (mt != mtmp)
95     {
96         delete mtmp;
97         sup->setMediaType(mt);
98     }
99     sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
100     emit dataObjectUpdated();
101     efObject = 0;
102     qDebug() << "Closing setup edit dialog";
103     close();
104 }
105
106 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
107 {
108     if (!ob) return;
109     qDebug() << "Updating data object with " << ob->getName() << ".";
110     efObject = ob;
111     Setup *sup= dynamic_cast<Setup*>(ob);
112     if (sup->getPlatform()) setSelectedPlatform(sup->getPlatform());
113     if (sup->getMediaType()) setSelectedMediaType(sup->getMediaType());
114     supportedFileTypesList->setItems(sup->getSupportedFileTypeExtensions());
115 }
116
117 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
118 {
119     platformComBox->setSelected(plf);
120 }
121
122 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
123 {
124     mediaTypeComBox->setSelected(plf);
125 }
126
127 Platform* SetupEditDialog::getSelectedPlatform() const
128 {
129     EmuFrontObject *o = platformComBox->getSelected();
130     if (!o) return 0;
131     Platform *plf = dynamic_cast<Platform*>(o);
132     return plf;
133 }
134
135 MediaType* SetupEditDialog::getSelectedMediaType() const
136 {
137     EmuFrontObject *o = mediaTypeComBox->getSelected();
138     if (!o) return 0;
139     MediaType *mt = dynamic_cast<MediaType*>(o);
140     return mt;
141 }
142
143 void SetupEditDialog::updateData()
144 {
145     platformComBox->updateDataModel();
146     mediaTypeComBox->updateDataModel();
147 }