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