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/>.
22 #include <QSqlQueryModel>
23 #include "comboboxdelegate.h"
24 #include "emufrontquerymodel.h"
25 #include "emufrontobject.h"
27 ComboBoxDelegate::ComboBoxDelegate(QSqlQueryModel *cbmodel, int cbmodelIdColumn, int cbmodelDisplayColumn, QWidget *parent) :
28 QStyledItemDelegate(parent), cbmodel(cbmodel), cbmodelIdColumn(cbmodelIdColumn), cbmodelDisplayColumn(cbmodelDisplayColumn)
31 void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
33 // get the item id from the hosting view's model
34 int objid = index.model()->data(index, Qt::DisplayRole).toInt();
36 // find a matching object from the editor's model
37 QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
38 QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, objid);
39 // If an object was found in the editor's model create an index to the editor's model
40 QModelIndex ind = indList.empty() ?
42 cbmodel->index(indList.first().row(), cbmodelDisplayColumn);
44 // Get the name field of an object using the created index
45 QString txt = ind.isValid() ?
46 cbmodel->data(ind).toString() : "";
48 // Render the name text
50 painter->drawText(option.rect, txt);
54 //QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { }
56 QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
58 QComboBox *editor = new QComboBox(parent);
59 editor->setEditable(false);
60 editor->setModel(cbmodel);
61 editor->setModelColumn(cbmodelDisplayColumn);
62 connect(editor, SIGNAL(currentIndexChanged(int)), this, SLOT(commitAndCloseEditor(int)));
66 void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
68 // set the correct item selected for the editor:
69 // get the item id from the parent view's model
70 int id = index.model()->data(index, Qt::DisplayRole).toInt();
71 QComboBox *cbox = qobject_cast<QComboBox *>(editor);
72 // create a start index from combobox model
73 QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
74 // search an index for selected item in parent view
75 QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, id);
76 // if no match set no item selected
77 int ind = indList.empty() ? -1 : indList.first().row();
78 cbox->setCurrentIndex(ind);
81 void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
83 QComboBox *cbox = qobject_cast<QComboBox *>(editor);
84 if (cbox->currentIndex() == -1) return;
85 QModelIndex mi = cbmodel->index(cbox->currentIndex(), cbmodelIdColumn);
86 int id = cbmodel->data(mi).toInt();
87 model->setData(index, id);
90 void ComboBoxDelegate::commitAndCloseEditor(int)
92 QComboBox *editor = qobject_cast<QComboBox *>(sender());
93 emit commitData(editor);
94 emit closeEditor(editor);