Fixed a bug: the edit dialog fields were not cleared if an
[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     : BrowseFilePathDialog(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     BrowseFilePathDialog::initWidgets();
63     // these widgets will be automatically parented using layout components
64     //filePathLabel = new QLabel;
65     //filePathButton = new QPushButton(tr("&Browse filepath"));
66     dbSetup = new DbSetup(this);
67     setupComBox = new SetupComboBox(dbSetup, this);
68 }
69
70 void MediaImagePathDialog::layout()
71 {
72    QLabel *setupLabel = new QLabel(tr("&Set up"));
73    setupLabel->setBuddy(setupComBox);
74
75    QGridLayout *gridLayout = new QGridLayout;
76    gridLayout->addWidget(setupLabel, 0, 0);
77    gridLayout->addWidget(setupComBox, 0, 1);
78    gridLayout->addWidget(filePathButton, 1, 0);
79    gridLayout->addWidget(filePathLabel, 1, 1);
80    QVBoxLayout *mainLayout = new QVBoxLayout;
81    mainLayout->addLayout(gridLayout);
82    mainLayout->addWidget(buttonBox);
83    setLayout(mainLayout);
84
85    setWindowTitle(tr("Set media image paths"));
86 }
87
88 void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
89 {
90     if (!ob) return;
91     efObject = ob;
92     FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
93     QString fpath = fpo->getName();
94     filePathLabel->setText(fpath);
95     if (fpo->getSetup()) setSelectedSetup(fpo->getSetup());
96 }
97
98 void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
99 {
100     setupComBox->setSelected(sup);
101 }
102
103 /* Returns a pointer to Setup object which must be deleted by calling code */
104 Setup* MediaImagePathDialog::getSelectedSetup()
105 {
106     EmuFrontObject *ob = 0;
107
108     try {
109         ob = setupComBox->getSelected();
110     }
111     catch(EmuFrontException &e){
112         errorMessage->showMessage(e.what());
113     }
114
115     if (!ob) return 0;
116     return dynamic_cast<Setup*>(ob);
117 }
118
119 void MediaImagePathDialog::acceptChanges()
120 {
121     FilePathObject *fpo = dynamic_cast<FilePathObject*>(efObject);
122     Setup *sup = getSelectedSetup();
123     if (!sup)
124     {
125         QMessageBox::information(this, tr("Set up"), tr("Set up not selected"), QMessageBox::Ok);
126         return;
127     }
128     bool change = false;
129     qDebug() << "Setup selected " << sup->getName();
130     QString filePath = filePathLabel->text();
131     if (filePath.isEmpty())
132     {
133         QMessageBox::information(this, tr("File path"), tr("File path was not selected"), QMessageBox::Ok);
134         return;
135     }
136     if (filePath != fpo->getName()) {
137         fpo->setName(filePath);
138         change = true;
139     }
140
141     Setup *tmp = fpo->getSetup();
142
143     if (!tmp || *sup != *tmp)
144     {
145         delete tmp;
146         fpo->setSetup(sup);
147         change = true;
148     }
149     if (change) emit dataObjectUpdated();
150     efObject = 0;
151     close();
152 }
153
154
155 void MediaImagePathDialog::updateData()
156 {
157     setupComBox->updateDataModel();
158 }
159
160 void MediaImagePathDialog::clear()
161 {
162     BrowseFilePathDialog::clear();
163     setupComBox->setCurrentIndex(-1);
164 }