2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
21 #include <QSqlQueryModel>
22 #include "comboboxdelegate.h"
23 #include "emufrontquerymodel.h"
24 #include "emufrontobject.h"
26 ComboBoxDelegate::ComboBoxDelegate(QSqlQueryModel *cbmodel, int cbmodelIdColumn, int cbmodelDisplayColumn, QWidget *parent) :
27 QStyledItemDelegate(parent), cbmodel(cbmodel), cbmodelIdColumn(cbmodelIdColumn), cbmodelDisplayColumn(cbmodelDisplayColumn)
30 void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
32 // get the item id from the hosting view's model
33 int objid = index.model()->data(index, Qt::DisplayRole).toInt();
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() ?
41 cbmodel->index(indList.first().row(), cbmodelDisplayColumn);
43 // Get the name field of an object using the created index
44 QString txt = ind.isValid() ?
45 cbmodel->data(ind).toString() : "";
47 // Render the name text
49 painter->drawText(option.rect, txt);
53 //QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { }
55 QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
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)));
65 void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
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);
80 void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
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);
89 void ComboBoxDelegate::commitAndCloseEditor(int)
91 QComboBox *editor = qobject_cast<QComboBox *>(sender());
92 emit commitData(editor);
93 emit closeEditor(editor);