From 7473a45f41db408c4a668e4853a9ca3052c0738d Mon Sep 17 00:00:00 2001 From: tangchao0503 <735056338@qq.com> Date: Mon, 16 Mar 2026 14:07:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=82=B9=E5=87=BB=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=85=89=E8=B0=B1=E5=8A=9F=E8=83=BD=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=9A=E6=89=93=E5=BC=80=E4=B8=80=E5=89=AF=E5=BD=B1?= =?UTF-8?q?=E5=83=8F=E5=90=8E=EF=BC=8C=E7=82=B9=E5=87=BB=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E5=85=89=E8=B0=B1=E5=87=BA=E7=8E=B0=E5=8D=81=E5=AD=97=E6=9E=B6?= =?UTF-8?q?=EF=BC=8C=E7=84=B6=E5=90=8E=E7=A7=BB=E9=99=A4=E5=9B=BE=E5=B1=82?= =?UTF-8?q?=E5=90=8E=EF=BC=8C=E5=86=8D=E6=89=93=E5=BC=80=E5=9B=BE=E5=B1=82?= =?UTF-8?q?=E5=B0=B1=E4=BC=9A=E5=87=BA=E7=8E=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HPPA/HPPA.cpp | 36 +++++++++++--------------- HPPA/HPPA.h | 1 + HPPA/ImageViewer.cpp | 44 ++++++++++++++++++++++++++++++++ HPPA/ImageViewer.h | 7 +++++ HPPA/MapTool.cpp | 2 +- HPPA/MapToolSpectral.cpp | 55 ++-------------------------------------- HPPA/MapToolSpectral.h | 7 ----- 7 files changed, 70 insertions(+), 82 deletions(-) diff --git a/HPPA/HPPA.cpp b/HPPA/HPPA.cpp index a98216e..4e0cd37 100644 --- a/HPPA/HPPA.cpp +++ b/HPPA/HPPA.cpp @@ -1570,16 +1570,7 @@ void HPPA::onOpenImg() QFileInfo fi(uri); QString baseName = fi.completeBaseName(); - RasterLayer* rl = new RasterLayer(baseName, uri); - - auto* layerNode = new LayerTreeLayer(rl); - m_LayerTreeModel->addLayer(m_RasterGroup, layerNode); - - QWidget* mapcavasContainer = onCreateTab(baseName); - if (m_MapLayerStore) m_MapLayerStore->addLayer(rl, mapcavasContainer); - QList mapcavas = mapcavasContainer->findChildren(); - mapcavas[0]->setLayers(rl); - mapcavas[0]->freshmap(); + addLayer(baseName, uri, true); } void HPPA::onconnect() @@ -2025,23 +2016,26 @@ void WorkerThread3::run() void HPPA::onLayerCreatedFromFile(const QString& baseName, const QString& filePath, int fileIndex) { if (!m_LayerTreeModel || !m_RasterGroup) return; + addLayer(baseName, filePath, false); +} + +void HPPA::addLayer(const QString& baseName, const QString& filePath,bool refresh) +{ + // Create MapLayer first and attach it to a LayerTreeLayerNode + RasterLayer* ml = new RasterLayer(baseName, filePath); + + auto* layerNode = new LayerTreeLayer(ml); + LayerTreeNode* node = m_LayerTreeModel->addLayer(m_RasterGroup, layerNode); QWidget* mapcavasContainer = onCreateTab(baseName); - - // Create MapLayer first and attach it to a LayerTreeLayerNode - RasterLayer* ml = new RasterLayer(baseName, filePath); if (m_MapLayerStore) m_MapLayerStore->addLayer(ml, mapcavasContainer); QList mapcavas = mapcavasContainer->findChildren(); mapcavas[0]->setLayers(ml); - auto* layerNode = new LayerTreeLayer(ml); - LayerTreeNode* node = m_LayerTreeModel->addLayer(m_RasterGroup, layerNode); - LayerTreeLayer* lnode = dynamic_cast(node); - if (!lnode) return; - - // layerNode already holds the MapLayer pointer from constructor - - qDebug() << "LayerFileCreated -> created layer:" << baseName << "path:" << filePath << "index:" << fileIndex; + if (refresh) + { + mapcavas[0]->freshmap(); + } } void HPPA::onLayerTreeSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected) diff --git a/HPPA/HPPA.h b/HPPA/HPPA.h index b1a7c26..6bf33b4 100644 --- a/HPPA/HPPA.h +++ b/HPPA/HPPA.h @@ -341,6 +341,7 @@ public Q_SLOTS: void onCreated3DModelPlantPhenotype(); void onCreated3DModelOneMotor(); + void addLayer(const QString& baseName, const QString& filePath, bool refresh); void onLayerCreatedFromFile(const QString& baseName, const QString& filePath, int fileIndex); void removeLayerByTreeIndex(); void removeAllLayersInRasterGroup(); diff --git a/HPPA/ImageViewer.cpp b/HPPA/ImageViewer.cpp index 8203247..c03fdd7 100644 --- a/HPPA/ImageViewer.cpp +++ b/HPPA/ImageViewer.cpp @@ -103,6 +103,50 @@ bool Mapcavas::HasImage() } } +void Mapcavas::updateCrosshair(double sceneX, double sceneY) +{ + QPen pen(Qt::red, 2.0); + pen.setCosmetic(true); // constant screen-width regardless of zoom + + if (!m_hLine) + { + m_hLine = m_qtGraphicsScene->addLine(0, 0, 0, 0, pen); + m_hLine->setZValue(1e9); + } + if (!m_vLine) + { + m_vLine = m_qtGraphicsScene->addLine(0, 0, 0, 0, pen); + m_vLine->setZValue(1e9); + } + + m_hLine->setPen(pen); + m_vLine->setPen(pen); + + m_hLine->setLine(sceneX - m_CrosshairHalfLen, sceneY, + sceneX + m_CrosshairHalfLen, sceneY); + m_vLine->setLine(sceneX, sceneY - m_CrosshairHalfLen, + sceneX, sceneY + m_CrosshairHalfLen); +} + +void Mapcavas::removeCrosshair() +{ + if (m_hLine) + { + if (m_hLine->scene()) + m_hLine->scene()->removeItem(m_hLine); + delete m_hLine; + m_hLine = nullptr; + } + if (m_vLine) + { + if (m_vLine->scene()) + m_vLine->scene()->removeItem(m_vLine); + delete m_vLine; + m_vLine = nullptr; + } +} + + void Mapcavas::wheelEvent(QWheelEvent *event) { // Always let the tool have a chance first diff --git a/HPPA/ImageViewer.h b/HPPA/ImageViewer.h index d312951..93d9e3c 100644 --- a/HPPA/ImageViewer.h +++ b/HPPA/ImageViewer.h @@ -29,6 +29,9 @@ public: bool HasImage(); void ensureSceneVisible(); + void updateCrosshair(double sceneX, double sceneY); + void removeCrosshair(); + void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE; void scaling(qreal scaleFactor); @@ -71,6 +74,10 @@ private: QPoint m_lastMousePos; // 鼠标最后按下的位置 qreal m_scale; // 缩放值 + double m_CrosshairHalfLen = 10.0; + QGraphicsLineItem* m_hLine = nullptr; // horizontal line + QGraphicsLineItem* m_vLine = nullptr; // vertical line + signals: void leftMouseButtonPressed(int, int, QVector, QVector); diff --git a/HPPA/MapTool.cpp b/HPPA/MapTool.cpp index 9953306..17216b5 100644 --- a/HPPA/MapTool.cpp +++ b/HPPA/MapTool.cpp @@ -34,7 +34,7 @@ void MapTool::setMapcavas(Mapcavas* canvas) if (m_isActive && m_canvas) { - deactivate(); + //deactivate(); } m_canvas = canvas; diff --git a/HPPA/MapToolSpectral.cpp b/HPPA/MapToolSpectral.cpp index 6d9978b..261de18 100644 --- a/HPPA/MapToolSpectral.cpp +++ b/HPPA/MapToolSpectral.cpp @@ -8,8 +8,6 @@ #include #include -const double MapToolSpectral::CrosshairHalfLen = 10.0; - MapToolSpectral::MapToolSpectral(QObject* parent) : MapTool(parent) { @@ -18,7 +16,6 @@ MapToolSpectral::MapToolSpectral(QObject* parent) MapToolSpectral::~MapToolSpectral() { - //removeCrosshair();//涓嶉渶瑕佸湪鏋愭瀯鍑芥暟涓皟鐢╮emoveCrosshair()锛屽洜涓哄綋MapToolSpectral琚攢姣佹椂锛屽畠鐨刢anvas()涔熶細琚攢姣侊紝crosshair鐨剆cene涔熶細琚攢姣侊紝鎵浠rosshair浼氳嚜鍔ㄨ鍒犻櫎锛屼笉浼氶犳垚鍐呭瓨娉勬紡銆 } void MapToolSpectral::activate() @@ -28,7 +25,7 @@ void MapToolSpectral::activate() void MapToolSpectral::deactivate() { - removeCrosshair(); + canvas()->removeCrosshair(); MapTool::deactivate(); } @@ -48,7 +45,7 @@ void MapToolSpectral::canvasMousePressEvent(QMouseEvent* e) if (rl && rl->isValidPixel(x, y)) { // Place crosshair at pixel center - updateCrosshair(x + 0.5, y + 0.5); + canvas()->updateCrosshair(x + 0.5, y + 0.5); QVector wavelengths; QVector spectrum; @@ -58,51 +55,3 @@ void MapToolSpectral::canvasMousePressEvent(QMouseEvent* e) } } } - -void MapToolSpectral::updateCrosshair(double sceneX, double sceneY) -{ - if (!canvas() || !canvas()->scene()) - return; - - QGraphicsScene* scene = canvas()->scene(); - - QPen pen(Qt::red, 2.0); - pen.setCosmetic(true); // constant screen-width regardless of zoom - - if (!m_hLine) - { - m_hLine = scene->addLine(0, 0, 0, 0, pen); - m_hLine->setZValue(1e9); - } - if (!m_vLine) - { - m_vLine = scene->addLine(0, 0, 0, 0, pen); - m_vLine->setZValue(1e9); - } - - m_hLine->setPen(pen); - m_vLine->setPen(pen); - - m_hLine->setLine(sceneX - CrosshairHalfLen, sceneY, - sceneX + CrosshairHalfLen, sceneY); - m_vLine->setLine(sceneX, sceneY - CrosshairHalfLen, - sceneX, sceneY + CrosshairHalfLen); -} - -void MapToolSpectral::removeCrosshair() -{ - if (m_hLine) - { - if (m_hLine->scene()) - m_hLine->scene()->removeItem(m_hLine); - delete m_hLine; - m_hLine = nullptr; - } - if (m_vLine) - { - if (m_vLine->scene()) - m_vLine->scene()->removeItem(m_vLine); - delete m_vLine; - m_vLine = nullptr; - } -} diff --git a/HPPA/MapToolSpectral.h b/HPPA/MapToolSpectral.h index ebb62f1..22012dc 100644 --- a/HPPA/MapToolSpectral.h +++ b/HPPA/MapToolSpectral.h @@ -23,13 +23,6 @@ signals: void spectralClicked(int x, int y, QVector wavelengths, QVector spectrum); private: - void updateCrosshair(double sceneX, double sceneY); - void removeCrosshair(); - - QGraphicsLineItem* m_hLine = nullptr; // horizontal line - QGraphicsLineItem* m_vLine = nullptr; // vertical line - - static const double CrosshairHalfLen; }; #endif // MAPTOOLSPECTRAL_H