BornAgain  1.19.0
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/coregui/Views/MaskWidgets/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 const double mask_width = 8.0;
24 const double mask_visible_width = 3.0;
25 } // namespace
26 
28 {
29  setFlag(QGraphicsItem::ItemIsSelectable);
30  setFlag(QGraphicsItem::ItemIsMovable);
31  setFlag(QGraphicsItem::ItemSendsGeometryChanges);
32  setCursor(Qt::SizeHorCursor);
33 }
34 
36 {
37  setBlockOnProperty(true);
39  setBlockOnProperty(false);
40 }
41 
42 void VerticalLineView::onPropertyChange(const QString& propertyName)
43 {
44  if (propertyName == VerticalLineItem::P_POSX)
46 }
47 
49 {
50  QRectF plot_scene_rectangle = m_adaptor->viewportRectangle();
51 
53  setY(plot_scene_rectangle.top());
54 
55  m_bounding_rect = QRectF(-mask_width / 2., 0.0, mask_width, plot_scene_rectangle.height());
56  update();
57 }
58 
59 void VerticalLineView::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
60 {
61  bool mask_value = m_item->getItemValue(MaskItem::P_MASK_VALUE).toBool();
62  painter->setBrush(MaskEditorHelper::getMaskBrush(mask_value));
63  painter->setPen(MaskEditorHelper::getMaskPen(mask_value));
64  painter->drawRect(
65  QRectF(-mask_visible_width / 2., 0.0, mask_visible_width, m_bounding_rect.height()));
66 
67  if (isSelected()) {
68  QPen pen;
69  pen.setStyle(Qt::DashLine);
70  painter->setPen(pen);
71  painter->setBrush(Qt::NoBrush);
72  painter->drawRect(
73  QRectF(-mask_visible_width / 2., 0.0, mask_visible_width, m_bounding_rect.height()));
74  }
75 }
76 
77 //! Allows item movement along x, prevent movement along y
78 QVariant VerticalLineView::itemChange(QGraphicsItem::GraphicsItemChange change,
79  const QVariant& value)
80 {
81  if (isSelected() && change == ItemPositionChange && scene()) {
82  QPointF newPos = value.toPointF();
83  newPos.setY(y());
84  return newPos;
85  }
86  return QGraphicsItem::itemChange(change, value);
87 }
88 
89 // --------------------------------------------------------------------------------------------- //
90 
92 {
93  setFlag(QGraphicsItem::ItemIsSelectable);
94  setFlag(QGraphicsItem::ItemIsMovable);
95  setFlag(QGraphicsItem::ItemSendsGeometryChanges);
96  setCursor(Qt::SizeVerCursor);
97 }
98 
100 {
101  setBlockOnProperty(true);
103  setBlockOnProperty(false);
104 }
105 
106 void HorizontalLineView::onPropertyChange(const QString& propertyName)
107 {
108  if (propertyName == HorizontalLineItem::P_POSY)
110 }
111 
113 {
114  QRectF plot_scene_rectangle = m_adaptor->viewportRectangle();
115 
116  setX(plot_scene_rectangle.left());
118 
119  m_bounding_rect = QRectF(0.0, -mask_width / 2., plot_scene_rectangle.width(), mask_width);
120  update();
121 }
122 
123 void HorizontalLineView::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
124 {
125  bool mask_value = m_item->getItemValue(MaskItem::P_MASK_VALUE).toBool();
126  painter->setBrush(MaskEditorHelper::getMaskBrush(mask_value));
127  painter->setPen(MaskEditorHelper::getMaskPen(mask_value));
128  painter->drawRect(
129  QRectF(0.0, -mask_visible_width / 2., m_bounding_rect.width(), mask_visible_width));
130 
131  if (isSelected()) {
132  QPen pen;
133  pen.setStyle(Qt::DashLine);
134  painter->setPen(pen);
135  painter->setBrush(Qt::NoBrush);
136  painter->drawRect(
137  QRectF(0.0, -mask_visible_width / 2., m_bounding_rect.width(), mask_visible_width));
138  }
139 }
140 
141 //! Allows item movement along y, prevent movement along x
142 QVariant HorizontalLineView::itemChange(QGraphicsItem::GraphicsItemChange change,
143  const QVariant& value)
144 {
145  if (isSelected() && change == ItemPositionChange && scene()) {
146  QPointF newPos = value.toPointF();
147  newPos.setX(x());
148  return newPos;
149  }
150  return QGraphicsItem::itemChange(change, value);
151 }
Defines class ColorMapSceneAdaptor.
Defines classes VerticalLineView and HorizontalLineView.
Defines MaskItems classes.
static const QString P_POSY
Definition: MaskItems.h:81
virtual void onPropertyChange(const QString &propertyName)
Definition: LineViews.cpp:106
virtual void update_view()
Definition: LineViews.cpp:112
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
Definition: LineViews.cpp:123
virtual void onChangedY()
Definition: LineViews.cpp:99
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
Allows item movement along y, prevent movement along x.
Definition: LineViews.cpp:142
virtual const QRectF & viewportRectangle() const
returns viewport rectangle in scene coordinates
Definition: ISceneAdaptor.h:39
qreal toSceneX(const QString &property_name) const
qreal toSceneY(const QString &property_name) const
SessionItem * m_item
Definition: IShape2DView.h:65
const ISceneAdaptor * m_adaptor
Definition: IShape2DView.h:66
qreal fromSceneY(qreal value) const
void setBlockOnProperty(bool value)
qreal fromSceneX(qreal value) const
convert scene coordinates to ColorMap plot coordinates
QRectF m_bounding_rect
Definition: IShape2DView.h:67
static QBrush getMaskBrush(bool mask_value)
static QPen getMaskPen(bool mask_value)
static const QString P_MASK_VALUE
Definition: MaskItems.h:33
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
void setItemValue(const QString &tag, const QVariant &variant)
Directly set value of item under given tag.
static const QString P_POSX
Definition: MaskItems.h:73
virtual void onChangedX()
Definition: LineViews.cpp:35
virtual void update_view()
Definition: LineViews.cpp:48
virtual void onPropertyChange(const QString &propertyName)
Definition: LineViews.cpp:42
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
Definition: LineViews.cpp:59
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
Allows item movement along x, prevent movement along y.
Definition: LineViews.cpp:78