Continued implementing ComboBoxDelegate ... not tested yet ...
[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 *model, int modelIdColumn, int modelDisplayColumn, QWidget *parent) :
8     QStyledItemDelegate(parent), viewColumn(viewColumn), model(model), modelIdColumn(modelIdColumn), modelDisplayColumn(modelDisplayColumn)
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 = model->index(0, modelIdColumn);
23     QModelIndexList indList = model->match(startInd, Qt::DisplayRole, objid);
24
25     QModelIndex ind = indList.empty() ?
26         QModelIndex() :
27         model->index(startInd.row(), modelDisplayColumn);
28
29     QString txt = ind.isValid() ?
30         model->data(ind).toString() : "";
31
32     painter->save();
33     //initStyleOption(&option, index);
34     painter->drawText(option.rect, txt);
35     painter->restore();
36 }
37
38 //QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { }
39
40 QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
41 {
42     if (index.column() != viewColumn) {
43         return QStyledItemDelegate::createEditor(parent, option, index);
44     }
45
46     QComboBox *editor = new QComboBox(parent);
47     editor->setEditable(false);
48     editor->setModel(model);
49     connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
50     return editor;
51 }
52
53 void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
54 {
55     if (index.column() != viewColumn) {
56         return QStyledItemDelegate::setEditorData(editor, index);
57     }
58
59     // set the correct item selected for the editor:
60     // get the item id from the parent view's model
61     int id = index.model()->data(index, Qt::DisplayRole).toInt();
62     QComboBox *cbox = qobject_cast<QComboBox *>(editor);
63     // create a start index from combobox model
64     QModelIndex startInd = model->index(0, modelIdColumn);
65     // search an index for selected item in parent view
66     QModelIndexList indList = model->match(startInd, Qt::DisplayRole, id);
67     // if no match set no item selected
68     int ind = indList.empty() ? -1 : indList.first().row();
69     cbox->setCurrentIndex(ind);
70 }
71
72 void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
73 {
74     if (index.column() != viewColumn) {
75         return QStyledItemDelegate::setModelData(editor, model, index);
76     }
77     QComboBox *cbox = qobject_cast<QComboBox *>(editor);
78     if (cbox->currentIndex() == -1) return;
79     QModelIndex mi = model->index(cbox->currentIndex(), modelIdColumn);
80     int id = model->data(mi).toInt();
81     model->setData(index, id);
82 }
83
84 void ComboBoxDelegate::commitAndCloseEditor()
85 {
86     QComboBox *editor = qobject_cast<QComboBox *>(sender());
87     emit commitData(editor);
88     emit closeEditor(editor);
89 }
90