Finish a move element with free terminal aligned with other terminal from other element, will create auto creation of conductor

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@3594 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-01-09 17:18:16 +00:00
parent f782976fa7
commit 3e80f7076f
10 changed files with 179 additions and 98 deletions

View File

@@ -25,13 +25,20 @@
#include "qet.h" #include "qet.h"
/** /**
*Constructor * @brief ConductorAutoNumerotation::ConductorAutoNumerotation
* @param c the conductor to apply automatic numerotation * Constructor of autonum, after create a class, call numerate to apply the autonum.
* When autonum is applyed, they do with an undo command added to the stack of diagram.
* If you give a parent_undo at constructor, the undo command create in this class have parent_undo for parent,
* and wasn't added to the stack of diagram (it's the responsabillty of the parent_undo)
* @param conductor : the conductor to apply automatic numerotation
* @param diagram : the diagram of conductor
* @param parent_undo : parent undo command
*/ */
ConductorAutoNumerotation::ConductorAutoNumerotation(Conductor *c) : ConductorAutoNumerotation::ConductorAutoNumerotation(Conductor *conductor, Diagram *diagram, QUndoCommand *parent_undo) :
m_diagram (c -> diagram()), m_diagram (diagram),
conductor_ (c), conductor_ (conductor),
conductor_list (c -> relatedPotentialConductors()) conductor_list (conductor -> relatedPotentialConductors()),
m_parent_undo (parent_undo)
{} {}
/** /**
@@ -63,7 +70,7 @@ void ConductorAutoNumerotation::checkPotential(Conductor *conductor) {
if (!QET::eachStrIsEqual(strl)) { if (!QET::eachStrIsEqual(strl)) {
PotentialTextsDialog ptd(conductor, conductor->diagramEditor()); PotentialTextsDialog ptd(conductor, conductor->diagramEditor());
if ( ptd.exec() == QDialog::Accepted ) { if ( ptd.exec() == QDialog::Accepted ) {
ConductorAutoNumerotation can(conductor); ConductorAutoNumerotation can(conductor, conductor -> diagram());
can.applyText(ptd.selectedText()); can.applyText(ptd.selectedText());
} }
} }
@@ -75,31 +82,38 @@ void ConductorAutoNumerotation::checkPotential(Conductor *conductor) {
*/ */
void ConductorAutoNumerotation::applyText(QString t) { void ConductorAutoNumerotation::applyText(QString t) {
if (!conductor_) return; if (!conductor_) return;
if (conductor_list.empty()) {
if (conductor_list.empty())
{
//initialize the corresponding UndoCommand object //initialize the corresponding UndoCommand object
ChangeConductorPropertiesCommand *ccpc = new ChangeConductorPropertiesCommand (conductor_); ChangeConductorPropertiesCommand *ccpc = new ChangeConductorPropertiesCommand (conductor_, m_parent_undo);
ccpc -> setOldSettings (conductor_ -> properties()); ccpc -> setOldSettings (conductor_ -> properties());
ConductorProperties cp = conductor_ -> properties(); ConductorProperties cp = conductor_ -> properties();
cp.text = t; cp.text = t;
ccpc -> setNewSettings(cp); ccpc -> setNewSettings(cp);
if (!m_parent_undo)
m_diagram -> undoStack().push(ccpc); m_diagram -> undoStack().push(ccpc);
} }
else { else
{
QList <Conductor *> clist = conductor_list.toList(); QList <Conductor *> clist = conductor_list.toList();
clist << conductor_; clist << conductor_;
QList <ConductorProperties> old_properties, new_properties; QList <ConductorProperties> old_properties, new_properties;
ConductorProperties cp; ConductorProperties cp;
foreach (Conductor *c, clist) { foreach (Conductor *c, clist)
{
old_properties << c -> properties(); old_properties << c -> properties();
cp = c -> properties(); cp = c -> properties();
cp.text = t; cp.text = t;
new_properties << cp; new_properties << cp;
} }
//initialize the corresponding UndoCommand object //initialize the corresponding UndoCommand object
ChangeSeveralConductorsPropertiesCommand *cscpc = new ChangeSeveralConductorsPropertiesCommand(clist); ChangeSeveralConductorsPropertiesCommand *cscpc = new ChangeSeveralConductorsPropertiesCommand(clist, m_parent_undo);
cscpc -> setOldSettings(old_properties); cscpc -> setOldSettings(old_properties);
cscpc -> setNewSettings(new_properties); cscpc -> setNewSettings(new_properties);
if (!m_parent_undo)
m_diagram -> undoStack().push(cscpc); m_diagram -> undoStack().push(cscpc);
} }
} }

View File

@@ -22,16 +22,18 @@
class Diagram; class Diagram;
class Conductor; class Conductor;
class QUndoCommand;
class ConductorAutoNumerotation { class ConductorAutoNumerotation
{
public: public:
//constructors & destructor //constructors & destructor
ConductorAutoNumerotation (Conductor *); ConductorAutoNumerotation (Conductor *conductor, Diagram *diagram, QUndoCommand *undo_parent = nullptr);
//methods //methods
void numerate(); void numerate ();
static void checkPotential(Conductor *); static void checkPotential (Conductor *);
void applyText(QString); void applyText (QString);
private: private:
//methods //methods
@@ -42,6 +44,7 @@ class ConductorAutoNumerotation {
Diagram *m_diagram; Diagram *m_diagram;
Conductor *conductor_; Conductor *conductor_;
QSet <Conductor *> conductor_list; QSet <Conductor *> conductor_list;
QUndoCommand *m_parent_undo;
}; };
#endif // CONDUCTORAUTONUMEROTATION_H #endif // CONDUCTORAUTONUMEROTATION_H

View File

@@ -297,12 +297,12 @@ MoveElementsCommand::MoveElementsCommand(
const QPointF &m, const QPointF &m,
QUndoCommand *parent QUndoCommand *parent
) : ) :
QUndoCommand(parent), QUndoCommand (parent),
diagram(dia), diagram (dia),
content_to_move(diagram_content), content_to_move (diagram_content),
movement(m), movement (m),
m_anim_group(nullptr), m_anim_group (nullptr),
first_redo(true) first_redo (true)
{ {
QString moved_content_sentence = content_to_move.sentence( QString moved_content_sentence = content_to_move.sentence(
DiagramContent::Elements | DiagramContent::Elements |
@@ -338,6 +338,7 @@ void MoveElementsCommand::undo() {
diagram -> showMe(); diagram -> showMe();
m_anim_group->setDirection(QAnimationGroup::Forward); m_anim_group->setDirection(QAnimationGroup::Forward);
m_anim_group->start(); m_anim_group->start();
QUndoCommand::undo();
} }
/** /**
@@ -353,6 +354,7 @@ void MoveElementsCommand::redo() {
m_anim_group->setDirection(QAnimationGroup::Backward); m_anim_group->setDirection(QAnimationGroup::Backward);
m_anim_group->start(); m_anim_group->start();
} }
QUndoCommand::redo();
} }
/** /**
@@ -924,8 +926,7 @@ ChangeConductorPropertiesCommand::ChangeConductorPropertiesCommand(Conductor *c,
QUndoCommand(QObject::tr("modifier les propri\351t\351s d'un conducteur", "undo caption"), parent), QUndoCommand(QObject::tr("modifier les propri\351t\351s d'un conducteur", "undo caption"), parent),
conductor(c), conductor(c),
old_settings_set(false), old_settings_set(false),
new_settings_set(false), new_settings_set(false)
diagram(c->diagram())
{ {
} }
@@ -950,7 +951,7 @@ void ChangeConductorPropertiesCommand::setNewSettings(const ConductorProperties
doivent avoir ete definis a l'aide de setNewSettings et setOldSettings doivent avoir ete definis a l'aide de setNewSettings et setOldSettings
*/ */
void ChangeConductorPropertiesCommand::undo() { void ChangeConductorPropertiesCommand::undo() {
diagram -> showMe(); if (conductor -> diagram()) conductor -> diagram() -> showMe();
if (old_settings_set && new_settings_set) { if (old_settings_set && new_settings_set) {
conductor -> setProperties(old_properties); conductor -> setProperties(old_properties);
conductor -> update(); conductor -> update();
@@ -962,7 +963,7 @@ void ChangeConductorPropertiesCommand::undo() {
doivent avoir ete definis a l'aide de setNewSettings et setOldSettings doivent avoir ete definis a l'aide de setNewSettings et setOldSettings
*/ */
void ChangeConductorPropertiesCommand::redo() { void ChangeConductorPropertiesCommand::redo() {
diagram -> showMe(); if (conductor -> diagram()) conductor -> diagram() -> showMe();
if (old_settings_set && new_settings_set) { if (old_settings_set && new_settings_set) {
conductor -> setProperties(new_properties); conductor -> setProperties(new_properties);
conductor -> update(); conductor -> update();
@@ -978,8 +979,7 @@ ChangeSeveralConductorsPropertiesCommand::ChangeSeveralConductorsPropertiesComma
QUndoCommand(QObject::tr("modifier les propri\351t\351s de plusieurs conducteurs", "undo caption"), parent), QUndoCommand(QObject::tr("modifier les propri\351t\351s de plusieurs conducteurs", "undo caption"), parent),
conductors(c), conductors(c),
old_settings_set(false), old_settings_set(false),
new_settings_set(false), new_settings_set(false)
diagram(c.first()->diagram())
{ {
} }
@@ -1015,7 +1015,7 @@ void ChangeSeveralConductorsPropertiesCommand::setNewSettings(const ConductorPro
doivent avoir ete definis a l'aide de setNewSettings et setOldSettings doivent avoir ete definis a l'aide de setNewSettings et setOldSettings
*/ */
void ChangeSeveralConductorsPropertiesCommand::undo() { void ChangeSeveralConductorsPropertiesCommand::undo() {
//diagram -> showMe(); if (conductors.first() -> diagram()) conductors.first() -> diagram() -> showMe();
if (old_settings_set && new_settings_set) { if (old_settings_set && new_settings_set) {
int i=0; int i=0;
foreach(Conductor *c, conductors) { foreach(Conductor *c, conductors) {
@@ -1031,7 +1031,7 @@ void ChangeSeveralConductorsPropertiesCommand::undo() {
doivent avoir ete definis a l'aide de setNewSettings et setOldSettings doivent avoir ete definis a l'aide de setNewSettings et setOldSettings
*/ */
void ChangeSeveralConductorsPropertiesCommand::redo() { void ChangeSeveralConductorsPropertiesCommand::redo() {
//diagram -> showMe(); if (conductors.first() -> diagram()) conductors.first() -> diagram() -> showMe();
if (old_settings_set && new_settings_set) { if (old_settings_set && new_settings_set) {
//new propertie are the same for each conductor //new propertie are the same for each conductor

View File

@@ -60,12 +60,14 @@ class AddItemCommand : public QUndoCommand {
virtual void undo() { virtual void undo() {
m_diagram -> showMe(); m_diagram -> showMe();
m_diagram -> removeItem(m_item); m_diagram -> removeItem(m_item);
QUndoCommand::undo();
} }
virtual void redo() { virtual void redo() {
m_diagram -> showMe(); m_diagram -> showMe();
m_diagram -> addItem(m_item); m_diagram -> addItem(m_item);
m_item -> setPos(m_pos); m_item -> setPos(m_pos);
QUndoCommand::redo();
} }
private: private:
@@ -471,7 +473,6 @@ class ChangeConductorPropertiesCommand : public QUndoCommand {
bool old_settings_set; bool old_settings_set;
/// track whether post-change properties were set /// track whether post-change properties were set
bool new_settings_set; bool new_settings_set;
Diagram *diagram;
}; };
/** /**
@@ -507,7 +508,6 @@ class ChangeSeveralConductorsPropertiesCommand : public QUndoCommand {
bool old_settings_set; bool old_settings_set;
/// track whether post-change properties were set /// track whether post-change properties were set
bool new_settings_set; bool new_settings_set;
Diagram *diagram;
}; };
class ItemResizerCommand : public QUndoCommand { class ItemResizerCommand : public QUndoCommand {

View File

@@ -24,6 +24,7 @@
#include "independenttextitem.h" #include "independenttextitem.h"
#include "diagramimageitem.h" #include "diagramimageitem.h"
#include "elementtextitem.h" #include "elementtextitem.h"
#include "conductorautonumerotation.h"
/** /**
* @brief ElementsMover::ElementsMover Constructor * @brief ElementsMover::ElementsMover Constructor
@@ -116,8 +117,11 @@ void ElementsMover::continueMovement(const QPointF &movement) {
/** /**
* @brief ElementsMover::endMovement * @brief ElementsMover::endMovement
* Ended the current movement by creating an undo added to the undostack of the diagram. * 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.
*/ */
void ElementsMover::endMovement() { void ElementsMover::endMovement()
{
// A movement must be inited // A movement must be inited
if (!movement_running_) return; if (!movement_running_) return;
@@ -130,6 +134,27 @@ void ElementsMover::endMovement() {
current_movement_ current_movement_
); );
//There is only one element moved, 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)
{
Element *elmt = moved_content_.elements.toList().first();
while (!elmt -> AlignedFreeTerminals().isEmpty())
{
QPair <Terminal *, Terminal *> pair = elmt->AlignedFreeTerminals().takeFirst();
Conductor *conductor = new Conductor(pair.first, pair.second);
conductor -> setProperties(diagram_ -> defaultConductorProperties);
//Create an undo object for each new auto conductor, with undo_object for parent;
new AddItemCommand<Conductor *>(conductor, diagram_, QPointF(), undo_object);
//Autonum the new conductor, the undo command associated for this, have for parent undo_object
ConductorAutoNumerotation can (conductor, diagram_, undo_object);
can.numerate();
};
}
diagram_ -> undoStack().push(undo_object); diagram_ -> undoStack().push(undo_object);
} }

View File

@@ -1380,15 +1380,6 @@ void Conductor::setHighlighted(Conductor::Highlight hl) {
update(); update();
} }
/**
* @brief Conductor::autoText
*lance l'autoNumerotation sur ce conducteur
*/
void Conductor::autoText() {
ConductorAutoNumerotation can(this);
can.numerate();
}
/** /**
Met a jour les proprietes du conducteur apres modification du champ de texte affiche Met a jour les proprietes du conducteur apres modification du champ de texte affiche
*/ */
@@ -1396,21 +1387,26 @@ void Conductor::displayedTextChanged() {
// verifie que le texte a reellement change // verifie que le texte a reellement change
if (text_item -> toPlainText() == properties_.text) return; if (text_item -> toPlainText() == properties_.text) return;
if (Diagram *my_diagram = diagram()) { if (Diagram *my_diagram = diagram())
{
int qmbreturn=0; int qmbreturn=0;
//if conductor isn't alone at this potential //if conductor isn't alone at this potential
//ask user to apply text on every conductors of this potential //ask user to apply text on every conductors of this potential
if (relatedPotentialConductors().size() >= 1){ if (relatedPotentialConductors().size() >= 1)
{
qmbreturn = QMessageBox::question(diagramEditor(), tr("Textes de conducteurs"), qmbreturn = QMessageBox::question(diagramEditor(), tr("Textes de conducteurs"),
tr("Voulez-vous appliquer le nouveau texte \n" tr("Voulez-vous appliquer le nouveau texte \n"
"\340 l'ensemble des conducteurs de ce potentiel ?"), "\340 l'ensemble des conducteurs de ce potentiel ?"),
QMessageBox::No| QMessageBox::Yes, QMessageBox::Yes); QMessageBox::No| QMessageBox::Yes, QMessageBox::Yes);
if (qmbreturn == QMessageBox::Yes){ if (qmbreturn == QMessageBox::Yes)
ConductorAutoNumerotation can(this); {
ConductorAutoNumerotation can(this, my_diagram);
can.applyText(text_item -> toPlainText()); can.applyText(text_item -> toPlainText());
} }
} }
if (qmbreturn == 0 || qmbreturn == QMessageBox::No) {
if (qmbreturn == 0 || qmbreturn == QMessageBox::No)
{
// initialise l'objet UndoCommand correspondant // initialise l'objet UndoCommand correspondant
ConductorProperties new_properties(properties_); ConductorProperties new_properties(properties_);
new_properties.text = text_item -> toPlainText(); new_properties.text = text_item -> toPlainText();

View File

@@ -112,7 +112,6 @@ class Conductor : public QObject, public QGraphicsPathItem {
void calculateTextItemPosition(); void calculateTextItemPosition();
virtual Highlight highlight() const; virtual Highlight highlight() const;
virtual void setHighlighted(Highlight); virtual void setHighlighted(Highlight);
void autoText();
QSet<Conductor *> relatedPotentialConductors(const bool all_diagram = true, QList <Terminal *> *t_list=0); QSet<Conductor *> relatedPotentialConductors(const bool all_diagram = true, QList <Terminal *> *t_list=0);
QETDiagramEditor* diagramEditor() const; QETDiagramEditor* diagramEditor() const;
void editProperty (); void editProperty ();

View File

@@ -494,6 +494,31 @@ QDomElement Element::toXml(QDomDocument &document, QHash<Terminal *, int> &table
return(element); return(element);
} }
/**
* @brief Element::AlignedFreeTerminals
* @return a list of terminal (owned by this element) aligned to other terminal (from other element)
* The first Terminal of QPair is a Terminal owned by this element,
* this terminal haven't got any conductor docked.
* The second Terminal of QPair is a Terminal owned by an other element,
* which is aligned with the first Terminal. The second Terminal can have or not docked conductors.
*/
QList <QPair <Terminal *, Terminal *> > Element::AlignedFreeTerminals() const
{
QList <QPair <Terminal *, Terminal *> > list;
foreach (Terminal *terminal, terminals())
{
if (terminal->conductors().isEmpty())
{
Terminal *other_terminal = terminal -> alignedWithTerminal();
if (other_terminal)
list << qMakePair(terminal, other_terminal);
}
}
return list;
}
/** /**
* @brief Element::initLink * @brief Element::initLink
* Initialise the link between this element and other elements. * Initialise the link between this element and other elements.
@@ -549,6 +574,10 @@ bool comparPos(const Element *elmt1, const Element *elmt2) {
return elmt1->pos().x() <= elmt2->pos().x(); return elmt1->pos().x() <= elmt2->pos().x();
} }
/**
* @brief Element::mouseMoveEvent
* @param event
*/
void Element::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void Element::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ {
QetGraphicsItem::mouseMoveEvent(event); QetGraphicsItem::mouseMoveEvent(event);
@@ -558,6 +587,10 @@ void Element::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
} }
} }
/**
* @brief Element::mouseReleaseEvent
* @param event
*/
void Element::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void Element::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
QetGraphicsItem::mouseReleaseEvent(event); QetGraphicsItem::mouseReleaseEvent(event);

View File

@@ -94,6 +94,7 @@ class Element : public QetGraphicsItem {
/// @return the maximum number of terminals for this element /// @return the maximum number of terminals for this element
virtual int maxTerminalsCount() const = 0; virtual int maxTerminalsCount() const = 0;
QList <QPair <Terminal *, Terminal *> > AlignedFreeTerminals () const;
/** /**
*related method and attributes, *related method and attributes,

View File

@@ -21,6 +21,7 @@
#include "qetgraphicsitem/conductor.h" #include "qetgraphicsitem/conductor.h"
#include "diagramcommands.h" #include "diagramcommands.h"
#include "qetapp.h" #include "qetapp.h"
#include "conductorautonumerotation.h"
QColor Terminal::neutralColor = QColor(Qt::blue); QColor Terminal::neutralColor = QColor(Qt::blue);
QColor Terminal::allowedColor = QColor(Qt::darkGreen); QColor Terminal::allowedColor = QColor(Qt::darkGreen);
@@ -527,35 +528,44 @@ void Terminal::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
other_terminal -> update(); other_terminal -> update();
} }
/** /**
Gere le fait qu'on relache la souris sur la Borne. * @brief Terminal::mouseReleaseEvent
@param e L'evenement souris correspondant * @param e
*/ */
void Terminal::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { void Terminal::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
//setCursor(Qt::ArrowCursor); {
previous_terminal_ = 0; previous_terminal_ = 0;
hovered_color_ = neutralColor; hovered_color_ = neutralColor;
// verifie que la scene est bien un Diagram
if (Diagram *d = diagram()) { if (Diagram *d = diagram())
// on arrete de dessiner l'apercu du conducteur {
//Stop conductor preview
d -> setConductor(false); d -> setConductor(false);
// on recupere l'element sous le pointeur lors du MouseReleaseEvent
//Get item under cursor
QGraphicsItem *qgi = d -> itemAt(e -> scenePos()); QGraphicsItem *qgi = d -> itemAt(e -> scenePos());
// s'il n'y a rien, on arrete la
if (!qgi) return; if (!qgi) return;
// idem si l'element obtenu n'est pas une borne
//Element must be a terminal
Terminal *other_terminal = qgraphicsitem_cast<Terminal *>(qgi); Terminal *other_terminal = qgraphicsitem_cast<Terminal *>(qgi);
if (!other_terminal) return; if (!other_terminal) return;
// on remet la couleur de hover a sa valeur par defaut
other_terminal -> hovered_color_ = neutralColor; other_terminal -> hovered_color_ = neutralColor;
other_terminal -> hovered_ = false; other_terminal -> hovered_ = false;
// on s'arrete la s'il n'est pas possible de relier les bornes
//We stop her if we can't link this terminal with other terminal
if (!canBeLinkedTo(other_terminal)) return; if (!canBeLinkedTo(other_terminal)) return;
// autrement, on pose un conducteur
//Create conductor
Conductor *new_conductor = new Conductor(this, other_terminal); Conductor *new_conductor = new Conductor(this, other_terminal);
new_conductor -> setProperties(d -> defaultConductorProperties); new_conductor -> setProperties(d -> defaultConductorProperties);
d -> undoStack().push(new AddItemCommand<Conductor *>(new_conductor, d)); QUndoCommand *undo = new AddItemCommand<Conductor *>(new_conductor, d);
new_conductor -> autoText(); //Autonum it
ConductorAutoNumerotation can (new_conductor, d, undo);
can.numerate();
//Add undo command to the parent diagram
d -> undoStack().push(undo);
} }
} }