Files
qelectrotech-source-mirror/sources/qetgraphicsitem/conductortextitem.cpp
T
ispyisail 649f06b3b8 Add View > Show to hide texts, shapes, pictures by kind (bugtracker #301)
A submenu under View (Affichage > Afficher) with one tick per kind:
symbol texts, wire texts, free texts, shapes, pictures, tables and
cross-references. Unticking one hides it on every folio of every open
project; the status bar says how many kinds are hidden. Session only,
not saved in the project.

Each item is tagged with its kind by its constructor (ShownKinds::tag),
so an item created while its kind is hidden starts hidden whatever
created it. A tag rather than type(): the cross-reference under a
contact's label is a plain QGraphicsTextItem.

Items that already hide themselves (wire text switched off, one text per
potential, a cross-reference snapped elsewhere) now go through
ShownKinds::setVisible(): the kind state can only veto a show, and
apply() re-shows only what it hid itself (hidden_key), never what an
item hid. Conductor::updateTextVisibility() replaces the same visibility
rule pasted in diagrampropertiesdialog.cpp and qetscriptapi.cpp.

Hidden items are not selectable (Qt), so Select All, copy and delete
skip them. Print, PDF and image export render the scene and leave them
out; the DXF export walks items itself and skips them through
ShownKinds::isHidden(). CrossRefItem::linkedChanged() keeps following
its slaves while hidden only by kind, so its label is right when shown.

Known limitation: QGraphicsScene::itemsBoundingRect() still counts hidden
items, so Zoom to fit and borderless image export keep their space.

Verified: tst_shownkinds (12 cases, two mutation checks) and ctest 55/55;
GUI hide/show of symbol texts, wire texts (incl. one text per potential),
free texts, shapes and cross-references returns pixel-identical folios;
with nothing hidden, --export-dxf of the 24 examples is byte-identical to
master (133 files) and --export-pdf renders identically (133 pages).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015FPuYPS4T7QuEwjNu22rXD
2026-10-03 19:44:12 +13:00

274 lines
7.3 KiB
C++

/*
Copyright 2006-2026 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/>.
*/
#include "conductortextitem.h"
#include "../shownkinds.h"
#include "../diagram.h"
#include "../diagramcommands.h"
#include "../qetgraphicsitem/conductor.h"
/**
Constructeur
@param parent_conductor Conducteur auquel ce texte est rattache
*/
ConductorTextItem::ConductorTextItem(Conductor *parent_conductor) :
DiagramTextItem(parent_conductor),
parent_conductor_(parent_conductor),
moved_by_user_(false),
rotate_by_user_(false)
{
ShownKinds::tag(this, ShownKinds::WireNumbers);
setAcceptHoverEvents(true);
}
/**
Constructeur
@param text Le texte affiche par le champ de texte
@param parent_conductor Conducteur auquel ce texte est rattache
*/
ConductorTextItem::ConductorTextItem(
const QString &text, Conductor *parent_conductor) :
DiagramTextItem(text, parent_conductor),
parent_conductor_(parent_conductor),
moved_by_user_(false),
rotate_by_user_(false)
{
ShownKinds::tag(this, ShownKinds::WireNumbers);
}
/**
Destructeur
*/
ConductorTextItem::~ConductorTextItem()
{
}
/**
@return le conducteur parent de ce champ de texte, ou 0 si celui-ci n'en a
pas
*/
Conductor *ConductorTextItem::parentConductor() const
{
return(parent_conductor_);
}
/**
@brief ConductorTextItem::fromXml
Read the properties stored in the xml element given in parameter
@param e
*/
void ConductorTextItem::fromXml(const QDomElement &e) {
//A non-finite ("nan", "inf") position or rotation is ignored,
//as if the text had never been moved or rotated
const qreal x = e.attribute("userx").toDouble();
const qreal y = e.attribute("usery").toDouble();
if (e.hasAttribute("userx") && qIsFinite(x) && qIsFinite(y)) {
setPos(x, y);
moved_by_user_ = true;
}
const qreal rotation = e.attribute("rotation").toDouble();
if (e.hasAttribute("rotation") && qIsFinite(rotation)) {
setRotation(rotation);
rotate_by_user_ = true;
}
}
/**
@return true si ce champ de texte a ete explictement deplace par
l'utilisateur, false sinon
*/
bool ConductorTextItem::wasMovedByUser() const
{
return(moved_by_user_);
}
/**
@brief ConductorTextItem::wasRotatedByUser
@return true if text was explicit moved by user else false
*/
bool ConductorTextItem::wasRotatedByUser() const
{
return(rotate_by_user_);
}
/**
@param moved_by_user true pour que la position du texte soit consideree
comme ayant ete definie par l'utilisateur (et donc soit sauvegardee), false
pour remettre le texte a sa position originelle
*/
void ConductorTextItem::forceMovedByUser(bool moved_by_user) {
if (moved_by_user == moved_by_user_) return;
moved_by_user_ = moved_by_user;
if (!moved_by_user && parent_conductor_) {
parent_conductor_ -> calculateTextItemPosition();
}
}
/**
@brief ConductorTextItem::forceRotateByUser
@param rotate_by_user true pour que la rotation du texte soit consideree
comme ayant ete definie par l'utilisateur (et donc soit sauvegardee), false
pour remettre le texte a sont angle originelle
*/
void ConductorTextItem::forceRotateByUser(bool rotate_by_user) {
if (rotate_by_user == rotate_by_user_) return;
rotate_by_user_ = rotate_by_user;
if (!rotate_by_user && parent_conductor_) {
parent_conductor_ -> calculateTextItemPosition();
}
}
/**
@brief ConductorTextItem::setPos
@param pos
*/
void ConductorTextItem::setPos(const QPointF &pos)
{
/*
* In some condition the conductor text item is outside the border of folio in the left.
* They cause a margin on the left of folio and in most case this margin is unwanted and annoying the user.
* If the text is empty and the scene position is outside the border (left and top),
* we can say that this position, is unwanted by user.
* So we move this text item to the top left of the bounding rect of parent conductors, because we sure this position is wanted by user.
*/
DiagramTextItem::setPos(pos);
if(toPlainText().isEmpty() && (scenePos().x() < 0 || scenePos().y() < 0))
{
Conductor *cond = parentConductor();
if(cond)
DiagramTextItem::setPos(cond->boundingRect().topLeft());
else
DiagramTextItem::setPos(0,0);
}
}
/**
@brief ConductorTextItem::setPos
@param x
@param y
*/
void ConductorTextItem::setPos(qreal x, qreal y)
{
QPointF p(x,y);
setPos(p);
}
/**
@brief ConductorTextItem::mousePressEvent
@param event
*/
void ConductorTextItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
before_mov_pos_ = pos();
DiagramTextItem::mousePressEvent(event);
}
/**
@brief ConductorTextItem::mouseMoveEvent
@param event
*/
void ConductorTextItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
if (textInteractionFlags() & Qt::TextEditable) QGraphicsTextItem::mouseMoveEvent(event);
else if ((flags() & QGraphicsItem::ItemIsMovable) && (event -> buttons() & Qt::LeftButton)) {
QPointF intended_pos = event ->scenePos() + m_mouse_to_origin_movement;
if (parent_conductor_) {
if (parent_conductor_->nearShape().contains(intended_pos)) {
event->modifiers() == Qt::ControlModifier ? setPos(intended_pos) : setPos(Diagram::snapToTextGrid(intended_pos));
parent_conductor_ -> setHighlighted(Conductor::Normal);
} else {
parent_conductor_ -> setHighlighted(Conductor::Alert);
}
}
}
else event -> ignore();
}
/**
@brief ConductorTextItem::mouseReleaseEvent
@param e
*/
void ConductorTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
if (flags() & QGraphicsItem::ItemIsMovable) {
if (Diagram *diagram_ptr = diagram()) {
QPointF applied_movement = pos() - before_mov_pos_;
if (!applied_movement.isNull()) {
//Create an undo object
MoveConductorsTextsCommand *undo_object = new MoveConductorsTextsCommand(diagram_ptr);
undo_object -> addTextMovement(this, before_mov_pos_, pos(), moved_by_user_);
moved_by_user_ = true;
diagram_ptr -> undoStack().push(undo_object);
}
if (parent_conductor_) {
parent_conductor_ -> setHighlighted(Conductor::None);
}
}
}
if (!(e -> modifiers() & Qt::ControlModifier)) {
QGraphicsTextItem::mouseReleaseEvent(e);
}
}
/**
When mouse over element
change m_mouse_hover to true (used in paint() function )
@param e QGraphicsSceneHoverEvent
*/
void ConductorTextItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e) {
Q_UNUSED(e);
m_mouse_hover = true;
QString str_ToolTip = toPlainText();
setToolTip( str_ToolTip );
update();
}
/**
When mouse over element leave the position
change m_mouse_hover to false(used in paint() function )
@param e QGraphicsSceneHoverEvent
*/
void ConductorTextItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *e)
{
Q_UNUSED(e);
m_mouse_hover = false;
update();
}
/**
Do nothing default function .
@param e QGraphicsSceneHoverEvent
*/
void ConductorTextItem::hoverMoveEvent(QGraphicsSceneHoverEvent *e)
{
QGraphicsTextItem::hoverMoveEvent(e);
}