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 // 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.
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/>.
21 #include <QSqlTableModel>
23 #include "mediaimagepathdialog.h"
24 #include "../db/dbsetup.h"
25 #include "../dataobjects/filepathobject.h"
27 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
28 : DataObjectEditDialog(parent, efObject, Qt::Horizontal)
37 MediaImagePathDialog::~MediaImagePathDialog()
41 void MediaImagePathDialog::connectSignals()
43 DataObjectEditDialog::connectSignals();
44 connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
47 void MediaImagePathDialog::browseFilePath()
49 QString fpath = QFileDialog::getExistingDirectory(this, tr("Select a directory"), ".",
50 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
52 if (d.exists() && d.isReadable())
54 filePathLabel->setText(d.path());
58 void MediaImagePathDialog::initWidgets()
60 // these widgets will be automatically parented using layout components
61 filePathLabel = new QLabel;
62 filePathButton = new QPushButton(tr("&Browse filepath"));
63 setupComBox = new QComboBox;
64 populateSetupComBox();
67 void MediaImagePathDialog::populateSetupComBox()
69 dbSetup = new DbSetup(this);
70 setupComBox->setModel(dbSetup->getDataModel());
71 setupComBox->setModelColumn(DbSetup::Setup_Name);
74 void MediaImagePathDialog::layout()
76 QLabel *setupLabel = new QLabel(tr("&Set up"));
77 setupLabel->setBuddy(setupComBox);
79 QGridLayout *gridLayout = new QGridLayout;
80 gridLayout->addWidget(setupLabel, 0, 0);
81 gridLayout->addWidget(setupComBox, 0, 1);
82 gridLayout->addWidget(filePathButton, 1, 0);
83 gridLayout->addWidget(filePathLabel, 1, 1);
84 QVBoxLayout *mainLayout = new QVBoxLayout;
85 mainLayout->addLayout(gridLayout);
86 mainLayout->addWidget(buttonBox);
87 setLayout(mainLayout);
89 setWindowTitle(tr("Set media image paths"));
92 void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
96 FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
97 QString fpath = fpo->getName();
98 filePathLabel->setText(fpath);
99 if (fpo->getSetup()) setSelectedSetup(fpo->getSetup());
102 void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
104 setSelected(setupComBox, sup, DbSetup::Setup_Id);
107 Setup* MediaImagePathDialog::getSelectedSetup()
109 if (!dbPlatform) dbPlatform = new DbPlatform(this);
110 if (!dbMediaType) dbMediaType = new DbMediaType(this);
112 int index = setupComBox->currentIndex();
113 if (index < 0) return sup;
114 QSqlQueryModel *model
115 = dynamic_cast<QSqlQueryModel*>(setupComBox->model());
118 errorMessage->showMessage(tr("Data model missing."));
121 QSqlRecord rec = model->record(index);
124 EmuFrontObject *efPlf = dbPlatform->getDataObject(rec.value(DbSetup::Setup_PlatformId).toInt());
125 EmuFrontObject *efMt = dbMediaType->getDataObject(rec.value(DbSetup::Setup_MediaTypeId).toInt());
127 Platform *plf = dynamic_cast<Platform*>(efPlf);
128 MediaType *mt = dynamic_cast<MediaType*>(efMt);
129 QString exts = rec.value(DbSetup::Setup_FileTypeExtensions).toString();
131 sup = new Setup(rec.value(DbSetup::Setup_Id).toInt(), plf, mt,
132 exts.split(DbSetup::FILE_TYPE_EXTENSION_SEPARATOR));
134 else errorMessage->showMessage("Record missing");
138 void MediaImagePathDialog::acceptChanges()
140 FilePathObject *fpo = dynamic_cast<FilePathObject*>(efObject);
141 Setup *sup = getSelectedSetup();
144 QMessageBox::information(this, tr("Set up"), tr("Set up not selected"), QMessageBox::Ok);
147 qDebug() << "Setup selected " << sup->getName();
148 QString filePath = filePathLabel->text();
149 if (filePath.isEmpty())
151 QMessageBox::information(this, tr("File path"), tr("File path was not selected"), QMessageBox::Ok);
154 Setup *tmp = fpo->getSetup();
160 fpo->setName(filePath);
161 emit dataObjectUpdated();