Element editor : a copied/pasted terminal part have a new uuid.

This commit is contained in:
Claveau Joshua
2020-08-12 15:45:48 +02:00
parent 1805ef300e
commit 86766a9090
7 changed files with 178 additions and 106 deletions

View File

@@ -0,0 +1,114 @@
/*
Copyright 2006-2020 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 "pastepartscommand.h"
#include "elementview.h"
#include "partterminal.h"
/**
* @brief PastePartsCommand::PastePartsCommand
* @param view : view where this command work
* @param content_to_paste : content to paste
* @param parent : parent undo command
*
* Note : all terminal stored in @content_to_paste get a new uuid in the constructor of this class to avoid have
* several terminal of an element with the same uuid.
*/
PastePartsCommand::PastePartsCommand(ElementView *view, const ElementContent &content_to_paste, QUndoCommand *parent) :
ElementEditionCommand(view ? view -> scene() : nullptr, view, parent)
{
for (auto qgi : content_to_paste)
{
if (qgi->type() == PartTerminal::Type) {
auto part_terminal = static_cast<PartTerminal*>(qgi);
part_terminal->setNewUuid();
}
m_pasted_content.append(qgi);
}
setText(QObject::tr("Coller"));
m_scene->qgiManager().manage(m_pasted_content);
}
/**
* @brief PastePartsCommand::~PastePartsCommand
*/
PastePartsCommand::~PastePartsCommand()
{
m_scene->qgiManager().release(m_pasted_content);
}
/**
* @brief PastePartsCommand::undo
*/
void PastePartsCommand::undo()
{
m_scene->blockSignals(true);
for (auto qgi : m_pasted_content) {
m_scene->removeItem(qgi);
}
m_scene->blockSignals(false);
if (m_uses_offset)
{
m_view->offset_paste_count_ = m_old_offset_paste_count;
m_view->start_top_left_corner_ = m_old_start_top_left_corner;
}
m_view->adjustSceneRect();
}
/**
* @brief PastePartsCommand::redo
*/
void PastePartsCommand::redo()
{
if (m_first_redo) {
m_first_redo = false;
} else {
m_scene->blockSignals(true);
for (auto qgi : m_pasted_content) {
m_scene->addItem(qgi);
}
m_scene->blockSignals(false);
if (m_uses_offset)
{
m_view->offset_paste_count_ = m_new_offset_paste_count;
m_view->start_top_left_corner_ = m_new_start_top_left_corner;
}
}
m_scene->slot_select(m_pasted_content);
m_view->adjustSceneRect();
}
/**
* @brief PastePartsCommand::setOffset
* Describe the offset to use with this undo command
* @param old_offset_paste_count
* @param old_start_top_left_corner
* @param new_offset_paste_count
* @param new_start_top_left_corner
*/
void PastePartsCommand::setOffset(int old_offset_paste_count, const QPointF &old_start_top_left_corner, int new_offset_paste_count, const QPointF &new_start_top_left_corner)
{
m_old_offset_paste_count = old_offset_paste_count;
m_old_start_top_left_corner = old_start_top_left_corner;
m_new_offset_paste_count = new_offset_paste_count;
m_new_start_top_left_corner = new_start_top_left_corner;
m_uses_offset = true;
}

View File

@@ -0,0 +1,56 @@
/*
Copyright 2006-2020 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 PASTEPARTSCOMMAND_H
#define PASTEPARTSCOMMAND_H
#include "editorcommands.h"
#include "elementcontent.h"
class ElementView;
/**
* @brief The PastePartsCommand class
* Undo command for paste element primitive in an element editor
*/
class PastePartsCommand : public ElementEditionCommand
{
public:
PastePartsCommand(ElementView *view, const ElementContent &content_to_paste, QUndoCommand *parent = nullptr);
~PastePartsCommand() override;
private:
PastePartsCommand(const PastePartsCommand &);
public:
void undo() override;
void redo() override;
virtual void setOffset(int old_offset_paste_count, const QPointF &old_start_top_left_corner, int new_offset_paste_count, const QPointF &new_start_top_left_corner);
private:
ElementContent m_pasted_content;
int m_old_offset_paste_count,
m_new_offset_paste_count;
QPointF m_old_start_top_left_corner,
m_new_start_top_left_corner;
bool m_uses_offset = false,
m_first_redo = true;
};
#endif // PASTEPARTSCOMMAND_H

View File

@@ -119,79 +119,6 @@ void DeletePartsCommand::redo() {
m_scene -> blockSignals(false);
}
/*** CutPartsCommand ***/
/**
Constructeur
@param view ElementView concernee
@param c Liste des parties collees
@param parent QUndoCommand parent
*/
PastePartsCommand::PastePartsCommand(
ElementView *view,
const ElementContent &c,
QUndoCommand *parent
) :
ElementEditionCommand(view ? view -> scene() : nullptr, view, parent),
content_(c),
uses_offset(false),
first_redo(true)
{
setText(QObject::tr("coller"));
m_scene -> qgiManager().manage(content_);
}
/// Destructeur
PastePartsCommand::~PastePartsCommand() {
m_scene -> qgiManager().release(content_);
}
/// annule le coller
void PastePartsCommand::undo() {
// enleve les parties
m_scene -> blockSignals(true);
foreach(QGraphicsItem *part, content_) {
m_scene -> removeItem(part);
}
m_scene -> blockSignals(false);
if (uses_offset) {
m_view -> offset_paste_count_ = old_offset_paste_count_;
m_view -> start_top_left_corner_ = old_start_top_left_corner_;
}
m_view -> adjustSceneRect();
}
/// refait le coller
void PastePartsCommand::redo() {
if (first_redo) first_redo = false;
else {
// pose les parties
m_scene -> blockSignals(true);
foreach(QGraphicsItem *part, content_) {
m_scene -> addItem(part);
}
m_scene -> blockSignals(false);
if (uses_offset) {
m_view -> offset_paste_count_ = new_offset_paste_count_;
m_view -> start_top_left_corner_ = new_start_top_left_corner_;
}
}
m_scene -> slot_select(content_);
m_view -> adjustSceneRect();
}
/**
Indique a cet objet d'annulation que le c/c a annuler ou refaire etait un
c/c avec decalage ; il faut plus d'informations pour annuler ce type de
collage.
*/
void PastePartsCommand::setOffset(int old_offset_pc, const QPointF &old_start_tlc, int new_offset_pc, const QPointF &new_start_tlc) {
old_offset_paste_count_ = old_offset_pc;
old_start_top_left_corner_ = old_start_tlc;
new_offset_paste_count_ = new_offset_pc;
new_start_top_left_corner_ = new_start_tlc;
uses_offset = true;
}
/*** CutPartsCommand ***/
/**
Constructeur

View File

@@ -76,37 +76,6 @@ class DeletePartsCommand : public ElementEditionCommand {
QList<QGraphicsItem *> deleted_parts;
};
/**
This command pastes primitives when editing an electrical element.
*/
class PastePartsCommand : public ElementEditionCommand {
// constructors, destructor
public:
PastePartsCommand(ElementView *, const ElementContent &, QUndoCommand * = nullptr);
~PastePartsCommand() override;
private:
PastePartsCommand(const PastePartsCommand &);
// methods
public:
void undo() override;
void redo() override;
virtual void setOffset(int, const QPointF &, int, const QPointF &);
// attributes
private:
/// Pasted content
ElementContent content_;
/// Data required to undo a copy/paste with offset
int old_offset_paste_count_;
QPointF old_start_top_left_corner_;
int new_offset_paste_count_;
QPointF new_start_top_left_corner_;
bool uses_offset;
/// Prevent the first call to redo()
bool first_redo;
};
/**
This command cut primitives when editing an electrical element.
*/

View File

@@ -17,8 +17,8 @@
*/
#include "elementview.h"
#include "qetelementeditor.h"
#include "editorcommands.h"
#include "qetapp.h"
#include "pastepartscommand.h"
/**
Constructeur
@param scene ElementScene visualisee par cette ElementView

View File

@@ -144,6 +144,11 @@ void PartTerminal::setName(QString& name)
emit nameChanged();
}
void PartTerminal::setNewUuid()
{
d->m_uuid = QUuid::createUuid();
}
/**
Met a jour la position du second point en fonction de la position et de
l'orientation de la borne.

View File

@@ -73,6 +73,7 @@ class PartTerminal : public CustomElementGraphicPart
void setOrientation(Qet::Orientation ori);
void setName(QString& name);
void setNewUuid();
private:
void updateSecondPoint();