OpenMS
Annotation1DCaret.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Chris Bielow $
32 // $Authors: Chris Bielow $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
41 
42 #include <QPainter>
43 #include <QtGui/QColor>
44 #include <QStaticText>
45 
46 #include <vector>
47 
48 namespace OpenMS
49 {
57  template <class DataPoint>
59  public Annotation1DItem
60  {
61 public:
62  typedef std::vector<DataPoint> PositionsType;
63  using PointType = DataPoint;
64 
66  Annotation1DCaret(const PositionsType& caret_positions, const QString& text, const QColor& color, const QColor& connection_line_color) :
67  Annotation1DItem(text), caret_positions_(caret_positions), position_(caret_positions[0]), color_(color), connection_line_color_(connection_line_color)
68  {
69  st_.setText(text);
70  }
71 
73  Annotation1DCaret(const Annotation1DCaret& rhs) = default;
74 
76  ~Annotation1DCaret() override = default;
77 
78  // Docu in base class
79  void ensureWithinDataRange(Plot1DCanvas* const canvas, const int layer_index) override
80  {
81  canvas->pushIntoDataRange(position_, layer_index);
82  }
83 
84 
85  // Docu in base class
86  void draw(Plot1DCanvas* const canvas, QPainter& painter, bool flipped = false) override
87  {
88  painter.save();
89 
90  painter.setPen(color_);
91  // translate mz/intensity to pixel coordinates
92  QPoint position_widget, caret_position_widget;
93 
94  auto xy_pos = canvas->getMapper().map(position_);
95  auto xy_1stcaret = canvas->getMapper().map(position_);
96  canvas->dataToWidget(xy_pos, position_widget, flipped);
97  canvas->dataToWidget(xy_1stcaret, caret_position_widget, flipped);
98 
99  // draw carets '^'
100  for (const auto& pos : caret_positions_)
101  {
102  auto xy_pos_caret = canvas->getMapper().map(pos);
103  QPoint caret;
104  canvas->dataToWidget(xy_pos_caret, caret, flipped);
105  Painter1DBase::drawCaret(caret, &painter);
106  }
107 
108  // compute bounding box of text_item on the specified painter
109  bounding_box_ = QRectF(position_widget, st_.size());
110 
111  // shift pos - annotation should be over peak or, if not possible, next to it
112  double vertical_shift = bounding_box_.height() / 2 + 5;
113  if (!flipped)
114  {
115  vertical_shift *= -1;
116  }
117 
118  bounding_box_.translate(0.0, vertical_shift);
119 
120  if (flipped && bounding_box_.bottom() > canvas->height())
121  {
122  bounding_box_.moveBottom(canvas->height());
123  bounding_box_.moveLeft(position_widget.x() + 5.0);
124  }
125  else if (!flipped && bounding_box_.top() < 0.0)
126  {
127  bounding_box_.moveTop(0.0);
128  bounding_box_.moveLeft(position_widget.x() + 5.0);
129  }
130  // keep inside canvas
131  if (bounding_box_.right() > canvas->width())
132  {
133  bounding_box_.moveRight(canvas->width());
134  }
135 
136  // draw connection line between anchor point and current position if pixel coordinates differ significantly
137  if ((position_widget - caret_position_widget).manhattanLength() > 2)
138  {
139  QPointF border_point = GUIHelpers::intersectionPoint(bounding_box_, caret_position_widget);
140  if (bounding_box_.center() != border_point)
141  {
142  painter.save();
143  painter.setPen(Qt::DashLine);
144  painter.drawLine(caret_position_widget, border_point);
145  painter.restore();
146  }
147  }
148 
149  painter.drawStaticText(bounding_box_.topLeft(), st_);
150 
151  if (selected_)
152  {
153  drawBoundingBox_(painter);
154  }
155 
156  painter.restore();
157  }
158 
159  // Docu in base class
160  void move(const PointXYType delta, const Gravitator& /*gr*/, const DimMapper<2>& dim_mapper) override
161  {
162  auto xy_before = dim_mapper.map(position_);
163  xy_before += delta;
164  dim_mapper.fromXY(xy_before, position_);
165  }
166 
169  {
170  return caret_positions_;
171  }
172 
174  void setPosition(const DataPoint& position)
175  {
176  position_ = position;
177  }
179  const DataPoint& getPosition() const
180  {
181  return position_;
182  }
183 
185  void setColor(const QColor& color)
186  {
187  color_ = color;
188  }
190  const QColor& getColor() const
191  {
192  return color_;
193  }
194 
197  void setRichText(const QString& text)
198  {
199  st_.setText(text);
200  text_ = text; // this is just to keep the base class consistent.. we don't really use text_
201  }
202 
203  // Docu in base class
204  Annotation1DItem* clone() const override
205  {
206  return new Annotation1DCaret(*this);
207  }
208 
209  protected:
213 
215  DataPoint position_;
216 
218  QColor color_;
219 
222 
224  QStaticText st_;
225  };
226 } // namespace OpenMS
227 
An annotation item which paints a set of carets on the canvas.
Definition: Annotation1DCaret.h:60
std::vector< DataPoint > PositionsType
Definition: Annotation1DCaret.h:62
const QColor & getColor() const
Returns the color of the carets.
Definition: Annotation1DCaret.h:190
Annotation1DItem * clone() const override
Creates a copy of the item on the heap and returns a pointer.
Definition: Annotation1DCaret.h:204
DataPoint PointType
Definition: Annotation1DCaret.h:63
~Annotation1DCaret() override=default
Destructor.
const DataPoint & getPosition() const
Returns the position of the annotated peak (in unit coordinates)
Definition: Annotation1DCaret.h:179
void draw(Plot1DCanvas *const canvas, QPainter &painter, bool flipped=false) override
Draws the item on painter.
Definition: Annotation1DCaret.h:86
QColor color_
The color of the label.
Definition: Annotation1DCaret.h:218
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:79
void setColor(const QColor &color)
Set the color of the carets (color of text must be set using html)
Definition: Annotation1DCaret.h:185
QStaticText st_
Holds the (rich) text.
Definition: Annotation1DCaret.h:224
void setPosition(const DataPoint &position)
Sets the position of the label (in unit coordinates)
Definition: Annotation1DCaret.h:174
Annotation1DCaret(const Annotation1DCaret &rhs)=default
Copy constructor.
void setRichText(const QString &text)
Definition: Annotation1DCaret.h:197
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:160
QColor connection_line_color_
The color of the (optional) dashed line connecting peak and label.
Definition: Annotation1DCaret.h:221
DataPoint position_
The position of the label (in unit coordinates)
Definition: Annotation1DCaret.h:215
PositionsType caret_positions_
Definition: Annotation1DCaret.h:212
Annotation1DCaret(const PositionsType &caret_positions, const QString &text, const QColor &color, const QColor &connection_line_color)
Constructor.
Definition: Annotation1DCaret.h:66
const PositionsType & getCaretPositions() const
Returns the positions of the lines (in unit coordinates)
Definition: Annotation1DCaret.h:168
An abstract class acting as an interface for the different 1D annotation items.
Definition: Annotation1DItem.h:62
QRectF bounding_box_
The current bounding box of this item on the canvas where it has last been drawn.
Definition: Annotation1DItem.h:109
QString text_
The displayed text.
Definition: Annotation1DItem.h:115
void drawBoundingBox_(QPainter &painter)
Draws the bounding_box_.
bool selected_
Determines whether this item is currently selected on the canvas.
Definition: Annotation1DItem.h:112
void fromXY(const DRange< N_DIM > &in, RangeManager< Ranges... > &output) const
Definition: DimMapper.h:735
Point map(const T &data) const
convert an OpenMS datatype (such as Feature) to an N_DIM-dimensional point
Definition: DimMapper.h:699
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:68
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:321
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:454
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:48