PowerModeler/source/attributeSelector.cpp

309 lines
12 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "attributeSelector.h"
#include "ui_attributeSelector.h"
#include "mainwindow.h"
#include "attributeView.h"
#include "customBorderContainer.h"
#include "sqlQueryExecutor.h"
AttributeSelector::AttributeSelector(const QString& connection, 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(110,110,110);border-radius:5px;background-color:rgb(30,30,30);}");
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);
regExp.setPattern("[0-9]+");
validator = new QRegularExpressionValidator(regExp, this);
ui->lineEditPage->setValidator(validator);
ui->lineEditPage->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);
m_attributeView->setEditable(false);//不可编辑
connect(m_attributeView->model(), &AttributeTableModel::showMessage, this, &AttributeSelector::onShowMessage);
connect(m_attributeView->model(), &AttributeTableModel::syncDataStatus, this, &AttributeSelector::onSyncDataStatus);
if(m_attributeView->model())
m_attributeView->model()->refresh();
ui->layoutTableView->addWidget(m_attributeView);
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);
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);
//editingFinished在输入的内容为空时不会触发所以改为在eventFilter中实现
//connect(ui->lineEditPage, &QLineEdit::editingFinished, this, &AttributeSelector::onEditingFinished_page);
//connect(ui->lineEdit_attributeType, &QLineEdit::editingFinished, this, &AttributeSelector::onEditingFinished_attributeType);
iniData();
}
AttributeSelector::~AttributeSelector()
{
delete ui;
}
void AttributeSelector::iniData()
{
ui->lineEdit_attributeType->setText("");
m_fliterChars_type = "";
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);
ui->comboBox_model->setCurrentIndex(0);
m_curModelName = ui->comboBox_model->currentText();
ui->comboBox_group->clear();
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<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();
}
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();
}
return QDialog::eventFilter(obj, event);
}
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()
{
refresh();
}
void AttributeSelector::onBtnClicked_submitSelect()
{
QVector<QVector<QVariant>> selectedResult = m_attributeView->model()->getSelectedRowData();
emit completeSelection(selectedResult);
close();
}
void AttributeSelector::onBtnClicked_cancleSelect()
{
close();
}
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()));
}
}
}
void AttributeSelector::onEditingFinished_attributeType()
{
if(m_fliterChars_type != ui->lineEdit_attributeType->text())
refresh();
}
void AttributeSelector::onSyncDataStatus(bool hasModifiedData, const PaginationInfo& paginationInfo)
{
// ui->btnSave->setEnabled(!hasModifiedData);
// ui->btnCancle->setEnabled(!hasModifiedData);
QString recordInfo = QString::fromWCharArray(L"共 %1 条记录").arg(paginationInfo.totalEntries);
ui->recordInfo->setText(recordInfo);
ui->lineEditPage->setText(QString::number(paginationInfo.currentPage));
ui->lineEditPage->setEnabled(true);
}
void AttributeSelector::onShowMessage(MessageDialogType type,const QString& strTitle,const QString& strContent)
{
if(m_pMainWindow)
m_pMainWindow->showMessageDialog(type, strTitle, strContent);
}