Added license text.
[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 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() ?
39         QModelIndex() :
40         cbmodel->index(indList.first().row(), cbmodelDisplayColumn);
41
42     QString txt = ind.isValid() ?
43         cbmodel->data(ind).toString() : "";
44
45     painter->save();
46     painter->drawText(option.rect, txt);
47     painter->restore();
48 }
49
50 //QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { }
51
52 QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
53 {
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)));
59     return editor;
60 }
61
62 void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
63 {
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);
75 }
76
77 void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
78 {
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);
84 }
85
86 void ComboBoxDelegate::commitAndCloseEditor(int)
87 {
88     QComboBox *editor = qobject_cast<QComboBox *>(sender());
89     emit commitData(editor);
90     emit closeEditor(editor);
91 }
92