更改、优化attributeTableModel的数据存储结构
This commit is contained in:
parent
9913bfda86
commit
e9a3fd0b45
|
|
@ -17,6 +17,7 @@
|
||||||
#include <QAbstractTableModel>
|
#include <QAbstractTableModel>
|
||||||
#include <QSqlRecord>
|
#include <QSqlRecord>
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
|
#include "messageDialog.h"
|
||||||
|
|
||||||
class AttributeTableModel : public QAbstractTableModel
|
class AttributeTableModel : public QAbstractTableModel
|
||||||
{
|
{
|
||||||
|
|
@ -50,6 +51,15 @@ public:
|
||||||
void setCurrentPage(int);
|
void setCurrentPage(int);
|
||||||
int currentPage() const;
|
int currentPage() const;
|
||||||
int totalPages() const;
|
int totalPages() const;
|
||||||
|
void previousPage();
|
||||||
|
void nextPage();
|
||||||
|
void firstPage();
|
||||||
|
void lastPage();
|
||||||
|
|
||||||
|
//数据操作
|
||||||
|
void setTable(const QString&);
|
||||||
|
void refresh();
|
||||||
|
void addNewRecord();
|
||||||
|
|
||||||
//展示列控制
|
//展示列控制
|
||||||
void setVisibleColumns(const QStringList& columns);
|
void setVisibleColumns(const QStringList& columns);
|
||||||
|
|
@ -58,11 +68,13 @@ public:
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void syncDataStatus(bool, const PaginationInfo&);
|
void syncDataStatus(bool, const PaginationInfo&);
|
||||||
|
void showMessage(MessageDialogType,const QString&,const QString&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct RowData
|
struct RowData
|
||||||
{
|
{
|
||||||
QSqlRecord record;
|
//QSqlRecord record;
|
||||||
|
QVector<QVariant> values;
|
||||||
EditState state = Clean;
|
EditState state = Clean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#define DBBROWSER_H
|
#define DBBROWSER_H
|
||||||
|
|
||||||
#include "global.h"
|
#include "global.h"
|
||||||
|
#include "messageDialog.h"
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
@ -10,6 +11,7 @@ class DatabaseBrowser;
|
||||||
}
|
}
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
class MainWindow;
|
||||||
class AttributeView;
|
class AttributeView;
|
||||||
class DatabaseBrowser : public QWidget
|
class DatabaseBrowser : public QWidget
|
||||||
{
|
{
|
||||||
|
|
@ -19,17 +21,20 @@ public:
|
||||||
DatabaseBrowser(QWidget *parent = nullptr);
|
DatabaseBrowser(QWidget *parent = nullptr);
|
||||||
~DatabaseBrowser();
|
~DatabaseBrowser();
|
||||||
|
|
||||||
|
void setMainWindow(MainWindow*);
|
||||||
void addTab_attribute(const QString&, ModelAttributeGroup&);
|
void addTab_attribute(const QString&, ModelAttributeGroup&);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onTabCloseRequested(int);
|
void onTabCloseRequested(int);
|
||||||
void onCurrentTabChanged(int);
|
void onCurrentTabChanged(int);
|
||||||
void onSyncDataStatus(bool, const PaginationInfo&);
|
void onSyncDataStatus(bool, const PaginationInfo&);
|
||||||
|
void onShowMessage(MessageDialogType,const QString&,const QString&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int tabIndex(const QString&);
|
int tabIndex(const QString&);
|
||||||
|
|
||||||
Ui::DatabaseBrowser *ui;
|
Ui::DatabaseBrowser *ui;
|
||||||
|
MainWindow* m_pMainWindow;
|
||||||
QList<AttributeView> m_attributeViewList;
|
QList<AttributeView> m_attributeViewList;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,8 @@ QVariant AttributeTableModel::data(const QModelIndex& index, int role) const
|
||||||
int dataCol = col - 1;
|
int dataCol = col - 1;
|
||||||
const RowData& rowData = m_currentPageData[row];
|
const RowData& rowData = m_currentPageData[row];
|
||||||
if (role == Qt::DisplayRole || role == Qt::EditRole)
|
if (role == Qt::DisplayRole || role == Qt::EditRole)
|
||||||
return rowData.record.value(dataCol);
|
//return rowData.record.value(dataCol);
|
||||||
|
return rowData.values.value(dataCol);
|
||||||
else if (role == Qt::UserRole + 100) //以100来标识数据是否被编辑,在相关代理Delegate类实现中同步进行处理(文字加粗等效果)
|
else if (role == Qt::UserRole + 100) //以100来标识数据是否被编辑,在相关代理Delegate类实现中同步进行处理(文字加粗等效果)
|
||||||
return (rowData.state != Clean);
|
return (rowData.state != Clean);
|
||||||
}
|
}
|
||||||
|
|
@ -84,7 +85,8 @@ bool AttributeTableModel::setData(const QModelIndex &index, const QVariant &valu
|
||||||
int dataCol = index.column() - 1; //第一列显示了行号
|
int dataCol = index.column() - 1; //第一列显示了行号
|
||||||
//记录修改
|
//记录修改
|
||||||
RowData modifiedRow = m_currentPageData[row];
|
RowData modifiedRow = m_currentPageData[row];
|
||||||
modifiedRow.record.setValue(dataCol, value);
|
//modifiedRow.record.setValue(dataCol, value);
|
||||||
|
modifiedRow.values[dataCol] = value;
|
||||||
modifiedRow.state = (modifiedRow.state == New) ? New : Modified;
|
modifiedRow.state = (modifiedRow.state == New) ? New : Modified;
|
||||||
|
|
||||||
m_modifiedRows[globalRow] = modifiedRow;
|
m_modifiedRows[globalRow] = modifiedRow;
|
||||||
|
|
@ -128,6 +130,8 @@ Qt::ItemFlags AttributeTableModel::flags(const QModelIndex &index) const
|
||||||
|
|
||||||
void AttributeTableModel::loadPageData()
|
void AttributeTableModel::loadPageData()
|
||||||
{
|
{
|
||||||
|
m_currentPageData.clear();
|
||||||
|
|
||||||
if (m_tableName.isEmpty())
|
if (m_tableName.isEmpty())
|
||||||
{
|
{
|
||||||
LOG_ERROR("DB", QString("Attribute table name is empty, load data failed"));
|
LOG_ERROR("DB", QString("Attribute table name is empty, load data failed"));
|
||||||
|
|
@ -147,7 +151,11 @@ void AttributeTableModel::loadPageData()
|
||||||
while(query.next())
|
while(query.next())
|
||||||
{
|
{
|
||||||
RowData data;
|
RowData data;
|
||||||
data.record = query.record();
|
//data.record = query.record();
|
||||||
|
for(int i = 0; i < m_visibleColumns.count(); i++)
|
||||||
|
{
|
||||||
|
data.values.append(query.value(i));
|
||||||
|
}
|
||||||
m_currentPageData.append(data);
|
m_currentPageData.append(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -159,6 +167,66 @@ void AttributeTableModel::loadPageData()
|
||||||
endResetModel();
|
endResetModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AttributeTableModel::setPageSize(int size)
|
||||||
|
{
|
||||||
|
if(m_paginationInfo.entriesPerPage != size && size > 0)
|
||||||
|
{
|
||||||
|
m_paginationInfo.entriesPerPage = size;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int AttributeTableModel::pageSize() const
|
||||||
|
{
|
||||||
|
return m_paginationInfo.entriesPerPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AttributeTableModel::setCurrentPage(int page)
|
||||||
|
{
|
||||||
|
if(m_paginationInfo.currentPage != page && page > 0 && page <= totalPages())
|
||||||
|
{
|
||||||
|
m_paginationInfo.currentPage = page;
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int AttributeTableModel::currentPage() const
|
||||||
|
{
|
||||||
|
return m_paginationInfo.currentPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
int AttributeTableModel::totalPages() const
|
||||||
|
{
|
||||||
|
return qCeil(static_cast<qreal>(m_paginationInfo.totalEntries) / m_paginationInfo.entriesPerPage);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AttributeTableModel::previousPage()
|
||||||
|
{
|
||||||
|
if(m_paginationInfo.entriesPerPage == 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
setCurrentPage(m_paginationInfo.entriesPerPage--);
|
||||||
|
}
|
||||||
|
void AttributeTableModel::nextPage()
|
||||||
|
{
|
||||||
|
if(m_paginationInfo.entriesPerPage == totalPages())
|
||||||
|
return;
|
||||||
|
|
||||||
|
setCurrentPage(m_paginationInfo.entriesPerPage++);
|
||||||
|
}
|
||||||
|
void AttributeTableModel::firstPage()
|
||||||
|
{
|
||||||
|
if(m_paginationInfo.entriesPerPage == 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
setCurrentPage(1);
|
||||||
|
}
|
||||||
|
void AttributeTableModel::lastPage()
|
||||||
|
{
|
||||||
|
if(m_paginationInfo.entriesPerPage == totalPages())
|
||||||
|
return;
|
||||||
|
|
||||||
|
setCurrentPage(totalPages());
|
||||||
|
}
|
||||||
|
|
||||||
void AttributeTableModel::updateTotalCount()
|
void AttributeTableModel::updateTotalCount()
|
||||||
{
|
{
|
||||||
if (m_tableName.isEmpty())
|
if (m_tableName.isEmpty())
|
||||||
|
|
@ -170,6 +238,41 @@ void AttributeTableModel::updateTotalCount()
|
||||||
m_paginationInfo.totalEntries = SqlQueryExecutor::instance().getAttributeCount(m_connection, m_tableName);
|
m_paginationInfo.totalEntries = SqlQueryExecutor::instance().getAttributeCount(m_connection, m_tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AttributeTableModel::setTable(const QString& tableName)
|
||||||
|
{
|
||||||
|
if(m_tableName == tableName)
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_tableName = tableName;
|
||||||
|
m_modifiedRows.clear();
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AttributeTableModel::refresh()
|
||||||
|
{
|
||||||
|
if(!m_modifiedRows.isEmpty())
|
||||||
|
{
|
||||||
|
emit showMessage(type_question, QString::fromWCharArray(L"提示"),
|
||||||
|
QString::fromWCharArray(L"当前有编辑或修改数据未提交,是否进行提交?"));
|
||||||
|
if(g_msgDlgBtn == btn_Yes)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_modifiedRows.clear();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
loadPageData();
|
||||||
|
updateTotalCount();
|
||||||
|
emit syncDataStatus(m_modifiedRows.isEmpty(), m_paginationInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AttributeTableModel::addNewRecord()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void AttributeTableModel::triggerSyncSignal()
|
void AttributeTableModel::triggerSyncSignal()
|
||||||
{
|
{
|
||||||
emit syncDataStatus(m_modifiedRows.isEmpty(), m_paginationInfo);
|
emit syncDataStatus(m_modifiedRows.isEmpty(), m_paginationInfo);
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
#include "dbBrowser.h"
|
#include "dbBrowser.h"
|
||||||
#include "ui_dbBrowser.h"
|
#include "ui_dbBrowser.h"
|
||||||
|
#include "mainwindow.h"
|
||||||
#include "attributeView.h"
|
#include "attributeView.h"
|
||||||
#include <QTabBar>
|
#include <QTabBar>
|
||||||
|
|
||||||
DatabaseBrowser::DatabaseBrowser(QWidget *parent)
|
DatabaseBrowser::DatabaseBrowser(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
, ui(new Ui::DatabaseBrowser)
|
, ui(new Ui::DatabaseBrowser)
|
||||||
|
, m_pMainWindow(nullptr)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
|
|
@ -18,6 +20,11 @@ DatabaseBrowser::~DatabaseBrowser()
|
||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DatabaseBrowser::setMainWindow(MainWindow* window)
|
||||||
|
{
|
||||||
|
m_pMainWindow = window;
|
||||||
|
}
|
||||||
|
|
||||||
int DatabaseBrowser::tabIndex(const QString& tabText)
|
int DatabaseBrowser::tabIndex(const QString& tabText)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < ui->tabWidget->count(); i++)
|
for(int i = 0; i < ui->tabWidget->count(); i++)
|
||||||
|
|
@ -40,6 +47,7 @@ void DatabaseBrowser::addTab_attribute(const QString& connection, ModelAttribute
|
||||||
}
|
}
|
||||||
|
|
||||||
AttributeView* view = new AttributeView(attributeGroup, ui->tabWidget, connection);
|
AttributeView* view = new AttributeView(attributeGroup, ui->tabWidget, connection);
|
||||||
|
connect(view->model(), &AttributeTableModel::showMessage, this, &DatabaseBrowser::onShowMessage);
|
||||||
connect(view->model(), &AttributeTableModel::syncDataStatus, this, &DatabaseBrowser::onSyncDataStatus);
|
connect(view->model(), &AttributeTableModel::syncDataStatus, this, &DatabaseBrowser::onSyncDataStatus);
|
||||||
index = ui->tabWidget->addTab(view, QIcon(":/img/images/icon_hierarchy.png"), tabText);
|
index = ui->tabWidget->addTab(view, QIcon(":/img/images/icon_hierarchy.png"), tabText);
|
||||||
//添加自定义按钮
|
//添加自定义按钮
|
||||||
|
|
@ -116,3 +124,9 @@ void DatabaseBrowser::onSyncDataStatus(bool hasModifiedData, const PaginationInf
|
||||||
ui->lineEdit->setText(QString::number(paginationInfo.currentPage));
|
ui->lineEdit->setText(QString::number(paginationInfo.currentPage));
|
||||||
ui->lineEdit->setEnabled(true);
|
ui->lineEdit->setEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DatabaseBrowser::onShowMessage(MessageDialogType type,const QString& strTitle,const QString& strContent)
|
||||||
|
{
|
||||||
|
if(m_pMainWindow)
|
||||||
|
m_pMainWindow->showMessageDialog(type, strTitle, strContent);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,7 @@ void MainWindow::initialize()
|
||||||
connect(m_dbManager, &DatabaseManager::connectionStatusChanged, this, &MainWindow::onSIG_connectionStatusChanged);
|
connect(m_dbManager, &DatabaseManager::connectionStatusChanged, this, &MainWindow::onSIG_connectionStatusChanged);
|
||||||
|
|
||||||
m_dbBrowser = new DatabaseBrowser(this);
|
m_dbBrowser = new DatabaseBrowser(this);
|
||||||
|
m_dbBrowser->setMainWindow(this);
|
||||||
ui->layoutAttributeBrowser->addWidget(m_dbBrowser);
|
ui->layoutAttributeBrowser->addWidget(m_dbBrowser);
|
||||||
|
|
||||||
m_pDBStrutureView = new DBStructureView(m_dbManager, this);
|
m_pDBStrutureView = new DBStructureView(m_dbManager, this);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue