26 #include <QGraphicsItem>
27 #include <QGraphicsSceneMoveEvent>
28 #include <QItemSelection>
32 #include <boost/polymorphic_cast.hpp>
34 using boost::polymorphic_downcast;
38 const QRectF default_scene_rect(0, 0, 800, 600);
39 const qreal min_distance_to_create_rect = 10;
44 : QGraphicsScene(parent)
45 , m_maskModel(nullptr)
46 , m_selectionModel(nullptr)
48 , m_block_selection(false)
49 , m_intensityItem(nullptr)
50 , m_currentItem(nullptr)
52 setSceneRect(default_scene_rect);
53 connect(
this, &MaskGraphicsScene::selectionChanged,
this,
71 disconnect(
m_maskModel, &SessionModel::modelAboutToBeReset,
this,
73 disconnect(
m_maskModel, &SessionModel::rowsInserted,
this,
75 disconnect(
m_maskModel, &SessionModel::rowsAboutToBeRemoved,
this,
77 disconnect(
m_maskModel, &SessionModel::rowsRemoved,
this,
79 disconnect(
m_maskModel, &SessionModel::modelReset,
this,
88 throw Error(
"MaskGraphicsScene::setMaskContext() -> Error. Not a container");
93 connect(
m_maskModel, &SessionModel::modelAboutToBeReset,
this,
95 connect(
m_maskModel, &SessionModel::rowsInserted,
this,
97 connect(
m_maskModel, &SessionModel::rowsAboutToBeRemoved,
this,
99 connect(
m_maskModel, &SessionModel::rowsRemoved,
this,
113 disconnect(
m_maskModel, &SessionModel::modelAboutToBeReset,
this,
115 disconnect(
m_maskModel, &SessionModel::rowsInserted,
this,
117 disconnect(
m_maskModel, &SessionModel::rowsAboutToBeRemoved,
this,
119 disconnect(
m_maskModel, &SessionModel::rowsRemoved,
this,
169 for (
int irow = first; irow <= last; ++irow) {
247 for (QGraphicsItem* graphicsItem : selectedItems()) {
248 if (
auto* view =
dynamic_cast<IShape2DView*
>(graphicsItem)) {
250 ASSERT(itemIndex.isValid());
260 if (event->buttons() & Qt::RightButton) {
283 QGraphicsScene::mousePressEvent(event);
292 QGraphicsScene::mouseMoveEvent(event);
310 view->setSelected(
true);
319 QGraphicsScene::mouseReleaseEvent(event);
335 painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
339 const QRectF& plot_scene_rectangle =
m_adaptor->viewportRectangle();
343 painter->setPen(QPen(Qt::black, 1, Qt::DashLine));
347 painter->drawLine(QLineF(p1, p2));
352 painter->drawLine(QLineF(p1, p2));
365 if (
dynamic_cast<IShape2DView*
>(itemAt(event->scenePos(), QTransform())))
397 parentView->
addView(childView, i_row);
441 if (it.key() == item) {
443 view->setSelected(
false);
457 if (!(event->buttons() & Qt::LeftButton))
459 if (!
m_adaptor->viewportRectangle().contains(event->scenePos()))
510 if (QGraphicsItem* graphicsItem = itemAt(event->scenePos(), QTransform())) {
539 for (QGraphicsItem* graphicsItem : this->items(event->scenePos()))
540 if (graphicsItem->type() == viewType)
566 Qt::MouseButtons acceptedButton = (value ? Qt::NoButton : Qt::LeftButton);
568 view->setAcceptedMouseButtons(acceptedButton);
587 if (QGraphicsItem* graphicsItem = itemAt(event->scenePos(), QTransform()))
588 graphicsItem->setSelected(
true);
601 QPointF click_pos =
event->buttonDownScenePos(Qt::LeftButton);
602 QPointF mouse_pos =
event->scenePos();
603 QLineF line(mouse_pos, click_pos);
605 if (!
m_currentItem && line.length() > min_distance_to_create_rect) {
615 qreal xmin = std::min(click_pos.x(), mouse_pos.x());
616 qreal xmax = std::max(click_pos.x(), mouse_pos.x());
617 qreal ymin = std::min(click_pos.y(), mouse_pos.y());
618 qreal ymax = std::max(click_pos.y(), mouse_pos.y());
622 auto* rectItem = polymorphic_downcast<RectangleItem*>(
m_currentItem);
623 rectItem->setXLow(
m_adaptor->fromSceneX(xmin));
624 rectItem->setYLow(
m_adaptor->fromSceneY(ymax));
625 rectItem->setXUp(
m_adaptor->fromSceneX(xmax));
626 rectItem->setYUp(
m_adaptor->fromSceneY(ymin));
628 auto* ellItem = polymorphic_downcast<EllipseItem*>(
m_currentItem);
629 ellItem->setXCenter(
m_adaptor->fromSceneX(xmin + (xmax - xmin) / 2.));
630 ellItem->setYCenter(
m_adaptor->fromSceneY(ymin + (ymax - ymin) / 2.));
631 ellItem->setXRadius((
m_adaptor->fromSceneX(xmax) -
m_adaptor->fromSceneX(xmin)) / 2.);
632 ellItem->setYRadius((
m_adaptor->fromSceneY(ymin) -
m_adaptor->fromSceneY(ymax)) / 2.);
647 QItemSelectionModel::Select);
653 if (polygon->closePolygonIfNecessary()) {
660 QPointF click_pos =
event->buttonDownScenePos(Qt::LeftButton);
663 point->setPosY(
m_adaptor->fromSceneY(click_pos.y()));
669 QPointF click_pos =
event->buttonDownScenePos(Qt::LeftButton);
689 item->setPosX(
m_adaptor->fromSceneX(pos.x()));
696 item->setPosY(
m_adaptor->fromSceneY(pos.y()));
746 if (currentItem->modelType() == itemToChange->
modelType()) {
747 QString itemName = currentItem->itemName();
748 if (itemName.startsWith(itemToChange->
itemName())) {
749 int item_index = itemName.remove(0, itemToChange->
itemName().size()).toInt();
750 if (item_index > glob_index)
751 glob_index = item_index;
Defines class ColorMapSceneAdaptor.
Defines class MaskGraphicsProxy.
Defines class MaskGraphicsScene.
Defines MaskItems classes.
Defines class MaskViewFactory.
Defines PolygonView class.
Defines items related to projections over color map.
Defines class PropertyItem.
Defines class SessionModel.
Performs conversion of MaskItems coordinates between ColorMap and GraphicsScene.
The ColorMap class presents 2D intensity data from IntensityDataItem as color map.
static constexpr auto M_TYPE
static constexpr auto M_TYPE
Main interface class for views representing MaskItems, Projections on graphics scene.
virtual void addView(IShape2DView *childView, int row=0)
static constexpr auto M_TYPE
static constexpr auto M_TYPE
QString activityToModelType() const
Returns model type corresponding to current activity.
bool isPolygonMode() const
int activityToRow() const
Returns model row corresponding to given activity. All shapes, except ROI, will be added on top of ea...
bool isInZoomMode() const
void setDrawingInProgress(bool value)
void setActivityType(MaskEditorFlags::Activity value)
bool isActivityRequiresDrawingCancel(MaskEditorFlags::Activity proposed_new_activity) const
Returns true, if proposed activity requires the cancel of drawing i.e. there is an ungoing polygon dr...
bool isVerticalLineMode() const
bool isRectangleShapeMode() const
bool isMaskAllMode() const
bool isHorizontalLineMode() const
bool getMaskValue() const
bool isDrawingInProgress() const
void setMaskValue(MaskEditorFlags::MaskValue value)
Graphics proxy to place QWidget inside QGraphicsScene, used by MaskEditorCanvas.
void setIntensityItem(IntensityDataItem *item)
void setInZoomMode(bool value)
Sets widget to zoom mode, when signals (zoom wheel, mouse clicks) are send down to ColorMap plot....
void setSceneAdaptor(ISceneAdaptor *sceneAdaptor)
PolygonView * currentPolygon() const
Returns polygon which is currently under the drawing.
bool isAreaContains(QGraphicsSceneMouseEvent *event, MaskEditorHelper::EViewTypes viewType)
Return true if area beneath the mouse contains views of given type.
bool isValidMouseClick(QGraphicsSceneMouseEvent *event)
Returns true if left mouse bottom click was inside ColorMap viewport rectangle.
void setSelectionModel(QItemSelectionModel *model)
QPointF m_currentMousePosition
void updateScene()
Main method to update scene on various changes in the model.
void processLineItem(QGraphicsSceneMouseEvent *event)
void processVerticalLineItem(const QPointF &pos)
SessionModel * m_maskModel
void setZValues()
Update Z-values of all IMaskView to reflect stacking order in SessionModel. Item with irow=0 is the t...
void setDrawingInProgress(bool value)
void processPolygonItem(QGraphicsSceneMouseEvent *event)
void processMaskAllItem(QGraphicsSceneMouseEvent *event)
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
void setInPanAndZoomMode(bool value)
Sets the state of all views in pan&zoom mode. In pan&zoom mode, the selection is removed,...
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
Finalizes item drawing or pass events to other items.
QItemSelectionModel * m_selectionModel
void processHorizontalLineItem(const QPointF &pos)
void onMaskValueChanged(MaskEditorFlags::MaskValue value)
IntensityDataItem * m_intensityItem
void processRectangleShapeItem(QGraphicsSceneMouseEvent *event)
Processes RectangleItem and EllipseItem drawing If the mouse move distance with left button down is l...
bool isDrawingInProgress() const
void removeItemViewFromScene(SessionItem *item)
Removes single view from scene.
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
void itemContextMenuRequest(const QPoint &point)
MaskGraphicsProxy * m_proxy
void setMaskContext(SessionModel *model, const QModelIndex &maskContainerIndex, IntensityDataItem *intensityItem)
void cancelCurrentDrawing()
void onSceneSelectionChanged()
Propagates selection from scene to model.
IShape2DView * addViewForItem(SessionItem *item)
Creates a view for given item.
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override
Creates item context menu if there is IMaskView beneath the mouse right click.
void deleteViews(const QModelIndex &parentIndex)
Recursive delete of all views corresponding to given index.
bool isValidForMaskAllDrawing(QGraphicsSceneMouseEvent *event)
Returns true if MaskAllItem can be drawn. Only one item of such type is allowed.
void updateViews(const QModelIndex &parentIndex={}, IShape2DView *parentView=nullptr)
Recutsively runs through the model and creates corresponding views.
bool isValidForRectangleShapeDrawing(QGraphicsSceneMouseEvent *event)
Returns true if mouse click is valid for rectangular/elliptic/ROI shapes.
void onRowsInserted(const QModelIndex &, int, int)
bool isValidForPolygonDrawing(QGraphicsSceneMouseEvent *event)
Returns true if mouse click is in context suitable for polygon drawing.
void setItemName(SessionItem *itemToChange)
Sets item name depending on alreay existent items. If there is already "Rectangle1",...
MaskDrawingContext m_context
QModelIndex m_maskContainerIndex
bool isValidForLineDrawing(QGraphicsSceneMouseEvent *event)
Returns true if mouse click is in context suitable for line drawing.
QMap< SessionItem *, IShape2DView * > m_ItemToView
void onRowsRemoved(const QModelIndex &, int, int)
void drawForeground(QPainter *painter, const QRectF &rect) override
Draws dashed line to the current mouse position in the case of ungoing line or polygon drawing.
void makeViewAtMousePosSelected(QGraphicsSceneMouseEvent *event)
MaskGraphicsScene(QObject *parent=nullptr)
void updateProxyWidget()
Updates proxy widget for intensity data item.
void onSessionSelectionChanged(const QItemSelection &, const QItemSelection &)
Propagates selection from model to scene.
~MaskGraphicsScene() override
void onRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
void updateCursors()
Change cursor to stress that hovered item is movable (when not in PanZoom mode)
SessionItem * m_currentItem
QSharedPointer< ISceneAdaptor > m_adaptor
void onActivityModeChanged(MaskEditorFlags::Activity value)
A base class for all mask items.
void setMaskValue(bool mask_value)
static IShape2DView * createMaskView(SessionItem *item, ISceneAdaptor *adaptor=nullptr)
static constexpr auto M_TYPE
void setPosX(double pos_x)
This is a View of polygon mask (represented by PolygonItem) on GraphicsScene.
static constexpr auto M_TYPE
static constexpr auto M_TYPE
static constexpr auto M_TYPE
Base class for a GUI data item.
QString itemName() const
Get item name, return display name if no name is set.
void setItemName(const QString &name)
Set item name, add property if necessary.
bool hasModelType() const
QString modelType() const
Get model type.
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
SessionItem * itemForIndex(const QModelIndex &index) const
T * insertItem(SessionItem *parent=nullptr, int row=-1, QString tag="")
QModelIndex index(int row, int column, const QModelIndex &parent) const override
SessionItem * insertNewItem(QString model_type, SessionItem *parent_item=nullptr, int row=-1, QString tag="")
bool removeRows(int row, int count, const QModelIndex &parent) override
int rowCount(const QModelIndex &parent) const override
QModelIndex indexOfItem(SessionItem *item) const
static constexpr auto M_TYPE