Media image path dialog initial implementation ready
[emufront] / src / dialogs / mediaimagepathdialog.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 // Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QtGui>
21 #include <QSqlTableModel>
22 #include <QSqlRecord>
23 #include "../db/dbplatform.h"
24 #include "../db/dbmediatype.h"
25 #include "mediaimagepathdialog.h"
26 #include "../dataobjects/platform.h"
27 #include "../dataobjects/mediatype.h"
28 #include "../dataobjects/filepathobject.h"
29
30 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
31     : DataObjectEditDialog(parent, efObject, Qt::Horizontal)
32 {
33     qDebug() << "Creating MediaImagePathDialog";
34     initWidgets();
35     populateMediaTypeComBox();
36     populatePlatformComBox();
37     connectSignals();
38     layout();
39 }
40
41 MediaImagePathDialog::~MediaImagePathDialog()
42 {
43     qDebug() << "Destroying MediaImagePathDialog";
44 }
45
46 void MediaImagePathDialog::connectSignals()
47 {
48     DataObjectEditDialog::connectSignals();
49     connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
50     qDebug() << "MediaImagePathDialog Connecting signals";
51 }
52
53 void MediaImagePathDialog::browseFilePath()
54 {
55     qDebug() << "Browse file path";
56     QString fpath = QFileDialog::getExistingDirectory(this, tr("Select a directory"), ".",
57         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
58     QDir d(fpath);
59     if (d.exists() && d.isReadable())
60     {
61         filePathLabel->setText(d.path());
62     }
63     qDebug() << fpath << " selected.";
64 }
65
66 void MediaImagePathDialog::initWidgets()
67 {
68     qDebug() << "MediaImagePathDialog initializing widgets.";
69     // these widgets will be automatically parented using layout components
70     filePathLabel = new QLabel;
71     filePathButton = new QPushButton(tr("&Browse filepath"));
72     mediaTypeComBox = new QComboBox;
73     platformComBox = new QComboBox;
74 }
75
76 void MediaImagePathDialog::populateMediaTypeComBox()
77 {
78     qDebug() << "MediaImagePathDialog populating media types combo box";
79     dbMediaType = new DbMediaType(this);
80     mediaTypeComBox->setModel(dbMediaType->getDataModel());
81     mediaTypeComBox->setModelColumn(DbMediaType::MediaType_Name);
82 }
83
84 void MediaImagePathDialog::populatePlatformComBox()
85 {
86     qDebug() << "MediaImagePathDialog populating platform combo box";
87     dbPlatform = new DbPlatform(this);
88     platformComBox->setModel(dbPlatform->getDataModel());
89     platformComBox->setModelColumn(DbPlatform::Platform_Name);
90 }
91
92 void MediaImagePathDialog::layout()
93 {
94     qDebug() << "MediaImagePathDialog setting layout";
95    QLabel *platformLabel = new QLabel(tr("&Platform"));
96    platformLabel->setBuddy(platformComBox);
97    QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
98    mediaTypeLabel->setBuddy(mediaTypeComBox);
99
100    QGridLayout *gridLayout = new QGridLayout;
101    gridLayout->addWidget(platformLabel, 0, 0);
102    gridLayout->addWidget(platformComBox, 0, 1);
103    gridLayout->addWidget(mediaTypeLabel, 1, 0);
104    gridLayout->addWidget(mediaTypeComBox, 1, 1);
105    gridLayout->addWidget(filePathButton, 2, 0);
106    gridLayout->addWidget(filePathLabel, 2, 1);
107    QVBoxLayout *mainLayout = new QVBoxLayout;
108    mainLayout->addLayout(gridLayout);
109    mainLayout->addWidget(buttonBox);
110    setLayout(mainLayout);
111
112    setWindowTitle(tr("Set media image paths"));
113 }
114
115 void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
116 {
117     if (!ob) return;
118     efObject = ob;
119     FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
120     QString fpath = fpo->getName();
121     filePathLabel->setText(fpath);
122     if (fpo->getPlatform()) setSelectedPlatform(fpo->getPlatform());
123     if (fpo->getMediaType()) setSelectedMediaType(fpo->getMediaType());
124 }
125
126 void MediaImagePathDialog::setSelectedPlatform(const Platform *plf)
127 {
128     setSelected(platformComBox, plf, DbPlatform::Platform_Id);
129 }
130
131 void MediaImagePathDialog::setSelectedMediaType(const MediaType *plf)
132 {
133     setSelected(mediaTypeComBox, plf, DbMediaType::MediaType_Id);
134 }
135
136 // TODO: this might be useful to lever to upper classes
137 void MediaImagePathDialog::setSelected(QComboBox *cbox, const EmuFrontObject *ob, int idIndex)
138 {
139     if (!ob) return;
140     QSqlTableModel *model = dynamic_cast<QSqlTableModel*>(cbox->model());
141     for (int i = 0; i < model->rowCount(); ++i)
142     {
143         QSqlRecord rec = model->record(i);
144         if (rec.value(idIndex) == ob->getId())
145         {
146             cbox->setCurrentIndex(i);
147             break;
148         }
149     }
150 }
151
152 Platform* MediaImagePathDialog::getSelectedPlatform() const
153 {
154     qDebug() << "MediaImagePathDialog Selecting platform";
155     Platform *plf = 0;
156     int index = platformComBox->currentIndex();
157     qDebug() << "Current index " << index;
158     if (index < 0) return plf;
159     QSqlTableModel* platformModel = dynamic_cast<QSqlTableModel*>(platformComBox->model());
160     if (!platformModel)
161     {
162         qDebug() << "Data model missing";
163         return plf;
164     }
165     QSqlRecord rec = platformModel->record(index);
166     if (!rec.isEmpty())
167     {
168         qDebug() << "We have a record";
169         plf = new Platform(rec.value(DbPlatform::Platform_Id).toInt(),
170         rec.value(DbPlatform::Platform_Name).toString(),
171         rec.value(DbPlatform::Platform_Filename).toString());
172     }
173     else qDebug() << "Record missing";
174     return plf;
175 }
176
177 MediaType* MediaImagePathDialog::getSelectedMediaType() const
178 {
179     MediaType *mt = 0;
180     int index = mediaTypeComBox->currentIndex();
181     if (index < 0) return mt;
182     QSqlTableModel* mediaTypeModel = dynamic_cast<QSqlTableModel*>(mediaTypeComBox->model());
183     if (!mediaTypeModel)
184     {
185         qDebug("Media type data model missing");
186         return mt;
187     }
188     QSqlRecord rec = mediaTypeModel->record(index);
189     if (!rec.isEmpty())
190         mt = new MediaType(rec.value(DbMediaType::MediaType_Id).toInt(),
191                            rec.value(DbMediaType::MediaType_Name).toString(),
192                            rec.value(DbMediaType::MediaType_Filename).toString());
193     return mt;
194 }
195
196 void MediaImagePathDialog::acceptChanges()
197 {
198     qDebug() << "Changes accepted";
199     FilePathObject *fpo = dynamic_cast<FilePathObject*>(efObject);
200     Platform *plf = getSelectedPlatform();
201     if (!plf)
202     {
203         QMessageBox::information(this, tr("Platform"), tr("Platform not selected"), QMessageBox::Ok);
204         return;
205     }
206     qDebug() << "Platform selected " << plf->getName();
207     MediaType *mt = getSelectedMediaType();
208     if (!mt)
209     {
210         QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
211         return;
212     }
213     qDebug() << "Media type selected " << mt->getName();
214     QString filePath = filePathLabel->text();
215     if (filePath.isEmpty())
216     {
217         QMessageBox::information(this, tr("File path"), tr("File path was not selected"), QMessageBox::Ok);
218         return;
219     }
220     qDebug() << "File path selected " << filePath;
221     Platform *ptmp = fpo->getPlatform();
222     if (plf != ptmp)
223     {
224         delete ptmp;
225         fpo->setPlatform(plf);
226     }
227     MediaType *mtmp = fpo->getMediaType();
228     if (mt != mtmp)
229     {
230         delete mtmp;
231         fpo->setMediaType(mt);
232     }
233     fpo->setName(filePath);
234     emit dataObjectUpdated();
235     efObject = 0;
236     close();
237 }
238
239 void MediaImagePathDialog::rejectChanges()
240 {
241     // we don't delete the data object here
242     efObject = 0;
243     emit updateRejected();
244     close();
245 }