OpenMS
Annotation1DCaret.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Chris Bielow $
6 // $Authors: Chris Bielow $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
15 
16 #include <QPainter>
17 #include <QtGui/QColor>
18 #include <QStaticText>
19 
20 #include <vector>
21 
22 namespace OpenMS
23 {
31  template <class DataPoint>
33  public Annotation1DItem
34  {
35 public:
36  typedef std::vector<DataPoint> PositionsType;
37  using PointType = DataPoint;
38 
40  Annotation1DCaret(const PositionsType& caret_positions, const QString& text, const QColor& color, const QColor& connection_line_color) :
41  Annotation1DItem(text), caret_positions_(caret_positions), position_(caret_positions[0]), color_(color), connection_line_color_(connection_line_color)
42  {
43  st_.setText(text);
44  }
45 
47  Annotation1DCaret(const Annotation1DCaret& rhs) = default;
48 
50  ~Annotation1DCaret() override = default;
51 
52  // Docu in base class
53  void ensureWithinDataRange(Plot1DCanvas* const canvas, const int layer_index) override
54  {
55  canvas->pushIntoDataRange(position_, layer_index);
56  }
57 
58 
59  // Docu in base class
60  void draw(Plot1DCanvas* const canvas, QPainter& painter, bool flipped = false) override
61  {
62  painter.save();
63 
64  painter.setPen(color_);
65  // translate mz/intensity to pixel coordinates
66  QPoint position_widget, caret_position_widget;
67 
68  auto xy_pos = canvas->getMapper().map(position_);
69  auto xy_1stcaret = canvas->getMapper().map(position_);
70  canvas->dataToWidget(xy_pos, position_widget, flipped);
71  canvas->dataToWidget(xy_1stcaret, caret_position_widget, flipped);
72 
73  // draw carets '^'
74  for (const auto& pos : caret_positions_)
75  {
76  auto xy_pos_caret = canvas->getMapper().map(pos);
77  QPoint caret;
78  canvas->dataToWidget(xy_pos_caret, caret, flipped);
79  Painter1DBase::drawCaret(caret, &painter);
80  }
81 
82  // compute bounding box of text_item on the specified painter
83  bounding_box_ = QRectF(position_widget, st_.size());
84 
85  // shift pos - annotation should be over peak or, if not possible, next to it
86  double vertical_shift = bounding_box_.height() / 2 + 5;
87  if (!flipped)
88  {
89  vertical_shift *= -1;
90  }
91 
92  bounding_box_.translate(0.0, vertical_shift);
93 
94  if (flipped && bounding_box_.bottom() > canvas->height())
95  {
96  bounding_box_.moveBottom(canvas->height());
97  bounding_box_.moveLeft(position_widget.x() + 5.0);
98  }
99  else if (!flipped && bounding_box_.top() < 0.0)
100  {
101  bounding_box_.moveTop(0.0);
102  bounding_box_.moveLeft(position_widget.x() + 5.0);
103  }
104  // keep inside canvas
105  if (bounding_box_.right() > canvas->width())
106  {
107  bounding_box_.moveRight(canvas->width());
108  }
109 
110  // draw connection line between anchor point and current position if pixel coordinates differ significantly
111  if ((position_widget - caret_position_widget).manhattanLength() > 2)
112  {
113  QPointF border_point = GUIHelpers::intersectionPoint(bounding_box_, caret_position_widget);
114  if (bounding_box_.center() != border_point)
115  {
116  painter.save();
117  painter.setPen(Qt::DashLine);
118  painter.drawLine(caret_position_widget, border_point);
119  painter.restore();
120  }
121  }
122 
123  painter.drawStaticText(bounding_box_.topLeft(), st_);
124 
125  if (selected_)
126  {
127  drawBoundingBox_(painter);
128  }
129 
130  painter.restore();
131  }
132 
133  // Docu in base class
134  void move(const PointXYType delta, const Gravitator& /*gr*/, const DimMapper<2>& dim_mapper) override
135  {
136  auto xy_before = dim_mapper.map(position_);
137  xy_before += delta;
138  dim_mapper.fromXY(xy_before, position_);
139  }
140 
143  {
144  return caret_positions_;
145  }
146 
148  void setPosition(const DataPoint& position)
149  {
150  position_ = position;
151  }
153  const DataPoint& getPosition() const
154  {
155  return position_;
156  }
157 
159  void setColor(const QColor& color)
160  {
161  color_ = color;
162  }
164  const QColor& getColor() const
165  {
166  return color_;
167  }
168 
171  void setRichText(const QString& text)
172  {
173  st_.setText(text);
174  text_ = text; // this is just to keep the base class consistent.. we don't really use text_
175  }
176 
177  // Docu in base class
178  Annotation1DItem* clone() const override
179  {
180  return new Annotation1DCaret(*this);
181  }
182 
183  protected:
187 
189  DataPoint position_;
190 
192  QColor color_;
193 
196 
198  QStaticText st_;
199  };
200 } // namespace OpenMS
201 
An annotation item which paints a set of carets on the canvas.
Definition: Annotation1DCaret.h:34
std::vector< DataPoint > PositionsType
Definition: Annotation1DCaret.h:36
const QColor & getColor() const
Returns the color of the carets.
Definition: Annotation1DCaret.h:164
Annotation1DItem * clone() const override
Creates a copy of the item on the heap and returns a pointer.
Definition: Annotation1DCaret.h:178
DataPoint PointType
Definition: Annotation1DCaret.h:37
~Annotation1DCaret() override=default
Destructor.
const DataPoint & getPosition() const
Returns the position of the annotated peak (in unit coordinates)
Definition: Annotation1DCaret.h:153
void draw(Plot1DCanvas *const canvas, QPainter &painter, bool flipped=false) override
Draws the item on painter.
Definition: Annotation1DCaret.h:60
QColor color_
The color of the label.
Definition: Annotation1DCaret.h:192
void ensureWithinDataRange(Plot1DCanvas *const canvas, const int layer_index) override
Ensures that the item has coordinates within the visible area of the canvas.
Definition: Annotation1DCaret.h:53
void setColor(const QColor &color)
Set the color of the carets (color of text must be set using html)
Definition: Annotation1DCaret.h:159
QStaticText st_
Holds the (rich) text.
Definition: Annotation1DCaret.h:198
void setPosition(const DataPoint &position)
Sets the position of the label (in unit coordinates)
Definition: Annotation1DCaret.h:148
Annotation1DCaret(const Annotation1DCaret &rhs)=default
Copy constructor.
void setRichText(const QString &text)
Definition: Annotation1DCaret.h:171
void move(const PointXYType delta, const Gravitator &, const DimMapper< 2 > &dim_mapper) override
Moves the item on the drawing canvas; behavior depends on item type and is implemented in the subclas...
Definition: Annotation1DCaret.h:134
QColor connection_line_color_
The color of the (optional) dashed line connecting peak and label.
Definition: Annotation1DCaret.h:195
DataPoint position_
The position of the label (in unit coordinates)
Definition: Annotation1DCaret.h:189
PositionsType caret_positions_
Definition: Annotation1DCaret.h:186
Annotation1DCaret(const PositionsType &caret_positions, const QString &text, const QColor &color, const QColor &connection_line_color)
Constructor.
Definition: Annotation1DCaret.h:40
const PositionsType & getCaretPositions() const
Returns the positions of the lines (in unit coordinates)
Definition: Annotation1DCaret.h:142
An abstract class acting as an interface for the different 1D annotation items.
Definition: Annotation1DItem.h:36
QRectF bounding_box_
The current bounding box of this item on the canvas where it has last been drawn.
Definition: Annotation1DItem.h:83
QString text_
The displayed text.
Definition: Annotation1DItem.h:89
void drawBoundingBox_(QPainter &painter)
Draws the bounding_box_.
bool selected_
Determines whether this item is currently selected on the canvas.
Definition: Annotation1DItem.h:86
void fromXY(const DRange< N_DIM > &in, RangeManager< Ranges... > &output) const
Definition: DimMapper.h:709
Point map(const T &data) const
convert an OpenMS datatype (such as Feature) to an N_DIM-dimensional point
Definition: DimMapper.h:673
Manipulates X or Y component of points in the X-Y plane, by assuming one axis (either X or Y axis) ha...
Definition: Plot1DCanvas.h:42
static void drawCaret(const QPoint &position, QPainter *painter, const int size=8)
draw a caret '^' at position, using a certain size (= width) of the caret
Canvas for visualization of one or several spectra.
Definition: Plot1DCanvas.h:295
void dataToWidget(const DPosition< 2 > &peak, QPoint &point, bool flipped=false)
For convenience - calls dataToWidget.
void pushIntoDataRange(T &data_point, const int layer_index)
Pushes a data point back into the valid data range of the current layer area. Useful for annotation i...
Definition: Plot1DCanvas.h:428
const DimMapper< 2 > & getMapper() const
Get Mapper to translate between values for axis (X/Y) and units (m/z, RT, intensity,...
QPointF intersectionPoint(const QRectF &rect, const QPointF &p)
Find the point on a rectangle where a ray/line from a point p to its center would intersect at.
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22