clean terminal and terminaldata class

This commit is contained in:
joshua 2021-02-05 21:20:37 +01:00
parent e2ebb445a8
commit a00404bc9f
3 changed files with 58 additions and 131 deletions

View File

@ -44,8 +44,8 @@ void TerminalData::setParent(QGraphicsObject* parent)
void TerminalData::toSettings(QSettings &settings, const QString prefix) const
{
Q_UNUSED(settings);
Q_UNUSED(prefix);
Q_UNUSED(settings)
Q_UNUSED(prefix)
}
/**

View File

@ -43,101 +43,48 @@ const qreal Terminal::Z = 1000;
*/
void Terminal::init()
{
hovered_color_ = Terminal::neutralColor;
// calcul de la position du point d'amarrage a l'element
//Calcul the docking point of the element
//m_pos of d is the docking point of conductor
dock_elmt_ = d->m_pos;
switch(d->m_orientation) {
case Qet::North: dock_elmt_ += QPointF(0, Terminal::terminalSize); break;
case Qet::East : dock_elmt_ += QPointF(-Terminal::terminalSize, 0); break;
case Qet::West : dock_elmt_ += QPointF(Terminal::terminalSize, 0); break;
case Qet::South:
default : dock_elmt_ += QPointF(0, -Terminal::terminalSize);
case Qet::South: dock_elmt_ += QPointF(0, -Terminal::terminalSize); break;
}
// QRectF null
br_ = new QRectF();
previous_terminal_ = nullptr;
// divers
//Calcul the bounding rect
qreal dcx = d->m_pos.x();
qreal dcy = d->m_pos.y();
qreal dex = dock_elmt_.x();
qreal dey = dock_elmt_.y();
QPointF origin = (dcx <= dex && dcy <= dey ? d->m_pos : dock_elmt_);
origin += QPointF(-3.0, -3.0);
qreal w = qAbs(dcx - dex) + 7;
qreal h = qAbs(dcy - dey) + 7;
m_br = QRectF(origin, QSizeF(w, h));
setAcceptHoverEvents(true);
setAcceptedMouseButtons(Qt::LeftButton);
hovered_ = false;
setToolTip(QObject::tr("Borne", "tooltip"));
setZValue(Z);
}
/*!
\brief Terminal::init
Additionaly to the init above, this method stores position and orientation into the data class
\param pf
\param o
\param number
\param name
\param hiddenName
*/
void Terminal::init(QPointF pf, Qet::Orientation o)
{
// definition du pount d'amarrage pour un conducteur
d->m_pos = pf;
// definition de l'orientation de la borne (par defaut : sud)
if (o < Qet::North || o > Qet::West) d->m_orientation = Qet::South;
else d->m_orientation = o;
init();
}
/**
initialise une borne
@param pf position du point d'amarrage pour un conducteur
@param o orientation de la borne : Qt::Horizontal ou Qt::Vertical
@param e Element auquel cette borne appartient
*/
Terminal::Terminal(QPointF pf, Qet::Orientation o, Element *e) :
QGraphicsObject(e),
d(new TerminalData(this)),
parent_element_ (e)
{
init(pf, o);
}
/**
initialise une borne
@param pf_x Abscisse du point d'amarrage pour un conducteur
@param pf_y Ordonnee du point d'amarrage pour un conducteur
@param o orientation de la borne : Qt::Horizontal ou Qt::Vertical
@param e Element auquel cette borne appartient
*/
Terminal::Terminal(qreal pf_x, qreal pf_y, Qet::Orientation o, Element *e) :
QGraphicsObject(e),
d(new TerminalData(this)),
parent_element_ (e)
{
init(QPointF(pf_x, pf_y), o);
}
Terminal::Terminal(TerminalData* data, Element* e) :
QGraphicsObject(e),
d(data),
parent_element_(e)
{
#if TODO_LIST
#pragma message("@TODO what is when multiple parents exist. So the other relation is lost.")
#endif
// TODO: what is when multiple parents exist. So the other relation is lost.
d->setParent(this);
init();
}
/**
Destructeur
La destruction de la borne entraine la destruction des conducteurs
associes.
*/
Terminal::~Terminal()
{
foreach(Conductor *c, conductors_) delete c;
delete br_;
* @brief Terminal::~Terminal
* Destruction of the terminal, and also docked conductor
*/
Terminal::~Terminal() {
qDeleteAll(m_conductors_list);
}
/**
@ -182,11 +129,11 @@ bool Terminal::addConductor(Conductor *conductor)
? conductor->terminal2 : conductor->terminal1;
//Check if this terminal isn't already linked with other_terminal
foreach (Conductor* cond, conductors_)
foreach (Conductor* cond, m_conductors_list)
if (cond -> terminal1 == other_terminal || cond -> terminal2 == other_terminal)
return false; //They already a conductor linked to this and other_terminal
conductors_.append(conductor);
m_conductors_list.append(conductor);
emit conductorWasAdded(conductor);
return(true);
}
@ -198,9 +145,9 @@ bool Terminal::addConductor(Conductor *conductor)
*/
void Terminal::removeConductor(Conductor *conductor)
{
int index = conductors_.indexOf(conductor);
int index = m_conductors_list.indexOf(conductor);
if (index == -1) return;
conductors_.removeAt(index);
m_conductors_list.removeAt(index);
emit conductorWasRemoved(conductor);
}
@ -257,10 +204,10 @@ void Terminal::paint(
painter -> drawLine(c, e);
// dessin du point d'amarrage au conducteur en bleu
t.setColor(hovered_color_);
t.setColor(m_hovered_color);
painter -> setPen(t);
painter -> setBrush(hovered_color_);
if (hovered_) {
painter -> setBrush(m_hovered_color);
if (m_hovered) {
painter -> setRenderHint(QPainter::Antialiasing, true);
painter -> drawEllipse(QRectF(c.x() - 2.5, c.y() - 2.5, 5.0, 5.0));
} else painter -> drawPoint(c);
@ -392,21 +339,8 @@ QLineF Terminal::HelpLine() const
@brief Terminal::boundingRect
@return Le rectangle (en precision flottante) delimitant la borne et ses alentours.
*/
QRectF Terminal::boundingRect() const
{
if (br_ -> isNull())
{
qreal dcx = d->m_pos.x();
qreal dcy = d->m_pos.y();
qreal dex = dock_elmt_.x();
qreal dey = dock_elmt_.y();
QPointF origin = (dcx <= dex && dcy <= dey ? d->m_pos : dock_elmt_);
origin += QPointF(-3.0, -3.0);
qreal w = qAbs((int)(dcx - dex)) + 7;
qreal h = qAbs((int)(dcy - dey)) + 7;
*br_ = QRectF(origin, QSizeF(w, h));
}
return(*br_);
QRectF Terminal::boundingRect() const {
return m_br;
}
/**
@ -477,7 +411,7 @@ Terminal* Terminal::alignedWithTerminal() const
*/
void Terminal::hoverEnterEvent(QGraphicsSceneHoverEvent *)
{
hovered_ = true;
m_hovered = true;
update();
}
@ -493,7 +427,7 @@ void Terminal::hoverMoveEvent(QGraphicsSceneHoverEvent *) {}
*/
void Terminal::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
{
hovered_ = false;
m_hovered = false;
update();
}
@ -523,11 +457,11 @@ void Terminal::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
//setCursor(Qt::CrossCursor);
// d'un mouvement a l'autre, il faut retirer l'effet hover de la borne precedente
if (previous_terminal_) {
if (previous_terminal_ == this) hovered_ = true;
else previous_terminal_ -> hovered_ = false;
previous_terminal_ -> hovered_color_ = previous_terminal_ -> neutralColor;
previous_terminal_ -> update();
if (m_previous_terminal) {
if (m_previous_terminal == this) m_hovered = true;
else m_previous_terminal -> m_hovered = false;
m_previous_terminal -> m_hovered_color = m_previous_terminal -> neutralColor;
m_previous_terminal -> update();
}
Diagram *diag = diagram();
@ -554,18 +488,18 @@ void Terminal::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
// si le qgi est une borne...
Terminal *other_terminal = qgraphicsitem_cast<Terminal *>(qgi);
if (!other_terminal) return;
previous_terminal_ = other_terminal;
m_previous_terminal = other_terminal;
// s'il s'agit d'une borne, on lui applique l'effet hover approprie
if (!canBeLinkedTo(other_terminal)) {
other_terminal -> hovered_color_ = forbiddenColor;
other_terminal -> m_hovered_color = forbiddenColor;
} else if (other_terminal -> conductorsCount()) {
other_terminal -> hovered_color_ = warningColor;
other_terminal -> m_hovered_color = warningColor;
} else {
other_terminal -> hovered_color_ = allowedColor;
other_terminal -> m_hovered_color = allowedColor;
}
other_terminal -> hovered_ = true;
other_terminal -> m_hovered = true;
other_terminal -> update();
}
@ -576,8 +510,8 @@ void Terminal::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
*/
void Terminal::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{
previous_terminal_ = nullptr;
hovered_color_ = neutralColor;
m_previous_terminal = nullptr;
m_hovered_color = neutralColor;
if (!diagram()) return;
@ -592,8 +526,8 @@ void Terminal::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
Terminal *other_terminal = qgraphicsitem_cast<Terminal *>(qgi);
if (!other_terminal) return;
other_terminal -> hovered_color_ = neutralColor;
other_terminal -> hovered_ = false;
other_terminal -> m_hovered_color = neutralColor;
other_terminal -> m_hovered = false;
//We stop her if we can't link this terminal with other terminal
if (!canBeLinkedTo(other_terminal)) return;
@ -650,7 +584,7 @@ void Terminal::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
*/
void Terminal::updateConductor()
{
foreach (Conductor *conductor, conductors_)
foreach (Conductor *conductor, m_conductors_list)
conductor->updatePath();
}
@ -663,7 +597,7 @@ bool Terminal::isLinkedTo(Terminal *other_terminal) {
if (other_terminal == this) return(false);
bool already_linked = false;
foreach (Conductor *c, conductors_) {
foreach (Conductor *c, m_conductors_list) {
if (c -> terminal1 == other_terminal || c -> terminal2 == other_terminal) {
already_linked = true;
break;
@ -696,7 +630,7 @@ bool Terminal::canBeLinkedTo(Terminal *other_terminal)
*/
QList<Conductor *> Terminal::conductors() const
{
return(conductors_);
return(m_conductors_list);
}
/**

View File

@ -42,8 +42,6 @@ class Terminal : public QGraphicsObject
// constructors, destructor
public:
Terminal(QPointF, Qet::Orientation, Element * = nullptr);
Terminal(qreal, qreal, Qet::Orientation, Element * = nullptr);
Terminal(TerminalData* data, Element *e = nullptr);
~Terminal() override;
@ -129,19 +127,14 @@ class Terminal : public QGraphicsObject
QPointF dock_elmt_;
private:
/// List of conductors attached to the terminal
QList<Conductor *> conductors_;
/**
Pointer to a rectangle representing the terminal bounding rect;
used to calculate the bounding rect once only;
used a pointer because boundingRect() is supposed to be const.
*/
QRectF *br_{nullptr};
QList<Conductor *> m_conductors_list;
QRectF m_br;
/// Last terminal seen through an attached conductor
Terminal *previous_terminal_;
Terminal *m_previous_terminal = nullptr;
/// Whether the mouse pointer is hovering the terminal
bool hovered_;
bool m_hovered = false;
/// Color used for the hover effect
QColor hovered_color_;
QColor m_hovered_color = Terminal::neutralColor;
private:
void init();
@ -154,7 +147,7 @@ class Terminal : public QGraphicsObject
*/
inline int Terminal::conductorsCount() const
{
return(conductors_.size());
return(m_conductors_list.size());
}
QList<Terminal *> relatedPotentialTerminal (const Terminal *terminal,