Files
qelectrotech-source-mirror/sources/qetgraphicsitem/crossrefitem.h
T
Laurent Trinques cd76b6a1d6 This adds native, clickable hyperlinks to PDF exports: cross-references jump
directly to the related component on its folio, framing the target element.

When a project is exported to PDF, every cross-reference becomes an internal
link. Four kinds are covered:

- **Master → contact**: the contact list on a coil/relay (`CrossRefItem`)
- **Folio report → report**: report element labels (`DynamicElementTextItem`)
- **Slave → master**: the `(folio-position)` reference shown on a slave
  (both standalone `DynamicElementTextItem` and grouped `ElementTextItemGroup`)

Clicking a link navigates **inside** the open document (no new viewer
instance) and zooms to frame the target element.

1. **Injection** (`printDiagram`, only when the paint engine is a `QPdfEngine`):
   link rectangles are added with `QPdfEngine::drawHyperlink()`. The scene→page
   mapping is rebuilt to match exactly what `QGraphicsScene::render()` does
   (top-left anchored, `KeepAspectRatio`, **no centering**), and rectangles are
   passed in device pixels — `pageMatrix()` already applies the 72/resolution
   scale and Y-flip internally.

2. Each link URL encodes the target page and the target element's rectangle, in
   PDF points on its own page: `#page=N&fitr=L_B_R_T`.

3. **Post-processing** (`pdfConvertUriToGoTo`, run after the painter is closed):
   the `/S /URI` annotations are rewritten to native `/S /GoTo` actions with a
   `/D [pageObj 0 R /FitR L B R T]` destination, and the xref table is rebuilt.
   Pages are enumerated from the `/Pages /Kids` tree (reliable), not by scanning
   for `/Type /Page` in raw bytes.

- `sources/print/projectprintwindow.{cpp,h}` — injection + post-processing
- `sources/qetgraphicsitem/crossrefitem.{cpp,h}` — `hoveredContactsMap()` accessor; store text rect for hit area
- `sources/qetgraphicsitem/dynamicelementtextitem.h` — `slaveXrefItem()` / `masterElement()` accessors
- `sources/qetgraphicsitem/elementtextitemgroup.h` — `slaveXrefItem()` accessor
- `qelectrotech.pro`, `cmake/qet_compilation_vars.cmake` — enable Qt gui-private headers (`<private/qpdf_p.h>`)

- **Fit-to-page mode only.** Links are not injected in tiled mode (multiple
  pages per folio), which would require a per-tile transform.
- Uses Qt private API (`QPdfEngine::drawHyperlink`), stable since Qt 4 but not
  part of the public API; the build links against `gui-private`.
- Page-tree enumeration assumes the flat `/Kids` array Qt produces (no nested
  page trees).
- The frame zoom is controlled by two constants in `destRectPdf` (`pad`,
  `minSide`) and can be tuned.
- Tested on Qt5; the `/Kids` parsing and `pageMatrix` behaviour are identical on
  Qt6.
2026-05-30 18:48:28 +02:00

139 lines
4.1 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/>.
*/
#ifndef CROSSREFITEM_H
#define CROSSREFITEM_H
#include "../properties/xrefproperties.h"
#include <QGraphicsObject>
#include <QMultiMap>
class Element;
class DynamicElementTextItem;
class ElementTextItemGroup;
/**
@brief The CrossRefItem class
This clas provide an item, for show the cross reference,
like the contacts linked to a coil.
The item setpos automatically when parent move.
All slave displayed in cross ref will be updated
when folio position change in the project.
It's the responsibility of the master element
to inform displayed slave are moved,
by calling the slot updateLabel
By default master element is the parent graphics item of this Xref,
but if the Xref must be snap to the label of master,
the label become the parent of this Xref.
This behavior can be changed at anytime by calling setProperties.
*/
class CrossRefItem : public QGraphicsObject
{
Q_OBJECT
//Methods
public:
explicit CrossRefItem(Element *elmt);
explicit CrossRefItem(
Element *elmt, DynamicElementTextItem *text);
explicit CrossRefItem(
Element *elmt, ElementTextItemGroup *group);
~CrossRefItem() override;
private:
void init();
void setUpConnection();
public:
enum { Type = UserType + 1009 };
int type() const override { return Type; }
/**
@brief The CONTACTS enum
*/
enum CONTACTS {
NO = 1,
NC = 2,
NOC = 3,
SW = 4,
Power = 8,
DelayOn = 16,
DelayOff = 32,
DelayOnOff = 64,
Delay = 112,
Other = 128
};
QRectF boundingRect() const override;
QPainterPath shape() const override;
QString elementPositionText(
const Element *elmt,
const bool &add_prefix = false) const;
public slots:
void updateProperties();
void updateLabel();
void autoPos();
protected:
bool sceneEvent(QEvent *event) override;
void paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget) override;
void mouseDoubleClickEvent (
QGraphicsSceneMouseEvent * event ) override;
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
private:
void linkedChanged();
void buildHeaderContact(QPainter &painter, QPointF no_pos, QPointF nc_pos);
void setUpCrossBoundingRect(QPainter &painter);
void drawAsCross(QPainter &painter);
void drawAsContacts(QPainter &painter);
QRectF drawContact(QPainter &painter, int flags, Element *elmt, int pole_index = 0);
void fillCrossRef(QPainter &painter);
void AddExtraInfo(QPainter &painter, const QString&);
QList<Element *> NOElements() const;
QList<Element *> NCElements() const;
//Attributes
private:
Element *m_element; //element to display the cross reference
QRectF m_bounding_rect;
QPainterPath m_shape_path;
XRefProperties m_properties;
int m_drawed_contacts;
bool m_update_map = false;
QMultiMap <Element *, QRectF> m_hovered_contacts_map;
Element *m_hovered_contact = nullptr;
DynamicElementTextItem *m_text = nullptr;
ElementTextItemGroup *m_group = nullptr;
QList <QMetaObject::Connection> m_slave_connection;
QList <QMetaObject::Connection> m_update_connection;
public:
/// Returns the map of linked elements and their clickable rects (local coords).
/// Used by the PDF export to inject hyperlink annotations.
const QMultiMap<Element *, QRectF> &hoveredContactsMap() const
{ return m_hovered_contacts_map; }
};
#endif // CROSSREFITEM_H