1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/Overlay/PolygonOverlay.cpp
//! @brief Implements class PolygonOverlay.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/View/Overlay/PolygonOverlay.h"
#include "Base/Util/Assert.h"
#include "GUI/Model/Mask/MaskItems.h"
#include "GUI/Model/Mask/PointItem.h"
#include "GUI/View/Overlay/OverlayStyle.h"
#include "GUI/View/Overlay/VertexOverlay.h"
#include "GUI/View/Plotter/ColorMap.h"
#include <QCursor>
namespace {
const double bbox_margins = 5; // additional margins around points to form bounding box
} // namespace
PolygonOverlay::PolygonOverlay(PolygonItem* item, ColorMap* plot)
: IMaskOverlay(plot)
, m_item(item)
, m_block_on_point_update(false)
, m_close_polygon_request(false)
{
m_item->maskVisibilityChanged();
}
OverlayItem* PolygonOverlay::parameterizedItem() const
{
return m_item;
}
void PolygonOverlay::addOverlay(IOverlay* child)
{
ASSERT(child);
if (childItems().contains(child))
return;
auto* pointView = dynamic_cast<VertexOverlay*>(child);
ASSERT(pointView);
pointView->setParentItem(this);
// polygon consisting from more than 2 points can be closed via hover event by clicking
// on first polygon point
if (!isClosedPolygon() && childItems().size() > 2)
childItems()[0]->setAcceptHoverEvents(true);
// during the drawing process the polygon is selected
pointView->setVisible(isSelected());
update_polygon();
connect(pointView, &VertexOverlay::propertyChanged, this, &PolygonOverlay::update_view);
connect(pointView, &VertexOverlay::closePolygonRequest, this,
&PolygonOverlay::onClosePolygonRequest);
}
//! Returns last added poligon point in scene coordinates
QPointF PolygonOverlay::lastAddedPoint() const
{
return !childItems().empty() ? childItems().back()->scenePos() : QPointF();
}
QPainterPath PolygonOverlay::shape() const
{
QPainterPath path;
path.addPolygon(m_polygon);
path.closeSubpath();
return path;
}
//! 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.
bool PolygonOverlay::closePolygonIfNecessary()
{
if (m_close_polygon_request) {
for (QGraphicsItem* childItem : childItems()) {
childItem->setFlag(QGraphicsItem::ItemIsMovable);
childItem->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
childItem->setAcceptHoverEvents(false);
}
m_item->setIsClosed(true);
update();
}
return isClosedPolygon();
}
void PolygonOverlay::onClosePolygonRequest(bool value)
{
m_close_polygon_request = value;
}
bool PolygonOverlay::isClosedPolygon()
{
return m_item->isClosed();
}
void PolygonOverlay::paint(QPainter* painter, const QStyleOptionGraphicsItem* o, QWidget* w)
{
if (isClosedPolygon())
IMaskOverlay::paint(painter, o, w);
else {
ASSERT(m_item);
const bool mask_value = static_cast<PolygonItem*>(m_item)->maskValue();
painter->setRenderHints(QPainter::Antialiasing);
painter->setPen(GUI::Overlay::getMaskPen(mask_value));
painter->drawPolyline(m_polygon.toPolygon());
}
}
QVariant PolygonOverlay::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant& value)
{
if (change == QGraphicsItem::ItemSelectedHasChanged)
setChildrenVisible(this->isSelected());
return value;
}
void PolygonOverlay::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsObject::mouseMoveEvent(event);
update_points();
}
void PolygonOverlay::update_view()
{
update_polygon();
update();
}
//! Runs through all PointItem and calculate bounding rectangle.
//! Determines position of the rectangle in scene.
//! Calculates position of VertexOverlay in local polygon coordinates
void PolygonOverlay::update_polygon()
{
if (m_block_on_point_update)
return;
m_block_on_point_update = true;
if (!m_item->points().isEmpty()) {
m_polygon.clear();
for (auto* point : m_item->points())
m_polygon << QPointF(x2pix(point->posX().dVal()), y2pix(point->posY().dVal()));
const QRectF scene_rect = m_polygon.boundingRect().marginsAdded(
QMarginsF(bbox_margins, bbox_margins, bbox_margins, bbox_margins));
m_bounding_rect = {0.0, 0.0, scene_rect.width(), scene_rect.height()};
setPos(scene_rect.x(), scene_rect.y());
update(); // to propagate changes to scene
m_polygon = mapFromScene(m_polygon);
int index(0);
for (auto* child : childItems())
child->setPos(m_polygon[index++]);
setPos(scene_rect.x(), scene_rect.y());
}
m_block_on_point_update = false;
}
//! When polygon moves as a whole thing across the scene, given method updates coordinates
//! of PointItem's
void PolygonOverlay::update_points()
{
if (m_block_on_point_update)
return;
for (QGraphicsItem* childItem : childItems()) {
auto* view = dynamic_cast<VertexOverlay*>(childItem);
QPointF pos = view->scenePos();
disconnect(view, &VertexOverlay::propertyChanged, this, &PolygonOverlay::update_view);
view->updateParameterizedItem(pos);
connect(view, &VertexOverlay::propertyChanged, this, &PolygonOverlay::update_view,
Qt::UniqueConnection);
}
}
void PolygonOverlay::setChildrenVisible(bool value)
{
for (QGraphicsItem* childItem : childItems())
childItem->setVisible(value);
}
|