Files
qelectrotech-source-mirror/sources/editor/UndoCommand/pastepartscommand.cpp
T
ispyisail 30e83d8c52 Element editor: give every primitive a uuid
Lines, rectangles, ellipses, arcs, polygons, PLC tables and static texts
now save a uuid, as terminals and dynamic texts already did, so a tool can
name one part of a symbol. The graphic parts keep it next to their styles,
since all of them save through stylesToXml(); a part read from a
definition that predates this gets one on the next save.

Paste renews them, as it already did for terminals -- and now for dynamic
texts too, which were pasted with their source's uuid. A placed element
gives its dynamic texts fresh uuids anyway, so only the definition's own
identity changes. PartTerminal::uuid() returns the terminal's saved uuid
rather than inheriting the graphic-part one it never writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 11:16:37 +13:00

135 lines
3.9 KiB
C++

/*
Copyright 2006-2026 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 "../graphicspart/partdynamictextfield.h"
#include "../graphicspart/partterminal.h"
#include "../graphicspart/parttext.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 and primitives stored in content_to_paste get a new
uuid in the constructor of this class to avoid have several parts
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();
}
//Same for the primitives, whose uuid would otherwise name both
//the original and its copy.
else if (qgi->type() == PartText::Type) {
static_cast<PartText *>(qgi)->setNewUuid();
}
//Only the definition's own identity: a placed element gives
//each of its dynamic texts a fresh uuid anyway (see
//Element::parseDynamicText()).
else if (qgi->type() == PartDynamicTextField::Type) {
static_cast<PartDynamicTextField *>(qgi)->setNewUuid();
}
else if (auto graphic_part = dynamic_cast<CustomElementGraphicPart *>(qgi)) {
graphic_part->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;
}