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