SelectionEditorFactory for creating combobox selections for use in QTableView.
TypeSelectionItemModelFactory to use the TypeSelectionItemModel with above factory.
This commit is contained in:
parent
f8d61b61f4
commit
e44f73166f
7 changed files with 138 additions and 0 deletions
19
pglablib/AbstractModelFactory.h
Normal file
19
pglablib/AbstractModelFactory.h
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef ABSTRACTMODELFACTORY_H
|
||||||
|
#define ABSTRACTMODELFACTORY_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
class QAbstractItemModel;
|
||||||
|
|
||||||
|
class AbstractModelFactory: public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
using QObject::QObject;
|
||||||
|
|
||||||
|
/// Create instance of a model
|
||||||
|
///
|
||||||
|
/// \param parent Will be passed to the constructor of the model.
|
||||||
|
virtual QAbstractItemModel* createModel(QObject *parent = nullptr) const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ABSTRACTMODELFACTORY_H
|
||||||
60
pglablib/SelectionEditorFactory.cpp
Normal file
60
pglablib/SelectionEditorFactory.cpp
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
#include "SelectionEditorFactory.h"
|
||||||
|
#include "TypeSelectionItemModel.h"
|
||||||
|
#include "AbstractModelFactory.h"
|
||||||
|
#include <QComboBox>
|
||||||
|
|
||||||
|
SelectionEditorFactory::SelectionEditorFactory(QObject *parent, AbstractModelFactory *model_factory, int key_column, int value_column)
|
||||||
|
: AbstractEditorFactory(parent)
|
||||||
|
, m_modelFactory(model_factory)
|
||||||
|
, m_keyColumn(key_column)
|
||||||
|
, m_valueColumn(value_column)
|
||||||
|
{}
|
||||||
|
|
||||||
|
QWidget *SelectionEditorFactory::createEditor (QWidget *parent, const QStyleOptionViewItem &,
|
||||||
|
const QModelIndex &) const
|
||||||
|
{
|
||||||
|
QWidget *w = nullptr;
|
||||||
|
|
||||||
|
QComboBox *cmbbx = new QComboBox(parent);
|
||||||
|
cmbbx->setMaxVisibleItems(32);
|
||||||
|
auto model = m_modelFactory->createModel(cmbbx);
|
||||||
|
cmbbx->setModel(model);
|
||||||
|
cmbbx->setModelColumn(m_valueColumn);
|
||||||
|
w = cmbbx;
|
||||||
|
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SelectionEditorFactory::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
QComboBox *cmbbx = dynamic_cast<QComboBox*>(editor);
|
||||||
|
if (cmbbx) {
|
||||||
|
auto data = index.data();
|
||||||
|
if (data.canConvert<QString>()) {
|
||||||
|
auto selection_model = cmbbx->model();
|
||||||
|
QModelIndexList indexes = selection_model->match(
|
||||||
|
selection_model->index(0, m_keyColumn), Qt::DisplayRole, data, 1, Qt::MatchFlags( Qt::MatchExactly ));
|
||||||
|
if (!indexes.empty()) {
|
||||||
|
cmbbx->setCurrentIndex(indexes.at(0).row());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
cmbbx->setCurrentIndex(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SelectionEditorFactory::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
QComboBox *cmbbx = dynamic_cast<QComboBox*>(editor);
|
||||||
|
if (cmbbx) {
|
||||||
|
auto data = index.data();
|
||||||
|
if (data.canConvert<QString>()) {
|
||||||
|
auto selection_model = cmbbx->model();
|
||||||
|
QVariant d = selection_model->data(
|
||||||
|
selection_model->index(cmbbx->currentIndex(), m_keyColumn));
|
||||||
|
model->setData(index, d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
24
pglablib/SelectionEditorFactory.h
Normal file
24
pglablib/SelectionEditorFactory.h
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#ifndef SELECTIONEDITORFACTORY_H
|
||||||
|
#define SELECTIONEDITORFACTORY_H
|
||||||
|
|
||||||
|
#include "AbstractEditorFactory.h"
|
||||||
|
|
||||||
|
class AbstractModelFactory;
|
||||||
|
|
||||||
|
class SelectionEditorFactory: public AbstractEditorFactory {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
SelectionEditorFactory(QObject *parent, AbstractModelFactory *model_factory, int key_column, int value_column);
|
||||||
|
|
||||||
|
virtual QWidget *createEditor (QWidget *parent, const QStyleOptionViewItem &option,
|
||||||
|
const QModelIndex &index) const override;
|
||||||
|
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override;
|
||||||
|
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
AbstractModelFactory *m_modelFactory = nullptr;
|
||||||
|
int m_keyColumn = 0;
|
||||||
|
int m_valueColumn = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SELECTIONEDITORFACTORY_H
|
||||||
15
pglablib/TypeSelectionItemModelFactory.cpp
Normal file
15
pglablib/TypeSelectionItemModelFactory.cpp
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
#include "TypeSelectionItemModelFactory.h"
|
||||||
|
#include "TypeSelectionItemModel.h"
|
||||||
|
|
||||||
|
TypeSelectionItemModelFactory::TypeSelectionItemModelFactory(QObject *parent, std::shared_ptr<const PgTypeContainer> types)
|
||||||
|
: AbstractModelFactory(parent)
|
||||||
|
, m_types(types)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
QAbstractItemModel* TypeSelectionItemModelFactory::createModel(QObject *parent) const
|
||||||
|
{
|
||||||
|
auto model = new TypeSelectionItemModel(parent);
|
||||||
|
model->setTypeList(m_types);
|
||||||
|
return model;
|
||||||
|
}
|
||||||
20
pglablib/TypeSelectionItemModelFactory.h
Normal file
20
pglablib/TypeSelectionItemModelFactory.h
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef TYPESELECTIONITEMMODELFACTORY_H
|
||||||
|
#define TYPESELECTIONITEMMODELFACTORY_H
|
||||||
|
|
||||||
|
#include "AbstractModelFactory.h"
|
||||||
|
|
||||||
|
class PgTypeContainer;
|
||||||
|
void setTypeList();
|
||||||
|
|
||||||
|
class TypeSelectionItemModelFactory: public AbstractModelFactory {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
TypeSelectionItemModelFactory(QObject *parent, std::shared_ptr<const PgTypeContainer> types);
|
||||||
|
|
||||||
|
virtual QAbstractItemModel* createModel(QObject *parent = nullptr) const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<const PgTypeContainer> m_types;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // TYPESELECTIONITEMMODELFACTORY_H
|
||||||
Loading…
Add table
Add a link
Reference in a new issue