More exception handling.
[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/fileextensionwidget.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     emit test();
39     layout();
40 }
41
42 void SetupEditDialog::initWidgets()
43 {
44     mediaTypeComBox = new EFFileObjectComboBox(dbMediaType, this);
45     platformComBox = new EFFileObjectComboBox(dbPlatform, this);
46     supportedFileTypesList = new FileExtensionWidget;
47     supportedFileTypesList->setToolTip(tr("Set the supported file types by entering the file extension(s) (no wildcards or dots!)."));
48 }
49
50 void SetupEditDialog::layout()
51 {
52    QLabel *platformLabel = new QLabel(tr("&Platform:"));
53    platformLabel->setBuddy(platformComBox);
54    QLabel *mediaTypeLabel = new QLabel(tr("Media&Type:"));
55    mediaTypeLabel->setBuddy(mediaTypeComBox);
56    QLabel *fileTypesLabel = new QLabel(tr("Supported file types:"));
57    QGridLayout *gridLayout = new QGridLayout;
58    gridLayout->addWidget(platformLabel, 0, 0);
59    gridLayout->addWidget(platformComBox, 0, 1);
60    gridLayout->addWidget(mediaTypeLabel, 1, 0);
61    gridLayout->addWidget(mediaTypeComBox, 1, 1);
62    gridLayout->addWidget(fileTypesLabel, 2, 0, 1, 2);
63    gridLayout->addWidget(supportedFileTypesList, 3, 0, 2, 2);
64    QVBoxLayout *mainLayout = new QVBoxLayout;
65    mainLayout->addLayout(gridLayout);
66    mainLayout->addWidget(buttonBox);
67    setLayout(mainLayout);
68    setWindowTitle(tr("Edit setup"));
69 }
70
71 void SetupEditDialog::acceptChanges()
72 {
73     Setup *sup = dynamic_cast<Setup*>(efObject);
74     Platform *plf = getSelectedPlatform();
75     if (!plf)
76     {
77         QMessageBox::information(this, tr("Platform"),
78             tr("Platform not selected"), QMessageBox::Ok);
79         return;
80     }
81     qDebug() << "Platform selected " << plf->getName();
82     MediaType *mt = getSelectedMediaType();
83     if (!mt)
84     {
85         QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
86         return;
87     }
88     qDebug() << "Media type selected " << mt->getName();
89
90
91     bool change = false;
92     Platform *ptmp = sup->getPlatform();
93     if (!ptmp || *plf != *ptmp)
94     {
95         delete ptmp;
96         sup->setPlatform(plf);
97         change = true;
98     }
99     MediaType *mtmp = sup->getMediaType();
100     if (!mtmp || *mt != *mtmp)
101     {
102         delete mtmp;
103         sup->setMediaType(mt);
104         change = true;
105     }
106
107     // "Two lists are considered equal if they contain the same values in the same order."
108     if (supportedFileTypesList->getItems() != sup->getSupportedFileTypeExtensions()) {
109         sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
110         change = true;
111     }
112     // TODO: the second time around this signal is not received!
113     if (change) emit dataObjectUpdated();
114     efObject = 0;
115     close();
116 }
117
118 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
119 {
120     if (!ob) return;
121     qDebug() << "Updating Setup edit dialog data object to "
122         << ob->getName() << ".";
123     if (efObject)
124         delete dynamic_cast<Setup*>(efObject); // TODO: caused crash if another instance of setupeditdialog was created and new instance destroyed object being referenced in another existing dialog.
125     efObject = ob;
126     Setup *sup= dynamic_cast<Setup*>(ob);
127     if (!sup) {
128         qDebug() << "Failed casting to Setup";
129         return;
130     }
131     if (sup->getPlatform()) setSelectedPlatform(sup->getPlatform());
132     if (sup->getMediaType()) setSelectedMediaType(sup->getMediaType());
133     supportedFileTypesList->setItems(sup->getSupportedFileTypeExtensions());
134 }
135
136 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
137 {
138     platformComBox->setSelected(plf);
139 }
140
141 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
142 {
143     mediaTypeComBox->setSelected(plf);
144 }
145
146 Platform* SetupEditDialog::getSelectedPlatform()
147 {
148     EmuFrontObject *o = 0;
149     try { o = platformComBox->getSelected();  }
150     catch(EmuFrontException &e){
151         errorMessage->showMessage(e.what());
152     }
153
154     if (!o) return 0;
155     Platform *plf = dynamic_cast<Platform*>(o);
156     return plf;
157 }
158
159 MediaType* SetupEditDialog::getSelectedMediaType()
160 {
161     EmuFrontObject *o = 0;
162     try { o = mediaTypeComBox->getSelected(); }
163     catch(EmuFrontException &e){ errorMessage->showMessage(e.what()); }
164     if (!o) return 0;
165     MediaType *mt = dynamic_cast<MediaType*>(o);
166     return mt;
167 }
168
169 void SetupEditDialog::updateData()
170 {
171     platformComBox->updateDataModel();
172     mediaTypeComBox->updateDataModel();
173 }