libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
baseplotwidget.cpp
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/////////////////////// StdLib includes
27#include <vector>
28
29
30/////////////////////// Qt includes
31#include <QVector>
32
33
34/////////////////////// Local includes
35#include "../../core/types.h"
37#include "baseplotwidget.h"
40
41
43 qRegisterMetaType<pappso::BasePlotContext>("pappso::BasePlotContext");
45 qRegisterMetaType<pappso::BasePlotContext *>("pappso::BasePlotContext *");
46
47namespace pappso
48{
49BasePlotWidget::BasePlotWidget(QWidget *parent): QCustomPlot(parent)
50{
51 if(parent == nullptr)
52 qFatal("Programming error.");
53
54 // Default settings for the pen used to graph the data.
55 m_pen.setStyle(Qt::SolidLine);
56 m_pen.setBrush(Qt::black);
57 m_pen.setWidth(1);
58
59 // qDebug() << "Created new BasePlotWidget with" << layerCount()
60 //<< "layers before setting up widget.";
61 // qDebug().noquote() << "All layer names:\n" << allLayerNamesToString();
62
63 // As of today 20210313, the QCustomPlot is created with the following 6
64 // layers:
65 //
66 // All layers' name:
67 //
68 // Layer index 0 name: background
69 // Layer index 1 name: grid
70 // Layer index 2 name: main
71 // Layer index 3 name: axes
72 // Layer index 4 name: legend
73 // Layer index 5 name: overlay
74
75 if(!setupWidget())
76 qFatal("Programming error.");
77
78 // Do not call createAllAncillaryItems() in this base class because all the
79 // items will have been created *before* the addition of plots and then the
80 // rendering order will hide them to the viewer, since the rendering order is
81 // according to the order in which the items have been created.
82 //
83 // The fact that the ancillary items are created before trace plots is not a
84 // problem because the trace plots are sparse and do not effectively hide the
85 // data.
86 //
87 // But, in the color map plot widgets, we cannot afford to create the
88 // ancillary items *before* the plot itself because then, the rendering of the
89 // plot (created after) would screen off the ancillary items (created before).
90 //
91 // So, the createAllAncillaryItems() function needs to be called in the
92 // derived classes at the most appropriate moment in the setting up of the
93 // widget.
94 //
95 // All this is only a workaround of a bug in QCustomPlot. See
96 // https://www.qcustomplot.com/index.php/support/forum/2283.
97 //
98 // I initially wanted to have a plots layer on top of the default background
99 // layer and a items layer on top of it. But that setting prevented the
100 // selection of graphs.
101
102 // qDebug() << "Created new BasePlotWidget with" << layerCount()
103 //<< "layers after setting up widget.";
104 // qDebug().noquote() << "All layer names:\n" << allLayerNamesToString();
105
106 show();
107}
108
110 const QString &x_axis_label,
111 const QString &y_axis_label)
112 : QCustomPlot(parent), m_axisLabelX(x_axis_label), m_axisLabelY(y_axis_label)
113{
114 // qDebug();
115
116 if(parent == nullptr)
117 qFatal("Programming error.");
118
119 // Default settings for the pen used to graph the data.
120 m_pen.setStyle(Qt::SolidLine);
121 m_pen.setBrush(Qt::black);
122 m_pen.setWidth(1);
123
124 xAxis->setLabel(x_axis_label);
125 yAxis->setLabel(y_axis_label);
126
127 // qDebug() << "Created new BasePlotWidget with" << layerCount()
128 //<< "layers before setting up widget.";
129 // qDebug().noquote() << "All layer names:\n" << allLayerNamesToString();
130
131 // As of today 20210313, the QCustomPlot is created with the following 6
132 // layers:
133 //
134 // All layers' name:
135 //
136 // Layer index 0 name: background
137 // Layer index 1 name: grid
138 // Layer index 2 name: main
139 // Layer index 3 name: axes
140 // Layer index 4 name: legend
141 // Layer index 5 name: overlay
142
143 if(!setupWidget())
144 qFatal("Programming error.");
145
146 // qDebug() << "Created new BasePlotWidget with" << layerCount()
147 //<< "layers after setting up widget.";
148 // qDebug().noquote() << "All layer names:\n" << allLayerNamesToString();
149
150 show();
151}
152
153//! Destruct \c this BasePlotWidget instance.
154/*!
155
156 The destruction involves clearing the history, deleting all the axis range
157 history items for x and y axes.
158
159*/
161{
162 // qDebug() << "In the destructor of plot widget:" << this;
163
164 m_xAxisRangeHistory.clear();
165 m_yAxisRangeHistory.clear();
166
167 // Note that the QCustomPlot xxxItem objects are allocated with (this) which
168 // means their destruction is automatically handled upon *this' destruction.
169}
170
171QString
173{
174
175 QString text;
176
177 for(int iter = 0; iter < layerCount(); ++iter)
178 {
179 text +=
180 QString("Layer index %1: %2\n").arg(iter).arg(layer(iter)->name());
181 }
182
183 return text;
184}
185
186QString
187BasePlotWidget::layerableLayerName(QCPLayerable *layerable_p) const
188{
189 if(layerable_p == nullptr)
190 qFatal("Programming error.");
191
192 QCPLayer *layer_p = layerable_p->layer();
193
194 return layer_p->name();
195}
196
197int
198BasePlotWidget::layerableLayerIndex(QCPLayerable *layerable_p) const
199{
200 if(layerable_p == nullptr)
201 qFatal("Programming error.");
202
203 QCPLayer *layer_p = layerable_p->layer();
204
205 for(int iter = 0; iter < layerCount(); ++iter)
206 {
207 if(layer(iter) == layer_p)
208 return iter;
209 }
210
211 return -1;
212}
213
214void
216{
217 // Make a copy of the pen to just change its color and set that color to
218 // the tracer line.
219 QPen pen = m_pen;
220
221 // Create the lines that will act as tracers for position and selection of
222 // regions.
223 //
224 // We have the cross hair that serves as the cursor. That crosshair cursor is
225 // made of a vertical line (green, because when click-dragging the mouse it
226 // becomes the tracer that is being anchored at the region start. The second
227 // line i horizontal and is always black.
228
229 pen.setColor(QColor("steelblue"));
230
231 // The set of tracers (horizontal and vertical) that track the position of the
232 // mouse cursor.
233
234 mp_vPosTracerItem = new QCPItemLine(this);
235 mp_vPosTracerItem->setLayer("plotsLayer");
236 mp_vPosTracerItem->setPen(pen);
237 mp_vPosTracerItem->start->setType(QCPItemPosition::ptPlotCoords);
238 mp_vPosTracerItem->end->setType(QCPItemPosition::ptPlotCoords);
239 mp_vPosTracerItem->start->setCoords(0, 0);
240 mp_vPosTracerItem->end->setCoords(0, 0);
241
242 mp_hPosTracerItem = new QCPItemLine(this);
243 mp_hPosTracerItem->setLayer("plotsLayer");
244 mp_hPosTracerItem->setPen(pen);
245 mp_hPosTracerItem->start->setType(QCPItemPosition::ptPlotCoords);
246 mp_hPosTracerItem->end->setType(QCPItemPosition::ptPlotCoords);
247 mp_hPosTracerItem->start->setCoords(0, 0);
248 mp_hPosTracerItem->end->setCoords(0, 0);
249
250 // The set of tracers (horizontal only) that track the region
251 // spanning/selection regions.
252 //
253 // The start vertical tracer is colored in greeen.
254 pen.setColor(QColor("green"));
255
256 mp_vStartTracerItem = new QCPItemLine(this);
257 mp_vStartTracerItem->setLayer("plotsLayer");
258 mp_vStartTracerItem->setPen(pen);
259 mp_vStartTracerItem->start->setType(QCPItemPosition::ptPlotCoords);
260 mp_vStartTracerItem->end->setType(QCPItemPosition::ptPlotCoords);
261 mp_vStartTracerItem->start->setCoords(0, 0);
262 mp_vStartTracerItem->end->setCoords(0, 0);
263
264 // The end vertical tracer is colored in red.
265 pen.setColor(QColor("red"));
266
267 mp_vEndTracerItem = new QCPItemLine(this);
268 mp_vEndTracerItem->setLayer("plotsLayer");
269 mp_vEndTracerItem->setPen(pen);
270 mp_vEndTracerItem->start->setType(QCPItemPosition::ptPlotCoords);
271 mp_vEndTracerItem->end->setType(QCPItemPosition::ptPlotCoords);
272 mp_vEndTracerItem->start->setCoords(0, 0);
273 mp_vEndTracerItem->end->setCoords(0, 0);
274
275 // When the user click-drags the mouse, the X distance between the drag start
276 // point and the drag end point (current point) is the xDelta.
277 mp_xDeltaTextItem = new QCPItemText(this);
278 mp_xDeltaTextItem->setLayer("plotsLayer");
279 mp_xDeltaTextItem->setColor(QColor("steelblue"));
280 mp_xDeltaTextItem->setPositionAlignment(Qt::AlignBottom | Qt::AlignCenter);
281 mp_xDeltaTextItem->position->setType(QCPItemPosition::ptPlotCoords);
282 mp_xDeltaTextItem->setVisible(false);
283
284 // Same for the y delta
285 mp_yDeltaTextItem = new QCPItemText(this);
286 mp_yDeltaTextItem->setLayer("plotsLayer");
287 mp_yDeltaTextItem->setColor(QColor("steelblue"));
288 mp_yDeltaTextItem->setPositionAlignment(Qt::AlignBottom | Qt::AlignCenter);
289 mp_yDeltaTextItem->position->setType(QCPItemPosition::ptPlotCoords);
290 mp_yDeltaTextItem->setVisible(false);
291
292 // Make sure we prepare the four lines that will be needed to
293 // draw the selection rectangle.
294 pen = m_pen;
295
296 pen.setColor("steelblue");
297
298 mp_selectionRectangeLine1 = new QCPItemLine(this);
299 mp_selectionRectangeLine1->setLayer("plotsLayer");
300 mp_selectionRectangeLine1->setPen(pen);
301 mp_selectionRectangeLine1->start->setType(QCPItemPosition::ptPlotCoords);
302 mp_selectionRectangeLine1->end->setType(QCPItemPosition::ptPlotCoords);
303 mp_selectionRectangeLine1->start->setCoords(0, 0);
304 mp_selectionRectangeLine1->end->setCoords(0, 0);
305 mp_selectionRectangeLine1->setVisible(false);
306
307 mp_selectionRectangeLine2 = new QCPItemLine(this);
308 mp_selectionRectangeLine2->setLayer("plotsLayer");
309 mp_selectionRectangeLine2->setPen(pen);
310 mp_selectionRectangeLine2->start->setType(QCPItemPosition::ptPlotCoords);
311 mp_selectionRectangeLine2->end->setType(QCPItemPosition::ptPlotCoords);
312 mp_selectionRectangeLine2->start->setCoords(0, 0);
313 mp_selectionRectangeLine2->end->setCoords(0, 0);
314 mp_selectionRectangeLine2->setVisible(false);
315
316 mp_selectionRectangeLine3 = new QCPItemLine(this);
317 mp_selectionRectangeLine3->setLayer("plotsLayer");
318 mp_selectionRectangeLine3->setPen(pen);
319 mp_selectionRectangeLine3->start->setType(QCPItemPosition::ptPlotCoords);
320 mp_selectionRectangeLine3->end->setType(QCPItemPosition::ptPlotCoords);
321 mp_selectionRectangeLine3->start->setCoords(0, 0);
322 mp_selectionRectangeLine3->end->setCoords(0, 0);
323 mp_selectionRectangeLine3->setVisible(false);
324
325 mp_selectionRectangeLine4 = new QCPItemLine(this);
326 mp_selectionRectangeLine4->setLayer("plotsLayer");
327 mp_selectionRectangeLine4->setPen(pen);
328 mp_selectionRectangeLine4->start->setType(QCPItemPosition::ptPlotCoords);
329 mp_selectionRectangeLine4->end->setType(QCPItemPosition::ptPlotCoords);
330 mp_selectionRectangeLine4->start->setCoords(0, 0);
331 mp_selectionRectangeLine4->end->setCoords(0, 0);
332 mp_selectionRectangeLine4->setVisible(false);
333}
334
335bool
337{
338 // qDebug();
339
340 // By default the widget comes with a graph. Remove it.
341
342 if(graphCount())
343 {
344 // QCPLayer *layer_p = graph(0)->layer();
345 // qDebug() << "The graph was on layer:" << layer_p->name();
346
347 // As of today 20210313, the graph is created on the currentLayer(), that
348 // is "main".
349
350 removeGraph(0);
351 }
352
353 // The general idea is that we do want custom layers for the trace|colormap
354 // plots.
355
356 // qDebug().noquote() << "Right before creating the new layer, layers:\n"
357 //<< allLayerNamesToString();
358
359 // Add the layer that will store all the plots and all the ancillary items.
360 addLayer(
361 "plotsLayer", layer("background"), QCustomPlot::LayerInsertMode::limAbove);
362 // qDebug().noquote() << "Added new plotsLayer, layers:\n"
363 //<< allLayerNamesToString();
364
365 // This is required so that we get the keyboard events.
366 setFocusPolicy(Qt::StrongFocus);
367 setInteractions(QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iMultiSelect);
368
369 // We want to capture the signals emitted by the QCustomPlot base class.
370 connect(
371 this, &QCustomPlot::mouseMove, this, &BasePlotWidget::mouseMoveHandler);
372
373 connect(
374 this, &QCustomPlot::mousePress, this, &BasePlotWidget::mousePressHandler);
375
376 connect(this,
377 &QCustomPlot::mouseRelease,
378 this,
380
381 connect(
382 this, &QCustomPlot::mouseWheel, this, &BasePlotWidget::mouseWheelHandler);
383
384 connect(this,
385 &QCustomPlot::axisDoubleClick,
386 this,
388
389 return true;
390}
391
392void
394{
395 m_pen = pen;
396}
397
398const QPen &
400{
401 return m_pen;
402}
403
404void
405BasePlotWidget::setPlottingColor(QCPAbstractPlottable *plottable_p,
406 const QColor &new_color)
407{
408 if(plottable_p == nullptr)
409 qFatal("Pointer cannot be nullptr.");
410
411 // First this single-graph widget
412 QPen pen;
413
414 pen = plottable_p->pen();
415 pen.setColor(new_color);
416 plottable_p->setPen(pen);
417
418 replot();
419}
420
421void
422BasePlotWidget::setPlottingColor(int index, const QColor &new_color)
423{
424 if(!new_color.isValid())
425 return;
426
427 QCPGraph *graph_p = graph(index);
428
429 if(graph_p == nullptr)
430 qFatal("Programming error.");
431
432 return setPlottingColor(graph_p, new_color);
433}
434
435QColor
436BasePlotWidget::getPlottingColor(QCPAbstractPlottable *plottable_p) const
437{
438 if(plottable_p == nullptr)
439 qFatal("Programming error.");
440
441 return plottable_p->pen().color();
442}
443
444QColor
446{
447 QCPGraph *graph_p = graph(index);
448
449 if(graph_p == nullptr)
450 qFatal("Programming error.");
451
452 return getPlottingColor(graph_p);
453}
454
455void
456BasePlotWidget::setAxisLabelX(const QString &label)
457{
458 xAxis->setLabel(label);
459}
460
461void
462BasePlotWidget::setAxisLabelY(const QString &label)
463{
464 yAxis->setLabel(label);
465}
466
467// AXES RANGE HISTORY-related functions
468void
470{
471 m_xAxisRangeHistory.clear();
472 m_yAxisRangeHistory.clear();
473
474 m_xAxisRangeHistory.push_back(new QCPRange(xAxis->range()));
475 m_yAxisRangeHistory.push_back(new QCPRange(yAxis->range()));
476
477 // qDebug() << "size of history:" << m_xAxisRangeHistory.size()
478 //<< "setting index to 0";
479
480 // qDebug() << "resetting axes history to values:" << xAxis->range().lower
481 //<< "--" << xAxis->range().upper << "and" << yAxis->range().lower
482 //<< "--" << yAxis->range().upper;
483
485}
486
487//! Create new axis range history items and append them to the history.
488/*!
489
490 The plot widget is queried to get the current x/y-axis ranges and the
491 current ranges are appended to the history for x-axis and for y-axis.
492
493*/
494void
496{
497 m_xAxisRangeHistory.push_back(new QCPRange(xAxis->range()));
498 m_yAxisRangeHistory.push_back(new QCPRange(yAxis->range()));
499
501
502 // qDebug() << "axes history size:" << m_xAxisRangeHistory.size()
503 //<< "current index:" << m_lastAxisRangeHistoryIndex
504 //<< xAxis->range().lower << "--" << xAxis->range().upper << "and"
505 //<< yAxis->range().lower << "--" << yAxis->range().upper;
506}
507
508//! Go up one history element in the axis history.
509/*!
510
511 If possible, back up one history item in the axis histories and update the
512 plot's x/y-axis ranges to match that history item.
513
514*/
515void
517{
518 // qDebug() << "axes history size:" << m_xAxisRangeHistory.size()
519 //<< "current index:" << m_lastAxisRangeHistoryIndex;
520
522 {
523 // qDebug() << "current index is 0 returning doing nothing";
524
525 return;
526 }
527
528 // qDebug() << "Setting index to:" << m_lastAxisRangeHistoryIndex - 1
529 //<< "and restoring axes history to that index";
530
532}
533
534//! Get the axis histories at index \p index and update the plot ranges.
535/*!
536
537 \param index index at which to select the axis history item.
538
539 \sa updateAxesRangeHistory().
540
541*/
542void
544{
545 // qDebug() << "Axes history size:" << m_xAxisRangeHistory.size()
546 //<< "current index:" << m_lastAxisRangeHistoryIndex
547 //<< "asking to restore index:" << index;
548
549 if(index >= m_xAxisRangeHistory.size())
550 {
551 // qDebug() << "index >= history size. Returning.";
552 return;
553 }
554
555 // We want to go back to the range history item at index, which means we want
556 // to pop back all the items between index+1 and size-1.
557
558 while(m_xAxisRangeHistory.size() > index + 1)
559 m_xAxisRangeHistory.pop_back();
560
561 if(m_xAxisRangeHistory.size() - 1 != index)
562 qFatal("Programming error.");
563
564 xAxis->setRange(*(m_xAxisRangeHistory.at(index)));
565 yAxis->setRange(*(m_yAxisRangeHistory.at(index)));
566
568
569 mp_vPosTracerItem->setVisible(false);
570 mp_hPosTracerItem->setVisible(false);
571
572 mp_vStartTracerItem->setVisible(false);
573 mp_vEndTracerItem->setVisible(false);
574
575
576 // The start tracer will keep beeing represented at the last position and last
577 // size even if we call this function repetitively. So actually do not show,
578 // it will reappare as soon as the mouse is moved.
579 // if(m_shouldTracersBeVisible)
580 //{
581 // mp_vStartTracerItem->setVisible(true);
582 //}
583
584 replot();
585
587
588 // qDebug() << "restored axes history to index:" << index
589 //<< "with values:" << xAxis->range().lower << "--"
590 //<< xAxis->range().upper << "and" << yAxis->range().lower << "--"
591 //<< yAxis->range().upper;
592
594}
595
596// AXES RANGE HISTORY-related functions
597
598
599/// KEYBOARD-related EVENTS
600void
602{
603 // qDebug() << "ENTER";
604
605 // We need this because some keys modify our behaviour.
606 m_context.m_pressedKeyCode = event->key();
607 m_context.m_keyboardModifiers = QGuiApplication::queryKeyboardModifiers();
608
609 if(event->key() == Qt::Key_Left || event->key() == Qt::Key_Right ||
610 event->key() == Qt::Key_Up || event->key() == Qt::Key_Down)
611 {
612 return directionKeyPressEvent(event);
613 }
614 else if(event->key() == m_leftMousePseudoButtonKey ||
615 event->key() == m_rightMousePseudoButtonKey)
616 {
617 return mousePseudoButtonKeyPressEvent(event);
618 }
619
620 // Do not do anything here, because this function is used by derived classes
621 // that will emit the signal below. Otherwise there are going to be multiple
622 // signals sent.
623 // qDebug() << "Going to emit keyPressEventSignal(m_context);";
624 // emit keyPressEventSignal(m_context);
625}
626
627//! Handle specific key codes and trigger respective actions.
628void
630{
631 m_context.m_releasedKeyCode = event->key();
632
633 // The keyboard key is being released, set the key code to 0.
634 m_context.m_pressedKeyCode = 0;
635
636 m_context.m_keyboardModifiers = QGuiApplication::queryKeyboardModifiers();
637
638 // Now test if the key that was released is one of the housekeeping keys.
639 if(event->key() == Qt::Key_Backspace)
640 {
641 // qDebug();
642
643 // The user wants to iterate back in the x/y axis range history.
645
646 event->accept();
647 }
648 else if(event->key() == Qt::Key_Space)
649 {
650 return spaceKeyReleaseEvent(event);
651 }
652 else if(event->key() == Qt::Key_Delete)
653 {
654 // The user wants to delete a graph. What graph is to be determined
655 // programmatically:
656
657 // If there is a single graph, then that is the graph to be removed.
658 // If there are more than one graph, then only the ones that are selected
659 // are to be removed.
660
661 // Note that the user of this widget might want to provide the user with
662 // the ability to specify if all the children graph needs to be removed
663 // also. This can be coded in key modifiers. So provide the context.
664
665 int graph_count = plottableCount();
666
667 if(!graph_count)
668 {
669 // qDebug() << "Not a single graph in the plot widget. Doing
670 // nothing.";
671
672 event->accept();
673 return;
674 }
675
676 if(graph_count == 1)
677 {
678 // qDebug() << "A single graph is in the plot widget. Emitting a graph
679 // " "destruction requested signal for it:"
680 //<< graph();
681
683 }
684 else
685 {
686 // At this point we know there are more than one graph in the plot
687 // widget. We need to get the selected one (if any).
688 QList<QCPGraph *> selected_graph_list;
689
690 selected_graph_list = selectedGraphs();
691
692 if(!selected_graph_list.size())
693 {
694 event->accept();
695 return;
696 }
697
698 // qDebug() << "Number of selected graphs to be destrobyed:"
699 //<< selected_graph_list.size();
700
701 for(int iter = 0; iter < selected_graph_list.size(); ++iter)
702 {
703 // qDebug()
704 //<< "Emitting a graph destruction requested signal for graph:"
705 //<< selected_graph_list.at(iter);
706
708 this, selected_graph_list.at(iter), m_context);
709
710 // We do not do this, because we want the slot called by the
711 // signal above to handle that removal. Remember that it is not
712 // possible to delete graphs manually.
713 //
714 // removeGraph(selected_graph_list.at(iter));
715 }
716 event->accept();
717 }
718 }
719 // End of
720 // else if(event->key() == Qt::Key_Delete)
721 else if(event->key() == Qt::Key_T)
722 {
723 // The user wants to toggle the visibiity of the tracers.
725
727 hideTracers();
728 else
729 showTracers();
730
731 event->accept();
732 }
733 else if(event->key() == Qt::Key_Left || event->key() == Qt::Key_Right ||
734 event->key() == Qt::Key_Up || event->key() == Qt::Key_Down)
735 {
736 return directionKeyReleaseEvent(event);
737 }
738 else if(event->key() == m_leftMousePseudoButtonKey ||
739 event->key() == m_rightMousePseudoButtonKey)
740 {
742 }
743 else if(event->key() == Qt::Key_S)
744 {
745 // The user is defining the size of the rhomboid fixed side. That could be
746 // either a vertical side (less intuitive) or a horizontal size (more
747 // intuitive, first exclusive implementation). But, in order to be able to
748 // perform identical integrations starting from non-transposed color maps
749 // and transposed color maps, the ability to define a vertical fixed size
750 // side of the rhomboid integration scope has become necessary.
751
752 // Check if the vertical displacement is significant (>= 10% of the color
753 // map height.
754
756 {
757 // The user is dragging the cursor vertically in a sufficient delta to
758 // consider that they are willing to define a vertical fixed size
759 // of the rhomboid integration scope.
760
761 m_context.m_integrationScopeRhombWidth = 0;
762 m_context.m_integrationScopeRhombHeight = abs(
763 m_context.m_currentDragPoint.y() - m_context.m_startDragPoint.y());
764
765 // qDebug() << "Set m_context.m_integrationScopePolyHeight to"
766 // << m_context.m_integrationScopeRhombHeight
767 // << "upon release of S key";
768 }
769 else
770 {
771 // The user is dragging the cursor horiontally to define a horizontal
772 // fixed size of the rhomboid integration scope.
773
774 m_context.m_integrationScopeRhombWidth = abs(
775 m_context.m_currentDragPoint.x() - m_context.m_startDragPoint.x());
776 m_context.m_integrationScopeRhombHeight = 0;
777
778 // qDebug() << "Set m_context.m_integrationScopePolyWidth to"
779 // << m_context.m_integrationScopeRhombWidth
780 // << "upon release of S key";
781 }
782 }
783 // At this point emit the signal, since we did not treat it. Maybe the
784 // consumer widget wants to know that the keyboard key was released.
785
787}
788
789void
790BasePlotWidget::spaceKeyReleaseEvent([[maybe_unused]] QKeyEvent *event)
791{
792 // qDebug();
793}
794
795void
797{
798 // qDebug() << "event key:" << event->key();
799
800 // The user is trying to move the positional cursor/markers. There are
801 // multiple way they can do that:
802 //
803 // 1.a. Hitting the arrow left/right keys alone will search for next pixel.
804 // 1.b. Hitting the arrow left/right keys with Alt modifier will search for
805 // a multiple of pixels that might be equivalent to one 20th of the pixel
806 // width of the plot widget. 1.c Hitting the left/right keys with Alt and
807 // Shift modifiers will search for a multiple of pixels that might be the
808 // equivalent to half of the pixel width.
809 //
810 // 2. Hitting the Control modifier will move the cursor to the next data
811 // point of the graph.
812
813 int pixel_increment = 0;
814
815 if(m_context.m_keyboardModifiers == Qt::NoModifier)
816 pixel_increment = 1;
817 else if(m_context.m_keyboardModifiers == Qt::AltModifier)
818 pixel_increment = 50;
819
820 // The user is moving the positional markers. This is equivalent to a
821 // non-dragging cursor movement to the next pixel. Note that the origin is
822 // located at the top left, so key down increments and key up decrements.
823
824 if(event->key() == Qt::Key_Left)
825 horizontalMoveMouseCursorCountPixels(-pixel_increment);
826 else if(event->key() == Qt::Key_Right)
828 else if(event->key() == Qt::Key_Up)
829 verticalMoveMouseCursorCountPixels(-pixel_increment);
830 else if(event->key() == Qt::Key_Down)
831 verticalMoveMouseCursorCountPixels(pixel_increment);
832
833 event->accept();
834}
835
836void
838{
839 // qDebug() << "event key:" << event->key();
840 event->accept();
841}
842
843void
845 [[maybe_unused]] QKeyEvent *event)
846{
847 // qDebug();
848}
849
850void
852{
853
854 QPointF pixel_coordinates(
855 xAxis->coordToPixel(m_context.m_lastCursorHoveredPoint.x()),
856 yAxis->coordToPixel(m_context.m_lastCursorHoveredPoint.y()));
857
858 Qt::MouseButton button = Qt::NoButton;
859 QEvent::Type q_event_type = QEvent::MouseButtonPress;
860
861 if(event->key() == m_leftMousePseudoButtonKey)
862 {
863 // Toggles the left mouse button on/off
864
865 button = Qt::LeftButton;
866
867 m_context.m_isLeftPseudoButtonKeyPressed =
868 !m_context.m_isLeftPseudoButtonKeyPressed;
869
870 if(m_context.m_isLeftPseudoButtonKeyPressed)
871 q_event_type = QEvent::MouseButtonPress;
872 else
873 q_event_type = QEvent::MouseButtonRelease;
874 }
875 else if(event->key() == m_rightMousePseudoButtonKey)
876 {
877 // Toggles the right mouse button.
878
879 button = Qt::RightButton;
880
881 m_context.m_isRightPseudoButtonKeyPressed =
882 !m_context.m_isRightPseudoButtonKeyPressed;
883
884 if(m_context.m_isRightPseudoButtonKeyPressed)
885 q_event_type = QEvent::MouseButtonPress;
886 else
887 q_event_type = QEvent::MouseButtonRelease;
888 }
889
890 // qDebug() << "pressed/released pseudo button:" << button
891 //<< "q_event_type:" << q_event_type;
892
893 // Synthesize a QMouseEvent and use it.
894
895 QMouseEvent *mouse_event_p =
896 new QMouseEvent(q_event_type,
897 pixel_coordinates,
898 mapToGlobal(pixel_coordinates.toPoint()),
899 mapToGlobal(pixel_coordinates.toPoint()),
900 button,
901 button,
902 m_context.m_keyboardModifiers,
903 Qt::MouseEventSynthesizedByApplication);
904
905 if(q_event_type == QEvent::MouseButtonPress)
906 mousePressHandler(mouse_event_p);
907 else
908 mouseReleaseHandler(mouse_event_p);
909
910 // event->accept();
911}
912
913/// KEYBOARD-related EVENTS
914
915
916/// MOUSE-related EVENTS
917
918void
920{
921
922 // If we have no focus, then get it. See setFocus() to understand why asking
923 // for focus is cosly and thus why we want to make this decision first.
924 if(!hasFocus())
925 setFocus();
926
927 // qDebug() << (graph() != nullptr);
928 // if(graph(0) != nullptr)
929 // { // check if the widget contains some graphs
930
931 // The event->button() must be by Qt instructions considered to be 0.
932
933 // Whatever happens, we want to store the plot coordinates of the current
934 // mouse cursor position (will be useful later for countless needs).
935
936 QPointF mousePoint = event->position();
937
938 // qDebug() << "local mousePoint position in pixels:" << mousePoint;
939
940 m_context.m_lastCursorHoveredPoint.setX(xAxis->pixelToCoord(mousePoint.x()));
941 m_context.m_lastCursorHoveredPoint.setY(yAxis->pixelToCoord(mousePoint.y()));
942
943 // qDebug() << "lastCursorHoveredPoint coord:"
944 //<< m_context.m_lastCursorHoveredPoint;
945
946 // Now, depending on the button(s) (if any) that are pressed or not, we
947 // have a different processing.
948
949 // qDebug();
950
951 if(m_context.m_pressedMouseButtons & Qt::LeftButton ||
952 m_context.m_pressedMouseButtons & Qt::RightButton)
954 else
956 // }
957 // qDebug();
958 event->accept();
959}
960
961void
963{
964
965 // qDebug();
966 m_context.m_isMouseDragging = false;
967
968 // qDebug();
969 // We are not dragging the mouse (no button pressed), simply let this
970 // widget's consumer know the position of the cursor and update the markers.
971 // The consumer of this widget will update mouse cursor position at
972 // m_context.m_lastCursorHoveredPoint if so needed.
973
974 emit lastCursorHoveredPointSignal(m_context.m_lastCursorHoveredPoint);
975
976 // qDebug();
977
978 // We are not dragging, so we do not show the region end tracer we only
979 // show the anchoring start trace that might be of use if the user starts
980 // using the arrow keys to move the cursor.
981 if(mp_vEndTracerItem != nullptr)
982 mp_vEndTracerItem->setVisible(false);
983
984 // qDebug();
985 // Only bother with the tracers if the user wants them to be visible.
986 // Their crossing point must be exactly at the last cursor-hovered point.
987
989 {
990 // We are not dragging, so only show the position markers (v and h);
991
992 // qDebug();
993 if(mp_hPosTracerItem != nullptr)
994 {
995 // Horizontal position tracer.
996 mp_hPosTracerItem->setVisible(true);
997 mp_hPosTracerItem->start->setCoords(
998 xAxis->range().lower, m_context.m_lastCursorHoveredPoint.y());
999 mp_hPosTracerItem->end->setCoords(
1000 xAxis->range().upper, m_context.m_lastCursorHoveredPoint.y());
1001 }
1002
1003 // qDebug();
1004 // Vertical position tracer.
1005 if(mp_vPosTracerItem != nullptr)
1006 {
1007 mp_vPosTracerItem->setVisible(true);
1008
1009 mp_vPosTracerItem->setVisible(true);
1010 mp_vPosTracerItem->start->setCoords(
1011 m_context.m_lastCursorHoveredPoint.x(), yAxis->range().upper);
1012 mp_vPosTracerItem->end->setCoords(
1013 m_context.m_lastCursorHoveredPoint.x(), yAxis->range().lower);
1014 }
1015
1016 // qDebug();
1017 replot();
1018 }
1019
1020
1021 return;
1022}
1023
1024void
1026{
1027 // qDebug();
1028
1029 m_context.m_isMouseDragging = true;
1030
1031 // Now store the mouse position data into the the current drag point
1032 // member datum, that will be used in countless occasions later.
1033 m_context.m_currentDragPoint = m_context.m_lastCursorHoveredPoint;
1034 m_context.m_keyboardModifiers = QGuiApplication::queryKeyboardModifiers();
1035
1036 // When we drag (either keyboard or mouse), we hide the position markers
1037 // (black) and we show the start and end vertical markers for the region.
1038 // Then, we draw the horizontal region range marker that delimits
1039 // horizontally the dragged-over region.
1040
1041 if(mp_hPosTracerItem != nullptr)
1042 mp_hPosTracerItem->setVisible(false);
1043 if(mp_vPosTracerItem != nullptr)
1044 mp_vPosTracerItem->setVisible(false);
1045
1046 // Only bother with the tracers if the user wants them to be visible.
1048 {
1049
1050 // The vertical end tracer position must be refreshed.
1051 mp_vEndTracerItem->start->setCoords(m_context.m_currentDragPoint.x(),
1052 yAxis->range().upper);
1053
1054 mp_vEndTracerItem->end->setCoords(m_context.m_currentDragPoint.x(),
1055 yAxis->range().lower);
1056
1057 mp_vEndTracerItem->setVisible(true);
1058 }
1059
1060 // Whatever the button, when we are dealing with the axes, we do not
1061 // want to show any of the tracers.
1062
1063 if(m_context.m_wasClickOnXAxis || m_context.m_wasClickOnYAxis)
1064 {
1065 if(mp_hPosTracerItem != nullptr)
1066 mp_hPosTracerItem->setVisible(false);
1067 if(mp_vPosTracerItem != nullptr)
1068 mp_vPosTracerItem->setVisible(false);
1069
1070 if(mp_vStartTracerItem != nullptr)
1071 mp_vStartTracerItem->setVisible(false);
1072 if(mp_vEndTracerItem != nullptr)
1073 mp_vEndTracerItem->setVisible(false);
1074 }
1075 else
1076 {
1077 // qDebug() << "Not moving the mouse cursor over any of the axes.";
1078
1079 // Since we are not dragging the mouse cursor over the axes, make sure
1080 // we store the drag directions in the context, as this might be
1081 // useful for later operations.
1082 // qDebug() << "Recording the drag direction(s).";
1083
1084 m_context.recordDragDirections();
1085
1086 // qDebug() << "Drag direction(s): " <<
1087 // m_context.dragDirectionsToString();
1088 }
1089
1090 // Because when we drag the mouse button (whatever the button) we need to
1091 // know what is the drag delta (distance between start point and current
1092 // point of the drag operation) on both axes, ask that these x|y deltas be
1093 // computed.
1095
1096 // Now deal with the BUTTON-SPECIFIC CODE.
1097
1098 if(m_context.m_mouseButtonsAtMousePress & Qt::LeftButton)
1099 {
1101 }
1102 else if(m_context.m_mouseButtonsAtMousePress & Qt::RightButton)
1103 {
1105 }
1106}
1107
1108void
1110{
1111 // qDebug() << "The left button is dragging.";
1112
1113 // Set the context.m_isMeasuringDistance to false, which later might be set
1114 // to true if effectively we are measuring a distance. This is required
1115 // because the derived widget classes might want to know if they have to
1116 // perform some action on the basis that context is measuring a distance,
1117 // for example the mass spectrum-specific widget might want to compute
1118 // deconvolutions.
1119
1120 m_context.m_isMeasuringDistance = false;
1121
1122 // Let's first check if the mouse drag operation originated on either
1123 // axis. In that case, the user is performing axis reframing or rescaling.
1124
1125 if(m_context.m_wasClickOnXAxis || m_context.m_wasClickOnYAxis)
1126 {
1127 // qDebug() << "Click was on one of the axes.";
1128
1129 if(m_context.m_keyboardModifiers & Qt::ControlModifier)
1130 {
1131 // The user is asking a rescale of the plot.
1132
1133 // We know that we do not want the tracers when we perform axis
1134 // rescaling operations.
1135
1136 if(mp_hPosTracerItem != nullptr)
1137 mp_hPosTracerItem->setVisible(false);
1138 if(mp_vPosTracerItem != nullptr)
1139 mp_vPosTracerItem->setVisible(false);
1140
1141 if(mp_vStartTracerItem != nullptr)
1142 mp_vStartTracerItem->setVisible(false);
1143 if(mp_vEndTracerItem != nullptr)
1144 mp_vEndTracerItem->setVisible(false);
1145
1146 // This operation is particularly intensive, thus we want to
1147 // reduce the number of calculations by skipping this calculation
1148 // a number of times. The user can ask for this feature by
1149 // clicking the 'Q' letter.
1150
1151 if(m_context.m_pressedKeyCode == Qt::Key_Q)
1152 {
1154 {
1156 return;
1157 }
1158 else
1159 {
1161 }
1162 }
1163
1164 // qDebug() << "Asking that the axes be rescaled.";
1165
1166 axisRescale();
1167 }
1168 else
1169 {
1170 // The user was simply dragging the axis. Just pan, that is slide
1171 // the plot in the same direction as the mouse movement and with the
1172 // same amplitude.
1173
1174 // qDebug() << "Asking that the axes be panned.";
1175
1176 axisPan();
1177 }
1178
1179 return;
1180 }
1181
1182 // At this point we understand that the user was not performing any
1183 // panning/rescaling operation by clicking on any one of the axes.. Go on
1184 // with other possibilities.
1185
1186 // Let's check if the user is actually drawing a rectangle (covering a
1187 // real area) or is drawing a line.
1188
1189 // qDebug() << "The mouse dragging did not originate on an axis.";
1190
1192 {
1193 // qDebug() << "Apparently the selection is two-dimensional.";
1194
1195 // When we draw a two-dimensional integration scope, the tracers are of no
1196 // use.
1197
1198 if(mp_hPosTracerItem != nullptr)
1199 mp_hPosTracerItem->setVisible(false);
1200 if(mp_vPosTracerItem != nullptr)
1201 mp_vPosTracerItem->setVisible(false);
1202
1203 if(mp_vStartTracerItem != nullptr)
1204 mp_vStartTracerItem->setVisible(false);
1205 if(mp_vEndTracerItem != nullptr)
1206 mp_vEndTracerItem->setVisible(false);
1207
1208 // Draw the rectangle, false, not as line segment and
1209 // false, not for integration
1210 drawSelectionRectangleAndPrepareZoom(false /*as_line_segment*/,
1211 false /* for_integration*/);
1212
1213 // Draw the selection width/height text
1216 }
1217 else
1218 {
1219 // qDebug() << "Apparently we are measuring a delta.";
1220
1221 // Draw the rectangle, true, as line segment and
1222 // false, not for integration
1224
1225 // The pure position tracers should be hidden.
1226 if(mp_hPosTracerItem != nullptr)
1227 mp_hPosTracerItem->setVisible(true);
1228 if(mp_vPosTracerItem != nullptr)
1229 mp_vPosTracerItem->setVisible(true);
1230
1231 // Then, make sure the region range vertical tracers are visible.
1232 if(mp_vStartTracerItem != nullptr)
1233 mp_vStartTracerItem->setVisible(true);
1234 if(mp_vEndTracerItem != nullptr)
1235 mp_vEndTracerItem->setVisible(true);
1236
1237 // Draw the selection width text
1239 }
1240}
1241
1242void
1244{
1245 // qDebug() << "The right button is dragging.";
1246
1247 // Set the context.m_isMeasuringDistance to false, which later might be set
1248 // to true if effectively we are measuring a distance. This is required
1249 // because the derived widgets might want to know if they have to perform
1250 // some action on the basis that context is measuring a distance, for
1251 // example the mass spectrum-specific widget might want to compute
1252 // deconvolutions.
1253
1254 m_context.m_isMeasuringDistance = false;
1255
1257 {
1258 // qDebug() << "Apparently the selection has height.";
1259
1260 // When we draw a rectangle the tracers are of no use.
1261
1262 if(mp_hPosTracerItem != nullptr)
1263 mp_hPosTracerItem->setVisible(false);
1264 if(mp_vPosTracerItem != nullptr)
1265 mp_vPosTracerItem->setVisible(false);
1266
1267 if(mp_vStartTracerItem != nullptr)
1268 mp_vStartTracerItem->setVisible(false);
1269 if(mp_vEndTracerItem != nullptr)
1270 mp_vEndTracerItem->setVisible(false);
1271
1272 // Draw the rectangle, false for as_line_segment and true for
1273 // integration.
1275
1276 // Draw the selection width/height text
1279 }
1280 else
1281 {
1282 // qDebug() << "Apparently the selection is a not a rectangle.";
1283
1284 // Draw the rectangle, true as line segment and
1285 // true for integration
1287
1288 // Draw the selection width text
1290 }
1291}
1292
1293void
1295{
1296 // qDebug() << "Entering";
1297
1298 // When the user clicks this widget it has to take focus.
1299 setFocus();
1300
1301 QPointF mousePoint = event->position();
1302
1303 m_context.m_lastPressedMouseButton = event->button();
1304 m_context.m_mouseButtonsAtMousePress = event->buttons();
1305
1306 // The pressedMouseButtons must continually inform on the status of
1307 // pressed buttons so add the pressed button.
1308 m_context.m_pressedMouseButtons |= event->button();
1309
1310 // qDebug().noquote() << m_context.toString();
1311
1312 // In all the processing of the events, we need to know if the user is
1313 // clicking somewhere with the intent to change the plot ranges (reframing
1314 // or rescaling the plot).
1315 //
1316 // Reframing the plot means that the new x and y axes ranges are modified
1317 // so that they match the region that the user has encompassed by left
1318 // clicking the mouse and dragging it over the plot. That is we reframe
1319 // the plot so that it contains only the "selected" region.
1320 //
1321 // Rescaling the plot means the the new x|y axis range is modified such
1322 // that the lower axis range is constant and the upper axis range is moved
1323 // either left or right by the same amont as the x|y delta encompassed by
1324 // the user moving the mouse. The axis is thus either compressed (mouse
1325 // movement is leftwards) or un-compressed (mouse movement is rightwards).
1326
1327 // There are two ways to perform axis range modifications:
1328 //
1329 // 1. By clicking on any of the axes
1330 // 2. By clicking on the plot region but using keyboard key modifiers,
1331 // like Alt and Ctrl.
1332 //
1333 // We need to know both cases separately which is why we need to perform a
1334 // number of tests below.
1335
1336 // Let's check if the click is on the axes, either X or Y, because that
1337 // will allow us to take proper actions.
1338
1339 if(isClickOntoXAxis(mousePoint))
1340 {
1341 // The X axis was clicked upon, we need to document that:
1342 // qDebug() << __FILE__ << __LINE__
1343 //<< "Layout element is axisRect and actually on an X axis part.";
1344
1345 m_context.m_wasClickOnXAxis = true;
1346
1347 // int currentInteractions = interactions();
1348 // currentInteractions |= QCP::iRangeDrag;
1349 // setInteractions((QCP::Interaction)currentInteractions);
1350 // axisRect()->setRangeDrag(xAxis->orientation());
1351 }
1352 else
1353 m_context.m_wasClickOnXAxis = false;
1354
1355 if(isClickOntoYAxis(mousePoint))
1356 {
1357 // The Y axis was clicked upon, we need to document that:
1358 // qDebug() << __FILE__ << __LINE__
1359 //<< "Layout element is axisRect and actually on an Y axis part.";
1360
1361 m_context.m_wasClickOnYAxis = true;
1362
1363 // int currentInteractions = interactions();
1364 // currentInteractions |= QCP::iRangeDrag;
1365 // setInteractions((QCP::Interaction)currentInteractions);
1366 // axisRect()->setRangeDrag(yAxis->orientation());
1367 }
1368 else
1369 m_context.m_wasClickOnYAxis = false;
1370
1371 // At this point, let's see if we need to remove the QCP::iRangeDrag bit:
1372
1373 if(!m_context.m_wasClickOnXAxis && !m_context.m_wasClickOnYAxis)
1374 {
1375 // qDebug() << __FILE__ << __LINE__
1376 // << "Click outside of axes.";
1377
1378 // int currentInteractions = interactions();
1379 // currentInteractions = currentInteractions & ~QCP::iRangeDrag;
1380 // setInteractions((QCP::Interaction)currentInteractions);
1381 }
1382
1383 m_context.m_startDragPoint.setX(xAxis->pixelToCoord(mousePoint.x()));
1384 m_context.m_startDragPoint.setY(yAxis->pixelToCoord(mousePoint.y()));
1385
1386 // Now install the vertical start tracer at the last cursor hovered
1387 // position.
1388 if((m_shouldTracersBeVisible) && (mp_vStartTracerItem != nullptr))
1389 mp_vStartTracerItem->setVisible(true);
1390
1391 if(mp_vStartTracerItem != nullptr)
1392 {
1393 mp_vStartTracerItem->start->setCoords(
1394 m_context.m_lastCursorHoveredPoint.x(), yAxis->range().upper);
1395 mp_vStartTracerItem->end->setCoords(
1396 m_context.m_lastCursorHoveredPoint.x(), yAxis->range().lower);
1397 }
1398
1399 replot();
1400
1402
1403 // qDebug() << "Exiting after having emitted mousePressEventSignal with base
1404 // context:"
1405 // << m_context.toString();
1406}
1407
1408void
1410{
1411 // qDebug() << "Entering";
1412
1413 // Now the real code of this function.
1414
1415 m_context.m_lastReleasedMouseButton = event->button();
1416
1417 // The event->buttons() is the description of the buttons that are pressed
1418 // at the moment the handler is invoked, that is now. If left and right were
1419 // pressed, and left was released, event->buttons() would be right.
1420 m_context.m_mouseButtonsAtMouseRelease = event->buttons();
1421
1422 // The pressedMouseButtons must continually inform on the status of pressed
1423 // buttons so remove the released button.
1424 m_context.m_pressedMouseButtons ^= event->button();
1425
1426 // qDebug().noquote() << m_context.toString();
1427
1428 // We'll need to know if modifiers were pressed a the moment the user
1429 // released the mouse button.
1430 m_context.m_keyboardModifiers = QGuiApplication::keyboardModifiers();
1431
1432 if(!m_context.m_isMouseDragging)
1433 {
1434 // Let the user know that the mouse was *not* being dragged.
1435 m_context.m_wasMouseDragging = false;
1436
1437 event->accept();
1438
1439 return;
1440 }
1441
1442 // Let the user know that the mouse was being dragged.
1443 m_context.m_wasMouseDragging = true;
1444
1445 // We cannot hide all items in one go because we rely on their visibility
1446 // to know what kind of dragging operation we need to perform (line-only
1447 // X-based zoom or rectangle-based X- and Y-based zoom, for example). The
1448 // only thing we know is that we can make the text invisible.
1449
1450 // Same for the x delta text item
1451 mp_xDeltaTextItem->setVisible(false);
1452 mp_yDeltaTextItem->setVisible(false);
1453
1454 // We do not show the end vertical region range marker.
1455 mp_vEndTracerItem->setVisible(false);
1456
1457 // Horizontal position tracer.
1458 mp_hPosTracerItem->setVisible(true);
1459 mp_hPosTracerItem->start->setCoords(xAxis->range().lower,
1460 m_context.m_lastCursorHoveredPoint.y());
1461 mp_hPosTracerItem->end->setCoords(xAxis->range().upper,
1462 m_context.m_lastCursorHoveredPoint.y());
1463
1464 // Vertical position tracer.
1465 mp_vPosTracerItem->setVisible(true);
1466
1467 mp_vPosTracerItem->setVisible(true);
1468 mp_vPosTracerItem->start->setCoords(m_context.m_lastCursorHoveredPoint.x(),
1469 yAxis->range().upper);
1470 mp_vPosTracerItem->end->setCoords(m_context.m_lastCursorHoveredPoint.x(),
1471 yAxis->range().lower);
1472
1473 // Force replot now because later that call might not be performed.
1474 replot();
1475
1476 // If we were using the "quantum" display for the rescale of the axes
1477 // using the Ctrl-modified left button click drag in the axes, then reset
1478 // the count to 0.
1480
1481 // By definition we are stopping the drag operation by releasing the mouse
1482 // button. Whatever that mouse button was pressed before and if there was
1483 // one pressed before. We cannot set that boolean value to false before
1484 // this place, because we call a number of routines above that need to know
1485 // that dragging was occurring. Like mouseReleaseHandledEvent(event) for
1486 // example.
1487
1488 m_context.m_isMouseDragging = false;
1489
1490 // Now that we have computed the useful ranges, we need to check what to do
1491 // depending on the button that was pressed.
1492
1493 if(m_context.m_lastReleasedMouseButton == Qt::LeftButton)
1494 {
1496 }
1497 else if(m_context.m_lastReleasedMouseButton == Qt::RightButton)
1498 {
1500 }
1501
1502 event->accept();
1503
1504 // Before returning, emit the signal for the user of
1505 // this class consumption.
1506 // qDebug() << "Emitting mouseReleaseEventSignal.";
1508
1509 // qDebug() << "Exiting after having emitted mouseReleaseEventSignal with base
1510 // context:"
1511 // << m_context.toString();
1512
1513 return;
1514}
1515
1516void
1518{
1519 // qDebug();
1520
1521 if(m_context.m_wasClickOnXAxis || m_context.m_wasClickOnYAxis)
1522 {
1523
1524 // When the mouse move handler pans the plot, we cannot store each axes
1525 // range history element that would mean store a huge amount of such
1526 // elements, as many element as there are mouse move event handled by
1527 // the Qt event queue. But we can store an axis range history element
1528 // for the last situation of the mouse move: when the button is
1529 // released:
1530
1532
1533 // qDebug() << "emit plotRangesChangedSignal(m_context);"
1534
1536
1537 replot();
1538
1539 // Nothing else to do.
1540 return;
1541 }
1542
1543 // There are two possibilities:
1544 //
1545 // 1. The full integration scope (four lines) were currently drawn, which
1546 // means the user was willing to perform a zoom operation.
1547 //
1548 // 2. Only the first top line was drawn, which means the user was dragging
1549 // the cursor horizontally. That might have two ends, as shown below.
1550
1551 // So, first check what is drawn of the selection polygon.
1552
1553 SelectionDrawingLines selection_drawing_lines =
1555
1556 // Now that we know what was currently drawn of the selection polygon, we
1557 // can remove it. true to reset the values to 0.
1559
1560 // Force replot now because later that call might not be performed.
1561 replot();
1562
1563 if(selection_drawing_lines == SelectionDrawingLines::FULL_POLYGON)
1564 {
1565 // qDebug() << "Yes, the full polygon was visible";
1566
1567 // If we were dragging with the left button pressed and could draw a
1568 // rectangle, then we were preparing a zoom operation. Let's bring that
1569 // operation to its accomplishment.
1570
1571 axisZoom();
1572
1573 return;
1574 }
1575 else if(selection_drawing_lines == SelectionDrawingLines::TOP_LINE)
1576 {
1577 // qDebug() << "No, only the top line of the full polygon was visible";
1578
1579 // The user was dragging the left mouse cursor and that may mean they
1580 // were measuring a distance or willing to perform a special zoom
1581 // operation if the Ctrl key was down.
1582
1583 // If the user started by clicking in the plot region, dragged the mouse
1584 // cursor with the left button and pressed the Ctrl modifier, then that
1585 // means that they wanted to do a rescale over the x-axis in the form of
1586 // a reframing.
1587
1588 if(m_context.m_keyboardModifiers & Qt::ControlModifier)
1589 {
1590 return axisReframe();
1591 }
1592 }
1593 // else
1594 // qDebug() << "Another possibility.";
1595}
1596
1597void
1599{
1600 // qDebug();
1601 // The right button is used for the integrations. Not for axis range
1602 // operations. So all we have to do is remove the various graphics items and
1603 // send a signal with the context that contains all the data required by the
1604 // user to perform the integrations over the right plot regions.
1605
1606 // Whatever we were doing we need to make the selection line invisible:
1607
1608 if(mp_xDeltaTextItem->visible())
1609 mp_xDeltaTextItem->setVisible(false);
1610 if(mp_yDeltaTextItem->visible())
1611 mp_yDeltaTextItem->setVisible(false);
1612
1613 // Also make the vertical end tracer invisible.
1614 mp_vEndTracerItem->setVisible(false);
1615
1616 // Once the integration is asked for, then the selection rectangle if of no
1617 // more use.
1619
1620 // Force replot now because later that call might not be performed.
1621 replot();
1622
1623 // Note that we only request an integration if the x-axis delta is enough.
1624
1625 double x_delta_pixel =
1626 fabs(xAxis->coordToPixel(m_context.m_currentDragPoint.x()) -
1627 xAxis->coordToPixel(m_context.m_startDragPoint.x()));
1628
1629 if(x_delta_pixel > 3)
1630 {
1631 // qDebug() << "Emitting integrationRequestedSignal(m_context)";
1633 }
1634 // else
1635 // qDebug() << "Not asking for integration.";
1636}
1637
1638void
1639BasePlotWidget::mouseWheelHandler([[maybe_unused]] QWheelEvent *event)
1640{
1641 // We should record the new range values each time the wheel is used to
1642 // zoom/unzoom.
1643
1644 m_context.m_xRange = QCPRange(xAxis->range());
1645 m_context.m_yRange = QCPRange(yAxis->range());
1646
1647 // qDebug() << "New x range: " << m_context.m_xRange;
1648 // qDebug() << "New y range: " << m_context.m_yRange;
1649
1651
1654
1655 event->accept();
1656}
1657
1658void
1660 QCPAxis *axis,
1661 [[maybe_unused]] QCPAxis::SelectablePart part,
1662 QMouseEvent *event)
1663{
1664 // qDebug();
1665
1666 m_context.m_keyboardModifiers = QGuiApplication::queryKeyboardModifiers();
1667
1668 if(m_context.m_keyboardModifiers & Qt::ControlModifier)
1669 {
1670 // qDebug();
1671
1672 // If the Ctrl modifiers is active, then both axes are to be reset. Also
1673 // the histories are reset also.
1674
1675 rescaleAxes();
1677 }
1678 else
1679 {
1680 // qDebug();
1681
1682 // Only the axis passed as parameter is to be rescaled.
1683 // Reset the range of that axis to the max view possible.
1684
1685 axis->rescale();
1686
1688
1689 event->accept();
1690 }
1691
1692 // The double-click event does not cancel the mouse press event. That is, if
1693 // left-double-clicking, at the end of the operation the button still
1694 // "pressed". We need to remove manually the button from the pressed buttons
1695 // context member.
1696
1697 m_context.m_pressedMouseButtons ^= event->button();
1698
1700
1702
1703 replot();
1704}
1705
1706bool
1707BasePlotWidget::isClickOntoXAxis(const QPointF &mousePoint)
1708{
1709 QCPLayoutElement *layoutElement = layoutElementAt(mousePoint);
1710
1711 if(layoutElement &&
1712 layoutElement == dynamic_cast<QCPLayoutElement *>(axisRect()))
1713 {
1714 // The graph is *inside* the axisRect that is the outermost envelope of
1715 // the graph. Thus, if we want to know if the click was indeed on an
1716 // axis, we need to check what selectable part of the the axisRect we
1717 // were clicking:
1718 QCPAxis::SelectablePart selectablePart;
1719
1720 selectablePart = xAxis->getPartAt(mousePoint);
1721
1722 if(selectablePart == QCPAxis::spAxisLabel ||
1723 selectablePart == QCPAxis::spAxis ||
1724 selectablePart == QCPAxis::spTickLabels)
1725 return true;
1726 }
1727
1728 return false;
1729}
1730
1731bool
1732BasePlotWidget::isClickOntoYAxis(const QPointF &mousePoint)
1733{
1734 QCPLayoutElement *layoutElement = layoutElementAt(mousePoint);
1735
1736 if(layoutElement &&
1737 layoutElement == dynamic_cast<QCPLayoutElement *>(axisRect()))
1738 {
1739 // The graph is *inside* the axisRect that is the outermost envelope of
1740 // the graph. Thus, if we want to know if the click was indeed on an
1741 // axis, we need to check what selectable part of the the axisRect we
1742 // were clicking:
1743 QCPAxis::SelectablePart selectablePart;
1744
1745 selectablePart = yAxis->getPartAt(mousePoint);
1746
1747 if(selectablePart == QCPAxis::spAxisLabel ||
1748 selectablePart == QCPAxis::spAxis ||
1749 selectablePart == QCPAxis::spTickLabels)
1750 return true;
1751 }
1752
1753 return false;
1754}
1755
1756/// MOUSE-related EVENTS
1757
1758
1759/// MOUSE MOVEMENTS mouse/keyboard-triggered
1760
1761int
1763{
1764 // The user is dragging the mouse, probably to rescale the axes, but we need
1765 // to sort out in which direction the drag is happening.
1766
1767 // This function should be called after calculateDragDeltas, so that
1768 // m_context has the proper x/y delta values that we'll compare.
1769
1770 // Note that we cannot compare simply x or y deltas because the y axis might
1771 // have a different scale that the x axis. So we first need to convert the
1772 // positions to pixels.
1773
1774 double x_delta_pixel =
1775 fabs(xAxis->coordToPixel(m_context.m_currentDragPoint.x()) -
1776 xAxis->coordToPixel(m_context.m_startDragPoint.x()));
1777
1778 double y_delta_pixel =
1779 fabs(yAxis->coordToPixel(m_context.m_currentDragPoint.y()) -
1780 yAxis->coordToPixel(m_context.m_startDragPoint.y()));
1781
1782 if(x_delta_pixel > y_delta_pixel)
1783 return Qt::Horizontal;
1784
1785 return Qt::Vertical;
1786}
1787
1788void
1790{
1791 // First convert the graph coordinates to pixel coordinates.
1792
1793 QPointF pixels_coordinates(xAxis->coordToPixel(graph_coordinates.x()),
1794 yAxis->coordToPixel(graph_coordinates.y()));
1795
1796 moveMouseCursorPixelCoordToGlobal(pixels_coordinates.toPoint());
1797}
1798
1799void
1801{
1802 // qDebug() << "Calling set pos with new cursor position.";
1803 QCursor::setPos(mapToGlobal(pixel_coordinates.toPoint()));
1804}
1805
1806void
1808{
1809 QPointF graph_coord = horizontalGetGraphCoordNewPointCountPixels(pixel_count);
1810
1811 QPointF pixel_coord(xAxis->coordToPixel(graph_coord.x()),
1812 yAxis->coordToPixel(graph_coord.y()));
1813
1814 // Now we need ton convert the new coordinates to the global position system
1815 // and to move the cursor to that new position. That will create an event to
1816 // move the mouse cursor.
1817
1818 moveMouseCursorPixelCoordToGlobal(pixel_coord.toPoint());
1819}
1820
1821QPointF
1823{
1824 QPointF pixel_coordinates(
1825 xAxis->coordToPixel(m_context.m_lastCursorHoveredPoint.x()) + pixel_count,
1826 yAxis->coordToPixel(m_context.m_lastCursorHoveredPoint.y()));
1827
1828 // Now convert back to local coordinates.
1829
1830 QPointF graph_coordinates(xAxis->pixelToCoord(pixel_coordinates.x()),
1831 yAxis->pixelToCoord(pixel_coordinates.y()));
1832
1833 return graph_coordinates;
1834}
1835
1836void
1838{
1839
1840 QPointF graph_coord = verticalGetGraphCoordNewPointCountPixels(pixel_count);
1841
1842 QPointF pixel_coord(xAxis->coordToPixel(graph_coord.x()),
1843 yAxis->coordToPixel(graph_coord.y()));
1844
1845 // Now we need ton convert the new coordinates to the global position system
1846 // and to move the cursor to that new position. That will create an event to
1847 // move the mouse cursor.
1848
1849 moveMouseCursorPixelCoordToGlobal(pixel_coord.toPoint());
1850}
1851
1852QPointF
1854{
1855 QPointF pixel_coordinates(
1856 xAxis->coordToPixel(m_context.m_lastCursorHoveredPoint.x()),
1857 yAxis->coordToPixel(m_context.m_lastCursorHoveredPoint.y()) + pixel_count);
1858
1859 // Now convert back to local coordinates.
1860
1861 QPointF graph_coordinates(xAxis->pixelToCoord(pixel_coordinates.x()),
1862 yAxis->pixelToCoord(pixel_coordinates.y()));
1863
1864 return graph_coordinates;
1865}
1866
1867/// MOUSE MOVEMENTS mouse/keyboard-triggered
1868
1869
1870/// RANGE-related functions
1871
1872QCPRange
1873BasePlotWidget::getRangeX(bool &found_range, int index) const
1874{
1875 QCPGraph *graph_p = graph(index);
1876
1877 if(graph_p == nullptr)
1878 qFatal("Programming error.");
1879
1880 return graph_p->getKeyRange(found_range);
1881}
1882
1883QCPRange
1884BasePlotWidget::getRangeY(bool &found_range, int index) const
1885{
1886 QCPGraph *graph_p = graph(index);
1887
1888 if(graph_p == nullptr)
1889 qFatal("Programming error.");
1890
1891 return graph_p->getValueRange(found_range);
1892}
1893
1894QCPRange
1896 RangeType range_type,
1897 bool &found_range) const
1898{
1899
1900 // Iterate in all the graphs in this widget and return a QCPRange that has
1901 // its lower member as the greatest lower value of all
1902 // its upper member as the smallest upper value of all
1903
1904 if(!graphCount())
1905 {
1906 found_range = false;
1907
1908 return QCPRange(0, 1);
1909 }
1910
1911 if(graphCount() == 1)
1912 return graph()->getKeyRange(found_range);
1913
1914 bool found_at_least_one_range = false;
1915
1916 // Create an invalid range.
1917 QCPRange result_range(QCPRange::minRange + 1, QCPRange::maxRange + 1);
1918
1919 for(int iter = 0; iter < graphCount(); ++iter)
1920 {
1921 QCPRange temp_range;
1922
1923 bool found_range_for_iter = false;
1924
1925 QCPGraph *graph_p = graph(iter);
1926
1927 // Depending on the axis param, select the key or value range.
1928
1929 if(axis == Enums::Axis::x)
1930 temp_range = graph_p->getKeyRange(found_range_for_iter);
1931 else if(axis == Enums::Axis::y)
1932 temp_range = graph_p->getValueRange(found_range_for_iter);
1933 else
1934 qFatal("Cannot reach this point. Programming error.");
1935
1936 // Was a range found for the iterated graph ? If not skip this
1937 // iteration.
1938
1939 if(!found_range_for_iter)
1940 continue;
1941
1942 // While the innermost_range is invalid, we need to seed it with a good
1943 // one. So check this.
1944
1945 if(!QCPRange::validRange(result_range))
1946 qFatal("The obtained range is invalid !");
1947
1948 // At this point we know the obtained range is OK.
1949 result_range = temp_range;
1950
1951 // We found at least one valid range!
1952 found_at_least_one_range = true;
1953
1954 // At this point we have two valid ranges to compare. Depending on
1955 // range_type, we need to perform distinct comparisons.
1956
1957 if(range_type == RangeType::innermost)
1958 {
1959 if(temp_range.lower > result_range.lower)
1960 result_range.lower = temp_range.lower;
1961 if(temp_range.upper < result_range.upper)
1962 result_range.upper = temp_range.upper;
1963 }
1964 else if(range_type == RangeType::outermost)
1965 {
1966 if(temp_range.lower < result_range.lower)
1967 result_range.lower = temp_range.lower;
1968 if(temp_range.upper > result_range.upper)
1969 result_range.upper = temp_range.upper;
1970 }
1971 else
1972 qFatal("Cannot reach this point. Programming error.");
1973
1974 // Continue to next graph, if any.
1975 }
1976 // End of
1977 // for(int iter = 0; iter < graphCount(); ++iter)
1978
1979 // Let the caller know if we found at least one range.
1980 found_range = found_at_least_one_range;
1981
1982 return result_range;
1983}
1984
1985QCPRange
1987{
1988
1989 return getRange(Enums::Axis::x, RangeType::innermost, found_range);
1990}
1991
1992QCPRange
1994{
1995 return getRange(Enums::Axis::x, RangeType::outermost, found_range);
1996}
1997
1998QCPRange
2000{
2001
2002 return getRange(Enums::Axis::y, RangeType::innermost, found_range);
2003}
2004
2005QCPRange
2007{
2008 return getRange(Enums::Axis::y, RangeType::outermost, found_range);
2009}
2010
2011/// RANGE-related functions
2012
2013
2014/// PLOTTING / REPLOTTING functions
2015
2016void
2018{
2019 // Get the current x lower/upper range, that is, leftmost/rightmost x
2020 // coordinate.
2021 double xLower = xAxis->range().lower;
2022 double xUpper = xAxis->range().upper;
2023
2024 // Get the current y lower/upper range, that is, bottommost/topmost y
2025 // coordinate.
2026 double yLower = yAxis->range().lower;
2027 double yUpper = yAxis->range().upper;
2028
2029 // This function is called only when the user has clicked on the x/y axis or
2030 // when the user has dragged the left mouse button with the Ctrl key
2031 // modifier. The m_context.m_wasClickOnXAxis is then simulated in the mouse
2032 // move handler. So we need to test which axis was clicked-on.
2033
2034 if(m_context.m_wasClickOnXAxis)
2035 {
2036 // We are changing the range of the X axis.
2037
2038 // If xDelta is < 0, then we were dragging from right to left, we are
2039 // compressing the view on the x axis, by adding new data to the right
2040 // hand size of the graph. So we add xDelta to the upper bound of the
2041 // range. Otherwise we are uncompressing the view on the x axis and
2042 // remove the xDelta from the upper bound of the range. This is why we
2043 // have the
2044 // '-'
2045 // and not '+' below;
2046
2047 xAxis->setRange(xLower, xUpper - m_context.m_xDelta);
2048 }
2049 // End of
2050 // if(m_context.m_wasClickOnXAxis)
2051 else // that is, if(m_context.m_wasClickOnYAxis)
2052 {
2053 // We are changing the range of the Y axis.
2054
2055 // See above for an explanation of the computation (the - sign below).
2056
2057 yAxis->setRange(yLower, yUpper - m_context.m_yDelta);
2058 }
2059 // End of
2060 // else // that is, if(m_context.m_wasClickOnYAxis)
2061
2062 // Update the context with the current axes ranges
2063
2065
2067
2068 replot();
2069}
2070
2071void
2073{
2074
2075 // double sorted_start_drag_point_x =
2076 // std::min(m_context.m_startDragPoint.x(),
2077 // m_context.m_currentDragPoint.x());
2078
2079 // xAxis->setRange(sorted_start_drag_point_x,
2080 // sorted_start_drag_point_x + fabs(m_context.m_xDelta));
2081
2082 xAxis->setRange(
2083 QCPRange(m_context.m_xRegionRangeStart, m_context.m_xRegionRangeEnd));
2084
2085 // Note that the y axis should be rescaled from current lower value to new
2086 // upper value matching the y-axis position of the cursor when the mouse
2087 // button was released.
2088
2089 yAxis->setRange(xAxis->range().lower,
2090 std::max<double>(m_context.m_yRegionRangeStart,
2091 m_context.m_yRegionRangeEnd));
2092
2093 // qDebug() << "xaxis:" << xAxis->range().lower << "-" <<
2094 // xAxis->range().upper
2095 //<< "yaxis:" << yAxis->range().lower << "-" << yAxis->range().upper;
2096
2098
2101
2102 replot();
2103}
2104
2105void
2107{
2108
2109 // Use the m_context.m_xRegionRangeStart/End values, but we need to sort the
2110 // values before using them, because now we want to really have the lower x
2111 // value. Simply craft a QCPRange that will swap the values if lower is not
2112 // < than upper QCustomPlot calls this normalization).
2113
2114 xAxis->setRange(
2115 QCPRange(m_context.m_xRegionRangeStart, m_context.m_xRegionRangeEnd));
2116
2117 yAxis->setRange(
2118 QCPRange(m_context.m_yRegionRangeStart, m_context.m_yRegionRangeEnd));
2119
2121
2124
2125 replot();
2126}
2127
2128void
2130{
2131 // Sanity check
2132 if(!m_context.m_wasClickOnXAxis && !m_context.m_wasClickOnYAxis)
2133 qFatal(
2134 "This function can only be called if the mouse click was on one of the "
2135 "axes");
2136
2137 if(m_context.m_wasClickOnXAxis)
2138 {
2139 xAxis->setRange(m_context.m_xRange.lower - m_context.m_xDelta,
2140 m_context.m_xRange.upper - m_context.m_xDelta);
2141 }
2142
2143 if(m_context.m_wasClickOnYAxis)
2144 {
2145 yAxis->setRange(m_context.m_yRange.lower - m_context.m_yDelta,
2146 m_context.m_yRange.upper - m_context.m_yDelta);
2147 }
2148
2150
2151 // qDebug() << "The updated context:" << m_context.toString();
2152
2153 // We cannot store the new ranges in the history, because the pan operation
2154 // involved a huge quantity of micro-movements elicited upon each mouse move
2155 // cursor event so we would have a huge history.
2156 // updateAxesRangeHistory();
2157
2158 // Now that the context has the right range values, we can emit the
2159 // signal that will be used by this plot widget users, typically to
2160 // abide by the x/y range lock required by the user.
2161
2163
2164 replot();
2165}
2166
2167void
2169 QCPRange yAxisRange,
2170 Enums::Axis axis)
2171{
2172 // qDebug() << "With axis:" << (int)axis;
2173
2174 if(static_cast<int>(axis) & static_cast<int>(Enums::Axis::x))
2175 {
2176 xAxis->setRange(xAxisRange.lower, xAxisRange.upper);
2177 }
2178
2179 if(static_cast<int>(axis) & static_cast<int>(Enums::Axis::y))
2180 {
2181 yAxis->setRange(yAxisRange.lower, yAxisRange.upper);
2182 }
2183
2184 // We do not want to update the history, because there would be way too
2185 // much history items, since this function is called upon mouse moving
2186 // handling and not only during mouse release events.
2187 // updateAxesRangeHistory();
2188
2189 replot();
2190}
2191
2192void
2193BasePlotWidget::replotWithAxisRangeX(double lower, double upper)
2194{
2195 // qDebug();
2196
2197 xAxis->setRange(lower, upper);
2198
2199 replot();
2200}
2201
2202void
2203BasePlotWidget::replotWithAxisRangeY(double lower, double upper)
2204{
2205 // qDebug();
2206
2207 yAxis->setRange(lower, upper);
2208
2209 replot();
2210}
2211
2212/// PLOTTING / REPLOTTING functions
2213
2214
2215/// PLOT ITEMS : TRACER TEXT ITEMS...
2216
2217//! Hide the selection line, the xDelta text and the zoom rectangle items.
2218void
2220{
2221 mp_xDeltaTextItem->setVisible(false);
2222 mp_yDeltaTextItem->setVisible(false);
2223
2224 // mp_zoomRectItem->setVisible(false);
2226
2227 // Force a replot to make sure the action is immediately visible by the
2228 // user, even without moving the mouse.
2229 replot();
2230}
2231
2232//! Show the traces (vertical and horizontal).
2233void
2235{
2237
2238 mp_vPosTracerItem->setVisible(true);
2239 mp_hPosTracerItem->setVisible(true);
2240
2241 mp_vStartTracerItem->setVisible(true);
2242 mp_vEndTracerItem->setVisible(true);
2243
2244 // Force a replot to make sure the action is immediately visible by the
2245 // user, even without moving the mouse.
2246 replot();
2247}
2248
2249//! Hide the traces (vertical and horizontal).
2250void
2252{
2254 mp_hPosTracerItem->setVisible(false);
2255 mp_vPosTracerItem->setVisible(false);
2256
2257 mp_vStartTracerItem->setVisible(false);
2258 mp_vEndTracerItem->setVisible(false);
2259
2260 // Force a replot to make sure the action is immediately visible by the
2261 // user, even without moving the mouse.
2262 replot();
2263}
2264
2265void
2267 bool for_integration)
2268{
2269 // The user has dragged the mouse left button on the graph, which means he
2270 // is willing to draw a selection rectangle, either for zooming-in or for
2271 // integration.
2272
2273 if(mp_xDeltaTextItem != nullptr)
2274 mp_xDeltaTextItem->setVisible(false);
2275 if(mp_yDeltaTextItem != nullptr)
2276 mp_yDeltaTextItem->setVisible(false);
2277
2278 // Ensure the right selection rectangle is drawn.
2279
2280 updateIntegrationScopeDrawing(as_line_segment, for_integration);
2281
2282 // Note that if we draw a zoom rectangle, then we are certainly not
2283 // measuring anything. So set the boolean value to false so that the user of
2284 // this widget or derived classes know that there is nothing to perform upon
2285 // (like deconvolution, for example).
2286
2287 m_context.m_isMeasuringDistance = false;
2288
2289 // Also remove the delta value from the pipeline by sending a simple
2290 // distance without measurement signal.
2291
2292 emit xAxisMeasurementSignal(m_context, false);
2293
2294 replot();
2295}
2296
2297void
2299{
2300 // Depending on the kind of integration scope, we will have to display
2301 // differently calculated values. We want to provide the user with
2302 // the horizontal span of the integration scope. There are different
2303 // situations.
2304
2305 // 1. The scope is mono-dimensional across the x axis: the span
2306 // is thus simply the width.
2307
2308 // 2. The scope is bi-dimensional and is a rectangle: the span is
2309 // thus simply the width.
2310
2311 // 3. The socpe is bi-dimensional and is a rhomboid: the span is
2312 // the width.
2313
2314 // In the first and second cases above, the width is equal to the
2315 // m_context.m_xDelta.
2316
2317 // In the case of the rhomboid, the span is not m_context.m_xDelta,
2318 // it is more than that if the rhomboid is horizontal because it is
2319 // the m_context.m_xDelta plus the rhomboid's horizontal size.
2320
2321 // FIXME: is this still true?
2322 //
2323 // We do not want to show the position markers because the only horiontal
2324 // line to be visible must be contained between the start and end vertical
2325 // tracer items.
2326 mp_hPosTracerItem->setVisible(false);
2327 mp_vPosTracerItem->setVisible(false);
2328
2329 // We want to draw the text in the middle position of the leftmost-rightmost
2330 // point, even with rhomboid scopes.
2331
2332 QPointF leftmost_point;
2333 if(!m_context.msp_integrationScope->getLeftMostPoint(leftmost_point))
2334 qFatal("Could not get the left-most point.");
2335
2336 double width;
2337 if(!m_context.msp_integrationScope->getWidth(width))
2338 qFatal("Could not get width.");
2339 // qDebug() << "width:" << width;
2340
2341 double x_axis_center_position = leftmost_point.x() + width / 2;
2342
2343 // We want the text to print inside the rectangle, always at the current
2344 // drag point so the eye can follow the delta value while looking where to
2345 // drag the mouse. To position the text inside the rectangle, we need to
2346 // know what is the drag direction.
2347
2348 // What is the distance between the rectangle line at current drag point and
2349 // the text itself. Think of this as a margin distance between the
2350 // point of interest and the actual position of the text.
2351 int pixels_away_from_line = 15;
2352
2353 QPointF reference_point_for_y_axis_label_position;
2354
2355 // ATTENTION: the pixel coordinates for the vertical direction go in reverse
2356 // order with respect to the y axis values !!! That is, pixel(0,0) is top
2357 // left of the graph.
2358 if(static_cast<int>(m_context.m_dragDirections) &
2359 static_cast<int>(DragDirections::BOTTOM_TO_TOP))
2360 {
2361 // We need to print outside the rectangle, that is pixels_away_from_line
2362 // pixels to the top, so with pixel y value decremented of that
2363 // pixels_above_line value (one would have expected to increment that
2364 // value, along the y axis, but the coordinates in pixel go in reverse
2365 // order).
2366
2367 pixels_away_from_line *= -1;
2368
2369 if(!m_context.msp_integrationScope->getTopMostPoint(
2370 reference_point_for_y_axis_label_position))
2371 qFatal("Failed to get top most point.");
2372 }
2373 else
2374 {
2375 if(!m_context.msp_integrationScope->getBottomMostPoint(
2376 reference_point_for_y_axis_label_position))
2377 qFatal("Failed to get bottom most point.");
2378 }
2379
2380 // double y_axis_pixel_coordinate =
2381 // yAxis->coordToPixel(m_context.m_currentDragPoint.y());
2382 double y_axis_pixel_coordinate =
2383 yAxis->coordToPixel(reference_point_for_y_axis_label_position.y());
2384
2385 // Now that we have the coordinate in pixel units, we can correct
2386 // it by the value of the margin we want to give.
2387 double y_axis_modified_pixel_coordinate =
2388 y_axis_pixel_coordinate + pixels_away_from_line;
2389
2390 // Set aside a point instance to store the pixel coordinates of the text.
2391 QPointF pixel_coordinates;
2392
2393 pixel_coordinates.setX(x_axis_center_position);
2394 pixel_coordinates.setY(y_axis_modified_pixel_coordinate);
2395
2396 // Now convert back to graph coordinates.
2397 QPointF graph_coordinates(xAxis->pixelToCoord(pixel_coordinates.x()),
2398 yAxis->pixelToCoord(pixel_coordinates.y()));
2399
2400 // qDebug() << "Should print the label at point:" << graph_coordinates;
2401
2402 if(mp_xDeltaTextItem != nullptr)
2403 {
2404 mp_xDeltaTextItem->position->setCoords(x_axis_center_position,
2405 graph_coordinates.y());
2406
2407 // Dynamically set the number of decimals to ensure we can read
2408 // a meaning full delta value even if it is very very very small.
2409 // That is, allow one to read 0.00333, 0.000333, 1.333 and so on.
2410
2411 // The computation below only works properly when the passed
2412 // value is fabs() (not negative !!!).
2413
2414 int decimals = Utils::zeroDecimalsInValue(width) + 3;
2415
2416 QString label_text = QString("full x span %1 -- x drag delta %2")
2417 .arg(width, 0, 'f', decimals)
2418 .arg(fabs(m_context.m_xDelta), 0, 'f', decimals);
2419
2420 mp_xDeltaTextItem->setText(label_text);
2421
2422 mp_xDeltaTextItem->setFont(QFont(font().family(), 9));
2423 mp_xDeltaTextItem->setVisible(true);
2424 }
2425
2426 // Set the boolean to true so that derived widgets know that something is
2427 // being measured, and they can act accordingly, for example by computing
2428 // deconvolutions in a mass spectrum.
2429 m_context.m_isMeasuringDistance = true;
2430
2431 replot();
2432
2433 // Let the caller know that we were measuring something.
2435
2436 return;
2437}
2438
2439void
2441{
2442 // See drawXScopeSpanFeatures() for explanations.
2443
2444 // Check right away if there is height!
2445 double height;
2446 if(!m_context.msp_integrationScope->getHeight(height))
2447 qFatal("Could not get height.");
2448
2449 // If there is no height, we have nothing to do here.
2450 if(!height)
2451 return;
2452 // qDebug() << "height:" << height;
2453
2454 // FIXME: is this still true?
2455 //
2456 // We do not want to show the position markers because the only horiontal
2457 // line to be visible must be contained between the start and end vertical
2458 // tracer items.
2459 mp_hPosTracerItem->setVisible(false);
2460 mp_vPosTracerItem->setVisible(false);
2461
2462 // First the easy part: the vertical position: centered on the
2463 // scope Y span.
2464 QPointF bottom_most_point;
2465 if(!m_context.msp_integrationScope->getBottomMostPoint(bottom_most_point))
2466 qFatal("Could not get the bottom-most bottom point.");
2467
2468 double y_axis_center_position = bottom_most_point.y() + height / 2;
2469
2470 // We want to draw the text outside the rectangle (if normal rectangle)
2471 // at a small distance from the vertical limit of the scope at the
2472 // position of the current drag point. We need to check the horizontal
2473 // drag direction to put the text at the right place (left of
2474 // current drag point if dragging right to left, for example).
2475
2476 // What is the distance between the rectangle line at current drag point and
2477 // the text itself.
2478 int pixels_away_from_line = 15;
2479 double x_axis_coordinate;
2480 double x_axis_pixel_coordinate;
2481
2482 if(static_cast<int>(m_context.m_dragDirections) &
2483 static_cast<int>(DragDirections::RIGHT_TO_LEFT))
2484 {
2485 QPointF left_most_point;
2486
2487 if(!m_context.msp_integrationScope->getLeftMostPoint(left_most_point))
2488 qFatal("Failed to get left most point.");
2489
2490 x_axis_coordinate = left_most_point.x();
2491
2492 pixels_away_from_line *= -1;
2493 }
2494 else
2495 {
2496 QPointF right_most_point;
2497
2498 if(!m_context.msp_integrationScope->getRightMostPoint(right_most_point))
2499 qFatal("Failed to get right most point.");
2500
2501 x_axis_coordinate = right_most_point.x();
2502 }
2503 x_axis_pixel_coordinate = xAxis->coordToPixel(x_axis_coordinate);
2504
2505 double x_axis_modified_pixel_coordinate =
2506 x_axis_pixel_coordinate + pixels_away_from_line;
2507
2508 // Set aside a point instance to store the pixel coordinates of the text.
2509 QPointF pixel_coordinates;
2510
2511 pixel_coordinates.setX(x_axis_modified_pixel_coordinate);
2512 pixel_coordinates.setY(y_axis_center_position);
2513
2514 // Now convert back to graph coordinates.
2515
2516 QPointF graph_coordinates(xAxis->pixelToCoord(pixel_coordinates.x()),
2517 yAxis->pixelToCoord(pixel_coordinates.y()));
2518
2519 mp_yDeltaTextItem->position->setCoords(graph_coordinates.x(),
2520 y_axis_center_position);
2521
2522 int decimals = Utils::zeroDecimalsInValue(height) + 3;
2523
2524 QString label_text = QString("full y span %1 -- y drag delta %2")
2525 .arg(height, 0, 'f', decimals)
2526 .arg(fabs(m_context.m_yDelta), 0, 'f', decimals);
2527
2528 mp_yDeltaTextItem->setText(label_text);
2529 mp_yDeltaTextItem->setFont(QFont(font().family(), 9));
2530 mp_yDeltaTextItem->setVisible(true);
2531 mp_yDeltaTextItem->setRotation(90);
2532
2533 // Set the boolean to true so that derived widgets know that something is
2534 // being measured, and they can act accordingly, for example by computing
2535 // deconvolutions in a mass spectrum.
2536 m_context.m_isMeasuringDistance = true;
2537
2538 replot();
2539
2540 // Let the caller know that we were measuring something.
2542}
2543
2544void
2546{
2547
2548 // We compute signed differentials. If the user does not want the sign,
2549 // fabs(double) is their friend.
2550
2551 // Compute the xAxis differential:
2552
2553 m_context.m_xDelta =
2554 m_context.m_currentDragPoint.x() - m_context.m_startDragPoint.x();
2555
2556 // Same with the Y-axis range:
2557
2558 m_context.m_yDelta =
2559 m_context.m_currentDragPoint.y() - m_context.m_startDragPoint.y();
2560
2561 return;
2562}
2563
2564bool
2566{
2567 // First get the height of the plot.
2568 double plotHeight = yAxis->range().upper - yAxis->range().lower;
2569
2570 double heightDiff =
2571 fabs(m_context.m_startDragPoint.y() - m_context.m_currentDragPoint.y());
2572
2573 double heightDiffRatio = (heightDiff / plotHeight) * 100;
2574
2575 if(heightDiffRatio > 10)
2576 {
2577 return true;
2578 }
2579
2580 return false;
2581}
2582
2583void
2585{
2586
2587 // if(for_integration)
2588 // qDebug() << "for_integration:" << for_integration;
2589
2590 // By essence, the one-dimension IntegrationScope is characterized
2591 // by the left-most point and the width. Using these two data bits
2592 // it is possible to compute the x value of the right-most point.
2593
2594 double x_range_start =
2595 std::min(m_context.m_currentDragPoint.x(), m_context.m_startDragPoint.x());
2596 double x_range_end =
2597 std::max(m_context.m_currentDragPoint.x(), m_context.m_startDragPoint.x());
2598
2599 // qDebug() << "x_range_start:" << x_range_start << "-" << "x_range_end:" <<
2600 // x_range_end;
2601
2602 double y_position = m_context.m_startDragPoint.y();
2603
2604 m_context.updateIntegrationScope();
2605
2606 // Top line
2607 mp_selectionRectangeLine1->start->setCoords(
2608 QPointF(x_range_start, y_position));
2609 mp_selectionRectangeLine1->end->setCoords(QPointF(x_range_end, y_position));
2610
2611 // Only if we are drawing a selection rectangle for integration, do we set
2612 // arrow heads to the line.
2613 if(for_integration)
2614 {
2615 mp_selectionRectangeLine1->setHead(QCPLineEnding::esSpikeArrow);
2616 mp_selectionRectangeLine1->setTail(QCPLineEnding::esSpikeArrow);
2617 }
2618 else
2619 {
2620 mp_selectionRectangeLine1->setHead(QCPLineEnding::esNone);
2621 mp_selectionRectangeLine1->setTail(QCPLineEnding::esNone);
2622 }
2623 mp_selectionRectangeLine1->setVisible(true);
2624
2625 // Right line: does not exist, start and end are the same end point of the
2626 // top line.
2627 mp_selectionRectangeLine2->start->setCoords(QPointF(x_range_end, y_position));
2628 mp_selectionRectangeLine2->end->setCoords(QPointF(x_range_end, y_position));
2629 mp_selectionRectangeLine2->setVisible(false);
2630
2631 // Bottom line: identical to the top line, but invisible
2632 mp_selectionRectangeLine3->start->setCoords(
2633 QPointF(x_range_start, y_position));
2634 mp_selectionRectangeLine3->end->setCoords(QPointF(x_range_end, y_position));
2635 mp_selectionRectangeLine3->setVisible(false);
2636
2637 // Left line: does not exist: start and end are the same end point of the
2638 // top line.
2639 mp_selectionRectangeLine4->start->setCoords(QPointF(x_range_end, y_position));
2640 mp_selectionRectangeLine4->end->setCoords(QPointF(x_range_end, y_position));
2641 mp_selectionRectangeLine4->setVisible(false);
2642}
2643
2644void
2646{
2647 // qDebug();
2648
2649 // if(for_integration)
2650 // qDebug() << "for_integration:" << for_integration;
2651
2652 // We are handling a conventional rectangle. Just create four points
2653 // from top left to bottom right. But we want the top left point to be
2654 // effectively the top left point and the bottom point to be the bottom
2655 // point. So we need to try all four direction combinations, left to right
2656 // or converse versus top to bottom or converse.
2657
2658 m_context.updateIntegrationScopeRect();
2659
2660 // Now that the integration scope has been updated as a rectangle,
2661 // use these newly set data to actually draw the integration
2662 // scope lines.
2663
2664 QPointF bottom_left_point;
2665 if(!m_context.msp_integrationScope->getPoint(bottom_left_point))
2666 qFatal("Failed to get point.");
2667 // qDebug() << "Starting point is left bottom point:" << bottom_left_point;
2668
2669 double width;
2670 if(!m_context.msp_integrationScope->getWidth(width))
2671 qFatal("Failed to get width.");
2672 // qDebug() << "Width:" << width;
2673
2674 double height;
2675 if(!m_context.msp_integrationScope->getHeight(height))
2676 qFatal("Failed to get height.");
2677 // qDebug() << "Height:" << height;
2678
2679 QPointF bottom_right_point(bottom_left_point.x() + width,
2680 bottom_left_point.y());
2681 // qDebug() << "bottom_right_point:" << bottom_right_point;
2682
2683 QPointF top_right_point(bottom_left_point.x() + width,
2684 bottom_left_point.y() + height);
2685 // qDebug() << "top_right_point:" << top_right_point;
2686
2687 QPointF top_left_point(bottom_left_point.x(), bottom_left_point.y() + height);
2688
2689 // qDebug() << "top_left_point:" << top_left_point;
2690
2691 // Start by drawing the bottom line because the IntegrationScopeRect has the
2692 // left bottom point and the width and the height to fully characterize it.
2693
2694 // Bottom line (left to right)
2695 mp_selectionRectangeLine3->start->setCoords(bottom_left_point);
2696 mp_selectionRectangeLine3->end->setCoords(bottom_right_point);
2697 mp_selectionRectangeLine3->setVisible(true);
2698
2699 // Right line (bottom to top)
2700 mp_selectionRectangeLine2->start->setCoords(bottom_right_point);
2701 mp_selectionRectangeLine2->end->setCoords(top_right_point);
2702 mp_selectionRectangeLine2->setVisible(true);
2703
2704 // Top line (right to left)
2705 mp_selectionRectangeLine1->start->setCoords(top_right_point);
2706 mp_selectionRectangeLine1->end->setCoords(top_left_point);
2707 mp_selectionRectangeLine1->setVisible(true);
2708
2709 // Left line (top to bottom)
2710 mp_selectionRectangeLine4->start->setCoords(top_left_point);
2711 mp_selectionRectangeLine4->end->setCoords(bottom_left_point);
2712 mp_selectionRectangeLine4->setVisible(true);
2713
2714 // Only if we are drawing a selection rectangle for integration, do we
2715 // set arrow heads to the line.
2716 if(for_integration)
2717 {
2718 mp_selectionRectangeLine1->setHead(QCPLineEnding::esSpikeArrow);
2719 mp_selectionRectangeLine1->setTail(QCPLineEnding::esSpikeArrow);
2720 }
2721 else
2722 {
2723 mp_selectionRectangeLine1->setHead(QCPLineEnding::esNone);
2724 mp_selectionRectangeLine1->setTail(QCPLineEnding::esNone);
2725 }
2726}
2727
2728void
2730{
2731 // We are handling a rhomboid scope, that is, a rectangle that
2732 // is tilted either to the left or to the right.
2733
2734 // There are two kinds of rhomboid integration scopes: horizontal and
2735 // vertical.
2736
2737 /*
2738 * +----------+
2739 * | |
2740 * | |
2741 * | |
2742 * | |
2743 * | |
2744 * | |
2745 * | |
2746 * +----------+
2747 * ----width---
2748 */
2749
2750 // As visible here, the fixed size of the rhomboid (using the S key in the
2751 // plot widget) is the *horizontal* side (this is the plot context's
2752 // m_integrationScopeRhombWidth).
2753
2754 IntegrationScopeFeatures scope_features;
2755
2756 // Top horizontal line
2757 QPointF point_1;
2758 scope_features = m_context.msp_integrationScope->getLeftMostTopPoint(point_1);
2759
2760 // When the user rotates the horizontal rhomboid, at some point, if the
2761 // current drag point has the same y axis value as the start drag point, then
2762 // we say that the rhomboid is flattened on the x axis. In this case, we do
2763 // not draw anything as this is a purely unusable situation.
2764
2765 if(scope_features & IntegrationScopeFeatures::FLAT_ON_X_AXIS)
2766 {
2767 // qDebug() << "The horizontal rhomboid is flattened on the x axis.";
2768
2769 mp_selectionRectangeLine1->setVisible(false);
2770 mp_selectionRectangeLine2->setVisible(false);
2771 mp_selectionRectangeLine3->setVisible(false);
2772 mp_selectionRectangeLine4->setVisible(false);
2773
2774 return;
2775 }
2776
2778 qFatal("The rhomboid should be horizontal!");
2779
2780 // At this point we can draw the rhomboid fine.
2781
2782 if(!m_context.msp_integrationScope->getLeftMostTopPoint(point_1))
2783 qFatal("Failed to getLeftMostTopPoint.");
2784 QPointF point_2;
2785 if(!m_context.msp_integrationScope->getRightMostTopPoint(point_2))
2786 qFatal("Failed to getRightMostTopPoint.");
2787
2788 // qDebug() << "For top line, two points:" << point_1 << "--" << point_2;
2789
2790 mp_selectionRectangeLine1->start->setCoords(point_1);
2791 mp_selectionRectangeLine1->end->setCoords(point_2);
2792
2793 // Only if we are drawing a selection rectangle for integration, do we set
2794 // arrow heads to the line.
2795 if(for_integration)
2796 {
2797 mp_selectionRectangeLine1->setHead(QCPLineEnding::esSpikeArrow);
2798 mp_selectionRectangeLine1->setTail(QCPLineEnding::esSpikeArrow);
2799 }
2800 else
2801 {
2802 mp_selectionRectangeLine1->setHead(QCPLineEnding::esNone);
2803 mp_selectionRectangeLine1->setTail(QCPLineEnding::esNone);
2804 }
2805
2806 mp_selectionRectangeLine1->setVisible(true);
2807
2808 // Right line
2809 if(!m_context.msp_integrationScope->getRightMostBottomPoint(point_1))
2810 qFatal("Failed to getRightMostBottomPoint.");
2811 mp_selectionRectangeLine2->start->setCoords(point_2);
2812 mp_selectionRectangeLine2->end->setCoords(point_1);
2813 mp_selectionRectangeLine2->setVisible(true);
2814
2815 // qDebug() << "For right line, two points:" << point_2 << "--" << point_1;
2816
2817 // Bottom horizontal line
2818 if(!m_context.msp_integrationScope->getLeftMostBottomPoint(point_2))
2819 qFatal("Failed to getLeftMostBottomPoint.");
2820 mp_selectionRectangeLine3->start->setCoords(point_1);
2821 mp_selectionRectangeLine3->end->setCoords(point_2);
2822 mp_selectionRectangeLine3->setVisible(true);
2823
2824 // qDebug() << "For bottom line, two points:" << point_1 << "--" << point_2;
2825
2826 // Left line
2827 if(!m_context.msp_integrationScope->getLeftMostTopPoint(point_1))
2828 qFatal("Failed to getLeftMostTopPoint.");
2829 mp_selectionRectangeLine4->end->setCoords(point_2);
2830 mp_selectionRectangeLine4->start->setCoords(point_1);
2831 mp_selectionRectangeLine4->setVisible(true);
2832
2833 // qDebug() << "For left line, two points:" << point_2 << "--" << point_1;
2834}
2835
2836void
2838{
2839 // We are handling a rhomboid scope, that is, a rectangle that
2840 // is tilted either to the left or to the right.
2841
2842 // There are two kinds of rhomboid integration scopes: horizontal and
2843 // vertical.
2844
2845 /*
2846 * +3
2847 * . |
2848 * . |
2849 * . |
2850 * . +2
2851 * . .
2852 * . .
2853 * . .
2854 * 4+ .
2855 * | | .
2856 * height | | .
2857 * | | .
2858 * 1+
2859 *
2860 */
2861
2862 // As visible here, the fixed size of the rhomboid (using the S key in the
2863 // plot widget) is the *vertical* side (this is the plot context's
2864 // m_integrationScopeRhombHeight).
2865
2866 IntegrationScopeFeatures scope_features;
2867
2868 // Left vertical line
2869 QPointF point_1;
2870 scope_features = m_context.msp_integrationScope->getLeftMostTopPoint(point_1);
2871
2872 // When the user rotates the vertical rhomboid, at some point, if the current
2873 // drag point is on the same x axis value as the start drag point, then we say
2874 // that the rhomboid is flattened on the y axis. In this case, we do not draw
2875 // anything as this is a purely unusable situation.
2876
2877 if(scope_features & IntegrationScopeFeatures::FLAT_ON_Y_AXIS)
2878 {
2879 // qDebug() << "The vertical rhomboid is flattened on the y axis.";
2880
2881 mp_selectionRectangeLine1->setVisible(false);
2882 mp_selectionRectangeLine2->setVisible(false);
2883 mp_selectionRectangeLine3->setVisible(false);
2884 mp_selectionRectangeLine4->setVisible(false);
2885
2886 return;
2887 }
2888
2890 qFatal("The rhomboid should be vertical!");
2891
2892 // At this point we can draw the rhomboid fine.
2893
2894 QPointF point_2;
2895 if(!m_context.msp_integrationScope->getLeftMostBottomPoint(point_2))
2896 qFatal("Failed to getLeftMostBottomPoint.");
2897
2898 // qDebug() << "For left vertical line, two points:" << point_1 << "--"
2899 // << point_2;
2900
2901 mp_selectionRectangeLine1->start->setCoords(point_1);
2902 mp_selectionRectangeLine1->end->setCoords(point_2);
2903
2904 // Only if we are drawing a selection rectangle for integration, do we set
2905 // arrow heads to the line.
2906 if(for_integration)
2907 {
2908 mp_selectionRectangeLine1->setHead(QCPLineEnding::esSpikeArrow);
2909 mp_selectionRectangeLine1->setTail(QCPLineEnding::esSpikeArrow);
2910 }
2911 else
2912 {
2913 mp_selectionRectangeLine1->setHead(QCPLineEnding::esNone);
2914 mp_selectionRectangeLine1->setTail(QCPLineEnding::esNone);
2915 }
2916
2917 mp_selectionRectangeLine1->setVisible(true);
2918
2919 // Lower oblique line
2920 if(!m_context.msp_integrationScope->getRightMostBottomPoint(point_1))
2921 qFatal("Failed to getRightMostBottomPoint.");
2922 mp_selectionRectangeLine2->start->setCoords(point_2);
2923 mp_selectionRectangeLine2->end->setCoords(point_1);
2924 mp_selectionRectangeLine2->setVisible(true);
2925
2926 // qDebug() << "For lower oblique line, two points:" << point_2 << "--"
2927 // << point_1;
2928
2929 // Right vertical line
2930 if(!m_context.msp_integrationScope->getRightMostTopPoint(point_2))
2931 qFatal("Failed to getRightMostTopPoint.");
2932 mp_selectionRectangeLine3->start->setCoords(point_1);
2933 mp_selectionRectangeLine3->end->setCoords(point_2);
2934 mp_selectionRectangeLine3->setVisible(true);
2935
2936 // qDebug() << "For right vertical line, two points:" << point_1 << "--"
2937 // << point_2;
2938
2939 // Upper oblique line
2940 if(!m_context.msp_integrationScope->getLeftMostTopPoint(point_1))
2941 qFatal("Failed to get the LeftMostTopPoint.");
2942 mp_selectionRectangeLine4->end->setCoords(point_2);
2943 mp_selectionRectangeLine4->start->setCoords(point_1);
2944 mp_selectionRectangeLine4->setVisible(true);
2945
2946 // qDebug() << "For upper oblique line, two points:" << point_2 << "--"
2947 // << point_1;
2948}
2949
2950void
2952{
2953 // qDebug();
2954
2955 // if(for_integration)
2956 // qDebug() << "for_integration:" << for_integration;
2957
2958 // We are handling a skewed rectangle (rhomboid), that is a rectangle that
2959 // is tilted either to the left or to the right.
2960
2961 // There are two kinds of rhomboid integration scopes:
2962
2963 /*
2964 4+----------+3
2965 | |
2966 | |
2967 | |
2968 | |
2969 | |
2970 | |
2971 | |
2972 1+----------+2
2973 ----width---
2974 */
2975
2976 // As visible here, the fixed size of the rhomboid (using the S key in the
2977 // plot widget) is the *horizontal* side (this is the plot context's
2978 // m_integrationScopeRhombWidth).
2979
2980 // and
2981
2982
2983 /*
2984 * +3
2985 * . |
2986 * . |
2987 * . |
2988 * . +2
2989 * . .
2990 * . .
2991 * . .
2992 * 4+ .
2993 * | | .
2994 * height | | .
2995 * | | .
2996 * 1+
2997 *
2998 */
2999
3000 // As visible here, the fixed size of the rhomboid (using the S key in the
3001 // plot widget) is the *vertical* side (this is the plot context's
3002 // m_integrationScopeRhombHeight).
3003
3004 // qDebug() << "Before calling updateIntegrationScopeRhomb(), "
3005 // "m_integrationScopeRhombWidth:"
3006 // << m_context.m_integrationScopeRhombWidth
3007 // << "and m_integrationScopeRhombHeight:"
3008 // << m_context.m_integrationScopeRhombHeight;
3009
3010 m_context.updateIntegrationScopeRhomb();
3011
3012 // qDebug() << "After, m_integrationScopeRhombWidth:"
3013 // << m_context.m_integrationScopeRhombWidth
3014 // << "and m_integrationScopeRhombHeight:"
3015 // << m_context.m_integrationScopeRhombHeight;
3016
3017 // Now that the integration scope has been updated as a rhomboid,
3018 // use these newly set data to actually draw the integration
3019 // scope lines.
3020
3021 // We thus need to first establish if we have a horiontal or a vertical
3022 // rhomboid scope. This information is located in
3023 // m_context.m_integrationScopeRhombWidth and
3024 // m_context.m_integrationScopeRhombHeight. If width > 0, height *has to be
3025 // 0*, which indicates a horizontal rhomb.Conversely, if height is > 0, then
3026 // the rhomb is vertical.
3027
3028 if(m_context.m_integrationScopeRhombWidth > 0)
3029 // We are dealing with a horizontal scope.
3031 else if(m_context.m_integrationScopeRhombHeight > 0)
3032 // We are dealing with a vertical scope.
3033 updateIntegrationScopeVerticalRhomb(for_integration);
3034 else
3035 qFatal("Cannot be both the width or height of rhomboid scope be 0.");
3036}
3037
3038void
3040 bool for_integration)
3041{
3042 // qDebug() << "as_line_segment:" << as_line_segment;
3043 // qDebug() << "for_integration:" << for_integration;
3044
3045 // We now need to construct the selection rectangle, either for zoom or for
3046 // integration.
3047
3048 // There are two situations :
3049 //
3050 // 1. if the rectangle should look like a line segment
3051 //
3052 // 2. if the rectangle should actually look like a rectangle. In this case,
3053 // there are two sub-situations:
3054 //
3055 // a. if the Alt modifier key is down, then the rectangle is rhomboid.
3056 //
3057 // b. otherwise the rectangle is conventional.
3058
3059 if(as_line_segment)
3060 {
3061 // qDebug() << "Updating the integration scope to an IntegrationScope.";
3062 updateIntegrationScope(for_integration);
3063 }
3064 else
3065 {
3066 if(!(m_context.m_keyboardModifiers & Qt::AltModifier))
3067 {
3068 // qDebug()
3069 // << "Updating the integration scope to an IntegrationScopeRect.";
3070 updateIntegrationScopeRect(for_integration);
3071 }
3072 else if(m_context.m_keyboardModifiers & Qt::AltModifier)
3073 {
3074 // The user might use the Alt modifier, but if no rhomboid side has
3075 // been defined using the S key, then we do not do any rhomboid
3076 // selection because we do not know the side size of that rhomboid.
3077
3078 if(!m_context.m_integrationScopeRhombHeight &&
3079 !m_context.m_integrationScopeRhombWidth)
3080 updateIntegrationScopeRect(for_integration);
3081 else
3082 // qDebug()
3083 // << "Updating the integration scope to an
3084 // IntegrationScopeRhomb.";
3085 updateIntegrationScopeRhomb(for_integration);
3086 }
3087 }
3088
3089 // Depending on the kind of IntegrationScope, (normal, rect or rhomb)
3090 // we have to measure things in different ways. We now set in the context
3091 // a number of parameters that will be used by its user.
3092
3093 QPointF point;
3094 double height;
3095 std::vector<QPointF> points;
3096
3097 // Integration scope values are sorted:
3098 // Line scope: point is left and width is right.x - left.x
3099 // Rect scope: point is bottom left.
3100 // Rhomb scope: points 1->4 are bottom left->bottom right->top right->top left
3101 // width is 2.x - 1.x.
3102
3103 if(m_context.msp_integrationScope->getPoints(points))
3104 {
3105 // We have defined a IntegrationScopeRhomb.
3106
3107 if(!m_context.msp_integrationScope->getLeftMostPoint(point))
3108 qFatal("Failed to get LeftMost point.");
3109 m_context.m_xRegionRangeStart = point.x();
3110
3111 if(!m_context.msp_integrationScope->getRightMostPoint(point))
3112 qFatal("Failed to get RightMost point.");
3113 m_context.m_xRegionRangeEnd = point.x();
3114 }
3115 else if(m_context.msp_integrationScope->getHeight(height))
3116 {
3117 // We have defined a IntegrationScopeRect.
3118
3119 if(!m_context.msp_integrationScope->getPoint(point))
3120 qFatal("Failed to get point.");
3121 m_context.m_xRegionRangeStart = point.x();
3122
3123 double width;
3124
3125 if(!m_context.msp_integrationScope->getWidth(width))
3126 qFatal("Failed to get width.");
3127
3128 m_context.m_xRegionRangeEnd = m_context.m_xRegionRangeStart + width;
3129
3130 m_context.m_yRegionRangeStart = point.y();
3131
3132 m_context.m_yRegionRangeEnd = point.y() + height;
3133 }
3134 else
3135 {
3136 // We have defined a IntegrationScope.
3137
3138 if(!m_context.msp_integrationScope->getPoint(point))
3139 qFatal("Failed to get point.");
3140 m_context.m_xRegionRangeStart = point.x();
3141
3142 double width;
3143
3144 if(!m_context.msp_integrationScope->getWidth(width))
3145 qFatal("Failed to get width.");
3146 m_context.m_xRegionRangeEnd = m_context.m_xRegionRangeStart + width;
3147 }
3148
3149 // At this point, draw the text describing the widths.
3150
3151 // We want the x-delta on the bottom of the rectangle, inside it
3152 // and the y-delta on the vertical side of the rectangle, inside it.
3153
3154 // Draw the selection width text
3156}
3157
3158void
3160{
3161 mp_selectionRectangeLine1->setVisible(false);
3162 mp_selectionRectangeLine2->setVisible(false);
3163 mp_selectionRectangeLine3->setVisible(false);
3164 mp_selectionRectangeLine4->setVisible(false);
3165
3166 if(reset_values)
3167 {
3169 }
3170}
3171
3172void
3174{
3175 std::const_pointer_cast<IntegrationScopeBase>(m_context.msp_integrationScope)
3176 ->reset();
3177}
3178
3181{
3182 // There are four lines that make the selection polygon. We want to know
3183 // which lines are visible.
3184
3185 int current_selection_polygon =
3186 static_cast<int>(SelectionDrawingLines::NOT_SET);
3187
3188 if(mp_selectionRectangeLine1->visible())
3189 {
3190 current_selection_polygon |=
3191 static_cast<int>(SelectionDrawingLines::TOP_LINE);
3192 // qDebug() << "current_selection_polygon:" <<
3193 // current_selection_polygon;
3194 }
3195 if(mp_selectionRectangeLine2->visible())
3196 {
3197 current_selection_polygon |=
3198 static_cast<int>(SelectionDrawingLines::RIGHT_LINE);
3199 // qDebug() << "current_selection_polygon:" <<
3200 // current_selection_polygon;
3201 }
3202 if(mp_selectionRectangeLine3->visible())
3203 {
3204 current_selection_polygon |=
3205 static_cast<int>(SelectionDrawingLines::BOTTOM_LINE);
3206 // qDebug() << "current_selection_polygon:" <<
3207 // current_selection_polygon;
3208 }
3209 if(mp_selectionRectangeLine4->visible())
3210 {
3211 current_selection_polygon |=
3212 static_cast<int>(SelectionDrawingLines::LEFT_LINE);
3213 // qDebug() << "current_selection_polygon:" <<
3214 // current_selection_polygon;
3215 }
3216
3217 // qDebug() << "returning visibility:" << current_selection_polygon;
3218
3219 return static_cast<SelectionDrawingLines>(current_selection_polygon);
3220}
3221
3222bool
3224{
3225 // Sanity check
3226 int check = 0;
3227
3228 check += mp_selectionRectangeLine1->visible();
3229 check += mp_selectionRectangeLine2->visible();
3230 check += mp_selectionRectangeLine3->visible();
3231 check += mp_selectionRectangeLine4->visible();
3232
3233 if(check > 0)
3234 return true;
3235
3236 return false;
3237}
3238
3239void
3241{
3242 // qDebug() << "Setting focus to the QCustomPlot:" << this;
3243
3244 QCustomPlot::setFocus();
3245
3246 // qDebug() << "Emitting setFocusSignal().";
3247
3248 emit setFocusSignal();
3249}
3250
3251//! Redraw the background of the \p focusedPlotWidget plot widget.
3252void
3253BasePlotWidget::redrawPlotBackground(QWidget *focusedPlotWidget)
3254{
3255 if(focusedPlotWidget == nullptr)
3257 "baseplotwidget.cpp @ redrawPlotBackground(QWidget *focusedPlotWidget "
3258 "-- "
3259 "ERROR focusedPlotWidget cannot be nullptr.");
3260
3261 if(dynamic_cast<QWidget *>(this) != focusedPlotWidget)
3262 {
3263 // The focused widget is not *this widget. We should make sure that
3264 // we were not the one that had the focus, because in this case we
3265 // need to redraw an unfocused background.
3266
3267 axisRect()->setBackground(m_unfocusedBrush);
3268 }
3269 else
3270 {
3271 axisRect()->setBackground(m_focusedBrush);
3272 }
3273
3274 replot();
3275}
3276
3277void
3279{
3280 m_context.m_xRange = QCPRange(xAxis->range().lower, xAxis->range().upper);
3281 m_context.m_yRange = QCPRange(yAxis->range().lower, yAxis->range().upper);
3282
3283 // qDebug() << "The new updated context: " << m_context.toString();
3284}
3285
3286const BasePlotContext &
3288{
3289 return m_context;
3290}
3291
3292
3293} // namespace pappso
int basePlotContextPtrMetaTypeId
int basePlotContextMetaTypeId
virtual void updateIntegrationScopeRect(bool for_integration=false)
int m_mouseMoveHandlerSkipAmount
How many mouse move events must be skipped *‍/.
std::size_t m_lastAxisRangeHistoryIndex
Index of the last axis range history item.
virtual void replotWithAxesRanges(QCPRange xAxisRange, QCPRange yAxisRange, Enums::Axis axis)
virtual void updateAxesRangeHistory()
Create new axis range history items and append them to the history.
virtual void mouseWheelHandler(QWheelEvent *event)
bool m_shouldTracersBeVisible
Tells if the tracers should be visible.
virtual void hideSelectionRectangle(bool reset_values=false)
virtual void mouseMoveHandlerDraggingCursor()
virtual void updateIntegrationScopeDrawing(bool as_line_segment=false, bool for_integration=false)
virtual void directionKeyReleaseEvent(QKeyEvent *event)
QCPItemText * mp_yDeltaTextItem
QCPItemLine * mp_selectionRectangeLine1
Rectangle defining the borders of zoomed-in/out data.
virtual QCPRange getOutermostRangeX(bool &found_range) const
void lastCursorHoveredPointSignal(const QPointF &pointf)
void plottableDestructionRequestedSignal(BasePlotWidget *base_plot_widget_p, QCPAbstractPlottable *plottable_p, const BasePlotContext &context)
virtual const BasePlotContext & getContext() const
virtual void drawSelectionRectangleAndPrepareZoom(bool as_line_segment=false, bool for_integration=false)
virtual QCPRange getRangeY(bool &found_range, int index) const
virtual void keyPressEvent(QKeyEvent *event)
KEYBOARD-related EVENTS.
virtual ~BasePlotWidget()
Destruct this BasePlotWidget instance.
virtual void updateIntegrationScope(bool for_integration=false)
QCPItemLine * mp_selectionRectangeLine2
QCPItemText * mp_xDeltaTextItem
Text describing the x-axis delta value during a drag operation.
void mousePressEventSignal(const BasePlotContext &context)
virtual void setAxisLabelX(const QString &label)
virtual void mouseMoveHandlerLeftButtonDraggingCursor()
int m_mouseMoveHandlerSkipCount
Counter to handle the "fat data" mouse move event handling.
virtual QCPRange getOutermostRangeY(bool &found_range) const
int dragDirection()
MOUSE-related EVENTS.
bool isClickOntoYAxis(const QPointF &mousePoint)
virtual void moveMouseCursorPixelCoordToGlobal(QPointF local_coordinates)
QCPItemLine * mp_hPosTracerItem
Horizontal position tracer.
QCPItemLine * mp_vPosTracerItem
Vertical position tracer.
virtual void setPen(const QPen &pen)
virtual void mouseReleaseHandlerRightButton()
virtual QCPRange getInnermostRangeX(bool &found_range) const
virtual void mouseMoveHandlerNotDraggingCursor()
virtual void redrawPlotBackground(QWidget *focusedPlotWidget)
Redraw the background of the focusedPlotWidget plot widget.
bool isClickOntoXAxis(const QPointF &mousePoint)
virtual void setAxisLabelY(const QString &label)
virtual void restoreAxesRangeHistory(std::size_t index)
Get the axis histories at index index and update the plot ranges.
virtual void drawXScopeSpanFeatures()
virtual void spaceKeyReleaseEvent(QKeyEvent *event)
virtual void replotWithAxisRangeX(double lower, double upper)
virtual void createAllAncillaryItems()
virtual QColor getPlottingColor(QCPAbstractPlottable *plottable_p) const
virtual void mouseReleaseHandlerLeftButton()
QBrush m_focusedBrush
Color used for the background of focused plot.
QPen m_pen
Pen used to draw the graph and textual elements in the plot widget.
virtual bool isSelectionRectangleVisible()
virtual bool isVerticalDisplacementAboveThreshold()
virtual void mousePressHandler(QMouseEvent *event)
KEYBOARD-related EVENTS.
virtual void verticalMoveMouseCursorCountPixels(int pixel_count)
virtual void updateIntegrationScopeRhomb(bool for_integration=false)
void mouseWheelEventSignal(const BasePlotContext &context)
virtual void resetAxesRangeHistory()
virtual SelectionDrawingLines whatIsVisibleOfTheSelectionRectangle()
virtual void showTracers()
Show the traces (vertical and horizontal).
virtual QPointF horizontalGetGraphCoordNewPointCountPixels(int pixel_count)
QCPItemLine * mp_selectionRectangeLine4
virtual void horizontalMoveMouseCursorCountPixels(int pixel_count)
BasePlotWidget(QWidget *parent)
std::vector< QCPRange * > m_yAxisRangeHistory
List of y axis ranges occurring during the panning zooming actions.
virtual QCPRange getInnermostRangeY(bool &found_range) const
void mouseReleaseEventSignal(const BasePlotContext &context)
virtual void setFocus()
PLOT ITEMS : TRACER TEXT ITEMS...
virtual void drawYScopeSpanFeatures()
void keyReleaseEventSignal(const BasePlotContext &context)
virtual const QPen & getPen() const
virtual void updateContextXandYAxisRanges()
virtual void updateIntegrationScopeHorizontalRhomb(bool for_integration=false)
virtual void mousePseudoButtonKeyPressEvent(QKeyEvent *event)
virtual void setPlottingColor(QCPAbstractPlottable *plottable_p, const QColor &new_color)
virtual void calculateDragDeltas()
QCPRange getRange(Enums::Axis axis, RangeType range_type, bool &found_range) const
virtual QPointF verticalGetGraphCoordNewPointCountPixels(int pixel_count)
void plotRangesChangedSignal(const BasePlotContext &context)
QCPItemLine * mp_vStartTracerItem
Vertical selection start tracer (typically in green).
virtual void mouseReleaseHandler(QMouseEvent *event)
QBrush m_unfocusedBrush
Color used for the background of unfocused plot.
virtual void axisRescale()
RANGE-related functions.
virtual void moveMouseCursorGraphCoordToGlobal(QPointF plot_coordinates)
virtual QString allLayerNamesToString() const
QCPItemLine * mp_selectionRectangeLine3
virtual void axisDoubleClickHandler(QCPAxis *axis, QCPAxis::SelectablePart part, QMouseEvent *event)
virtual void mouseMoveHandlerRightButtonDraggingCursor()
QCPItemLine * mp_vEndTracerItem
Vertical selection end tracer (typically in red).
virtual void mouseMoveHandler(QMouseEvent *event)
KEYBOARD-related EVENTS.
virtual void directionKeyPressEvent(QKeyEvent *event)
virtual QString layerableLayerName(QCPLayerable *layerable_p) const
virtual void keyReleaseEvent(QKeyEvent *event)
Handle specific key codes and trigger respective actions.
virtual void resetSelectionRectangle()
virtual void restorePreviousAxesRangeHistory()
Go up one history element in the axis history.
virtual int layerableLayerIndex(QCPLayerable *layerable_p) const
void integrationRequestedSignal(const BasePlotContext &context)
void xAxisMeasurementSignal(const BasePlotContext &context, bool with_delta)
virtual void replotWithAxisRangeY(double lower, double upper)
virtual void hideTracers()
Hide the traces (vertical and horizontal).
virtual void updateIntegrationScopeVerticalRhomb(bool for_integration=false)
virtual void mousePseudoButtonKeyReleaseEvent(QKeyEvent *event)
virtual void hideAllPlotItems()
PLOTTING / REPLOTTING functions.
virtual QCPRange getRangeX(bool &found_range, int index) const
MOUSE MOVEMENTS mouse/keyboard-triggered.
std::vector< QCPRange * > m_xAxisRangeHistory
List of x axis ranges occurring during the panning zooming actions.
BasePlotContext m_context
static int zeroDecimalsInValue(pappso_double value)
Determine the number of zero decimals between the decimal point and the first non-zero decimal.
Definition utils.cpp:102
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
SelectionDrawingLines