2010-05-08 21:24:43 +00:00
|
|
|
/*
|
2015-02-20 14:56:22 +00:00
|
|
|
Copyright 2006-2015 The QElectroTech Team
|
2010-05-08 21:24:43 +00:00
|
|
|
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 "elementsmover.h"
|
2014-10-17 21:30:42 +00:00
|
|
|
#include "conductor.h"
|
|
|
|
#include "conductortextitem.h"
|
2010-05-08 21:24:43 +00:00
|
|
|
#include "diagram.h"
|
|
|
|
#include "diagramcommands.h"
|
2014-10-17 21:30:42 +00:00
|
|
|
#include "element.h"
|
|
|
|
#include "independenttextitem.h"
|
|
|
|
#include "diagramimageitem.h"
|
|
|
|
#include "elementtextitem.h"
|
2015-01-09 17:18:16 +00:00
|
|
|
#include "conductorautonumerotation.h"
|
2010-05-08 21:24:43 +00:00
|
|
|
|
|
|
|
/**
|
2014-06-15 18:03:25 +00:00
|
|
|
* @brief ElementsMover::ElementsMover Constructor
|
|
|
|
*/
|
2010-05-08 21:24:43 +00:00
|
|
|
ElementsMover::ElementsMover() :
|
|
|
|
movement_running_(false),
|
|
|
|
current_movement_(),
|
|
|
|
diagram_(0),
|
|
|
|
movement_driver_(0),
|
|
|
|
moved_content_()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-15 18:03:25 +00:00
|
|
|
* @brief ElementsMover::~ElementsMover Destructor
|
|
|
|
*/
|
2010-05-08 21:24:43 +00:00
|
|
|
ElementsMover::~ElementsMover() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-15 18:03:25 +00: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.
|
|
|
|
*/
|
2010-05-08 21:24:43 +00:00
|
|
|
bool ElementsMover::isReady() const {
|
|
|
|
return(!movement_running_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-15 18:03:25 +00: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.
|
|
|
|
*/
|
2010-05-08 21:24:43 +00:00
|
|
|
int ElementsMover::beginMovement(Diagram *diagram, QGraphicsItem *driver_item) {
|
2014-06-15 18:03:25 +00:00
|
|
|
// They must be no movement in progress
|
2010-05-08 21:24:43 +00:00
|
|
|
if (movement_running_) return(-1);
|
|
|
|
|
2014-06-15 18:03:25 +00:00
|
|
|
// Be sure we have diagram to work
|
2010-05-08 21:24:43 +00:00
|
|
|
if (!diagram) return(-1);
|
|
|
|
diagram_ = diagram;
|
|
|
|
|
2014-06-15 18:03:25 +00:00
|
|
|
// Take count of driver item
|
2010-05-08 21:24:43 +00:00
|
|
|
movement_driver_ = driver_item;
|
|
|
|
|
2014-06-15 18:03:25 +00:00
|
|
|
// At the beginning of movement, move is NULL
|
2010-05-08 21:24:43 +00:00
|
|
|
current_movement_ = QPointF(0.0, 0.0);
|
|
|
|
|
|
|
|
moved_content_ = diagram -> selectedContent();
|
2014-10-17 21:30:42 +00:00
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
if (!moved_content_.count()) return(-1);
|
|
|
|
|
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 */
|
2010-05-08 21:24:43 +00:00
|
|
|
movement_running_ = true;
|
|
|
|
|
|
|
|
return(moved_content_.count());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-15 18:03:25 +00:00
|
|
|
* @brief ElementsMover::continueMovement
|
|
|
|
* Add a move to the current movement.
|
|
|
|
* @param movement movement to applied
|
|
|
|
*/
|
2010-05-08 21:24:43 +00:00
|
|
|
void ElementsMover::continueMovement(const QPointF &movement) {
|
2014-06-15 18:03:25 +00:00
|
|
|
if (!movement_running_ || movement.isNull()) return;
|
|
|
|
|
2010-05-08 21:24:43 +00:00
|
|
|
current_movement_ += movement;
|
2014-06-15 18:03:25 +00:00
|
|
|
|
|
|
|
//Move every movable item, except conductor
|
|
|
|
typedef DiagramContent dc;
|
2014-10-25 21:21:52 +00:00
|
|
|
foreach (QGraphicsItem *qgi, moved_content_.items(dc::Elements | dc::TextFields | dc::Images | dc::Shapes)) {
|
2014-06-15 18:03:25 +00:00
|
|
|
if (qgi == movement_driver_) continue;
|
|
|
|
qgi -> setPos(qgi->pos() + movement);
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
|
|
|
|
2014-06-15 18:03:25 +00:00
|
|
|
// Move some conductors
|
2010-05-08 21:24:43 +00:00
|
|
|
foreach(Conductor *conductor, moved_content_.conductorsToMove) {
|
|
|
|
conductor -> setPos(conductor -> pos() + movement);
|
|
|
|
}
|
|
|
|
|
2014-06-15 18:03:25 +00:00
|
|
|
// Recalcul the path of other conductors
|
2010-05-08 21:24:43 +00:00
|
|
|
foreach(Conductor *conductor, moved_content_.conductorsToUpdate) {
|
|
|
|
conductor -> updatePath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-15 18:03:25 +00:00
|
|
|
* @brief ElementsMover::endMovement
|
|
|
|
* Ended the current movement by creating an undo added to the undostack of the diagram.
|
2015-01-09 17:18:16 +00:00
|
|
|
* If there is only one element moved, we try to auto-connect new conductor from this element
|
|
|
|
* and other possible element.
|
2014-06-15 18:03:25 +00:00
|
|
|
*/
|
2015-01-09 17:18:16 +00:00
|
|
|
void ElementsMover::endMovement()
|
|
|
|
{
|
|
|
|
// A movement must be inited
|
2010-05-08 21:24:43 +00:00
|
|
|
if (!movement_running_) return;
|
|
|
|
|
2015-01-23 14:35:49 +00:00
|
|
|
QUndoCommand *undo_object = nullptr;
|
|
|
|
|
|
|
|
//Create undo move if there is a movement
|
|
|
|
if (!current_movement_.isNull())
|
|
|
|
undo_object = new MoveElementsCommand(diagram_, moved_content_, current_movement_);
|
|
|
|
|
|
|
|
//There is only one element moved, and project authorize auto conductor,
|
|
|
|
//we try auto connection of conductor;
|
|
|
|
typedef DiagramContent dc;
|
|
|
|
if (moved_content_.items(dc::TextFields | dc::Images | dc::Shapes).size() == 0 &&
|
|
|
|
moved_content_.items(dc::Elements).size() == 1 &&
|
|
|
|
diagram_ -> project() -> autoConductor())
|
|
|
|
{
|
|
|
|
Element *elmt = moved_content_.elements.toList().first();
|
|
|
|
|
|
|
|
while (!elmt -> AlignedFreeTerminals().isEmpty())
|
2015-01-09 17:18:16 +00:00
|
|
|
{
|
2015-01-23 14:35:49 +00:00
|
|
|
QPair <Terminal *, Terminal *> pair = elmt -> AlignedFreeTerminals().takeFirst();
|
2015-01-09 17:18:16 +00:00
|
|
|
|
2015-01-23 14:35:49 +00:00
|
|
|
Conductor *conductor = new Conductor(pair.first, pair.second);
|
|
|
|
conductor -> setProperties(diagram_ -> defaultConductorProperties);
|
2015-01-09 17:18:16 +00:00
|
|
|
|
2015-01-23 14:35:49 +00:00
|
|
|
//Create an undo object for each new auto conductor, with undo_object for parent if exist
|
|
|
|
//Else the first undo for this auto conductor become the undo_object
|
|
|
|
if (undo_object)
|
2015-01-09 17:18:16 +00:00
|
|
|
new AddItemCommand<Conductor *>(conductor, diagram_, QPointF(), undo_object);
|
2015-01-23 14:35:49 +00:00
|
|
|
else
|
|
|
|
undo_object = new AddItemCommand<Conductor *>(conductor, diagram_, QPointF());
|
|
|
|
|
|
|
|
//Autonum the new conductor, the undo command associated for this, have for parent undo_object
|
|
|
|
ConductorAutoNumerotation can (conductor, diagram_, undo_object);
|
|
|
|
can.numerate();
|
|
|
|
};
|
2010-05-08 21:24:43 +00:00
|
|
|
}
|
2015-01-23 14:35:49 +00:00
|
|
|
|
|
|
|
//Add undo_object if exist
|
|
|
|
if (undo_object)
|
|
|
|
diagram_ -> undoStack().push(undo_object);
|
2010-05-08 21:24:43 +00:00
|
|
|
|
2015-01-09 17:18:16 +00:00
|
|
|
// There is no movement in progress now
|
2010-05-08 21:24:43 +00:00
|
|
|
movement_running_ = false;
|
|
|
|
}
|