diff --git a/include/attributeSelector.h b/include/attributeSelector.h index 9a2d3ec..ff6e317 100644 --- a/include/attributeSelector.h +++ b/include/attributeSelector.h @@ -3,7 +3,7 @@ #include "global.h" #include "messageDialog.h" -#include +#include QT_BEGIN_NAMESPACE namespace Ui { @@ -13,8 +13,9 @@ QT_END_NAMESPACE class MainWindow; class AttributeView; +class CustomBorderContainer; -class AttributeSelector : public QWidget +class AttributeSelector : public QDialog { Q_OBJECT @@ -25,20 +26,34 @@ public: void setMainWindow(MainWindow*); protected: - virtual void showEvent(QShowEvent*); + void showEvent(QShowEvent*) override; + bool eventFilter(QObject*, QEvent*) override; private slots: + void onBtnClicked_search(); 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 onShowMessage(MessageDialogType,const QString&,const QString&); private: void iniData(); + void refresh(); Ui::AttributeSelector *ui; MainWindow* m_pMainWindow; AttributeView* m_attributeView; + CustomBorderContainer* m_customBorderContainer; + + bool m_isFirstShow; QString m_connection; + QString m_fliterChars_type; + QString m_curModelName; + QString m_curGroupName; }; #endif //ATTRIBUTESELECTOR_H diff --git a/include/attributeTableModel.h b/include/attributeTableModel.h index f4c0846..6bb86b2 100644 --- a/include/attributeTableModel.h +++ b/include/attributeTableModel.h @@ -19,6 +19,7 @@ #include "global.h" #include "messageDialog.h" +class QItemSelectionModel; class AttributeTableModel : public QAbstractTableModel { Q_OBJECT @@ -55,6 +56,7 @@ public: int columnCount(const QModelIndex& parent = QModelIndex()) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; + void setSelectionModel(QItemSelectionModel*); //ModelAttributeGroup getModelAttributeGroup() {return m_modelAttributeGroup;} void setModelAttributeGroup(const ModelAttributeGroup&); bool attributeTypeExistsInCurrentGroup(int, const QString&); //除数据库外,还有从内存存储的数据(当前页m_currentPageData)中进行查找判断,因为可能有编辑完但未提交至数据空的信息 @@ -76,7 +78,8 @@ public: void refresh(); void forceRefresh(); //强制刷新(不会出现询问提示,数据同步时会用到) void insertRecord(int); - void removeRecord(int); + void removeRecord(); + bool removeRecord(int); void submitChanges(); //提交更改(增、删、改) void cancleChanges(); //取消修改 @@ -87,6 +90,7 @@ public: QMap getDataTypes() {return m_dataTypes;} void triggerSyncSignal(); bool dataHasbeenModified() {return !m_modifiedRows.isEmpty();}; + void setFilterChars_attributeType(const QString& filterChars) {m_filterChars_attributeType = filterChars;} signals: void syncDataStatus(bool, const PaginationInfo&); @@ -115,8 +119,11 @@ private: QList filterRowsByEditState(EditStateFlag); int getDataTypeID(const QString&); + QItemSelectionModel* m_selectionModel; + QString m_connection; QString m_tableName; + QString m_filterChars_attributeType; ModelAttributeGroup m_modelAttributeGroup; PaginationInfo m_paginationInfo; diff --git a/include/dbBrowser.h b/include/dbBrowser.h index ac22166..9317551 100644 --- a/include/dbBrowser.h +++ b/include/dbBrowser.h @@ -32,6 +32,7 @@ private slots: void onSyncDataStatus(bool, const PaginationInfo&); void onShowMessage(MessageDialogType,const QString&,const QString&); + void onBtnClicked_selectRecord(); void onBtnClicked_addRecord(); void onBtnClicked_removeRecord(); void onBtnClicked_submitChanges(); @@ -51,6 +52,9 @@ private: MainWindow* m_pMainWindow; QList m_attributeViewList; int m_previousTabIndex; + +signals: + void openAttributeSelector(); }; #endif //DBBROWSER_H diff --git a/include/mainwindow.h b/include/mainwindow.h index a36f982..4ad2532 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -13,6 +13,7 @@ class DBStructureView; class DBStructureModel; class ModelInfoEditDialog; class GroupSelectionDialog; +class AttributeSelector; QT_BEGIN_NAMESPACE namespace Ui { @@ -49,6 +50,7 @@ private: DBStructureModel* m_pDBStrutureModel; ModelInfoEditDialog* m_pModelInfoDialog; GroupSelectionDialog* m_pGroupSelectionDialog; + AttributeSelector* m_pAttributeSelector; private slots: void onActionTrigger_connect(); @@ -66,6 +68,7 @@ private slots: void onSIG_openAttributeInfo(const QString&, ModelAttributeGroup&); void onSIG_closeAttributeInfo(ModelAttributeGroup&); void onSIG_closeAllAttributeInfo(); + void onSIG_openAttributeSelector(); }; #endif // MAINWINDOW_H diff --git a/include/sqlQueryExecutor.h b/include/sqlQueryExecutor.h index 8f126bd..b28d775 100644 --- a/include/sqlQueryExecutor.h +++ b/include/sqlQueryExecutor.h @@ -31,6 +31,7 @@ public: bool removeAttributeGroup(const QString&, int, int); //模型相关 const QVector getModels(const QString&); + int getModelCount(const QString&); bool addModel(const QString&, Model&); bool modelNameExistsInDB(const QString&, const QString&); bool modelTypeExistsInDB(const QString&, const QString&); diff --git a/source/attributeSelector.cpp b/source/attributeSelector.cpp index ec7f90f..cf77522 100644 --- a/source/attributeSelector.cpp +++ b/source/attributeSelector.cpp @@ -2,19 +2,37 @@ #include "ui_attributeSelector.h" #include "mainwindow.h" #include "attributeView.h" +#include "customBorderContainer.h" #include "sqlQueryExecutor.h" AttributeSelector::AttributeSelector(const QString& connection, QWidget *parent) - : QWidget(parent) + : QDialog(parent) , ui(new Ui::AttributeSelector) , m_connection(connection) { 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->btnRemove->setVisible(false); ui->btnSave->setVisible(false); ui->btnCancle->setVisible(false); + ui->btnSearch->setVisible(false); ModelAttributeGroup attributeGroup(-1, -1, "" ,""); 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::syncDataStatus, this, &AttributeSelector::onSyncDataStatus); + connect(ui->btnSearch, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_search); 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() @@ -34,23 +59,98 @@ AttributeSelector::~AttributeSelector() void AttributeSelector::iniData() { ui->lineEdit_attributeType->setText(""); + m_fliterChars_type = ""; ui->comboBox_model->clear(); - ui->comboBox_model->addItem(QString::fromWCharArray(L"所有模型"), -1); + ui->comboBox_model->addItem(QString::fromWCharArray(L"所有属性"), -1); const QVector models = SqlQueryExecutor::instance().getModels(m_connection); for(const Model& model : models) 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->addItem(QString::fromWCharArray(L"所有属性组"), -1); - const QVector groups = SqlQueryExecutor::instance().getAttributeGroup(m_connection); - for(const AttributeGroup& group : groups) - ui->comboBox_group->addItem(group.name, group.id); + ui->comboBox_group->addItem(QString::fromWCharArray(L"所有属性"), -1); + ui->comboBox_group->setCurrentIndex(0); + m_curGroupName = ui->comboBox_group->currentText(); + + 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) { + 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 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(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) @@ -58,11 +158,72 @@ void AttributeSelector::setMainWindow(MainWindow* 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 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() { - AttributeTableModel* model = m_attributeView->model(); - if(model) - model->forceRefresh(); + refresh(); +} + +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) diff --git a/source/attributeTableDelegate.cpp b/source/attributeTableDelegate.cpp index 250a0db..23f4101 100644 --- a/source/attributeTableDelegate.cpp +++ b/source/attributeTableDelegate.cpp @@ -140,6 +140,24 @@ void AttributeTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem //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); diff --git a/source/attributeTableModel.cpp b/source/attributeTableModel.cpp index 740bc51..51a398f 100644 --- a/source/attributeTableModel.cpp +++ b/source/attributeTableModel.cpp @@ -2,13 +2,16 @@ #include "attributeNamespace.h" #include "logger.h" #include "sqlQueryExecutor.h" +#include AttributeTableModel::AttributeTableModel(const ModelAttributeGroup& modelAttributeGroup, QObject* parent, const QString& connection, const QString& tableName) : QAbstractTableModel(parent) , m_connection(connection) , m_tableName(tableName) , m_modelAttributeGroup(modelAttributeGroup) + , m_selectionModel(nullptr) { + m_filterChars_attributeType = ""; m_paginationInfo.entriesPerPage = 100; m_paginationInfo.currentPage = 1; m_paginationInfo.totalEntries = 0; @@ -189,6 +192,17 @@ Qt::ItemFlags AttributeTableModel::flags(const QModelIndex &index) const 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) { m_modelAttributeGroup = modelAttributeGroup; @@ -350,6 +364,10 @@ void AttributeTableModel::loadPageData() QSqlQuery query = SqlQueryExecutor::instance().executeSQL(m_connection, strSQL); while(query.next()) { + QString type = query.value(0).toString(); + if(!m_filterChars_attributeType.isEmpty() && !type.contains(m_filterChars_attributeType)) + continue; + RowData data; for(int i = 0; i < columns.count(); i++) data.values.append(query.value(i)); @@ -370,8 +388,8 @@ void AttributeTableModel::loadPageData() .arg(m_modelAttributeGroup.groupID) .arg(m_paginationInfo.entriesPerPage) .arg((m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage); - else //利用短路机制,当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") + //利用短路机制,当attribute_group_id!=-1时,括号整体为false,需要进行内部条件判断,=-1时,整体为true,忽略整体,也就忽略了attribute_group_id的条件判断 + 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.groupID) .arg(m_paginationInfo.entriesPerPage) @@ -385,6 +403,9 @@ void AttributeTableModel::loadPageData() attribute.id = query.value(0).toInt(); if(SqlQueryExecutor::instance().getAttributeInfo(m_connection, columns.join(", "), attribute)) { + if(!m_filterChars_attributeType.isEmpty() && !attribute.type.contains(m_filterChars_attributeType)) + continue; + RowData data; data.values.append(attribute.type); data.values.append(attribute.name); @@ -442,28 +463,28 @@ int AttributeTableModel::totalPages() const void AttributeTableModel::previousPage() { - if(m_paginationInfo.entriesPerPage == 1) + if(m_paginationInfo.currentPage == 1) return; - setCurrentPage(m_paginationInfo.entriesPerPage--); + setCurrentPage(m_paginationInfo.currentPage--); } void AttributeTableModel::nextPage() { - if(m_paginationInfo.entriesPerPage == totalPages()) + if(m_paginationInfo.currentPage == totalPages()) return; - setCurrentPage(m_paginationInfo.entriesPerPage++); + setCurrentPage(m_paginationInfo.currentPage++); } void AttributeTableModel::firstPage() { - if(m_paginationInfo.entriesPerPage == 1) + if(m_paginationInfo.currentPage == 1) return; setCurrentPage(1); } void AttributeTableModel::lastPage() { - if(m_paginationInfo.entriesPerPage == totalPages()) + if(m_paginationInfo.currentPage == totalPages()) return; setCurrentPage(totalPages()); @@ -547,10 +568,23 @@ void AttributeTableModel::insertRecord(int row) 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()) - return; + return false; /*emit showMessage(type_question, QString::fromWCharArray(L"提示"), QString::fromWCharArray(L"确认要删除该记录吗?")); @@ -565,6 +599,7 @@ void AttributeTableModel::removeRecord(int row) m_modifiedRows.remove(globalRow); m_currentPageData.removeAt(row); endRemoveRows(); + return true; } else //整行画红线进行标记 { @@ -575,6 +610,7 @@ void AttributeTableModel::removeRecord(int row) QModelIndex topLeft = createIndex(row, 0); QModelIndex bottomRight = createIndex(row, columnCount() - 1); emit dataChanged(topLeft, bottomRight, {Qt::DisplayRole}); + return false; } } diff --git a/source/attributeView.cpp b/source/attributeView.cpp index c5460c3..796f649 100644 --- a/source/attributeView.cpp +++ b/source/attributeView.cpp @@ -24,6 +24,7 @@ AttributeView::AttributeView(const ModelAttributeGroup& modelAttributeGroup, QWi m_attributeTableModel = new AttributeTableModel(m_modelAttributeGroup, this, m_connection, m_attributeTable); m_tableView->setModel(m_attributeTableModel); + m_attributeTableModel->setSelectionModel(m_tableView->selectionModel()); //注意selectionModel()一定要在视图setModel后,否则返回的是空指针 m_attributeTableDelegate = new AttributeTableDelegate(m_tableView, m_connection, m_tableView); m_tableView->setItemDelegate(m_attributeTableDelegate); @@ -45,10 +46,19 @@ AttributeView::AttributeView(const ModelAttributeGroup& modelAttributeGroup, QWi m_multiLinHeader->setSectionLineStyle(i, 1, subTitleStyle); } m_tableView->setHorizontalHeader(m_multiLinHeader); - //除了第一列其余列恢复可以手动调整模式 + //除了第一列其余列恢复可以手动调整模式,并根据外部承载容器的宽度调整section 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++) + { + //恢复手动调整模式 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); diff --git a/source/dbBrowser.cpp b/source/dbBrowser.cpp index 6b213ce..08bc8ed 100644 --- a/source/dbBrowser.cpp +++ b/source/dbBrowser.cpp @@ -21,6 +21,7 @@ DatabaseBrowser::DatabaseBrowser(QWidget *parent) connect(ui->tabWidget, &QTabWidget::tabCloseRequested, this, &DatabaseBrowser::onTabCloseRequested); 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->btnRemove, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_removeRecord); connect(ui->btnSave, &QPushButton::clicked, this, &DatabaseBrowser::onBtnClicked_submitChanges); @@ -151,6 +152,7 @@ void DatabaseBrowser::onCurrentTabChanged(int index) if(index == -1) //最后一个tab关闭时会触发 { + ui->btnSelect->setEnabled(false); ui->btnAdd->setEnabled(false); ui->btnRemove->setEnabled(false); ui->btnCancle->setEnabled(false); @@ -173,6 +175,7 @@ void DatabaseBrowser::onCurrentTabChanged(int index) AttributeView* attributeView = qobject_cast(widget); if(attributeView) { + ui->btnSelect->setEnabled(true); ui->btnAdd->setEnabled(true); ui->btnRemove->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() { QWidget* widget = ui->tabWidget->currentWidget(); @@ -219,14 +227,17 @@ void DatabaseBrowser::onBtnClicked_removeRecord() AttributeTableModel* model = attributeView->model(); if(view && model) { - QModelIndex currentIndex = view->currentIndex(); + /*QModelIndex currentIndex = view->currentIndex(); int row = currentIndex.row(); if( row != -1) { model->removeRecord(row); ui->btnSave->setEnabled(true); ui->btnCancle->setEnabled(true); - } + }*/ + model->removeRecord(); + ui->btnSave->setEnabled(true); + ui->btnCancle->setEnabled(true); } } } diff --git a/source/mainwindow.cpp b/source/mainwindow.cpp index 52a94b4..e0da2ed 100644 --- a/source/mainwindow.cpp +++ b/source/mainwindow.cpp @@ -10,6 +10,7 @@ #include "sqlQueryExecutor.h" #include "modelInfoEditDialog.h" #include "groupSelectionDialog.h" +#include "attributeSelector.h" #include #include @@ -23,6 +24,7 @@ MainWindow::MainWindow(QWidget *parent) , m_pConnectionDialog(nullptr) , m_pModelInfoDialog(nullptr) , m_pGroupSelectionDialog(nullptr) + , m_pAttributeSelector(nullptr) { ui->setupUi(this); @@ -75,6 +77,7 @@ void MainWindow::initialize() m_dbBrowser = new DatabaseBrowser(this); m_dbBrowser->setMainWindow(this); + connect(m_dbBrowser, &DatabaseBrowser::openAttributeSelector, this, &MainWindow::onSIG_openAttributeSelector); ui->layoutAttributeBrowser->addWidget(m_dbBrowser); m_pDBStrutureView = new DBStructureView(m_dbManager, this); @@ -304,3 +307,29 @@ void MainWindow::onSIG_closeAllAttributeInfo() if(m_dbBrowser) 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(); +} diff --git a/source/sqlQueryExecutor.cpp b/source/sqlQueryExecutor.cpp index 9c3b6cc..bdb84cf 100644 --- a/source/sqlQueryExecutor.cpp +++ b/source/sqlQueryExecutor.cpp @@ -213,6 +213,23 @@ const QVector SqlQueryExecutor::getModels(const QString& connectionName) } 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 SqlQueryExecutor::getModelGroups(const QString& connectionName, int modelID) { QVector groups; diff --git a/ui/attributeSelector.ui b/ui/attributeSelector.ui index b11c73a..41977b6 100644 --- a/ui/attributeSelector.ui +++ b/ui/attributeSelector.ui @@ -6,8 +6,8 @@ 0 0 - 1127 - 585 + 1042 + 492 @@ -133,7 +133,7 @@ background-color:rgb(67,160,249); 255 5 113 - 25 + 26 @@ -148,7 +148,7 @@ background-color:rgb(67,160,249); 450 5 113 - 25 + 26 @@ -349,7 +349,49 @@ QPushButton:pressed - false + true + + + + 36 + 5 + 21 + 21 + + + + + 21 + 21 + + + + + 21 + 21 + + + + 完成 + + + + + + + :/img/images/icon_done.png + :/img/images/icon_done_disable.png:/img/images/icon_done.png + + + + 16 + 16 + + + + + + true @@ -372,49 +414,7 @@ QPushButton:pressed - 提交修改 - - - - - - - :/img/images/icon_done.png - :/img/images/icon_done_disable.png:/img/images/icon_done.png - - - - 16 - 16 - - - - - - false - - - - 88 - 5 - 21 - 21 - - - - - 21 - 21 - - - - - 21 - 21 - - - - 取销修改 + 取销 @@ -437,7 +437,7 @@ QPushButton:pressed - 114 + 10 5 21 21 @@ -542,7 +542,7 @@ background-color: rgb(255, 255, 255); - false + true @@ -586,7 +586,7 @@ background-color: rgb(255, 255, 255); - false + true @@ -630,7 +630,7 @@ background-color: rgb(255, 255, 255); - false + true @@ -674,7 +674,7 @@ background-color: rgb(255, 255, 255); - false + true diff --git a/ui/mainwindow.ui b/ui/mainwindow.ui index cff7848..335d58d 100644 --- a/ui/mainwindow.ui +++ b/ui/mainwindow.ui @@ -99,7 +99,6 @@ QTableView::item:hover QTableView::item:selected { color:rgb(0,0,0); - background-color:rgb(211, 241, 250); } QTreeView