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 void TerminalData::toSettings(QSettings &settings, const QString prefix) const
{ {
Q_UNUSED(settings); Q_UNUSED(settings)
Q_UNUSED(prefix); Q_UNUSED(prefix)
} }
/** /**

View File

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

View File

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