mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-18 13:30:34 +01:00
Element editor : improve code
Better use of signal partsAdded and partsRemoved
This commit is contained in:
84
sources/editor/UndoCommand/addpartcommand.cpp
Normal file
84
sources/editor/UndoCommand/addpartcommand.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
Copyright 2006-2022 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "addpartcommand.h"
|
||||
#include "../elementscene.h"
|
||||
|
||||
/**
|
||||
* @brief AddPartCommand::AddPartCommand
|
||||
* @param text : text of the undo command
|
||||
* @param scene : scene where @a part must be added
|
||||
* @param part : part to add
|
||||
* @param parent : parent undo command
|
||||
*/
|
||||
AddPartCommand::AddPartCommand(const QString &text,
|
||||
QPointer<ElementScene> scene,
|
||||
QGraphicsItem *part,
|
||||
QUndoCommand *parent) :
|
||||
QUndoCommand{parent},
|
||||
m_scene{scene},
|
||||
m_part{part}
|
||||
{
|
||||
setText(text);
|
||||
m_scene->qgiManager().manage(part);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief AddPartCommand::~AddPartCommand
|
||||
*/
|
||||
AddPartCommand::~AddPartCommand()
|
||||
{
|
||||
if (m_scene) {
|
||||
m_scene->qgiManager().release(m_part.first());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief AddPartCommand::undo
|
||||
*/
|
||||
void AddPartCommand::undo()
|
||||
{
|
||||
if (m_scene) {
|
||||
m_scene->removeItems(m_part);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief AddPartCommand::redo
|
||||
*/
|
||||
void AddPartCommand::redo()
|
||||
{
|
||||
if (!m_scene) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_first_redo)
|
||||
{
|
||||
auto part_{m_part.first()};
|
||||
if (!part_->zValue())
|
||||
{
|
||||
// the added part has no specific zValue already defined, we put it
|
||||
// above existing items (but still under terminals)
|
||||
const auto existing_items = m_scene->zItems(ElementScene::SortByZValue | ElementScene::SelectedOrNot);
|
||||
const auto z_{existing_items.count() ? existing_items.last()->zValue() + 1 : 1};
|
||||
part_->setZValue(z_);
|
||||
}
|
||||
m_scene->clearSelection();
|
||||
m_first_redo = false;
|
||||
}
|
||||
m_scene->addItems(m_part);
|
||||
}
|
||||
50
sources/editor/UndoCommand/addpartcommand.h
Normal file
50
sources/editor/UndoCommand/addpartcommand.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
Copyright 2006-2022 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef ADDPARTCOMMAND_H
|
||||
#define ADDPARTCOMMAND_H
|
||||
|
||||
#include <QUndoCommand>
|
||||
#include <QPointer>
|
||||
|
||||
class ElementScene;
|
||||
class QGraphicsItem;
|
||||
|
||||
/**
|
||||
* @brief The AddPartCommand class
|
||||
* Undo command use to add a graphics part into an element scene.
|
||||
*/
|
||||
class AddPartCommand : public QUndoCommand
|
||||
{
|
||||
public:
|
||||
AddPartCommand(const QString &text, QPointer<ElementScene> scene, QGraphicsItem *part, QUndoCommand *parent = nullptr);
|
||||
~AddPartCommand() override;
|
||||
|
||||
private:
|
||||
AddPartCommand(const AddPartCommand &);
|
||||
|
||||
public:
|
||||
void undo() override;
|
||||
void redo() override;
|
||||
|
||||
private:
|
||||
QPointer<ElementScene> m_scene;
|
||||
QVector<QGraphicsItem *> m_part;
|
||||
bool m_first_redo{true};
|
||||
};
|
||||
|
||||
#endif // ADDPARTCOMMAND_H
|
||||
@@ -31,11 +31,16 @@ OpenElmtCommand::OpenElmtCommand(const QDomDocument &document,
|
||||
setText(QObject::tr("Ouvrir un element"));
|
||||
}
|
||||
|
||||
OpenElmtCommand::~OpenElmtCommand()
|
||||
{
|
||||
if (m_scene) {
|
||||
m_scene->qgiManager().release(m_graphics_item);
|
||||
}
|
||||
}
|
||||
|
||||
void OpenElmtCommand::undo()
|
||||
{
|
||||
for (const auto &item : qAsConst(m_graphics_item)) {
|
||||
m_scene->removeItem(item);
|
||||
}
|
||||
m_scene->removeItems(m_graphics_item.toVector());
|
||||
}
|
||||
|
||||
void OpenElmtCommand::redo()
|
||||
@@ -47,6 +52,7 @@ void OpenElmtCommand::redo()
|
||||
if (m_first_redo)
|
||||
{
|
||||
m_scene->fromXml(m_document, QPointF(), true, &m_graphics_item);
|
||||
m_scene->qgiManager().manage(m_graphics_item);
|
||||
m_first_redo = false;
|
||||
|
||||
//m_document is now useless,
|
||||
@@ -54,9 +60,8 @@ void OpenElmtCommand::redo()
|
||||
m_document.clear();
|
||||
}
|
||||
else {
|
||||
for (const auto &item : qAsConst(m_graphics_item)) {
|
||||
m_scene->addItem(item);
|
||||
}
|
||||
m_scene->addItems(m_graphics_item.toVector());
|
||||
}
|
||||
|
||||
m_scene->slot_select(m_graphics_item);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ class OpenElmtCommand : public QUndoCommand
|
||||
{
|
||||
public:
|
||||
OpenElmtCommand(const QDomDocument &document, QPointer<ElementScene> scene, QUndoCommand *parent = nullptr);
|
||||
~OpenElmtCommand() override;
|
||||
|
||||
virtual void undo() override;
|
||||
virtual void redo() override;
|
||||
|
||||
@@ -203,58 +203,6 @@ void MovePartsCommand::redo()
|
||||
foreach(QGraphicsItem *qgi, moved_parts) qgi -> moveBy(movement.x(), movement.y());
|
||||
}
|
||||
|
||||
/*** AddPartCommand ***/
|
||||
/**
|
||||
Constructeur
|
||||
@param name Nom de la partie ajoutee
|
||||
@param scene ElementScene concernee
|
||||
@param p partie ajoutee
|
||||
@param parent QUndoCommand parent
|
||||
*/
|
||||
AddPartCommand::AddPartCommand(
|
||||
const QString &name,
|
||||
ElementScene *scene,
|
||||
QGraphicsItem *p,
|
||||
QUndoCommand *parent
|
||||
) :
|
||||
ElementEditionCommand(QString(QObject::tr("ajout %1", "undo caption")).arg(name), scene, nullptr, parent),
|
||||
part(p),
|
||||
first_redo(true)
|
||||
{
|
||||
m_scene -> qgiManager().manage(part);
|
||||
}
|
||||
|
||||
/// Destructeur
|
||||
AddPartCommand::~AddPartCommand()
|
||||
{
|
||||
m_scene -> qgiManager().release(part);
|
||||
}
|
||||
|
||||
/// Annule l'ajout
|
||||
void AddPartCommand::undo()
|
||||
{
|
||||
m_scene -> removeItem(part);
|
||||
}
|
||||
|
||||
/// Refait l'ajout
|
||||
void AddPartCommand::redo()
|
||||
{
|
||||
// le premier appel a redo, lors de la construction de l'objet, ne doit pas se faire
|
||||
if (first_redo) {
|
||||
if (!part -> zValue()) {
|
||||
// the added part has no specific zValue already defined, we put it
|
||||
// above existing items (but still under terminals)
|
||||
QList<QGraphicsItem *> existing_items = m_scene -> zItems(ElementScene::SortByZValue | ElementScene::SelectedOrNot);
|
||||
qreal z = existing_items.count() ? existing_items.last() -> zValue() + 1 : 1;
|
||||
part -> setZValue(z);
|
||||
}
|
||||
m_scene -> clearSelection();
|
||||
first_redo = false;
|
||||
return;
|
||||
}
|
||||
m_scene -> addItem(part);
|
||||
}
|
||||
|
||||
/**
|
||||
Constructeur
|
||||
@param element_scene Element edite
|
||||
|
||||
@@ -132,30 +132,6 @@ class MovePartsCommand : public ElementEditionCommand {
|
||||
bool first_redo;
|
||||
};
|
||||
|
||||
/**
|
||||
This command adds a primitive when editing an electrical element.
|
||||
*/
|
||||
class AddPartCommand : public ElementEditionCommand {
|
||||
// constructors, destructor
|
||||
public:
|
||||
AddPartCommand(const QString &, ElementScene *, QGraphicsItem *, QUndoCommand * = nullptr);
|
||||
~AddPartCommand() override;
|
||||
private:
|
||||
AddPartCommand(const AddPartCommand &);
|
||||
|
||||
// methods
|
||||
public:
|
||||
void undo() override;
|
||||
void redo() override;
|
||||
|
||||
// attributes
|
||||
private:
|
||||
/// Added primitive
|
||||
QGraphicsItem *part;
|
||||
/// Prevent the first call to redo()
|
||||
bool first_redo;
|
||||
};
|
||||
|
||||
/**
|
||||
This command changes the translated names of an electrical element.
|
||||
*/
|
||||
@@ -311,5 +287,4 @@ private:
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -119,7 +119,6 @@ void ElementScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
|
||||
if (m_event_interface->isFinish()) {
|
||||
delete m_event_interface;
|
||||
m_event_interface = nullptr;
|
||||
emit(partsAdded());
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -151,7 +150,6 @@ void ElementScene::mousePressEvent(QGraphicsSceneMouseEvent *e)
|
||||
if (m_event_interface->isFinish()) {
|
||||
delete m_event_interface;
|
||||
m_event_interface = nullptr;
|
||||
emit(partsAdded());
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -171,7 +169,6 @@ void ElementScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
|
||||
if (m_event_interface->isFinish()) {
|
||||
delete m_event_interface;
|
||||
m_event_interface = nullptr;
|
||||
emit(partsAdded());
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -199,7 +196,6 @@ void ElementScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
|
||||
if (m_event_interface->isFinish()) {
|
||||
delete m_event_interface;
|
||||
m_event_interface = nullptr;
|
||||
emit(partsAdded());
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -223,7 +219,6 @@ void ElementScene::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
delete m_event_interface;
|
||||
m_event_interface = nullptr;
|
||||
emit(partsAdded());
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -725,6 +720,38 @@ QETElementEditor* ElementScene::editor() const
|
||||
return m_element_editor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementScene::addItems
|
||||
* Add items to the scene and emit partsAdded.
|
||||
* Prefer always use this method instead of QGraphicsScene::addItem
|
||||
* even if you want to add one item, for gain the signal emission
|
||||
* @param items
|
||||
*/
|
||||
void ElementScene::addItems(QVector<QGraphicsItem *> items)
|
||||
{
|
||||
for (const auto &item : items) {
|
||||
addItem(item);
|
||||
}
|
||||
|
||||
emit partsAdded();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ElementScene::removeItems
|
||||
* Remove items from the scene and emit partsRemoved.
|
||||
* Prefer always use this method instead of QGraphicsScene::removeItem
|
||||
* even if you want to remove one item, for gain the signal emission
|
||||
* @param items
|
||||
*/
|
||||
void ElementScene::removeItems(QVector<QGraphicsItem *> items)
|
||||
{
|
||||
for (const auto &item : items) {
|
||||
removeItem(item);
|
||||
}
|
||||
|
||||
emit partsRemoved();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief ElementScene::slot_select
|
||||
Select the item in content,
|
||||
@@ -806,8 +833,7 @@ void ElementScene::slot_delete()
|
||||
m_undo_stack.push(new DeletePartsCommand(this, selected_items));
|
||||
|
||||
// removing items does not trigger QGraphicsScene::selectionChanged()
|
||||
emit(partsRemoved());
|
||||
emit(selectionChanged());
|
||||
emit selectionChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -37,6 +37,9 @@ class QKeyEvent;
|
||||
This class is the canvas allowing the visual edition of an electrial element.
|
||||
It displays the various primitives composing the drawing of the element,
|
||||
the border due to its fixed size and its hotspot.
|
||||
|
||||
For add and remove item prefer use custom method addItems and removeItems instead of
|
||||
addItem and removeItem, because these methods emit signal partAdded and partRemoved.
|
||||
*/
|
||||
class ElementScene : public QGraphicsScene
|
||||
{
|
||||
@@ -124,6 +127,8 @@ class ElementScene : public QGraphicsScene
|
||||
void cut();
|
||||
void copy();
|
||||
QETElementEditor* editor() const;
|
||||
void addItems(QVector<QGraphicsItem *> items);
|
||||
void removeItems(QVector<QGraphicsItem *> items);
|
||||
|
||||
protected:
|
||||
void mouseMoveEvent (QGraphicsSceneMouseEvent *) override;
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
#include "eseventaddarc.h"
|
||||
|
||||
#include "../editorcommands.h"
|
||||
#include "../UndoCommand/addpartcommand.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../graphicspart/partarc.h"
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
#include "eseventadddynamictextfield.h"
|
||||
|
||||
#include "../editorcommands.h"
|
||||
#include "../UndoCommand/addpartcommand.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../graphicspart/partdynamictextfield.h"
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
#include "eseventaddellipse.h"
|
||||
|
||||
#include "../editorcommands.h"
|
||||
#include "../UndoCommand/addpartcommand.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../graphicspart/partellipse.h"
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
#include "eseventaddline.h"
|
||||
|
||||
#include "../editorcommands.h"
|
||||
#include "../UndoCommand/addpartcommand.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../graphicspart/partline.h"
|
||||
#include "../ui/qetelementeditor.h"
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
#include "eseventaddpolygon.h"
|
||||
|
||||
#include "../editorcommands.h"
|
||||
#include "../UndoCommand/addpartcommand.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../graphicspart/partpolygon.h"
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
#include "eseventaddrect.h"
|
||||
|
||||
#include "../editorcommands.h"
|
||||
#include "../UndoCommand/addpartcommand.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../graphicspart/partrectangle.h"
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
#include "eseventaddterminal.h"
|
||||
|
||||
#include "../editorcommands.h"
|
||||
#include "../UndoCommand/addpartcommand.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../graphicspart/partterminal.h"
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
#include "eseventaddtext.h"
|
||||
|
||||
#include "../editorcommands.h"
|
||||
#include "../UndoCommand/addpartcommand.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../graphicspart/parttext.h"
|
||||
|
||||
|
||||
@@ -1130,7 +1130,6 @@ void QETElementEditor::setupConnection()
|
||||
});
|
||||
|
||||
connect(&(m_elmt_scene -> undoStack()), &QUndoStack::indexChanged, [this]() {
|
||||
this->updatePartsList();
|
||||
this->updateInformations();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user