Element editor : Improve responsiveness when several shapes are selected.

This commit is contained in:
joshua
2022-08-13 12:40:59 +02:00
parent af5d5e0aa3
commit eee1c7fff7
13 changed files with 43 additions and 197 deletions

View File

@@ -1354,24 +1354,28 @@ void ElementScene::managePrimitivesGroups()
m_decorator -> hide(); m_decorator -> hide();
} }
if (m_single_selected_item) {
m_single_selected_item->removeHandler();
m_single_selected_item.clear();
}
// should we hide the decorator? // should we hide the decorator?
QList<QGraphicsItem *> selected_items = zItems( const auto selected_items{zItems(ElementScene::Selected | ElementScene::IncludeTerminals)};
ElementScene::Selected
| ElementScene::IncludeTerminals);
if (selected_items.size() <= 1) if (selected_items.size() <= 1)
{ {
m_decorator->hide(); m_decorator->hide();
if (!selected_items.isEmpty())
{
if (CustomElementGraphicPart *item_ = dynamic_cast<CustomElementGraphicPart *>(selected_items.first()))
{
item_->addHandler();
m_single_selected_item = item_;
}
}
} }
else else
{ {
for(QGraphicsItem *qgi : selected_items)
{
/* We recall set selected,
* then every primitive will remove there handler
* because there are several item selected
*/
qgi->setSelected(true);
}
m_decorator -> setZValue(1000000); m_decorator -> setZValue(1000000);
m_decorator -> setPos(0, 0); m_decorator -> setPos(0, 0);
m_decorator -> setItems(selected_items); m_decorator -> setItems(selected_items);

View File

@@ -32,6 +32,7 @@ class ElementPrimitiveDecorator;
class QETElementEditor; class QETElementEditor;
class ESEventInterface; class ESEventInterface;
class QKeyEvent; class QKeyEvent;
class CustomElementGraphicPart;
/** /**
@brief The ElementScene class @brief The ElementScene class
This class is the canvas allowing the visual edition of an electrial element. This class is the canvas allowing the visual edition of an electrial element.
@@ -88,6 +89,8 @@ class ElementScene : public QGraphicsScene
int m_x_grid, int m_x_grid,
m_y_grid; m_y_grid;
QPointer<CustomElementGraphicPart> m_single_selected_item;
// methods // methods
public: public:
ElementData elementData(); ElementData elementData();

View File

@@ -298,6 +298,9 @@ class CustomElementGraphicPart : public QGraphicsObject, public CustomElementPar
return QObject::property(name);} return QObject::property(name);}
virtual QPainterPath shadowShape ()const = 0; virtual QPainterPath shadowShape ()const = 0;
virtual void addHandler() {}
virtual void removeHandler() {}
virtual void setHandlerColor(QPointF /*pos*/, virtual void setHandlerColor(QPointF /*pos*/,
const QColor &/*color*/) {} const QColor &/*color*/) {}
virtual void resetAllHandlerColor() {} virtual void resetAllHandlerColor() {}

View File

@@ -197,32 +197,12 @@ void PartArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
*/ */
QVariant PartArc::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) QVariant PartArc::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{ {
if (change == ItemSelectedHasChanged && scene()) if (change == ItemPositionHasChanged)
{
if (value.toBool() == true)
{
//When item is selected, he must to be up to date whene the selection in the scene change, for display or not the handler,
//according to the number of selected items.
connect(scene(), &QGraphicsScene::selectionChanged, this, &PartArc::sceneSelectionChanged);
if (scene()->selectedItems().size() == 1)
addHandler();
}
else
{
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartArc::sceneSelectionChanged);
removeHandler();
}
}
else if (change == ItemPositionHasChanged)
{ {
adjusteHandlerPos(); adjusteHandlerPos();
} }
else if (change == ItemSceneChange) else if (change == ItemSceneChange)
{ {
if(scene())
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartArc::sceneSelectionChanged);
setSelected(false); //This is item removed from scene, then we deselect this, and so, the handlers is also removed. setSelected(false); //This is item removed from scene, then we deselect this, and so, the handlers is also removed.
} }
@@ -442,18 +422,6 @@ void PartArc::handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSc
} }
} }
/**
@brief PartArc::sceneSelectionChanged
When the scene selection change, if there are several primitive selected, we remove the handler of this item
*/
void PartArc::sceneSelectionChanged()
{
if (this->isSelected() && scene()->selectedItems().size() == 1)
addHandler();
else
removeHandler();
}
/** /**
@brief PartArc::addHandler @brief PartArc::addHandler
Add handlers for this item Add handlers for this item

View File

@@ -61,6 +61,9 @@ class PartArc : public AbstractPartEllipse
void setSpanAngle(const int &span_angle) override {AbstractPartEllipse::setSpanAngle(span_angle); adjusteHandlerPos();} void setSpanAngle(const int &span_angle) override {AbstractPartEllipse::setSpanAngle(span_angle); adjusteHandlerPos();}
QRectF sceneGeometricRect() const override; QRectF sceneGeometricRect() const override;
void addHandler() override;
void removeHandler() override;
protected: protected:
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
@@ -72,10 +75,6 @@ class PartArc : public AbstractPartEllipse
void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void sceneSelectionChanged ();
void addHandler();
void removeHandler();
private: private:
QPropertyUndoCommand *m_undo_command = nullptr; QPropertyUndoCommand *m_undo_command = nullptr;

View File

@@ -179,32 +179,12 @@ void PartEllipse::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
*/ */
QVariant PartEllipse::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) QVariant PartEllipse::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{ {
if (change == ItemSelectedHasChanged && scene()) if (change == ItemPositionHasChanged)
{
if (value.toBool() == true)
{
//When item is selected, he must to be up to date whene the selection in the scene change, for display or not the handler,
//according to the number of selected items.
connect(scene(), &QGraphicsScene::selectionChanged, this, &PartEllipse::sceneSelectionChanged);
if (scene()->selectedItems().size() == 1)
addHandler();
}
else
{
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartEllipse::sceneSelectionChanged);
removeHandler();
}
}
else if (change == ItemPositionHasChanged)
{ {
adjusteHandlerPos(); adjusteHandlerPos();
} }
else if (change == ItemSceneChange) else if (change == ItemSceneChange)
{ {
if(scene())
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartEllipse::sceneSelectionChanged);
setSelected(false); //This item is removed from scene, then we deselect this, and so, the handlers is also removed. setSelected(false); //This item is removed from scene, then we deselect this, and so, the handlers is also removed.
} }
@@ -339,18 +319,6 @@ void PartEllipse::handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphi
m_vector_index = -1; m_vector_index = -1;
} }
/**
@brief PartEllipse::sceneSelectionChanged
When the scene selection change, if there are several primitive selected, we remove the handler of this item
*/
void PartEllipse::sceneSelectionChanged()
{
if (this->isSelected() && scene()->selectedItems().size() == 1)
addHandler();
else
removeHandler();
}
/** /**
@brief PartEllipse::addHandler @brief PartEllipse::addHandler
Add handlers for this item Add handlers for this item

View File

@@ -58,6 +58,9 @@ class PartEllipse : public AbstractPartEllipse
QPainterPath shadowShape() const override; QPainterPath shadowShape() const override;
void setRect(const QRectF &rect) override {AbstractPartEllipse::setRect(rect); adjusteHandlerPos();} void setRect(const QRectF &rect) override {AbstractPartEllipse::setRect(rect); adjusteHandlerPos();}
void addHandler() override;
void removeHandler() override;
protected: protected:
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
@@ -69,10 +72,6 @@ class PartEllipse : public AbstractPartEllipse
void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void sceneSelectionChanged ();
void addHandler();
void removeHandler();
private: private:
QPropertyUndoCommand *m_undo_command; QPropertyUndoCommand *m_undo_command;

View File

@@ -155,32 +155,12 @@ void PartLine::fromXml(const QDomElement &qde) {
*/ */
QVariant PartLine::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) QVariant PartLine::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{ {
if (change == ItemSelectedHasChanged && scene()) if (change == ItemPositionHasChanged)
{
if (value.toBool() == true)
{
//When item is selected, he must to be up to date whene the selection in the scene change, for display or not the handler,
//according to the number of selected items.
connect(scene(), &QGraphicsScene::selectionChanged, this, &PartLine::sceneSelectionChanged);
if (scene()->selectedItems().size() == 1)
addHandler();
}
else
{
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartLine::sceneSelectionChanged);
removeHandler();
}
}
else if (change == ItemPositionHasChanged)
{ {
adjusteHandlerPos(); adjusteHandlerPos();
} }
else if (change == ItemSceneChange) else if (change == ItemSceneChange)
{ {
if(scene())
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartLine::sceneSelectionChanged);
setSelected(false); //This is item removed from scene, then we deselect this, and so, the handlers is also removed. setSelected(false); //This is item removed from scene, then we deselect this, and so, the handlers is also removed.
} }
@@ -304,18 +284,6 @@ void PartLine::handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsS
m_vector_index = -1; m_vector_index = -1;
} }
/**
@brief PartLine::sceneSelectionChanged
When the scene selection change, if there are several primitive selected, we remove the handler of this item
*/
void PartLine::sceneSelectionChanged()
{
if (this->isSelected() && scene()->selectedItems().size() == 1)
addHandler();
else
removeHandler();
}
/** /**
@brief PartLine::addHandler @brief PartLine::addHandler
Add handlers for this item Add handlers for this item

View File

@@ -97,6 +97,9 @@ class PartLine : public CustomElementGraphicPart
void setRotation(qreal angle); void setRotation(qreal angle);
qreal rotation() const; qreal rotation() const;
void addHandler() override;
void removeHandler() override;
protected: protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override; bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
@@ -106,10 +109,6 @@ class PartLine : public CustomElementGraphicPart
void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void sceneSelectionChanged ();
void addHandler();
void removeHandler();
QPainterPath path() const; QPainterPath path() const;
QRectF firstEndCircleRect() const; QRectF firstEndCircleRect() const;

View File

@@ -316,32 +316,12 @@ qreal PartPolygon::rotation() const {
*/ */
QVariant PartPolygon::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) QVariant PartPolygon::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{ {
if (change == ItemSelectedHasChanged && scene()) if (change == ItemPositionHasChanged)
{
if (value.toBool() == true)
{
//When item is selected, he must to be up to date whene the selection in the scene change, for display or not the handler,
//according to the number of selected items.
connect(scene(), &QGraphicsScene::selectionChanged, this, &PartPolygon::sceneSelectionChanged);
if (scene()->selectedItems().size() == 1)
addHandler();
}
else
{
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartPolygon::sceneSelectionChanged);
removeHandler();
}
}
else if (change == ItemPositionHasChanged)
{ {
adjusteHandlerPos(); adjusteHandlerPos();
} }
else if (change == ItemSceneChange) else if (change == ItemSceneChange)
{ {
if(scene())
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartPolygon::sceneSelectionChanged);
setSelected(false); //This is item removed from scene, then we deselect this, and so, the handlers is also removed. setSelected(false); //This is item removed from scene, then we deselect this, and so, the handlers is also removed.
} }
@@ -484,18 +464,6 @@ void PartPolygon::handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphi
m_vector_index = -1; m_vector_index = -1;
} }
/**
@brief PartPolygon::sceneSelectionChanged
When the scene selection change, if there are several primitive selected, we remove the handler of this item
*/
void PartPolygon::sceneSelectionChanged()
{
if (this->isSelected() && scene()->selectedItems().size() == 1)
addHandler();
else
removeHandler();
}
/** /**
@brief PartPolygon::addHandler @brief PartPolygon::addHandler
Add handlers for this item Add handlers for this item

View File

@@ -90,6 +90,9 @@ class PartPolygon : public CustomElementGraphicPart
void setRotation (qreal angle); void setRotation (qreal angle);
qreal rotation () const; qreal rotation () const;
void addHandler() override;
void removeHandler() override;
protected: protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override; bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
@@ -100,10 +103,7 @@ class PartPolygon : public CustomElementGraphicPart
void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void sceneSelectionChanged ();
void addHandler();
void removeHandler();
void insertPoint(); void insertPoint();
void removePoint(); void removePoint();

View File

@@ -305,32 +305,12 @@ void PartRectangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
*/ */
QVariant PartRectangle::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) QVariant PartRectangle::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{ {
if (change == ItemSelectedHasChanged && scene()) if (change == ItemPositionHasChanged)
{
if (value.toBool() == true)
{
//When item is selected, he must to be up to date whene the selection in the scene change, for display or not the handler,
//according to the number of selected items.
connect(scene(), &QGraphicsScene::selectionChanged, this, &PartRectangle::sceneSelectionChanged);
if (scene()->selectedItems().size() == 1)
addHandler();
}
else
{
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartRectangle::sceneSelectionChanged);
removeHandler();
}
}
else if (change == ItemPositionHasChanged)
{ {
adjusteHandlerPos(); adjusteHandlerPos();
} }
else if (change == ItemSceneChange) else if (change == ItemSceneChange)
{ {
if(scene())
disconnect(scene(), &QGraphicsScene::selectionChanged, this, &PartRectangle::sceneSelectionChanged);
setSelected(false); //This item is removed from scene, then we deselect this, and so, the handlers is also removed. setSelected(false); //This item is removed from scene, then we deselect this, and so, the handlers is also removed.
} }
@@ -521,18 +501,6 @@ void PartRectangle::handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGrap
m_vector_index = -1; m_vector_index = -1;
} }
/**
@brief PartRectangle::sceneSelectionChanged
When the scene selection change, if there are several primitive selected, we remove the handler of this item
*/
void PartRectangle::sceneSelectionChanged()
{
if (this->isSelected() && scene()->selectedItems().size() == 1)
addHandler();
else
removeHandler();
}
/** /**
@brief PartRectangle::addHandler @brief PartRectangle::addHandler
Add handlers for this item Add handlers for this item

View File

@@ -85,6 +85,9 @@ class PartRectangle : public CustomElementGraphicPart
void startUserTransformation(const QRectF &) override; void startUserTransformation(const QRectF &) override;
void handleUserTransformation(const QRectF &, const QRectF &) override; void handleUserTransformation(const QRectF &, const QRectF &) override;
void addHandler() override;
void removeHandler() override;
protected: protected:
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
@@ -96,10 +99,6 @@ class PartRectangle : public CustomElementGraphicPart
void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event); void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
void sceneSelectionChanged ();
void addHandler();
void removeHandler();
private: private:
QRectF m_rect, QRectF m_rect,