Updated the git clone command.
[emufront] / src / dialogs / setupeditdialog.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <QtGui>
22 #include <QSqlTableModel>
23 #include <QSqlRecord>
24 #include "setupeditdialog.h"
25 #include "fileextensionwidget.h"
26 #include "effileobjectcombobox.h"
27 #include "dbmediatype.h"
28 #include "dbplatform.h"
29 #include "dbsetup.h"
30
31 SetupEditDialog::SetupEditDialog(QWidget *parent, EmuFrontObject* obj)
32     : DataObjectEditDialog(parent, obj)
33 {
34     dbSetup = 0;
35     dbPlatform = new DbPlatform(this);
36     dbMediaType = new DbMediaType(this);
37     initWidgets();
38     connectSignals();
39     emit test();
40     layout();
41 }
42
43 void SetupEditDialog::initWidgets()
44 {
45     mediaTypeComBox = new EFFileObjectComboBox(dbMediaType, this);
46     platformComBox = new EFFileObjectComboBox(dbPlatform, this);
47     supportedFileTypesList = new FileExtensionWidget;
48     supportedFileTypesList->setToolTip(tr("Set the supported file types by entering the file extension(s) (no wildcards or dots!)."));
49 }
50
51 void SetupEditDialog::layout()
52 {
53    QLabel *platformLabel = new QLabel(tr("&Platform:"));
54    platformLabel->setBuddy(platformComBox);
55    QLabel *mediaTypeLabel = new QLabel(tr("Media&Type:"));
56    mediaTypeLabel->setBuddy(mediaTypeComBox);
57    QLabel *fileTypesLabel = new QLabel(tr("Supported file types:"));
58    QGridLayout *gridLayout = new QGridLayout;
59    gridLayout->addWidget(platformLabel, 0, 0);
60    gridLayout->addWidget(platformComBox, 0, 1);
61    gridLayout->addWidget(mediaTypeLabel, 1, 0);
62    gridLayout->addWidget(mediaTypeComBox, 1, 1);
63    gridLayout->addWidget(fileTypesLabel, 2, 0, 1, 2);
64    gridLayout->addWidget(supportedFileTypesList, 3, 0, 2, 2);
65    QVBoxLayout *mainLayout = new QVBoxLayout;
66    mainLayout->addLayout(gridLayout);
67    mainLayout->addWidget(buttonBox);
68    setLayout(mainLayout);
69    setWindowTitle(tr("Edit setup"));
70 }
71
72 void SetupEditDialog::acceptChanges()
73 {
74     Setup *sup = dynamic_cast<Setup*>(efObject);
75     Platform *plf = getSelectedPlatform();
76     if (!plf)
77     {
78         QMessageBox::information(this, tr("Platform"),
79             tr("Platform not selected"), QMessageBox::Ok);
80         return;
81     }
82     qDebug() << "Platform selected " << plf->getName();
83     MediaType *mt = getSelectedMediaType();
84     if (!mt)
85     {
86         QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
87         return;
88     }
89     qDebug() << "Media type selected " << mt->getName();
90
91
92     bool change = false;
93     Platform *ptmp = sup->getPlatform();
94     if (!ptmp || *plf != *ptmp)
95     {
96         delete ptmp;
97         sup->setPlatform(plf);
98         change = true;
99     }
100     MediaType *mtmp = sup->getMediaType();
101     if (!mtmp || *mt != *mtmp)
102     {
103         delete mtmp;
104         sup->setMediaType(mt);
105         change = true;
106     }
107
108     // "Two lists are considered equal if they contain the same values in the same order."
109     if (supportedFileTypesList->getItems() != sup->getSupportedFileTypeExtensions()) {
110         sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
111         change = true;
112     }
113     // TODO: the second time around this signal is not received!
114     if (change) emit dataObjectUpdated();
115     efObject = 0;
116     close();
117 }
118
119 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
120 {
121     if (!ob) return;
122     qDebug() << "Updating Setup edit dialog data object to "
123         << ob->getName() << ".";
124     if (efObject)
125         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.
126     efObject = ob;
127     Setup *sup= dynamic_cast<Setup*>(ob);
128     if (!sup) {
129         qDebug() << "Failed casting to Setup";
130         return;
131     }
132     if (sup->getPlatform()) setSelectedPlatform(sup->getPlatform());
133     if (sup->getMediaType()) setSelectedMediaType(sup->getMediaType());
134     supportedFileTypesList->setItems(sup->getSupportedFileTypeExtensions());
135 }
136
137 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
138 {
139     platformComBox->setSelected(plf);
140 }
141
142 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
143 {
144     mediaTypeComBox->setSelected(plf);
145 }
146
147 /* Returns a pointer to an object which must be deleted by calling code */
148 Platform* SetupEditDialog::getSelectedPlatform()
149 {
150     EmuFrontObject *o = 0;
151     try { o = platformComBox->getSelected();  }
152     catch(EmuFrontException &e){
153         errorMessage->showMessage(e.what());
154     }
155
156     if (!o) return 0;
157     Platform *plf = dynamic_cast<Platform*>(o);
158     return plf;
159 }
160
161 /* Returns a pointer to an object which must be deleted by calling code */
162 MediaType* SetupEditDialog::getSelectedMediaType()
163 {
164     EmuFrontObject *o = 0;
165     try { o = mediaTypeComBox->getSelected(); }
166     catch(EmuFrontException &e){ errorMessage->showMessage(e.what()); }
167     if (!o) return 0;
168     MediaType *mt = dynamic_cast<MediaType*>(o);
169     return mt;
170 }
171
172 void SetupEditDialog::updateData()
173 {
174     platformComBox->updateDataModel();
175     mediaTypeComBox->updateDataModel();
176 }
177
178 void SetupEditDialog::clear()
179 {
180     platformComBox->setCurrentIndex(-1);
181     mediaTypeComBox->setCurrentIndex(-1);
182     supportedFileTypesList->clear();
183 }