| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/*************************************************************** |
| 2 |
|
|
QGVCore |
| 3 |
|
|
Copyright (c) 2014, Bergont Nicolas, All rights reserved. |
| 4 |
|
|
|
| 5 |
|
|
This library is free software; you can redistribute it and/or |
| 6 |
|
|
modify it under the terms of the GNU Lesser General Public |
| 7 |
|
|
License as published by the Free Software Foundation; either |
| 8 |
|
|
version 3.0 of the License, or (at your option) any later version. |
| 9 |
|
|
|
| 10 |
|
|
This library is distributed in the hope that it will be useful, |
| 11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
|
|
Lesser General Public License for more details. |
| 14 |
|
|
|
| 15 |
|
|
You should have received a copy of the GNU Lesser General Public |
| 16 |
|
|
License along with this library. |
| 17 |
|
|
***************************************************************/ |
| 18 |
|
|
#include "QGVCore.h" |
| 19 |
|
|
|
| 20 |
|
|
#include <QDebug> |
| 21 |
|
|
|
| 22 |
|
✗ |
qreal QGVCore::graphHeight(Agraph_t *graph) { |
| 23 |
|
|
// Hauteur totale du graphique (permet d'effectuer le calcul inverse des |
| 24 |
|
|
// coordonn�es) |
| 25 |
|
✗ |
return GD_bb(graph).UR.y; |
| 26 |
|
|
} |
| 27 |
|
|
|
| 28 |
|
✗ |
bool QGVCore::gvToQtPos(QString att, qreal dpi, qreal gheight, QPointF &pos) { |
| 29 |
|
✗ |
QStringList split = att.split(","); |
| 30 |
|
✗ |
if (split.length() != 2) return false; |
| 31 |
|
✗ |
bool ok = true; |
| 32 |
|
✗ |
float x = split[0].toFloat(&ok); |
| 33 |
|
✗ |
if (!ok) return false; |
| 34 |
|
✗ |
float y = split[1].toFloat(&ok); |
| 35 |
|
✗ |
if (!ok) return false; |
| 36 |
|
|
// Le repere Y commence du bas dans graphViz et du haut pour Qt ! |
| 37 |
|
✗ |
pos.setX(x); |
| 38 |
|
✗ |
pos.setY((gheight - y)); |
| 39 |
|
✗ |
return true; |
| 40 |
|
|
} |
| 41 |
|
|
|
| 42 |
|
✗ |
QString QGVCore::qtToGvPos(QPointF pos, qreal gheight) { |
| 43 |
|
✗ |
float x = pos.x(); |
| 44 |
|
✗ |
float y = pos.y(); |
| 45 |
|
|
// Le repere Y commence du bas dans graphViz et du haut pour Qt ! |
| 46 |
|
✗ |
return QString("%1,%2").arg(x).arg(gheight - y); |
| 47 |
|
|
} |
| 48 |
|
|
|
| 49 |
|
✗ |
QPointF QGVCore::toPoint(pointf p, qreal gheight) { |
| 50 |
|
|
// Le repere Y commence du bas dans graphViz et du haut pour Qt ! |
| 51 |
|
✗ |
return QPointF(p.x, gheight - p.y); |
| 52 |
|
|
} |
| 53 |
|
|
|
| 54 |
|
✗ |
QPointF QGVCore::toPoint(point p, qreal gheight) { |
| 55 |
|
|
// Le repere Y commence du bas dans graphViz et du haut pour Qt ! |
| 56 |
|
✗ |
return QPointF(p.x, gheight - p.y); |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
✗ |
QPointF QGVCore::centerToOrigin(const QPointF &p, qreal width, qreal height) { |
| 60 |
|
|
// L'origine d'un objet est le centre dans graphViz et du haut gauche pour Qt |
| 61 |
|
|
// ! |
| 62 |
|
✗ |
return QPointF(p.x() - width / 2, p.y() - height / 2); |
| 63 |
|
|
} |
| 64 |
|
|
|
| 65 |
|
✗ |
QPolygonF QGVCore::toPolygon(const polygon_t *poly, qreal width, qreal height) { |
| 66 |
|
✗ |
if (poly->peripheries != 1) |
| 67 |
|
✗ |
qWarning("unsupported number of peripheries %d", poly->peripheries); |
| 68 |
|
|
|
| 69 |
|
✗ |
const int sides = poly->sides; |
| 70 |
|
✗ |
const pointf *vertices = poly->vertices; |
| 71 |
|
|
|
| 72 |
|
✗ |
QPolygonF polygon; |
| 73 |
|
✗ |
for (int side = 0; side < sides; side++) |
| 74 |
|
✗ |
polygon.append( |
| 75 |
|
✗ |
QPointF(vertices[side].x + width / 2, vertices[side].y + height / 2)); |
| 76 |
|
✗ |
return polygon; |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
✗ |
QPainterPath QGVCore::toPath(const char *type, const polygon_t *poly, |
| 80 |
|
|
qreal width, qreal height) { |
| 81 |
|
✗ |
QPainterPath path; |
| 82 |
|
✗ |
if ((strcmp(type, "rectangle") == 0) || (strcmp(type, "box") == 0) || |
| 83 |
|
✗ |
(strcmp(type, "hexagon") == 0) || (strcmp(type, "polygon") == 0) || |
| 84 |
|
✗ |
(strcmp(type, "diamond") == 0)) { |
| 85 |
|
✗ |
QPolygonF polygon = toPolygon(poly, width, height); |
| 86 |
|
✗ |
polygon.append(polygon[0]); |
| 87 |
|
✗ |
path.addPolygon(polygon); |
| 88 |
|
✗ |
} else if ((strcmp(type, "ellipse") == 0) || (strcmp(type, "circle") == 0)) { |
| 89 |
|
✗ |
QPolygonF polygon = toPolygon(poly, width, height); |
| 90 |
|
✗ |
path.addEllipse(QRectF(polygon[0], polygon[1])); |
| 91 |
|
✗ |
} else { |
| 92 |
|
✗ |
qWarning("unsupported shape %s", type); |
| 93 |
|
|
} |
| 94 |
|
✗ |
return path; |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
|
✗ |
QPainterPath QGVCore::toPath(const splines *spl, qreal gheight) { |
| 98 |
|
✗ |
QPainterPath path; |
| 99 |
|
✗ |
if ((spl->list != 0) && (spl->list->size % 3 == 1)) { |
| 100 |
|
✗ |
bezier bez = spl->list[0]; |
| 101 |
|
|
// If there is a starting point, draw a line from it to the first curve |
| 102 |
|
|
// point |
| 103 |
|
✗ |
if (bez.sflag) { |
| 104 |
|
✗ |
path.moveTo(toPoint(bez.sp, gheight)); |
| 105 |
|
✗ |
path.lineTo(toPoint(bez.list[0], gheight)); |
| 106 |
|
|
} else |
| 107 |
|
✗ |
path.moveTo(toPoint(bez.list[0], gheight)); |
| 108 |
|
|
|
| 109 |
|
|
// Loop over the curve points |
| 110 |
|
✗ |
for (int i = 1; i < bez.size; i += 3) |
| 111 |
|
✗ |
path.cubicTo(toPoint(bez.list[i], gheight), |
| 112 |
|
✗ |
toPoint(bez.list[i + 1], gheight), |
| 113 |
|
✗ |
toPoint(bez.list[i + 2], gheight)); |
| 114 |
|
|
|
| 115 |
|
|
// If there is an ending point, draw a line to it |
| 116 |
|
✗ |
if (bez.eflag) path.lineTo(toPoint(bez.ep, gheight)); |
| 117 |
|
|
} |
| 118 |
|
✗ |
return path; |
| 119 |
|
|
} |
| 120 |
|
|
|
| 121 |
|
✗ |
Qt::BrushStyle QGVCore::toBrushStyle(const QString &style) { |
| 122 |
|
✗ |
if (style == "filled") |
| 123 |
|
✗ |
return Qt::SolidPattern; |
| 124 |
|
✗ |
else if (style == "dashed") |
| 125 |
|
✗ |
return Qt::Dense5Pattern; |
| 126 |
|
✗ |
return Qt::NoBrush; |
| 127 |
|
|
} |
| 128 |
|
|
|
| 129 |
|
✗ |
Qt::PenStyle QGVCore::toPenStyle(const QString &style) { |
| 130 |
|
✗ |
if (style == "dashed") |
| 131 |
|
✗ |
return Qt::DashLine; |
| 132 |
|
✗ |
else if (style == "dotted") |
| 133 |
|
✗ |
return Qt::DotLine; |
| 134 |
|
✗ |
else if (style == "invisible") |
| 135 |
|
✗ |
return Qt::NoPen; |
| 136 |
|
✗ |
return Qt::SolidLine; |
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
|
✗ |
int QGVCore::toPenWidth(const QString &width) { |
| 140 |
|
|
bool ok; |
| 141 |
|
✗ |
int w = width.toInt(&ok); |
| 142 |
|
✗ |
if (!ok) |
| 143 |
|
✗ |
return 1; |
| 144 |
|
|
else |
| 145 |
|
✗ |
return w; |
| 146 |
|
|
} |
| 147 |
|
|
|
| 148 |
|
✗ |
QColor QGVCore::toColor(const QString &color) { return QColor(color); } |
| 149 |
|
|
|