更改、优化attributeTableModel的数据存储结构
This commit is contained in:
parent
9913bfda86
commit
e9a3fd0b45
|
|
@ -17,6 +17,7 @@
|
|||
#include <QAbstractTableModel>
|
||||
#include <QSqlRecord>
|
||||
#include "global.h"
|
||||
#include "messageDialog.h"
|
||||
|
||||
class AttributeTableModel : public QAbstractTableModel
|
||||
{
|
||||
|
|
@ -50,6 +51,15 @@ public:
|
|||
void setCurrentPage(int);
|
||||
int currentPage() 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);
|
||||
|
|
@ -58,11 +68,13 @@ public:
|
|||
|
||||
signals:
|
||||
void syncDataStatus(bool, const PaginationInfo&);
|
||||
void showMessage(MessageDialogType,const QString&,const QString&);
|
||||
|
||||
private:
|
||||
struct RowData
|
||||
{
|
||||
QSqlRecord record;
|
||||
//QSqlRecord record;
|
||||
QVector<QVariant> values;
|
||||
EditState state = Clean;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define DBBROWSER_H
|
||||
|
||||
#include "global.h"
|
||||
#include "messageDialog.h"
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
|
@ -10,6 +11,7 @@ class DatabaseBrowser;
|
|||
}
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class MainWindow;
|
||||
class AttributeView;
|
||||
class DatabaseBrowser : public QWidget
|
||||
{
|
||||
|
|
@ -19,17 +21,20 @@ public:
|
|||
DatabaseBrowser(QWidget *parent = nullptr);
|
||||
~DatabaseBrowser();
|
||||
|
||||
void setMainWindow(MainWindow*);
|
||||
void addTab_attribute(const QString&, ModelAttributeGroup&);
|
||||
|
||||
private slots:
|
||||
void onTabCloseRequested(int);
|
||||
void onCurrentTabChanged(int);
|
||||
void onSyncDataStatus(bool, const PaginationInfo&);
|
||||
void onShowMessage(MessageDialogType,const QString&,const QString&);
|
||||
|
||||
private:
|
||||
int tabIndex(const QString&);
|
||||
|
||||
Ui::DatabaseBrowser *ui;
|
||||
MainWindow* m_pMainWindow;
|
||||
QList<AttributeView> m_attributeViewList;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -66,7 +66,8 @@ QVariant AttributeTableModel::data(const QModelIndex& index, int role) const
|
|||
int dataCol = col - 1;
|
||||
const RowData& rowData = m_currentPageData[row];
|
||||
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类实现中同步进行处理(文字加粗等效果)
|
||||
return (rowData.state != Clean);
|
||||
}
|
||||
|
|
@ -84,7 +85,8 @@ bool AttributeTableModel::setData(const QModelIndex &index, const QVariant &valu
|
|||
int dataCol = index.column() - 1; //第一列显示了行号
|
||||
//记录修改
|
||||
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;
|
||||
|
||||
m_modifiedRows[globalRow] = modifiedRow;
|
||||
|
|
@ -128,6 +130,8 @@ Qt::ItemFlags AttributeTableModel::flags(const QModelIndex &index) const
|
|||
|
||||
void AttributeTableModel::loadPageData()
|
||||
{
|
||||
m_currentPageData.clear();
|
||||
|
||||
if (m_tableName.isEmpty())
|
||||
{
|
||||
LOG_ERROR("DB", QString("Attribute table name is empty, load data failed"));
|
||||
|
|
@ -147,7 +151,11 @@ void AttributeTableModel::loadPageData()
|
|||
while(query.next())
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
@ -159,6 +167,66 @@ void AttributeTableModel::loadPageData()
|
|||
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()
|
||||
{
|
||||
if (m_tableName.isEmpty())
|
||||
|
|
@ -170,6 +238,41 @@ void AttributeTableModel::updateTotalCount()
|
|||
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()
|
||||
{
|
||||
emit syncDataStatus(m_modifiedRows.isEmpty(), m_paginationInfo);
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
#include "dbBrowser.h"
|
||||
#include "ui_dbBrowser.h"
|
||||
#include "mainwindow.h"
|
||||
#include "attributeView.h"
|
||||
#include <QTabBar>
|
||||
|
||||
DatabaseBrowser::DatabaseBrowser(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, ui(new Ui::DatabaseBrowser)
|
||||
, m_pMainWindow(nullptr)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
|
|
@ -18,6 +20,11 @@ DatabaseBrowser::~DatabaseBrowser()
|
|||
delete ui;
|
||||
}
|
||||
|
||||
void DatabaseBrowser::setMainWindow(MainWindow* window)
|
||||
{
|
||||
m_pMainWindow = window;
|
||||
}
|
||||
|
||||
int DatabaseBrowser::tabIndex(const QString& tabText)
|
||||
{
|
||||
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);
|
||||
connect(view->model(), &AttributeTableModel::showMessage, this, &DatabaseBrowser::onShowMessage);
|
||||
connect(view->model(), &AttributeTableModel::syncDataStatus, this, &DatabaseBrowser::onSyncDataStatus);
|
||||
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->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);
|
||||
|
||||
m_dbBrowser = new DatabaseBrowser(this);
|
||||
m_dbBrowser->setMainWindow(this);
|
||||
ui->layoutAttributeBrowser->addWidget(m_dbBrowser);
|
||||
|
||||
m_pDBStrutureView = new DBStructureView(m_dbManager, this);
|
||||
|
|
|
|||
Loading…
Reference in New Issue