BornAgain  1.19.0
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/coregui/Views/MaskWidgets/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/Utils/Assert.h"
17 
18 namespace {
19 const double bbox_margins = 5; // additional margins around rectangle to form bounding box
20 }
21 
22 RectangleBaseView::RectangleBaseView() : m_activeHandleElement(0)
23 {
24  setFlag(QGraphicsItem::ItemIsSelectable);
25  setFlag(QGraphicsItem::ItemIsMovable);
26  setFlag(QGraphicsItem::ItemSendsGeometryChanges);
27  setAcceptHoverEvents(true);
29 }
30 
31 //! triggered by SizeHandleElement
33 {
34  if (going_to_resize) {
35  setFlag(QGraphicsItem::ItemIsMovable, false);
36  m_activeHandleElement = qobject_cast<SizeHandleElement*>(sender());
38  SizeHandleElement::EHandleLocation oposite_corner =
40  m_resize_opposite_origin = m_resize_handles[oposite_corner]->scenePos();
41  } else {
42  setFlag(QGraphicsItem::ItemIsMovable, true);
44  }
45 }
46 
47 //! Track if item selected/deselected and show/hide size handles
48 QVariant RectangleBaseView::itemChange(QGraphicsItem::GraphicsItemChange change,
49  const QVariant& value)
50 {
51  if (change == QGraphicsItem::ItemSelectedChange) {
52  for (auto it = m_resize_handles.begin(); it != m_resize_handles.end(); ++it) {
53  it.value()->setVisible(!this->isSelected());
54  }
55  }
56  return value;
57 }
58 
59 void RectangleBaseView::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
60 {
62  IShape2DView::mouseReleaseEvent(event);
63 }
64 
66 {
67  prepareGeometryChange();
70 }
71 
72 //! updates view's bounding rectangle using item properties
73 
75 {
76  if (m_item) {
78  m_bounding_rect = m_mask_rect.marginsAdded(
79  QMarginsF(bbox_margins, bbox_margins, bbox_margins, bbox_margins));
80  }
81  for (QMap<SizeHandleElement::EHandleLocation, SizeHandleElement*>::iterator it =
82  m_resize_handles.begin();
83  it != m_resize_handles.end(); ++it) {
84  it.value()->updateHandleElementPosition(m_mask_rect);
85  }
86 }
87 
88 //! returns width of the rectangle
89 
91 {
92  return right() - left();
93 }
94 
96 {
97  return bottom() - top();
98 }
99 
101 {
102  QList<SizeHandleElement::EHandleLocation> points;
107 
108  for (SizeHandleElement::EHandleLocation point_type : points) {
109  SizeHandleElement* el = new SizeHandleElement(point_type, this);
110  connect(el, &SizeHandleElement::resize_request, this,
112  el->setVisible(false);
113  m_resize_handles[point_type] = el;
114  }
115 }
Defines the macro ASSERT.
#define ASSERT(condition)
Definition: Assert.h:31
Defines RectangleBaseView class.
SessionItem * m_item
Definition: IShape2DView.h:65
QRectF m_bounding_rect
Definition: IShape2DView.h:67
virtual qreal right() const =0
virtual QRectF mask_rectangle()=0
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
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
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
Track if item selected/deselected and show/hide size handles.
void onSizeHandleElementRequest(bool going_to_resize)
triggered by SizeHandleElement
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
void update_view()
update visual appearance of view (triggered by ISceneAdaptor)
virtual void update_bounding_rect()
updates view's bounding rectangle using item properties
QPointF m_resize_opposite_origin
virtual qreal left() const =0
virtual qreal bottom() const =0
Size handle on top of RectangleView represented as small circle or small rectangle.
void resize_request(bool going_to_resize)
EHandleLocation getOppositeHandleLocation() const