3f83f1dac5056dc2ddad0e12daf1108655c3a91e
[emufront] / src / delegates / comboboxdelegate.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 version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
11 //
12 // EmuFront 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 EmuFront.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QtGui>
21 #include <QSqlQueryModel>
22 #include "comboboxdelegate.h"
23 #include "emufrontquerymodel.h"
24 #include "emufrontobject.h"
25
26 ComboBoxDelegate::ComboBoxDelegate(QSqlQueryModel *cbmodel, int cbmodelIdColumn, int cbmodelDisplayColumn, QWidget *parent) :
27     QStyledItemDelegate(parent), cbmodel(cbmodel), cbmodelIdColumn(cbmodelIdColumn), cbmodelDisplayColumn(cbmodelDisplayColumn)
28 { }
29
30 void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
31 {
32     // get the item id from the hosting view's model
33     int objid = index.model()->data(index,  Qt::DisplayRole).toInt();
34
35     // find a matching object from the editor's model
36     QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
37     QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, objid);
38     // If an object was found in the editor's model create an index to the editor's model
39     QModelIndex ind = indList.empty() ?
40         QModelIndex() :
41         cbmodel->index(indList.first().row(), cbmodelDisplayColumn);
42
43     // Get the name field of an object using the created index
44     QString txt = ind.isValid() ?
45         cbmodel->data(ind).toString() : "";
46
47     // Render the name text
48     painter->save();
49     painter->drawText(option.rect, txt);
50     painter->restore();
51 }
52
53 //QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { }
54
55 QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
56 {
57     QComboBox *editor = new QComboBox(parent);
58     editor->setEditable(false);
59     editor->setModel(cbmodel);
60     editor->setModelColumn(cbmodelDisplayColumn);
61     connect(editor, SIGNAL(currentIndexChanged(int)), this, SLOT(commitAndCloseEditor(int)));
62     return editor;
63 }
64
65 void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
66 {
67     // set the correct item selected for the editor:
68     // get the item id from the parent view's model
69     int id = index.model()->data(index, Qt::DisplayRole).toInt();
70     QComboBox *cbox = qobject_cast<QComboBox *>(editor);
71     // create a start index from combobox model
72     QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
73     // search an index for selected item in parent view
74     QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, id);
75     // if no match set no item selected
76     int ind = indList.empty() ? -1 : indList.first().row();
77     cbox->setCurrentIndex(ind);
78 }
79
80 void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
81 {
82     QComboBox *cbox = qobject_cast<QComboBox *>(editor);
83     if (cbox->currentIndex() == -1) return;
84     QModelIndex mi = cbmodel->index(cbox->currentIndex(), cbmodelIdColumn);
85     int id = cbmodel->data(mi).toInt();
86     model->setData(index, id);
87 }
88
89 void ComboBoxDelegate::commitAndCloseEditor(int)
90 {
91     QComboBox *editor = qobject_cast<QComboBox *>(sender());
92     emit commitData(editor);
93     emit closeEditor(editor);
94 }
95