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

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

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. More...
 
void onClosePolygonRequest (bool value)
 

Public Member Functions

 PolygonView ()
 
void addView (IShape2DView *childView, int row)
 
QRectF boundingRect () const
 
bool isClosedPolygon ()
 
QPointF lastAddedPoint () const
 returns last added poligon point in scene coordinates More...
 
virtual SessionItemparameterizedItem ()
 
virtual void setParameterizedItem (SessionItem *item)
 
void setSceneAdaptor (const ISceneAdaptor *adaptor)
 
int type () const
 

Protected Slots

virtual void onChangedX ()
 
virtual void onChangedY ()
 
virtual void onPropertyChange (const QString &)
 
void update_view ()
 

Protected Member Functions

bool blockOnProperty () const
 
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)
 
void mouseMoveEvent (QGraphicsSceneMouseEvent *event)
 
void paint (QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
 
double par (const QString &property_name) const
 
void setBlockOnProperty (bool value)
 
qreal toSceneX (const QString &property_name) const
 
qreal toSceneX (qreal value) const
 
qreal toSceneY (const QString &property_name) const
 
qreal toSceneY (qreal value) const
 

Protected Attributes

const ISceneAdaptorm_adaptor
 
bool m_block_on_property_change
 
QRectF m_bounding_rect
 
SessionItemm_item
 

Private Member Functions

bool makePolygonClosed ()
 
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. More...
 

Private Attributes

bool m_block_on_point_update
 
bool m_close_polygon_request
 
QPolygonF m_polygon
 

Detailed Description

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

Definition at line 23 of file PolygonView.h.

Constructor & Destructor Documentation

◆ PolygonView()

PolygonView::PolygonView ( )

Definition at line 25 of file PolygonView.cpp.

26 {
27  setFlag(QGraphicsItem::ItemIsSelectable);
28  setFlag(QGraphicsItem::ItemIsMovable);
29  setFlag(QGraphicsItem::ItemSendsGeometryChanges);
30 }
bool m_close_polygon_request
Definition: PolygonView.h:55
bool m_block_on_point_update
Definition: PolygonView.h:54

Member Function Documentation

◆ addView()

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

Reimplemented from IShape2DView.

Definition at line 32 of file PolygonView.cpp.

33 {
34  Q_UNUSED(row);
35 
36  if (childItems().contains(childView))
37  return;
38 
39  PolygonPointView* pointView = dynamic_cast<PolygonPointView*>(childView);
40  ASSERT(pointView);
41  pointView->setParentItem(this);
42 
43  // polygon consisting from more than 2 points can be closed via hover event by clicking
44  // on first polygon point
45  if (!isClosedPolygon() && childItems().size() > 2)
46  childItems()[0]->setAcceptHoverEvents(true);
47 
48  pointView->setVisible(true);
50 
52  connect(pointView, &PolygonPointView::closePolygonRequest, this,
54 }
#define ASSERT(condition)
Definition: Assert.h:31
This is a View of polygon point for PolygonMaskItem.
void closePolygonRequest(bool)
void propertyChanged()
void update_view()
void onClosePolygonRequest(bool value)
Definition: PolygonView.cpp:79
void update_polygon()
Runs through all PolygonPointItem and calculate bounding rectangle.
bool isClosedPolygon()
Definition: PolygonView.cpp:84

References ASSERT, 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 127 of file IShape2DView.cpp.

128 {
130 }
bool m_block_on_property_change
Definition: IShape2DView.h:68

References IShape2DView::m_block_on_property_change.

◆ boundingRect()

QRectF IShape2DView::boundingRect ( ) const
inherited

Definition at line 36 of file IShape2DView.cpp.

37 {
38  return m_bounding_rect;
39 }
QRectF m_bounding_rect
Definition: IShape2DView.h:67

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 64 of file PolygonView.cpp.

65 {
67  for (QGraphicsItem* childItem : childItems()) {
68  childItem->setFlag(QGraphicsItem::ItemIsMovable);
69  childItem->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
70  childItem->setAcceptHoverEvents(false);
71  childItem->setCursor(Qt::SizeAllCursor);
72  }
74  update();
75  }
76  return isClosedPolygon();
77 }
SessionItem * m_item
Definition: IShape2DView.h:65
static const QString P_ISCLOSED
Definition: MaskItems.h:65
void setItemValue(const QString &tag, const QVariant &variant)
Directly set value of item under given tag.

References isClosedPolygon(), m_close_polygon_request, IShape2DView::m_item, PolygonItem::P_ISCLOSED, and SessionItem::setItemValue().

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 106 of file IShape2DView.cpp.

107 {
108  return m_adaptor ? m_adaptor->fromSceneX(value) : value;
109 }
virtual qreal fromSceneX(qreal) const =0
convert scene x-coordinate to native mask coordinate
const ISceneAdaptor * m_adaptor
Definition: IShape2DView.h:66

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 111 of file IShape2DView.cpp.

112 {
113  return m_adaptor ? m_adaptor->fromSceneY(value) : value;
114 }
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 84 of file PolygonView.cpp.

85 {
87 }
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.

References SessionItem::getItemValue(), IShape2DView::m_item, and PolygonItem::P_ISCLOSED.

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

Here is the call graph for this function:

◆ itemChange()

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

Definition at line 106 of file PolygonView.cpp.

107 {
108  if (change == QGraphicsItem::ItemSelectedHasChanged)
109  setChildrenVisible(this->isSelected());
110 
111  return value;
112 }
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 57 of file PolygonView.cpp.

58 {
59  return childItems().size() ? childItems().back()->scenePos() : QPointF();
60 }

◆ makePolygonClosed()

bool PolygonView::makePolygonClosed ( )
private

◆ mouseMoveEvent()

void PolygonView::mouseMoveEvent ( QGraphicsSceneMouseEvent *  event)
protected

Definition at line 114 of file PolygonView.cpp.

115 {
116  IShape2DView::mouseMoveEvent(event);
117  update_points();
118 }
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 48 of file IShape2DView.h.

48 {}

Referenced by IShape2DView::IShape2DView().

◆ onChangedY

virtual void IShape2DView::onChangedY ( )
inlineprotectedvirtualslotinherited

Definition at line 49 of file IShape2DView.h.

49 {}

Referenced by IShape2DView::IShape2DView().

◆ onClosePolygonRequest

void PolygonView::onClosePolygonRequest ( bool  value)
slot

Definition at line 79 of file PolygonView.cpp.

80 {
82 }

References m_close_polygon_request.

Referenced by addView().

◆ onItemPropertyChange()

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

Definition at line 132 of file IShape2DView.cpp.

133 {
135  return;
136 
138 
139  bool schedule_update = false;
140  if (propertyName == MaskItem::P_MASK_VALUE) {
141  schedule_update = true;
142  } else if (propertyName == MaskItem::P_IS_VISIBLE) {
143  this->setVisible(parameterizedItem()->getItemValue(MaskItem::P_IS_VISIBLE).toBool());
144  schedule_update = true;
145  }
146 
147  onPropertyChange(propertyName);
148 
149  if (schedule_update)
150  update();
151 
153 }
virtual void onPropertyChange(const QString &)
Definition: IShape2DView.h:50
virtual SessionItem * parameterizedItem()
static const QString P_IS_VISIBLE
Definition: MaskItems.h:34
static const QString P_MASK_VALUE
Definition: MaskItems.h:33

References IShape2DView::m_block_on_property_change, IShape2DView::onPropertyChange(), MaskItem::P_IS_VISIBLE, MaskItem::P_MASK_VALUE, and IShape2DView::parameterizedItem().

Referenced by IShape2DView::setParameterizedItem().

Here is the call graph for this function:

◆ onPropertyChange

virtual void IShape2DView::onPropertyChange ( const QString &  )
inlineprotectedvirtualslotinherited

Definition at line 50 of file IShape2DView.h.

50 {}

Referenced by IShape2DView::onItemPropertyChange().

◆ paint()

void PolygonView::paint ( QPainter *  painter,
const QStyleOptionGraphicsItem *  ,
QWidget *   
)
protected

Definition at line 89 of file PolygonView.cpp.

90 {
91  ASSERT(m_item);
92  painter->setRenderHints(QPainter::Antialiasing);
93 
94  bool mask_value = m_item->getItemValue(MaskItem::P_MASK_VALUE).toBool();
95  painter->setBrush(MaskEditorHelper::getMaskBrush(mask_value));
96  painter->setPen(MaskEditorHelper::getMaskPen(mask_value));
97 
98  // painter->drawRect(m_bounding_rect);
99 
100  painter->drawPolyline(m_polygon.toPolygon());
101 
102  if (isClosedPolygon())
103  painter->drawPolygon(m_polygon.toPolygon());
104 }
static QBrush getMaskBrush(bool mask_value)
static QPen getMaskPen(bool mask_value)
QPolygonF m_polygon
Definition: PolygonView.h:53

References ASSERT, SessionItem::getItemValue(), MaskEditorHelper::getMaskBrush(), MaskEditorHelper::getMaskPen(), isClosedPolygon(), IShape2DView::m_item, m_polygon, and MaskItem::P_MASK_VALUE.

Here is the call graph for this function:

◆ par()

double IShape2DView::par ( const QString &  property_name) const
protectedinherited

Definition at line 81 of file IShape2DView.cpp.

82 {
83  return m_item->getItemValue(property_name).toReal();
84 }

References SessionItem::getItemValue(), and IShape2DView::m_item.

Referenced by EllipseView::bottom(), RectangleView::bottom(), EllipseView::left(), RectangleView::left(), EllipseView::onPropertyChange(), EllipseView::right(), RectangleView::right(), EllipseView::top(), RectangleView::top(), and EllipseView::update_position().

Here is the call graph for this function:

◆ parameterizedItem()

SessionItem * IShape2DView::parameterizedItem ( )
virtualinherited

Definition at line 61 of file IShape2DView.cpp.

62 {
63  return m_item;
64 }

References IShape2DView::m_item.

Referenced by IShape2DView::onItemPropertyChange().

◆ setBlockOnProperty()

void IShape2DView::setBlockOnProperty ( bool  value)
protectedinherited

◆ setChildrenVisible()

void PolygonView::setChildrenVisible ( bool  value)
private

Definition at line 184 of file PolygonView.cpp.

185 {
186  for (QGraphicsItem* childItem : childItems())
187  childItem->setVisible(value);
188 }

Referenced by itemChange().

◆ setParameterizedItem()

void IShape2DView::setParameterizedItem ( SessionItem item)
virtualinherited

Definition at line 41 of file IShape2DView.cpp.

42 {
43  if (m_item == item) {
44  return;
45 
46  } else {
47  if (m_item)
48  m_item->mapper()->unsubscribe(this);
49 
50  m_item = item;
51  if (!m_item)
52  return;
53 
55  [this](const QString& name) { onItemPropertyChange(name); }, this);
56 
57  m_item->mapper()->setOnItemDestroy([this](SessionItem*) { m_item = 0; }, this);
58  }
59 }
void onItemPropertyChange(const QString &propertyName)
void unsubscribe(const void *caller)
Cancells all subscribtion of given caller.
Definition: ModelMapper.cpp:98
void setOnItemDestroy(std::function< void(SessionItem *)> f, const void *caller=0)
Definition: ModelMapper.cpp:87
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=0)
Definition: ModelMapper.cpp:35
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
QString const & name(EShape k)
Definition: particles.cpp:21

References IShape2DView::m_item, SessionItem::mapper(), RealSpace::Particles::name(), IShape2DView::onItemPropertyChange(), ModelMapper::setOnItemDestroy(), ModelMapper::setOnPropertyChange(), and ModelMapper::unsubscribe().

Referenced by MaskViewFactory::createMaskView().

Here is the call graph for this function:

◆ setSceneAdaptor()

void IShape2DView::setSceneAdaptor ( const ISceneAdaptor adaptor)
inherited

Definition at line 66 of file IShape2DView.cpp.

67 {
68  ASSERT(adaptor);
69 
70  if (m_adaptor != adaptor) {
71  if (m_adaptor)
73 
74  m_adaptor = adaptor;
76  Qt::UniqueConnection);
77  update_view();
78  }
79 }
void update_request()
virtual void update_view()=0
update visual appearance of view (triggered by ISceneAdaptor)

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

Referenced by MaskViewFactory::createMaskView().

Here is the call graph for this function:

◆ toSceneX() [1/2]

qreal IShape2DView::toSceneX ( const QString &  property_name) const
protectedinherited

Definition at line 86 of file IShape2DView.cpp.

87 {
88  return toSceneX(m_item->getItemValue(property_name).toReal());
89 }
qreal toSceneX(const QString &property_name) const

References SessionItem::getItemValue(), and IShape2DView::m_item.

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:

◆ toSceneX() [2/2]

qreal IShape2DView::toSceneX ( qreal  value) const
protectedinherited

Definition at line 91 of file IShape2DView.cpp.

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

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

Here is the call graph for this function:

◆ toSceneY() [1/2]

qreal IShape2DView::toSceneY ( const QString &  property_name) const
protectedinherited

Definition at line 96 of file IShape2DView.cpp.

97 {
98  return toSceneY(m_item->getItemValue(property_name).toReal());
99 }
qreal toSceneY(const QString &property_name) const

References SessionItem::getItemValue(), and IShape2DView::m_item.

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:

◆ toSceneY() [2/2]

qreal IShape2DView::toSceneY ( qreal  value) const
protectedinherited

Definition at line 101 of file IShape2DView.cpp.

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

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

Here is the call graph for this function:

◆ type()

int PolygonView::type ( ) const
inlinevirtual

Reimplemented from IShape2DView.

Definition at line 27 of file PolygonView.h.

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 170 of file PolygonView.cpp.

171 {
173  return;
174 
175  for (QGraphicsItem* childItem : childItems()) {
176  PolygonPointView* view = dynamic_cast<PolygonPointView*>(childItem);
177  QPointF pos = view->scenePos();
179  view->updateParameterizedItem(pos);
181  }
182 }
void updateParameterizedItem(const QPointF &pos)

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

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 129 of file PolygonView.cpp.

130 {
132  return;
133 
135 
136  if (m_item->numberOfChildren()) {
137 
138  m_polygon.clear();
139 
140  for (SessionItem* item : m_item->getChildrenOfType("PolygonPoint")) {
141  qreal px = toSceneX(item->getItemValue(PolygonPointItem::P_POSX).toReal());
142  qreal py = toSceneY(item->getItemValue(PolygonPointItem::P_POSY).toReal());
143  m_polygon << QPointF(px, py);
144  }
145  QRectF scene_rect = m_polygon.boundingRect().marginsAdded(
146  QMarginsF(bbox_margins, bbox_margins, bbox_margins, bbox_margins));
147 
148  m_bounding_rect = QRectF(0.0, 0.0, scene_rect.width(), scene_rect.height());
149 
150  setPos(scene_rect.x(), scene_rect.y());
151  update(); // to propagate changes to scene
152 
153  m_polygon = mapFromScene(m_polygon);
154 
155  // for (int i = 0; i < childItems().size(); ++i) {
156  // QGraphicsItem* childView = childItems()[i];
157  // childView->setPos(m_polygon[i]);
158  // }
159  int index(0);
160  for (auto childView : childItems())
161  childView->setPos(m_polygon[index++]);
162 
163  setPos(scene_rect.x(), scene_rect.y());
164  }
165  m_block_on_point_update = false;
166 }
static const QString P_POSX
Definition: MaskItems.h:57
static const QString P_POSY
Definition: MaskItems.h:58
int numberOfChildren() const
Returns total number of children.
Definition: SessionItem.cpp:94
QVector< SessionItem * > getChildrenOfType(const QString &model_type) const
Returns a vector of all children of the given type.

References SessionItem::getChildrenOfType(), m_block_on_point_update, IShape2DView::m_bounding_rect, IShape2DView::m_item, m_polygon, SessionItem::numberOfChildren(), PolygonPointItem::P_POSX, PolygonPointItem::P_POSY, 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 ( )
protectedslot

Definition at line 120 of file PolygonView.cpp.

121 {
122  update_polygon();
123  update();
124 }

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 54 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 55 of file PolygonView.h.

Referenced by closePolygonIfNecessary(), and onClosePolygonRequest().

◆ m_item

◆ m_polygon

QPolygonF PolygonView::m_polygon
private

Definition at line 53 of file PolygonView.h.

Referenced by paint(), and update_polygon().


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