Element editor : improve code

Better use of signal partsAdded and partsRemoved
This commit is contained in:
joshua
2022-07-26 16:17:47 +02:00
parent cae7cbdbd8
commit 5a497cd6b5
17 changed files with 192 additions and 99 deletions

View 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);
}

View 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

View File

@@ -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);
}

View File

@@ -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;