Started implementing a combo box delegate.
authorMikko Keinänen <mikko.keinanen@gmail.com>
Sun, 5 Dec 2010 00:43:54 +0000 (02:43 +0200)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Sun, 5 Dec 2010 00:43:54 +0000 (02:43 +0200)
src/delegates/comboboxdelegate.cpp [new file with mode: 0644]
src/delegates/comboboxdelegate.h [new file with mode: 0644]
src/emufront.pro
src/models/emufrontquerymodel.cpp
src/models/emufrontquerymodel.h

diff --git a/src/delegates/comboboxdelegate.cpp b/src/delegates/comboboxdelegate.cpp
new file mode 100644 (file)
index 0000000..75a55bc
--- /dev/null
@@ -0,0 +1,53 @@
+#include <QtGui>
+#include "comboboxdelegate.h"
+#include "emufrontquerymodel.h"
+#include "emufrontobject.h"
+
+ComboBoxDelegate::ComboBoxDelegate(int column, EmuFrontQueryModel *model, QWidget *parent) :
+    QStyledItemDelegate(parent), column(column), model(model)
+{ }
+
+void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+    if (index.column() != column) {
+        QStyledItemDelegate::paint(painter, option, index);
+        return;
+    }
+
+    int objid = index.model()->data(index,  Qt::DisplayRole).toInt();
+    EmuFrontObject *efo = model->getObject(objid);
+    QString txt = efo->getName();
+    painter->save();
+    //initStyleOption(&option, index);
+    painter->drawText(option.rect, txt);
+    painter->restore();
+}
+
+//QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { }
+
+QWidget* ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+    if (index.column() != column) {
+        return QStyledItemDelegate::createEditor(parent, option, index);
+    }
+
+    QComboBox *editor = new QComboBox(parent);
+    editor->setEditable(false);
+    editor->setModel(model);
+    connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
+    return editor;
+}
+
+void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
+{
+
+}
+
+void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
+{
+}
+
+void ComboBoxDelegate::commitAndCloseEditor()
+{
+}
+
diff --git a/src/delegates/comboboxdelegate.h b/src/delegates/comboboxdelegate.h
new file mode 100644 (file)
index 0000000..a517a3e
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef COMBOBOXDELEGATE_H
+#define COMBOBOXDELEGATE_H
+
+#include <QStyledItemDelegate>
+
+class EmuFrontQueryModel;
+
+class ComboBoxDelegate : public QStyledItemDelegate
+{
+    Q_OBJECT
+public:
+    ComboBoxDelegate(int column, EmuFrontQueryModel *, QWidget *parent = 0);
+    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
+    //QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
+    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
+    void setEditorData(QWidget *editor, const QModelIndex &index) const;
+    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
+
+private slots:
+    void commitAndCloseEditor();
+private:
+    EmuFrontQueryModel *model;
+    int column;
+};
+
+#endif // COMBOBOXDELEGATE_H
index 9fa39a7..1fa6f53 100644 (file)
@@ -90,7 +90,8 @@ HEADERS += mainwindow.h \
     views/platformeditview.h \
     views/mediatypeeditview.h \
     views/emufrontfileobjecteditview.h \
-    views/emufronteditview.h
+    views/emufronteditview.h \
+    delegates/comboboxdelegate.h
 SOURCES += main.cpp \
     mainwindow.cpp \
     db/databasemanager.cpp \
@@ -158,7 +159,8 @@ SOURCES += main.cpp \
     views/platformeditview.cpp \
     views/mediatypeeditview.cpp \
     views/emufrontfileobjecteditview.cpp \
-    views/emufronteditview.cpp
+    views/emufronteditview.cpp \
+    delegates/comboboxdelegate.cpp
 OTHER_FILES +=  
 
 CONFIG += mobility
index 9d99e46..dc6f8ed 100644 (file)
 // along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "emufrontquerymodel.h"
+#include "emufrontobject.h"
 
 EmuFrontQueryModel::EmuFrontQueryModel(QObject *parent) :
     QSqlQueryModel(parent)
 {
 }
+
+EmuFrontObject* EmuFrontQueryModel::getObject(int id) const
+{
+    // TODO
+    return 0;
+}
index 5e56c18..09e8654 100644 (file)
 
 #include <QSqlQueryModel>
 
+class EmuFrontObject;
+
 class EmuFrontQueryModel : public QSqlQueryModel
 {
     Q_OBJECT
 public:
     EmuFrontQueryModel(QObject *parent = 0);
+    EmuFrontObject* getObject(int id) const;
 
 signals: