BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
EllipseView.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Mask/EllipseView.cpp
6 //! @brief Implements EllipseView class
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 
17 #include <QGraphicsSceneMouseEvent>
18 #include <QPainter>
19 
21  : RectangleBaseView(item)
22  , m_item(item)
23 {
24 }
25 
27 {
29 }
30 
32 {
33  setBlockOnProperty(true);
34  m_item->setXCenter(fromSceneX(this->x()));
35  setBlockOnProperty(false);
36 }
37 
39 {
40  setBlockOnProperty(true);
41  m_item->setYCenter(fromSceneY(this->y()));
42  setBlockOnProperty(false);
43 }
44 
46 {
47  update_view();
48  setX(toSceneX(m_item->xCenter()));
49  setY(toSceneY(m_item->yCenter()));
50  setTransform(QTransform().rotate(-1.0 * m_item->angle()));
51 }
52 
53 void EllipseView::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
54 {
56  QPointF opposPos = mapFromScene(m_resize_opposite_origin);
57  qreal xmin = std::min(event->pos().x(), opposPos.x());
58  qreal xmax = std::max(event->pos().x(), opposPos.x());
59  qreal ymin = std::min(event->pos().y(), opposPos.y());
60  qreal ymax = std::max(event->pos().y(), opposPos.y());
61  qreal width = xmax - xmin;
62  qreal height = ymax - ymin;
63 
64  qreal xcenter = xmin + (xmax - xmin) / 2.;
65  qreal ycenter = ymin + (ymax - ymin) / 2.;
66 
67  QPointF center(xcenter, ycenter);
68  QPointF centerInScene = mapToScene(center);
69 
71  m_item->setXCenter(fromSceneX(centerInScene.x()));
72  m_item->setYCenter(fromSceneY(centerInScene.y()));
73 
74  m_item->setXRadius((fromSceneX(centerInScene.x() + width / 2.)
75  - fromSceneX(centerInScene.x() - width / 2.))
76  / 2.);
77  m_item->setYRadius((fromSceneY(centerInScene.y() - height / 2.)
78  - fromSceneY(centerInScene.y() + height / 2.))
79  / 2.);
81  m_item->setYCenter(fromSceneY(centerInScene.y()));
82  m_item->setYRadius((fromSceneY(centerInScene.y() - height / 2.)
83  - fromSceneY(centerInScene.y() + height / 2.))
84  / 2.);
86  m_item->setXCenter(fromSceneX(centerInScene.x()));
87  m_item->setXRadius((fromSceneX(centerInScene.x() + width / 2.)
88  - fromSceneX(centerInScene.x() - width / 2.))
89  / 2.);
90  }
91  } else {
92  IShape2DView::mouseMoveEvent(event);
93  }
94 }
95 
96 QPainterPath EllipseView::shape() const
97 {
98  QPainterPath path;
99  path.addEllipse(m_mask_rect);
100  return path;
101 }
102 
104 {
105  return m_item;
106 }
107 
108 //! updates position of view using item properties
109 
111 {
112  disconnect(this, &EllipseView::xChanged, this, &EllipseView::onChangedX);
113  disconnect(this, &EllipseView::yChanged, this, &EllipseView::onChangedY);
114 
115  setX(toSceneX(m_item->xCenter()));
116  setY(toSceneY(m_item->yCenter()));
117 
118  connect(this, &EllipseView::xChanged, this, &EllipseView::onChangedX);
119  connect(this, &EllipseView::yChanged, this, &EllipseView::onChangedY);
120 
121  if (m_item->angle() != 0.0)
122  setTransform(QTransform().rotate(-1.0 * m_item->angle()));
123 }
124 
126 {
127  return QRectF(-width() / 2., -height() / 2., width(), height());
128 }
129 
130 //! Returns the x-coordinate of the rectangle's left edge
131 
132 qreal EllipseView::left() const
133 {
134  return toSceneX(m_item->xCenter() - m_item->xRadius());
135 }
136 
137 //! Returns the x-coordinate of the rectangle's right edge
138 
139 qreal EllipseView::right() const
140 {
141  return toSceneX(m_item->xCenter() + m_item->xRadius());
142 }
143 
144 //! Returns the y-coordinate of the rectangle's top edge.
145 
146 qreal EllipseView::top() const
147 {
148  return toSceneY(m_item->yCenter() + m_item->yRadius());
149 }
150 
151 //! Returns the y-coordinate of the rectangle's bottom edge.
152 
153 qreal EllipseView::bottom() const
154 {
155  return toSceneY(m_item->yCenter() - m_item->yRadius());
156 }
157 
159 {
160  m_item = nullptr;
161 }
Defines EllipseView class.
Defines MaskItems classes.
DoubleDescriptor yRadius() const
Definition: MaskItems.cpp:436
DoubleDescriptor xRadius() const
Definition: MaskItems.cpp:426
void setYRadius(double y_radius)
Definition: MaskItems.cpp:441
DoubleDescriptor xCenter() const
Definition: MaskItems.cpp:406
DoubleDescriptor angle() const
Definition: MaskItems.cpp:446
void setYCenter(double y_center)
Definition: MaskItems.cpp:421
void setXCenter(double x_center)
Definition: MaskItems.cpp:411
void setXRadius(double x_radius)
Definition: MaskItems.cpp:431
DoubleDescriptor yCenter() const
Definition: MaskItems.cpp:416
QRectF mask_rectangle() override
QPainterPath shape() const override
Definition: EllipseView.cpp:96
EllipseView(EllipseItem *item)
Definition: EllipseView.cpp:20
~EllipseView() override
Definition: EllipseView.cpp:26
void onPropertyChange() override
Definition: EllipseView.cpp:45
void update_position() override
updates position of view using item properties
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
Definition: EllipseView.cpp:53
qreal top() const override
Returns the y-coordinate of the rectangle's top edge.
void onChangedY() override
Definition: EllipseView.cpp:38
EllipseItem * m_item
Definition: EllipseView.h:55
void onChangedX() override
Definition: EllipseView.cpp:31
void onItemDestroyed() override
SessionItem * parameterizedItem() const override
qreal bottom() const override
Returns the y-coordinate of the rectangle's bottom edge.
qreal left() const override
Returns the x-coordinate of the rectangle's left edge.
qreal right() const override
Returns the x-coordinate of the rectangle's right edge.
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
Base view for all rectangular-like masks.
SizeHandleElement * m_activeHandleElement
QRectF m_mask_rect
mask rectangle in scene coordinates
qreal width() const
Returns width of the rectangle.
void update_view() override
update visual appearance of view (triggered by ISceneAdaptor)
QPointF m_resize_opposite_origin
Base class for a GUI data item.
Definition: SessionItem.h:204
EHandleType getHandleType() const