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

1
.gitignore vendored
View file

@ -6,3 +6,4 @@ build/*
DIST/
*.autosave
srcdoc/
pglabAll.pro.user.4.8-pre1

View file

@ -0,0 +1,132 @@
#include "EditColumnTableModel.h"
EditColumnTableModel::EditColumnTableModel(QObject *parent)
: QAbstractTableModel(parent)
{
m_data.push_back({});
}
QVariant EditColumnTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole) {
if (orientation == Qt::Horizontal) {
switch(section) {
case NameCol: return tr("Column name");
case TypeCol: return tr("Type");
case LengthCol: return tr("Length");
case ScaleCol: return tr("Scale");
case CollateCol: return tr("Collate");
case NotNullCol: return tr("NN");
case DefaultCol: return tr("Default");
case PrimaryKeyCol: return tr("P");
case UniqueCol: return tr("U");
case CommentCol: return tr("Comment");
}
}
}
return {};
}
int EditColumnTableModel::rowCount(const QModelIndex &) const
{
return static_cast<int>(m_data.size());
}
int EditColumnTableModel::columnCount(const QModelIndex &) const
{
return colCount;
}
QVariant EditColumnTableModel::data(const QModelIndex &index, int role) const
{
size_t rij = static_cast<size_t>(index.row());
auto && d = m_data.at(rij);
if (role == Qt::EditRole || role == Qt::DisplayRole) {
switch (index.column()) {
case NameCol: return d.name;
case TypeCol: return d.type;
case LengthCol: return d.length;
case ScaleCol: return d.scale;
case CollateCol: return d.collate;
case NotNullCol: return d.notNull;
case DefaultCol: return d.def;
case PrimaryKeyCol: return d.primaryKey;
case UniqueCol: return d.unique;
case CommentCol: return d.comment;
}
}
return {};
}
Qt::ItemFlags EditColumnTableModel::flags(const QModelIndex &) const
{
Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
flags |= Qt::ItemIsEditable;
return flags;
}
bool EditColumnTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
size_t rij = static_cast<size_t>(index.row());
auto && d = m_data.at(rij);
if (role == Qt::EditRole || role == Qt::DisplayRole) {
const int col = index.column();
if (col == NameCol) {
d.name = value.toString();
return true;
}
else if (col == TypeCol) {
d.type = value.toInt();
return true;
}
else if (col == LengthCol) {
d.length = value.toInt();
return true;
}
else if (col == ScaleCol) {
d.scale = value.toInt();
return true;
}
else if (col == CollateCol) {
d.collate = value.toInt();
return true;
}
else if (col == NotNullCol) {
d.notNull = value.toBool();
return true;
}
else if (col == DefaultCol) {
d.def = value.toString();
return true;
}
else if (col == PrimaryKeyCol) {
d.primaryKey = value.toInt();
return true;
}
else if (col == UniqueCol) {
d.notNull = value.toBool();
return true;
}
else if (col == CommentCol) {
d.comment = value.toString();
return true;
}
}
return false;
}
bool EditColumnTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
return false;
}
//bool EditColumnTableModel::submit()
//{
//}
//void EditColumnTableModel::revert()
//{
//}

View file

@ -0,0 +1,64 @@
#ifndef EDITCOLUMNTABLEMODEL_H
#define EDITCOLUMNTABLEMODEL_H
#include <QAbstractTableModel>
#include "Pgsql_oids.h"
class Column {
public:
QString name;
Oid type = InvalidOid;
int length = -1; // varchar, date/time, numeric (precision)
int scale = -1; // numeric scale
Oid collate = InvalidOid;
bool notNull = true;
QString def;
int primaryKey = 0;
bool unique = false;
QString comment;
};
class EditColumnTableModel: public QAbstractTableModel {
Q_OBJECT
public:
enum Columns : int {
NameCol = 0,
TypeCol,
LengthCol,
ScaleCol,
CollateCol,
NotNullCol,
DefaultCol,
PrimaryKeyCol,
UniqueCol,
CommentCol,
colCount
};
EditColumnTableModel(QObject *parent = nullptr);
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QVariant data(const QModelIndex &index, int role) const override;
virtual Qt::ItemFlags flags(const QModelIndex &) const override;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override;
virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
public slots:
// virtual bool submit() override;
// virtual void revert() override;
private:
using ColumnList = std::vector<Column>;
ColumnList m_data; ///< Better call it data as columns would be confusing
};
#endif // EDITCOLUMNTABLEMODEL_H

44
pglab/EditTableWidget.cpp Normal file
View file

@ -0,0 +1,44 @@
#include "EditTableWidget.h"
#include "EditColumnTableModel.h"
#include "PgLabItemDelegate.h"
#include <QVBoxLayout>
#include <QTableView>
#include "OpenDatabase.h"
#include "PgDatabaseCatalog.h"
#include "SelectionEditorFactory.h"
#include "TypeModelFactory.h"
EditTableWidget::EditTableWidget(std::shared_ptr<OpenDatabase> database, QWidget *parent)
: QWidget(parent)
, m_database(database)
{
// Table
auto table = new QTableView(this);
// Dialogbutton
auto mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(table);
setLayout(mainLayout);
auto model = new EditColumnTableModel(this);
table->setModel(model);
table->setItemDelegate(new PgLabItemDelegate(this));
auto types = database->catalog()->types();
auto type_delegate = new PgLabItemDelegate(this);
type_delegate->setEditorFactory(
new SelectionEditorFactory(this, new TypeModelFactory(this, types), 0, 1)
);
table->setItemDelegateForColumn(EditColumnTableModel::TypeCol, type_delegate);
// if (opendb) {
// m_typeDelegate.setTypeSelectionModel(opendb->typeSelectionModel());
// }
// paramTableView->setModel(&m_paramList);
// paramTableView->setItemDelegateForColumn(1, &m_typeDelegate);
}

23
pglab/EditTableWidget.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef EDITTABLEWIDGET_H
#define EDITTABLEWIDGET_H
#include <QObject>
#include <QWidget>
#include <memory>
class OpenDatabase;
class EditTableWidget : public QWidget {
Q_OBJECT
public:
explicit EditTableWidget(std::shared_ptr<OpenDatabase> database, QWidget *parent = nullptr);
signals:
public slots:
private:
std::shared_ptr<OpenDatabase> m_database;
};
#endif // EDITTABLEWIDGET_H

View file

@ -20,6 +20,7 @@
#include "CrudTab.h"
#include "WorkManager.h"
#include "ScopeGuard.h"
#include "EditTableWidget.h"
namespace pg = Pgsql;
@ -43,20 +44,22 @@ QueryTab* MainWindow::newSqlPage()
{
QueryTab *qt = new QueryTab(this);
qt->setConfig(m_config, m_database->catalog());
// ui->tabWidget->addTab(qt, "Tab");
// ui->tabWidget->setCurrentWidget(qt);
addPage(qt, "Tab");
qt->newdoc();
qt->focusEditor();
return qt;
}
void MainWindow::newCreateTablePage()
{
auto w = new EditTableWidget(m_database, this);
ui->tabWidget->addTab(w, "Create table");
}
void MainWindow::newCrudPage(const PgClass &table)
{
CrudTab *ct = new CrudTab(this);
ct->setConfig(m_database, table);
// ui->tabWidget->addTab(ct, table.name);
// ui->tabWidget->setCurrentWidget(ct);
addPage(ct, table.objectName());
}
@ -110,6 +113,7 @@ void MainWindow::catalogLoaded()
ui->tabWidget->addTab(functions_page, "Functions");
newSqlPage();
newCreateTablePage();
} catch (std::runtime_error &ex) {
QMessageBox::critical(this, "Error reading database",
QString::fromUtf8(ex.what()));

View file

@ -90,6 +90,7 @@ private:
void closeEvent(QCloseEvent *event);
void showEvent(QShowEvent *event);
QueryTab *newSqlPage();
void newCreateTablePage();
void catalogLoaded();

View file

@ -26,24 +26,11 @@ QWidget *ParamTypeDelegate::createEditor(QWidget *parent,
cmbbx->setModel(m_typeSelectionModel);
w = cmbbx;
// ...
// m_ComboBox->setView(m_ColumnView);
// m_ComboBox->view()->setCornerWidget(new QSizeGrip(m_ColumnView));
// m_ComboBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
// ...
return w;
}
void ParamTypeDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
// if (index.data().canConvert<StarRating>()) {
// StarRating starRating = qvariant_cast<StarRating>(index.data());
// StarEditor *starEditor = qobject_cast<StarEditor *>(editor);
// starEditor->setStarRating(starRating);
// } else {
// QStyledItemDelegate::setEditorData(editor, index);
// }
if (index.column() == 1) {
QComboBox *cmbbx = dynamic_cast<QComboBox*>(editor);
if (cmbbx) {
@ -68,12 +55,6 @@ void ParamTypeDelegate::setEditorData(QWidget *editor, const QModelIndex &index)
void ParamTypeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
// if (index.data().canConvert<StarRating>()) {
// StarEditor *starEditor = qobject_cast<StarEditor *>(editor);
// model->setData(index, QVariant::fromValue(starEditor->starRating()));
// } else {
// QStyledItemDelegate::setModelData(editor, model, index);
// }
if (index.column() == 1) {
QComboBox *cmbbx = dynamic_cast<QComboBox*>(editor);
if (cmbbx) {

View file

@ -1,25 +1,27 @@
#ifndef PARAMTYPEDELEGATE_H
#define PARAMTYPEDELEGATE_H
#include <QStyledItemDelegate>
//#include <QStyledItemDelegate>
#include "PgLabItemDelegate.h"
class TypeSelectionItemModel;
/** Item delegate for supplying a combobox for selected the parameter type in
* the parameter list.
*/
class ParamTypeDelegate : public QStyledItemDelegate {
class ParamTypeDelegate : public PgLabItemDelegate {
Q_OBJECT
public:
ParamTypeDelegate();
~ParamTypeDelegate();
~ParamTypeDelegate() override;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void setTypeSelectionModel(TypeSelectionItemModel* model);
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const override;
void setTypeSelectionModel(TypeSelectionItemModel* model);
private:
TypeSelectionItemModel* m_typeSelectionModel = nullptr;

View file

@ -134,19 +134,23 @@ QWidget *PgLabItemDelegate::createEditor(QWidget *parent, const QStyleOptionView
{
if (m_editorFactory)
return m_editorFactory->createEditor(parent, option, index);
return nullptr;
return QStyledItemDelegate::createEditor(parent, option, index);
}
void PgLabItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (m_editorFactory)
m_editorFactory->setEditorData(editor, index);
else
QStyledItemDelegate::setEditorData(editor, index);
}
void PgLabItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (m_editorFactory)
m_editorFactory->setModelData(editor, model, index);
else
QStyledItemDelegate::setModelData(editor, model, index);
}
AbstractEditorFactory* PgLabItemDelegate::editorFactory()

View file

@ -37,7 +37,6 @@ SOURCES += main.cpp\
stopwatch.cpp \
TuplesResultWidget.cpp \
BackupDialog.cpp \
TypeSelectionItemModel.cpp \
MasterController.cpp \
ParamTypeDelegate.cpp \
OpenDatabase.cpp \
@ -80,7 +79,9 @@ PropertyProxyModel.cpp \
PasswordPromptDialog.cpp \
ProcTableModel.cpp \
FunctionsPage.cpp \
ColumnPage.cpp
ColumnPage.cpp \
EditTableWidget.cpp \
EditColumnTableModel.cpp
HEADERS += \
QueryResultModel.h \
@ -93,7 +94,6 @@ HEADERS += \
stopwatch.h \
TuplesResultWidget.h \
BackupDialog.h \
TypeSelectionItemModel.h \
MasterController.h \
ParamTypeDelegate.h \
OpenDatabase.h \
@ -138,7 +138,9 @@ CustomDataRole.h \
PasswordPromptDialog.h \
ProcTableModel.h \
FunctionsPage.h \
ColumnPage.h
ColumnPage.h \
EditTableWidget.h \
EditColumnTableModel.h
FORMS += mainwindow.ui \
ConnectionManagerWindow.ui \

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