BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
PolygonView Class Reference

Description

This is a View of polygon mask (represented by PolygonItem) on GraphicsScene.

Definition at line 26 of file PolygonView.h.

Inheritance diagram for PolygonView:
[legend]
Collaboration diagram for PolygonView:
[legend]

Public Slots

bool closePolygonIfNecessary ()
 Returns true if there was a request to close polygon (emitted by its start point), and then closes a polygon. Returns true if polygon was closed. More...
 
void onClosePolygonRequest (bool value)
 

Public Member Functions

 PolygonView (PolygonItem *item)
 
 ~PolygonView () override
 
void addView (IShape2DView *childView, int row) override
 
QRectF boundingRect () const override
 
bool isClosedPolygon ()
 
QPointF lastAddedPoint () const
 Returns last added poligon point in scene coordinates. More...
 
SessionItemparameterizedItem () const override
 
void setSceneAdaptor (const ISceneAdaptor *adaptor)
 
QPainterPath shape () const override
 
int type () const override
 

Protected Slots

virtual void onChangedX ()
 
virtual void onChangedY ()
 
virtual void onPropertyChange ()
 
void update_view () override
 

Protected Member Functions

bool blockOnProperty () const
 
void disconnectFromItem (SessionItem *item)
 
qreal fromSceneX (qreal value) const
 convert scene coordinates to ColorMap plot coordinates More...
 
qreal fromSceneY (qreal value) const
 
QVariant itemChange (GraphicsItemChange change, const QVariant &value) override
 
QPainterPath maskedShape () const
 Returns the shape with all masking already applied. More...
 
void mouseMoveEvent (QGraphicsSceneMouseEvent *event) override
 
void paint (QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
 
void setBlockOnProperty (bool value)
 
qreal toSceneX (qreal value) const
 
qreal toSceneY (qreal value) const
 

Protected Attributes

const ISceneAdaptorm_adaptor
 
bool m_block_on_property_change
 
QRectF m_bounding_rect
 

Private Member Functions

bool makePolygonClosed ()
 
void onItemDestroyed () override
 
void onItemPropertyChange (const QString &propertyName)
 
void setChildrenVisible (bool value)
 
void update_points ()
 When polygon moves as a whole thing across the scene, given method updates coordinates of PolygonPointItem's. More...
 
void update_polygon ()
 Runs through all PolygonPointItem and calculate bounding rectangle. Determines position of the rectangle in scene. Calculates position of PolygonPointView in local polygon coordinates. More...
 

Private Attributes

bool m_block_on_point_update
 
bool m_close_polygon_request
 
PolygonItemm_item
 
QPolygonF m_polygon
 

Constructor & Destructor Documentation

◆ PolygonView()

PolygonView::PolygonView ( PolygonItem item)
explicit

Definition at line 26 of file PolygonView.cpp.

27  : IShape2DView(item)
28  , m_item(item)
31 {
32  setFlag(QGraphicsItem::ItemIsSelectable);
33  setFlag(QGraphicsItem::ItemIsMovable);
34  setFlag(QGraphicsItem::ItemSendsGeometryChanges);
35 }
IShape2DView(SessionItem *item)
bool m_close_polygon_request
Definition: PolygonView.h:65
PolygonItem * m_item
Definition: PolygonView.h:62
bool m_block_on_point_update
Definition: PolygonView.h:64

◆ ~PolygonView()

PolygonView::~PolygonView ( )
override

Definition at line 37 of file PolygonView.cpp.

38 {
40 }
void disconnectFromItem(SessionItem *item)

References IShape2DView::disconnectFromItem(), and m_item.

Here is the call graph for this function:

Member Function Documentation

◆ addView()

void PolygonView::addView ( IShape2DView childView,
int  row 
)
overridevirtual

Reimplemented from IShape2DView.

Definition at line 47 of file PolygonView.cpp.

48 {
49  Q_UNUSED(row);
50 
51  if (childItems().contains(childView))
52  return;
53 
54  auto* pointView = dynamic_cast<PolygonPointView*>(childView);
55  ASSERT(pointView);
56  pointView->setParentItem(this);
57 
58  // polygon consisting from more than 2 points can be closed via hover event by clicking
59  // on first polygon point
60  if (!isClosedPolygon() && childItems().size() > 2)
61  childItems()[0]->setAcceptHoverEvents(true);
62 
63  pointView->setVisible(true);
65 
67  connect(pointView, &PolygonPointView::closePolygonRequest, this,
69 }
This is a View of polygon point for PolygonMaskItem.
void closePolygonRequest(bool)
void propertyChanged()
void onClosePolygonRequest(bool value)
void update_view() override
void update_polygon()
Runs through all PolygonPointItem and calculate bounding rectangle. Determines position of the rectan...
bool isClosedPolygon()

References PolygonPointView::closePolygonRequest(), isClosedPolygon(), onClosePolygonRequest(), PolygonPointView::propertyChanged(), update_polygon(), and update_view().

Here is the call graph for this function:

◆ blockOnProperty()

bool IShape2DView::blockOnProperty ( ) const
protectedinherited

Definition at line 140 of file IShape2DView.cpp.

141 {
143 }
bool m_block_on_property_change
Definition: IShape2DView.h:69

References IShape2DView::m_block_on_property_change.

◆ boundingRect()

QRectF IShape2DView::boundingRect ( ) const
overrideinherited

Definition at line 50 of file IShape2DView.cpp.

51 {
52  return m_bounding_rect;
53 }
QRectF m_bounding_rect
Definition: IShape2DView.h:68

References IShape2DView::m_bounding_rect.

◆ closePolygonIfNecessary

bool PolygonView::closePolygonIfNecessary ( )
slot

Returns true if there was a request to close polygon (emitted by its start point), and then closes a polygon. Returns true if polygon was closed.

Definition at line 87 of file PolygonView.cpp.

88 {
90  for (QGraphicsItem* childItem : childItems()) {
91  childItem->setFlag(QGraphicsItem::ItemIsMovable);
92  childItem->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
93  childItem->setAcceptHoverEvents(false);
94  childItem->setCursor(Qt::SizeAllCursor);
95  }
96  m_item->setIsClosed(true);
97  update();
98  }
99  return isClosedPolygon();
100 }
void setIsClosed(bool closed)
Definition: MaskItems.cpp:278

References isClosedPolygon(), m_close_polygon_request, m_item, and PolygonItem::setIsClosed().

Here is the call graph for this function:

◆ disconnectFromItem()

void IShape2DView::disconnectFromItem ( SessionItem item)
protectedinherited

Definition at line 145 of file IShape2DView.cpp.

146 {
147  if (item)
148  item->mapper()->unsubscribe(this);
149 }
void unsubscribe(const void *caller)
Cancels all subscriptions of given caller.
Definition: ModelMapper.cpp:78
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.

References SessionItem::mapper(), and ModelMapper::unsubscribe().

Referenced by EllipseView::~EllipseView(), HorizontalLineView::~HorizontalLineView(), MaskAllView::~MaskAllView(), MaskContainerView::~MaskContainerView(), PolygonPointView::~PolygonPointView(), ~PolygonView(), RectangleView::~RectangleView(), and VerticalLineView::~VerticalLineView().

Here is the call graph for this function:

◆ fromSceneX()

qreal IShape2DView::fromSceneX ( qreal  value) const
protectedinherited

convert scene coordinates to ColorMap plot coordinates

Definition at line 119 of file IShape2DView.cpp.

120 {
121  return m_adaptor ? m_adaptor->fromSceneX(value) : value;
122 }
virtual qreal fromSceneX(qreal) const =0
convert scene x-coordinate to native mask coordinate
const ISceneAdaptor * m_adaptor
Definition: IShape2DView.h:67

References ISceneAdaptor::fromSceneX(), and IShape2DView::m_adaptor.

Referenced by EllipseView::mouseMoveEvent(), RectangleView::mouseMoveEvent(), EllipseView::onChangedX(), VerticalLineView::onChangedX(), RectangleView::onChangedX(), and PolygonPointView::updateParameterizedItem().

Here is the call graph for this function:

◆ fromSceneY()

qreal IShape2DView::fromSceneY ( qreal  value) const
protectedinherited

Definition at line 124 of file IShape2DView.cpp.

125 {
126  return m_adaptor ? m_adaptor->fromSceneY(value) : value;
127 }
virtual qreal fromSceneY(qreal) const =0
convert scene y-coordinate to native mask coordinate

References ISceneAdaptor::fromSceneY(), and IShape2DView::m_adaptor.

Referenced by EllipseView::mouseMoveEvent(), RectangleView::mouseMoveEvent(), EllipseView::onChangedY(), HorizontalLineView::onChangedY(), RectangleView::onChangedY(), and PolygonPointView::updateParameterizedItem().

Here is the call graph for this function:

◆ isClosedPolygon()

bool PolygonView::isClosedPolygon ( )

Definition at line 107 of file PolygonView.cpp.

108 {
109  return m_item->isClosed();
110 }
bool isClosed() const
Definition: MaskItems.cpp:273

References PolygonItem::isClosed(), and m_item.

Referenced by addView(), closePolygonIfNecessary(), and paint().

Here is the call graph for this function:

◆ itemChange()

QVariant PolygonView::itemChange ( GraphicsItemChange  change,
const QVariant &  value 
)
overrideprotected

Definition at line 125 of file PolygonView.cpp.

126 {
127  if (change == QGraphicsItem::ItemSelectedHasChanged)
128  setChildrenVisible(this->isSelected());
129 
130  return value;
131 }
void setChildrenVisible(bool value)

References setChildrenVisible().

Here is the call graph for this function:

◆ lastAddedPoint()

QPointF PolygonView::lastAddedPoint ( ) const

Returns last added poligon point in scene coordinates.

Definition at line 72 of file PolygonView.cpp.

73 {
74  return !childItems().empty() ? childItems().back()->scenePos() : QPointF();
75 }

◆ makePolygonClosed()

bool PolygonView::makePolygonClosed ( )
private

◆ maskedShape()

QPainterPath IShape2DView::maskedShape ( ) const
protectedinherited

Returns the shape with all masking already applied.

Definition at line 79 of file IShape2DView.cpp.

80 {
81  static const QSet<MaskEditorHelper::EViewTypes> relevantMaskTypes = {
84 
85  QPainterPath resultingShape = mapToScene(shape());
86  for (auto* item : scene()->items()) {
87  auto* const maskItem = dynamic_cast<IShape2DView*>(item);
88  if (!maskItem
89  || !relevantMaskTypes.contains((MaskEditorHelper::EViewTypes)maskItem->type()))
90  continue;
91 
92  const bool isMaskingItem = itemMaskValue(maskItem->parameterizedItem());
93  const bool isOnTop = maskItem->zValue() > zValue();
94  if (isMaskingItem || !isOnTop)
95  continue;
96 
97  const auto maskItemShape = maskItem->mapToScene(maskItem->shape());
98  if (!maskItemShape.intersects(resultingShape))
99  continue;
100 
101  // Item lays on top and is non-masking -> subtract the
102  // path of the item
103  resultingShape = resultingShape.subtracted(maskItemShape);
104  }
105 
106  return mapFromScene(resultingShape);
107 }
Main interface class for views representing MaskItems, Projections on graphics scene.
Definition: IShape2DView.h:27

References MaskEditorHelper::ELLIPSE, MaskEditorHelper::HORIZONTALLINE, MaskEditorHelper::POLYGON, MaskEditorHelper::RECTANGLE, and MaskEditorHelper::VERTICALLINE.

Referenced by IShape2DView::paint().

◆ mouseMoveEvent()

void PolygonView::mouseMoveEvent ( QGraphicsSceneMouseEvent *  event)
overrideprotected

Definition at line 133 of file PolygonView.cpp.

134 {
135  IShape2DView::mouseMoveEvent(event);
136  update_points();
137 }
void update_points()
When polygon moves as a whole thing across the scene, given method updates coordinates of PolygonPoin...

References update_points().

Here is the call graph for this function:

◆ onChangedX

virtual void IShape2DView::onChangedX ( )
inlineprotectedvirtualslotinherited

Definition at line 44 of file IShape2DView.h.

44 {}

Referenced by IShape2DView::IShape2DView().

◆ onChangedY

virtual void IShape2DView::onChangedY ( )
inlineprotectedvirtualslotinherited

Definition at line 45 of file IShape2DView.h.

45 {}

Referenced by IShape2DView::IShape2DView().

◆ onClosePolygonRequest

void PolygonView::onClosePolygonRequest ( bool  value)
slot

Definition at line 102 of file PolygonView.cpp.

103 {
104  m_close_polygon_request = value;
105 }

References m_close_polygon_request.

Referenced by addView().

◆ onItemDestroyed()

void PolygonView::onItemDestroyed ( )
overrideprivatevirtual

Implements IShape2DView.

Definition at line 202 of file PolygonView.cpp.

203 {
204  m_item = nullptr;
205 }

References m_item.

◆ onItemPropertyChange()

void IShape2DView::onItemPropertyChange ( const QString &  propertyName)
privateinherited

Definition at line 151 of file IShape2DView.cpp.

152 {
154  return;
155 
157 
158  bool schedule_update = false;
159  if (MaskItem::isMaskValuePropertyName(propertyName))
160  schedule_update = true;
161  else if (MaskItem::isIsVisiblePropertyName(propertyName)) {
162  bool visible = false;
163  if (auto* maskItem = dynamic_cast<MaskItem*>(parameterizedItem()))
164  visible = maskItem->isVisibleValue();
165  this->setVisible(visible);
166  schedule_update = true;
167  }
168 
170 
171  if (schedule_update)
172  update();
173 
175 }
virtual void onPropertyChange()
Definition: IShape2DView.h:46
virtual SessionItem * parameterizedItem() const =0
A base class for all mask items.
Definition: MaskItems.h:27
static bool isIsVisiblePropertyName(const QString &name)
Definition: MaskItems.cpp:105
static bool isMaskValuePropertyName(const QString &name)
Definition: MaskItems.cpp:90

References MaskItem::isIsVisiblePropertyName(), MaskItem::isMaskValuePropertyName(), IShape2DView::m_block_on_property_change, IShape2DView::onPropertyChange(), and IShape2DView::parameterizedItem().

Referenced by IShape2DView::IShape2DView().

Here is the call graph for this function:

◆ onPropertyChange

virtual void IShape2DView::onPropertyChange ( )
inlineprotectedvirtualslotinherited

Definition at line 46 of file IShape2DView.h.

46 {}

Referenced by IShape2DView::onItemPropertyChange().

◆ paint()

void PolygonView::paint ( QPainter *  painter,
const QStyleOptionGraphicsItem *  o,
QWidget *  w 
)
overrideprotected

Definition at line 112 of file PolygonView.cpp.

113 {
114  if (isClosedPolygon())
115  IShape2DView::paint(painter, o, w);
116  else {
117  ASSERT(m_item);
118  const bool mask_value = static_cast<PolygonItem*>(m_item)->maskValue();
119  painter->setRenderHints(QPainter::Antialiasing);
120  painter->setPen(MaskEditorHelper::getMaskPen(mask_value));
121  painter->drawPolyline(m_polygon.toPolygon());
122  }
123 }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override
static QPen getMaskPen(bool mask_value)
QPolygonF m_polygon
Definition: PolygonView.h:63

References MaskEditorHelper::getMaskPen(), isClosedPolygon(), m_item, m_polygon, and IShape2DView::paint().

Here is the call graph for this function:

◆ parameterizedItem()

SessionItem * PolygonView::parameterizedItem ( ) const
overridevirtual

Implements IShape2DView.

Definition at line 42 of file PolygonView.cpp.

43 {
44  return m_item;
45 }

References m_item.

◆ setBlockOnProperty()

void IShape2DView::setBlockOnProperty ( bool  value)
protectedinherited

◆ setChildrenVisible()

void PolygonView::setChildrenVisible ( bool  value)
private

Definition at line 196 of file PolygonView.cpp.

197 {
198  for (QGraphicsItem* childItem : childItems())
199  childItem->setVisible(value);
200 }

Referenced by itemChange().

◆ setSceneAdaptor()

void IShape2DView::setSceneAdaptor ( const ISceneAdaptor adaptor)
inherited

Definition at line 55 of file IShape2DView.cpp.

56 {
57  ASSERT(adaptor);
58 
59  if (m_adaptor != adaptor) {
60  if (m_adaptor)
62 
63  m_adaptor = adaptor;
65  Qt::UniqueConnection);
66  update_view();
67  }
68 }
void update_request()
virtual void update_view()=0
update visual appearance of view (triggered by ISceneAdaptor)

References IShape2DView::m_adaptor, ISceneAdaptor::update_request(), and IShape2DView::update_view().

Referenced by MaskViewFactory::createMaskView().

Here is the call graph for this function:

◆ shape()

QPainterPath PolygonView::shape ( ) const
override

Definition at line 77 of file PolygonView.cpp.

78 {
79  QPainterPath path;
80  path.addPolygon(m_polygon);
81  path.closeSubpath();
82  return path;
83 }

References m_polygon.

◆ toSceneX()

qreal IShape2DView::toSceneX ( qreal  value) const
protectedinherited

Definition at line 109 of file IShape2DView.cpp.

110 {
111  return m_adaptor ? m_adaptor->toSceneX(value) : value;
112 }
virtual qreal toSceneX(qreal) const =0
convert native mask x-coordinate to scene coordinate

References IShape2DView::m_adaptor, and ISceneAdaptor::toSceneX().

Referenced by EllipseView::left(), RectangleView::left(), EllipseView::onPropertyChange(), VerticalLineView::onPropertyChange(), EllipseView::right(), RectangleView::right(), update_polygon(), EllipseView::update_position(), RectangleView::update_position(), and VerticalLineView::update_view().

Here is the call graph for this function:

◆ toSceneY()

qreal IShape2DView::toSceneY ( qreal  value) const
protectedinherited

Definition at line 114 of file IShape2DView.cpp.

115 {
116  return m_adaptor ? m_adaptor->toSceneY(value) : value;
117 }
virtual qreal toSceneY(qreal) const =0
convert native mask y-coordinate to scene coordinate

References IShape2DView::m_adaptor, and ISceneAdaptor::toSceneY().

Referenced by EllipseView::bottom(), RectangleView::bottom(), EllipseView::onPropertyChange(), HorizontalLineView::onPropertyChange(), EllipseView::top(), RectangleView::top(), update_polygon(), EllipseView::update_position(), RectangleView::update_position(), and HorizontalLineView::update_view().

Here is the call graph for this function:

◆ type()

int PolygonView::type ( ) const
inlineoverride

Definition at line 30 of file PolygonView.h.

30 { return MaskEditorHelper::POLYGON; }

References MaskEditorHelper::POLYGON.

◆ update_points()

void PolygonView::update_points ( )
private

When polygon moves as a whole thing across the scene, given method updates coordinates of PolygonPointItem's.

Definition at line 182 of file PolygonView.cpp.

183 {
185  return;
186 
187  for (QGraphicsItem* childItem : childItems()) {
188  auto* view = dynamic_cast<PolygonPointView*>(childItem);
189  QPointF pos = view->scenePos();
191  view->updateParameterizedItem(pos);
193  }
194 }

References m_block_on_point_update, PolygonPointView::propertyChanged(), and update_view().

Referenced by mouseMoveEvent().

Here is the call graph for this function:

◆ update_polygon()

void PolygonView::update_polygon ( )
private

Runs through all PolygonPointItem and calculate bounding rectangle. Determines position of the rectangle in scene. Calculates position of PolygonPointView in local polygon coordinates.

Definition at line 148 of file PolygonView.cpp.

149 {
151  return;
152 
154 
155  if (!m_item->points().isEmpty()) {
156  m_polygon.clear();
157 
158  for (auto* point : m_item->points())
159  m_polygon << QPointF(toSceneX(point->posX()), toSceneY(point->posY()));
160 
161  const QRectF scene_rect = m_polygon.boundingRect().marginsAdded(
162  QMarginsF(bbox_margins, bbox_margins, bbox_margins, bbox_margins));
163 
164  m_bounding_rect = QRectF(0.0, 0.0, scene_rect.width(), scene_rect.height());
165 
166  setPos(scene_rect.x(), scene_rect.y());
167  update(); // to propagate changes to scene
168 
169  m_polygon = mapFromScene(m_polygon);
170 
171  int index(0);
172  for (auto* childView : childItems())
173  childView->setPos(m_polygon[index++]);
174 
175  setPos(scene_rect.x(), scene_rect.y());
176  }
177  m_block_on_point_update = false;
178 }
qreal toSceneX(qreal value) const
qreal toSceneY(qreal value) const
QVector< PolygonPointItem * > points() const
Definition: MaskItems.cpp:284

References m_block_on_point_update, IShape2DView::m_bounding_rect, m_item, m_polygon, PolygonItem::points(), IShape2DView::toSceneX(), and IShape2DView::toSceneY().

Referenced by addView(), and update_view().

Here is the call graph for this function:

◆ update_view

void PolygonView::update_view ( )
overrideprotectedslot

Definition at line 139 of file PolygonView.cpp.

140 {
141  update_polygon();
142  update();
143 }

References update_polygon().

Referenced by addView(), and update_points().

Here is the call graph for this function:

Member Data Documentation

◆ m_adaptor

◆ m_block_on_point_update

bool PolygonView::m_block_on_point_update
private

Definition at line 64 of file PolygonView.h.

Referenced by update_points(), and update_polygon().

◆ m_block_on_property_change

bool IShape2DView::m_block_on_property_change
protectedinherited

◆ m_bounding_rect

◆ m_close_polygon_request

bool PolygonView::m_close_polygon_request
private

Definition at line 65 of file PolygonView.h.

Referenced by closePolygonIfNecessary(), and onClosePolygonRequest().

◆ m_item

◆ m_polygon

QPolygonF PolygonView::m_polygon
private

Definition at line 63 of file PolygonView.h.

Referenced by paint(), shape(), and update_polygon().


The documentation for this class was generated from the following files: