完成属性记录的多行选择删除

This commit is contained in:
duanshengchao 2025-04-29 17:54:44 +08:00
parent 5ab836da99
commit 8ba8353a3b
14 changed files with 391 additions and 80 deletions

View File

@ -3,7 +3,7 @@
#include "global.h" #include "global.h"
#include "messageDialog.h" #include "messageDialog.h"
#include <QWidget> #include <QDialog>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { namespace Ui {
@ -13,8 +13,9 @@ QT_END_NAMESPACE
class MainWindow; class MainWindow;
class AttributeView; class AttributeView;
class CustomBorderContainer;
class AttributeSelector : public QWidget class AttributeSelector : public QDialog
{ {
Q_OBJECT Q_OBJECT
@ -25,20 +26,34 @@ public:
void setMainWindow(MainWindow*); void setMainWindow(MainWindow*);
protected: protected:
virtual void showEvent(QShowEvent*); void showEvent(QShowEvent*) override;
bool eventFilter(QObject*, QEvent*) override;
private slots: private slots:
void onBtnClicked_search();
void onBtnClicked_refreshData(); void onBtnClicked_refreshData();
void onBtnClicked_submitSelect();
void onBtnClicked_cancleSelect();
void onEditingFinished_attributeType();
void onComboBoxTextChanged_model(const QString&);
void onComboBoxTextChanged_group(const QString&);
void onSyncDataStatus(bool, const PaginationInfo&); void onSyncDataStatus(bool, const PaginationInfo&);
void onShowMessage(MessageDialogType,const QString&,const QString&); void onShowMessage(MessageDialogType,const QString&,const QString&);
private: private:
void iniData(); void iniData();
void refresh();
Ui::AttributeSelector *ui; Ui::AttributeSelector *ui;
MainWindow* m_pMainWindow; MainWindow* m_pMainWindow;
AttributeView* m_attributeView; AttributeView* m_attributeView;
CustomBorderContainer* m_customBorderContainer;
bool m_isFirstShow;
QString m_connection; QString m_connection;
QString m_fliterChars_type;
QString m_curModelName;
QString m_curGroupName;
}; };
#endif //ATTRIBUTESELECTOR_H #endif //ATTRIBUTESELECTOR_H

View File

@ -19,6 +19,7 @@
#include "global.h" #include "global.h"
#include "messageDialog.h" #include "messageDialog.h"
class QItemSelectionModel;
class AttributeTableModel : public QAbstractTableModel class AttributeTableModel : public QAbstractTableModel
{ {
Q_OBJECT Q_OBJECT
@ -55,6 +56,7 @@ public:
int columnCount(const QModelIndex& parent = QModelIndex()) const override; int columnCount(const QModelIndex& parent = QModelIndex()) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override; Qt::ItemFlags flags(const QModelIndex &index) const override;
void setSelectionModel(QItemSelectionModel*);
//ModelAttributeGroup getModelAttributeGroup() {return m_modelAttributeGroup;} //ModelAttributeGroup getModelAttributeGroup() {return m_modelAttributeGroup;}
void setModelAttributeGroup(const ModelAttributeGroup&); void setModelAttributeGroup(const ModelAttributeGroup&);
bool attributeTypeExistsInCurrentGroup(int, const QString&); //除数据库外,还有从内存存储的数据(当前页m_currentPageData)中进行查找判断,因为可能有编辑完但未提交至数据空的信息 bool attributeTypeExistsInCurrentGroup(int, const QString&); //除数据库外,还有从内存存储的数据(当前页m_currentPageData)中进行查找判断,因为可能有编辑完但未提交至数据空的信息
@ -76,7 +78,8 @@ public:
void refresh(); void refresh();
void forceRefresh(); //强制刷新(不会出现询问提示,数据同步时会用到) void forceRefresh(); //强制刷新(不会出现询问提示,数据同步时会用到)
void insertRecord(int); void insertRecord(int);
void removeRecord(int); void removeRecord();
bool removeRecord(int);
void submitChanges(); //提交更改(增、删、改) void submitChanges(); //提交更改(增、删、改)
void cancleChanges(); //取消修改 void cancleChanges(); //取消修改
@ -87,6 +90,7 @@ public:
QMap<int, DataType> getDataTypes() {return m_dataTypes;} QMap<int, DataType> getDataTypes() {return m_dataTypes;}
void triggerSyncSignal(); void triggerSyncSignal();
bool dataHasbeenModified() {return !m_modifiedRows.isEmpty();}; bool dataHasbeenModified() {return !m_modifiedRows.isEmpty();};
void setFilterChars_attributeType(const QString& filterChars) {m_filterChars_attributeType = filterChars;}
signals: signals:
void syncDataStatus(bool, const PaginationInfo&); void syncDataStatus(bool, const PaginationInfo&);
@ -115,8 +119,11 @@ private:
QList<RowData> filterRowsByEditState(EditStateFlag); QList<RowData> filterRowsByEditState(EditStateFlag);
int getDataTypeID(const QString&); int getDataTypeID(const QString&);
QItemSelectionModel* m_selectionModel;
QString m_connection; QString m_connection;
QString m_tableName; QString m_tableName;
QString m_filterChars_attributeType;
ModelAttributeGroup m_modelAttributeGroup; ModelAttributeGroup m_modelAttributeGroup;
PaginationInfo m_paginationInfo; PaginationInfo m_paginationInfo;

View File

@ -32,6 +32,7 @@ private slots:
void onSyncDataStatus(bool, const PaginationInfo&); void onSyncDataStatus(bool, const PaginationInfo&);
void onShowMessage(MessageDialogType,const QString&,const QString&); void onShowMessage(MessageDialogType,const QString&,const QString&);
void onBtnClicked_selectRecord();
void onBtnClicked_addRecord(); void onBtnClicked_addRecord();
void onBtnClicked_removeRecord(); void onBtnClicked_removeRecord();
void onBtnClicked_submitChanges(); void onBtnClicked_submitChanges();
@ -51,6 +52,9 @@ private:
MainWindow* m_pMainWindow; MainWindow* m_pMainWindow;
QList<AttributeView> m_attributeViewList; QList<AttributeView> m_attributeViewList;
int m_previousTabIndex; int m_previousTabIndex;
signals:
void openAttributeSelector();
}; };
#endif //DBBROWSER_H #endif //DBBROWSER_H

View File

@ -13,6 +13,7 @@ class DBStructureView;
class DBStructureModel; class DBStructureModel;
class ModelInfoEditDialog; class ModelInfoEditDialog;
class GroupSelectionDialog; class GroupSelectionDialog;
class AttributeSelector;
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { namespace Ui {
@ -49,6 +50,7 @@ private:
DBStructureModel* m_pDBStrutureModel; DBStructureModel* m_pDBStrutureModel;
ModelInfoEditDialog* m_pModelInfoDialog; ModelInfoEditDialog* m_pModelInfoDialog;
GroupSelectionDialog* m_pGroupSelectionDialog; GroupSelectionDialog* m_pGroupSelectionDialog;
AttributeSelector* m_pAttributeSelector;
private slots: private slots:
void onActionTrigger_connect(); void onActionTrigger_connect();
@ -66,6 +68,7 @@ private slots:
void onSIG_openAttributeInfo(const QString&, ModelAttributeGroup&); void onSIG_openAttributeInfo(const QString&, ModelAttributeGroup&);
void onSIG_closeAttributeInfo(ModelAttributeGroup&); void onSIG_closeAttributeInfo(ModelAttributeGroup&);
void onSIG_closeAllAttributeInfo(); void onSIG_closeAllAttributeInfo();
void onSIG_openAttributeSelector();
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -31,6 +31,7 @@ public:
bool removeAttributeGroup(const QString&, int, int); bool removeAttributeGroup(const QString&, int, int);
//模型相关 //模型相关
const QVector<Model> getModels(const QString&); const QVector<Model> getModels(const QString&);
int getModelCount(const QString&);
bool addModel(const QString&, Model&); bool addModel(const QString&, Model&);
bool modelNameExistsInDB(const QString&, const QString&); bool modelNameExistsInDB(const QString&, const QString&);
bool modelTypeExistsInDB(const QString&, const QString&); bool modelTypeExistsInDB(const QString&, const QString&);

View File

@ -2,19 +2,37 @@
#include "ui_attributeSelector.h" #include "ui_attributeSelector.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "attributeView.h" #include "attributeView.h"
#include "customBorderContainer.h"
#include "sqlQueryExecutor.h" #include "sqlQueryExecutor.h"
AttributeSelector::AttributeSelector(const QString& connection, QWidget *parent) AttributeSelector::AttributeSelector(const QString& connection, QWidget *parent)
: QWidget(parent) : QDialog(parent)
, ui(new Ui::AttributeSelector) , ui(new Ui::AttributeSelector)
, m_connection(connection) , m_connection(connection)
{ {
ui->setupUi(this); ui->setupUi(this);
if(QSysInfo::kernelType() == "linux")
{
//Linux下默认的Qt::Dialog即使有父窗口也无法按照子窗口的行为进行展示并且最大、最小按钮不好关闭因此需要去掉Dialog属性随之而来的问题是模态无法起作用
setWindowFlags(windowFlags() & ~Qt::Dialog);
setStyleSheet("QDialog{border: 1px solid rgb(205,205,205);border-radius:5px;background-color:rgb(245,245,245);}");
m_customBorderContainer = new CustomBorderContainer(this);
m_customBorderContainer->setOperationOptions(CustomBorderContainer::Movable | CustomBorderContainer::Resizable);
}
m_isFirstShow = true;
QRegularExpression regExp("[A-Za-z0-9_]+");
QRegularExpressionValidator* validator = new QRegularExpressionValidator(regExp, this);
ui->lineEdit_attributeType->setValidator(validator);
ui->lineEdit_attributeType->installEventFilter(this);
//隐藏一些功能按钮 //隐藏一些功能按钮
ui->btnAdd->setVisible(false); ui->btnAdd->setVisible(false);
ui->btnRemove->setVisible(false); ui->btnRemove->setVisible(false);
ui->btnSave->setVisible(false); ui->btnSave->setVisible(false);
ui->btnCancle->setVisible(false); ui->btnCancle->setVisible(false);
ui->btnSearch->setVisible(false);
ModelAttributeGroup attributeGroup(-1, -1, "" ,""); ModelAttributeGroup attributeGroup(-1, -1, "" ,"");
m_attributeView = new AttributeView(attributeGroup, ui->attributeViewContainer, connection); m_attributeView = new AttributeView(attributeGroup, ui->attributeViewContainer, connection);
@ -23,7 +41,14 @@ AttributeSelector::AttributeSelector(const QString& connection, QWidget *parent)
connect(m_attributeView->model(), &AttributeTableModel::showMessage, this, &AttributeSelector::onShowMessage); connect(m_attributeView->model(), &AttributeTableModel::showMessage, this, &AttributeSelector::onShowMessage);
connect(m_attributeView->model(), &AttributeTableModel::syncDataStatus, this, &AttributeSelector::onSyncDataStatus); connect(m_attributeView->model(), &AttributeTableModel::syncDataStatus, this, &AttributeSelector::onSyncDataStatus);
connect(ui->btnSearch, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_search);
connect(ui->btnRefresh, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_refreshData); connect(ui->btnRefresh, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_refreshData);
connect(ui->btnSave, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_submitSelect);
connect(ui->btnCancle, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_cancleSelect);
//editingFinished在输入的内容为空时不会触发所以改为在eventFilter中实现
//connect(ui->lineEdit_attributeType, &QLineEdit::editingFinished, this, &AttributeSelector::onEditingFinished_attributeType);
iniData();
} }
AttributeSelector::~AttributeSelector() AttributeSelector::~AttributeSelector()
@ -34,23 +59,98 @@ AttributeSelector::~AttributeSelector()
void AttributeSelector::iniData() void AttributeSelector::iniData()
{ {
ui->lineEdit_attributeType->setText(""); ui->lineEdit_attributeType->setText("");
m_fliterChars_type = "";
ui->comboBox_model->clear(); ui->comboBox_model->clear();
ui->comboBox_model->addItem(QString::fromWCharArray(L"所有模型"), -1); ui->comboBox_model->addItem(QString::fromWCharArray(L"所有属性"), -1);
const QVector<Model> models = SqlQueryExecutor::instance().getModels(m_connection); const QVector<Model> models = SqlQueryExecutor::instance().getModels(m_connection);
for(const Model& model : models) for(const Model& model : models)
ui->comboBox_model->addItem(model.name, model.id); ui->comboBox_model->addItem(model.name, model.id);
ui->comboBox_model->setCurrentIndex(0);
m_curModelName = ui->comboBox_model->currentText();
ui->comboBox_group->clear(); ui->comboBox_group->clear();
ui->comboBox_group->addItem(QString::fromWCharArray(L"所有属性组"), -1); ui->comboBox_group->addItem(QString::fromWCharArray(L"所有属性"), -1);
const QVector<AttributeGroup> groups = SqlQueryExecutor::instance().getAttributeGroup(m_connection); ui->comboBox_group->setCurrentIndex(0);
for(const AttributeGroup& group : groups) m_curGroupName = ui->comboBox_group->currentText();
ui->comboBox_group->addItem(group.name, group.id);
connect(ui->comboBox_model, &QComboBox::currentTextChanged, this, &AttributeSelector::onComboBoxTextChanged_model);
connect(ui->comboBox_group, &QComboBox::currentTextChanged, this, &AttributeSelector::onComboBoxTextChanged_group);
}
void AttributeSelector::refresh()
{
//qDebug() << "refresh";
m_fliterChars_type = ui->lineEdit_attributeType->text();
int modelID = ui->comboBox_model->currentData().toInt();
m_curModelName = ui->comboBox_model->currentText();
int groupID = ui->comboBox_group->currentData().toInt();
m_curGroupName = ui->comboBox_group->currentText();
ModelAttributeGroup attributeGroup(modelID, groupID, m_curModelName ,m_curGroupName);
AttributeTableModel* model = m_attributeView->model();
if(model)
{
model->setFilterChars_attributeType(m_fliterChars_type);
model->setModelAttributeGroup(attributeGroup);
}
} }
void AttributeSelector::showEvent(QShowEvent* e) void AttributeSelector::showEvent(QShowEvent* e)
{ {
if(m_isFirstShow)
{
m_isFirstShow = false;
return QWidget::showEvent(e);
}
//存储数据有可能发生变化,所以每次打开都执行刷新操作
//刷新模型数据
disconnect(ui->comboBox_model, &QComboBox::currentTextChanged, this, &AttributeSelector::onComboBoxTextChanged_model); //断开信号
ui->comboBox_model->clear();
ui->comboBox_model->addItem(QString::fromWCharArray(L"所有属性"), -1);
const QVector<Model> models = SqlQueryExecutor::instance().getModels(m_connection);
for(const Model& model : models)
ui->comboBox_model->addItem(model.name, model.id);
bool modelExists = true;
if(ui->comboBox_model->currentIndex() != 0)
modelExists = SqlQueryExecutor::instance().modelNameExistsInDB(m_connection, m_curModelName);
if(!modelExists)
{
//更新当前模型,更新前链接信号,同步属性组
connect(ui->comboBox_model, &QComboBox::currentTextChanged, this, &AttributeSelector::onComboBoxTextChanged_model);
ui->comboBox_model->setCurrentIndex(0);
}
else
{
//当前模型并未变化,先定位再链接信号,防止属性组被刷新(因为上面ui->comboBox_model进行了重置因此这里的setCurrentText会触发currentTextChanged信号)
ui->comboBox_model->setCurrentText(m_curModelName);
connect(ui->comboBox_model, &QComboBox::currentTextChanged, this, &AttributeSelector::onComboBoxTextChanged_model);
}
refresh();
QWidget::showEvent(e);
}
bool AttributeSelector::eventFilter(QObject* obj, QEvent* event)
{
if(obj == ui->lineEdit_attributeType)
{
if(event->type() == QEvent::KeyPress)
{
QKeyEvent* pKeyEvent = static_cast<QKeyEvent*>(event);
if (pKeyEvent->key() == Qt::Key_Enter || pKeyEvent->key() == Qt::Key_Return)
{
onEditingFinished_attributeType();
}
}
else if(event->type() == QEvent::FocusOut)
onEditingFinished_attributeType();
}
return QDialog::eventFilter(obj, event);
} }
void AttributeSelector::setMainWindow(MainWindow* window) void AttributeSelector::setMainWindow(MainWindow* window)
@ -58,11 +158,72 @@ void AttributeSelector::setMainWindow(MainWindow* window)
m_pMainWindow = window; m_pMainWindow = window;
} }
void AttributeSelector::onBtnClicked_search()
{
if(m_fliterChars_type != ui->lineEdit_attributeType->text() || m_curModelName != ui->comboBox_model->currentText()
|| m_curGroupName != ui->comboBox_group->currentText()) //只有搜索条件发生了变化才做响应
{
refresh();
}
}
void AttributeSelector::onComboBoxTextChanged_model(const QString& text)
{
//刷新group的combobox所以先断开链接
disconnect(ui->comboBox_group, &QComboBox::currentTextChanged, this, &AttributeSelector::onComboBoxTextChanged_group);
ui->comboBox_group->clear();
if(ui->comboBox_model->currentIndex() == 0)
ui->comboBox_group->addItem(QString::fromWCharArray(L"所有属性"), -1);
else //一个属性可以出现在同一个模型的不同属性组,所以具体模型就不设置‘所有属性组’这一选项了
{
int modelID = ui->comboBox_model->currentData().toInt();
QVector<int> groups = SqlQueryExecutor::instance().getModelGroups(m_connection, modelID);
for(int groupID : groups)
{
AttributeGroup group = SqlQueryExecutor::instance().getAttributeGroupData(m_connection, groupID);
if(group.name.isEmpty())
continue;
ui->comboBox_group->addItem(group.name, group.id);
}
}
if(ui->comboBox_group->count() == 0)
ui->comboBox_group->addItem(QString::fromWCharArray(L"未查询到属性组"), -1);
ui->comboBox_group->setCurrentIndex(0);
m_curGroupName = ui->comboBox_group->currentText();
//刷新完成,重新链接
connect(ui->comboBox_group, &QComboBox::currentTextChanged, this, &AttributeSelector::onComboBoxTextChanged_group);
refresh();
}
void AttributeSelector::onComboBoxTextChanged_group(const QString& text)
{
refresh();
}
void AttributeSelector::onBtnClicked_refreshData() void AttributeSelector::onBtnClicked_refreshData()
{ {
AttributeTableModel* model = m_attributeView->model(); refresh();
if(model) }
model->forceRefresh();
void AttributeSelector::onBtnClicked_submitSelect()
{
}
void AttributeSelector::onBtnClicked_cancleSelect()
{
close();
}
void AttributeSelector::onEditingFinished_attributeType()
{
if(m_fliterChars_type != ui->lineEdit_attributeType->text())
refresh();
} }
void AttributeSelector::onSyncDataStatus(bool hasModifiedData, const PaginationInfo& paginationInfo) void AttributeSelector::onSyncDataStatus(bool hasModifiedData, const PaginationInfo& paginationInfo)

View File

@ -140,6 +140,24 @@ void AttributeTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem
//opt.palette.setColor(QPalette::Text, Qt::red); //opt.palette.setColor(QPalette::Text, Qt::red);
} }
//处理被选择时的背景色注意如果qss中有设置会覆盖此处逻辑
if(option.state.testFlag(QStyle::State_Selected))
{
if(index.column() == 0)
painter->fillRect(opt.rect, QColor(70, 130, 180));
else
painter->fillRect(opt.rect, QColor(211, 241, 250));
}
//如果行号列(第一列)被选中,做整行处理
if(m_tableView && m_tableView->model() && index.column() != 0)
{
QModelIndex numberIndex = m_tableView->model()->index(index.row(), 0);
QItemSelectionModel *selectionModel = m_tableView->selectionModel();
if(selectionModel->isSelected(numberIndex))
painter->fillRect(opt.rect, QColor(211, 241, 250));
}
//先执行默认绘制(包括背景、文本等基础元素) //先执行默认绘制(包括背景、文本等基础元素)
QStyledItemDelegate::paint(painter, opt, index); QStyledItemDelegate::paint(painter, opt, index);

View File

@ -2,13 +2,16 @@
#include "attributeNamespace.h" #include "attributeNamespace.h"
#include "logger.h" #include "logger.h"
#include "sqlQueryExecutor.h" #include "sqlQueryExecutor.h"
#include <QItemSelectionModel>
AttributeTableModel::AttributeTableModel(const ModelAttributeGroup& modelAttributeGroup, QObject* parent, const QString& connection, 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_tableName(tableName) , m_tableName(tableName)
, m_modelAttributeGroup(modelAttributeGroup) , m_modelAttributeGroup(modelAttributeGroup)
, m_selectionModel(nullptr)
{ {
m_filterChars_attributeType = "";
m_paginationInfo.entriesPerPage = 100; m_paginationInfo.entriesPerPage = 100;
m_paginationInfo.currentPage = 1; m_paginationInfo.currentPage = 1;
m_paginationInfo.totalEntries = 0; m_paginationInfo.totalEntries = 0;
@ -189,6 +192,17 @@ Qt::ItemFlags AttributeTableModel::flags(const QModelIndex &index) const
return flags; return flags;
} }
void AttributeTableModel::setSelectionModel(QItemSelectionModel* sm)
{
m_selectionModel = sm;
connect(m_selectionModel, &QItemSelectionModel::selectionChanged, this, [this]{
//刷新整个区域
QModelIndex topLeft = createIndex(0, 0);
QModelIndex bottomRight = createIndex(rowCount() - 1, columnCount() - 1);
emit dataChanged(topLeft, bottomRight);
});
}
void AttributeTableModel::setModelAttributeGroup(const ModelAttributeGroup& modelAttributeGroup) void AttributeTableModel::setModelAttributeGroup(const ModelAttributeGroup& modelAttributeGroup)
{ {
m_modelAttributeGroup = modelAttributeGroup; m_modelAttributeGroup = modelAttributeGroup;
@ -350,6 +364,10 @@ void AttributeTableModel::loadPageData()
QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL); QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL);
while(query.next()) while(query.next())
{ {
QString type = query.value(0).toString();
if(!m_filterChars_attributeType.isEmpty() && !type.contains(m_filterChars_attributeType))
continue;
RowData data; RowData data;
for(int i = 0; i < columns.count(); i++) for(int i = 0; i < columns.count(); i++)
data.values.append(query.value(i)); data.values.append(query.value(i));
@ -370,8 +388,8 @@ void AttributeTableModel::loadPageData()
.arg(m_modelAttributeGroup.groupID) .arg(m_modelAttributeGroup.groupID)
.arg(m_paginationInfo.entriesPerPage) .arg(m_paginationInfo.entriesPerPage)
.arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage); .arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage);
else //利用短路机制当attribute_group_id!=-1时括号整体为false需要进行内部条件判断=-1时整体为true忽略整体也就忽略了attribute_group_id的条件判断 //利用短路机制当attribute_group_id!=-1时括号整体为false需要进行内部条件判断=-1时整体为true忽略整体也就忽略了attribute_group_id的条件判断
strSQL = QString("SELECT attribute_id FROM basic.model_attribute WHERE model_type_id = %1 AND (attribute_group_id = %2 OR %2 = -1) LIMIT %3 OFFSET %4") else strSQL = QString("SELECT attribute_id FROM basic.model_attribute WHERE model_type_id = %1 AND (attribute_group_id = %2 OR %2 = -1) LIMIT %3 OFFSET %4")
.arg(m_modelAttributeGroup.modelID) .arg(m_modelAttributeGroup.modelID)
.arg(m_modelAttributeGroup.groupID) .arg(m_modelAttributeGroup.groupID)
.arg(m_paginationInfo.entriesPerPage) .arg(m_paginationInfo.entriesPerPage)
@ -385,6 +403,9 @@ void AttributeTableModel::loadPageData()
attribute.id = query.value(0).toInt(); attribute.id = query.value(0).toInt();
if(SqlQueryExecutor::instance().getAttributeInfo(m_connection, columns.join(", "), attribute)) if(SqlQueryExecutor::instance().getAttributeInfo(m_connection, columns.join(", "), attribute))
{ {
if(!m_filterChars_attributeType.isEmpty() && !attribute.type.contains(m_filterChars_attributeType))
continue;
RowData data; RowData data;
data.values.append(attribute.type); data.values.append(attribute.type);
data.values.append(attribute.name); data.values.append(attribute.name);
@ -442,28 +463,28 @@ int AttributeTableModel::totalPages() const
void AttributeTableModel::previousPage() void AttributeTableModel::previousPage()
{ {
if(m_paginationInfo.entriesPerPage == 1) if(m_paginationInfo.currentPage == 1)
return; return;
setCurrentPage(m_paginationInfo.entriesPerPage--); setCurrentPage(m_paginationInfo.currentPage--);
} }
void AttributeTableModel::nextPage() void AttributeTableModel::nextPage()
{ {
if(m_paginationInfo.entriesPerPage == totalPages()) if(m_paginationInfo.currentPage == totalPages())
return; return;
setCurrentPage(m_paginationInfo.entriesPerPage++); setCurrentPage(m_paginationInfo.currentPage++);
} }
void AttributeTableModel::firstPage() void AttributeTableModel::firstPage()
{ {
if(m_paginationInfo.entriesPerPage == 1) if(m_paginationInfo.currentPage == 1)
return; return;
setCurrentPage(1); setCurrentPage(1);
} }
void AttributeTableModel::lastPage() void AttributeTableModel::lastPage()
{ {
if(m_paginationInfo.entriesPerPage == totalPages()) if(m_paginationInfo.currentPage == totalPages())
return; return;
setCurrentPage(totalPages()); setCurrentPage(totalPages());
@ -547,10 +568,23 @@ void AttributeTableModel::insertRecord(int row)
endInsertRows(); endInsertRows();
} }
void AttributeTableModel::removeRecord(int row) void AttributeTableModel::removeRecord()
{
//以第一列也就是编号列是否选中为判断依据,找出所有被选中的行
for(int row = 0; row < rowCount(); row++)
{
QModelIndex numberIndex = createIndex(row, 0);
if(m_selectionModel && m_selectionModel->isSelected(numberIndex))
{
if(removeRecord(row))
row--;
}
}
}
bool AttributeTableModel::removeRecord(int row)
{ {
if(row < 0 || row > rowCount()) if(row < 0 || row > rowCount())
return; return false;
/*emit showMessage(type_question, QString::fromWCharArray(L"提示"), /*emit showMessage(type_question, QString::fromWCharArray(L"提示"),
QString::fromWCharArray(L"确认要删除该记录吗?")); QString::fromWCharArray(L"确认要删除该记录吗?"));
@ -565,6 +599,7 @@ void AttributeTableModel::removeRecord(int row)
m_modifiedRows.remove(globalRow); m_modifiedRows.remove(globalRow);
m_currentPageData.removeAt(row); m_currentPageData.removeAt(row);
endRemoveRows(); endRemoveRows();
return true;
} }
else //整行画红线进行标记 else //整行画红线进行标记
{ {
@ -575,6 +610,7 @@ void AttributeTableModel::removeRecord(int row)
QModelIndex topLeft = createIndex(row, 0); QModelIndex topLeft = createIndex(row, 0);
QModelIndex bottomRight = createIndex(row, columnCount() - 1); QModelIndex bottomRight = createIndex(row, columnCount() - 1);
emit dataChanged(topLeft, bottomRight, {Qt::DisplayRole}); emit dataChanged(topLeft, bottomRight, {Qt::DisplayRole});
return false;
} }
} }

View File

@ -24,6 +24,7 @@ AttributeView::AttributeView(const ModelAttributeGroup& modelAttributeGroup, QWi
m_attributeTableModel = new AttributeTableModel(m_modelAttributeGroup, this, m_connection, m_attributeTable); m_attributeTableModel = new AttributeTableModel(m_modelAttributeGroup, this, m_connection, m_attributeTable);
m_tableView->setModel(m_attributeTableModel); m_tableView->setModel(m_attributeTableModel);
m_attributeTableModel->setSelectionModel(m_tableView->selectionModel()); //注意selectionModel()一定要在视图setModel后否则返回的是空指针
m_attributeTableDelegate = new AttributeTableDelegate(m_tableView, m_connection, m_tableView); m_attributeTableDelegate = new AttributeTableDelegate(m_tableView, m_connection, m_tableView);
m_tableView->setItemDelegate(m_attributeTableDelegate); m_tableView->setItemDelegate(m_attributeTableDelegate);
@ -45,10 +46,19 @@ AttributeView::AttributeView(const ModelAttributeGroup& modelAttributeGroup, QWi
m_multiLinHeader->setSectionLineStyle(i, 1, subTitleStyle); m_multiLinHeader->setSectionLineStyle(i, 1, subTitleStyle);
} }
m_tableView->setHorizontalHeader(m_multiLinHeader); m_tableView->setHorizontalHeader(m_multiLinHeader);
//除了第一列其余列恢复可以手动调整模式 //除了第一列其余列恢复可以手动调整模式并根据外部承载容器的宽度调整section
QTimer::singleShot(1000, this, [=](){ QTimer::singleShot(1000, this, [=](){
int containerWidth = this->width() - 100;
int totalSize = m_multiLinHeader->length() - m_multiLinHeader->sectionSize(0);
for(int i = 1; i < m_multiLinHeader->count(); i++) for(int i = 1; i < m_multiLinHeader->count(); i++)
{
//恢复手动调整模式
m_multiLinHeader->setSectionResizeMode(i, QHeaderView::Interactive); m_multiLinHeader->setSectionResizeMode(i, QHeaderView::Interactive);
//根据比例计算每个section应当对应的大小
/*double ratio = m_multiLinHeader->sectionSize(i) / (double)totalSize;
int size = containerWidth * ratio;
m_multiLinHeader->resizeSection(i, size);*/
}
}); });
m_vLayout = new QVBoxLayout(this); m_vLayout = new QVBoxLayout(this);

View File

@ -21,6 +21,7 @@ DatabaseBrowser::DatabaseBrowser(QWidget *parent)
connect(ui->tabWidget, &QTabWidget::tabCloseRequested, this, &DatabaseBrowser::onTabCloseRequested); connect(ui->tabWidget, &QTabWidget::tabCloseRequested, this, &DatabaseBrowser::onTabCloseRequested);
connect(ui->tabWidget, &QTabWidget::currentChanged, this, &DatabaseBrowser::onCurrentTabChanged); connect(ui->tabWidget, &QTabWidget::currentChanged, this, &DatabaseBrowser::onCurrentTabChanged);
connect(ui->btnSelect, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_selectRecord);
connect(ui->btnAdd, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_addRecord); connect(ui->btnAdd, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_addRecord);
connect(ui->btnRemove, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_removeRecord); connect(ui->btnRemove, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_removeRecord);
connect(ui->btnSave, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_submitChanges); connect(ui->btnSave, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_submitChanges);
@ -151,6 +152,7 @@ void DatabaseBrowser::onCurrentTabChanged(int index)
if(index == -1) //最后一个tab关闭时会触发 if(index == -1) //最后一个tab关闭时会触发
{ {
ui->btnSelect->setEnabled(false);
ui->btnAdd->setEnabled(false); ui->btnAdd->setEnabled(false);
ui->btnRemove->setEnabled(false); ui->btnRemove->setEnabled(false);
ui->btnCancle->setEnabled(false); ui->btnCancle->setEnabled(false);
@ -173,6 +175,7 @@ void DatabaseBrowser::onCurrentTabChanged(int index)
AttributeView* attributeView = qobject_cast<AttributeView*>(widget); AttributeView* attributeView = qobject_cast<AttributeView*>(widget);
if(attributeView) if(attributeView)
{ {
ui->btnSelect->setEnabled(true);
ui->btnAdd->setEnabled(true); ui->btnAdd->setEnabled(true);
ui->btnRemove->setEnabled(true); ui->btnRemove->setEnabled(true);
ui->btnCancle->setEnabled(true); ui->btnCancle->setEnabled(true);
@ -188,6 +191,11 @@ void DatabaseBrowser::onCurrentTabChanged(int index)
} }
} }
void DatabaseBrowser::onBtnClicked_selectRecord()
{
emit openAttributeSelector();
}
void DatabaseBrowser::onBtnClicked_addRecord() void DatabaseBrowser::onBtnClicked_addRecord()
{ {
QWidget* widget = ui->tabWidget->currentWidget(); QWidget* widget = ui->tabWidget->currentWidget();
@ -219,14 +227,17 @@ void DatabaseBrowser::onBtnClicked_removeRecord()
AttributeTableModel* model = attributeView->model(); AttributeTableModel* model = attributeView->model();
if(view && model) if(view && model)
{ {
QModelIndex currentIndex = view->currentIndex(); /*QModelIndex currentIndex = view->currentIndex();
int row = currentIndex.row(); int row = currentIndex.row();
if( row != -1) if( row != -1)
{ {
model->removeRecord(row); model->removeRecord(row);
ui->btnSave->setEnabled(true); ui->btnSave->setEnabled(true);
ui->btnCancle->setEnabled(true); ui->btnCancle->setEnabled(true);
} }*/
model->removeRecord();
ui->btnSave->setEnabled(true);
ui->btnCancle->setEnabled(true);
} }
} }
} }

View File

@ -10,6 +10,7 @@
#include "sqlQueryExecutor.h" #include "sqlQueryExecutor.h"
#include "modelInfoEditDialog.h" #include "modelInfoEditDialog.h"
#include "groupSelectionDialog.h" #include "groupSelectionDialog.h"
#include "attributeSelector.h"
#include <QKeyEvent> #include <QKeyEvent>
#include <QScreen> #include <QScreen>
@ -23,6 +24,7 @@ MainWindow::MainWindow(QWidget *parent)
, m_pConnectionDialog(nullptr) , m_pConnectionDialog(nullptr)
, m_pModelInfoDialog(nullptr) , m_pModelInfoDialog(nullptr)
, m_pGroupSelectionDialog(nullptr) , m_pGroupSelectionDialog(nullptr)
, m_pAttributeSelector(nullptr)
{ {
ui->setupUi(this); ui->setupUi(this);
@ -75,6 +77,7 @@ void MainWindow::initialize()
m_dbBrowser = new DatabaseBrowser(this); m_dbBrowser = new DatabaseBrowser(this);
m_dbBrowser->setMainWindow(this); m_dbBrowser->setMainWindow(this);
connect(m_dbBrowser, &DatabaseBrowser::openAttributeSelector, this, &MainWindow::onSIG_openAttributeSelector);
ui->layoutAttributeBrowser->addWidget(m_dbBrowser); ui->layoutAttributeBrowser->addWidget(m_dbBrowser);
m_pDBStrutureView = new DBStructureView(m_dbManager, this); m_pDBStrutureView = new DBStructureView(m_dbManager, this);
@ -304,3 +307,29 @@ void MainWindow::onSIG_closeAllAttributeInfo()
if(m_dbBrowser) if(m_dbBrowser)
m_dbBrowser->closeAllTab_attribute(); m_dbBrowser->closeAllTab_attribute();
} }
void MainWindow::onSIG_openAttributeSelector()
{
if(m_pAttributeSelector && m_pAttributeSelector->isVisible())
return;
if(m_pAttributeSelector == nullptr)
{
m_pAttributeSelector = new AttributeSelector(getCurConnection(), this);
m_pAttributeSelector->setMainWindow(this);
m_pAttributeSelector->installEventFilter(this);
connect(m_pAttributeSelector, &AttributeSelector::finished, this, [=]{ MaskManager::instance()->hideMask(m_pGroupSelectionDialog);});
}
int nX = this->geometry().x() + (this->width() - m_pAttributeSelector->width()) * 0.5;
int nY = this->geometry().y() + (this->height() - m_pAttributeSelector->height()) * 0.5;
m_pAttributeSelector->move(nX, nY);
if(QSysInfo::kernelType() == "linux")
{
MaskManager::instance()->showMask(m_pAttributeSelector);
m_pAttributeSelector->show();
}
else
m_pAttributeSelector->exec();
}

View File

@ -213,6 +213,23 @@ const QVector<Model> SqlQueryExecutor::getModels(const QString& connectionName)
} }
return models; //编译器的RVO/NRVO会自动优化、避免临时拷贝 return models; //编译器的RVO/NRVO会自动优化、避免临时拷贝
} }
int SqlQueryExecutor::getModelCount(const QString& connectionName)
{
int count = 0;
QString strSQL = QString("SELECT COUNT(*) FROM basic.model_type");
try
{
QSqlQuery query = executeSQL(connectionName, strSQL);
if(query.next())
count = query.value(0).toInt();
}
catch (const DatabaseException& e)
{
LOG_ERROR("SQL", QString::fromWCharArray(L"获取模型数量失败"));
}
return count;
}
QVector<int> SqlQueryExecutor::getModelGroups(const QString& connectionName, int modelID) QVector<int> SqlQueryExecutor::getModelGroups(const QString& connectionName, int modelID)
{ {
QVector<int> groups; QVector<int> groups;

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1127</width> <width>1042</width>
<height>585</height> <height>492</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -133,7 +133,7 @@ background-color:rgb(67,160,249);
<x>255</x> <x>255</x>
<y>5</y> <y>5</y>
<width>113</width> <width>113</width>
<height>25</height> <height>26</height>
</rect> </rect>
</property> </property>
<item> <item>
@ -148,7 +148,7 @@ background-color:rgb(67,160,249);
<x>450</x> <x>450</x>
<y>5</y> <y>5</y>
<width>113</width> <width>113</width>
<height>25</height> <height>26</height>
</rect> </rect>
</property> </property>
<item> <item>
@ -349,7 +349,49 @@ QPushButton:pressed
</widget> </widget>
<widget class="QPushButton" name="btnSave"> <widget class="QPushButton" name="btnSave">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>36</x>
<y>5</y>
<width>21</width>
<height>21</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>21</width>
<height>21</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>21</width>
<height>21</height>
</size>
</property>
<property name="toolTip">
<string>完成</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resource/PowerModeler.qrc">
<normaloff>:/img/images/icon_done.png</normaloff>
<disabledoff>:/img/images/icon_done_disable.png</disabledoff>:/img/images/icon_done.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
<widget class="QPushButton" name="btnCancle">
<property name="enabled">
<bool>true</bool>
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
@ -372,49 +414,7 @@ QPushButton:pressed
</size> </size>
</property> </property>
<property name="toolTip"> <property name="toolTip">
<string>提交修改</string> <string>取销</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resource/PowerModeler.qrc">
<normaloff>:/img/images/icon_done.png</normaloff>
<disabledoff>:/img/images/icon_done_disable.png</disabledoff>:/img/images/icon_done.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
<widget class="QPushButton" name="btnCancle">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>88</x>
<y>5</y>
<width>21</width>
<height>21</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>21</width>
<height>21</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>21</width>
<height>21</height>
</size>
</property>
<property name="toolTip">
<string>取销修改</string>
</property> </property>
<property name="text"> <property name="text">
<string/> <string/>
@ -437,7 +437,7 @@ QPushButton:pressed
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>114</x> <x>10</x>
<y>5</y> <y>5</y>
<width>21</width> <width>21</width>
<height>21</height> <height>21</height>
@ -542,7 +542,7 @@ background-color: rgb(255, 255, 255);</string>
</widget> </widget>
<widget class="QPushButton" name="btnFirstPage"> <widget class="QPushButton" name="btnFirstPage">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>true</bool>
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
@ -586,7 +586,7 @@ background-color: rgb(255, 255, 255);</string>
</widget> </widget>
<widget class="QPushButton" name="btnLastPage"> <widget class="QPushButton" name="btnLastPage">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>true</bool>
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
@ -630,7 +630,7 @@ background-color: rgb(255, 255, 255);</string>
</widget> </widget>
<widget class="QPushButton" name="btnPreviousPage"> <widget class="QPushButton" name="btnPreviousPage">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>true</bool>
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>
@ -674,7 +674,7 @@ background-color: rgb(255, 255, 255);</string>
</widget> </widget>
<widget class="QPushButton" name="btnNextPage"> <widget class="QPushButton" name="btnNextPage">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>true</bool>
</property> </property>
<property name="geometry"> <property name="geometry">
<rect> <rect>

View File

@ -99,7 +99,6 @@ QTableView::item:hover
QTableView::item:selected QTableView::item:selected
{ {
color:rgb(0,0,0); color:rgb(0,0,0);
background-color:rgb(211, 241, 250);
} }
QTreeView QTreeView