]> Devoid-pointer.net GitWeb - anyanka.git/commitdiff
- Simplify mouse input handling in GraphView
authorMichal Malý <madcatxster@devoid-pointer.net>
Tue, 7 Oct 2014 00:09:56 +0000 (02:09 +0200)
committerMichal Malý <madcatxster@devoid-pointer.net>
Tue, 7 Oct 2014 00:09:56 +0000 (02:09 +0200)
- Remove unneeded variables from SignalDrawer::erasePeak()
- Slightly improve debugging messages

gui/graphview.cpp
gui/graphview.h
gui/mainwindow.cpp
gui/mainwindow.h
metatypes.h
signalcontroller.cpp
signaldrawer.cpp

index 5d7a06d858c8de8bd95ef9369edd4a6642b8a102..da77da8993736834deaaf8e2ff3b56d49b74c6f5 100644 (file)
@@ -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);
index eaa7009ab94f72141990d2560ed4ac9cb50a936b..58179c88c21d6b4718332804902527da7cd49f6d 100644 (file)
@@ -34,12 +34,6 @@ class GraphView : public QWidget, protected SignalDrawer
 {
   Q_OBJECT
 public:
-  enum class MouseMode {
-    CROSSHAIR,
-    ZOOM,
-    INTEGRATE
-  };
-
   explicit GraphView(std::shared_ptr<SignalController> 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:
index f220bed75fb291825cd31c32dd88ab6605020713..f41973cfcb678e4747c58f9551c96f2f03ad56cc 100644 (file)
@@ -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()
index 88cc39860c8c82b2b783fe40528804045af0c5f5..b3f5da554bcaa294df7e37124b0778b2f0ec73b3 100644 (file)
@@ -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
index 62eccee0ae93dc088f3ffc5b566d44c3c6b71972..67fd43e9b416474bb4f8e0d7b0b5419bf2ff9fee 100644 (file)
@@ -30,11 +30,4 @@ class SignalView;
 typedef std::shared_ptr<SignalView> SignalViewPtr;
 Q_DECLARE_METATYPE(SignalViewPtr);
 
-enum class GraphControlModes {
-  ZOOM,
-  INTEGRATE_INTERSECT,
-  INTEGRATE_BASELINE
-};
-Q_DECLARE_METATYPE(GraphControlModes);
-
 #endif // METATYPES_H
index f93c0e23f6863511d8b805537264cb1427ef0d03..87913bc242630a787cc2c0ced957fe28ac7a01cc 100644 (file)
@@ -139,11 +139,11 @@ std::vector<PeakDrawData> SignalController::getPeaksDrawData(const double fromX,
   std::vector<std::shared_ptr<const IntegratedPeak>> 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_ptr<const Integ
 {
   double beforeFromY = m_signal->valueAt(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:
index 12be1072d10499e1c0683929f4c15e12859355ea..91f334e6f0ec92061ee2f54bb8ff89217f19b4fa 100644 (file)
@@ -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);