Re-used the ComboBoxDelegate created earlier for MediaTypeModel in
[emufront] / src / delegates / comboboxdelegate.cpp
index d3993d7..0a9f925 100644 (file)
@@ -4,25 +4,18 @@
 #include "emufrontquerymodel.h"
 #include "emufrontobject.h"
 
-ComboBoxDelegate::ComboBoxDelegate(int viewColumn, QSqlQueryModel *cbmodel, int cbmodelIdColumn, int cbmodelDisplayColumn, QWidget *parent) :
-    QStyledItemDelegate(parent), viewColumn(viewColumn), cbmodel(cbmodel), cbmodelIdColumn(cbmodelIdColumn), cbmodelDisplayColumn(cbmodelDisplayColumn)
+ComboBoxDelegate::ComboBoxDelegate(QSqlQueryModel *cbmodel, int cbmodelIdColumn, int cbmodelDisplayColumn, QWidget *parent) :
+    QStyledItemDelegate(parent), cbmodel(cbmodel), cbmodelIdColumn(cbmodelIdColumn), cbmodelDisplayColumn(cbmodelDisplayColumn)
 { }
 
 void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 {
-    if (index.column() != viewColumn) {
-        QStyledItemDelegate::paint(painter, option, index);
-        return;
-    }
-
     // get the item id from the hosting view's model
     int objid = index.model()->data(index,  Qt::DisplayRole).toInt();
 
     // find matching object from QComboBox's model
     QModelIndex startInd = cbmodel->index(0, cbmodelIdColumn);
     QModelIndexList indList = cbmodel->match(startInd, Qt::DisplayRole, objid);
-
-
     QModelIndex ind = indList.empty() ?
         QModelIndex() :
         cbmodel->index(indList.first().row(), cbmodelDisplayColumn);
@@ -31,7 +24,6 @@ void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
         cbmodel->data(ind).toString() : "";
 
     painter->save();
-    //initStyleOption(&option, index);
     painter->drawText(option.rect, txt);
     painter->restore();
 }
@@ -40,24 +32,16 @@ void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
 
 QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
 {
-    if (index.column() != viewColumn) {
-        return QStyledItemDelegate::createEditor(parent, option, index);
-    }
-
     QComboBox *editor = new QComboBox(parent);
     editor->setEditable(false);
     editor->setModel(cbmodel);
     editor->setModelColumn(cbmodelDisplayColumn);
-    connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
+    connect(editor, SIGNAL(currentIndexChanged(int)), this, SLOT(commitAndCloseEditor(int)));
     return editor;
 }
 
 void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 {
-    if (index.column() != viewColumn) {
-        return QStyledItemDelegate::setEditorData(editor, index);
-    }
-
     // set the correct item selected for the editor:
     // get the item id from the parent view's model
     int id = index.model()->data(index, Qt::DisplayRole).toInt();
@@ -73,9 +57,6 @@ void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index)
 
 void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
 {
-    if (index.column() != viewColumn) {
-        return QStyledItemDelegate::setModelData(editor, model, index);
-    }
     QComboBox *cbox = qobject_cast<QComboBox *>(editor);
     if (cbox->currentIndex() == -1) return;
     QModelIndex mi = cbmodel->index(cbox->currentIndex(), cbmodelIdColumn);
@@ -83,7 +64,7 @@ void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
     model->setData(index, id);
 }
 
-void ComboBoxDelegate::commitAndCloseEditor()
+void ComboBoxDelegate::commitAndCloseEditor(int)
 {
     QComboBox *editor = qobject_cast<QComboBox *>(sender());
     emit commitData(editor);