SelectionEditorFactory for creating combobox selections for use in QTableView.

TypeSelectionItemModelFactory to use the TypeSelectionItemModel with above factory.
This commit is contained in:
eelke 2018-12-15 15:18:19 +01:00
parent f8d61b61f4
commit e44f73166f
7 changed files with 138 additions and 0 deletions

View 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

View 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);
}
}
}

View 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

View file

@ -0,0 +1,71 @@
#include "TypeSelectionItemModel.h"
#include "PgTypeContainer.h"
#include <algorithm>
TypeSelectionItemModel::TypeSelectionItemModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int TypeSelectionItemModel::rowCount(const QModelIndex &/*parent*/) const
{
int result = m_types.size();
// if (!parent.isValid()) {
// }
return result;
}
int TypeSelectionItemModel::columnCount(const QModelIndex &/*parent*/) const
{
int result = 1;
// if (parent.isValid())
// result = 1;
return result;
}
QVariant TypeSelectionItemModel::data(const QModelIndex &index, int role) const
{
QVariant result;
if (index.isValid()) {
int row = index.row();
int column = index.column();
if (role == Qt::DisplayRole) {
//const PgType &tp = m_types->getByIdx(row);
if (column == 0) {
result = m_types[row]; //tp.typname;
// switch (row) {
// case 0: result = "integer"; break;
// case 1: result = "numeric"; break;
// case 2: result = "timestamp"; break;
// case 3: result = "timestamptz"; break;
// case 4: result = "float"; break;
// case 5: result = "double"; break;
// case 6: result = "date"; break;
// case 7: result = "varchar"; break;
// case 8: result = "varchar"; break;
// case 9: result = "varchar"; break;
// }
}
}
}
return result;
}
void TypeSelectionItemModel::setTypeList(std::shared_ptr<const PgTypeContainer> types)
{
beginResetModel();
m_types.clear();
for (const auto &e : *types) {
if (e.category != TypCategory::Array && e.type != "c") {
m_types.push_back(e.name);
}
}
std::sort(m_types.begin(), m_types.end());
//emit dataChanged(this->createIndex(0, 0), this->createIndex(types->count(), 0), QVector<int>() << Qt::DisplayRole);
endResetModel();
}

View file

@ -0,0 +1,25 @@
#ifndef TYPESELECTIONITEMMODEL_H
#define TYPESELECTIONITEMMODEL_H
#include <QAbstractListModel>
#include <memory>
#include <vector>
class PgTypeContainer;
class TypeSelectionItemModel : public QAbstractListModel {
Q_OBJECT
public:
explicit TypeSelectionItemModel(QObject *parent = 0);
void setTypeList(std::shared_ptr<const PgTypeContainer> types);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
private:
std::vector<QString> m_types;
};
#endif // TYPESELECTIONITEMMODEL_H

View 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;
}

View 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