QComboBox delegate implemented and quickly tested being functional! :)
[emufront] / src / delegates / comboboxdelegate.cpp
1 #include <QtGui>
2 #include <QSqlQueryModel>
3 #include "comboboxdelegate.h"
4 #include "emufrontquerymodel.h"
5 #include "emufrontobject.h"
6
7 ComboBoxDelegate::ComboBoxDelegate(int viewColumn, QSqlQueryModel *cbmodel, int cbmodelIdColumn, int cbmodelDisplayColumn, QWidget *parent) :
8     QStyledItemDelegate(parent), viewColumn(viewColumn), cbmodel(cbmodel), cbmodelIdColumn(cbmodelIdColumn), cbmodelDisplayColumn(cbmodelDisplayColumn)
9 { }
10
11 void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
12 {
13     if (index.column() != viewColumn) {
14         QStyledItemDelegate::paint(painter, option, index);
15         return;
16     }
17
18     // get the item id from the hosting view's model
19     int objid = index.model()->data(index,  Qt::DisplayRole).toInt();
20
21     // find matching object from QComboBox's model
22     QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
23     QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, objid);
24
25
26     QModelIndex ind = indList.empty() ?
27         QModelIndex() :
28         cbmodel->index(indList.first().row(), cbmodelDisplayColumn);
29
30     QString txt = ind.isValid() ?
31         cbmodel->data(ind).toString() : "";
32
33     painter->save();
34     //initStyleOption(&option, index);
35     painter->drawText(option.rect, txt);
36     painter->restore();
37 }
38
39 //QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { }
40
41 QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
42 {
43     if (index.column() != viewColumn) {
44         return QStyledItemDelegate::createEditor(parent, option, index);
45     }
46
47     QComboBox *editor = new QComboBox(parent);
48     editor->setEditable(false);
49     editor->setModel(cbmodel);
50     editor->setModelColumn(cbmodelDisplayColumn);
51     connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
52     return editor;
53 }
54
55 void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
56 {
57     if (index.column() != viewColumn) {
58         return QStyledItemDelegate::setEditorData(editor, index);
59     }
60
61     // set the correct item selected for the editor:
62     // get the item id from the parent view's model
63     int id = index.model()->data(index, Qt::DisplayRole).toInt();
64     QComboBox *cbox = qobject_cast<QComboBox *>(editor);
65     // create a start index from combobox model
66     QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
67     // search an index for selected item in parent view
68     QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, id);
69     // if no match set no item selected
70     int ind = indList.empty() ? -1 : indList.first().row();
71     cbox->setCurrentIndex(ind);
72 }
73
74 void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
75 {
76     if (index.column() != viewColumn) {
77         return QStyledItemDelegate::setModelData(editor, model, index);
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()
87 {
88     QComboBox *editor = qobject_cast<QComboBox *>(sender());
89     emit commitData(editor);
90     emit closeEditor(editor);
91 }
92