Style updates and a small introduction.
[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 }
48
49 void SetupEditDialog::layout()
50 {
51     qDebug() << "SetupEditDialog::layout";
52    QLabel *platformLabel = new QLabel(tr("&Platform"));
53    platformLabel->setBuddy(platformComBox);
54    QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
55    mediaTypeLabel->setBuddy(mediaTypeComBox);
56    QGridLayout *gridLayout = new QGridLayout;
57    gridLayout->addWidget(platformLabel, 0, 0);
58    gridLayout->addWidget(platformComBox, 0, 1);
59    gridLayout->addWidget(mediaTypeLabel, 1, 0);
60    gridLayout->addWidget(mediaTypeComBox, 1, 1);
61    gridLayout->addWidget(supportedFileTypesList, 2, 0, 2, 2);
62    QVBoxLayout *mainLayout = new QVBoxLayout;
63    mainLayout->addLayout(gridLayout);
64    mainLayout->addWidget(buttonBox);
65    setLayout(mainLayout);
66     qDebug() << "SetupEditDialog::layout done";
67
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"), tr("Platform not selected"), QMessageBox::Ok);
78         return;
79     }
80     qDebug() << "Platform selected " << plf->getName();
81     MediaType *mt = getSelectedMediaType();
82     if (!mt)
83     {
84         QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
85         return;
86     }
87     qDebug() << "Media type selected " << mt->getName();
88
89
90     Platform *ptmp = sup->getPlatform();
91     if (plf != ptmp)
92     {
93         delete ptmp;
94         sup->setPlatform(plf);
95     }
96     MediaType *mtmp = sup->getMediaType();
97     if (mt != mtmp)
98     {
99         delete mtmp;
100         sup->setMediaType(mt);
101     }
102     sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
103     emit dataObjectUpdated();
104     efObject = 0;
105     qDebug() << "Closing setup edit dialog";
106     close();
107 }
108
109 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
110 {
111     if (!ob) return;
112     efObject = ob;
113     Setup *sup= dynamic_cast<Setup*>(ob);
114     if (sup->getPlatform()) setSelectedPlatform(sup->getPlatform());
115     if (sup->getMediaType()) setSelectedMediaType(sup->getMediaType());
116     supportedFileTypesList->setItems(sup->getSupportedFileTypeExtensions());
117 }
118
119 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
120 {
121     //setSelected(platformComBox, plf, DbPlatform::EmuFrontFileObject_Id);
122     platformComBox->setSelected(plf);
123 }
124
125 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
126 {
127     //setSelected(mediaTypeComBox, plf, DbMediaType::EmuFrontFileObject_Id);
128     mediaTypeComBox->setSelected(plf);
129 }
130
131 Platform* SetupEditDialog::getSelectedPlatform() const
132 {
133     /*Platform *plf = 0;
134     int index = platformComBox->currentIndex();
135     qDebug() << "Platform from index " << index << " selected";
136     if (index < 0)
137         return plf;
138     QSqlQueryModel* platformModel
139         = dynamic_cast<QSqlQueryModel*>(platformComBox->model());
140     if (!platformModel)
141     {
142         qDebug() << "No platform model";
143         return plf;
144     }
145     QSqlRecord rec = platformModel->record(index);
146     if (!rec.isEmpty())
147     {
148         qDebug() << "Trying to fetch platform with id "
149             << rec.value(DbPlatform::EmuFrontFileObject_Id).toInt() ;
150         EmuFrontObject *o
151             = dbPlatform->getDataObject(rec.value(DbPlatform::EmuFrontFileObject_Id).toInt());
152         plf = dynamic_cast<Platform*>(o);
153     }*/
154
155     EmuFrontObject *o = platformComBox->getSelected();
156     if (!o) return 0;
157     Platform *plf = dynamic_cast<Platform*>(o);
158     return plf;
159 }
160
161 MediaType* SetupEditDialog::getSelectedMediaType() const
162 {
163     /*MediaType *mt = 0;
164     int index = mediaTypeComBox->currentIndex();
165     if (index < 0) return mt;
166     QSqlQueryModel* mediaTypeModel = dynamic_cast<QSqlQueryModel*>(mediaTypeComBox->model());
167     if (!mediaTypeModel)
168     {
169         qDebug("Media type data model missing");
170         return mt;
171     }
172     QSqlRecord rec = mediaTypeModel->record(index);
173     if (!rec.isEmpty())
174     {
175         EmuFrontObject *o = dbMediaType->getDataObject(rec.value(DbMediaType::EmuFrontFileObject_Id).toInt());
176         mt = dynamic_cast<MediaType*>(o);
177     }
178     return mt;*/
179     EmuFrontObject *o = mediaTypeComBox->getSelected();
180     if (!o) return 0;
181     MediaType *mt = dynamic_cast<MediaType*>(o);
182     return mt;
183 }
184
185 void SetupEditDialog::updateData()
186 {
187     qDebug() << "Updating combobox data";
188     platformComBox->updateDataModel();
189     mediaTypeComBox->updateDataModel();
190 }