BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SizeHandleElement.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Mask/SizeHandleElement.cpp
6 //! @brief Implements SizeHandleElement 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 <QGraphicsSceneHoverEvent>
17 #include <QPainter>
18 
19 namespace {
20 
21 QMap<SizeHandleElement::EHandleLocation, Qt::CursorShape> create_cursors_map()
22 {
23  QMap<SizeHandleElement::EHandleLocation, Qt::CursorShape> result;
24  result[SizeHandleElement::NONE] = Qt::ArrowCursor;
25  result[SizeHandleElement::TOPLEFT] = Qt::SizeFDiagCursor;
26  result[SizeHandleElement::TOPMIDDLE] = Qt::SizeVerCursor;
27  result[SizeHandleElement::TOPRIGHT] = Qt::SizeBDiagCursor;
28  result[SizeHandleElement::MIDDLERIGHT] = Qt::SizeHorCursor;
29  result[SizeHandleElement::BOTTOMRIGHT] = Qt::SizeFDiagCursor;
30  result[SizeHandleElement::BOTTOMMIDLE] = Qt::SizeVerCursor;
31  result[SizeHandleElement::BOTTOMLEFT] = Qt::SizeBDiagCursor;
32  result[SizeHandleElement::MIDDLELEFT] = Qt::SizeHorCursor;
33  return result;
34 }
35 
36 QMap<SizeHandleElement::EHandleLocation, SizeHandleElement::EHandleType>
37 create_location_to_type_map()
38 {
39  QMap<SizeHandleElement::EHandleLocation, SizeHandleElement::EHandleType> result;
49  return result;
50 }
51 
52 QMap<SizeHandleElement::EHandleLocation, SizeHandleElement::EHandleLocation>
53 getMapOfOppositeCorners()
54 {
55  QMap<SizeHandleElement::EHandleLocation, SizeHandleElement::EHandleLocation> result;
64  return result;
65 }
66 
67 } // namespace
68 
69 QMap<SizeHandleElement::EHandleLocation, SizeHandleElement::EHandleType>
70  SizeHandleElement::m_location_to_type = create_location_to_type_map();
71 
72 QMap<SizeHandleElement::EHandleLocation, Qt::CursorShape> SizeHandleElement::m_cursors =
73  create_cursors_map();
74 
75 QMap<SizeHandleElement::EHandleLocation, SizeHandleElement::EHandleLocation>
76  SizeHandleElement::m_opposite_handle_location = getMapOfOppositeCorners();
77 
78 // ----------------------------------------------------------------------------
79 
80 SizeHandleElement::SizeHandleElement(EHandleLocation pointType, QGraphicsObject* parent)
81  : QGraphicsObject(parent)
82  , m_handleLocation(pointType)
83  , m_handleType(m_location_to_type[pointType])
84 {
85  setCursor(m_cursors[m_handleLocation]);
86  setParentItem(parent);
87 }
88 
90 {
91  return QRectF(-4, -4, 8, 8);
92 }
93 
94 void SizeHandleElement::paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget*)
95 {
96  painter->setRenderHints(QPainter::Antialiasing);
97 
98  painter->setBrush(MaskEditorHelper::getSelectionMarkerBrush());
99  painter->setPen(MaskEditorHelper::getSelectionMarkerPen());
100  if (getHandleType() == RESIZE)
101  painter->drawRect(boundingRect());
102  else
103  painter->drawEllipse(boundingRect());
104 }
105 
106 void SizeHandleElement::mousePressEvent(QGraphicsSceneMouseEvent* event)
107 {
108  emit resize_request(true);
109  QGraphicsObject::mousePressEvent(event);
110 }
111 
112 void SizeHandleElement::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
113 {
114  emit resize_request(false);
115  QGraphicsObject::mouseReleaseEvent(event);
116 }
117 
118 //! set position from location type using coordinates of external rectangle
120 {
121  if (m_handleLocation == TOPLEFT)
122  setPos(rect.topLeft());
123 
124  else if (m_handleLocation == TOPMIDDLE)
125  setPos(QPointF(rect.x() + rect.width() / 2., rect.y()));
126 
127 
128  else if (m_handleLocation == TOPRIGHT)
129  setPos(rect.topRight());
130 
131 
132  else if (m_handleLocation == MIDDLERIGHT)
133  setPos(QPointF(rect.x() + rect.width(), rect.y() + rect.height() / 2.));
134 
135 
136  else if (m_handleLocation == BOTTOMRIGHT)
137  setPos(rect.bottomRight());
138 
139 
140  else if (m_handleLocation == BOTTOMMIDLE)
141  setPos(QPointF(rect.x() + rect.width() / 2., rect.y() + rect.height()));
142 
143 
144  else if (m_handleLocation == BOTTOMLEFT)
145  setPos(rect.bottomLeft());
146 
147 
148  else if (m_handleLocation == MIDDLELEFT)
149  setPos(QPointF(rect.x(), rect.y() + rect.height() / 2.));
150 }
151 
153 {
154  return m_handleLocation;
155 }
156 
158 {
160 }
161 
163 {
164  return m_handleType;
165 }
166 
168 {
169  update();
170 }
Defines SizeHandleElement class.
static QPen getSelectionMarkerPen()
static QBrush getSelectionMarkerBrush()
EHandleLocation m_handleLocation
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
SizeHandleElement(EHandleLocation pointType, QGraphicsObject *parent=nullptr)
static QMap< EHandleLocation, EHandleType > m_location_to_type
static QMap< EHandleLocation, Qt::CursorShape > m_cursors
void resize_request(bool going_to_resize)
void updateHandleElementPosition(const QRectF &rect)
set position from location type using coordinates of external rectangle
EHandleType m_handleType
virtual void update_view()
static QMap< EHandleLocation, EHandleLocation > m_opposite_handle_location
EHandleType getHandleType() const
EHandleLocation getHandleLocation() const
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
EHandleLocation getOppositeHandleLocation() const