EmuFront ... NOT Foobar :D
[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 // 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
27 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
28     : DataObjectEditDialog(parent, efObject, Qt::Horizontal)
29 {
30     initWidgets();
31     dbPlatform = 0;
32     dbMediaType = 0;
33     connectSignals();
34     layout();
35 }
36
37 MediaImagePathDialog::~MediaImagePathDialog()
38 {
39 }
40
41 void MediaImagePathDialog::connectSignals()
42 {
43     DataObjectEditDialog::connectSignals();
44     connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
45 }
46
47 void MediaImagePathDialog::browseFilePath()
48 {
49     QString fpath = QFileDialog::getExistingDirectory(this, tr("Select a directory"), ".",
50         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
51     QDir d(fpath);
52     if (d.exists() && d.isReadable())
53     {
54         filePathLabel->setText(d.path());
55     }
56 }
57
58 void MediaImagePathDialog::initWidgets()
59 {
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();
65 }
66
67 void MediaImagePathDialog::populateSetupComBox()
68 {
69     dbSetup = new DbSetup(this);
70     setupComBox->setModel(dbSetup->getDataModel());
71     setupComBox->setModelColumn(DbSetup::Setup_Name);
72 }
73
74 void MediaImagePathDialog::layout()
75 {
76    QLabel *setupLabel = new QLabel(tr("&Set up"));
77    setupLabel->setBuddy(setupComBox);
78
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);
88
89    setWindowTitle(tr("Set media image paths"));
90 }
91
92 void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
93 {
94     if (!ob) return;
95     efObject = ob;
96     FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
97     QString fpath = fpo->getName();
98     filePathLabel->setText(fpath);
99     if (fpo->getSetup()) setSelectedSetup(fpo->getSetup());
100 }
101
102 void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
103 {
104     setSelected(setupComBox, sup, DbSetup::Setup_Id);
105 }
106
107 Setup* MediaImagePathDialog::getSelectedSetup()
108 {
109     if (!dbPlatform) dbPlatform = new DbPlatform(this);
110     if (!dbMediaType) dbMediaType = new DbMediaType(this);
111     Setup *sup = 0;
112     int index = setupComBox->currentIndex();
113     if (index < 0) return sup;
114     QSqlQueryModel *model
115         = dynamic_cast<QSqlQueryModel*>(setupComBox->model());
116     if (!model)
117     {
118         errorMessage->showMessage(tr("Data model missing."));
119         return sup;
120     }
121     QSqlRecord rec = model->record(index);
122     if (!rec.isEmpty())
123     {
124         EmuFrontObject *efPlf = dbPlatform->getDataObject(rec.value(DbSetup::Setup_PlatformId).toInt());
125         EmuFrontObject *efMt = dbMediaType->getDataObject(rec.value(DbSetup::Setup_MediaTypeId).toInt());
126
127         Platform *plf = dynamic_cast<Platform*>(efPlf);
128         MediaType *mt = dynamic_cast<MediaType*>(efMt);
129         QString exts = rec.value(DbSetup::Setup_FileTypeExtensions).toString();
130
131         sup = new Setup(rec.value(DbSetup::Setup_Id).toInt(), plf, mt,
132             exts.split(DbSetup::FILE_TYPE_EXTENSION_SEPARATOR));
133     }
134     else errorMessage->showMessage("Record missing");
135     return sup;
136 }
137
138 void MediaImagePathDialog::acceptChanges()
139 {
140     FilePathObject *fpo = dynamic_cast<FilePathObject*>(efObject);
141     Setup *sup = getSelectedSetup();
142     if (!sup)
143     {
144         QMessageBox::information(this, tr("Set up"), tr("Set up not selected"), QMessageBox::Ok);
145         return;
146     }
147     qDebug() << "Setup selected " << sup->getName();
148     QString filePath = filePathLabel->text();
149     if (filePath.isEmpty())
150     {
151         QMessageBox::information(this, tr("File path"), tr("File path was not selected"), QMessageBox::Ok);
152         return;
153     }
154     Setup *tmp = fpo->getSetup();
155     if (sup != tmp)
156     {
157         delete tmp;
158         fpo->setSetup(sup);
159     }
160     fpo->setName(filePath);
161     emit dataObjectUpdated();
162     efObject = 0;
163     close();
164 }