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

@ -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 \