优化程序结构

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

View File

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

View File

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

View File

@ -86,6 +86,14 @@ struct ModelAttributeGroup
strGroupName(std::move(strGroupName)){} strGroupName(std::move(strGroupName)){}
}; };
struct PaginationInfo
{
int totalEntries; //数据条目总数量
int entriesPerPage; //每页条目数量
int totalPages; //总页数
int currentPage; //当前页数
};
class DatabaseException : public std::runtime_error class DatabaseException : public std::runtime_error
{ {
public: 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 "logger.h"
#include "sqlQueryExecutor.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) : QAbstractTableModel(parent)
, m_connection(connection) , m_connection(connection)
, m_modelID(modelID)
, m_groupID(groupID)
, m_tableName(tableName) , m_tableName(tableName)
, m_modelAttributeGroup(modelAttributeGroup)
{ {
m_pageSize = 100; m_paginationInfo.entriesPerPage = 100;
m_currentPage = 1; m_paginationInfo.currentPage = 1;
m_totalCount = 0; m_paginationInfo.totalEntries = 0;
m_visibleColumns << "attribute" << "attribute_name" << "data_type_id" << "length_precision" << "default_value"; 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 row = index.row();
int col = index.column(); 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) //第一列显示行号 if(index.column() == 0) //第一列显示行号
{ {
@ -81,7 +80,7 @@ bool AttributeTableModel::setData(const QModelIndex &index, const QVariant &valu
return false; return false;
int row = index.row(); 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; //第一列显示了行号 int dataCol = index.column() - 1; //第一列显示了行号
//记录修改 //记录修改
RowData modifiedRow = m_currentPageData[row]; RowData modifiedRow = m_currentPageData[row];
@ -140,8 +139,8 @@ void AttributeTableModel::loadPageData()
QString strSQL = QString("SELECT %1 FROM %2 LIMIT %3 OFFSET %4") QString strSQL = QString("SELECT %1 FROM %2 LIMIT %3 OFFSET %4")
.arg(m_visibleColumns.join(", ")) .arg(m_visibleColumns.join(", "))
.arg(m_tableName) .arg(m_tableName)
.arg(m_pageSize) .arg(m_paginationInfo.entriesPerPage)
.arg((m_currentPage - 1) * m_pageSize); .arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage);
try try
{ {
QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL); QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL);
@ -154,7 +153,7 @@ void AttributeTableModel::loadPageData()
} }
catch (const DatabaseException& e) 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(); endResetModel();
@ -168,5 +167,5 @@ void AttributeTableModel::updateTotalCount()
return; 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 "attributeView.h"
#include "attributeTableModel.h"
#include <QTableView>
#include <QVBoxLayout> #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) : QWidget(parent)
, m_connection(connection) , m_connection(connection)
, m_modelID(modelID)
, m_groupID(groupID)
, m_attributeTable(tableName) , m_attributeTable(tableName)
, m_modelAttributeGroup(modelAttributeGroup)
{ {
m_tableView = new QTableView(this); 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_tableView->setModel(m_attributeTableModel);
m_vLayout = new QVBoxLayout(this); m_vLayout = new QVBoxLayout(this);
@ -23,3 +20,8 @@ AttributeView::AttributeView(QWidget* parent, const QString& connection, const Q
AttributeView::~AttributeView() AttributeView::~AttributeView()
{} {}
void AttributeView::active()
{
}

View File

@ -9,7 +9,8 @@ DatabaseBrowser::DatabaseBrowser(QWidget *parent)
{ {
ui->setupUi(this); 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() DatabaseBrowser::~DatabaseBrowser()
@ -38,7 +39,7 @@ void DatabaseBrowser::addTab_attribute(const QString& connection, ModelAttribute
return; 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); index = ui->tabWidget->addTab(view, QIcon(":/img/images/icon_hierarchy.png"), tabText);
//添加自定义按钮 //添加自定义按钮
/*QPushButton* closeBtn = new QPushButton(""); /*QPushButton* closeBtn = new QPushButton("");
@ -63,7 +64,7 @@ void DatabaseBrowser::addTab_attribute(const QString& connection, ModelAttribute
ui->tabWidget->setCurrentIndex(index); ui->tabWidget->setCurrentIndex(index);
} }
void DatabaseBrowser::onBtnClick_tabCloseBtn(int index) void DatabaseBrowser::onTabCloseRequested(int index)
{ {
/*QObject* sender = QObject::sender(); /*QObject* sender = QObject::sender();
QPushButton* button = qobject_cast<QPushButton*>(sender); QPushButton* button = qobject_cast<QPushButton*>(sender);
@ -75,3 +76,20 @@ void DatabaseBrowser::onBtnClick_tabCloseBtn(int index)
ui->tabWidget->removeTab(index); ui->tabWidget->removeTab(index);
delete widget; 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> </property>
<item> <item>
<widget class="QTabWidget" name="tabWidget"> <widget class="QTabWidget" name="tabWidget">
<property name="enabled">
<bool>true</bool>
</property>
<property name="styleSheet"> <property name="styleSheet">
<string notr="true">QTabBar::close-button { <string notr="true">QTabBar::close-button {
border-image: url(:/img/images/btn_close_default.png); border-image: url(:/img/images/btn_close_default.png);
@ -278,7 +281,8 @@ QPushButton:pressed
</property> </property>
<property name="icon"> <property name="icon">
<iconset resource="../resource/PowerModeler.qrc"> <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>
<property name="iconSize"> <property name="iconSize">
<size> <size>
@ -322,7 +326,8 @@ QPushButton:pressed
</property> </property>
<property name="icon"> <property name="icon">
<iconset resource="../resource/PowerModeler.qrc"> <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>
<property name="iconSize"> <property name="iconSize">
<size> <size>
@ -340,7 +345,7 @@ QPushButton:pressed
</property> </property>
<property name="sizeHint" stdset="0"> <property name="sizeHint" stdset="0">
<size> <size>
<width>827</width> <width>727</width>
<height>20</height> <height>20</height>
</size> </size>
</property> </property>
@ -350,13 +355,13 @@ QPushButton:pressed
<widget class="QWidget" name="pageControlPanel" native="true"> <widget class="QWidget" name="pageControlPanel" native="true">
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>200</width> <width>300</width>
<height>0</height> <height>0</height>
</size> </size>
</property> </property>
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>200</width> <width>300</width>
<height>16777215</height> <height>16777215</height>
</size> </size>
</property> </property>
@ -366,7 +371,7 @@ QPushButton:pressed
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>108</x> <x>210</x>
<y>5</y> <y>5</y>
<width>41</width> <width>41</width>
<height>21</height> <height>21</height>
@ -401,7 +406,7 @@ background-color: rgb(255, 255, 255);</string>
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>68</x> <x>170</x>
<y>5</y> <y>5</y>
<width>21</width> <width>21</width>
<height>21</height> <height>21</height>
@ -445,7 +450,7 @@ background-color: rgb(255, 255, 255);</string>
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>170</x> <x>272</x>
<y>5</y> <y>5</y>
<width>21</width> <width>21</width>
<height>21</height> <height>21</height>
@ -489,7 +494,7 @@ background-color: rgb(255, 255, 255);</string>
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>88</x> <x>190</x>
<y>5</y> <y>5</y>
<width>21</width> <width>21</width>
<height>21</height> <height>21</height>
@ -533,7 +538,7 @@ background-color: rgb(255, 255, 255);</string>
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>150</x> <x>252</x>
<y>5</y> <y>5</y>
<width>21</width> <width>21</width>
<height>21</height> <height>21</height>
@ -571,6 +576,22 @@ background-color: rgb(255, 255, 255);</string>
</size> </size>
</property> </property>
</widget> </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> </widget>
</item> </item>
</layout> </layout>