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 matching object from QComboBox's model
36 QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
37 QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, objid);
38 QModelIndex ind = indList.empty() ?
40 cbmodel->index(indList.first().row(), cbmodelDisplayColumn);
42 QString txt = ind.isValid() ?
43 cbmodel->data(ind).toString() : "";
46 painter->drawText(option.rect, txt);
50 //QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { }
52 QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
54 QComboBox *editor = new QComboBox(parent);
55 editor->setEditable(false);
56 editor->setModel(cbmodel);
57 editor->setModelColumn(cbmodelDisplayColumn);
58 connect(editor, SIGNAL(currentIndexChanged(int)), this, SLOT(commitAndCloseEditor(int)));
62 void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
64 // set the correct item selected for the editor:
65 // get the item id from the parent view's model
66 int id = index.model()->data(index, Qt::DisplayRole).toInt();
67 QComboBox *cbox = qobject_cast<QComboBox *>(editor);
68 // create a start index from combobox model
69 QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
70 // search an index for selected item in parent view
71 QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, id);
72 // if no match set no item selected
73 int ind = indList.empty() ? -1 : indList.first().row();
74 cbox->setCurrentIndex(ind);
77 void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
79 QComboBox *cbox = qobject_cast<QComboBox *>(editor);
80 if (cbox->currentIndex() == -1) return;
81 QModelIndex mi = cbmodel->index(cbox->currentIndex(), cbmodelIdColumn);
82 int id = cbmodel->data(mi).toInt();
83 model->setData(index, id);
86 void ComboBoxDelegate::commitAndCloseEditor(int)
88 QComboBox *editor = qobject_cast<QComboBox *>(sender());
89 emit commitData(editor);
90 emit closeEditor(editor);