BornAgain  1.19.0
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/coregui/Views/MaskWidgets/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 
20 EllipseView::EllipseView() = default;
21 
23 {
24  setBlockOnProperty(true);
26  setBlockOnProperty(false);
27 }
28 
30 {
31  setBlockOnProperty(true);
33  setBlockOnProperty(false);
34 }
35 
36 void EllipseView::onPropertyChange(const QString& propertyName)
37 {
38  if (propertyName == EllipseItem::P_XRADIUS || propertyName == EllipseItem::P_YRADIUS) {
39  update_view();
40  } else if (propertyName == EllipseItem::P_XCENTER) {
42  } else if (propertyName == EllipseItem::P_YCENTER) {
44  } else if (propertyName == EllipseItem::P_ANGLE) {
45  setTransform(QTransform().rotate(-1.0 * par(EllipseItem::P_ANGLE)));
46  }
47 }
48 
49 void EllipseView::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
50 {
51  painter->setRenderHints(QPainter::Antialiasing);
52  bool mask_value = m_item->getItemValue(MaskItem::P_MASK_VALUE).toBool();
53  painter->setBrush(MaskEditorHelper::getMaskBrush(mask_value));
54  painter->setPen(MaskEditorHelper::getMaskPen(mask_value));
55  painter->drawEllipse(m_mask_rect);
56 }
57 
58 void EllipseView::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
59 {
61  QPointF opposPos = mapFromScene(m_resize_opposite_origin);
62  qreal xmin = std::min(event->pos().x(), opposPos.x());
63  qreal xmax = std::max(event->pos().x(), opposPos.x());
64  qreal ymin = std::min(event->pos().y(), opposPos.y());
65  qreal ymax = std::max(event->pos().y(), opposPos.y());
66  qreal width = xmax - xmin;
67  qreal height = ymax - ymin;
68 
69  qreal xcenter = xmin + (xmax - xmin) / 2.;
70  qreal ycenter = ymin + (ymax - ymin) / 2.;
71 
72  QPointF center(xcenter, ycenter);
73  QPointF centerInScene = mapToScene(center);
74 
78 
80  (fromSceneX(centerInScene.x() + width / 2.)
81  - fromSceneX(centerInScene.x() - width / 2.))
82  / 2.);
84  (fromSceneY(centerInScene.y() - height / 2.)
85  - fromSceneY(centerInScene.y() + height / 2.))
86  / 2.);
87 
91  (fromSceneY(centerInScene.y() - height / 2.)
92  - fromSceneY(centerInScene.y() + height / 2.))
93  / 2.);
94 
98  (fromSceneX(centerInScene.x() + width / 2.)
99  - fromSceneX(centerInScene.x() - width / 2.))
100  / 2.);
101  }
102 
103  } else {
104  IShape2DView::mouseMoveEvent(event);
105  }
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 
117 
118  connect(this, &EllipseView::xChanged, this, &EllipseView::onChangedX);
119  connect(this, &EllipseView::yChanged, this, &EllipseView::onChangedY);
120 
121  if (par(EllipseItem::P_ANGLE) != 0.0)
122  setTransform(QTransform().rotate(-1.0 * par(EllipseItem::P_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 {
135 }
136 
137 //! returns the x-coordinate of the rectangle's right edge
138 
139 qreal EllipseView::right() const
140 {
142 }
143 
144 //! Returns the y-coordinate of the rectangle's top edge.
145 
146 qreal EllipseView::top() const
147 {
149 }
150 
151 //! Returns the y-coordinate of the rectangle's bottom edge.
152 
153 qreal EllipseView::bottom() const
154 {
156 }
Defines EllipseView class.
Defines MaskItems classes.
static const QString P_XRADIUS
Definition: MaskItems.h:91
static const QString P_YRADIUS
Definition: MaskItems.h:92
static const QString P_XCENTER
Definition: MaskItems.h:89
static const QString P_ANGLE
Definition: MaskItems.h:93
static const QString P_YCENTER
Definition: MaskItems.h:90
qreal top() const
Returns the y-coordinate of the rectangle's top edge.
QRectF mask_rectangle()
qreal right() const
returns the x-coordinate of the rectangle's right edge
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Definition: EllipseView.cpp:58
virtual void onPropertyChange(const QString &propertyName)
Definition: EllipseView.cpp:36
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
Definition: EllipseView.cpp:49
void update_position()
updates position of view using item properties
virtual void onChangedY()
Definition: EllipseView.cpp:29
qreal bottom() const
Returns the y-coordinate of the rectangle's bottom edge.
qreal left() const
returns the x-coordinate of the rectangle's left edge
virtual void onChangedX()
Definition: EllipseView.cpp:22
qreal toSceneX(const QString &property_name) const
qreal toSceneY(const QString &property_name) const
SessionItem * m_item
Definition: IShape2DView.h:65
qreal fromSceneY(qreal value) const
void setBlockOnProperty(bool value)
qreal fromSceneX(qreal value) const
convert scene coordinates to ColorMap plot coordinates
double par(const QString &property_name) const
static QBrush getMaskBrush(bool mask_value)
static QPen getMaskPen(bool mask_value)
static const QString P_MASK_VALUE
Definition: MaskItems.h:33
SizeHandleElement * m_activeHandleElement
QRectF m_mask_rect
mask rectangle in scene coordinates
qreal width() const
returns width of the rectangle
void update_view()
update visual appearance of view (triggered by ISceneAdaptor)
QPointF m_resize_opposite_origin
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.
EHandleType getHandleType() const