3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
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.
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.
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/>.
21 #include "efcombobox.h"
22 #include "emufrontexception.h"
23 #include "emufrontobject.h"
24 #include <QSqlQueryModel>
28 #include <QAbstractItemView>
30 EFComboBox::EFComboBox(DatabaseManager *dbMan, QWidget *parent)
31 : QComboBox(parent), dbManager(dbMan)
33 setSizeAdjustPolicy(QComboBox::AdjustToContents);
37 EFComboBox::~EFComboBox()
41 void EFComboBox::updateDataModel(bool reset)
43 QSqlQueryModel *model = dbManager->getDataModel(reset);
49 Returns a pointer to EmuFrontObject which should be deleted
50 later by calling code!
52 Throws EmuFrontException
54 EmuFrontObject* EFComboBox::getSelected()
56 EmuFrontObject *efo = 0;
57 int index = currentIndex();
60 QSqlQueryModel *qmodel
61 = dynamic_cast<QSqlQueryModel*>(model());
63 throw EmuFrontException(tr("No data model available!"));
65 QSqlRecord rec = qmodel->record(index);
67 throw EmuFrontException(tr("No data available for selected item!"));
69 int id = rec.value(dataModelIndex_id).toInt();
70 EmuFrontObject *o = dbManager->getDataObject(id); /* Throws EmuFrontException */
72 setCurrentIndex(index);
73 if (!o) throw EmuFrontException(tr("Failed creating selected data object!"));
77 void EFComboBox::reset()
79 dbManager->resetModel();
82 void EFComboBox::setSelected(const EmuFrontObject *efo)
86 qDebug() << "EFCombobox selecting " << efo->getName()
87 << " [" << efo->getId() << "].";
88 QSqlQueryModel *qmodel
89 = dynamic_cast<QSqlQueryModel*>(model());
90 QModelIndex idStart = qmodel->index(0, dataModelIndex_id);
91 int targetId = efo->getId();
93 QModelIndexList indLst = qmodel->match(idStart,Qt::DisplayRole, targetId, 1);
94 if (indLst.count() >= 1) {
95 QModelIndex ind = indLst.first();
96 view()->setCurrentIndex(ind);
97 setCurrentIndex(ind.row());