Updated the git clone command.
[emufront] / src / dialogs / mediaimagepathdialog.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <QtGui>
22 #include <QSqlTableModel>
23 #include <QSqlRecord>
24 #include "mediaimagepathdialog.h"
25 #include "dbsetup.h"
26 #include "filepathobject.h"
27 #include "setupcombobox.h"
28
29 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
30     : BrowseFilePathDialog(parent, efObject, Qt::Horizontal)
31 {
32     initWidgets();
33     dbPlatform = 0;
34     dbMediaType = 0;
35     dbSetup = 0;
36     connectSignals();
37     layout();
38 }
39
40 MediaImagePathDialog::~MediaImagePathDialog()
41 {
42 }
43
44 /*void MediaImagePathDialog::connectSignals()
45 {
46     DataObjectEditDialog::connectSignals();
47     connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
48 }*/
49
50 /*void MediaImagePathDialog::browseFilePath()
51 {
52     QString fpath = QFileDialog::getExistingDirectory(this, tr("Select a directory"), ".",
53         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
54     QDir d(fpath);
55     if (d.exists() && d.isReadable())
56     {
57         filePathLabel->setText(d.path());
58     }
59 }*/
60
61 void MediaImagePathDialog::initWidgets()
62 {
63     BrowseFilePathDialog::initWidgets();
64     // these widgets will be automatically parented using layout components
65     //filePathLabel = new QLabel;
66     //filePathButton = new QPushButton(tr("&Browse filepath"));
67     dbSetup = new DbSetup(this);
68     setupComBox = new SetupComboBox(dbSetup, this);
69 }
70
71 void MediaImagePathDialog::layout()
72 {
73    QLabel *setupLabel = new QLabel(tr("&Set up"));
74    setupLabel->setBuddy(setupComBox);
75
76    QGridLayout *gridLayout = new QGridLayout;
77    gridLayout->addWidget(setupLabel, 0, 0);
78    gridLayout->addWidget(setupComBox, 0, 1);
79    gridLayout->addWidget(filePathButton, 1, 0);
80    gridLayout->addWidget(filePathLabel, 1, 1);
81    QVBoxLayout *mainLayout = new QVBoxLayout;
82    mainLayout->addLayout(gridLayout);
83    mainLayout->addWidget(buttonBox);
84    setLayout(mainLayout);
85
86    setWindowTitle(tr("Set media image paths"));
87 }
88
89 void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
90 {
91     if (!ob) return;
92     efObject = ob;
93     FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
94     QString fpath = fpo->getName();
95     filePathLabel->setText(fpath);
96     if (fpo->getSetup()) setSelectedSetup(fpo->getSetup());
97 }
98
99 void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
100 {
101     setupComBox->setSelected(sup);
102 }
103
104 /* Returns a pointer to Setup object which must be deleted by calling code */
105 Setup* MediaImagePathDialog::getSelectedSetup()
106 {
107     EmuFrontObject *ob = 0;
108
109     try {
110         ob = setupComBox->getSelected();
111     }
112     catch(EmuFrontException &e){
113         errorMessage->showMessage(e.what());
114     }
115
116     if (!ob) return 0;
117     return dynamic_cast<Setup*>(ob);
118 }
119
120 void MediaImagePathDialog::acceptChanges()
121 {
122     FilePathObject *fpo = dynamic_cast<FilePathObject*>(efObject);
123     Setup *sup = getSelectedSetup();
124     if (!sup)
125     {
126         QMessageBox::information(this, tr("Set up"), tr("Set up not selected"), QMessageBox::Ok);
127         return;
128     }
129     bool change = false;
130     qDebug() << "Setup selected " << sup->getName();
131     QString filePath = filePathLabel->text();
132     if (filePath.isEmpty())
133     {
134         QMessageBox::information(this, tr("File path"), tr("File path was not selected"), QMessageBox::Ok);
135         return;
136     }
137     if (filePath != fpo->getName()) {
138         fpo->setName(filePath);
139         change = true;
140     }
141
142     Setup *tmp = fpo->getSetup();
143
144     if (!tmp || *sup != *tmp)
145     {
146         delete tmp;
147         fpo->setSetup(sup);
148         change = true;
149     }
150     if (change) emit dataObjectUpdated();
151     efObject = 0;
152     close();
153 }
154
155
156 void MediaImagePathDialog::updateData()
157 {
158     setupComBox->updateDataModel();
159 }
160
161 void MediaImagePathDialog::clear()
162 {
163     BrowseFilePathDialog::clear();
164     setupComBox->setCurrentIndex(-1);
165 }