[QGIS Commit] r8764 - in branches/advanced_printing_branch/src: app/composer ui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sun Jul 13 08:42:30 EDT 2008


Author: mhugent
Date: 2008-07-13 08:42:30 -0400 (Sun, 13 Jul 2008)
New Revision: 8764

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/qgscomposerlegendwidget.h
   branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp
   branches/advanced_printing_branch/src/ui/qgscomposerlegendwidgetbase.ui
Log:
Better support for point symbols in legend

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp	2008-07-12 21:01:43 UTC (rev 8763)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.cpp	2008-07-13 12:42:30 UTC (rev 8764)
@@ -18,6 +18,7 @@
 #include "qgscomposerlegend.h"
 #include "qgsmaplayer.h"
 #include "qgsmaplayerregistry.h"
+#include "qgssymbol.h"
 #include <QPainter>
 
 QgsComposerLegend::QgsComposerLegend(QgsComposition* composition): QgsComposerItem(composition), mTitle(QObject::tr("Legend")), mBoxSpace(2), mLayerSpace(3), mSymbolSpace(2), mIconLabelSpace(2)
@@ -137,8 +138,29 @@
       if(secondItem) //an item with an icon
 	{
 	  QIcon symbolIcon = firstItem->icon();
+
+	  //take QgsSymbol* from user data
+	  QVariant symbolVariant = firstItem->data();
+	  if(!symbolVariant.canConvert<void*>())
+	    {
+	      continue;
+	    }
+	  void* symbolData = symbolVariant.value<void*>();
+	  QgsSymbol* symbol = (QgsSymbol*)(symbolData);
+	  if(!symbol)
+	    {
+	      continue;
+	    }
+
+	  //draw symbol considering output device resolution
+	  drawSymbol(p, symbol, currentYCoord + (itemHeight - mSymbolHeight) /2, currentXCoord);
+	  currentXCoord += mIconLabelSpace;
+
+	  /*
 	  symbolIcon.paint(p, QRect(currentXCoord, currentYCoord + (itemHeight - mSymbolHeight) /2, mSymbolWidth, mSymbolHeight));
 	  currentXCoord += (mSymbolWidth + mIconLabelSpace);
+	  */
+	  
 	  p->drawText(QPointF(currentXCoord, currentYCoord + itemFontMetrics.height()), secondItem->text());
 	}
       else //an item witout icon (e.g. name of classification field)
@@ -149,3 +171,82 @@
       currentYCoord += itemHeight;
     }
 }
+
+void QgsComposerLegend::drawSymbol(QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition) const
+{
+  if(!p || !s)
+    {
+      return;
+    }
+
+  QGis::VectorType symbolType = s->type();
+  switch(symbolType)
+    {
+    case QGis::Point:
+      drawPointSymbol(p, s, currentYCoord, currentXPosition);
+      break;
+    case QGis::Line:
+      drawLineSymbol(p, s, currentYCoord, currentXPosition);
+      break;
+    case QGis::Polygon:
+      drawPolygonSymbol(p, s, currentYCoord, currentXPosition);
+      break;
+    }
+}
+
+void QgsComposerLegend::drawPointSymbol(QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition) const
+{
+  if(!p || !s)
+    {
+      return;
+    }
+
+  QPaintDevice* paintDevice = p->device();
+  if(!paintDevice)
+    {
+      return;
+    }
+
+  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);
+
+  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)
+    {
+      return;
+    }
+
+  double yCoord = currentYCoord + mSymbolHeight/2;
+
+  p->setPen(s->pen());
+  p->drawLine(QPointF(currentXPosition, yCoord), QPointF(currentXPosition + mSymbolWidth, yCoord));
+  currentXPosition += mSymbolWidth;
+}
+
+void QgsComposerLegend::drawPolygonSymbol(QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition) const
+{
+  if(!p || !s)
+    {
+      return;
+    }
+
+  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-12 21:01:43 UTC (rev 8763)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposerlegend.h	2008-07-13 12:42:30 UTC (rev 8764)
@@ -21,6 +21,8 @@
 #include "qgscomposeritem.h"
 #include "qgslegendmodel.h"
 
+class QgsSymbol;
+
 class QgsComposerLegend: public QgsComposerItem
 {
  public:
@@ -98,6 +100,12 @@
      @param layerItem parent model item (layer)
      @param currentYCoord in/out: current y position of legend item*/
   void drawLayerChildItems(QPainter* p, QStandardItem* layerItem, double& currentYCoord);
+
+  /**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;
+  void drawPointSymbol(QPainter*, QgsSymbol* s, double currentYCoord, double& currentXPosition) const;
+  void drawLineSymbol(QPainter*, QgsSymbol* s, double currentYCoord, double& currentXPosition) const;
+  void drawPolygonSymbol(QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition) const; 
 };
 
 #endif

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposerlegendwidget.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposerlegendwidget.cpp	2008-07-12 21:01:43 UTC (rev 8763)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposerlegendwidget.cpp	2008-07-13 12:42:30 UTC (rev 8764)
@@ -22,6 +22,13 @@
 QgsComposerLegendWidget::QgsComposerLegendWidget(QgsComposerLegend* legend): mLegend(legend)
 {
   setupUi(this);
+
+  if(legend)
+    {
+      mItemTreeView->setModel(legend->model());
+    }
+
+  setGuiElements();
 }
 
 QgsComposerLegendWidget::QgsComposerLegendWidget(): mLegend(0)
@@ -34,6 +41,34 @@
 
 }
 
+void QgsComposerLegendWidget::setGuiElements()
+{
+  if(!mLegend)
+    {
+      return;
+    }
+
+  blockSignals(true);
+  mTitleLineEdit->setText(mLegend->title());
+  mSymbolWidthSpinBox->setValue(mLegend->symbolWidth());
+  mSymbolHeightSpinBox->setValue(mLegend->symbolHeight());
+  mLayerSpaceSpinBox->setValue(mLegend->layerSpace());
+  mSymbolSpaceSpinBox->setValue(mLegend->symbolSpace());
+  mIconLabelSpaceSpinBox->setValue(mLegend->iconLabelSpace());
+  mBoxSpaceSpinBox->setValue(mLegend->boxSpace());
+
+  if(mLegend->frame())
+    { 
+      mBoxCheckBox->setCheckState(Qt::Checked);
+    }
+  else
+    {
+      mBoxCheckBox->setCheckState(Qt::Unchecked);
+    }
+  blockSignals(false);
+}
+
+
 void QgsComposerLegendWidget::on_mTitleLineEdit_textChanged(const QString& text)
 {
   if(mLegend)

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposerlegendwidget.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposerlegendwidget.h	2008-07-12 21:01:43 UTC (rev 8763)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposerlegendwidget.h	2008-07-13 12:42:30 UTC (rev 8764)
@@ -45,6 +45,8 @@
 
  private:
   QgsComposerLegendWidget();
+  /**Sets GUI according to state of mLegend*/
+  void setGuiElements();
 
   QgsComposerLegend* mLegend;
 };

Modified: branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp	2008-07-12 21:01:43 UTC (rev 8763)
+++ branches/advanced_printing_branch/src/app/composer/qgslegendmodel.cpp	2008-07-13 12:42:30 UTC (rev 8764)
@@ -23,9 +23,12 @@
 #include "qgssymbol.h"
 #include "qgsvectorlayer.h"
 
-QgsLegendModel::QgsLegendModel()
+QgsLegendModel::QgsLegendModel(): QStandardItemModel()
 {
-
+  QStringList headerLabels;
+  headerLabels << tr("Symbol");
+  headerLabels << tr("Value");
+  setHorizontalHeaderLabels(headerLabels);
 }
 
 QgsLegendModel::~QgsLegendModel()
@@ -118,13 +121,17 @@
 	  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));
+
       if(!currentSymbolItem)
 	{
 	  continue;
 	}
 
       int currentRowCount = layerItem->rowCount();
-      layerItem->setChild(currentRowCount, 0, currentSymbolItem);
+      currentSymbolItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
 
       //label
       QString label;
@@ -141,7 +148,12 @@
 	}
 
       currentLabelItem = new QStandardItem(label);
-      layerItem->setChild(currentRowCount, 1, currentLabelItem);
+      currentLabelItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
+
+      QList<QStandardItem*> childItemList;
+      childItemList.push_back(currentSymbolItem);
+      childItemList.push_back(currentLabelItem);
+      layerItem->appendRow(childItemList);
       
     }
   

Modified: branches/advanced_printing_branch/src/ui/qgscomposerlegendwidgetbase.ui
===================================================================
--- branches/advanced_printing_branch/src/ui/qgscomposerlegendwidgetbase.ui	2008-07-12 21:01:43 UTC (rev 8763)
+++ branches/advanced_printing_branch/src/ui/qgscomposerlegendwidgetbase.ui	2008-07-13 12:42:30 UTC (rev 8764)
@@ -5,8 +5,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>482</width>
-    <height>415</height>
+    <width>495</width>
+    <height>513</height>
    </rect>
   </property>
   <property name="sizePolicy" >
@@ -18,7 +18,7 @@
   <property name="windowTitle" >
    <string>Barscale Options</string>
   </property>
-  <layout class="QGridLayout" >
+  <layout class="QGridLayout" name="gridLayout_2" >
    <item row="0" column="0" >
     <widget class="QGroupBox" name="mGeneralGroupBox" >
      <property name="title" >
@@ -138,7 +138,7 @@
           <property name="orientation" >
            <enum>Qt::Horizontal</enum>
           </property>
-          <property name="sizeHint" >
+          <property name="sizeHint" stdset="0" >
            <size>
             <width>141</width>
             <height>20</height>
@@ -162,7 +162,7 @@
           <property name="orientation" >
            <enum>Qt::Horizontal</enum>
           </property>
-          <property name="sizeHint" >
+          <property name="sizeHint" stdset="0" >
            <size>
             <width>131</width>
             <height>20</height>
@@ -190,6 +190,11 @@
      <property name="title" >
       <string>Items</string>
      </property>
+     <layout class="QGridLayout" name="gridLayout" >
+      <item row="0" column="0" >
+       <widget class="QTreeView" name="mItemTreeView" />
+      </item>
+     </layout>
     </widget>
    </item>
   </layout>



More information about the QGIS-commit mailing list