BornAgain  1.19.0
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/coregui/Views/MaskWidgets/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 
17 #include <QGraphicsSceneHoverEvent>
18 #include <QPainter>
19 
20 namespace {
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 
107 void SizeHandleElement::mousePressEvent(QGraphicsSceneMouseEvent* event)
108 {
109  emit resize_request(true);
110  QGraphicsObject::mousePressEvent(event);
111 }
112 
113 void SizeHandleElement::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
114 {
115  emit resize_request(false);
116  QGraphicsObject::mouseReleaseEvent(event);
117 }
118 
119 //! set position from location type using coordinates of external rectangle
121 {
122  if (m_handleLocation == TOPLEFT) {
123  setPos(rect.topLeft());
124  }
125 
126  else if (m_handleLocation == TOPMIDDLE) {
127  setPos(QPointF(rect.x() + rect.width() / 2., rect.y()));
128  }
129 
130  else if (m_handleLocation == TOPRIGHT) {
131  setPos(rect.topRight());
132  }
133 
134  else if (m_handleLocation == MIDDLERIGHT) {
135  setPos(QPointF(rect.x() + rect.width(), rect.y() + rect.height() / 2.));
136  }
137 
138  else if (m_handleLocation == BOTTOMRIGHT) {
139  setPos(rect.bottomRight());
140  }
141 
142  else if (m_handleLocation == BOTTOMMIDLE) {
143  setPos(QPointF(rect.x() + rect.width() / 2., rect.y() + rect.height()));
144  }
145 
146  else if (m_handleLocation == BOTTOMLEFT) {
147  setPos(rect.bottomLeft());
148  }
149 
150  else if (m_handleLocation == MIDDLELEFT) {
151  setPos(QPointF(rect.x(), rect.y() + rect.height() / 2.));
152  }
153 }
154 
156 {
157  return m_handleLocation;
158 }
159 
161 {
163 }
164 
166 {
167  return m_handleType;
168 }
169 
171 {
172  update();
173 }
Defines interface class ISceneAdaptor.
Defines SizeHandleElement class.
static QPen getSelectionMarkerPen()
static QBrush getSelectionMarkerBrush()
EHandleLocation m_handleLocation
static QMap< EHandleLocation, EHandleType > m_location_to_type
static QMap< EHandleLocation, Qt::CursorShape > m_cursors
SizeHandleElement(EHandleLocation pointType, QGraphicsObject *parent=0)
void mousePressEvent(QGraphicsSceneMouseEvent *event)
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
QRectF boundingRect() const
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
virtual void update_view()
static QMap< EHandleLocation, EHandleLocation > m_opposite_handle_location
EHandleType getHandleType() const
EHandleLocation getHandleLocation() const
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
EHandleLocation getOppositeHandleLocation() const