Element editor : add two new actions in context menu for insert or remove point of a selected polygon.

All parts items : remove the 'open hand cursor' when hover a selected part.


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5419 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun 2018-06-30 21:41:27 +00:00
parent 114c3cdcc3
commit 28faaf12c5
9 changed files with 176 additions and 38 deletions

View File

@ -8,6 +8,7 @@
* Context menu display only enabled actions.
* Add new feature -> alignment.
* Alignment of text field can be edited.
* Add two new actions in context menu for insert or remove point of a selected polygon.
* Diagram editor :
* Conductors can now be drawn with two colors.

View File

@ -240,6 +240,10 @@ void ElementScene::keyPressEvent(QKeyEvent *event)
*/
void ElementScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
QGraphicsScene::contextMenuEvent(event);
if(event->isAccepted())
return;
if (m_behavior == ElementScene::Normal)
m_element_editor -> contextMenu(event->screenPos());
}
@ -302,11 +306,14 @@ void ElementScene::clearEventInterface()
* Modifie the current behavior of this scene
* @param b
*/
void ElementScene::setBehavior(ElementScene::Behavior b)
{
void ElementScene::setBehavior(ElementScene::Behavior b) {
m_behavior = b;
}
ElementScene::Behavior ElementScene::behavior() const {
return m_behavior;
}
/**
@return la taille horizontale de la grille
*/

View File

@ -91,6 +91,7 @@ class ElementScene : public QGraphicsScene
void setEventInterface (ESEventInterface *event_interface);
void clearEventInterface();
void setBehavior (ElementScene::Behavior);
ElementScene::Behavior behavior() const;
QPointF snapToGrid(QPointF point);
void setNames(const NamesList &);
NamesList names() const;

View File

@ -424,14 +424,6 @@ void CustomElementGraphicPart::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
QGraphicsObject::hoverEnterEvent(event);
}
void CustomElementGraphicPart::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
if (isSelected())
setCursor(Qt::OpenHandCursor);
QGraphicsObject::hoverMoveEvent(event);
}
/**
* @brief CustomElementGraphicPart::hoverLeaveEvent
* Reimplemented from QGraphicsObject.
@ -441,7 +433,6 @@ void CustomElementGraphicPart::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
void CustomElementGraphicPart::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
m_hovered = false;
unsetCursor();
QGraphicsObject::hoverLeaveEvent(event);
}

View File

@ -103,7 +103,6 @@ class CustomElementGraphicPart : public QGraphicsObject, public CustomElementPar
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
void hoverMoveEvent (QGraphicsSceneHoverEvent *event) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;

View File

@ -19,6 +19,8 @@
#include "QPropertyUndoCommand/qpropertyundocommand.h"
#include "elementscene.h"
#include "QetGraphicsItemModeler/qetgraphicshandleritem.h"
#include "qetelementeditor.h"
#include "qeticons.h"
/**
@ -31,7 +33,14 @@ PartPolygon::PartPolygon(QETElementEditor *editor, QGraphicsItem *parent) :
CustomElementGraphicPart(editor, parent),
m_closed(false),
m_undo_command(nullptr)
{}
{
m_insert_point = new QAction(tr("Ajouter un point"), this);
m_insert_point->setIcon(QET::Icons::Add);
connect(m_insert_point, &QAction::triggered, this, &PartPolygon::insertPoint);
m_remove_point = new QAction(tr("Supprimer ce point"), this);
m_remove_point->setIcon(QET::Icons::Remove);
connect(m_remove_point, &QAction::triggered, this, &PartPolygon::removePoint);
}
/**
* @brief PartPolygon::~PartPolygon
@ -327,6 +336,30 @@ bool PartPolygon::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
return false;
}
void PartPolygon::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
m_context_menu_pos = event->pos();
event->ignore();
if (isSelected() && elementScene() && (elementScene()->behavior() == ElementScene::Normal))
{
QList<QAction *> list;
list << m_insert_point;
if (m_handler_vector.count() > 2)
{
for (QetGraphicsHandlerItem *qghi : m_handler_vector)
{
if (qghi->contains(qghi->mapFromScene(event->scenePos())))
{
list << m_remove_point;
break;
}
}
}
elementScene()->editor()->contextMenu(event->screenPos(), list);
event->accept();
}
}
/**
* @brief PartPolygon::adjusteHandlerPos
*/
@ -341,6 +374,12 @@ void PartPolygon::adjusteHandlerPos()
for (int i = 0 ; i < points_vector.size() ; ++i)
m_handler_vector.at(i)->setPos(points_vector.at(i));
}
else
{
qDeleteAll(m_handler_vector);
m_handler_vector.clear();
addHandler();
}
}
/**
@ -438,6 +477,95 @@ void PartPolygon::removeHandler()
}
}
/**
* @brief PartPolygon::insertPoint
* Insert a point in this polygone
*/
void PartPolygon::insertPoint()
{
qreal max_angle = 0;
int index = 0;
for (int i=1 ; i<m_polygon.size() ; i++)
{
QPointF A = m_polygon.at(i-1);
QPointF B = m_polygon.at(i);
QLineF line_a(A, m_context_menu_pos);
QLineF line_b(m_context_menu_pos, B);
qreal angle = line_a.angleTo(line_b);
if(angle<180)
angle = 360-angle;
if (i==1)
{
max_angle = angle;
index=i;
}
if (angle > max_angle)
{
max_angle = angle;
index=i;
}
}
//Special case when polygon is close
if (m_closed)
{
QLineF line_a(m_polygon.last(), m_context_menu_pos);
QLineF line_b(m_context_menu_pos, m_polygon.first());
qreal angle = line_a.angleTo(line_b);
if (angle<180)
angle = 360-angle;
if (angle > max_angle)
{
max_angle = angle;
index=m_polygon.size();
}
}
QPolygonF polygon = this->polygon();
polygon.insert(index, elementScene()->snapToGrid(m_context_menu_pos));
//Wrap the undo for avoid to merge the undo commands when user add several points.
QUndoCommand *undo = new QUndoCommand(tr("Ajouter un point à un polygone"));
new QPropertyUndoCommand(this, "polygon", this->polygon(), polygon, undo);
elementScene()->undoStack().push(undo);
}
/**
* @brief PartPolygon::removePoint
* remove a point on this polygon
*/
void PartPolygon::removePoint()
{
if (m_handler_vector.size() == 2)
return;
QPointF point = mapToScene(m_context_menu_pos);
int index = -1;
for (int i=0 ; i<m_handler_vector.size() ; i++)
{
QetGraphicsHandlerItem *qghi = m_handler_vector.at(i);
if (qghi->contains(qghi->mapFromScene(point)))
{
index = i;
break;
}
}
if (index > -1 && index<m_handler_vector.count())
{
QPolygonF polygon = this->polygon();
polygon.removeAt(index);
//Wrap the undo for avoid to merge the undo commands when user add several points.
QUndoCommand *undo = new QUndoCommand(tr("Supprimer un point d'un polygone"));
new QPropertyUndoCommand(this, "polygon", this->polygon(), polygon, undo);
elementScene()->undoStack().push(undo);
}
}
/**
* @brief PartPolygon::shape
* @return the shape of this item

View File

@ -23,6 +23,7 @@
class QPropertyUndoCommand;
class QetGraphicsHandlerItem;
class QAction;
/**
* @brief The PartPolygon class
@ -86,6 +87,7 @@ class PartPolygon : public CustomElementGraphicPart
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
private:
void adjusteHandlerPos();
@ -96,6 +98,8 @@ class PartPolygon : public CustomElementGraphicPart
void addHandler();
void removeHandler();
void insertPoint();
void removePoint();
bool m_closed;
@ -104,5 +108,8 @@ class PartPolygon : public CustomElementGraphicPart
QPropertyUndoCommand *m_undo_command;
int m_vector_index = -1;
QVector<QetGraphicsHandlerItem *> m_handler_vector;
QAction *m_insert_point,
*m_remove_point;
QPointF m_context_menu_pos;
};
#endif

View File

@ -400,12 +400,16 @@ void QETElementEditor::setupMenus() {
}
/**
* @brief QETElementEditor::contextMenuEvent
* @param event
* @brief QETElementEditor::contextMenu
* Display a context menu, with all available action.
* @param p, the pos of the menu, in screen coordinate
* @param actions, a list of actions who can be prepended to the context menu.
*/
void QETElementEditor::contextMenu(QPoint p)
void QETElementEditor::contextMenu(QPoint p, QList<QAction *> actions)
{
QMenu menu(this);
menu.addActions(actions);
menu.addSeparator();
menu.addAction(undo);
menu.addAction(redo);
menu.addAction(selectall);
@ -423,14 +427,14 @@ void QETElementEditor::contextMenu(QPoint p)
menu.addActions(m_depth_action_group -> actions());
//Remove from the context menu the actions which are disabled.
const QList<QAction *>actions = menu.actions();
for(QAction *action : actions)
const QList<QAction *>menu_actions = menu.actions();
for(QAction *action : menu_actions)
{
if(!action->isEnabled())
menu.removeAction(action);
}
menu.exec(p);
}
}
/**

View File

@ -110,7 +110,7 @@ class QETElementEditor : public QETMainWindow {
void writeSettings();
static QPointF pasteOffset();
static QString getOpenElementFileName(QWidget * = nullptr, const QString & = QString());
void contextMenu(QPoint p);
void contextMenu(QPoint p, QList<QAction *> actions = QList<QAction*>());
signals:
void saveToLocation(ElementsLocation loc);