BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
LineViews.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Mask/LineViews.cpp
6 //! @brief Implements classes VerticalLineView and HorizontalLineView
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
18 #include <QCursor>
19 #include <QPainter>
20 #include <QStyleOptionGraphicsItem>
21 
22 namespace {
23 
24 const double mask_width = 8.0;
25 const double mask_visible_width = 3.0;
26 
27 } // namespace
28 
30  : IShape2DView(item)
31  , m_item(item)
32 {
33  setFlag(QGraphicsItem::ItemIsSelectable);
34  setFlag(QGraphicsItem::ItemIsMovable);
35  setFlag(QGraphicsItem::ItemSendsGeometryChanges);
36  setCursor(Qt::SizeHorCursor);
37 }
38 
40 {
42 }
43 
44 QPainterPath VerticalLineView::shape() const
45 {
46  QPainterPath p;
47  p.addRect(QRectF(-mask_visible_width / 2., 0.0, mask_visible_width, m_bounding_rect.height()));
48  return p;
49 }
50 
52 {
53  return m_item;
54 }
55 
57 {
58  setBlockOnProperty(true);
59  m_item->setPosX(fromSceneX(this->x()));
60  setBlockOnProperty(false);
61 }
62 
64 {
65  setX(toSceneX(m_item->posX()));
66 }
67 
69 {
70  QRectF plot_scene_rectangle = m_adaptor->viewportRectangle();
71 
72  setX(toSceneX(m_item->posX()));
73  setY(plot_scene_rectangle.top());
74 
75  m_bounding_rect = QRectF(-mask_width / 2., 0.0, mask_width, plot_scene_rectangle.height());
76  update();
77 }
78 
79 void VerticalLineView::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
80 {
81  bool mask_value = static_cast<VerticalLineItem*>(m_item)->maskValue();
82  painter->setBrush(MaskEditorHelper::getMaskBrush(mask_value));
83  painter->setPen(MaskEditorHelper::getMaskPen(mask_value));
84  painter->drawRect(
85  QRectF(-mask_visible_width / 2., 0.0, mask_visible_width, m_bounding_rect.height()));
86 
87  if (isSelected()) {
88  QPen pen;
89  pen.setStyle(Qt::DashLine);
90  painter->setPen(pen);
91  painter->setBrush(Qt::NoBrush);
92  painter->drawRect(
93  QRectF(-mask_visible_width / 2., 0.0, mask_visible_width, m_bounding_rect.height()));
94  }
95 }
96 
97 //! Allows item movement along x, prevent movement along y
98 QVariant VerticalLineView::itemChange(QGraphicsItem::GraphicsItemChange change,
99  const QVariant& value)
100 {
101  if (isSelected() && change == ItemPositionChange && scene()) {
102  QPointF newPos = value.toPointF();
103  newPos.setY(y());
104  return newPos;
105  }
106  return QGraphicsItem::itemChange(change, value);
107 }
108 
110 {
111  m_item = nullptr;
112 }
113 
114 // --------------------------------------------------------------------------------------------- //
115 
117  : IShape2DView(item)
118  , m_item(item)
119 {
120  setFlag(QGraphicsItem::ItemIsSelectable);
121  setFlag(QGraphicsItem::ItemIsMovable);
122  setFlag(QGraphicsItem::ItemSendsGeometryChanges);
123  setCursor(Qt::SizeVerCursor);
124 }
125 
127 {
129 }
130 
131 QPainterPath HorizontalLineView::shape() const
132 {
133  QPainterPath path;
134  path.addRect(
135  QRectF(0.0, -mask_visible_width / 2., m_bounding_rect.width(), mask_visible_width));
136  return path;
137 }
138 
140 {
141  return m_item;
142 }
143 
145 {
146  setBlockOnProperty(true);
147  m_item->setPosY(fromSceneY(this->y()));
148  setBlockOnProperty(false);
149 }
150 
152 {
153  setY(toSceneY(m_item->posY()));
154 }
155 
157 {
158  QRectF plot_scene_rectangle = m_adaptor->viewportRectangle();
159 
160  setX(plot_scene_rectangle.left());
161  setY(toSceneY(m_item->posY()));
162 
163  m_bounding_rect = QRectF(0.0, -mask_width / 2., plot_scene_rectangle.width(), mask_width);
164  update();
165 }
166 
167 void HorizontalLineView::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
168 {
169  bool mask_value = static_cast<HorizontalLineItem*>(m_item)->maskValue();
170  painter->setBrush(MaskEditorHelper::getMaskBrush(mask_value));
171  painter->setPen(MaskEditorHelper::getMaskPen(mask_value));
172  painter->drawRect(
173  QRectF(0.0, -mask_visible_width / 2., m_bounding_rect.width(), mask_visible_width));
174 
175  if (isSelected()) {
176  QPen pen;
177  pen.setStyle(Qt::DashLine);
178  painter->setPen(pen);
179  painter->setBrush(Qt::NoBrush);
180  painter->drawRect(
181  QRectF(0.0, -mask_visible_width / 2., m_bounding_rect.width(), mask_visible_width));
182  }
183 }
184 
185 //! Allows item movement along y, prevent movement along x
186 QVariant HorizontalLineView::itemChange(QGraphicsItem::GraphicsItemChange change,
187  const QVariant& value)
188 {
189  if (isSelected() && change == ItemPositionChange && scene()) {
190  QPointF newPos = value.toPointF();
191  newPos.setX(x());
192  return newPos;
193  }
194  return QGraphicsItem::itemChange(change, value);
195 }
196 
198 {
199  m_item = nullptr;
200 }
Defines class ColorMapSceneAdaptor.
Defines classes VerticalLineView and HorizontalLineView.
Defines MaskItems classes.
void setPosY(double pos_y)
Definition: MaskItems.cpp:369
DoubleDescriptor posY() const
Definition: MaskItems.cpp:364
HorizontalLineView(HorizontalLineItem *item)
Definition: LineViews.cpp:116
void onPropertyChange() override
Definition: LineViews.cpp:151
void update_view() override
Definition: LineViews.cpp:156
HorizontalLineItem * m_item
Definition: LineViews.h:79
void onChangedY() override
Definition: LineViews.cpp:144
QPainterPath shape() const override
Definition: LineViews.cpp:131
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
Allows item movement along y, prevent movement along x.
Definition: LineViews.cpp:186
SessionItem * parameterizedItem() const override
Definition: LineViews.cpp:139
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
Definition: LineViews.cpp:167
~HorizontalLineView() override
Definition: LineViews.cpp:126
void onItemDestroyed() override
Definition: LineViews.cpp:197
virtual const QRectF & viewportRectangle() const
Returns viewport rectangle in scene coordinates.
Definition: ISceneAdaptor.h:39
Main interface class for views representing MaskItems, Projections on graphics scene.
Definition: IShape2DView.h:27
const ISceneAdaptor * m_adaptor
Definition: IShape2DView.h:67
qreal toSceneX(qreal value) const
qreal fromSceneY(qreal value) const
void setBlockOnProperty(bool value)
void disconnectFromItem(SessionItem *item)
qreal fromSceneX(qreal value) const
convert scene coordinates to ColorMap plot coordinates
qreal toSceneY(qreal value) const
QRectF m_bounding_rect
Definition: IShape2DView.h:68
static QBrush getMaskBrush(bool mask_value)
static QPen getMaskPen(bool mask_value)
Base class for a GUI data item.
Definition: SessionItem.h:204
void setPosX(double pos_x)
Definition: MaskItems.cpp:336
DoubleDescriptor posX() const
Definition: MaskItems.cpp:331
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
Allows item movement along x, prevent movement along y.
Definition: LineViews.cpp:98
void onItemDestroyed() override
Definition: LineViews.cpp:109
void onChangedX() override
Definition: LineViews.cpp:56
void onPropertyChange() override
Definition: LineViews.cpp:63
SessionItem * parameterizedItem() const override
Definition: LineViews.cpp:51
QPainterPath shape() const override
Definition: LineViews.cpp:44
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
Definition: LineViews.cpp:79
VerticalLineView(VerticalLineItem *item)
Definition: LineViews.cpp:29
void update_view() override
Definition: LineViews.cpp:68
~VerticalLineView() override
Definition: LineViews.cpp:39
VerticalLineItem * m_item
Definition: LineViews.h:49