[kgraphviewer-devel] [kgraphviewer/libkgraphviz] src/kgraphviz: Remove a lot of redundant code in QGI::paint()
Kevin Funk
krf at electrostorm.net
Thu Jan 27 15:10:44 CET 2011
Git commit 54b9810be809da56550c48ebc32b4f69f9d797e3 by Kevin Funk.
Pushed by kfunk into branch 'libkgraphviz'.
Remove a lot of redundant code in QGI::paint()
This causes a few regressions but likely improves performance a lot.
M +0 -304 src/kgraphviz/canvasedge.cpp
M +1 -4 src/kgraphviz/canvasedge.h
M +191 -123 src/kgraphviz/canvaselement.cpp
M +9 -1 src/kgraphviz/canvaselement_p.h
M +0 -34 src/kgraphviz/graphedge.cpp
M +0 -4 src/kgraphviz/graphedge.h
M +1 -1 src/kgraphviz/graphedge_p.h
M +2 -0 src/kgraphviz/graphelement.cpp
http://commits.kde.org/9c7b74a4/54b9810be809da56550c48ebc32b4f69f9d797e3
diff --git a/src/kgraphviz/canvasedge.cpp b/src/kgraphviz/canvasedge.cpp
index 3928299..b7bb11b 100644
--- a/src/kgraphviz/canvasedge.cpp
+++ b/src/kgraphviz/canvasedge.cpp
@@ -144,310 +144,6 @@ QPainterPath CanvasEdge::shape () const
*/
}
-
-
-void CanvasEdge::paint(QPainter* p, const QStyleOptionGraphicsItem* option,
- QWidget* widget)
-{
- Q_UNUSED(option)
- Q_UNUSED(widget)
-
- if (boundingRect() == QRectF())
- return;
-
- /// computes the scaling of line width
- qreal widthScaleFactor = (scaleX()+scaleY())/2;
- if (widthScaleFactor < 1)
- {
- widthScaleFactor = 1;
- }
-
- if (edge()->style()=="invis")
- {
- return;
- }
- if (edge()->renderOperations().isEmpty())
- {
- if ((edge()->fromNode()->canvasElement()!=0)
- && (edge()->toNode()->canvasElement()!=0))
- {
- p->drawLine(
- edge()->fromNode()->canvasElement()->boundingRect().center()+edge()->fromNode()->canvasElement()->pos(),
- edge()->toNode()->canvasElement()->boundingRect().center()+edge()->toNode()->canvasElement()->pos());
- }
- return;
- }
-
- QColor lineColor = Dot2QtConsts::componentData().qtColor(edge()->color(0));
- QColor backColor;
-
- QList<QPointF> allPoints;
- foreach (const DotRenderOp& dro, edge()->renderOperations())
- {
- // kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << "renderop" << dro.renderop << "; selected:" << edge()->isSelected();
- if (dro.renderop == "c")
- {
- QColor c(dro.str.mid(0,7));
- bool ok;
- c.setAlpha(255-dro.str.mid(8).toInt(&ok,16));
- lineColor = c;
-// kDebug() << "c" << dro.str.mid(0,7) << lineColor;
- }
- else if (dro.renderop == "C")
- {
- QColor c(dro.str.mid(0,7));
- bool ok;
- c.setAlpha(255-dro.str.mid(8).toInt(&ok,16));
-/* if (m_hovered && m_view->highlighting())
- {
- c = c.lighter();
- }*/
- backColor = c;
-// kDebug() << "C" << dro.str.mid(0,7) << backColor;
- }
- else if ( dro.renderop == "T" )
- {
- const QString& str = dro.str;
-
- qreal stringWidthGoal = dro.integers[3] * scaleX();
- int fontSize = edge()->fontSize();
- QFont font = CanvasElement::font();
- if (fontSize > 0)
- font.setPointSize(fontSize);
- QFontMetrics fm(font);
- while (fm.width(str) > stringWidthGoal && fontSize > 1)
- {
- fontSize--;
- font.setPointSize(fontSize);
- fm = QFontMetrics(font);
- }
- p->save();
- p->setFont(font);
-
- p->setPen(Dot2QtConsts::componentData().qtColor(edge()->fontColor()));
-
- qreal x = (scaleX() *
- (
- (dro.integers[0])
- + (((-dro.integers[2])*(fm.width(dro.str)))/2)
- - ( (fm.width(dro.str))/2 )
- )
- )
- + marginX();
- qreal y = ((gh() - (dro.integers[1]))*scaleY())+ marginY();
- QPointF point(x,y);
-// kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << "drawText" << edge()->fontColor() << point;
-
- p->drawText(point,str);
- p->restore();
-
- // save back
- setFont(font);
- }
- else if (( dro.renderop == "p" ) || (dro.renderop == "P" ))
- {
- QPolygonF polygon(dro.integers[0]);
- for (int i = 0; i < dro.integers[0]; i++)
- {
- QPointF point(
- (int(dro.integers[2*i+1])/*%m_wdhcf*/)*scaleX() +marginX(),
- (int(gh()-dro.integers[2*i+2])/*%m_hdvcf*/)*scaleY() + marginY()
- );
- polygon[i] = point;
-// kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << point;
- allPoints.append(point);
- }
- if (dro.renderop == "P" )
- {
- p->save();
- p->setBrush(lineColor);
- p->drawPolygon(polygon);
-// kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << "drawPolygon" << edge()->color(0) << polygon;
- p->restore();
- }
- else
- {
- p->setBrush(Dot2QtConsts::componentData().qtColor("white"));
- }
- QPen pen(lineColor);
- if (edge()->style() == "bold")
- {
- pen.setStyle(Qt::SolidLine);
- pen.setWidth((int)(2 * widthScaleFactor));
- }
- else
- {
- pen.setWidth((int)(1 * widthScaleFactor));
- pen.setStyle(Dot2QtConsts::componentData().qtPenStyle(edge()->style()));
- }
- p->save();
- p->setPen(pen);
-// kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << "drawPolyline" << edge()->color(0) << polygon;
- p->drawPolyline(polygon);
- p->restore();
- }
- else if (( dro.renderop == "e" ) || (dro.renderop == "E" ))
- {
- qreal w = scaleX() * dro.integers[2] * 2;
- qreal h = scaleY() * dro.integers[3] * 2;
- qreal x = (marginX() + (dro.integers[0]/*%m_wdhcf*/)*scaleX()) - w/2;
- qreal y = ((gh() - dro.integers[1]/*%m_hdvcf*/)*scaleY() + marginY()) - h/2;
- p->save();
- if (dro.renderop == "E" )
- {
- p->setBrush(lineColor);
- }
- else
- {
- p->setBrush(Dot2QtConsts::componentData().qtColor("white"));
- }
- QPen pen(lineColor);
- if (edge()->style() == "bold")
- {
- pen.setStyle(Qt::SolidLine);
- pen.setWidth(int(2 * widthScaleFactor));
- }
- else
- {
- pen.setWidth(int(1 * widthScaleFactor));
- pen.setStyle(Dot2QtConsts::componentData().qtPenStyle(edge()->style()));
- }
- p->setPen(pen);
- QRectF rect(x,y,w,h);
-// kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << "drawEllipse" << edge()->color(0) << rect;
- p->drawEllipse(rect);
- p->restore();
- }
- else if ( dro.renderop == "B" )
- {
- uint lineWidth = 1;
- QPen pen;
- if (edge()->style() == "bold")
- {
- pen.setStyle(Qt::SolidLine);
- pen.setWidth(int(2 * widthScaleFactor));
- }
- else if (edge()->style() != "filled")
- {
- pen.setStyle(Dot2QtConsts::componentData().qtPenStyle(edge()->style()));
- }
- if (edge()->style().left(12) == "setlinewidth")
- {
- bool ok;
- lineWidth = edge()->style().mid(12, edge()->style().length()-1-12).toInt(&ok);
- pen.setWidth(int(lineWidth * widthScaleFactor));
- }
- if (edge()->attributes().contains("penwidth"))
- {
- bool ok;
- lineWidth = edge()->attributes()["penwidth"].toInt(&ok);
- pen.setWidth(int(lineWidth * widthScaleFactor));
- }
- if (edge()->attributes().contains("color"))
- {
- kDebug() << "set edge color to " << QColor(edge()->attributes()["color"]).name();
- lineColor = QColor(edge()->attributes()["color"]);
- }
- for (int splineNum = 0; splineNum < edge()->colors().count() || (splineNum==0 && edge()->colors().count()==0); splineNum++)
- {
- QPolygonF points(dro.integers[0]);
- for (int i = 0; i < dro.integers[0]; i++)
- {
- // computing of diffX and diffY to draw parallel edges
- // when asked through the corresponding GraphViz feature
- qreal nom = (dro.integers[2*dro.integers[0]]-dro.integers[2]);
- qreal denom = (dro.integers[2*dro.integers[0]-1]-dro.integers[1]);
- qreal diffX, diffY;
- if (nom == 0)
- {
- diffX = 0;
- diffY = 2*(edge()->colors().count()/2 - splineNum);
- }
- else if (denom ==0)
- {
- diffX = 2*(edge()->colors().count()/2 - splineNum);
- diffY = 0;
- }
- else
- {
- double pente = nom/denom;
- if (pente < 0)
- {
- diffX = 2*(edge()->colors().count()/2 - splineNum);
- diffY = edge()->colors().count()/2 + splineNum;
- }
- else
- {
- diffX = 2*(edge()->colors().count()/2 - splineNum);
- diffY = 2*(edge()->colors().count()/2 - splineNum);
- }
- }
- QPointF p(
- (dro.integers[2*i+1]/*%m_wdhcf*/*scaleX()) +marginX() + diffX,
- (gh()-dro.integers[2*i+2]/*%m_hdvcf*/)*scaleY() + marginY() + diffY
- );
- points[i] = p;
-// kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << p;
- allPoints.append(p);
- }
-
-// kDebug() << "Setting pen color to " << edge()->color(splineNum);
- if (splineNum != 0)
- lineColor = Dot2QtConsts::componentData().qtColor(edge()->color(splineNum));
- pen.setColor(lineColor);
- p->save();
-// p->setBrush(Dot2QtConsts::componentData().qtColor(edge()->color(0)));
- p->setBrush(Qt::NoBrush);
- p->setPen(pen);
- QPainterPath path;
- path.moveTo(points[0]);
- for (int j = 0; j < (points.size()-1)/3; j++)
- {
- path.cubicTo(points[3*j + 1],points[3*j+1 + 1], points[3*j+2 + 1]);
- }
-// kDebug() << edge()->fromNode()->id() << "->" << edge()->toNode()->id() << "drawPath" << edge()->color(splineNum) << points.first() << points.last();
- p->drawPath(path);
- p->restore();
- }
- }
- }
- if (isSelected())
- {
-// kDebug() << "draw square";
-// p->drawRect(m_boundingRect);
- qreal maxDist = 0;
- QPair<QPointF,QPointF> pointsPair;
- foreach(const QPointF& point1, allPoints)
- {
- foreach(const QPointF& point2, allPoints)
- {
- if (QLineF(point1, point2).length() > maxDist)
- {
- maxDist = QLineF(point1, point2).length();
- pointsPair = qMakePair(point1, point2);
- }
- }
- }
- if (maxDist>0)
- {
- p->save();
- // p->setBrush(Dot2QtConsts::componentData().qtColor(edge()->color(0)));
- p->setBrush(Qt::black);
- p->setPen(Qt::black);
- p->drawRect(QRectF(pointsPair.first-QPointF(3,3),QSizeF(6,6)));
- p->drawRect(QRectF(pointsPair.second-QPointF(3,3),QSizeF(6,6)));
- p->restore();
- }
- }
-
-#ifdef KGRAPHVIZ_GRAPHICSVIEW_DEBUG
- p->save();
- p->setPen(Qt::green);
- p->drawRect(boundingRect());
- p->restore();
-#endif
-}
-
void CanvasEdge::computeBoundingRect()
{
Q_D(CanvasEdge);
diff --git a/src/kgraphviz/canvasedge.h b/src/kgraphviz/canvasedge.h
index 0fb1679..0c43cb2 100644
--- a/src/kgraphviz/canvasedge.h
+++ b/src/kgraphviz/canvasedge.h
@@ -55,10 +55,7 @@ public:
virtual void computeBoundingRect();
- QPainterPath shape () const;
-
- void paint(QPainter* p, const QStyleOptionGraphicsItem *option,
- QWidget *widget);
+ QPainterPath shape() const;
GraphEdge* edge() const;
diff --git a/src/kgraphviz/canvaselement.cpp b/src/kgraphviz/canvaselement.cpp
index dd34b68..5e0301c 100644
--- a/src/kgraphviz/canvaselement.cpp
+++ b/src/kgraphviz/canvaselement.cpp
@@ -42,7 +42,8 @@
using namespace KGraphViz;
-CanvasElementPrivate::CanvasElementPrivate() :
+CanvasElementPrivate::CanvasElementPrivate(CanvasElement* parent) :
+ q_ptr(parent),
m_scaleX(0), m_scaleY(0),
m_xMargin(0), m_yMargin(0),
m_gh(0),
@@ -54,12 +55,38 @@ CanvasElementPrivate::~CanvasElementPrivate()
{
}
+void CanvasElementPrivate::colors(const QString& cs)
+{
+ m_colors = QStringList::split(":", cs);
+// kDebug() << fromNode()->id() << " -> " << toNode()->id() << ": nb colors: " << d->m_colors.size();
+}
+
+QString CanvasElementPrivate::color(uint i)
+{
+ Q_Q(const CanvasElement);
+ if (i >= (uint)m_colors.count() && q->element()->attributes().contains("color"))
+ {
+ colors(q->element()->attributes()["color"]);
+ }
+ if (i < (uint)m_colors.count())
+ {
+// std::cerr << "edge color " << i << " is " << d->m_colors[i] << std::endl;
+// kDebug() << fromNode()->id() << " -> " << toNode()->id() << "color" << i << "is" << d->m_colors[i];
+ return m_colors[i];
+ }
+ else
+ {
+// kDebug() << fromNode()->id() << " -> " << toNode()->id() << "no edge color " << i << ". returning " << DOT_DEFAULT_EDGE_COLOR;
+ return DOT_DEFAULT_EDGE_COLOR;
+ }
+}
+
CanvasElement::CanvasElement(DotGraphView* v,
GraphElement* gelement,
QGraphicsScene* c,
QGraphicsItem* parent)
: QAbstractGraphicsShapeItem(parent)
- , d_ptr(new CanvasElementPrivate)
+ , d_ptr(new CanvasElementPrivate(this))
{
Q_D(CanvasElement);
d->m_element = gelement;
@@ -254,8 +281,6 @@ void CanvasElement::computeBoundingRect()
if ((*it).renderop == "e" || (*it).renderop == "E")
{
-// kDebug() << "integers[0]=" << (*it).integers[0] << "; d->m_wdhcf=" << d->m_wdhcf
-// << "(*it).integers[0]/*%d->m_wdhcf*/=" << (*it).integers[0]/*%d->m_wdhcf*/;
qreal w = d->m_scaleX * (*it).integers[2] * 2;
qreal h = d->m_scaleY * (*it).integers[3] * 2;
qreal x = d->m_xMargin + (((*it).integers[0]/*%d->m_wdhcf*/)*d->m_scaleX) - w/2;
@@ -302,19 +327,6 @@ void CanvasElement::paint(QPainter* p,
{
widthScaleFactor = 1;
}
-
- QString msg;
- QTextStream dd(&msg);
- foreach (const DotRenderOp &op, element()->renderOperations())
- {
- dd << element()->id() << " an op: " << op.renderop << " ";
- foreach (int i, op.integers)
- {
- dd << i << " ";
- }
- dd << op.str << endl;
- }
-// kDebug() << msg;
if (element()->renderOperations().isEmpty() && !d->m_view->isReadOnly())
{
@@ -333,13 +345,107 @@ void CanvasElement::paint(QPainter* p,
while (it.hasNext())
{
+ p->save();
const DotRenderOp& dro = it.next();
- if (dro.renderop == "c")
+ if ( dro.renderop == "B" )
+ {
+ uint lineWidth = 1;
+ QPen pen = p->pen();
+ if (element()->style() == "bold")
+ {
+ pen.setStyle(Qt::SolidLine);
+ pen.setWidth(int(2 * widthScaleFactor));
+ }
+ else if (element()->style() != "filled")
+ {
+ pen.setStyle(Dot2QtConsts::componentData().qtPenStyle(element()->style()));
+ }
+ if (element()->style().left(12) == "setlinewidth")
+ {
+ bool ok;
+ lineWidth = element()->style().mid(12, element()->style().length()-1-12).toInt(&ok);
+ pen.setWidth(int(lineWidth * widthScaleFactor));
+ }
+ if (element()->attributes().contains("penwidth"))
+ {
+ bool ok;
+ lineWidth = element()->attributes()["penwidth"].toInt(&ok);
+ pen.setWidth(int(lineWidth * widthScaleFactor));
+ }
+ if (element()->attributes().contains("color"))
+ {
+ kDebug() << "set edge color to " << QColor(element()->attributes()["color"]).name();
+ lineColor = QColor(element()->attributes()["color"]);
+ }
+
+ const int count = d->m_colors.count();
+ for (int splineNum = 0; splineNum < count || (splineNum==0 && count==0); splineNum++)
+ {
+ QPolygonF points(dro.integers[0]);
+ for (int i = 0; i < dro.integers[0]; i++)
+ {
+ // computing of diffX and diffY to draw parallel edges
+ // when asked through the corresponding GraphViz feature
+ qreal nom = (dro.integers[2*dro.integers[0]]-dro.integers[2]);
+ qreal denom = (dro.integers[2*dro.integers[0]-1]-dro.integers[1]);
+ qreal diffX, diffY;
+ if (nom == 0)
+ {
+ diffX = 0;
+ diffY = 2*(count/2 - splineNum);
+ }
+ else if (denom ==0)
+ {
+ diffX = 2*(count/2 - splineNum);
+ diffY = 0;
+ }
+ else
+ {
+ double pente = nom/denom;
+ if (pente < 0)
+ {
+ diffX = 2*(count/2 - splineNum);
+ diffY = count/2 + splineNum;
+ }
+ else
+ {
+ diffX = 2*(count/2 - splineNum);
+ diffY = 2*(count/2 - splineNum);
+ }
+ }
+ QPointF p(
+ (dro.integers[2*i+1]/*%m_wdhcf*/*scaleX()) +marginX() + diffX,
+ (gh()-dro.integers[2*i+2]/*%m_hdvcf*/)*scaleY() + marginY() + diffY
+ );
+ points[i] = p;
+// kDebug() << element()->fromNode()->id() << "->" << element()->toNode()->id() << p;
+ }
+
+// kDebug() << "Setting pen color to " << element()->color(splineNum);
+ if (splineNum != 0)
+ lineColor = Dot2QtConsts::componentData().qtColor(d->color(splineNum));
+ pen.setColor(lineColor);
+ p->save();
+// p->setBrush(Dot2QtConsts::componentData().qtColor(element()->color(0)));
+ p->setBrush(Qt::NoBrush);
+ p->setPen(pen);
+ QPainterPath path;
+ path.moveTo(points[0]);
+ for (int j = 0; j < (points.size()-1)/3; j++)
+ {
+ path.cubicTo(points[3*j + 1],points[3*j+1 + 1], points[3*j+2 + 1]);
+ }
+// kDebug() << element()->fromNode()->id() << "->" << element()->toNode()->id() << "drawPath" << element()->color(splineNum) << points.first() << points.last();
+ p->drawPath(path);
+ p->restore();
+ }
+ }
+ else if (dro.renderop == "c")
{
QColor c(dro.str.mid(0,7));
bool ok;
c.setAlpha(255-dro.str.mid(8).toInt(&ok,16));
- lineColor = c;
+ p->setPen(c);
// kDebug() << "c" << dro.str.mid(0,7) << lineColor;
}
else if (dro.renderop == "C")
@@ -351,34 +457,72 @@ void CanvasElement::paint(QPainter* p,
{
c = c.lighter();
}
- backColor = c;
-// kDebug() << "C" << dro.str.mid(0,7) << backColor;
+ p->setBrush(c);
+ kDebug() << "C" << dro.str.mid(0,7) << backColor;
}
else if (dro.renderop == "e" || dro.renderop == "E")
{
+ const qreal w = scaleX() * dro.integers[2] * 2;
+ const qreal h = scaleY() * dro.integers[3] * 2;
+ const qreal x = (marginX() + (dro.integers[0]/*%m_wdhcf*/)*scaleX()) - w/2;
+ const qreal y = ((gh() - dro.integers[1]/*%m_hdvcf*/)*scaleY() + marginY()) - h/2;
+ p->save();
+ if (dro.renderop == "E" )
+ {
+ p->setBrush(backColor);
+ }
+ else
+ {
+ p->setBrush(Dot2QtConsts::componentData().qtColor("white"));
+ }
QPen pen = p->pen();
- qreal w = d->m_scaleX * dro.integers[2] * 2;
- qreal h = d->m_scaleY * dro.integers[3] * 2;
- qreal x = d->m_xMargin + ((dro.integers[0]/*%d->m_wdhcf*/)*d->m_scaleX) - w/2;
- qreal y = ((d->m_gh - dro.integers[1]/*%d->m_hdvcf*/)*d->m_scaleY) + d->m_yMargin - h/2;
+ if (element()->style() == "bold")
+ {
+ pen.setStyle(Qt::SolidLine);
+ pen.setWidth(int(2 * widthScaleFactor));
+ }
+ else
+ {
+ pen.setWidth(int(1 * widthScaleFactor));
+ pen.setStyle(Dot2QtConsts::componentData().qtPenStyle(element()->style()));
+ }
+ p->setPen(pen);
QRectF rect(x,y,w,h);
+ p->drawEllipse(rect);
+ p->restore();
+ }
+ else if ( dro.renderop == "L" )
+ {
+// kDebug() << "Label";
+ QPolygonF points(dro.integers[0]);
+ for (int i = 0; i < dro.integers[0]; i++)
+ {
+ qreal x,y;
+ x = (dro.integers[2*i+1] == d->m_wdhcf)?dro.integers[2*i+1]:dro.integers[2*i+1]/*%d->m_wdhcf*/;
+ y = (dro.integers[2*i+2] == d->m_hdvcf)?dro.integers[2*i+2]:dro.integers[2*i+2]/*%d->m_hdvcf*/;
+ QPointF p(
+ (x*d->m_scaleX) +d->m_xMargin,
+ ((d->m_gh-y)*d->m_scaleY) + d->m_yMargin
+ );
+ points[i] = p;
+ }
p->save();
- p->setBrush(backColor);
- pen.setColor(lineColor);
- if (element()->attributes().contains("penwidth"))
+ QPen pen(lineColor);
+ if (element()->style() == "bold")
{
- bool ok;
- int lineWidth = element()->attributes()["penwidth"].toInt(&ok);
- pen.setWidth(int(lineWidth * widthScaleFactor));
+ pen.setStyle(Qt::SolidLine);
+ pen.setWidth(2);
+ }
+ else if (element()->style() != "filled")
+ {
+ pen.setStyle(Dot2QtConsts::componentData().qtPenStyle(element()->style()));
}
p->setPen(pen);
-
-// kDebug() << element()->id() << "drawEllipse" << lineColor << backColor << rect;
-// rect = QRectF(0,0,100,100);
- p->drawEllipse(rect);
+// kDebug() << element()->id() << "drawPolyline" << points;
+ p->drawPolyline(points);
p->restore();
}
- else if(dro.renderop == "p" || dro.renderop == "P")
+ else if (dro.renderop == "p" || dro.renderop == "P")
{
// std::cerr << "Drawing polygon for node '"<<element()->id()<<"': ";
QPolygonF points(dro.integers[0]);
@@ -443,77 +587,6 @@ void CanvasElement::paint(QPainter* p,
}
}
}
-
- }
-
- it.toFront();
- while (it.hasNext())
- {
- const DotRenderOp& dro = it.next();
- if (dro.renderop == "c")
- {
- QColor c(dro.str.mid(0,7));
- bool ok;
- c.setAlpha(255-dro.str.mid(8).toInt(&ok,16));
- lineColor = c;
-// kDebug() << "c" << dro.str.mid(0,7) << lineColor;
- }
- else if (dro.renderop == "C")
- {
- QColor c(dro.str.mid(0,7));
- bool ok;
- c.setAlpha(255-dro.str.mid(8).toInt(&ok,16));
- if (d->m_view->highlighting() && option->state & QStyle::State_MouseOver)
- {
- c = c.lighter();
- }
- backColor = c;
-// kDebug() << "C" << dro.str.mid(0,7) << backColor;
- }
- else if ( dro.renderop == "L" )
- {
-// kDebug() << "Label";
- QPolygonF points(dro.integers[0]);
- for (int i = 0; i < dro.integers[0]; i++)
- {
- qreal x,y;
- x = (dro.integers[2*i+1] == d->m_wdhcf)?dro.integers[2*i+1]:dro.integers[2*i+1]/*%d->m_wdhcf*/;
- y = (dro.integers[2*i+2] == d->m_hdvcf)?dro.integers[2*i+2]:dro.integers[2*i+2]/*%d->m_hdvcf*/;
- QPointF p(
- (x*d->m_scaleX) +d->m_xMargin,
- ((d->m_gh-y)*d->m_scaleY) + d->m_yMargin
- );
- points[i] = p;
- }
- p->save();
- QPen pen(lineColor);
- if (element()->style() == "bold")
- {
- pen.setStyle(Qt::SolidLine);
- pen.setWidth(2);
- }
- else if (element()->style() != "filled")
- {
- pen.setStyle(Dot2QtConsts::componentData().qtPenStyle(element()->style()));
- }
- p->setPen(pen);
-// kDebug() << element()->id() << "drawPolyline" << points;
- p->drawPolyline(points);
- p->restore();
- }
- }
-
-// kDebug() << "Drawing" << element()->id() << "labels";
- QString color = lineColor.name();
- it.toFront();
- while (it.hasNext())
- {
- const DotRenderOp& dro = it.next();
- if (dro.renderop == "c" || dro.renderop == "C")
- {
- color = dro.str.mid(0,7);
-// kDebug() << dro.renderop << color;
- }
else if (dro.renderop == "F")
{
element()->setFontName(dro.str);
@@ -522,28 +595,21 @@ void CanvasElement::paint(QPainter* p,
}
else if ( dro.renderop == "T" )
{
- // we suppose here that the color has been set just before
- element()->setFontColor(color);
- // draw a label
-// kDebug() << "Drawing a label " << dro.integers[0]
-// << " " << dro.integers[1] << " " << dro.integers[2]
-// << " " << dro.integers[3] << " " << dro.str
-// << " (" << element()->fontName() << ", " << element()->fontSize()
-// << ", " << element()->fontColor() << ")";
-
- int stringWidthGoal = int(dro.integers[3] * d->m_scaleX);
+ const qreal stringWidthGoal = dro.integers[3] * scaleX();
int fontSize = element()->fontSize();
-// kDebug() << element()->id() << " initial fontSize " << fontSize;
- d->m_font.setPointSize(fontSize);
- QFontMetrics fm(d->m_font);
+ QFont font = CanvasElement::font();
+ if (fontSize > 0)
+ font.setPointSize(fontSize);
+ QFontMetrics fm(font);
while (fm.width(dro.str) > stringWidthGoal && fontSize > 1)
{
fontSize--;
- d->m_font.setPointSize(fontSize);
- fm = QFontMetrics(d->m_font);
+ font.setPointSize(fontSize);
+ fm = QFontMetrics(font);
}
p->save();
- p->setFont(d->m_font);
+ p->setFont(font);
+
QPen pen(d->m_pen);
pen.setColor(element()->fontColor());
p->setPen(pen);
@@ -561,7 +627,9 @@ void CanvasElement::paint(QPainter* p,
p->drawText(point, dro.str);
p->restore();
}
+ p->restore();
}
+
if (isSelected())
{
// kDebug() << "element is selected: draw selection marks";
diff --git a/src/kgraphviz/canvaselement_p.h b/src/kgraphviz/canvaselement_p.h
index 049a220..1a03016 100644
--- a/src/kgraphviz/canvaselement_p.h
+++ b/src/kgraphviz/canvaselement_p.h
@@ -32,9 +32,17 @@ namespace KGraphViz
class CanvasElementPrivate
{
public:
- CanvasElementPrivate();
+ CanvasElementPrivate(CanvasElement* parent);
~CanvasElementPrivate();
+ Q_DECLARE_PUBLIC(CanvasElement);
+ CanvasElement* q_ptr;
+
+ QString color(uint i);
+ void colors(const QString& cs);
+
+ QStringList m_colors;
+
qreal m_scaleX, m_scaleY;
qreal m_xMargin, m_yMargin, m_gh, m_wdhcf, m_hdvcf;
GraphElement* m_element;
diff --git a/src/kgraphviz/graphedge.cpp b/src/kgraphviz/graphedge.cpp
index 126d44d..e4d88bf 100644
--- a/src/kgraphviz/graphedge.cpp
+++ b/src/kgraphviz/graphedge.cpp
@@ -96,19 +96,6 @@ const QList< DotRenderOp >& GraphEdge::arrowheads() const
return d->m_arrowHeads;
}
-const QStringList& GraphEdge::colors() const
-{
- Q_D(const GraphEdge);
- return d->m_colors;
-}
-
-void GraphEdge::colors(const QString& cs)
-{
- Q_D(GraphEdge);
- d->m_colors = QStringList::split(":", cs);
-// kDebug() << fromNode()->id() << " -> " << toNode()->id() << ": nb colors: " << d->m_colors.size();
-}
-
GraphNode* GraphEdge::fromNode() const
{
Q_D(const GraphEdge);
@@ -133,31 +120,10 @@ GraphNode* GraphEdge::toNode() const
return d->m_toNode;
}
-const QString GraphEdge::color(uint i)
-{
- Q_D(const GraphEdge);
- if (i >= (uint)d->m_colors.count() && m_attributes.find("color") != m_attributes.end())
- {
- colors(m_attributes["color"]);
- }
- if (i < (uint)d->m_colors.count())
- {
-// std::cerr << "edge color " << i << " is " << d->m_colors[i] << std::endl;
-// kDebug() << fromNode()->id() << " -> " << toNode()->id() << "color" << i << "is" << d->m_colors[i];
- return d->m_colors[i];
- }
- else
- {
-// kDebug() << fromNode()->id() << " -> " << toNode()->id() << "no edge color " << i << ". returning " << DOT_DEFAULT_EDGE_COLOR;
- return DOT_DEFAULT_EDGE_COLOR;
- }
-}
-
void GraphEdge::updateWithEdge(const GraphEdge& edge)
{
Q_D(GraphEdge);
d->m_arrowHeads = edge.arrowheads();
- d->m_colors = edge.colors();
d->m_fromNode = edge.fromNode();
d->m_toNode = edge.toNode();
diff --git a/src/kgraphviz/graphedge.h b/src/kgraphviz/graphedge.h
index 8ae8b7c..f14fe4e 100644
--- a/src/kgraphviz/graphedge.h
+++ b/src/kgraphviz/graphedge.h
@@ -61,10 +61,6 @@ public:
// inline QVector< QPair< float, float > >& edgePoints() {return m_edgePoints;}
// inline void edgePoints(const QVector< QPair< float, float > >& ep) {m_edgePoints = ep;}
- const QStringList& colors() const;
- const QString color(uint i);
- void colors(const QString& cs);
-
/* inline void labelX(float x) {m_labelX = x;}
inline void labelY(float y) {m_labelY = y;}
inline float labelX() const {return m_labelX;}
diff --git a/src/kgraphviz/graphedge_p.h b/src/kgraphviz/graphedge_p.h
index aa8703d..13d06fc 100644
--- a/src/kgraphviz/graphedge_p.h
+++ b/src/kgraphviz/graphedge_p.h
@@ -36,7 +36,7 @@ public:
~GraphEdgePrivate();
GraphNode *m_fromNode, *m_toNode;
- QStringList m_colors;
+
QList< DotRenderOp > m_arrowHeads;
};
diff --git a/src/kgraphviz/graphelement.cpp b/src/kgraphviz/graphelement.cpp
index 7235c5b..75fff56 100644
--- a/src/kgraphviz/graphelement.cpp
+++ b/src/kgraphviz/graphelement.cpp
@@ -29,6 +29,8 @@
#include <kdebug.h>
#include <QRegExp>
+#include <QStringList>
+
#include <graphviz/gvc.h>
using namespace KGraphViz;
More information about the kgraphviewer-devel
mailing list