Initial implementation of Media image path editor.
[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     qDebug() << "Creating MediaImagePathDialog";
34     initWidgets();
35     populateMediaTypeComBox();
36     populatePlatformComBox();
37     layout();
38     connectSignals();
39 }
40
41 MediaImagePathDialog::~MediaImagePathDialog()
42 {
43     qDebug() << "Destroying MediaImagePathDialog";
44 }
45
46 void MediaImagePathDialog::connectSignals()
47 {
48     qDebug() << "MediaImagePathDialog Connecting signals";
49 }
50
51 void MediaImagePathDialog::initWidgets()
52 {
53     qDebug() << "MediaImagePathDialog initializing widgets.";
54     // these widgets will be automatically parented using layout components
55     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal);
56     filePathLabel = new QLabel;
57     filePathButton = new QPushButton(tr("&Browse filepath"));
58     mediaTypeComBox = new QComboBox;
59     platformComBox = new QComboBox;
60 }
61
62 void MediaImagePathDialog::populateMediaTypeComBox()
63 {
64     qDebug() << "MediaImagePathDialog populating media types combo box";
65     dbMediaType = new DbMediaType(this);
66     mediaTypeComBox->setModel(dbMediaType->getDataModel());
67     mediaTypeComBox->setModelColumn(DbMediaType::MediaType_Name);
68 }
69
70 void MediaImagePathDialog::populatePlatformComBox()
71 {
72     qDebug() << "MediaImagePathDialog populating platform combo box";
73     dbPlatform = new DbPlatform(this);
74     platformComBox->setModel(dbPlatform->getDataModel());
75     platformComBox->setModelColumn(DbPlatform::Platform_Name);
76 }
77
78 void MediaImagePathDialog::layout()
79 {
80     qDebug() << "MediaImagePathDialog setting layout";
81    QLabel *platformLabel = new QLabel(tr("&Platform"));
82    platformLabel->setBuddy(platformComBox);
83    QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
84    mediaTypeLabel->setBuddy(mediaTypeComBox);
85
86    QGridLayout *gridLayout = new QGridLayout;
87    gridLayout->addWidget(platformLabel, 0, 0);
88    gridLayout->addWidget(platformComBox, 0, 1);
89    gridLayout->addWidget(mediaTypeLabel, 1, 0);
90    gridLayout->addWidget(mediaTypeComBox, 1, 1);
91    gridLayout->addWidget(filePathButton, 2, 0);
92    gridLayout->addWidget(filePathLabel, 2, 1);
93    gridLayout->addWidget(buttonBox, 3, 0, 1, 2);
94    setLayout(gridLayout);
95
96    setWindowTitle(tr("Set media image paths"));
97 }
98
99 void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
100 {
101     if (!ob) return;
102     efObject = ob;
103     FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
104     QString fpath = fpo->getName();
105     filePathLabel->setText(fpath);
106     if (fpo->getPlatform()) setSelectedPlatform(fpo->getPlatform());
107     if (fpo->getMediaType()) setSelectedMediaType(fpo->getMediaType());
108 }
109
110 void MediaImagePathDialog::setSelectedPlatform(const Platform *plf)
111 {
112     setSelected(platformModel, platformComBox, plf, DbPlatform::Platform_Id);
113 }
114
115 void MediaImagePathDialog::setSelectedMediaType(const MediaType *plf)
116 {
117     setSelected(mediaTypeModel, mediaTypeComBox, plf, DbMediaType::MediaType_Id);
118 }
119
120 // TODO: this might be useful to lever to upper classes
121 void MediaImagePathDialog::setSelected(const QSqlTableModel *model, QComboBox *cbox, const EmuFrontObject *ob, int idIndex)
122 {
123     if (!ob) return;
124     for (int i = 0; i < model->rowCount(); ++i)
125     {
126         QSqlRecord rec = model->record(i);
127         if (rec.value(idIndex) == ob->getId())
128         {
129             cbox->setCurrentIndex(i);
130             break;
131         }
132     }
133 }
134
135
136 void MediaImagePathDialog::acceptChanges()
137 {
138 }
139
140 void MediaImagePathDialog::rejectChanges()
141 {
142     // we don't
143     efObject = 0;
144 }