libpappsomspp
Library for mass spectrometry
baseplotwidget.h
Go to the documentation of this file.
1 /* This code comes right from the msXpertSuite software project.
2  *
3  * msXpertSuite - mass spectrometry software suite
4  * -----------------------------------------------
5  * Copyright(C) 2009,...,2018 Filippo Rusconi
6  *
7  * http://www.msxpertsuite.org
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * END software license
23  */
24 
25 
26 #pragma once
27 
28 /////////////////////// StdLib includes
29 #include <memory>
30 
31 
32 /////////////////////// Qt includes
33 #include <QObject>
34 #include <QString>
35 #include <QWidget>
36 #include <QBrush>
37 #include <QColor>
38 #include <QVector>
39 
40 
41 /////////////////////// QCustomPlot
42 #include <qcustomplot.h>
43 
44 
45 /////////////////////// Local includes
46 #include "../../exportinmportconfig.h"
47 #include "../../types.h"
48 
49 
50 namespace pappso
51 {
52 
53 enum class PlotAxis
54 {
55  none = 0x000,
56  x_axis = 1 << 1,
57  y_axis = 1 << 2,
58  both = (x_axis | y_axis)
59 };
60 
61 
62 enum class RangeType
63 {
64  outermost = 1,
65  innermost = 2,
66 };
67 
68 
70 {
71  DataKind dataKind = DataKind::unset;
72 
73  bool isMouseDragging = false;
74  bool wasMouseDragging = false;
75 
76  bool isKeyBoardDragging = false;
77  bool isLeftPseudoButtonKeyPressed = false;
78  bool isRightPseudoButtonKeyPressed = false;
79  bool wassKeyBoardDragging = false;
80 
81  QPointF startDragPoint;
84 
85  // The effective range of the axes.
86  QCPRange xRange;
87  QCPRange yRange;
88 
89  // Tell if the mouse move was started onto either axis, because that will
90  // condition if some calculations needs to be performed or not (for example,
91  // if the mouse cursor motion was started on an axis, there is no point to
92  // perform deconvolutions).
93  bool wasClickOnXAxis = false;
94  bool wasClickOnYAxis = false;
95 
96  bool isMeasuringDistance = false;
97 
98  // The user-selected region over the plot.
99  // Note that we cannot use QCPRange structures because these are normalized by
100  // QCustomPlot in such a manner that lower is actually < upper. But we need
101  // for a number of our calculations (specifically for the deconvolutions) to
102  // actually have the lower value be start drag point.x even if the drag
103  // direction was from right to left.
104  double xRegionRangeStart = std::numeric_limits<double>::min();
105  double xRegionRangeEnd = std::numeric_limits<double>::min();
106 
107  double yRegionRangeStart = std::numeric_limits<double>::min();
108  double yRegionRangeEnd = std::numeric_limits<double>::min();
109 
110  double xDelta = 0;
111  double yDelta = 0;
112 
115 
116  Qt::KeyboardModifiers keyboardModifiers;
117 
118  Qt::MouseButtons lastPressedMouseButton;
119  Qt::MouseButtons lastReleasedMouseButton;
120 
121  Qt::MouseButtons pressedMouseButtons;
122 
123  Qt::MouseButtons mouseButtonsAtMousePress;
124  Qt::MouseButtons mouseButtonsAtMouseRelease;
125 
126  QString toString() const;
127 };
128 
129 
130 class BasePlotWidget;
131 
132 typedef std::shared_ptr<BasePlotWidget> BasePlotWidgetSPtr;
133 typedef std::shared_ptr<const BasePlotWidget> BasePlotWidgetCstSPtr;
134 
135 class PMSPP_LIB_DECL BasePlotWidget : public QCustomPlot
136 {
137  Q_OBJECT
138 
139  public:
140  explicit BasePlotWidget(QWidget *parent);
141  explicit BasePlotWidget(QWidget *parent,
142  const QString &x_axis_label,
143  const QString &y_axis_label);
144 
145  virtual ~BasePlotWidget();
146 
147  virtual bool setupWidget();
148 
149  virtual void setPen(const QPen &pen);
150  virtual const QPen &getPen() const;
151 
152  virtual void setPlottingColor(QCPAbstractPlottable *plottable_p,
153  const QColor &new_color);
154  virtual void setPlottingColor(int index, const QColor &new_color);
155 
156  virtual QColor getPlottingColor(QCPAbstractPlottable *plottable_p) const;
157  virtual QColor getPlottingColor(int index = 0) const;
158 
159  virtual void setAxisLabelX(const QString &label);
160  virtual void setAxisLabelY(const QString &label);
161 
162  // AXES RANGE HISTORY-related functions
163  virtual void resetAxesRangeHistory();
164  virtual void updateAxesRangeHistory();
165  virtual void restorePreviousAxesRangeHistory();
166  virtual void restoreAxesRangeHistory(std::size_t index);
167  // AXES RANGE HISTORY-related functions
168 
169 
170  /// KEYBOARD-related EVENTS
171  virtual void keyPressEvent(QKeyEvent *event);
172  virtual void keyReleaseEvent(QKeyEvent *event);
173 
174  virtual void spaceKeyReleaseEvent(QKeyEvent *event);
175 
176  virtual void directionKeyPressEvent(QKeyEvent *event);
177  virtual void directionKeyReleaseEvent(QKeyEvent *event);
178 
179  virtual void mousePseudoButtonKeyPressEvent(QKeyEvent *event);
180  virtual void mousePseudoButtonKeyReleaseEvent(QKeyEvent *event);
181  /// KEYBOARD-related EVENTS
182 
183 
184  /// MOUSE-related EVENTS
185  virtual void mousePressHandler(QMouseEvent *event);
186  virtual void mouseReleaseHandler(QMouseEvent *event);
187  virtual void mouseReleaseHandlerLeftButton();
188  virtual void mouseReleaseHandlerRightButton();
189 
190  virtual void mouseMoveHandler(QMouseEvent *event);
191  virtual void mouseMoveHandlerNotDraggingCursor();
192  virtual void mouseMoveHandlerDraggingCursor();
193  virtual void mouseMoveHandlerLeftButtonDraggingCursor();
194  virtual void mouseMoveHandlerRightButtonDraggingCursor();
195 
196  virtual void axisDoubleClickHandler(QCPAxis *axis,
197  QCPAxis::SelectablePart part,
198  QMouseEvent *event);
199  bool isClickOntoXAxis(const QPointF &mousePoint);
200  bool isClickOntoYAxis(const QPointF &mousePoint);
201  /// MOUSE-related EVENTS
202 
203 
204  /// MOUSE MOVEMENTS mouse/keyboard-triggered
205  int dragDirection();
206  virtual void moveMouseCursorGraphCoordToGlobal(QPointF plot_coordinates);
207  virtual void moveMouseCursorPixelCoordToGlobal(QPointF local_coordinates);
208  virtual void horizontalMoveMouseCursorCountPixels(int pixel_count);
209  virtual QPointF horizontalGetGraphCoordNewPointCountPixels(int pixel_count);
210  virtual void verticalMoveMouseCursorCountPixels(int pixel_count);
211  virtual QPointF verticalGetGraphCoordNewPointCountPixels(int pixel_count);
212  /// MOUSE MOVEMENTS mouse/keyboard-triggered
213 
214 
215  /// RANGE-related functions
216  virtual QCPRange getRangeX(bool &found_range, int index) const;
217  virtual QCPRange getRangeY(bool &found_range, int index) const;
218  QCPRange
219  getRange(PlotAxis axis, RangeType range_type, bool &found_range) const;
220 
221  virtual QCPRange getInnermostRangeX(bool &found_range) const;
222  virtual QCPRange getOutermostRangeX(bool &found_range) const;
223 
224  virtual QCPRange getInnermostRangeY(bool &found_range) const;
225  virtual QCPRange getOutermostRangeY(bool &found_range) const;
226 
227  void yMinMaxOnXAxisCurrentRange(double &min,
228  double &max,
229  QCPAbstractPlottable *plottable_p = nullptr);
230  void yMinMaxOnXAxisCurrentRange(double &min, double &max, int index);
231  /// RANGE-related functions
232 
233 
234  /// PLOTTING / REPLOTTING functions
235  virtual void axisRescale();
236  virtual void axisReframe();
237  virtual void axisZoom();
238  virtual void axisPan();
239 
240  virtual void replotWithAxesRanges(QCPRange xAxisRange,
241  QCPRange yAxisRange,
242  PlotAxis whichAxis);
243  virtual void replotWithAxisRangeX(double lower, double upper);
244  virtual void replotWithAxisRangeY(double lower, double upper);
245  /// PLOTTING / REPLOTTING functions
246 
247 
248  /// PLOT ITEMS : TRACER TEXT ITEMS...
249  virtual void hideAllPlotItems();
250 
251  virtual void showTracers();
252  virtual void hideTracers();
253 
254  virtual void drawRectangleAndPrepareZoom();
255 
256  virtual void prepareXDeltaLineAndMeasure();
257  virtual void drawXDeltaLineAndMeasure();
258  virtual void drawXDeltaLineForIntegration();
259 
260  virtual void calculateDragDeltas();
261  virtual void calculateDragDeltasAndUnSortedRegionCorners();
262 
263  virtual bool isProperSelectionRectangle();
264  /// PLOT ITEMS : TRACER TEXT ITEMS...
265 
266 
267  virtual void setFocus();
268 
269  virtual void redrawPlotBackground(QWidget *focusedPlotWidget);
270 
271  virtual void updateContextRanges();
272 
273  virtual const BasePlotContext &getContext() const;
274 
275  signals:
276 
278 
279  void lastCursorHoveredPointSignal(const QPointF &pointf);
280 
282 
283  void xAxisMeasurementSignal(const BasePlotContext &context, bool with_delta);
284 
285  void keyPressEventSignal(const BasePlotContext &context);
287 
289 
290  void plottableSelectionChangedSignal(QCPAbstractPlottable *plottable_p,
291  bool selected);
292 
294 
296  QCPAbstractPlottable *plottable_p,
297  const BasePlotContext &context);
298 
299  protected:
300  //! Name of the plot widget.
301  QString m_name = "NOT_SET";
302 
303  //! Description of the plot widget.
304  QString m_desc = "NOT_SET";
305 
306  //! The name of the data file from which the mass data were read.
307  QString m_fileName;
308 
309  QString m_axisLabelX;
310  QString m_axisLabelY;
311 
313 
314  int m_leftMousePseudoButtonKey = Qt::Key_Less;
315  int m_rightMousePseudoButtonKey = Qt::Key_Greater;
316 
317  //! Rectangle defining the borders of zoomed-in/out data.
318  QCPItemRect *mp_zoomRectItem = nullptr;
319 
320  //! Line that is printed when the user selects a range.
321  QCPItemLine *mp_selectLineItem = nullptr;
322 
323  //! Text describing the x-axis delta value during a drag operation.
324  QCPItemText *mp_xDeltaTextItem = nullptr;
325 
326  //! Tells if the tracers should be visible.
327  bool m_shouldTracersBeVisible = true;
328 
329  //! Horizontal position tracer
330  QCPItemLine *mp_hPosTracerItem;
331 
332  //! Vertical position tracer
333  QCPItemLine *mp_vPosTracerItem;
334 
335  //! Vertical selection start tracer (typically in green).
336  QCPItemLine *mp_vStartTracerItem;
337 
338  //! Vertical selection end tracer (typically in red).
339  QCPItemLine *mp_vEndTracerItem /*only vertical*/;
340 
341  //! Index of the last axis range history item.
342  /*!
343 
344  Each time the user modifies the ranges (x/y axis) during panning or
345  zooming of the graph, the new axis ranges are stored in a axis ranges
346  history list. This index allows to point to the last range of that
347  history.
348 
349 */
350  std::size_t m_lastAxisRangeHistoryIndex = 0;
351 
352  //! List of x axis ranges occurring during the panning zooming actions.
353  std::vector<QCPRange *> m_xAxisRangeHistory;
354 
355  //! List of y axis ranges occurring during the panning zooming actions.
356  std::vector<QCPRange *> m_yAxisRangeHistory;
357 
358  //! How many mouse move events must be skipped */
359  /*!
360 
361  when the data are so massive that the graph panning becomes sluggish. By
362  default, the value is 10 events to be skipped before accounting one. The
363  "fat data" mouse movement handler mechanism is actuated by using a
364  keyboard key combination. There is no automatic shift between normal
365  processing and "fat data" processing.
366 
367 */
368  int m_mouseMoveHandlerSkipAmount = 10;
369 
370  //! Counter to handle the "fat data" mouse move event handling.
371  /*!
372 
373  \sa m_mouseMoveHandlerSkipAmount.
374 
375 */
376  int m_mouseMoveHandlerSkipCount = 0;
377 
378  // QColor m_unfocusedColor = QColor(Qt::lightGray);
379  // QColor m_unfocusedColor = QColor(230, 230, 230, 255);
380 
381  //! Color used for the background of unfocused plot.
382  QColor m_unfocusedColor = QColor("lightgray");
383  //! Color used for the background of unfocused plot.
384  QBrush m_unfocusedBrush = QBrush(m_unfocusedColor);
385 
386  //! Color used for the background of focused plot.
387  QColor m_focusedColor = QColor(Qt::transparent);
388  //! Color used for the background of focused plot.
389  QBrush m_focusedBrush = QBrush(m_focusedColor);
390 
391  //! Pen used to draw the graph and textual elements in the plot widget.
392  QPen m_pen;
393 };
394 
395 
396 } // namespace pappso
397 
398 
400 extern int basePlotContextMetaTypeId;
401 
pappso::BasePlotWidget::m_xAxisRangeHistory
std::vector< QCPRange * > m_xAxisRangeHistory
List of x axis ranges occurring during the panning zooming actions.
Definition: baseplotwidget.h:353
pappso::PlotAxis
PlotAxis
Definition: baseplotwidget.h:54
pappso::BasePlotContext::lastReleasedMouseButton
Qt::MouseButtons lastReleasedMouseButton
Definition: baseplotwidget.h:119
pappso::BasePlotWidget::lastCursorHoveredPointSignal
void lastCursorHoveredPointSignal(const QPointF &pointf)
pappso::BasePlotWidget::mp_hPosTracerItem
QCPItemLine * mp_hPosTracerItem
Horizontal position tracer.
Definition: baseplotwidget.h:330
pappso::BasePlotContext::pressedMouseButtons
Qt::MouseButtons pressedMouseButtons
Definition: baseplotwidget.h:121
pappso::BasePlotWidget::m_pen
QPen m_pen
Pen used to draw the graph and textual elements in the plot widget.
Definition: baseplotwidget.h:392
pappso::BasePlotWidget::mp_vStartTracerItem
QCPItemLine * mp_vStartTracerItem
Vertical selection start tracer (typically in green).
Definition: baseplotwidget.h:336
pappso::BasePlotContext::currentDragPoint
QPointF currentDragPoint
Definition: baseplotwidget.h:82
pappso::BasePlotWidget::m_axisLabelX
QString m_axisLabelX
Definition: baseplotwidget.h:309
pappso::BasePlotWidget::xAxisMeasurementSignal
void xAxisMeasurementSignal(const BasePlotContext &context, bool with_delta)
pappso::BasePlotContext::lastPressedMouseButton
Qt::MouseButtons lastPressedMouseButton
Definition: baseplotwidget.h:118
PMSPP_LIB_DECL
#define PMSPP_LIB_DECL
Definition: exportinmportconfig.h:14
pappso::BasePlotWidget
Definition: baseplotwidget.h:136
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
pappso::BasePlotContext::lastCursorHoveredPoint
QPointF lastCursorHoveredPoint
Definition: baseplotwidget.h:83
pappso::DataKind
DataKind
Definition: types.h:171
pappso::PlotAxis::none
@ none
pappso::BasePlotWidget::mouseReleaseEventSignal
void mouseReleaseEventSignal(const BasePlotContext &context)
pappso::BasePlotWidget::plottableDestructionRequestedSignal
void plottableDestructionRequestedSignal(BasePlotWidget *base_plot_widget_p, QCPAbstractPlottable *plottable_p, const BasePlotContext &context)
pappso::BasePlotContext::xRange
QCPRange xRange
Definition: baseplotwidget.h:86
pappso::BasePlotContext
Definition: baseplotwidget.h:70
pappso::BasePlotContext::mouseButtonsAtMousePress
Qt::MouseButtons mouseButtonsAtMousePress
Definition: baseplotwidget.h:123
pappso::BasePlotWidget::plottableSelectionChangedSignal
void plottableSelectionChangedSignal(QCPAbstractPlottable *plottable_p, bool selected)
pappso::BasePlotWidget::keyPressEventSignal
void keyPressEventSignal(const BasePlotContext &context)
pappso::BasePlotContext::pressedKeyCode
int pressedKeyCode
Definition: baseplotwidget.h:113
pappso::BasePlotContext::keyboardModifiers
Qt::KeyboardModifiers keyboardModifiers
Definition: baseplotwidget.h:116
pappso::RangeType
RangeType
Definition: baseplotwidget.h:63
pappso::BasePlotContext::mouseButtonsAtMouseRelease
Qt::MouseButtons mouseButtonsAtMouseRelease
Definition: baseplotwidget.h:124
pappso::BasePlotWidget::mp_vPosTracerItem
QCPItemLine * mp_vPosTracerItem
Vertical position tracer.
Definition: baseplotwidget.h:333
pappso::BasePlotWidget::m_yAxisRangeHistory
std::vector< QCPRange * > m_yAxisRangeHistory
List of y axis ranges occurring during the panning zooming actions.
Definition: baseplotwidget.h:356
pappso::BasePlotWidget::setFocusSignal
void setFocusSignal()
pappso::BasePlotContext::yRange
QCPRange yRange
Definition: baseplotwidget.h:87
pappso::RangeType::outermost
@ outermost
pappso::BasePlotWidget::m_axisLabelY
QString m_axisLabelY
Definition: baseplotwidget.h:310
pappso::BasePlotContext::releasedKeyCode
int releasedKeyCode
Definition: baseplotwidget.h:114
pappso::BasePlotWidget::keyReleaseEventSignal
void keyReleaseEventSignal(const BasePlotContext &context)
pappso::BasePlotWidget::yMinMaxOnXAxisCurrentRange
void yMinMaxOnXAxisCurrentRange(double &min, double &max, int index)
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(pappso::BasePlotContext)
pappso::BasePlotWidgetCstSPtr
std::shared_ptr< const BasePlotWidget > BasePlotWidgetCstSPtr
Definition: baseplotwidget.h:133
basePlotContextMetaTypeId
int basePlotContextMetaTypeId
Definition: baseplotwidget.cpp:40
pappso::BasePlotWidget::integrationRequestedSignal
void integrationRequestedSignal(const BasePlotContext &context)
pappso::BasePlotWidget::m_fileName
QString m_fileName
The name of the data file from which the mass data were read.
Definition: baseplotwidget.h:307
basePlotContextPtrMetaTypeId
int basePlotContextPtrMetaTypeId
Definition: baseplotwidget.cpp:42
pappso::BasePlotWidget::yMinMaxOnXAxisCurrentRange
void yMinMaxOnXAxisCurrentRange(double &min, double &max, QCPAbstractPlottable *plottable_p=nullptr)
pappso::BasePlotWidget::m_context
BasePlotContext m_context
Definition: baseplotwidget.h:312
pappso::BasePlotContext::startDragPoint
QPointF startDragPoint
Definition: baseplotwidget.h:81
pappso::BasePlotWidget::plotRangesChangedSignal
void plotRangesChangedSignal(const BasePlotContext &context)
pappso::BasePlotWidgetSPtr
std::shared_ptr< BasePlotWidget > BasePlotWidgetSPtr
Definition: baseplotwidget.h:130