Initial implementation of setup editor. Some refactoring and bug hunting
[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 "../db/dbsetup.h"
26 #include "mediaimagepathdialog.h"
27 #include "../dataobjects/filepathobject.h"
28
29 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
30     : DataObjectEditDialog(parent, efObject, Qt::Horizontal)
31 {
32     qDebug() << "Creating MediaImagePathDialog";
33     initWidgets();
34     dbPlatform = 0;
35     dbMediaType = 0;
36     connectSignals();
37     layout();
38 }
39
40 MediaImagePathDialog::~MediaImagePathDialog()
41 {
42     qDebug() << "Destroying MediaImagePathDialog";
43 }
44
45 void MediaImagePathDialog::connectSignals()
46 {
47     DataObjectEditDialog::connectSignals();
48     connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
49     qDebug() << "MediaImagePathDialog Connecting signals";
50 }
51
52 void MediaImagePathDialog::browseFilePath()
53 {
54     qDebug() << "Browse file path";
55     QString fpath = QFileDialog::getExistingDirectory(this, tr("Select a directory"), ".",
56         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
57     QDir d(fpath);
58     if (d.exists() && d.isReadable())
59     {
60         filePathLabel->setText(d.path());
61     }
62     qDebug() << fpath << " selected.";
63 }
64
65 void MediaImagePathDialog::initWidgets()
66 {
67     qDebug() << "MediaImagePathDialog initializing widgets.";
68     // these widgets will be automatically parented using layout components
69     filePathLabel = new QLabel;
70     filePathButton = new QPushButton(tr("&Browse filepath"));
71      setupComBox = new QComboBox;
72 }
73
74 void MediaImagePathDialog::populateSetupComBox()
75 {
76     qDebug() << "MediaImagePathDialog populating media types combo box";
77     dbSetup = new DbSetup(this);
78     setupComBox->setModel(dbSetup->getDataModel());
79     setupComBox->setModelColumn(DbSetup::Setup_Name);
80 }
81
82 void MediaImagePathDialog::layout()
83 {
84     qDebug() << "MediaImagePathDialog setting layout";
85    QLabel *setupLabel = new QLabel(tr("&Set up"));
86    setupLabel->setBuddy(setupComBox);
87
88    QGridLayout *gridLayout = new QGridLayout;
89    /*gridLayout->addWidget(platformLabel, 0, 0);
90    gridLayout->addWidget(platformComBox, 0, 1);
91    gridLayout->addWidget(mediaTypeLabel, 1, 0);
92    gridLayout->addWidget(mediaTypeComBox, 1, 1);*/
93    gridLayout->addWidget(setupLabel, 0, 0);
94    gridLayout->addWidget(setupComBox, 0, 1);
95    gridLayout->addWidget(filePathButton, 1, 0);
96    gridLayout->addWidget(filePathLabel, 1, 1);
97    QVBoxLayout *mainLayout = new QVBoxLayout;
98    mainLayout->addLayout(gridLayout);
99    mainLayout->addWidget(buttonBox);
100    setLayout(mainLayout);
101
102    setWindowTitle(tr("Set media image paths"));
103 }
104
105 void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
106 {
107     if (!ob) return;
108     efObject = ob;
109     FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
110     QString fpath = fpo->getName();
111     filePathLabel->setText(fpath);
112     if (fpo->getSetup()) setSelectedSetup(fpo->getSetup());
113 }
114
115 void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
116 {
117     setSelected(setupComBox, sup, DbSetup::Setup_Id);
118 }
119
120 Setup* MediaImagePathDialog::getSelectedSetup()
121 {
122     if (!dbPlatform) dbPlatform = new DbPlatform(this);
123     if (!dbMediaType) dbMediaType = new DbMediaType(this);
124     qDebug() << "MediaImagePathDialog Selecting setup";
125     Setup *sup = 0;
126     int index = setupComBox->currentIndex();
127     qDebug() << "Current index " << index;
128     if (index < 0) return sup;
129     QSqlQueryModel *model
130         = dynamic_cast<QSqlQueryModel*>(setupComBox->model());
131     if (!model)
132     {
133         qDebug() << "Data model missing";
134         return sup;
135     }
136     QSqlRecord rec = model->record(index);
137     if (!rec.isEmpty())
138     {
139         qDebug() << "We have a record";
140         EmuFrontObject *efPlf = dbPlatform->getDataObject(rec.value(DbSetup::Setup_PlatformId).toInt());
141         EmuFrontObject *efMt = dbMediaType->getDataObject(rec.value(DbSetup::Setup_MediaTypeId).toInt());
142
143         Platform *plf = dynamic_cast<Platform*>(efPlf);
144         MediaType *mt = dynamic_cast<MediaType*>(efMt);
145         QString exts = rec.value(DbSetup::Setup_FileTypeExtensions).toString();
146
147         sup = new Setup(rec.value(DbSetup::Setup_Id).toInt(), plf, mt,
148             exts.split(DbSetup::FILE_TYPE_EXTENSION_SEPARATOR));
149     }
150     else qDebug() << "Record missing";
151     return sup;
152 }
153
154 void MediaImagePathDialog::acceptChanges()
155 {
156     qDebug() << "Changes accepted";
157     FilePathObject *fpo = dynamic_cast<FilePathObject*>(efObject);
158     Setup *sup = getSelectedSetup();
159     if (!sup)
160     {
161         QMessageBox::information(this, tr("Set up"), tr("Set up not selected"), QMessageBox::Ok);
162         return;
163     }
164     qDebug() << "Setup selected " << sup->getName();
165     QString filePath = filePathLabel->text();
166     if (filePath.isEmpty())
167     {
168         QMessageBox::information(this, tr("File path"), tr("File path was not selected"), QMessageBox::Ok);
169         return;
170     }
171     qDebug() << "File path selected " << filePath;
172     Setup *tmp = fpo->getSetup();
173     if (sup != tmp)
174     {
175         delete tmp;
176         fpo->setSetup(sup);
177     }
178     fpo->setName(filePath);
179     emit dataObjectUpdated();
180     efObject = 0;
181     close();
182 }