mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-20 08:10:52 +01:00
Element editor : improve code
Better use of signal partsAdded and partsRemoved
This commit is contained in:
66
sources/editor/UndoCommand/deletepartscommand.cpp
Normal file
66
sources/editor/UndoCommand/deletepartscommand.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
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 "deletepartscommand.h"
|
||||
#include "../elementscene.h"
|
||||
|
||||
/**
|
||||
* @brief DeletePartsCommand::DeletePartsCommand
|
||||
* @param scene
|
||||
* @param parts
|
||||
* @param parent
|
||||
*/
|
||||
DeletePartsCommand::DeletePartsCommand(QPointer<ElementScene> scene,
|
||||
const QVector<QGraphicsItem *> &parts,
|
||||
QUndoCommand *parent) :
|
||||
QUndoCommand{parent},
|
||||
m_scene{scene},
|
||||
m_parts{parts}
|
||||
{
|
||||
setText(QObject::tr("suppression", "undo caption"));
|
||||
m_scene->qgiManager().manage(parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DeletePartsCommand::~DeletePartsCommand
|
||||
*/
|
||||
DeletePartsCommand::~DeletePartsCommand()
|
||||
{
|
||||
if (m_scene) {
|
||||
m_scene->qgiManager().release(m_parts);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DeletePartsCommand::undo
|
||||
*/
|
||||
void DeletePartsCommand::undo()
|
||||
{
|
||||
if (m_scene) {
|
||||
m_scene->addItems(m_parts);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DeletePartsCommand::redo
|
||||
*/
|
||||
void DeletePartsCommand::redo()
|
||||
{
|
||||
if (m_scene) {
|
||||
m_scene->removeItems(m_parts);
|
||||
}
|
||||
}
|
||||
48
sources/editor/UndoCommand/deletepartscommand.h
Normal file
48
sources/editor/UndoCommand/deletepartscommand.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
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 DELETEPARTSCOMMAND_H
|
||||
#define DELETEPARTSCOMMAND_H
|
||||
|
||||
#include <QUndoCommand>
|
||||
#include <QPointer>
|
||||
|
||||
class ElementScene;
|
||||
class QGraphicsItem;
|
||||
|
||||
/**
|
||||
* @brief The DeletePartsCommand class
|
||||
* Class used to remove part from an element scene.
|
||||
*/
|
||||
class DeletePartsCommand : public QUndoCommand
|
||||
{
|
||||
public:
|
||||
DeletePartsCommand(QPointer<ElementScene> scene, const QVector<QGraphicsItem *> &parts, QUndoCommand *parent = nullptr);
|
||||
~DeletePartsCommand() override;
|
||||
private:
|
||||
DeletePartsCommand(const DeletePartsCommand &);
|
||||
|
||||
public:
|
||||
void undo() override;
|
||||
void redo() override;
|
||||
|
||||
private:
|
||||
QPointer<ElementScene> m_scene;
|
||||
QVector<QGraphicsItem *> m_parts;
|
||||
};
|
||||
|
||||
#endif // DELETEPARTSCOMMAND_H
|
||||
@@ -90,54 +90,6 @@ void ElementEditionCommand::setElementView(ElementView *view) {
|
||||
m_view = view;
|
||||
}
|
||||
|
||||
/*** DeletePartsCommand ***/
|
||||
/**
|
||||
Constructeur
|
||||
@param scene ElementScene concernee
|
||||
@param parts Liste des parties supprimees
|
||||
@param parent QUndoCommand parent
|
||||
*/
|
||||
DeletePartsCommand::DeletePartsCommand(
|
||||
ElementScene *scene,
|
||||
const QList<QGraphicsItem *>& parts,
|
||||
QUndoCommand *parent
|
||||
) :
|
||||
ElementEditionCommand(QObject::tr("suppression", "undo caption"), scene, nullptr, parent),
|
||||
deleted_parts(parts)
|
||||
{
|
||||
foreach(QGraphicsItem *qgi, deleted_parts) {
|
||||
m_scene -> qgiManager().manage(qgi);
|
||||
}
|
||||
}
|
||||
|
||||
/// Destructeur : detruit egalement les parties supprimees
|
||||
DeletePartsCommand::~DeletePartsCommand()
|
||||
{
|
||||
foreach(QGraphicsItem *qgi, deleted_parts) {
|
||||
m_scene -> qgiManager().release(qgi);
|
||||
}
|
||||
}
|
||||
|
||||
/// Restaure les parties supprimees
|
||||
void DeletePartsCommand::undo()
|
||||
{
|
||||
m_scene -> blockSignals(true);
|
||||
foreach(QGraphicsItem *qgi, deleted_parts) {
|
||||
m_scene -> addItem(qgi);
|
||||
}
|
||||
m_scene -> blockSignals(false);
|
||||
}
|
||||
|
||||
/// Supprime les parties
|
||||
void DeletePartsCommand::redo()
|
||||
{
|
||||
m_scene -> blockSignals(true);
|
||||
foreach(QGraphicsItem *qgi, deleted_parts) {
|
||||
m_scene -> removeItem(qgi);
|
||||
}
|
||||
m_scene -> blockSignals(false);
|
||||
}
|
||||
|
||||
/*** CutPartsCommand ***/
|
||||
/**
|
||||
Constructeur
|
||||
@@ -150,7 +102,7 @@ CutPartsCommand::CutPartsCommand(
|
||||
const QList<QGraphicsItem *>& parts,
|
||||
QUndoCommand *parent
|
||||
) :
|
||||
DeletePartsCommand(scene, parts, parent)
|
||||
DeletePartsCommand(scene, parts.toVector(), parent)
|
||||
{
|
||||
setText(QString(QObject::tr("couper des parties", "undo caption")));
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include "graphicspart/partterminal.h"
|
||||
#include "graphicspart/parttext.h"
|
||||
#include "../QPropertyUndoCommand/qpropertyundocommand.h"
|
||||
#include "UndoCommand/deletepartscommand.h"
|
||||
|
||||
|
||||
/**
|
||||
@@ -71,29 +72,6 @@ class ElementEditionCommand : public QUndoCommand
|
||||
ElementView *m_view;
|
||||
};
|
||||
|
||||
/**
|
||||
This command deletes one or several primitives/parts when editing an
|
||||
electrical element.
|
||||
*/
|
||||
class DeletePartsCommand : public ElementEditionCommand {
|
||||
// constructors, destructor
|
||||
public:
|
||||
DeletePartsCommand(ElementScene *, const QList<QGraphicsItem *>&, QUndoCommand * = nullptr);
|
||||
~DeletePartsCommand() override;
|
||||
private:
|
||||
DeletePartsCommand(const DeletePartsCommand &);
|
||||
|
||||
// methods
|
||||
public:
|
||||
void undo() override;
|
||||
void redo() override;
|
||||
|
||||
// attributes
|
||||
private:
|
||||
/// Deleted primitives
|
||||
QList<QGraphicsItem *> deleted_parts;
|
||||
};
|
||||
|
||||
/**
|
||||
This command cut primitives when editing an electrical element.
|
||||
*/
|
||||
|
||||
@@ -823,13 +823,11 @@ void ElementScene::slot_invertSelection()
|
||||
*/
|
||||
void ElementScene::slot_delete()
|
||||
{
|
||||
// check that there is something selected
|
||||
// verifie qu'il y a qqc de selectionne
|
||||
QList<QGraphicsItem *> selected_items = selectedItems();
|
||||
if (selected_items.isEmpty()) return;
|
||||
const auto selected_items{selectedItems().toVector()};
|
||||
if (selected_items.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// erase everything that is selected
|
||||
// efface tout ce qui est selectionne
|
||||
m_undo_stack.push(new DeletePartsCommand(this, selected_items));
|
||||
|
||||
// removing items does not trigger QGraphicsScene::selectionChanged()
|
||||
|
||||
@@ -86,6 +86,18 @@ void QGIManager::release(const QList<QGraphicsItem *> &qgis) {
|
||||
foreach(QGraphicsItem *qgi, qgis) release(qgi);
|
||||
}
|
||||
|
||||
void QGIManager::manage(const QVector<QGraphicsItem *> &items) {
|
||||
for (const auto &qgi : items) {
|
||||
manage(qgi);
|
||||
}
|
||||
}
|
||||
|
||||
void QGIManager::release(const QVector<QGraphicsItem *> &items) {
|
||||
for (const auto &qgi : items) {
|
||||
release(qgi);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Indique au QGIManager de detruire les QGraphicsItem restants lors de sa
|
||||
destruction si ceux-ci n'appartiennent pas a la scene
|
||||
|
||||
@@ -42,8 +42,12 @@ class QGIManager {
|
||||
public:
|
||||
void manage(QGraphicsItem *);
|
||||
void release(QGraphicsItem *);
|
||||
QT_DEPRECATED_X("Use QGIManager::manage(const QVector<QGraphicsItem *> &) instead")
|
||||
void manage(const QList<QGraphicsItem *> &);
|
||||
QT_DEPRECATED_X("Use QGIManager::release(const QVector<QGraphicsItem *> &) instead")
|
||||
void release(const QList<QGraphicsItem *> &);
|
||||
void manage(const QVector<QGraphicsItem *> &items);
|
||||
void release(const QVector<QGraphicsItem *> &items);
|
||||
void setDestroyQGIOnDelete(bool);
|
||||
bool manages(QGraphicsItem *) const;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user