From: Michal MalĂ˝ Date: Tue, 7 Oct 2014 00:09:56 +0000 (+0200) Subject: - Simplify mouse input handling in GraphView X-Git-Tag: 0.3e~7 X-Git-Url: https://gitweb.devoid-pointer.net/?a=commitdiff_plain;h=6a389a576cf9b822968c84b8734c70968def00c8;p=anyanka.git - Simplify mouse input handling in GraphView - Remove unneeded variables from SignalDrawer::erasePeak() - Slightly improve debugging messages --- diff --git a/gui/graphview.cpp b/gui/graphview.cpp index 5d7a06d..da77da8 100644 --- a/gui/graphview.cpp +++ b/gui/graphview.cpp @@ -83,10 +83,10 @@ void GraphView::leaveEvent(QEvent *) case MouseMode::CROSSHAIR: this->eraseCrosshair(true); break; - case MouseMode::INTEGRATE: + case MouseMode::INTEGRATION_BASELINE: this->eraseIntegrationBaseline(true); break; - case MouseMode::ZOOM: + case MouseMode::ZOOM_RECTANGLE: this->eraseZoomRect(true); break; } @@ -103,10 +103,10 @@ void GraphView::mouseMoveEvent(QMouseEvent* ev) case GraphView::MouseMode::CROSSHAIR: drawCrosshair(xPix, yPix); break; - case GraphView::MouseMode::ZOOM: + case GraphView::MouseMode::ZOOM_RECTANGLE: drawZoomRect(xPix, yPix); break; - case GraphView::MouseMode::INTEGRATE: + case GraphView::MouseMode::INTEGRATION_BASELINE: drawIntegrationBaseline(xPix, yPix); break; } @@ -120,63 +120,39 @@ void GraphView::mousePressEvent(QMouseEvent* ev) switch (ev->button()) { case Qt::LeftButton: switch (m_mouseMode) { - case GraphView::MouseMode::CROSSHAIR: - switch (m_graphCtrlMode) { - case GraphControlModes::ZOOM: - m_zoomRectStartXPix = ev->x(); - m_zoomRectStartYPix = ev->y(); - m_zoomRectLastXPix = ev->x(); - m_zoomRectLastYPix = ev->y(); - eraseCrosshair(true); - m_mouseMode = GraphView::MouseMode::ZOOM; - break; - case GraphControlModes::INTEGRATE_BASELINE: - m_integrateStartBLYPix = relToYPix(m_controller->valueToRel(m_controller->getXYValues(xPixToRel(ev->x())).second)); - m_integrateStopBLYPix = m_integrateStartBLYPix; - case GraphControlModes::INTEGRATE_INTERSECT: - m_integrateStartXPix = ev->x(); - m_integrateStartYPix = ev->y(); - m_integrateStopXPix = ev->x(); - m_integrateStopYPix = ev->y(); - eraseCrosshair(true); - m_graphCrosshairXPix = -1; - m_mouseMode = GraphView::MouseMode::INTEGRATE; - } + case GraphView::MouseMode::CROSSHAIR: + switch (m_graphCtrlMode) { + case GraphControlModes::ZOOM: + startZoom(ev->pos()); break; - case GraphView::MouseMode::ZOOM: - zoom(m_zoomRectStartXPix, m_zoomRectStartYPix, m_zoomRectLastXPix, m_zoomRectLastYPix); - m_mouseMode = GraphView::MouseMode::CROSSHAIR; + case GraphControlModes::INTEGRATE: + startIntegration(ev->pos()); break; - case GraphView::MouseMode::INTEGRATE: - PeakDrawData pdData = m_controller->integratePeak(xPixToRel(m_integrateStartXPix), yPixToRel(m_integrateStartYPix), - xPixToRel(m_integrateStopXPix), yPixToRel(m_integrateStopYPix), m_integrationType); - eraseIntegrationBaseline(true); - QRegion reg = renderPeak(pdData, m_background); - copyPixmapRegion(reg, m_background, m_pixmap); - update(reg); - m_integrateStartXPix = -1; - m_mouseMode = GraphView::MouseMode::CROSSHAIR; - break; - } + } + break; + + case GraphView::MouseMode::ZOOM_RECTANGLE: + doZoom(); + break; + case GraphView::MouseMode::INTEGRATION_BASELINE: + doIntegration(); + break; + } break; case Qt::RightButton: switch (m_mouseMode) { - case GraphView::MouseMode::CROSSHAIR: - eraseCrosshair(true); - showContextMenu(ev->pos(), ev->globalPos()); - break; - case GraphView::MouseMode::ZOOM: - eraseZoomRect(true); - m_mouseMode = GraphView::MouseMode::CROSSHAIR; - break; - case GraphView::MouseMode::INTEGRATE: - eraseIntegrationBaseline(true); - m_integrateStartXPix = -1; - m_mouseMode = GraphView::MouseMode::CROSSHAIR; - break; - default: - break; + case GraphView::MouseMode::CROSSHAIR: + doShowContextMenu(ev->pos(), ev->globalPos()); + break; + case GraphView::MouseMode::ZOOM_RECTANGLE: + cancelZoom(); + break; + case GraphView::MouseMode::INTEGRATION_BASELINE: + cancelIntegration(); + break; + default: + break; } break; default: @@ -203,6 +179,57 @@ void GraphView::resizeEvent(QResizeEvent* ev) /** Private methods **/ +void GraphView::cancelIntegration() +{ + eraseIntegrationBaseline(true); + m_integrateStartXPix = -1; + m_mouseMode = GraphView::MouseMode::CROSSHAIR; +} + +void GraphView::cancelZoom() +{ + eraseZoomRect(true); + m_mouseMode = GraphView::MouseMode::CROSSHAIR; +} + +void GraphView::doIntegration() +{ + IntegrationType itype; + switch (m_integrationMode) { + case GraphView::IntegrationMode::BASELINE: + itype = IntegrationType::BASELINE; + break; + case GraphView::IntegrationMode::INTERSECTION: + itype = IntegrationType::INTERSECTION; + break; + default: + itype = IntegrationType::NONE; + break; + } + + PeakDrawData pdData = m_controller->integratePeak(xPixToRel(m_integrateStartXPix), yPixToRel(m_integrateStartYPix), + xPixToRel(m_integrateStopXPix), yPixToRel(m_integrateStopYPix), itype); + eraseIntegrationBaseline(true); + + QRegion reg = renderPeak(pdData, m_background); + copyPixmapRegion(reg, m_background, m_pixmap); + update(reg); + m_integrateStartXPix = -1; + m_mouseMode = GraphView::MouseMode::CROSSHAIR; +} + +void GraphView::doShowContextMenu(const QPoint& pos, const QPoint& globalPos) +{ + eraseCrosshair(true); + showContextMenu(pos, globalPos); +} + +void GraphView::doZoom() +{ + zoom(m_zoomRectStartXPix, m_zoomRectStartYPix, m_zoomRectLastXPix, m_zoomRectLastYPix); + m_mouseMode = GraphView::MouseMode::CROSSHAIR; +} + void GraphView::drawCrosshair(const int xPix, const int yPix) { QPainter p; @@ -241,7 +268,7 @@ void GraphView::drawIntegrationBaseline(const int x, const int y) reg = eraseIntegrationBaseline(); - if (m_integrationType == IntegrationType::BASELINE) { + if (m_integrationMode == GraphView::IntegrationMode::BASELINE) { m_integrateStopBLYPix = relToYPix(m_controller->valueToRel(m_controller->getXYValues(xPixToRel(x)).second)); double vstarty = std::min(m_integrateStartBLYPix, m_integrateStartYPix); double vstopy = std::min(m_integrateStopBLYPix, y); @@ -371,7 +398,7 @@ QRegion GraphView::eraseIntegrationBaseline(bool apply) if (m_integrateStartXPix < 0) return QRegion(); - if (m_integrationType == IntegrationType::BASELINE) { + if (m_integrationMode == GraphView::IntegrationMode::BASELINE) { int top, bottom, left, right; top = Helpers::minof(m_integrateStartYPix, m_integrateStopYPix, m_integrateStartBLYPix, m_integrateStopBLYPix); bottom = Helpers::maxof(m_integrateStartYPix, m_integrateStopYPix, m_integrateStartBLYPix, m_integrateStopBLYPix); @@ -470,6 +497,39 @@ void GraphView::showContextMenu(const QPoint& pos, const QPoint& globalPos) m_ctxMenu.exec(globalPos); } +void GraphView::startIntegration(const QPoint& pos) +{ + switch (m_integrationMode) { + case GraphView::IntegrationMode::BASELINE: + Logger::log(Logger::Level::DEBUG, ME_SENDER_STR, "Starting integration by baseline"); + m_integrateStartBLYPix = relToYPix(m_controller->valueToRel(m_controller->getXYValues(xPixToRel(pos.x())).second)); + m_integrateStopBLYPix = m_integrateStartBLYPix; + break; + case GraphView::IntegrationMode::INTERSECTION: + Logger::log(Logger::Level::DEBUG, ME_SENDER_STR, "Starting inregration by intersection"); + break; + } + m_integrateStartXPix = pos.x(); + m_integrateStartYPix = pos.y(); + m_integrateStopXPix = pos.x(); + m_integrateStopYPix = pos.y(); + + eraseCrosshair(true); + m_graphCrosshairXPix = -1; + m_mouseMode = GraphView::MouseMode::INTEGRATION_BASELINE; +} + +void GraphView::startZoom(const QPoint& pos) +{ + m_zoomRectStartXPix = pos.x(); + m_zoomRectStartYPix = pos.y(); + m_zoomRectLastXPix = pos.x(); + m_zoomRectLastYPix = pos.y(); + + eraseCrosshair(true); + m_mouseMode = GraphView::MouseMode::ZOOM_RECTANGLE; +} + void GraphView::updateValuesUnderCrosshair(const int xPix) { double x = xPixToRel(xPix); diff --git a/gui/graphview.h b/gui/graphview.h index eaa7009..58179c8 100644 --- a/gui/graphview.h +++ b/gui/graphview.h @@ -34,12 +34,6 @@ class GraphView : public QWidget, protected SignalDrawer { Q_OBJECT public: - enum class MouseMode { - CROSSHAIR, - ZOOM, - INTEGRATE - }; - explicit GraphView(std::shared_ptr controller, QWidget* parent = nullptr); void leaveEvent(QEvent* ); void mouseDoubleClickEvent(QMouseEvent* ); @@ -49,6 +43,25 @@ public: bool refresh(QRegion reg = QRegion()); private: + enum class GraphControlModes { + ZOOM, + INTEGRATE + }; + enum class MouseMode { + CROSSHAIR, + ZOOM_RECTANGLE, + INTEGRATION_BASELINE + }; + enum class IntegrationMode { + BASELINE, + INTERSECTION + }; + + void cancelIntegration(); + void cancelZoom(); + void doIntegration(); + void doShowContextMenu(const QPoint& pos, const QPoint& globalPos); + void doZoom(); void drawCrosshair(const int xPix, const int yPix); void drawIntegrationBaseline(const int x, const int y); void drawZoomRect(const int x, const int y); @@ -60,6 +73,8 @@ private: void updateValuesUnderCrosshair(const int xPix); void setDefaultZoom(); void showContextMenu(const QPoint& pos, const QPoint& globalPos); + void startIntegration(const QPoint& pos); + void startZoom(const QPoint& pos); void zoom(const int fromXPix, const int fromYPix, const int toXPix, const int toYPix); static inline double RELATIVE_Y_MAX_WITH_MARGIN() @@ -75,14 +90,14 @@ private: GraphViewContextMenu m_ctxMenu; - MouseMode m_mouseMode; GraphControlModes m_graphCtrlMode; + MouseMode m_mouseMode; + IntegrationMode m_integrationMode; /* Crosshair */ int m_graphCrosshairXPix; int m_graphCrosshairYPix; /* Integration baseline */ - IntegrationType m_integrationType; int m_integrateStartBLYPix; int m_integrateStopBLYPix; int m_integrateStartXPix; @@ -103,10 +118,16 @@ private slots: void onCtxMenuZoomOut(); public slots: - void onControlModeChanged(GraphControlModes mode) - { m_graphCtrlMode = mode; - if (mode == GraphControlModes::INTEGRATE_BASELINE) m_integrationType = IntegrationType::BASELINE; - else if (mode == GraphControlModes::INTEGRATE_INTERSECT) m_integrationType = IntegrationType::INTERSECTION; + void onSetZoomMode() { + m_graphCtrlMode = GraphView::GraphControlModes::ZOOM; + } + void onSetIntegrateBaseline() { + m_graphCtrlMode = GraphView::GraphControlModes::INTEGRATE; + m_integrationMode = GraphView::IntegrationMode::BASELINE; + } + void onSetIntegrateIntersection() { + m_graphCtrlMode = GraphView::GraphControlModes::INTEGRATE; + m_integrationMode = GraphView::IntegrationMode::INTERSECTION; } signals: diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index f220bed..f41973c 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -65,7 +65,7 @@ void MainWindow::connectActions() { /* Controls panel */ connect(ui->qpb_integrateBaseline, SIGNAL(pressed()), this, SLOT(onIntegrateBaselineSelected())); - connect(ui->qpb_integrateIntersect, SIGNAL(pressed()), this, SLOT(onIntegrateIntersectSelected())); + connect(ui->qpb_integrateIntersect, SIGNAL(pressed()), this, SLOT(onIntegrateIntersectionSelected())); connect(ui->qpb_zoom, SIGNAL(pressed()), this, SLOT(onZoomSelected())); /* DATA menu */ @@ -87,13 +87,15 @@ void MainWindow::connectActions() void MainWindow::onAddToDashboard(SignalView* sw) { if (ui->qpb_integrateBaseline->isChecked()) - sw->m_graphView->onControlModeChanged(GraphControlModes::INTEGRATE_BASELINE); + sw->m_graphView->onSetIntegrateBaseline(); else if(ui->qpb_integrateIntersect->isChecked()) - sw->m_graphView->onControlModeChanged(GraphControlModes::INTEGRATE_INTERSECT); + sw->m_graphView->onSetIntegrateIntersection(); else if(ui->qpb_zoom->isChecked()) - sw->m_graphView->onControlModeChanged(GraphControlModes::ZOOM); + sw->m_graphView->onSetZoomMode(); - connect(this, SIGNAL(controlModeChanged(GraphControlModes)), sw->m_graphView, SLOT(onControlModeChanged(GraphControlModes))); + connect(this, SIGNAL(integrateBaselineMode()), sw->m_graphView, SLOT(onSetIntegrateBaseline())); + connect(this, SIGNAL(integrateIntersectionMode()), sw->m_graphView, SLOT(onSetIntegrateIntersection())); + connect(this, SIGNAL(zoomMode()), sw->m_graphView, SLOT(onSetZoomMode())); connect(sw, SIGNAL(fullSizeToggle()), this, SLOT(onSWFullSizeToggle())); m_dashboard->addWidget(sw); @@ -149,12 +151,12 @@ void MainWindow::onAboutAnyanka() void MainWindow::onIntegrateBaselineSelected() { - emit controlModeChanged(GraphControlModes::INTEGRATE_BASELINE); + emit integrateBaselineMode(); } -void MainWindow::onIntegrateIntersectSelected() +void MainWindow::onIntegrateIntersectionSelected() { - emit controlModeChanged(GraphControlModes::INTEGRATE_INTERSECT); + emit integrateIntersectionMode(); } void MainWindow::onLoadSequence() @@ -201,7 +203,7 @@ void MainWindow::onSWFullSizeToggle() void MainWindow::onZoomSelected() { - emit controlModeChanged(GraphControlModes::ZOOM); + emit zoomMode(); } MainWindow::~MainWindow() diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 88cc398..b3f5da5 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -74,7 +74,7 @@ private slots: void onExportPeaks() { emit exportPeaks(); } void onExportRawData() { emit exportRawData(); } void onIntegrateBaselineSelected(); - void onIntegrateIntersectSelected(); + void onIntegrateIntersectionSelected(); void onLoadSequence(); void onLoadSingleRun(); void onSaveChanges() { emit saveChanges(); } @@ -84,15 +84,17 @@ private slots: void onZoomSelected(); signals: - void controlModeChanged(GraphControlModes mode); void exportGraphToImage(); void exportPeaks(); void exportRawData(); + void integrateBaselineMode(); + void integrateIntersectionMode(); void loadSequence(const QString& dir); void loadSingleRun(const QString& dir); void saveChanges(); void sequenceSelected(const QString& str); void singleRunSelected(const QString& str); + void zoomMode(); }; #endif // MAINWINDOW_H diff --git a/metatypes.h b/metatypes.h index 62eccee..67fd43e 100644 --- a/metatypes.h +++ b/metatypes.h @@ -30,11 +30,4 @@ class SignalView; typedef std::shared_ptr SignalViewPtr; Q_DECLARE_METATYPE(SignalViewPtr); -enum class GraphControlModes { - ZOOM, - INTEGRATE_INTERSECT, - INTEGRATE_BASELINE -}; -Q_DECLARE_METATYPE(GraphControlModes); - #endif // METATYPES_H diff --git a/signalcontroller.cpp b/signalcontroller.cpp index f93c0e2..87913bc 100644 --- a/signalcontroller.cpp +++ b/signalcontroller.cpp @@ -139,11 +139,11 @@ std::vector SignalController::getPeaksDrawData(const double fromX, std::vector> allPeaks; if (toX <= fromX) { - Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " toX is lower than fromX"); + Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " toX (" + QString::number(toX) + " is not greater than fromX (" + QString::number(fromX) + ")"); return peaks; } if (toY <= fromY) { - Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " toY is lower than fromY"); + Logger::log(Logger::Level::WARNING, ME_SENDER_STR, __QFUNC__ + " toY (" + QString::number(toY) + ") is not greater than fromY (" + QString::number(fromY) + ")"); return peaks; } @@ -280,8 +280,8 @@ PeakDrawData SignalController::genPeakDrawData(const std::shared_ptrvalueAt(peak->fromIdx() - 1); double beforeToY = m_signal->valueAt(peak->toIdx() - 1); - double fromY = m_signal->valueAt(peak->fromIdx()); - double toY = m_signal->valueAt(peak->toIdx()); + double fromY; + double toY; switch (peak->itype()) { case IntegrationType::INTERSECTION: diff --git a/signaldrawer.cpp b/signaldrawer.cpp index 12be107..91f334e 100644 --- a/signaldrawer.cpp +++ b/signaldrawer.cpp @@ -314,10 +314,10 @@ QRegion SignalDrawer::erasePeak(const PeakDrawData& pd) QFontMetrics fm(font); QString aucText, timeText; QRegion peakReg; - int tTextHeight, tTextWidth, aTextWidth; + int tTextHeight, aTextWidth; int fromXPix; int toXPix; - int peakX, peakY; + int peakX; int beginXPix, endXPix; if (m_background == nullptr) { @@ -330,12 +330,9 @@ QRegion SignalDrawer::erasePeak(const PeakDrawData& pd) fromXPix = relToXPix(pd.fromX); toXPix = relToXPix(pd.toX); - peakX = relToXPix(pd.peakX); peakY = relToYPix(pd.peakY); + peakX = relToXPix(pd.peakX); aucText = m_locale.toString(pd.auc, 'f', 4); timeText = m_locale.toString(pd.time, 'f', 4); - tTextWidth = fm.width(timeText); - if (peakY - tTextWidth < 2) - peakY += tTextWidth - peakY + 2; tTextHeight = fm.height(); aTextWidth = fm.width(aucText);