2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
21 #include <QSqlTableModel>
23 #include "../db/dbplatform.h"
24 #include "../db/dbmediatype.h"
25 #include "../db/dbsetup.h"
26 #include "mediaimagepathdialog.h"
27 #include "../dataobjects/filepathobject.h"
29 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
30 : DataObjectEditDialog(parent, efObject, Qt::Horizontal)
32 qDebug() << "Creating MediaImagePathDialog";
40 MediaImagePathDialog::~MediaImagePathDialog()
42 qDebug() << "Destroying MediaImagePathDialog";
45 void MediaImagePathDialog::connectSignals()
47 DataObjectEditDialog::connectSignals();
48 connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
49 qDebug() << "MediaImagePathDialog Connecting signals";
52 void MediaImagePathDialog::browseFilePath()
54 qDebug() << "Browse file path";
55 QString fpath = QFileDialog::getExistingDirectory(this, tr("Select a directory"), ".",
56 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
58 if (d.exists() && d.isReadable())
60 filePathLabel->setText(d.path());
62 qDebug() << fpath << " selected.";
65 void MediaImagePathDialog::initWidgets()
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;
74 void MediaImagePathDialog::populateSetupComBox()
76 qDebug() << "MediaImagePathDialog populating media types combo box";
77 dbSetup = new DbSetup(this);
78 setupComBox->setModel(dbSetup->getDataModel());
79 setupComBox->setModelColumn(DbSetup::Setup_Name);
82 void MediaImagePathDialog::layout()
84 qDebug() << "MediaImagePathDialog setting layout";
85 QLabel *setupLabel = new QLabel(tr("&Set up"));
86 setupLabel->setBuddy(setupComBox);
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);
102 setWindowTitle(tr("Set media image paths"));
105 void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
109 FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
110 QString fpath = fpo->getName();
111 filePathLabel->setText(fpath);
112 if (fpo->getSetup()) setSelectedSetup(fpo->getSetup());
115 void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
117 setSelected(setupComBox, sup, DbSetup::Setup_Id);
120 Setup* MediaImagePathDialog::getSelectedSetup()
122 if (!dbPlatform) dbPlatform = new DbPlatform(this);
123 if (!dbMediaType) dbMediaType = new DbMediaType(this);
124 qDebug() << "MediaImagePathDialog Selecting setup";
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());
133 qDebug() << "Data model missing";
136 QSqlRecord rec = model->record(index);
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());
143 Platform *plf = dynamic_cast<Platform*>(efPlf);
144 MediaType *mt = dynamic_cast<MediaType*>(efMt);
145 QString exts = rec.value(DbSetup::Setup_FileTypeExtensions).toString();
147 sup = new Setup(rec.value(DbSetup::Setup_Id).toInt(), plf, mt,
148 exts.split(DbSetup::FILE_TYPE_EXTENSION_SEPARATOR));
150 else qDebug() << "Record missing";
154 void MediaImagePathDialog::acceptChanges()
156 qDebug() << "Changes accepted";
157 FilePathObject *fpo = dynamic_cast<FilePathObject*>(efObject);
158 Setup *sup = getSelectedSetup();
161 QMessageBox::information(this, tr("Set up"), tr("Set up not selected"), QMessageBox::Ok);
164 qDebug() << "Setup selected " << sup->getName();
165 QString filePath = filePathLabel->text();
166 if (filePath.isEmpty())
168 QMessageBox::information(this, tr("File path"), tr("File path was not selected"), QMessageBox::Ok);
171 qDebug() << "File path selected " << filePath;
172 Setup *tmp = fpo->getSetup();
178 fpo->setName(filePath);
179 emit dataObjectUpdated();