SelectionEditorFactory + ItemModel + ItemModelFactory combination is working

in new EditTableWidget

(EditTableWidget is very much WIP)
This commit is contained in:
eelke 2018-12-15 20:27:40 +01:00
parent e44f73166f
commit 742fd0a4d3
19 changed files with 419 additions and 80 deletions

View file

@ -30,16 +30,15 @@ void SelectionEditorFactory::setEditorData(QWidget *editor, const QModelIndex &i
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);
}
auto list_model = cmbbx->model();
QModelIndexList indexes = list_model->match(
list_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);
}
}
}
@ -50,9 +49,9 @@ void SelectionEditorFactory::setModelData(QWidget *editor, QAbstractItemModel *m
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));
auto list_model = cmbbx->model();
QVariant d = list_model->data(
list_model->index(cmbbx->currentIndex(), m_keyColumn));
model->setData(index, d);
}
}

View file

@ -411,3 +411,23 @@ QString getConstraintDefinition(const PgDatabaseCatalog &catalog, const PgConstr
}
//rolename=xxxx -- privileges granted to a role
// =xxxx -- privileges granted to PUBLIC
// r -- SELECT ("read")
// w -- UPDATE ("write")
// a -- INSERT ("append")
// d -- DELETE
// D -- TRUNCATE
// x -- REFERENCES
// t -- TRIGGER
// X -- EXECUTE
// U -- USAGE
// C -- CREATE
// c -- CONNECT
// T -- TEMPORARY
// arwdDxt -- ALL PRIVILEGES (for tables, varies for other objects)
// * -- grant option for preceding privilege
// /yyyy -- role that granted this privilege

View file

@ -0,0 +1,15 @@
#include "TypeModelFactory.h"
#include "TypeSelectionItemModel.h"
TypeModelFactory::TypeModelFactory(QObject *parent, std::shared_ptr<const PgTypeContainer> types)
: AbstractModelFactory(parent)
, m_types(types)
{}
QAbstractItemModel* TypeModelFactory::createModel(QObject *parent) const
{
auto model = new TypeModel(parent);
model->setTypeList(m_types);
return model;
}

View file

@ -6,10 +6,10 @@
class PgTypeContainer;
void setTypeList();
class TypeSelectionItemModelFactory: public AbstractModelFactory {
class TypeModelFactory: public AbstractModelFactory {
Q_OBJECT
public:
TypeSelectionItemModelFactory(QObject *parent, std::shared_ptr<const PgTypeContainer> types);
TypeModelFactory(QObject *parent, std::shared_ptr<const PgTypeContainer> types);
virtual QAbstractItemModel* createModel(QObject *parent = nullptr) const override;

View file

@ -9,7 +9,7 @@ TypeSelectionItemModel::TypeSelectionItemModel(QObject *parent)
int TypeSelectionItemModel::rowCount(const QModelIndex &/*parent*/) const
{
int result = m_types.size();
int result = static_cast<int>(m_types.size());
// if (!parent.isValid()) {
// }
@ -18,7 +18,7 @@ int TypeSelectionItemModel::rowCount(const QModelIndex &/*parent*/) const
int TypeSelectionItemModel::columnCount(const QModelIndex &/*parent*/) const
{
int result = 1;
int result = 2;
// if (parent.isValid())
// result = 1;
@ -35,21 +35,8 @@ QVariant TypeSelectionItemModel::data(const QModelIndex &index, int role) const
if (role == Qt::DisplayRole) {
//const PgType &tp = m_types->getByIdx(row);
if (column == 0) {
result = m_types[row]; //tp.typname;
result = m_types[static_cast<size_t>(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;
// }
}
}
}
@ -69,3 +56,48 @@ void TypeSelectionItemModel::setTypeList(std::shared_ptr<const PgTypeContainer>
//emit dataChanged(this->createIndex(0, 0), this->createIndex(types->count(), 0), QVector<int>() << Qt::DisplayRole);
endResetModel();
}
// ----------------
TypeModel::TypeModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int TypeModel::rowCount(const QModelIndex &/*parent*/) const
{
if (m_types)
return static_cast<int>(m_types->count());
return 0;
}
int TypeModel::columnCount(const QModelIndex &/*parent*/) const
{
return colCount;
}
QVariant TypeModel::data(const QModelIndex &index, int role) const
{
if (index.isValid()) {
int row = index.row();
int column = index.column();
if (role == Qt::DisplayRole) {
//const PgType &tp = m_types->getByIdx(row);
auto elem = m_types->getByIdx(row);
switch (column) {
case OidCol: return elem.oid;
case NameCol: return elem.name;
}
}
}
return QVariant();
}
void TypeModel::setTypeList(std::shared_ptr<const PgTypeContainer> types)
{
beginResetModel();
m_types = types;
endResetModel();
}

View file

@ -22,4 +22,26 @@ private:
std::vector<QString> m_types;
};
class TypeModel : public QAbstractListModel {
Q_OBJECT
public:
enum e_Columns : int {
OidCol,
NameCol, //
colCount
};
explicit TypeModel(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::shared_ptr<const PgTypeContainer> m_types;
};
#endif // TYPESELECTIONITEMMODEL_H

View file

@ -1,15 +0,0 @@
#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

@ -75,7 +75,10 @@ codebuilder/StructureTemplate.cpp \
PgCollation.cpp \
PgCollationContainer.cpp \
PgInherits.cpp \
PgInheritsContainer.cpp
PgInheritsContainer.cpp \
TypeSelectionItemModel.cpp \
SelectionEditorFactory.cpp \
TypeModelFactory.cpp
HEADERS += \
Pglablib.h \
@ -132,8 +135,13 @@ codebuilder/StructureTemplate.h \
PgNamespaceObject.h \
PgCollation.h \
PgCollationContainer.h \
AbstractModelFactory.h \
AbstractEditorFactory.h \
PgInherits.h \
PgInheritsContainer.h
PgInheritsContainer.h \
SelectionEditorFactory.h \
TypeSelectionItemModel.h \
TypeModelFactory.h
unix {
target.path = /usr/lib