24 #include <QDomElement> 26 #include <QTextDocument> 29 #include <QAbstractTextDocumentLayout> 30 #include <QGraphicsSimpleTextItem> 44 if(!location.
exist()) {
48 QUuid uuid = location.
uuid();
49 if(Q_UNLIKELY(uuid.isNull()))
51 build(location, &picture, &low_picture);
78 QUuid uuid = location.
uuid();
86 QDomElement dom = location.
xml();
88 int w = dom.attribute(
"width").toInt();
89 int h = dom.attribute(
"height").toInt();
93 int hsx = qMin(dom.attribute(
"hotspot_x").toInt(), w);
94 int hsy = qMin(dom.attribute(
"hotspot_y").toInt(), h);
97 pix.fill(QColor(255, 255, 255, 0));
99 QPainter painter(&pix);
100 painter.setRenderHint(QPainter::Antialiasing,
true);
101 painter.setRenderHint(QPainter::SmoothPixmapTransform,
true);
102 painter.translate(hsx, hsy);
105 if (!uuid.isNull()) {
130 qDeleteAll(p.m_texts);
146 QDomElement dom = location.
xml();
149 if (dom.hasAttribute(
"version"))
152 qreal element_version = dom.attribute(
"version").toDouble(&conv_ok);
153 if (conv_ok &&
QET::version.toDouble() < element_version)
155 std::cerr << qPrintable(
157 " a été enregistré avec une version" 158 " ultérieure de QElectroTech.")
164 int w, h, hot_x, hot_y;
177 painter.begin(picture);
182 painter.setRenderHint(QPainter::Antialiasing,
true);
183 painter.setRenderHint(QPainter::TextAntialiasing,
true);
184 painter.setRenderHint(QPainter::SmoothPixmapTransform,
true);
187 QPainter low_painter;
190 low_painter.begin(low_picture);
193 low_painter.begin(&low_pic);
195 low_painter.setRenderHint(QPainter::Antialiasing,
true);
196 low_painter.setRenderHint(QPainter::TextAntialiasing,
true);
197 low_painter.setRenderHint(QPainter::SmoothPixmapTransform,
true);
201 tmp.setCosmetic(
true);
202 low_painter.setPen(tmp);
205 for (QDomNode node = dom.firstChild() ; !node.isNull() ; node = node.nextSibling())
207 QDomElement elmts = node.toElement();
208 if (elmts.isNull()) {
212 if (elmts.tagName() ==
"description")
215 for (QDomNode n = node.firstChild() ; !n.isNull() ; n = n.nextSibling())
217 QDomElement qde = n.toElement();
245 if (dom.tagName() ==
"line") (
parseLine (dom, painter, prim));
246 else if (dom.tagName() ==
"rect") (
parseRect (dom, painter, prim));
247 else if (dom.tagName() ==
"ellipse") (
parseEllipse(dom, painter, prim));
248 else if (dom.tagName() ==
"circle") (
parseCircle (dom, painter, prim));
249 else if (dom.tagName() ==
"arc") (
parseArc (dom, painter, prim));
250 else if (dom.tagName() ==
"polygon") (
parsePolygon(dom, painter, prim));
251 else if (dom.tagName() ==
"text") (
parseText (dom, painter, prim));
257 qreal x1, y1, x2, y2;
265 qreal length1, length2;
271 QPen t = painter.pen();
272 t.setJoinStyle(Qt::MiterJoin);
275 QLineF line(x1, y1, x2, y2);
279 QPointF point1(line.p1());
280 QPointF point2(line.p2());
282 qreal line_length(line.length());
283 qreal pen_width = painter.pen().widthF();
286 bool draw_1st_end, draw_2nd_end;
288 draw_1st_end = first_end && reduced_line_length >= 0;
294 draw_2nd_end = second_end && reduced_line_length >= 0;
297 QPointF start_point, stop_point;
301 painter.drawEllipse(QRectF(four_points1[0] - QPointF(length1, length1), QSizeF(length1 * 2.0, length1 * 2.0)));
302 start_point = four_points1[1];
304 painter.drawPolygon(QPolygonF() << four_points1[1] << four_points1[2] << point1 << four_points1[3]);
305 start_point = four_points1[1];
307 painter.drawPolyline(QPolygonF() << four_points1[3] << point1 << four_points1[2]);
308 start_point = point1;
311 painter.drawPolygon(QPolygonF() << four_points1[0] << four_points1[2] << point1 << four_points1[3]);
312 start_point = four_points1[0];
317 start_point = QLineF(start_point, point2).pointAt(pen_width / 2.0 / line_length);
320 start_point = point1;
327 painter.drawEllipse(QRectF(four_points2[0] - QPointF(length2, length2), QSizeF(length2 * 2.0, length2 * 2.0)));
328 stop_point = four_points2[1];
330 painter.drawPolygon(QPolygonF() << four_points2[2] << point2 << four_points2[3] << four_points2[1]);
331 stop_point = four_points2[1];
333 painter.drawPolyline(QPolygonF() << four_points2[3] << point2 << four_points2[2]);
336 painter.drawPolygon(QPolygonF() << four_points2[0] << four_points2[2] << point2 << four_points2[3] << four_points2[0]);
337 stop_point = four_points2[0];
342 stop_point = QLineF(point1, stop_point).pointAt((line_length - (pen_width / 2.0)) / line_length);
348 painter.drawLine(start_point, stop_point);
356 qreal rect_x, rect_y, rect_w, rect_h, rect_rx, rect_ry;
361 rect_rx = dom.attribute(
"rx",
"0").toDouble();
362 rect_ry = dom.attribute(
"ry",
"0").toDouble();
364 prim.
m_rectangles << QRectF(rect_x, rect_y, rect_w, rect_h);
369 QPen p = painter.pen();
370 p.setJoinStyle(Qt::MiterJoin);
373 painter.drawRoundedRect(QRectF(rect_x, rect_y, rect_w, rect_h), rect_rx, rect_ry);
380 qreal ellipse_x, ellipse_y, ellipse_l, ellipse_h;
389 arc.push_back(ellipse_x);
390 arc.push_back(ellipse_y);
391 arc.push_back(ellipse_l);
392 arc.push_back(ellipse_h);
397 painter.drawEllipse(QRectF(ellipse_x, ellipse_y, ellipse_l, ellipse_h));
404 qreal cercle_x, cercle_y, cercle_r;
410 QRectF circle_bounding_rect(cercle_x, cercle_y, cercle_r, cercle_r);
414 painter.drawEllipse(circle_bounding_rect);
421 qreal arc_x, arc_y, arc_l, arc_h, arc_s, arc_a;
433 arc.push_back(arc_x);
434 arc.push_back(arc_y);
435 arc.push_back(arc_l);
436 arc.push_back(arc_h);
437 arc.push_back(arc_s);
438 arc.push_back(arc_a);
441 painter.drawArc(QRectF(arc_x, arc_y, arc_l, arc_h), (
int)(arc_s * 16), (
int)(arc_a * 16));
456 QVector<QPointF> points;
457 for (
int j = 1 ; j < i ; ++ j) {
461 dom.attribute(QString(
"x%1").arg(j)).toDouble(),
462 dom.attribute(QString(
"y%1").arg(j)).toDouble()
469 if (dom.attribute(
"closed") ==
"false") painter.drawPolyline(points.data(), i-1);
471 painter.drawPolygon(points.data(), i-1);
474 points.push_back(points[0]);
486 if (dom.tagName() !=
"text") {
495 if (dom.hasAttribute(
"size")) {
498 else if (dom.hasAttribute(
"font")) {
499 font_.fromString(dom.attribute(
"font"));
502 QColor text_color(dom.attribute(
"color",
"#000000"));
506 QTextDocument text_document;
507 text_document.setDefaultFont(font_);
508 text_document.setPlainText(dom.attribute(
"text"));
510 painter.setTransform(QTransform(),
false);
511 painter.translate(dom.attribute(
"x").toDouble(), dom.attribute(
"y").toDouble());
512 painter.rotate(dom.attribute(
"rotation",
"0").toDouble());
520 QFontMetrics qfm(font_);
521 QPointF qpainter_offset(0.0, -qfm.ascent());
524 text_document.setDocumentMargin(0.0);
526 painter.translate(qpainter_offset);
529 QAbstractTextDocumentLayout::PaintContext ctx;
530 ctx.palette.setColor(QPalette::Text, text_color);
531 text_document.documentLayout() -> draw(&painter, ctx);
534 QGraphicsSimpleTextItem *qgsti =
new QGraphicsSimpleTextItem();
535 qgsti->setText(dom.attribute(
"text"));
536 qgsti->setFont(font_);
537 qgsti->setPos(dom.attribute(
"x").toDouble(), dom.attribute(
"y").toDouble());
538 qgsti->setRotation(dom.attribute(
"rotation",
"0").toDouble());
552 QPen pen = painter.pen();
553 QBrush brush = painter.brush();
555 pen.setJoinStyle(Qt::BevelJoin);
556 pen.setCapStyle(Qt::SquareCap);
559 const QStringList styles = dom.attribute(
"style").split(
";", QString::SkipEmptyParts);
561 QRegExp rx(
"^\\s*([a-z-]+)\\s*:\\s*([a-z-]+)\\s*$");
562 for (QString style : styles) {
563 if (rx.exactMatch(style)) {
564 QString style_name = rx.cap(1);
565 QString style_value = rx.cap(2);
566 if (style_name ==
"line-style") {
567 if (style_value ==
"dashed") pen.setStyle(Qt::DashLine);
568 else if (style_value ==
"dotted") pen.setStyle(Qt::DotLine);
569 else if (style_value ==
"dashdotted") pen.setStyle(Qt::DashDotLine);
570 else if (style_value ==
"normal") pen.setStyle(Qt::SolidLine);
571 }
else if (style_name ==
"line-weight") {
572 if (style_value ==
"none") pen.setColor(QColor(0, 0, 0, 0));
573 else if (style_value ==
"thin") pen.setWidth(0);
574 else if (style_value ==
"normal") pen.setWidthF(1.0);
575 else if (style_value ==
"hight") pen.setWidthF(2.0);
576 else if (style_value ==
"eleve") pen.setWidthF(5.0);
578 }
else if (style_name ==
"filling") {
579 if (style_value ==
"white") {
580 brush.setStyle(Qt::SolidPattern);
581 brush.setColor(Qt::white);
582 }
else if (style_value ==
"black") {
583 brush.setStyle(Qt::SolidPattern);
584 brush.setColor(Qt::black);
585 }
else if (style_value ==
"blue") {
586 brush.setStyle(Qt::SolidPattern);
587 brush.setColor(Qt::blue);
588 }
else if (style_value ==
"red") {
589 brush.setStyle(Qt::SolidPattern);
590 brush.setColor(Qt::red);
591 }
else if (style_value ==
"green") {
592 brush.setStyle(Qt::SolidPattern);
593 brush.setColor(Qt::green);
594 }
else if (style_value ==
"gray") {
595 brush.setStyle(Qt::SolidPattern);
596 brush.setColor(Qt::gray);
597 }
else if (style_value ==
"brun") {
598 brush.setStyle(Qt::SolidPattern);
599 brush.setColor(QColor(97, 44, 0));
600 }
else if (style_value ==
"yellow") {
601 brush.setStyle(Qt::SolidPattern);
602 brush.setColor(Qt::yellow);
603 }
else if (style_value ==
"cyan") {
604 brush.setStyle(Qt::SolidPattern);
605 brush.setColor(Qt::cyan);
606 }
else if (style_value ==
"magenta") {
607 brush.setStyle(Qt::SolidPattern);
608 brush.setColor(Qt::magenta);
609 }
else if (style_value ==
"lightgray") {
610 brush.setStyle(Qt::SolidPattern);
611 brush.setColor(Qt::lightGray);
612 }
else if (style_value ==
"orange") {
613 brush.setStyle(Qt::SolidPattern);
614 brush.setColor(QColor(255, 128, 0));
615 }
else if (style_value ==
"purple") {
616 brush.setStyle(Qt::SolidPattern);
617 brush.setColor(QColor(136, 28, 168));
618 }
else if (style_value ==
"hor") {
619 brush.setStyle(Qt::HorPattern);
620 brush.setColor(Qt::black);
621 }
else if (style_value ==
"ver") {
622 brush.setStyle(Qt::VerPattern);
623 brush.setColor(Qt::black);
624 }
else if (style_value ==
"bdiag") {
625 brush.setStyle(Qt::BDiagPattern);
626 brush.setColor(Qt::black);
627 }
else if (style_value ==
"fdiag") {
628 brush.setStyle(Qt::FDiagPattern);
629 brush.setColor(Qt::black);
630 }
else if (style_value ==
"none") {
631 brush.setStyle(Qt::NoBrush);
633 }
else if (style_name ==
"color") {
634 if (style_value ==
"black") {
635 pen.setColor(QColor(0, 0, 0, pen.color().alpha()));
636 }
else if (style_value ==
"white") {
637 pen.setColor(QColor(255, 255, 255, pen.color().alpha()));
638 }
else if (style_value ==
"red") {
639 pen.setColor(Qt::red);
640 }
else if (style_value ==
"blue") {
641 pen.setColor(Qt::blue);
642 }
else if (style_value ==
"green") {
643 pen.setColor(Qt::green);
644 }
else if (style_value ==
"gray") {
645 pen.setColor(Qt::gray);
646 }
else if (style_value ==
"brun") {
647 pen.setColor(QColor(97, 44, 0));
648 }
else if (style_value ==
"yellow") {
649 pen.setColor(Qt::yellow);
650 }
else if (style_value ==
"cyan") {
651 pen.setColor(Qt::cyan);
652 }
else if (style_value ==
"magenta") {
653 pen.setColor(Qt::magenta);
654 }
else if (style_value ==
"lightgray") {
655 pen.setColor(Qt::lightGray);
656 }
else if (style_value ==
"orange") {
657 pen.setColor(QColor(255, 128, 0));
658 }
else if (style_value ==
"purple") {
659 pen.setColor(QColor(136, 28, 168));
660 }
else if (style_value ==
"none") {
661 pen.setBrush(Qt::transparent);
668 painter.setBrush(brush);
EndType
This enum lists the various available endings for line primitives when drawing an electrical element...
void parseCircle(const QDomElement &dom, QPainter &painter, primitives &prim) const
bool attributeIsAnInteger(const QDomElement &, const QString &, int *=nullptr)
static ElementPictureFactory * m_factory
QHash< QUuid, primitives > m_primitives_H
QHash< QUuid, QPicture > m_pictures_H
void parseEllipse(const QDomElement &dom, QPainter &painter, primitives &prim) const
void parseElement(const QDomElement &dom, QPainter &painter, primitives &prim) const
void setPainterStyle(const QDomElement &dom, QPainter &painter) const
ElementPictureFactory::setPainterStyle apply the style store in dom to painter.
void parseText(const QDomElement &dom, QPainter &painter, primitives &prim) const
void parseArc(const QDomElement &dom, QPainter &painter, primitives &prim) const
void getPictures(const ElementsLocation &location, QPicture &picture, QPicture &low_picture)
ElementPictureFactory::getPictures Set the picture of the element at location. Note, picture can be null.
QList< QVector< qreal > > m_arcs
const QString version
QElectroTech version (as string, used to mark projects and elements XML documents) ...
void parseRect(const QDomElement &dom, QPainter &painter, primitives &prim) const
QUuid uuid() const
ElementsLocation::uuid.
static uint requiredLengthForEndType(const Qet::EndType &)
PartLine::requiredLengthForEndType.
ElementPictureFactory::primitives getPrimitives(const ElementsLocation &location)
ElementPictureFactory::getPrimitives.
QPixmap pixmap(const ElementsLocation &location)
ElementPictureFactory::pixmap.
static Qet::EndType endTypeFromString(const QString &)
QDomElement xml() const
ElementsLocation::xml.
QList< QRectF > m_rectangles
QHash< QUuid, QPixmap > m_pixmap_H
bool attributeIsAReal(const QDomElement &, const QString &, qreal *=nullptr)
bool exist() const
ElementsLocation::exist.
QList< QVector< QPointF > > m_polygons
QList< QGraphicsSimpleTextItem * > m_texts
static QList< QPointF > fourEndPoints(const QPointF &, const QPointF &, const qreal &)
PartLine::fourEndPoints Return the four interesting point needed to draw the shape at extremity of li...
static QFont diagramTextsFont(qreal=-1.0)
QETApp::diagramTextsFont The font to use By default the font is "sans Serif" and size 9...
void parseLine(const QDomElement &dom, QPainter &painter, primitives &prim) const
QList< QRectF > m_circles
bool build(const ElementsLocation &location, QPicture *picture=nullptr, QPicture *low_picture=nullptr)
ElementPictureFactory::build Build the picture from location.
void parsePolygon(const QDomElement &dom, QPainter &painter, primitives &prim) const
The ElementPictureFactory class This class is singleton factory, use to create and get the picture us...
QHash< QUuid, QPicture > m_low_pictures_H