Media image path dialog crash fixed (was not initialized properly).
[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 // Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QtGui>
21 #include <QSqlTableModel>
22 #include <QSqlRecord>
23 #include "../db/dbplatform.h"
24 #include "../db/dbmediatype.h"
25 #include "mediaimagepathdialog.h"
26 #include "../dataobjects/platform.h"
27 #include "../dataobjects/mediatype.h"
28 #include "../dataobjects/filepathobject.h"
29
30 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
31     : DataObjectEditDialog(parent, efObject)
32 {
33     initWidgets();
34     populateMediaTypeComBox();
35     populatePlatformComBox();
36     layout();
37     connectSignals();
38 }
39
40 MediaImagePathDialog::~MediaImagePathDialog()
41 {
42 }
43
44 void MediaImagePathDialog::connectSignals()
45 {
46 }
47
48 void MediaImagePathDialog::initWidgets()
49 {
50     // these widgets will be automatically parented using layout components
51     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal);
52     filePathLabel = new QLabel;
53     filePathButton = new QPushButton(tr("&Browse filepath"));
54     mediaTypeComBox = new QComboBox;
55     platformComBox = new QComboBox;
56 }
57
58 void MediaImagePathDialog::populateMediaTypeComBox()
59 {
60     dbMediaType = new DbMediaType(this);
61     mediaTypeComBox->setModel(dbMediaType->getDataModel());
62     mediaTypeComBox->setModelColumn(DbMediaType::MediaType_Name);
63 }
64
65 void MediaImagePathDialog::populatePlatformComBox()
66 {
67     dbPlatform = new DbPlatform(this);
68     platformComBox->setModel(dbPlatform->getDataModel());
69     platformComBox->setModelColumn(DbPlatform::Platform_Name);
70 }
71
72 void MediaImagePathDialog::layout()
73 {
74    QLabel *platformLabel = new QLabel(tr("&Platform"));
75    platformLabel->setBuddy(platformComBox);
76    QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
77    mediaTypeLabel->setBuddy(mediaTypeComBox);
78
79    QGridLayout *gridLayout = new QGridLayout;
80    gridLayout->addWidget(platformLabel, 0, 0);
81    gridLayout->addWidget(platformComBox, 0, 1);
82    gridLayout->addWidget(mediaTypeLabel, 1, 0);
83    gridLayout->addWidget(mediaTypeComBox, 1, 1);
84    gridLayout->addWidget(filePathButton, 2, 0);
85    gridLayout->addWidget(filePathLabel, 2, 1);
86    gridLayout->addWidget(buttonBox, 3, 0, 1, 2);
87    setLayout(gridLayout);
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     setSelectedPlatform(fpo->getPlatform());
100     setSelectedMediaType(fpo->getMediaType());
101 }
102
103 void MediaImagePathDialog::setSelectedPlatform(const Platform *plf)
104 {
105     setSelected(platformModel, platformComBox, plf, DbPlatform::Platform_Id);
106 }
107
108 void MediaImagePathDialog::setSelectedMediaType(const MediaType *plf)
109 {
110     setSelected(mediaTypeModel, mediaTypeComBox, plf, DbMediaType::MediaType_Id);
111 }
112
113 // TODO: this might be useful to lever to upper classes
114 void MediaImagePathDialog::setSelected(const QSqlTableModel *model, QComboBox *cbox, const EmuFrontObject *ob, int idIndex)
115 {
116     if (!ob) return;
117     for (int i = 0; i < model->rowCount(); ++i)
118     {
119         QSqlRecord rec = model->record(i);
120         if (rec.value(idIndex) == ob->getId())
121         {
122             cbox->setCurrentIndex(i);
123             break;
124         }
125     }
126 }
127
128
129 void MediaImagePathDialog::acceptChanges()
130 {
131 }
132
133 void MediaImagePathDialog::rejectChanges()
134 {
135     // we don't
136     efObject = 0;
137 }