2025-04-11 17:14:17 +08:00
|
|
|
|
#include "attributeSelector.h"
|
|
|
|
|
|
#include "ui_attributeSelector.h"
|
|
|
|
|
|
#include "mainwindow.h"
|
|
|
|
|
|
#include "attributeView.h"
|
2025-04-29 17:54:44 +08:00
|
|
|
|
#include "customBorderContainer.h"
|
2025-04-28 14:25:45 +08:00
|
|
|
|
#include "sqlQueryExecutor.h"
|
2025-04-11 17:14:17 +08:00
|
|
|
|
|
|
|
|
|
|
AttributeSelector::AttributeSelector(const QString& connection, QWidget *parent)
|
2025-04-29 17:54:44 +08:00
|
|
|
|
: QDialog(parent)
|
2025-04-11 17:14:17 +08:00
|
|
|
|
, ui(new Ui::AttributeSelector)
|
|
|
|
|
|
, m_connection(connection)
|
|
|
|
|
|
{
|
|
|
|
|
|
ui->setupUi(this);
|
2025-04-29 17:54:44 +08:00
|
|
|
|
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;
|
2025-05-16 18:42:59 +08:00
|
|
|
|
QRegularExpression regExp("[A-Za-z0-9_$]+");
|
2025-04-29 17:54:44 +08:00
|
|
|
|
QRegularExpressionValidator* validator = new QRegularExpressionValidator(regExp, this);
|
|
|
|
|
|
ui->lineEdit_attributeType->setValidator(validator);
|
|
|
|
|
|
ui->lineEdit_attributeType->installEventFilter(this);
|
|
|
|
|
|
|
2025-05-19 11:04:46 +08:00
|
|
|
|
regExp.setPattern("[0-9]+");
|
|
|
|
|
|
validator = new QRegularExpressionValidator(regExp, this);
|
|
|
|
|
|
ui->lineEditPage->setValidator(validator);
|
|
|
|
|
|
ui->lineEditPage->installEventFilter(this);
|
|
|
|
|
|
|
2025-04-11 17:14:17 +08:00
|
|
|
|
//隐藏一些功能按钮
|
|
|
|
|
|
ui->btnAdd->setVisible(false);
|
|
|
|
|
|
ui->btnRemove->setVisible(false);
|
2025-04-30 11:27:07 +08:00
|
|
|
|
//ui->btnSave->setVisible(false);
|
|
|
|
|
|
//ui->btnCancle->setVisible(false);
|
2025-04-29 17:54:44 +08:00
|
|
|
|
ui->btnSearch->setVisible(false);
|
2025-04-11 17:14:17 +08:00
|
|
|
|
|
|
|
|
|
|
ModelAttributeGroup attributeGroup(-1, -1, "" ,"");
|
|
|
|
|
|
m_attributeView = new AttributeView(attributeGroup, ui->attributeViewContainer, connection);
|
2025-04-28 14:25:45 +08:00
|
|
|
|
m_attributeView->setEditable(false);//不可编辑
|
|
|
|
|
|
connect(m_attributeView->model(), &AttributeTableModel::showMessage, this, &AttributeSelector::onShowMessage);
|
|
|
|
|
|
connect(m_attributeView->model(), &AttributeTableModel::syncDataStatus, this, &AttributeSelector::onSyncDataStatus);
|
2025-05-16 18:42:59 +08:00
|
|
|
|
if(m_attributeView->model())
|
|
|
|
|
|
m_attributeView->model()->refresh();
|
|
|
|
|
|
ui->layoutTableView->addWidget(m_attributeView);
|
2025-04-11 17:14:17 +08:00
|
|
|
|
|
2025-04-29 17:54:44 +08:00
|
|
|
|
connect(ui->btnSearch, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_search);
|
2025-04-11 17:14:17 +08:00
|
|
|
|
connect(ui->btnRefresh, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_refreshData);
|
2025-04-29 17:54:44 +08:00
|
|
|
|
connect(ui->btnSave, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_submitSelect);
|
|
|
|
|
|
connect(ui->btnCancle, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_cancleSelect);
|
2025-05-19 11:04:46 +08:00
|
|
|
|
connect(ui->btnFirstPage, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_firstPage);
|
|
|
|
|
|
connect(ui->btnPreviousPage, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_previousPage);
|
|
|
|
|
|
connect(ui->btnLastPage, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_lastPage);
|
|
|
|
|
|
connect(ui->btnNextPage, &QPushButton::clicked, this, &AttributeSelector::onBtnClicked_nextPage);
|
2025-04-29 17:54:44 +08:00
|
|
|
|
//editingFinished在输入的内容为空时不会触发,所以改为在eventFilter中实现
|
2025-05-19 11:04:46 +08:00
|
|
|
|
//connect(ui->lineEditPage, &QLineEdit::editingFinished, this, &AttributeSelector::onEditingFinished_page);
|
2025-04-29 17:54:44 +08:00
|
|
|
|
//connect(ui->lineEdit_attributeType, &QLineEdit::editingFinished, this, &AttributeSelector::onEditingFinished_attributeType);
|
|
|
|
|
|
|
|
|
|
|
|
iniData();
|
2025-04-11 17:14:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AttributeSelector::~AttributeSelector()
|
|
|
|
|
|
{
|
|
|
|
|
|
delete ui;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-28 14:25:45 +08:00
|
|
|
|
void AttributeSelector::iniData()
|
|
|
|
|
|
{
|
|
|
|
|
|
ui->lineEdit_attributeType->setText("");
|
2025-04-29 17:54:44 +08:00
|
|
|
|
m_fliterChars_type = "";
|
2025-04-28 14:25:45 +08:00
|
|
|
|
|
|
|
|
|
|
ui->comboBox_model->clear();
|
2025-04-29 17:54:44 +08:00
|
|
|
|
ui->comboBox_model->addItem(QString::fromWCharArray(L"所有属性"), -1);
|
2025-04-28 14:25:45 +08:00
|
|
|
|
const QVector<Model> models = SqlQueryExecutor::instance().getModels(m_connection);
|
|
|
|
|
|
for(const Model& model : models)
|
|
|
|
|
|
ui->comboBox_model->addItem(model.name, model.id);
|
2025-04-29 17:54:44 +08:00
|
|
|
|
ui->comboBox_model->setCurrentIndex(0);
|
|
|
|
|
|
m_curModelName = ui->comboBox_model->currentText();
|
2025-04-28 14:25:45 +08:00
|
|
|
|
|
|
|
|
|
|
ui->comboBox_group->clear();
|
2025-04-29 17:54:44 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2025-04-28 14:25:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AttributeSelector::showEvent(QShowEvent* e)
|
|
|
|
|
|
{
|
2025-04-29 17:54:44 +08:00
|
|
|
|
if(m_isFirstShow)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_isFirstShow = false;
|
|
|
|
|
|
return QWidget::showEvent(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//存储数据有可能发生变化,所以每次打开都执行刷新操作
|
|
|
|
|
|
//刷新模型数据
|
|
|
|
|
|
disconnect(ui->comboBox_model, &QComboBox::currentTextChanged, this, &AttributeSelector::onComboBoxTextChanged_model); //断开信号
|
2025-04-28 14:25:45 +08:00
|
|
|
|
|
2025-04-29 17:54:44 +08:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
2025-05-19 11:04:46 +08:00
|
|
|
|
else if(obj == ui->lineEditPage)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(event->type() == QEvent::KeyPress)
|
|
|
|
|
|
{
|
|
|
|
|
|
QKeyEvent* pKeyEvent = static_cast<QKeyEvent*>(event);
|
|
|
|
|
|
if (pKeyEvent->key() == Qt::Key_Enter || pKeyEvent->key() == Qt::Key_Return)
|
|
|
|
|
|
{
|
|
|
|
|
|
onEditingFinished_page();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(event->type() == QEvent::FocusOut)
|
|
|
|
|
|
onEditingFinished_page();
|
|
|
|
|
|
}
|
2025-04-29 17:54:44 +08:00
|
|
|
|
return QDialog::eventFilter(obj, event);
|
2025-04-28 14:25:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-11 17:14:17 +08:00
|
|
|
|
void AttributeSelector::setMainWindow(MainWindow* window)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_pMainWindow = window;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 17:54:44 +08:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-11 17:14:17 +08:00
|
|
|
|
void AttributeSelector::onBtnClicked_refreshData()
|
|
|
|
|
|
{
|
2025-04-29 17:54:44 +08:00
|
|
|
|
refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AttributeSelector::onBtnClicked_submitSelect()
|
|
|
|
|
|
{
|
2025-05-08 09:30:47 +08:00
|
|
|
|
QVector<QVector<QVariant>> selectedResult = m_attributeView->model()->getSelectedRowData();
|
|
|
|
|
|
emit completeSelection(selectedResult);
|
2025-04-29 17:54:44 +08:00
|
|
|
|
|
2025-05-08 09:30:47 +08:00
|
|
|
|
close();
|
2025-04-29 17:54:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AttributeSelector::onBtnClicked_cancleSelect()
|
|
|
|
|
|
{
|
|
|
|
|
|
close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-19 11:04:46 +08:00
|
|
|
|
void AttributeSelector::onBtnClicked_firstPage()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(m_attributeView->model())
|
|
|
|
|
|
m_attributeView->model()->firstPage();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AttributeSelector::onBtnClicked_previousPage()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(m_attributeView->model())
|
|
|
|
|
|
m_attributeView->model()->previousPage();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AttributeSelector::onBtnClicked_nextPage()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(m_attributeView->model())
|
|
|
|
|
|
m_attributeView->model()->nextPage();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AttributeSelector::onBtnClicked_lastPage()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(m_attributeView->model())
|
|
|
|
|
|
m_attributeView->model()->lastPage();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AttributeSelector::onEditingFinished_page()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(m_attributeView->model())
|
|
|
|
|
|
{
|
|
|
|
|
|
QString strPage = ui->lineEditPage->text();
|
|
|
|
|
|
if(strPage.isEmpty())
|
|
|
|
|
|
ui->lineEditPage->setText(QString::number(m_attributeView->model()->currentPage()));
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
bool result = m_attributeView->model()->setCurrentPage(strPage.toInt());
|
|
|
|
|
|
if(!result)
|
|
|
|
|
|
ui->lineEditPage->setText(QString::number(m_attributeView->model()->currentPage()));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-29 17:54:44 +08:00
|
|
|
|
void AttributeSelector::onEditingFinished_attributeType()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(m_fliterChars_type != ui->lineEdit_attributeType->text())
|
|
|
|
|
|
refresh();
|
2025-04-11 17:14:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AttributeSelector::onSyncDataStatus(bool hasModifiedData, const PaginationInfo& paginationInfo)
|
|
|
|
|
|
{
|
2025-04-28 14:25:45 +08:00
|
|
|
|
// ui->btnSave->setEnabled(!hasModifiedData);
|
|
|
|
|
|
// ui->btnCancle->setEnabled(!hasModifiedData);
|
2025-04-11 17:14:17 +08:00
|
|
|
|
|
|
|
|
|
|
QString recordInfo = QString::fromWCharArray(L"共 %1 条记录").arg(paginationInfo.totalEntries);
|
|
|
|
|
|
ui->recordInfo->setText(recordInfo);
|
2025-05-19 11:04:46 +08:00
|
|
|
|
ui->lineEditPage->setText(QString::number(paginationInfo.currentPage));
|
|
|
|
|
|
ui->lineEditPage->setEnabled(true);
|
2025-04-11 17:14:17 +08:00
|
|
|
|
}
|
2025-04-28 14:25:45 +08:00
|
|
|
|
|
|
|
|
|
|
void AttributeSelector::onShowMessage(MessageDialogType type,const QString& strTitle,const QString& strContent)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(m_pMainWindow)
|
|
|
|
|
|
m_pMainWindow->showMessageDialog(type, strTitle, strContent);
|
|
|
|
|
|
}
|
|
|
|
|
|
|