Files
qelectrotech-source-mirror/sources/factory/elementpicturefactory.h
T
ispyisail a3594c6633 Add a generic device: a box symbol with terminals on any side
Edit > Add > Generic device (also on the Add toolbar, and found by the
command search as diagrameditor.add_generic_device) opens a three-page
wizard for devices used too rarely to draw a symbol for, such as a VSD
or a power supply:

1. Layout: terminals per side (0 for none), a gap every N terminals,
   divider marks on gaps, groups lined up across opposite sides, pitch
   and extra size.
2. Terminals: a table per side and the same list as one line, where
   In{4} is In1..In4, Q{z4} is Q0..Q3, X{3-6} is X3..X6 and an empty
   entry is a gap; number pins 1..n counter-clockwise; paste rows of
   side, name, function from a spreadsheet or a CSV file.
3. Information: name, label, manufacturer, reference, description.

A preview beside every page shows the box as it will be placed. Finish,
available from the first page, writes an ordinary symbol to the folder
"Generic devices" of the project's collection and puts it on the cursor.
The box sizes itself to its terminals and their names; every terminal
lands on the grid and gets a uuid. No new XML: any QElectroTech reads it.

The layout is pure (sources/genericdevice/genericdevice.cpp) and tested
by tst_genericdevice. ElementPictureFactory gains pictureFromDefinition()
to draw the preview with the same code as a placed element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 23:31:26 +13:00

131 lines
4.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/>.
*/
#ifndef ELEMENTPICTUREFACTORY_H
#define ELEMENTPICTUREFACTORY_H
#include <QMutex>
#include <QSharedPointer>
#include <QHash>
#include <QPair>
#include <QTransform>
class ElementsLocation;
class QPicture;
class QUuid;
class QDomElement;
class QPainter;
class QGraphicsSimpleTextItem;
/**
@brief The ElementPictureFactory class
This class is singleton factory, use
to create and get the picture use by elements
*/
class ElementPictureFactory
{
public :
struct primitives
{
QList<QLineF> m_lines;
QList<QRectF> m_rectangles;
QList<QRectF> m_circles;
QList<QVector<QPointF>> m_polygons;
QList<QVector<qreal>> m_arcs;
QList<QGraphicsSimpleTextItem*> m_texts;
};
/**
@brief instance
@return The instance of the factory
*/
static ElementPictureFactory* instance()
{
static QMutex mutex;
if (!m_factory)
{
mutex.lock();
if (!m_factory) {
m_factory = new ElementPictureFactory();
}
mutex.unlock();
}
return m_factory;
}
/**
@brief dropInstance
Drop the instance of factory
*/
static void dropInstance()
{
static QMutex mutex;
if (m_factory)
{
mutex.lock();
delete m_factory;
m_factory = nullptr;
mutex.unlock();
}
}
void getPictures(const ElementsLocation &location, QPicture &picture, QPicture &low_picture);
void getReadablePictures(const ElementsLocation &location, const QTransform &texts_transform,
QPicture &picture, QPicture &low_picture);
QPixmap pixmap(const ElementsLocation &location);
ElementPictureFactory::primitives getPrimitives(const ElementsLocation &location);
void dropCache(const ElementsLocation &location);
QPicture pictureFromDefinition(const QDomElement &definition) const;
private:
ElementPictureFactory() {}
ElementPictureFactory (const ElementPictureFactory &);
ElementPictureFactory operator= (const ElementPictureFactory &);
~ElementPictureFactory();
static QUuid cacheKey(const ElementsLocation &location);
static int readableKey(const QTransform &texts_transform);
bool build(const ElementsLocation &location, QPicture *picture=nullptr, QPicture *low_picture=nullptr,
const QTransform &texts_transform=QTransform());
void parseElement(const QDomElement &dom, QPainter &painter, primitives &prim) const;
void parseLine (const QDomElement &dom, QPainter &painter, primitives &prim) const;
void parseRect (const QDomElement &dom, QPainter &painter, primitives &prim) const;
void parseEllipse(const QDomElement &dom, QPainter &painter, primitives &prim) const;
void parseCircle (const QDomElement &dom, QPainter &painter, primitives &prim) const;
void parseArc (const QDomElement &dom, QPainter &painter, primitives &prim) const;
void parsePolygon(const QDomElement &dom, QPainter &painter, primitives &prim) const;
void parseText (const QDomElement &dom, QPainter &painter, primitives &prim) const;
void parsePlcTable(const QDomElement &dom, const QDomElement &plc_data, QPainter &painter) const;
void setPainterStyle(const QDomElement &dom, QPainter &painter) const;
QHash<QUuid, QPicture> m_pictures_H;
QHash<QUuid, QPicture> m_low_pictures_H;
/// Drawings whose texts read normally once an element has
/// mirrored or turned them, by element and readableKey(), see
/// getReadablePictures()
QHash<QPair<QUuid, int>, QPicture> m_readable_pictures_H;
QHash<QPair<QUuid, int>, QPicture> m_readable_low_pictures_H;
/// What build() undoes on each text, read by parseText()
QTransform m_build_texts_undo;
QHash<QUuid, QPixmap> m_pixmap_H;
QHash<QUuid, primitives> m_primitives_H;
static ElementPictureFactory* m_factory;
};
#endif // ELEMENTPICTUREFACTORY_H