[Uml-devel] KDE/kdesdk/umbrello/umbrello

Ralf Habacker ralf.habacker at gmail.com
Wed Sep 26 20:53:21 UTC 2012


SVN commit 1317960 by habacker:

Refactored LayoutGrid to be called from UMLScene::drawBackground. Added line base grid printing.

 M  +8 -1      umlscene.cpp  
 M  +2 -0      umlscene.h  
 M  +68 -26    widgets/layoutgrid.cpp  
 M  +8 -1      widgets/layoutgrid.h  


--- trunk/KDE/kdesdk/umbrello/umbrello/umlscene.cpp #1317959:1317960
@@ -152,7 +152,6 @@
 //    // settings for background
 //    setBackgroundBrush(QColor(195, 195, 195));
     m_layoutGrid = new LayoutGrid(0, this);
-    addItem(m_layoutGrid);
 
     DEBUG_REGISTER(DBG_SRC);
 }
@@ -3943,6 +3942,14 @@
 }
 
 /**
+ * Overrides standard method from QGraphicsScene drawing the background.
+ */
+void UMLScene::drawBackground(QPainter *painter, const QRectF &rect)
+{
+    m_layoutGrid->paint(painter, rect);
+}
+
+/**
  * Creates the "diagram" tag and fills it with the contents of the diagram.
  */
 void UMLScene::saveToXMI(QDomDocument & qDoc, QDomElement & qElement)
--- trunk/KDE/kdesdk/umbrello/umbrello/umlscene.h #1317959:1317960
@@ -527,6 +527,8 @@
                                   UMLSceneValue& px, UMLSceneValue& py, UMLSceneValue& qx, UMLSceneValue& qy);
     void forceUpdateWidgetFontMetrics(QPainter *painter);
 
+    virtual void drawBackground(QPainter *painter, const QRectF &rect);
+
     int m_nCollaborationId;  ///< Used for creating unique name of collaboration messages.
     UMLScenePoint m_Pos;
     bool m_bCreateObject;
--- trunk/KDE/kdesdk/umbrello/umbrello/widgets/layoutgrid.cpp #1317959:1317960
@@ -27,6 +27,7 @@
 #include <QGraphicsSceneMouseEvent>
 #include <QPainter>
 #include <QTextStream>
+#include <QVarLengthArray>
 
 /**
  * Constructor.
@@ -60,14 +61,51 @@
     return QRectF(m_gridRect);
 }
 
-#if 0
-void LayoutGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
+void LayoutGrid::paint(QPainter *painter, const QRectF &rect)
 {
-    Q_UNUSED(item); Q_UNUSED(widget);
+    // speed top 1
+    paintLineGrid(painter, rect);
+    // speed top 2
+    //paintPointsLevelOfDetail(painter, rect);
+    // speed top 3
+    //paintPoints(painter, rect);
+    // speed top 4
+    //paintEllipses(painter, rect);
+}
+
+void LayoutGrid::paintLineGrid(QPainter *painter, const QRectF &rect)
+{
+    if (!isVisible())
+        return;
+
+    QGraphicsView *view = scene()->views()[0];
+    QRectF visibleSceneRect = view->mapToScene(view->viewport()->geometry()).boundingRect();
+
+    int gridSizeX = gridSpacingX();
+    int gridSizeY = gridSpacingY();
+
+    qreal left = int(rect.left()) - (int(rect.left()) % gridSizeX);
+    qreal top = int(rect.top()) - (int(rect.top()) % gridSizeY);
+
+    QVarLengthArray<QLineF, 200> lines;
+
+    for (qreal x = left; x < rect.right(); x += gridSizeX)
+        lines.append(QLineF(x, rect.top(), x, rect.bottom()));
+    for (qreal y = top; y < rect.bottom(); y += gridSizeY)
+        lines.append(QLineF(rect.left(), y, rect.right(), y));
+
+    qDebug() << lines.size();
+
+    painter->setPen(m_gridDotColor);
+    painter->drawLines(lines.data(), lines.size());
+}
+
+void LayoutGrid::paintEllipses(QPainter *painter, const QRectF &rect)
+{
     DEBUG("LayoutGrid") << "painting...";
     if (m_isVisible) {
-        for(int x = m_gridRect.left(); x < m_gridRect.right(); x += m_gridSpacingX) {
-            for(int y = m_gridRect.top(); y < m_gridRect.bottom(); y += m_gridSpacingY) {
+        for(int x = rect.left(); x < rect.right(); x += m_gridSpacingX) {
+            for(int y = rect.top(); y < rect.bottom(); y += m_gridSpacingY) {
                 if (x % 100 == 0 && y % 100 == 0) {
                     // cross
                     painter->setPen(m_gridCrossColor);
@@ -92,7 +130,7 @@
         }
     }
 }
-#else
+
 /**
  * 1. try: speed up painting by
  *    - moving pen/font setting out of loop
@@ -101,20 +139,19 @@
  *
  * Remaining problem: On low zoom levels drawing is still very slow
  */
-void LayoutGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
+void LayoutGrid::paintPoints(QPainter *painter, const QRectF &rect)
 {
-    Q_UNUSED(item); Q_UNUSED(widget);
     DEBUG("LayoutGrid") << "painting...";
     if (m_isVisible) {
         painter->setPen(Qt::black);
         painter->setFont(m_textFont);
         QGraphicsView *view = scene()->views()[0];
-        QRectF rect = view->mapToScene(view->viewport()->geometry()).boundingRect();
+        QRectF visibleSceneRect = view->mapToScene(view->viewport()->geometry()).boundingRect();
 
-        for(int x = m_gridRect.left(); x < m_gridRect.right(); x += m_gridSpacingX) {
-            for(int y = m_gridRect.top(); y < m_gridRect.bottom(); y += m_gridSpacingY) {
+        for(int x = rect.left(); x < rect.right(); x += m_gridSpacingX) {
+            for(int y = rect.top(); y < rect.bottom(); y += m_gridSpacingY) {
                 // limit to visible area
-                if (x <= rect.left() || y < rect.top() || x >= rect.right() || y >= rect.bottom())
+                if (x <= visibleSceneRect.left() || y < visibleSceneRect.top() || x >= visibleSceneRect.right() || y >= visibleSceneRect.bottom())
                     continue;
                 if (x % 100 == 0 && y % 100 == 0) {
                     // cross
@@ -131,61 +168,66 @@
         }
     }
 }
-#endif
-#if 0
+
 /**
  * 2. try: speed painting on low zoom levels by
  *    - reducing the numbers of dots for lower zoom levels
  *
  * Remaining problem: grid is not aligned to '+' dots for some zoom levels
  */
-void LayoutGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
+void LayoutGrid::paintPointsLevelOfDetail(QPainter *painter, const QRectF &rect)
 {
-    Q_UNUSED(item); Q_UNUSED(widget);
     DEBUG("LayoutGrid") << "painting...";
     if (m_isVisible) {
         painter->setPen(Qt::black);
         painter->setFont(m_textFont);
         QGraphicsView *view = scene()->views()[0];
-        QRectF rect = view->mapToScene(view->viewport()->geometry()).boundingRect();
+        QRectF visibleSceneRect = view->mapToScene(view->viewport()->geometry()).boundingRect();
         DEBUG("LayoutGrid") << view->viewport()->geometry() << rect << rect.width()/m_gridSpacingX << rect.height()/m_gridSpacingY << rect.width()/m_gridSpacingX * rect.height()/m_gridSpacingY;
         bool showDots1 = rect.width() <= 1000;
         bool showDots2 = rect.width() <= 2000;
         bool showDots4 = rect.width() <= 4000;
         bool showDots5 = rect.width() <= 5000;
-
+        QVarLengthArray<QLineF, 60000> lines;
+        QVarLengthArray<QPointF, 60000> points;
         for(int x = m_gridRect.left(); x < m_gridRect.right(); x += m_gridSpacingX) {
             for(int y = m_gridRect.top(); y < m_gridRect.bottom(); y += m_gridSpacingY) {
                 // limit to visible area
-                if (x <= rect.left() || y < rect.top() || x >= rect.right() || y >= rect.bottom())
+                if (x <= visibleSceneRect.left() || y < visibleSceneRect.top() || x >= visibleSceneRect.right() || y >= visibleSceneRect.bottom())
                     continue;
                 if (x % 100 == 0 && y % 100 == 0) {
                     // cross
-                    painter->drawLine(x, y-2, x, y+2);
-                    painter->drawLine(x-2, y, x+2, y);
+                    lines.append(QLineF(x, y-2, x, y+2));
+                    lines.append(QLineF(x-2, y, x+2, y));
                     // text
                     if (m_isTextVisible) {
                         painter->drawText(x,y, QString("%1,%2").arg(x).arg(y));
                     }
                 } else if (showDots1){
                     //painter->drawEllipse(x, y, 1, 1);
-                    painter->drawPoint(x, y);
+                    points.append(QPointF(x, y));
                 } else if (showDots2) {
                     if (x % (m_gridSpacingX*2) == 0 && y % (m_gridSpacingY*2) == 0)
-                        painter->drawPoint(x, y);
+                        points.append(QPointF(x, y));
                 } else if (showDots4) {
                     if (x % (m_gridSpacingX*4) == 0 && y % (m_gridSpacingY*4) == 0)
-                        painter->drawPoint(x, y);
+                        points.append(QPointF(x, y));
                 } else if (showDots5) {
                     if (x % (m_gridSpacingX*8) == 0 && y % (m_gridSpacingY*8) == 0)
-                        painter->drawPoint(x, y);
+                        points.append(QPointF(x, y));
                 }
             }
         }
+        painter->drawLines(lines.data(), lines.size());
+        painter->drawPoints(points.data(), points.size());
     }
 }
-#endif
 
+void LayoutGrid::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
+{
+    // doc says drawing grid in scene background should be faster
+}
+
 QRect LayoutGrid::gridRect() const
 {
     return m_gridRect;
--- trunk/KDE/kdesdk/umbrello/umbrello/widgets/layoutgrid.h #1317959:1317960
@@ -40,8 +40,15 @@
     ~LayoutGrid();
 
     QRectF boundingRect() const;
-    void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
 
+    void paint(QPainter *painter, const QRectF &rect);
+    void paintLineGrid(QPainter *painter, const QRectF &rect);
+    void paintEllipses(QPainter *painter, const QRectF &rect);
+    void paintPoints(QPainter *painter, const QRectF &rect);
+    void paintPointsLevelOfDetail(QPainter *painter, const QRectF &rect);
+
+    virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget);
+
     QRect gridRect() const;
     void setGridRect(const QRect& rect);
 




More information about the umbrello-devel mailing list