310 lines
10 KiB
C++
310 lines
10 KiB
C++
#include <QLineEdit>
|
||
#include <QComboBox>
|
||
#include <QSpinBox>
|
||
#include <QCheckBox>
|
||
#include <QCompleter>
|
||
#include <QKeyEvent>
|
||
#include "structDataMeasurementDelegate.h"
|
||
#include "structDataMeasurementModel.h"
|
||
#include "uiCommunicationBus.h"
|
||
|
||
StructDataMeasurementDelegate::StructDataMeasurementDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
|
||
|
||
QWidget* StructDataMeasurementDelegate::createEditor(QWidget* parent,
|
||
const QStyleOptionViewItem& option,
|
||
const QModelIndex& index) const {
|
||
Q_UNUSED(option);
|
||
|
||
int column = index.column();
|
||
|
||
switch (column) {
|
||
case StructDataMeasurementModel::ColConnectPara:
|
||
return createConnectParaEditor(parent);
|
||
|
||
case StructDataMeasurementModel::ColType:
|
||
return createMeasurementTypeEditor(parent);
|
||
case StructDataMeasurementModel::ColSize:
|
||
return createSizeEditor(parent);
|
||
case StructDataMeasurementModel::ColSource:
|
||
return createSourceEditor(parent);
|
||
case StructDataMeasurementModel::ColStation:
|
||
return createTextEditor(parent);
|
||
case StructDataMeasurementModel::ColEquipment:
|
||
return createTextEditor(parent);
|
||
case StructDataMeasurementModel::ColChannel:
|
||
return createTextEditor(parent);
|
||
case StructDataMeasurementModel::ColPacket:
|
||
return createNumberEditor(parent);
|
||
case StructDataMeasurementModel::ColOffset:
|
||
return createNumberEditor(parent);
|
||
case StructDataMeasurementModel::ColEnable:
|
||
return createEnableEditor(parent);
|
||
}
|
||
|
||
return nullptr;
|
||
}
|
||
|
||
void StructDataMeasurementDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
|
||
int column = index.column();
|
||
QVariant value = index.data(Qt::EditRole);
|
||
|
||
switch (column) {
|
||
case StructDataMeasurementModel::ColConnectPara:
|
||
if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
|
||
lineEdit->setText(value.toString());
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColType:
|
||
if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
|
||
int typeValue = value.toInt();
|
||
comboBox->setCurrentIndex(qBound(0, typeValue, 2));
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColSize:
|
||
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
|
||
spinBox->setValue(value.toInt());
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColSource:
|
||
if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
|
||
int sourceValue = value.toInt();
|
||
int index = (sourceValue == 1) ? 0 : 1; // 1: cl3611, 2: 104
|
||
comboBox->setCurrentIndex(index);
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColStation:
|
||
if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
|
||
lineEdit->setText(value.toString());
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColEquipment:
|
||
if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
|
||
lineEdit->setText(value.toString());
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColChannel:
|
||
if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
|
||
lineEdit->setText(value.toString());
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColPacket:
|
||
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
|
||
spinBox->setValue(value.toInt());
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColOffset:
|
||
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
|
||
spinBox->setValue(value.toInt());
|
||
}
|
||
break;
|
||
|
||
case StructDataMeasurementModel::ColEnable:
|
||
if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
|
||
bool res = value.toBool();
|
||
int index = (res) ? 0 : 1; // 0: 启用, 1: 禁用
|
||
comboBox->setCurrentIndex(index);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
|
||
void StructDataMeasurementDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
|
||
const QModelIndex& index) const {
|
||
int column = index.column();
|
||
QVariant newValue;
|
||
|
||
switch (column) {
|
||
case StructDataMeasurementModel::ColConnectPara:
|
||
if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
|
||
newValue = lineEdit->text().trimmed();
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColEquipment:
|
||
if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
|
||
newValue = lineEdit->text().trimmed();
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColChannel:
|
||
if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
|
||
newValue = lineEdit->text().trimmed();
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColStation:
|
||
if (QLineEdit* lineEdit = qobject_cast<QLineEdit*>(editor)) {
|
||
newValue = lineEdit->text().trimmed();
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColType:
|
||
if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
|
||
newValue = comboBox->currentIndex(); // 0:遥测, 1:遥信, 2:遥控
|
||
}
|
||
break;
|
||
|
||
case StructDataMeasurementModel::ColSize:
|
||
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
|
||
newValue = spinBox->value();
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColPacket:
|
||
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
|
||
newValue = spinBox->value();
|
||
}
|
||
break;
|
||
case StructDataMeasurementModel::ColOffset:
|
||
if (QSpinBox* spinBox = qobject_cast<QSpinBox*>(editor)) {
|
||
newValue = spinBox->value();
|
||
}
|
||
break;
|
||
|
||
case StructDataMeasurementModel::ColSource:
|
||
if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
|
||
// 转换文本为数值:cl3611 -> 0, 104 -> 1
|
||
QString text = comboBox->currentText();
|
||
newValue = (text == "cl3611") ? 0 : 1;
|
||
}
|
||
break;
|
||
|
||
case StructDataMeasurementModel::ColEnable:
|
||
if (QComboBox* comboBox = qobject_cast<QComboBox*>(editor)) {
|
||
// 转换文本为数值:启用 -> 1, 禁用-> 0
|
||
QString text = comboBox->currentText();
|
||
newValue = (text == "启用") ? true : false;
|
||
}
|
||
break;
|
||
}
|
||
|
||
if (!newValue.isNull()) {
|
||
model->setData(index, newValue, Qt::EditRole);
|
||
}
|
||
}
|
||
|
||
void StructDataMeasurementDelegate::updateEditorGeometry(QWidget* editor,
|
||
const QStyleOptionViewItem& option,
|
||
const QModelIndex& index) const
|
||
{
|
||
Q_UNUSED(index);
|
||
editor->setGeometry(option.rect);
|
||
}
|
||
|
||
QString StructDataMeasurementDelegate::displayText(const QVariant& value, const QLocale& locale) const {
|
||
Q_UNUSED(locale);
|
||
|
||
// 特殊列显示格式化
|
||
if (value.userType() == QMetaType::Bool) {
|
||
return value.toBool() ? "启用" : "禁用";
|
||
}
|
||
|
||
return QStyledItemDelegate::displayText(value, locale);
|
||
}
|
||
|
||
// 提供工具提示
|
||
QString StructDataMeasurementDelegate::displayText(const QVariant& value, const QLocale& locale,
|
||
const QModelIndex& index) const {
|
||
int column = index.column();
|
||
|
||
if (column == StructDataMeasurementModel::ColType) {
|
||
int type = value.toInt();
|
||
switch (type) {
|
||
case 0: return "遥测";
|
||
case 1: return "遥信";
|
||
case 2: return "遥控";
|
||
default: return QString::number(type);
|
||
}
|
||
} else if (column == StructDataMeasurementModel::ColSource) {
|
||
int source = value.toInt();
|
||
switch (source) {
|
||
case 1: return "cl3611";
|
||
case 2: return "104";
|
||
default: return QString::number(source);
|
||
}
|
||
} else if (column == StructDataMeasurementModel::ColEnable) {
|
||
bool enabled = value.toBool();
|
||
return enabled ? "启用" : "禁用";
|
||
}
|
||
|
||
return displayText(value, locale);
|
||
}
|
||
|
||
void StructDataMeasurementDelegate::onConnectParamChanged(const QString& str) const
|
||
{
|
||
QVariantMap map;
|
||
map.insert("input",str);
|
||
UiCommunicationBus::instance()->sendHttpRequest("/measurement/recommend",QVariant(),"GET",map);
|
||
}
|
||
|
||
bool StructDataMeasurementDelegate::eventFilter(QObject *obj, QEvent *event)
|
||
{
|
||
if (QLineEdit *editor = qobject_cast<QLineEdit*>(obj)){
|
||
if (event->type() == QEvent::KeyPress) {
|
||
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
|
||
|
||
if (keyEvent->key() == Qt::Key_Space) {
|
||
qDebug() << "eventFilter:space";
|
||
onConnectParamChanged(editor->text());
|
||
_connectCompleter->complete();
|
||
return true;
|
||
}
|
||
|
||
if (keyEvent->text() == ".") {
|
||
qDebug() << "eventFilter:dot";
|
||
// 返回false让事件继续传播
|
||
}
|
||
}
|
||
}
|
||
|
||
return QStyledItemDelegate::eventFilter(obj, event);
|
||
}
|
||
|
||
|
||
QWidget* StructDataMeasurementDelegate::createConnectParaEditor(QWidget* parent) const {
|
||
QLineEdit* lineEdit = new QLineEdit(parent);
|
||
lineEdit->setMaxLength(150);
|
||
lineEdit->installEventFilter(const_cast<StructDataMeasurementDelegate*>(this));
|
||
lineEdit->setPlaceholderText("空格获取初始值");
|
||
lineEdit->setCompleter(_connectCompleter);
|
||
|
||
connect(lineEdit, &QLineEdit::textChanged, this, [=](const QString &text) {
|
||
if (text.endsWith(".")) {
|
||
onConnectParamChanged(text);
|
||
}
|
||
});
|
||
|
||
return lineEdit;
|
||
}
|
||
|
||
QWidget* StructDataMeasurementDelegate::createTextEditor(QWidget* parent) const {
|
||
QLineEdit* lineEdit = new QLineEdit(parent);
|
||
lineEdit->setMaxLength(100);
|
||
return lineEdit;
|
||
}
|
||
|
||
QWidget* StructDataMeasurementDelegate::createMeasurementTypeEditor(QWidget* parent) const {
|
||
QComboBox* comboBox = new QComboBox(parent);
|
||
comboBox->addItems({"遥测", "遥信", "遥控"});
|
||
return comboBox;
|
||
}
|
||
|
||
QWidget* StructDataMeasurementDelegate::createSizeEditor(QWidget* parent) const {
|
||
QSpinBox* spinBox = new QSpinBox(parent);
|
||
spinBox->setRange(1, 1000);
|
||
return spinBox;
|
||
}
|
||
|
||
QWidget* StructDataMeasurementDelegate::createSourceEditor(QWidget* parent) const {
|
||
QComboBox* comboBox = new QComboBox(parent);
|
||
comboBox->addItems({"cl3611", "104"});
|
||
return comboBox;
|
||
}
|
||
|
||
QWidget* StructDataMeasurementDelegate::createNumberEditor(QWidget* parent) const {
|
||
QSpinBox* spinBox = new QSpinBox(parent);
|
||
spinBox->setRange(0, 9999);
|
||
return spinBox;
|
||
}
|
||
|
||
QWidget* StructDataMeasurementDelegate::createEnableEditor(QWidget* parent) const {
|
||
QComboBox* comboBox = new QComboBox(parent);
|
||
comboBox->addItems({"启用", "禁用"});
|
||
return comboBox;
|
||
}
|