2010-05-08 21:24:43 +00:00
|
|
|
/*
|
2023-01-01 17:05:57 +01:00
|
|
|
Copyright 2006-2023 The QElectroTech Team
|
2010-05-08 21:24:43 +00:00
|
|
|
This file is part of QElectroTech.
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
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.
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
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.
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "elementsmover.h"
|
2020-12-08 19:57:35 +01:00
|
|
|
|
|
|
|
#include "conductorautonumerotation.h"
|
2010-05-08 21:24:43 +00:00
|
|
|
#include "diagram.h"
|
2020-12-08 19:57:35 +01:00
|
|
|
#include "qetgraphicsitem/conductor.h"
|
2021-02-07 16:20:55 +01:00
|
|
|
#include "diagramcommands.h"
|
2020-12-08 19:57:35 +01:00
|
|
|
#include "qetgraphicsitem/conductortextitem.h"
|
|
|
|
#include "qetgraphicsitem/diagramimageitem.h"
|
|
|
|
#include "qetgraphicsitem/dynamicelementtextitem.h"
|
|
|
|
#include "qetgraphicsitem/element.h"
|
|
|
|
#include "qetgraphicsitem/elementtextitemgroup.h"
|
|
|
|
#include "qetgraphicsitem/independenttextitem.h"
|
2021-02-07 16:20:55 +01:00
|
|
|
#include "undocommand/addgraphicsobjectcommand.h"
|
2022-11-04 20:25:49 +01:00
|
|
|
#include "qetapp.h"
|
|
|
|
#include "qetdiagrameditor.h"
|
2010-05-08 21:24:43 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementsMover::ElementsMover Constructor
|
|
|
|
*/
|
2022-11-05 13:36:46 +01:00
|
|
|
ElementsMover::ElementsMover(){}
|
2010-05-08 21:24:43 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementsMover::~ElementsMover Destructor
|
|
|
|
*/
|
2022-11-05 13:36:46 +01:00
|
|
|
ElementsMover::~ElementsMover(){}
|
2010-05-08 21:24:43 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementsMover::isReady
|
|
|
|
@return True if this element mover is ready to be used.
|
|
|
|
A element mover is ready when the previous managed movement is finish.
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
bool ElementsMover::isReady() const
|
|
|
|
{
|
2022-11-05 13:36:46 +01:00
|
|
|
return(!m_movement_running);
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementsMover::beginMovement
|
|
|
|
Start a new movement
|
|
|
|
@param diagram diagram where the movement is applied
|
|
|
|
@param driver_item item moved by mouse and don't be moved by Element mover
|
|
|
|
@return the numbers of items to be moved or -1 if movement can't be init.
|
|
|
|
*/
|
2019-03-16 11:34:52 +00:00
|
|
|
int ElementsMover::beginMovement(Diagram *diagram, QGraphicsItem *driver_item)
|
|
|
|
{
|
|
|
|
// They must be no movement in progress
|
2022-11-05 13:36:46 +01:00
|
|
|
if (m_movement_running) return(-1);
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2019-03-16 11:34:52 +00:00
|
|
|
// Be sure we have diagram to work
|
2010-05-08 21:24:43 +00:00
|
|
|
if (!diagram) return(-1);
|
2022-11-05 13:36:46 +01:00
|
|
|
m_diagram = diagram;
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2022-11-04 20:25:49 +01:00
|
|
|
if (!diagram->views().isEmpty()) {
|
|
|
|
const auto qde = QETApp::diagramEditorAncestorOf(diagram->views().at(0));
|
|
|
|
if (qde) {
|
|
|
|
m_status_bar = qde->statusBar();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_status_bar.clear();
|
|
|
|
}
|
|
|
|
|
2019-03-16 11:34:52 +00:00
|
|
|
// Take count of driver item
|
2017-12-05 20:51:54 +00:00
|
|
|
m_movement_driver = driver_item;
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2019-03-16 11:34:52 +00:00
|
|
|
// At the beginning of movement, move is NULL
|
2022-11-08 18:17:55 +01:00
|
|
|
m_current_movement -= m_current_movement;
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2017-12-05 20:51:54 +00:00
|
|
|
m_moved_content = DiagramContent(diagram);
|
|
|
|
m_moved_content.removeNonMovableItems();
|
2014-10-17 21:30:42 +00:00
|
|
|
|
2022-11-16 19:06:56 +01:00
|
|
|
//Remove element text and text group, if the parent element is selected.
|
|
|
|
const auto element_text{m_moved_content.m_element_texts};
|
|
|
|
for(const auto &deti : element_text) {
|
2019-03-16 11:34:52 +00:00
|
|
|
if(m_moved_content.m_elements.contains(deti->parentElement())) {
|
|
|
|
m_moved_content.m_element_texts.remove(deti);
|
|
|
|
}
|
2022-11-16 19:06:56 +01:00
|
|
|
}
|
|
|
|
const auto element_text_group{m_moved_content.m_texts_groups};
|
|
|
|
for(const auto &etig : element_text_group) {
|
2019-03-16 11:34:52 +00:00
|
|
|
if (m_moved_content.m_elements.contains(etig->parentElement())) {
|
|
|
|
m_moved_content.m_texts_groups.remove(etig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:51:54 +00:00
|
|
|
if (!m_moved_content.count()) return(-1);
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2014-06-15 18:03:25 +00:00
|
|
|
/* At this point, we've got all info to manage movement.
|
|
|
|
* There is now a move in progress */
|
2022-11-05 13:36:46 +01:00
|
|
|
m_movement_running = true;
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2017-12-05 20:51:54 +00:00
|
|
|
return(m_moved_content.count());
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementsMover::continueMovement
|
|
|
|
Add a move to the current movement.
|
|
|
|
@param movement movement to applied
|
|
|
|
*/
|
2020-03-22 20:19:41 +01:00
|
|
|
void ElementsMover::continueMovement(const QPointF &movement)
|
|
|
|
{
|
2022-11-05 13:36:46 +01:00
|
|
|
if (!m_movement_running || movement.isNull()) return;
|
2014-06-15 18:03:25 +00:00
|
|
|
|
2022-11-05 13:36:46 +01:00
|
|
|
m_current_movement += movement;
|
2014-06-15 18:03:25 +00:00
|
|
|
|
2022-11-08 18:17:55 +01:00
|
|
|
//Move every movable item, except conductor
|
2014-06-15 18:03:25 +00:00
|
|
|
typedef DiagramContent dc;
|
2022-11-08 18:17:55 +01:00
|
|
|
for (auto &qgi : m_moved_content.items(dc::Elements
|
|
|
|
| dc::TextFields
|
|
|
|
| dc::Images
|
|
|
|
| dc::Shapes
|
|
|
|
| dc::ElementTextFields
|
|
|
|
| dc::TextGroup
|
|
|
|
| dc::ConductorsToMove))
|
2017-12-05 20:51:54 +00:00
|
|
|
{
|
|
|
|
if (qgi == m_movement_driver)
|
|
|
|
continue;
|
2022-11-08 18:17:55 +01:00
|
|
|
qgi->setPos(qgi->pos() + movement);
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2014-06-15 18:03:25 +00:00
|
|
|
// Move some conductors
|
2022-11-08 18:17:55 +01:00
|
|
|
for (auto &conductor : m_moved_content.m_conductors_to_update)
|
2019-04-02 17:36:32 +00:00
|
|
|
{
|
2020-09-24 17:01:33 +02:00
|
|
|
#if TODO_LIST
|
2022-12-04 06:24:28 -05:00
|
|
|
#pragma message("@TODO fix this problem correctly, probably we must see conductor class.")
|
2020-09-24 17:01:33 +02:00
|
|
|
#endif
|
2022-12-04 06:24:28 -05:00
|
|
|
//Due to a weird behavior, we must ensure that the position of the conductor is (0,0).
|
2019-04-02 17:36:32 +00:00
|
|
|
//If not, in some unknown case the function QGraphicsScene::itemsBoundingRect() return a rectangle
|
2022-12-04 06:24:28 -05:00
|
|
|
//that take into account the pos() of the conductor, even if the bounding rect returned by the conductor is not in the pos().
|
|
|
|
//For the user this situation appears when the top right of the folio is not at the top right of the graphicsview,
|
2019-04-02 17:36:32 +00:00
|
|
|
//but displaced to the right and/or bottom.
|
2020-03-22 20:19:41 +01:00
|
|
|
|
2022-12-04 06:24:28 -05:00
|
|
|
//@TODO fix this problem correctly, probably we must see conductor class.
|
|
|
|
// if (c->pos() != QPointF(0,0)) { //<- they work, but the conductor text return to its original pos when the pos is set by user and not auto
|
2020-03-22 20:19:41 +01:00
|
|
|
// c->setPos(0,0); // because set the pos to 0,0 so text move to, and after call updatePath but because text pos is user defined
|
|
|
|
// } // we don't move it.
|
2022-11-08 18:17:55 +01:00
|
|
|
conductor->updatePath();
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
2022-11-04 20:25:49 +01:00
|
|
|
|
2022-11-14 20:45:16 +01:00
|
|
|
if (m_status_bar && m_movement_driver)
|
2022-11-04 20:25:49 +01:00
|
|
|
{
|
|
|
|
const auto point_{m_movement_driver->scenePos()};
|
2022-11-05 13:28:39 +01:00
|
|
|
m_status_bar->showMessage(QString("x %1 : y %2").arg(QString::number(point_.x()), QString::number(point_.y())));
|
2022-11-04 20:25:49 +01:00
|
|
|
}
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementsMover::endMovement
|
|
|
|
Ended the current movement by creating an undo added to the undostack of the diagram.
|
|
|
|
If there is only one element moved, we try to auto-connect new conductor from this element
|
|
|
|
and other possible element.
|
|
|
|
*/
|
2015-01-09 17:18:16 +00:00
|
|
|
void ElementsMover::endMovement()
|
|
|
|
{
|
|
|
|
// A movement must be inited
|
2022-11-05 13:36:46 +01:00
|
|
|
if (!m_movement_running) return;
|
2016-10-13 18:00:31 +00:00
|
|
|
|
|
|
|
//empty command to be used has parent of commands below
|
2022-11-08 18:17:55 +01:00
|
|
|
QUndoCommand *undo_object{new QUndoCommand()};
|
2015-01-23 14:35:49 +00:00
|
|
|
|
|
|
|
//Create undo move if there is a movement
|
2022-11-05 13:36:46 +01:00
|
|
|
if (!m_current_movement.isNull()) {
|
2022-11-08 18:17:55 +01:00
|
|
|
QUndoCommand *quc{new MoveElementsCommand(m_diagram, m_moved_content, m_current_movement, undo_object)};
|
2016-10-13 18:00:31 +00:00
|
|
|
undo_object->setText(quc->text());
|
|
|
|
}
|
2015-01-23 14:35:49 +00:00
|
|
|
|
|
|
|
//There is only one element moved, and project authorize auto conductor,
|
|
|
|
//we try auto connection of conductor;
|
|
|
|
typedef DiagramContent dc;
|
2022-11-08 18:17:55 +01:00
|
|
|
if (m_moved_content.items(dc::TextFields
|
|
|
|
| dc::Images
|
|
|
|
| dc::Shapes).isEmpty()
|
|
|
|
&& m_moved_content.items(dc::Elements).size() == 1
|
|
|
|
&& m_diagram->project()->autoConductor())
|
2015-01-23 14:35:49 +00:00
|
|
|
{
|
2022-11-08 18:17:55 +01:00
|
|
|
const Element *elmt{m_moved_content.m_elements.first()};
|
|
|
|
const auto aligned_free_terminals{elmt->AlignedFreeTerminals()};
|
2016-10-13 18:00:31 +00:00
|
|
|
|
2022-11-08 18:17:55 +01:00
|
|
|
if (const int acc = aligned_free_terminals.size())
|
2015-01-09 17:18:16 +00:00
|
|
|
{
|
2022-11-08 18:17:55 +01:00
|
|
|
for (const auto &pair : aligned_free_terminals)
|
|
|
|
{
|
|
|
|
Conductor *conductor{new Conductor(pair.first, pair.second)};
|
2015-01-09 17:18:16 +00:00
|
|
|
|
2022-11-08 18:17:55 +01:00
|
|
|
//Create an undo object for each new auto conductor, with undo_object for parent
|
|
|
|
new AddGraphicsObjectCommand(conductor, m_diagram, QPointF(), undo_object);
|
2016-10-13 18:00:31 +00:00
|
|
|
if (undo_object->text().isEmpty())
|
|
|
|
undo_object->setText(QObject::tr("Ajouter %n conducteur(s)", "add a numbers of conductor one or more", acc));
|
2015-01-23 14:35:49 +00:00
|
|
|
|
2022-11-08 18:17:55 +01:00
|
|
|
//Get all conductors at the same potential of conductor
|
|
|
|
const auto conductors_list{conductor->relatedPotentialConductors()};
|
|
|
|
|
|
|
|
//Compare the properties of every conductors stored in conductors_list,
|
|
|
|
//if every conductors properties is equal, we use this properties for conductor.
|
|
|
|
ConductorProperties others_properties;
|
|
|
|
bool use_properties = false;
|
|
|
|
if (!conductors_list.isEmpty())
|
|
|
|
{
|
|
|
|
use_properties = true;
|
|
|
|
others_properties = (*conductors_list.cbegin())->properties();
|
|
|
|
for (const auto &cond : conductors_list)
|
|
|
|
if (cond->properties() != others_properties)
|
|
|
|
use_properties = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_properties)
|
|
|
|
conductor->setProperties(others_properties);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
conductor -> setProperties(m_diagram -> defaultConductorProperties);
|
2015-08-17 17:01:39 +00:00
|
|
|
//Autonum the new conductor, the undo command associated for this, have for parent undo_object
|
2022-11-08 18:17:55 +01:00
|
|
|
ConductorAutoNumerotation can (conductor, m_diagram, undo_object);
|
|
|
|
can.numerate();
|
|
|
|
}
|
2015-08-17 17:01:39 +00:00
|
|
|
}
|
2020-03-22 20:19:41 +01:00
|
|
|
}
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
2015-01-23 14:35:49 +00:00
|
|
|
|
2016-10-13 18:00:31 +00:00
|
|
|
//Add undo_object if have child
|
2022-11-08 18:17:55 +01:00
|
|
|
if (undo_object->childCount())
|
|
|
|
m_diagram->undoStack().push(undo_object);
|
2016-10-13 18:00:31 +00:00
|
|
|
else
|
|
|
|
delete undo_object;
|
2020-09-24 17:01:33 +02:00
|
|
|
|
2015-01-09 17:18:16 +00:00
|
|
|
// There is no movement in progress now
|
2022-11-05 13:36:46 +01:00
|
|
|
m_movement_running = false;
|
2018-04-23 13:50:36 +00:00
|
|
|
m_moved_content.clear();
|
2022-11-04 20:25:49 +01:00
|
|
|
|
|
|
|
if (m_status_bar) {
|
|
|
|
m_status_bar->clearMessage();
|
|
|
|
}
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|