Modified the license text comment type.
[emufront] / src / widgets / efcombobox.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 "efcombobox.h"
22 #include "emufrontexception.h"
23 #include "emufrontobject.h"
24 #include <QSqlQueryModel>
25 #include <QSqlRecord>
26 #include <QDebug>
27 #include <QTime>
28 #include <QAbstractItemView>
29
30 EFComboBox::EFComboBox(DatabaseManager *dbMan, QWidget *parent)
31     : QComboBox(parent), dbManager(dbMan)
32 {
33     setSizeAdjustPolicy(QComboBox::AdjustToContents);
34     updateDataModel();
35 }
36
37 EFComboBox::~EFComboBox()
38 {
39 }
40
41 void EFComboBox::updateDataModel(bool reset)
42 {
43     QSqlQueryModel *model = dbManager->getDataModel(reset);
44     if (model)
45         setModel(model);
46 }
47
48 /*
49     Returns a pointer to EmuFrontObject which should be deleted
50     later by calling code!
51
52     Throws EmuFrontException
53 */
54 EmuFrontObject* EFComboBox::getSelected()
55 {
56     EmuFrontObject *efo = 0;
57     int index = currentIndex();
58     if (index < 0)
59         return 0;
60     QSqlQueryModel *qmodel
61         = dynamic_cast<QSqlQueryModel*>(model());
62     if (!qmodel) {
63         throw EmuFrontException(tr("No data model available!"));
64     }
65     QSqlRecord rec = qmodel->record(index);
66     if (rec.isEmpty()) {
67         throw EmuFrontException(tr("No data available for selected item!"));
68     }
69     int id = rec.value(dataModelIndex_id).toInt();
70     EmuFrontObject *o = dbManager->getDataObject(id); /* Throws EmuFrontException */
71     reset();
72     setCurrentIndex(index);
73     if (!o) throw EmuFrontException(tr("Failed creating selected data object!"));
74     return o;
75 }
76
77 void EFComboBox::reset()
78 {
79     dbManager->resetModel();
80 }
81
82 void EFComboBox::setSelected(const EmuFrontObject *efo)
83 {
84     if (!efo)
85         return;
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();
92
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());
98     }
99 }