BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
RectangleBaseView.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Mask/RectangleBaseView.cpp
6 //! @brief Implements RectangleBaseView 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 
16 #include "Base/Util/Assert.h"
17 
18 namespace {
19 
20 const double bbox_margins = 5; // additional margins around rectangle to form bounding box
21 }
22 
24  : IShape2DView(item)
25  , m_activeHandleElement(nullptr)
26 {
27  setFlag(QGraphicsItem::ItemIsSelectable);
28  setFlag(QGraphicsItem::ItemIsMovable);
29  setFlag(QGraphicsItem::ItemSendsGeometryChanges);
30  setAcceptHoverEvents(true);
32 }
33 
34 //! triggered by SizeHandleElement
36 {
37  if (going_to_resize) {
38  setFlag(QGraphicsItem::ItemIsMovable, false);
39  m_activeHandleElement = qobject_cast<SizeHandleElement*>(sender());
40  ASSERT(m_activeHandleElement);
41  SizeHandleElement::EHandleLocation oposite_corner =
43  m_resize_opposite_origin = m_resize_handles[oposite_corner]->scenePos();
44  } else {
45  setFlag(QGraphicsItem::ItemIsMovable, true);
46  m_activeHandleElement = nullptr;
47  }
48 }
49 
50 //! Track if item selected/deselected and show/hide size handles
51 QVariant RectangleBaseView::itemChange(QGraphicsItem::GraphicsItemChange change,
52  const QVariant& value)
53 {
54  if (change == QGraphicsItem::ItemSelectedChange) {
55  for (auto it = m_resize_handles.begin(); it != m_resize_handles.end(); ++it)
56  it.value()->setVisible(!this->isSelected());
57  }
58  return value;
59 }
60 
61 void RectangleBaseView::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
62 {
64  IShape2DView::mouseReleaseEvent(event);
65 }
66 
68 {
69  prepareGeometryChange();
72 }
73 
74 //! updates view's bounding rectangle using item properties
75 
77 {
78  if (parameterizedItem()) {
81  m_bounding_rect = m_mask_rect.marginsAdded(
82  QMarginsF(bbox_margins, bbox_margins, bbox_margins, bbox_margins));
83  }
84  for (QMap<SizeHandleElement::EHandleLocation, SizeHandleElement*>::iterator it =
85  m_resize_handles.begin();
86  it != m_resize_handles.end(); ++it) {
87  it.value()->updateHandleElementPosition(m_mask_rect);
88  }
89 }
90 
91 //! Returns width of the rectangle
92 
94 {
95  return right() - left();
96 }
97 
99 {
100  return bottom() - top();
101 }
102 
104 {
105  QList<SizeHandleElement::EHandleLocation> points;
110 
111  for (SizeHandleElement::EHandleLocation point_type : points) {
112  auto* el = new SizeHandleElement(point_type, this);
113  connect(el, &SizeHandleElement::resize_request, this,
115  el->setVisible(false);
116  m_resize_handles[point_type] = el;
117  }
118 }
Defines RectangleBaseView class.
Main interface class for views representing MaskItems, Projections on graphics scene.
Definition: IShape2DView.h:27
virtual SessionItem * parameterizedItem() const =0
QRectF m_bounding_rect
Definition: IShape2DView.h:68
virtual qreal right() const =0
virtual QRectF mask_rectangle()=0
SizeHandleElement * m_activeHandleElement
virtual qreal top() const =0
QRectF m_mask_rect
mask rectangle in scene coordinates
qreal width() const
Returns width of the rectangle.
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
void onSizeHandleElementRequest(bool going_to_resize)
triggered by SizeHandleElement
int type() const override
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override
Track if item selected/deselected and show/hide size handles.
virtual void update_position()=0
QMap< SizeHandleElement::EHandleLocation, SizeHandleElement * > m_resize_handles
coordinates of corner opposite to the grip corner at the moment it first clicked in scene coordinates
virtual void update_bounding_rect()
updates view's bounding rectangle using item properties
void update_view() override
update visual appearance of view (triggered by ISceneAdaptor)
QPointF m_resize_opposite_origin
virtual qreal left() const =0
RectangleBaseView(SessionItem *item)
virtual qreal bottom() const =0
Base class for a GUI data item.
Definition: SessionItem.h:204
Size handle on top of RectangleView represented as small circle or small rectangle....
void resize_request(bool going_to_resize)
EHandleLocation getOppositeHandleLocation() const