2013-05-03 17:35:22 +00:00
|
|
|
/*
|
|
|
|
Copyright 2006-2013 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/>.
|
|
|
|
*/
|
2013-04-04 16:57:15 +00:00
|
|
|
#include "conductorautonumerotation.h"
|
|
|
|
#include "conductorautonumerotationwidget.h"
|
2013-04-10 19:35:47 +00:00
|
|
|
#include "diagramcommands.h"
|
2013-05-03 17:35:22 +00:00
|
|
|
#include "numerotationcontextcommands.h"
|
2013-04-04 16:57:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*Constructor
|
2013-04-10 19:35:47 +00:00
|
|
|
* @param c the conductor to apply automatic numerotation
|
2013-04-04 16:57:15 +00:00
|
|
|
*/
|
|
|
|
ConductorAutoNumerotation::ConductorAutoNumerotation(Conductor *c) :
|
2013-05-03 17:35:22 +00:00
|
|
|
AutoNumerotation (c -> diagram()),
|
2013-04-04 16:57:15 +00:00
|
|
|
conductor_ (c),
|
2013-05-03 17:35:22 +00:00
|
|
|
conductor_list(c -> relatedPotentialConductors())
|
|
|
|
{
|
|
|
|
num_context = diagram_ -> getNumerotation(Diagram::Conductors);
|
|
|
|
}
|
2013-04-04 16:57:15 +00:00
|
|
|
|
2013-04-19 14:59:20 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param d a diagram to apply automatic numerotation
|
|
|
|
*/
|
|
|
|
ConductorAutoNumerotation::ConductorAutoNumerotation(Diagram *d) :
|
2013-05-03 17:35:22 +00:00
|
|
|
AutoNumerotation (d),
|
|
|
|
conductor_ (NULL)
|
2013-04-19 14:59:20 +00:00
|
|
|
{}
|
|
|
|
|
2013-04-04 16:57:15 +00:00
|
|
|
/**
|
2013-04-10 19:35:47 +00:00
|
|
|
* @param c the conductor to apply automatic numerotation
|
2013-04-04 16:57:15 +00:00
|
|
|
*/
|
|
|
|
void ConductorAutoNumerotation::setConductor(Conductor *c) {
|
|
|
|
conductor_ = c;
|
|
|
|
diagram_ = c -> diagram();
|
|
|
|
conductor_list = c -> relatedPotentialConductors();
|
2013-05-03 17:35:22 +00:00
|
|
|
num_context = diagram_ -> getNumerotation(Diagram::Conductors);
|
2013-04-04 16:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ConductorAutoNumerotation::numerate
|
2013-04-10 19:35:47 +00:00
|
|
|
* execute the automatic numerotation
|
2013-04-04 16:57:15 +00:00
|
|
|
*/
|
|
|
|
void ConductorAutoNumerotation::numerate() {
|
2013-04-19 13:22:30 +00:00
|
|
|
if (!conductor_) return;
|
2013-05-03 17:35:22 +00:00
|
|
|
if (conductor_list.size() >= 1 ) numeratePotential();
|
2013-06-24 09:36:05 +00:00
|
|
|
else if (conductor_ -> properties().type == ConductorProperties::Multi) numerateNewConductor();
|
|
|
|
else return;
|
2013-05-03 17:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ConductorAutoNumerotation::numerateDiagram
|
|
|
|
* Numerate all conductor in diagram
|
|
|
|
*/
|
|
|
|
void ConductorAutoNumerotation::numerateDiagram() {
|
|
|
|
if (!diagram_) return;
|
|
|
|
//Get all potentials presents in diagram
|
|
|
|
QList <QSet <Conductor *> > potential_list = diagram_ -> potentials();
|
|
|
|
//Browse all potentials and set new numerotation
|
|
|
|
for (int i=0; i < potential_list.size(); ++i) {
|
|
|
|
setConductor (potential_list.at(i).toList().first());
|
|
|
|
NumerotationContextCommands ncc(diagram_, num_context);
|
|
|
|
applyText(ncc.toRepresentedString());
|
|
|
|
diagram_ -> setNumerotation(Diagram::Conductors, ncc.next());
|
2013-04-19 13:22:30 +00:00
|
|
|
}
|
2013-04-10 19:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-03 17:35:22 +00:00
|
|
|
* @brief ConductorAutoNumerotation::applyText
|
|
|
|
* apply the text @t to @conductor_ and all conductors at the same potential
|
2013-04-10 19:35:47 +00:00
|
|
|
*/
|
2013-04-19 13:22:30 +00:00
|
|
|
void ConductorAutoNumerotation::applyText(QString t) {
|
|
|
|
if (!conductor_) return;
|
|
|
|
if (conductor_list.empty()) {
|
|
|
|
//initialize the corresponding UndoCommand object
|
|
|
|
ChangeConductorPropertiesCommand *ccpc = new ChangeConductorPropertiesCommand (conductor_);
|
2013-04-25 22:32:29 +00:00
|
|
|
ccpc -> setOldSettings (conductor_ -> properties());
|
|
|
|
ConductorProperties cp = conductor_ -> properties();
|
2013-04-19 13:22:30 +00:00
|
|
|
cp.text = t;
|
|
|
|
ccpc -> setNewSettings(cp);
|
|
|
|
diagram_ -> undoStack().push(ccpc);
|
|
|
|
}
|
|
|
|
else {
|
2014-01-06 18:21:58 +00:00
|
|
|
QList <Conductor *> clist = conductor_list.toList();
|
2013-04-19 13:22:30 +00:00
|
|
|
clist << conductor_;
|
|
|
|
QList <ConductorProperties> old_properties, new_properties;
|
|
|
|
ConductorProperties cp;
|
|
|
|
|
|
|
|
foreach (Conductor *c, clist) {
|
|
|
|
old_properties << c -> properties();
|
|
|
|
cp = c -> properties();
|
|
|
|
cp.text = t;
|
2013-04-25 22:32:29 +00:00
|
|
|
new_properties << cp;
|
2013-04-19 13:22:30 +00:00
|
|
|
}
|
|
|
|
//initialize the corresponding UndoCommand object
|
|
|
|
ChangeSeveralConductorsPropertiesCommand *cscpc = new ChangeSeveralConductorsPropertiesCommand(clist);
|
|
|
|
cscpc -> setOldSettings(old_properties);
|
|
|
|
cscpc -> setNewSettings(new_properties);
|
|
|
|
diagram_ -> undoStack().push(cscpc);
|
|
|
|
}
|
2013-04-04 16:57:15 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 08:00:09 +00:00
|
|
|
/**
|
2013-04-19 14:59:20 +00:00
|
|
|
* @brief Set the default text to all potentials of the diagram
|
2013-04-18 08:00:09 +00:00
|
|
|
*/
|
2013-05-03 17:35:22 +00:00
|
|
|
void ConductorAutoNumerotation::removeNumOfDiagram() {
|
2013-04-19 14:59:20 +00:00
|
|
|
if (!diagram_) return;
|
|
|
|
//Get all potentials presents in diagram
|
|
|
|
QList <QSet <Conductor *> > potential_list = diagram_ -> potentials();
|
|
|
|
//Browse all potentials and set the default text
|
|
|
|
for (int i=0; i < potential_list.size(); i++) {
|
2013-04-21 13:55:51 +00:00
|
|
|
setConductor (potential_list.at(i).toList().first());
|
|
|
|
applyText (diagram_ -> defaultConductorProperties.text);
|
2013-04-18 08:00:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 19:35:47 +00:00
|
|
|
/**
|
2013-05-03 17:35:22 +00:00
|
|
|
* @brief ConductorAutoNumerotation::numeratePotential
|
|
|
|
* Numerate a conductor on an existing potential
|
2013-04-10 19:35:47 +00:00
|
|
|
*/
|
2013-05-03 17:35:22 +00:00
|
|
|
void ConductorAutoNumerotation::numeratePotential() {
|
|
|
|
QStringList strl;
|
|
|
|
foreach (const Conductor *cc, conductor_list) strl<<(cc->text());
|
|
|
|
//the texts is identicals
|
|
|
|
if (eachIsEqual(strl)) {
|
2013-06-04 23:11:05 +00:00
|
|
|
ConductorProperties cp = conductor_ -> properties();
|
2013-05-03 17:35:22 +00:00
|
|
|
cp.text = strl.at(0);
|
|
|
|
conductor_ -> setProperties(cp);
|
|
|
|
conductor_ -> setText(strl.at(0));
|
|
|
|
}
|
|
|
|
//the texts isn't identicals
|
|
|
|
else {
|
|
|
|
ConductorAutoNumerotationWidget *canw = new ConductorAutoNumerotationWidget(conductor_, conductor_list, conductor_ -> diagramEditor());
|
|
|
|
connect(canw, SIGNAL(textIsSelected(QString)),
|
|
|
|
this, SLOT(applyText(QString)));
|
|
|
|
canw -> exec();
|
|
|
|
}
|
|
|
|
}
|
2013-04-10 09:31:51 +00:00
|
|
|
|
2013-05-03 17:35:22 +00:00
|
|
|
/**
|
|
|
|
* @brief ConductorAutoNumerotation::numerateNewConductor
|
|
|
|
* create and apply a new numerotation to @conductor_
|
|
|
|
*/
|
|
|
|
void ConductorAutoNumerotation::numerateNewConductor() {
|
2013-05-21 16:35:50 +00:00
|
|
|
if (!conductor_ || num_context.isEmpty()) return;
|
|
|
|
|
2013-05-03 17:35:22 +00:00
|
|
|
NumerotationContextCommands ncc (diagram_, num_context);
|
|
|
|
applyText(ncc.toRepresentedString());
|
|
|
|
diagram_-> setNumerotation(Diagram::Conductors, ncc.next());
|
|
|
|
}
|
2013-04-04 16:57:15 +00:00
|
|
|
|
2013-04-10 19:35:47 +00:00
|
|
|
/**
|
|
|
|
* @return true if every text of qsl is identical, else false.
|
|
|
|
*/
|
2013-04-04 16:57:15 +00:00
|
|
|
bool eachIsEqual (const QStringList &qsl) {
|
|
|
|
foreach (const QString t, qsl) {
|
|
|
|
if (qsl.at(0) != t) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|