优化程序结构

This commit is contained in:
duanshengchao 2025-03-25 17:58:48 +08:00
parent f7c4062068
commit ed76612436
10 changed files with 99 additions and 50 deletions

View File

@ -16,6 +16,7 @@
#include <QAbstractTableModel>
#include <QSqlRecord>
#include "global.h"
class AttributeTableModel : public QAbstractTableModel
{
@ -30,10 +31,9 @@ public:
Deleted
};
explicit AttributeTableModel(QObject* parent = nullptr
explicit AttributeTableModel(const ModelAttributeGroup& modelAttributeGroup
, QObject* parent = nullptr
, const QString& connection = ""
, const QString& modelID = ""
, const QString& groupID = ""
, const QString& tableName = "basic.attribute");
~AttributeTableModel();
@ -65,13 +65,10 @@ private:
void updateTotalCount(); // 更新总记录数
QString m_connection;
QString m_modelID;
QString m_groupID;
QString m_tableName;
ModelAttributeGroup m_modelAttributeGroup;
int m_pageSize;
int m_currentPage;
int m_totalCount;
PaginationInfo m_paginationInfo;
QStringList m_visibleColumns;
QList<RowData> m_currentPageData;
QHash<int, RowData> m_modifiedRows; //key:global row number

View File

@ -2,28 +2,31 @@
#define ATTRIBUTEVIEW_H
#include <QWidget>
#include <QTableView>
#include "global.h"
#include "attributeTableModel.h"
class QTableView;
class AttributeTableModel;
class QVBoxLayout;
class AttributeView : public QWidget
{
Q_OBJECT
public:
AttributeView(QWidget *parent = nullptr
AttributeView(const ModelAttributeGroup& modelAttributeGroup
, QWidget* parent = nullptr
, const QString& connection = ""
, const QString& modelID = ""
, const QString& groupID = ""
, const QString& tableName = "basic.attribute");
~AttributeView();
QTableView* view() const { return m_tableView; }
AttributeTableModel* model() const { return m_attributeTableModel; }
void active();
private:
QString m_connection;
QString m_modelID;
QString m_groupID;
QString m_attributeTable;
ModelAttributeGroup m_modelAttributeGroup;
QTableView* m_tableView;
AttributeTableModel* m_attributeTableModel;

View File

@ -22,7 +22,8 @@ public:
void addTab_attribute(const QString&, ModelAttributeGroup&);
private slots:
void onBtnClick_tabCloseBtn(int);
void onTabCloseRequested(int);
void onCurrentTabChanged(int);
private:
int tabIndex(const QString&);

View File

@ -86,6 +86,14 @@ struct ModelAttributeGroup
strGroupName(std::move(strGroupName)){}
};
struct PaginationInfo
{
int totalEntries; //数据条目总数量
int entriesPerPage; //每页条目数量
int totalPages; //总页数
int currentPage; //当前页数
};
class DatabaseException : public std::runtime_error
{
public:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 411 B

After

Width:  |  Height:  |  Size: 485 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 580 B

After

Width:  |  Height:  |  Size: 582 B

View File

@ -2,16 +2,15 @@
#include "logger.h"
#include "sqlQueryExecutor.h"
AttributeTableModel::AttributeTableModel(QObject* parent, const QString& connection, const QString& modelID, const QString& groupID, const QString& tableName)
AttributeTableModel::AttributeTableModel(const ModelAttributeGroup& modelAttributeGroup, QObject* parent, const QString& connection, const QString& tableName)
: QAbstractTableModel(parent)
, m_connection(connection)
, m_modelID(modelID)
, m_groupID(groupID)
, m_tableName(tableName)
, m_modelAttributeGroup(modelAttributeGroup)
{
m_pageSize = 100;
m_currentPage = 1;
m_totalCount = 0;
m_paginationInfo.entriesPerPage = 100;
m_paginationInfo.currentPage = 1;
m_paginationInfo.totalEntries = 0;
m_visibleColumns << "attribute" << "attribute_name" << "data_type_id" << "length_precision" << "default_value";
}
@ -26,7 +25,7 @@ QVariant AttributeTableModel::data(const QModelIndex& index, int role) const
int row = index.row();
int col = index.column();
int globalRow = (m_currentPage - 1) * m_pageSize + row;
int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row;
if(index.column() == 0) //第一列显示行号
{
@ -81,7 +80,7 @@ bool AttributeTableModel::setData(const QModelIndex &index, const QVariant &valu
return false;
int row = index.row();
int globalRow = (m_currentPage - 1) * m_pageSize + row;
int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row;
int dataCol = index.column() - 1; //第一列显示了行号
//记录修改
RowData modifiedRow = m_currentPageData[row];
@ -140,8 +139,8 @@ void AttributeTableModel::loadPageData()
QString strSQL = QString("SELECT %1 FROM %2 LIMIT %3 OFFSET %4")
.arg(m_visibleColumns.join(", "))
.arg(m_tableName)
.arg(m_pageSize)
.arg((m_currentPage - 1) * m_pageSize);
.arg(m_paginationInfo.entriesPerPage)
.arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage);
try
{
QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL);
@ -154,7 +153,7 @@ void AttributeTableModel::loadPageData()
}
catch (const DatabaseException& e)
{
LOG_ERROR("SQL", QString::fromWCharArray(L"获取属性信息失败。modelID:%1, groupID:%2").arg(m_modelID, m_groupID));
LOG_ERROR("SQL", QString::fromWCharArray(L"获取属性信息失败。modelID:%1, groupID:%2").arg(m_modelAttributeGroup.modelID, m_modelAttributeGroup.groupID));
}
endResetModel();
@ -168,5 +167,5 @@ void AttributeTableModel::updateTotalCount()
return;
}
m_totalCount = SqlQueryExecutor::instance().getAttributeCount(m_connection, m_tableName);
m_paginationInfo.totalEntries = SqlQueryExecutor::instance().getAttributeCount(m_connection, m_tableName);
}

View File

@ -1,17 +1,14 @@
#include "attributeView.h"
#include "attributeTableModel.h"
#include <QTableView>
#include <QVBoxLayout>
AttributeView::AttributeView(QWidget* parent, const QString& connection, const QString& modelID, const QString& groupID, const QString& tableName)
AttributeView::AttributeView(const ModelAttributeGroup& modelAttributeGroup, QWidget* parent, const QString& connection, const QString& tableName)
: QWidget(parent)
, m_connection(connection)
, m_modelID(modelID)
, m_groupID(groupID)
, m_attributeTable(tableName)
, m_modelAttributeGroup(modelAttributeGroup)
{
m_tableView = new QTableView(this);
m_attributeTableModel = new AttributeTableModel(this, m_connection, m_modelID, m_groupID, m_attributeTable);
m_attributeTableModel = new AttributeTableModel(m_modelAttributeGroup, this, m_connection, m_attributeTable);
m_tableView->setModel(m_attributeTableModel);
m_vLayout = new QVBoxLayout(this);
@ -23,3 +20,8 @@ AttributeView::AttributeView(QWidget* parent, const QString& connection, const Q
AttributeView::~AttributeView()
{}
void AttributeView::active()
{
}

View File

@ -9,7 +9,8 @@ DatabaseBrowser::DatabaseBrowser(QWidget *parent)
{
ui->setupUi(this);
connect(ui->tabWidget, &QTabWidget::tabCloseRequested, this, &DatabaseBrowser::onBtnClick_tabCloseBtn);
connect(ui->tabWidget, &QTabWidget::tabCloseRequested, this, &DatabaseBrowser::onTabCloseRequested);
connect(ui->tabWidget, &QTabWidget::currentChanged, this, &DatabaseBrowser::onCurrentTabChanged);
}
DatabaseBrowser::~DatabaseBrowser()
@ -38,7 +39,7 @@ void DatabaseBrowser::addTab_attribute(const QString& connection, ModelAttribute
return;
}
AttributeView* view = new AttributeView(ui->tabWidget, connection, QString::number(attributeGroup.modelID), QString::number(attributeGroup.groupID));
AttributeView* view = new AttributeView(attributeGroup, ui->tabWidget, connection);
index = ui->tabWidget->addTab(view, QIcon(":/img/images/icon_hierarchy.png"), tabText);
//添加自定义按钮
/*QPushButton* closeBtn = new QPushButton("");
@ -63,7 +64,7 @@ void DatabaseBrowser::addTab_attribute(const QString& connection, ModelAttribute
ui->tabWidget->setCurrentIndex(index);
}
void DatabaseBrowser::onBtnClick_tabCloseBtn(int index)
void DatabaseBrowser::onTabCloseRequested(int index)
{
/*QObject* sender = QObject::sender();
QPushButton* button = qobject_cast<QPushButton*>(sender);
@ -75,3 +76,20 @@ void DatabaseBrowser::onBtnClick_tabCloseBtn(int index)
ui->tabWidget->removeTab(index);
delete widget;
}
void DatabaseBrowser::onCurrentTabChanged(int index)
{
if(index == -1) //最后一个tab关闭时会触发
{
ui->recordInfo->clear();
return;
}
QString recordInfo = QString::fromWCharArray(L"共 * 条记录");
QWidget* widget = ui->tabWidget->widget(index);
AttributeView* attributeView = qobject_cast<AttributeView*>(widget);
if(attributeView)
{
attributeView->active();
}
}

View File

@ -31,6 +31,9 @@
</property>
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="enabled">
<bool>true</bool>
</property>
<property name="styleSheet">
<string notr="true">QTabBar::close-button {
border-image: url(:/img/images/btn_close_default.png);
@ -278,7 +281,8 @@ QPushButton:pressed
</property>
<property name="icon">
<iconset resource="../resource/PowerModeler.qrc">
<normaloff>:/img/images/icon_close.png</normaloff>:/img/images/icon_close.png</iconset>
<normaloff>:/img/images/icon_close.png</normaloff>
<disabledoff>:/img/images/icon_close_disable.png</disabledoff>:/img/images/icon_close.png</iconset>
</property>
<property name="iconSize">
<size>
@ -322,7 +326,8 @@ QPushButton:pressed
</property>
<property name="icon">
<iconset resource="../resource/PowerModeler.qrc">
<normaloff>:/img/images/icon_refresh3.png</normaloff>:/img/images/icon_refresh3.png</iconset>
<normaloff>:/img/images/icon_refresh3.png</normaloff>
<disabledoff>:/img/images/icon_refresh3_disable.png</disabledoff>:/img/images/icon_refresh3.png</iconset>
</property>
<property name="iconSize">
<size>
@ -340,7 +345,7 @@ QPushButton:pressed
</property>
<property name="sizeHint" stdset="0">
<size>
<width>827</width>
<width>727</width>
<height>20</height>
</size>
</property>
@ -350,13 +355,13 @@ QPushButton:pressed
<widget class="QWidget" name="pageControlPanel" native="true">
<property name="minimumSize">
<size>
<width>200</width>
<width>300</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>200</width>
<width>300</width>
<height>16777215</height>
</size>
</property>
@ -366,7 +371,7 @@ QPushButton:pressed
</property>
<property name="geometry">
<rect>
<x>108</x>
<x>210</x>
<y>5</y>
<width>41</width>
<height>21</height>
@ -401,7 +406,7 @@ background-color: rgb(255, 255, 255);</string>
</property>
<property name="geometry">
<rect>
<x>68</x>
<x>170</x>
<y>5</y>
<width>21</width>
<height>21</height>
@ -445,7 +450,7 @@ background-color: rgb(255, 255, 255);</string>
</property>
<property name="geometry">
<rect>
<x>170</x>
<x>272</x>
<y>5</y>
<width>21</width>
<height>21</height>
@ -489,7 +494,7 @@ background-color: rgb(255, 255, 255);</string>
</property>
<property name="geometry">
<rect>
<x>88</x>
<x>190</x>
<y>5</y>
<width>21</width>
<height>21</height>
@ -533,7 +538,7 @@ background-color: rgb(255, 255, 255);</string>
</property>
<property name="geometry">
<rect>
<x>150</x>
<x>252</x>
<y>5</y>
<width>21</width>
<height>21</height>
@ -571,6 +576,22 @@ background-color: rgb(255, 255, 255);</string>
</size>
</property>
</widget>
<widget class="QLabel" name="recordInfo">
<property name="geometry">
<rect>
<x>12</x>
<y>5</y>
<width>151</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>共 10 条记录</string>
</property>
<property name="alignment">
<set>Qt::AlignmentFlag::AlignRight|Qt::AlignmentFlag::AlignTrailing|Qt::AlignmentFlag::AlignVCenter</set>
</property>
</widget>
</widget>
</item>
</layout>