refactor:调整坐标轴配置UI 及逻辑

This commit is contained in:
duanshengchao 2025-07-15 19:35:37 +08:00
parent 9962a6718e
commit eaaafcbce0
7 changed files with 162 additions and 31 deletions

View File

@ -2,6 +2,7 @@
#define DPBASECHART_H
#include "dpBaseWidget.h"
#include "util/Chart/qcustomplot.h"
class dpBaseChart : public dpBaseWidget
{
@ -27,12 +28,40 @@ protected:
QPen gridPen; //网格线画笔
};
struct Axis //坐标轴配置信息
struct AxisConfig //坐标轴配置信息
{
QString name; //坐标轴名称,例如:电压
QString unit; //坐标轴单位例如V
RealTimeDataType dataType;
};
struct Axis //坐标轴配置信息
{
AxisConfig _cfg;
QCPAxis* qAxis = nullptr;
Axis(const AxisConfig& cfg, QCPAxisRect* rect)
{
applyConfig(cfg);
qAxis = new QCPAxis(rect, QCPAxis::atRight);
}
void applyConfig(const AxisConfig& cfg)
{
_cfg.name = cfg.name;
_cfg.unit = cfg.unit;
_cfg.dataType = cfg.dataType;
}
~Axis()
{
if(qAxis)
{
delete qAxis;
qAxis = nullptr;
}
}
};
};
#endif

View File

@ -230,6 +230,8 @@ void dpConfigurationDialog::setPanel(DataPanel* pPanel)
{
ui->specialSettings->setCurrentIndex(0);
ui->specialSettings->setVisible(true);
ui->axisObject->clear();
m_axisCfgInfo.clear();
break;
}
default:
@ -297,6 +299,7 @@ void dpConfigurationDialog::onBtnClicked_confirm()
copyModelData(m_pModel_dataSelected, m_pDataPanel->m_cofigurationResults.m_pModel_dataSource);
m_pDataPanel->m_cofigurationResults.dataServiceIP = ui->serviceIP->text();
m_pDataPanel->m_cofigurationResults.dataServicePort = ui->servicePort->text().toInt();
m_pDataPanel->m_cofigurationResults.arrangement = (AxisArrangementMode)ui->axisArrangement->currentIndex();
m_pDataPanel->configurationComplete();
}
}
@ -347,6 +350,22 @@ void dpConfigurationDialog::onItemClicked_typeSource(const QModelIndex& index)
m_pModel_typeSelected->appendRow(newItem);
ui->typeSelectedList->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
//其它需要同步配置项
DataPanelType panelType = m_pDataPanel->getType();
switch (panelType)
{
case lineChart:
case curveChart:
case barChart:
{
m_axisCfgInfo.insert(dataType, AxisCfgInfo());
ui->axisObject->addItem(item->text(), dataType);
break;
}
default:
break;
}
if(ui->errorTip->isVisible())
{
ui->errorTip->setVisible(false);
@ -379,6 +398,24 @@ void dpConfigurationDialog::onBtnClicked_remove_type()
for(QStandardItem* item: items)
delete item;
//其它需要同步配置项
DataPanelType panelType = m_pDataPanel->getType();
switch (panelType)
{
case lineChart:
case curveChart:
case barChart:
{
m_axisCfgInfo.remove(itemDataType);
int index = ui->axisObject->findData(itemDataType);
if(index != -1)
ui->axisObject->removeItem(index);
break;
}
default:
break;
}
if(m_pModel_typeSelected->rowCount() == 0)
{
ui->errorTip->setVisible(true);

View File

@ -2,6 +2,7 @@
#define DPCONFIGURATIONDIALOG_H
#include <QDialog>
#include "dpGlobals.h"
QT_BEGIN_NAMESPACE
namespace Ui {
@ -57,6 +58,8 @@ private:
//dataSource
QStandardItemModel* m_pModel_dataSource;
QStandardItemModel* m_pModel_dataSelected;
QHash<RealTimeDataType, AxisCfgInfo> m_axisCfgInfo;
};

View File

@ -21,19 +21,26 @@ enum RealTimeDataType
current //电流
};
enum AxisArrangementMode //坐标轴排列方式
{
AlternateSides = 0, //左右交替排列
AllRight //右侧排列
};
#include <QStandardItemModel>
struct AxisCfgInfo
{
QString name;
QString unit;
};
struct configurationResults
{
QStandardItemModel* m_pModel_dataType;
QStandardItemModel* m_pModel_dataSource;
QString dataServiceIP;
int dataServicePort;
struct Axis
{
QString name;
QString unit;
} axisInfo;
AxisArrangementMode arrangement;
QHash<RealTimeDataType, AxisCfgInfo> axisCfgInfo; //可能有多个轴(数据类别)
configurationResults()
{
@ -43,11 +50,10 @@ struct configurationResults
dataServiceIP = "127.0.0.1";
dataServicePort = 1987;
axisInfo.name = "";
axisInfo.unit = "";
arrangement = AlternateSides;
}
void setParent(QObject* parent)
void setModelParent(QObject* parent) //为QStandardItemModel对象构造对象树从而利用QT的资源自动管理
{
m_pModel_dataType = new QStandardItemModel(parent);
m_pModel_dataSource = new QStandardItemModel(parent);

View File

@ -1,5 +1,4 @@
#include "dpLineChart.h"
#include "util/Chart/qcustomplot.h"
dpLineChart::dpLineChart(QWidget* parent)
:dpBaseChart(parent)
@ -157,16 +156,40 @@ void dpLineChart::viewHistoricalData(const QDateTime& dateTime)
void dpLineChart::synchronizeConfigData(const configurationResults& cfg)
{
//Y坐标轴的数量由数据类型决定
QHash<RealTimeDataType, Axis> cfgAxisMap; //将最新配置信息中的坐标轴相关数据存储在QHash中有助于更好的判断当前坐标轴是否需要发生同步更新
//将最新配置信息中的坐标轴相关数据存储在QHash中有助于更好的判断当前坐标轴是否需要发生同步更新
QHash<RealTimeDataType, AxisConfig> axisCfgMap;
for(int i = 0; i< cfg.m_pModel_dataType->rowCount(); i++)
{
Axis axis;
axis.name = cfg.axisInfo.name;
axis.unit = cfg.axisInfo.unit;
AxisConfig axisConfig;
//axisConfig.name = cfg.axisInfo.name;
//axisConfig.unit = cfg.axisInfo.unit;
RealTimeDataType dataType = (RealTimeDataType)cfg.m_pModel_dataType->item(i, 0)->data(Qt::UserRole + itemRole_dataType).toInt();
axis.dataType = dataType;
cfgAxisMap.insert(dataType, axis);
axisConfig.dataType = dataType;
axisCfgMap.insert(dataType, axisConfig);
}
//QHash::insert()的第二个参数接收的是副本(引起拷贝),所以采用指针可以减少拷贝从而提升效率
QHash<RealTimeDataType, Axis*> axesMap;
for(auto& axis : m_axes)
{
axesMap.insert(axis._cfg.dataType, &axis);
}
//删除轴
for(int i = 0; i < m_axes.size();)
{
Axis& axis = m_axes[i];
if(!axisCfgMap.contains(axis._cfg.dataType))
{
m_pCustomPlot->axisRect()->removeAxis(axis.qAxis);
m_axes.remove(i); //QVector::remove()会调用存储对象的析构函数
}
else
i++;
}
}
void dpLineChart::onSignal_rangeChanged_xAxis(const QCPRange& range)

View File

@ -84,7 +84,7 @@ DataPanel::DataPanel(QWidget *parent, DataPanelType type)
m_pToolMenu->addAction(QString::fromWCharArray(L"放置最后"), this, SLOT(onAction_moveToBack()));
createDataWidget();
m_cofigurationResults.setParent(this);
m_cofigurationResults.setModelParent(this);
connect(DataManager::instance(), &DataManager::dataUpdated, this, &DataPanel::onSignal_dataUpdated);
}

View File

@ -82,7 +82,7 @@ QComboBox QAbstractItemView
{
outline: 0px; /*去除选中虚线框 */
border:0px;
background-color:rgba(25,25,25,240);
background-color:rgba(25,25,25,220);
}
QComboBox QAbstractItemView::item:hover
{
@ -91,7 +91,7 @@ background-color: rgb(43, 102, 158);
QComboBox QAbstractItemView::item:selected
{
color: rgb(250, 250, 250);
background-color: rgb(43, 102, 158);
background-color: rgba(43, 102, 158, 80);
}
QTreeView
@ -613,24 +613,24 @@ background-color: rgb(24, 32, 38);
background-color:transparent;
}</string>
</property>
<widget class="QLabel" name="setting_Server_2">
<widget class="QLabel" name="label_axisObject">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>41</width>
<y>35</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Y轴</string>
<string>Y轴配置</string>
</property>
</widget>
<widget class="QComboBox" name="comboBox">
<widget class="QComboBox" name="axisObject">
<property name="geometry">
<rect>
<x>40</x>
<y>0</y>
<x>70</x>
<y>35</y>
<width>91</width>
<height>22</height>
</rect>
@ -655,7 +655,7 @@ background-color:transparent;
<property name="geometry">
<rect>
<x>30</x>
<y>35</y>
<y>70</y>
<width>41</width>
<height>21</height>
</rect>
@ -668,7 +668,7 @@ background-color:transparent;
<property name="geometry">
<rect>
<x>80</x>
<y>35</y>
<y>70</y>
<width>71</width>
<height>21</height>
</rect>
@ -678,7 +678,7 @@ background-color:transparent;
<property name="geometry">
<rect>
<x>190</x>
<y>35</y>
<y>70</y>
<width>41</width>
<height>21</height>
</rect>
@ -691,12 +691,45 @@ background-color:transparent;
<property name="geometry">
<rect>
<x>240</x>
<y>35</y>
<y>70</y>
<width>71</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QComboBox" name="axisArrangement">
<property name="geometry">
<rect>
<x>70</x>
<y>0</y>
<width>91</width>
<height>22</height>
</rect>
</property>
<item>
<property name="text">
<string>左右交替</string>
</property>
</item>
<item>
<property name="text">
<string>居右排列</string>
</property>
</item>
</widget>
<widget class="QLabel" name="label_axisArrangement">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>71</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string>Y轴排列</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="page_2"/>
</widget>