From f8d61b61f4e351c432efa5529c8c43eff93675ee Mon Sep 17 00:00:00 2001 From: eelke Date: Sat, 15 Dec 2018 12:11:23 +0100 Subject: [PATCH] Added capability to create editors from an editor factory to PgLabItemDelegate --- pglab/PgLabItemDelegate.cpp | 30 ++++++++++++++++++++++++++++++ pglab/PgLabItemDelegate.h | 11 +++++++++++ pglablib/AbstractEditorFactory.h | 25 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 pglablib/AbstractEditorFactory.h diff --git a/pglab/PgLabItemDelegate.cpp b/pglab/PgLabItemDelegate.cpp index 52afeaf..85aa03e 100644 --- a/pglab/PgLabItemDelegate.cpp +++ b/pglab/PgLabItemDelegate.cpp @@ -4,6 +4,7 @@ #include "Pgsql_oids.h" #include "ResultTableModelUtil.h" #include "CustomDataRole.h" +#include "AbstractEditorFactory.h" PgLabItemDelegate::PgLabItemDelegate(QObject *parent) : QStyledItemDelegate(parent) @@ -128,3 +129,32 @@ void PgLabItemDelegate::paint(QPainter *painter, QStyle *style = widget ? widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget); } + +QWidget *PgLabItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + if (m_editorFactory) + return m_editorFactory->createEditor(parent, option, index); + return nullptr; +} + +void PgLabItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +{ + if (m_editorFactory) + m_editorFactory->setEditorData(editor, index); +} + +void PgLabItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +{ + if (m_editorFactory) + m_editorFactory->setModelData(editor, model, index); +} + +AbstractEditorFactory* PgLabItemDelegate::editorFactory() +{ + return m_editorFactory; +} + +void PgLabItemDelegate::setEditorFactory(AbstractEditorFactory *editor_factory) +{ + m_editorFactory = editor_factory; +} diff --git a/pglab/PgLabItemDelegate.h b/pglab/PgLabItemDelegate.h index d11701b..b5a26ea 100644 --- a/pglab/PgLabItemDelegate.h +++ b/pglab/PgLabItemDelegate.h @@ -3,6 +3,8 @@ #include +class AbstractEditorFactory; + /** Delegate for rendering SQL data types in tableviews * * This delegate removes the need for the model to provide formatting information @@ -16,9 +18,18 @@ public: virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + void setEditorData(QWidget *editor, const QModelIndex &index) const override; + void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; + + AbstractEditorFactory* editorFactory(); + void setEditorFactory(AbstractEditorFactory *editor_factory); + protected: virtual void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override; +private: + AbstractEditorFactory *m_editorFactory = nullptr; }; #endif // PGLABITEMDELEGATE_H diff --git a/pglablib/AbstractEditorFactory.h b/pglablib/AbstractEditorFactory.h new file mode 100644 index 0000000..a47d546 --- /dev/null +++ b/pglablib/AbstractEditorFactory.h @@ -0,0 +1,25 @@ +#ifndef ABSTRACTEDITORFACTORY_H +#define ABSTRACTEDITORFACTORY_H + +#include + +class QWidget; +class QStyleOptionViewItem; +class QModelIndex; +class QAbstractItemModel; + +/// Base class for factory classes that create item editors used in views. +/// +/// Specifically the PgLabItemDelegate supports the use of EditorFactories. +class AbstractEditorFactory: public QObject { + Q_OBJECT +public: + using QObject::QObject; + + virtual QWidget *createEditor (QWidget *parent, const QStyleOptionViewItem &option, + const QModelIndex &index) const = 0; + virtual void setEditorData(QWidget *editor, const QModelIndex &index) const = 0; + virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const = 0; +}; + +#endif // ABSTRACTEDITORFACTORY_H