[QGIS Commit] r8772 - branches/advanced_printing_branch/src/app/composer

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Jul 14 15:51:16 EDT 2008


Author: mhugent
Date: 2008-07-14 15:51:14 -0400 (Mon, 14 Jul 2008)
New Revision: 8772

Modified:
   branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp
   branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.h
   branches/advanced_printing_branch/src/app/composer/qgscomposerlegendwidget.cpp
   branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp
Log:
Calculate size of composer legend

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp	2008-07-14 13:55:18 UTC (rev 8771)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp	2008-07-14 19:51:14 UTC (rev 8772)
@@ -55,14 +55,25 @@
 
 void QgsComposerLegend::paint(QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget)
 {
+  paintAndDetermineSize(painter);
+}
+
+QSizeF QgsComposerLegend::paintAndDetermineSize(QPainter* painter)
+{
+  QSizeF size;
+  double maxXCoord = 0;
+
   //go through model...
   QStandardItem* rootItem = mLegendModel.invisibleRootItem();
-  if(!rootItem || !painter)
+  if(!rootItem)
     {
-      return;
+      return size;
     }
 
-  painter->save();
+  if(painter)
+    {
+      painter->save();
+    }
 
   int numLayerItems = rootItem->rowCount();
   QStandardItem* currentLayerItem = 0;
@@ -74,9 +85,14 @@
 
   //draw title
   currentYCoordinate += titleFontMetrics.height();
-  painter->setFont(mTitleFont);
-  painter->drawText(QPointF(mBoxSpace, currentYCoordinate), mTitle);
+  if(painter)
+    {
+      painter->setFont(mTitleFont);
+      painter->drawText(QPointF(mBoxSpace, currentYCoordinate), mTitle);
+    }
 
+  maxXCoord = 2 * mBoxSpace + titleFontMetrics.width(mTitle);
+
   //draw layer items
   for(int i = 0; i < numLayerItems; ++i)
     {
@@ -87,33 +103,50 @@
 	  currentYCoordinate += layerFontMetrics.height();
 
 	  //draw layer Item
-	  painter->setFont(mLayerFont);
-	  painter->drawText(QPointF(mBoxSpace, currentYCoordinate), currentLayerItem->text());
+	  if(painter)
+	    {
+	      painter->setFont(mLayerFont);
+	      painter->drawText(QPointF(mBoxSpace, currentYCoordinate), currentLayerItem->text());
+	    }
+
+	  maxXCoord = std::max(maxXCoord, 2 * mBoxSpace + layerFontMetrics.width(currentLayerItem->text()));
 	  
 	  //and child items
-	  drawLayerChildItems(painter, currentLayerItem, currentYCoordinate);
+	  drawLayerChildItems(painter, currentLayerItem, currentYCoordinate, maxXCoord);
 	}
     }
 
-  painter->restore();
+  currentYCoordinate += mBoxSpace;
 
-  //draw frame and selection boxes if necessary
-  drawFrame(painter);
-  if(isSelected())
+  if(painter)
     {
-      drawSelectionBoxes(painter);
+      painter->restore();
+
+      //draw frame and selection boxes if necessary
+      drawFrame(painter);
+      if(isSelected())
+	{
+	  drawSelectionBoxes(painter);
+	}
     }
+
+  size.setHeight(currentYCoordinate);
+  size.setWidth(maxXCoord);
+  return size;
 }
 
 void QgsComposerLegend::adjustBoxSize()
 {
-  //todo...
-  setSceneRect(QRectF(transform().dx(), transform().dy(), 10, 10));
+  QSizeF size = paintAndDetermineSize(0);
+  if(size.isValid())
+    {
+      setSceneRect(QRectF(transform().dx(), transform().dy(), size.width(), size.height()));
+    }
 }
 
-void QgsComposerLegend::drawLayerChildItems(QPainter* p, QStandardItem* layerItem, double& currentYCoord)
+void QgsComposerLegend::drawLayerChildItems(QPainter* p, QStandardItem* layerItem, double& currentYCoord, double& maxXCoord)
 {
-  if(!layerItem || !p)
+  if(!layerItem)
     {
       return;
     }
@@ -125,8 +158,12 @@
   QStandardItem* secondItem;
 
   int numChildren = layerItem->rowCount();
-  p->setFont(mItemFont);
 
+  if(p)
+    {
+      p->setFont(mItemFont);
+    }
+
   for(int i = 0; i < numChildren; ++i)
     {
       currentYCoord += mSymbolSpace;
@@ -155,17 +192,21 @@
 	  //draw symbol considering output device resolution
 	  drawSymbol(p, symbol, currentYCoord + (itemHeight - mSymbolHeight) /2, currentXCoord);
 	  currentXCoord += mIconLabelSpace;
+	  
+	  if(p)
+	    {
+	      p->drawText(QPointF(currentXCoord, currentYCoord + itemFontMetrics.height()), secondItem->text());
+	    }
 
-	  /*
-	  symbolIcon.paint(p, QRect(currentXCoord, currentYCoord + (itemHeight - mSymbolHeight) /2, mSymbolWidth, mSymbolHeight));
-	  currentXCoord += (mSymbolWidth + mIconLabelSpace);
-	  */
-	  
-	  p->drawText(QPointF(currentXCoord, currentYCoord + itemFontMetrics.height()), secondItem->text());
+	  maxXCoord = std::max(maxXCoord, currentXCoord + itemFontMetrics.width(secondItem->text()) + mBoxSpace);
 	}
       else //an item witout icon (e.g. name of classification field)
 	{
-	  p->drawText(QPointF(currentXCoord, currentYCoord + itemFontMetrics.height()), firstItem->text());
+	  if(p)
+	    {
+	      p->drawText(QPointF(currentXCoord, currentYCoord + itemFontMetrics.height()), firstItem->text());
+	    }
+	  maxXCoord = std::max(maxXCoord, currentXCoord + itemFontMetrics.width(firstItem->text()) + mBoxSpace);
 	}
  
       currentYCoord += itemHeight;
@@ -174,7 +215,7 @@
 
 void QgsComposerLegend::drawSymbol(QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition) const
 {
-  if(!p || !s)
+  if(!s)
     {
       return;
     }
@@ -196,57 +237,74 @@
 
 void QgsComposerLegend::drawPointSymbol(QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition) const
 {
-  if(!p || !s)
+  if(!s)
     {
       return;
     }
 
-  QPaintDevice* paintDevice = p->device();
-  if(!paintDevice)
+  QImage pointImage;
+  double rasterScaleFactor = 1.0;
+  if(p)
     {
-      return;
+      QPaintDevice* paintDevice = p->device();
+      if(!paintDevice)
+	{
+	  return;
+	}
+      
+      rasterScaleFactor = (paintDevice->logicalDpiX() + paintDevice->logicalDpiY()) / 2.0 / 25.4;
+      double widthScale = (paintDevice->logicalDpiX() + paintDevice->logicalDpiY()) / 2 / 25.4;
     }
-
-  double rasterScaleFactor = (paintDevice->logicalDpiX() + paintDevice->logicalDpiY()) / 2.0 / 25.4;
-  double widthScale = (paintDevice->logicalDpiX() + paintDevice->logicalDpiY()) / 2 / 25.4;
-
+  
   //width scale is 1.0
-  QImage pointImage = s->getPointSymbolAsImage(1.0, false, Qt::yellow, 1.0, 0.0, rasterScaleFactor);
+  pointImage = s->getPointSymbolAsImage(1.0, false, Qt::yellow, 1.0, 0.0, rasterScaleFactor);
 
-  p->save();
-  p->scale(1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor);
-
-  QPointF imageTopLeft(currentXPosition * rasterScaleFactor, currentYCoord * rasterScaleFactor);
-  p->drawImage(imageTopLeft, pointImage);
-  p->restore();
-
+  if(p)
+    {
+      p->save();
+      p->scale(1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor);
+      
+      QPointF imageTopLeft(currentXPosition * rasterScaleFactor, currentYCoord * rasterScaleFactor);
+      p->drawImage(imageTopLeft, pointImage);
+      p->restore();
+    }
+  
   currentXPosition += pointImage.width();
 }
 
 void QgsComposerLegend::drawLineSymbol(QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition) const
 {
-  if(!p || !s)
+  if(!s)
     {
       return;
     }
 
   double yCoord = currentYCoord + mSymbolHeight/2;
 
-  p->setPen(s->pen());
-  p->drawLine(QPointF(currentXPosition, yCoord), QPointF(currentXPosition + mSymbolWidth, yCoord));
+  if(p)
+    {
+      p->save();
+      p->setPen(s->pen());
+      p->drawLine(QPointF(currentXPosition, yCoord), QPointF(currentXPosition + mSymbolWidth, yCoord));
+      p->restore();
+    }
+
   currentXPosition += mSymbolWidth;
 }
 
 void QgsComposerLegend::drawPolygonSymbol(QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition) const
 {
-  if(!p || !s)
+  if(!s)
     {
       return;
     }
 
-  p->setBrush(s->brush());
-  p->setPen(s->pen());
-  p->drawRect(QRectF(currentXPosition, currentYCoord, mSymbolWidth, mSymbolHeight));
+  if(p)
+    {
+      p->setBrush(s->brush());
+      p->setPen(s->pen());
+      p->drawRect(QRectF(currentXPosition, currentYCoord, mSymbolWidth, mSymbolHeight));
+    }
 
   currentXPosition += mSymbolWidth;
 }

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.h	2008-07-14 13:55:18 UTC (rev 8771)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.h	2008-07-14 19:51:14 UTC (rev 8772)
@@ -32,6 +32,9 @@
   /** \brief Reimplementation of QCanvasItem::paint*/
   void paint (QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget);
 
+  /**Paints the legend and calculates its size. If painter is 0, only size is calculated*/
+  QSizeF paintAndDetermineSize(QPainter* painter);
+
   /**Sets item box to the whole content*/
   void adjustBoxSize();
 
@@ -98,8 +101,10 @@
 
   /**Draws child items of a layer item
      @param layerItem parent model item (layer)
-     @param currentYCoord in/out: current y position of legend item*/
-  void drawLayerChildItems(QPainter* p, QStandardItem* layerItem, double& currentYCoord);
+     @param currentYCoord in/out: current y position of legend item
+     @param maxXCoord in/out: maximum x-coordinate of the whole legend
+  */
+  void drawLayerChildItems(QPainter* p, QStandardItem* layerItem, double& currentYCoord, double& maxXCoord);
 
   /**Draws a symbol at the current y position and returns the new x position*/
   void drawSymbol(QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition) const;

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposerlegendwidget.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposerlegendwidget.cpp	2008-07-14 13:55:18 UTC (rev 8771)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposerlegendwidget.cpp	2008-07-14 19:51:14 UTC (rev 8772)
@@ -74,6 +74,7 @@
   if(mLegend)
     {
       mLegend->setTitle(text);
+      mLegend->adjustBoxSize();
       mLegend->update();
     }
 }
@@ -83,6 +84,7 @@
   if(mLegend)
     {
       mLegend->setSymbolWidth(d);
+      mLegend->adjustBoxSize();
       mLegend->update();
     }
 }
@@ -92,6 +94,7 @@
   if(mLegend)
     {
       mLegend->setSymbolHeight(d);
+      mLegend->adjustBoxSize();
       mLegend->update();
     }
 }
@@ -101,6 +104,7 @@
   if(mLegend)
     {
       mLegend->setLayerSpace(d);
+      mLegend->adjustBoxSize();
       mLegend->update();
     }
 }
@@ -110,6 +114,7 @@
   if(mLegend)
     {
       mLegend->setSymbolSpace(d);
+      mLegend->adjustBoxSize();
       mLegend->update();
     }
 }
@@ -119,6 +124,7 @@
   if(mLegend)
     {
       mLegend->setIconLabelSpace(d);
+      mLegend->adjustBoxSize();
       mLegend->update();
     }
 }
@@ -132,6 +138,7 @@
       if(ok)
 	{
 	  mLegend->setTitleFont(newFont);
+	  mLegend->adjustBoxSize();
 	  mLegend->update();
 	}
     }
@@ -146,6 +153,7 @@
       if(ok)
 	{
 	  mLegend->setLayerFont(newFont);
+	  mLegend->adjustBoxSize();
 	  mLegend->update();
 	}
     }
@@ -160,6 +168,7 @@
       if(ok)
 	{
 	  mLegend->setItemFont(newFont);
+	  mLegend->adjustBoxSize();
 	  mLegend->update();
 	}
     }

Modified: branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp	2008-07-14 13:55:18 UTC (rev 8771)
+++ branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp	2008-07-14 19:51:14 UTC (rev 8772)
@@ -121,9 +121,8 @@
 	  break;
 	}
 
-      //Copy QgsSymbol as user data. Cast to void* necessary such that QMetaType handles it
-      QgsSymbol* legendSymbol = new QgsSymbol(**symbolIt);
-      currentSymbolItem->setData(QVariant::fromValue((void*)legendSymbol));
+      //Pass pointer to QgsSymbol as user data. Cast to void* necessary such that QMetaType handles it
+      currentSymbolItem->setData(QVariant::fromValue((void*)(*symbolIt)));
 
       if(!currentSymbolItem)
 	{



More information about the QGIS-commit mailing list