[QGIS Commit] r9405 - in branches/advanced_printing_branch2/src: core core/composer gui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Sep 24 14:37:47 EDT 2008


Author: mhugent
Date: 2008-09-24 14:37:47 -0400 (Wed, 24 Sep 2008)
New Revision: 9405

Modified:
   branches/advanced_printing_branch2/src/core/CMakeLists.txt
   branches/advanced_printing_branch2/src/core/composer/qgscomposeritem.cpp
   branches/advanced_printing_branch2/src/core/composer/qgscomposeritem.h
   branches/advanced_printing_branch2/src/core/composer/qgscomposition.cpp
   branches/advanced_printing_branch2/src/core/composer/qgscomposition.h
   branches/advanced_printing_branch2/src/gui/qgscomposerview.cpp
Log:
Improving for snap to grid in composer. Still needs more work

Modified: branches/advanced_printing_branch2/src/core/CMakeLists.txt
===================================================================
--- branches/advanced_printing_branch2/src/core/CMakeLists.txt	2008-09-24 16:35:10 UTC (rev 9404)
+++ branches/advanced_printing_branch2/src/core/CMakeLists.txt	2008-09-24 18:37:47 UTC (rev 9405)
@@ -56,6 +56,7 @@
   composer/qgscomposerscalebar.cpp
   composer/qgslegendmodel.cpp
   composer/qgscomposerlegend.cpp
+  composer/qgspaperitem.cpp
   composer/qgsscalebarstyle.cpp
   composer/qgsdoubleboxscalebarstyle.cpp
   composer/qgsnumericscalebarstyle.cpp

Modified: branches/advanced_printing_branch2/src/core/composer/qgscomposeritem.cpp
===================================================================
--- branches/advanced_printing_branch2/src/core/composer/qgscomposeritem.cpp	2008-09-24 16:35:10 UTC (rev 9404)
+++ branches/advanced_printing_branch2/src/core/composer/qgscomposeritem.cpp	2008-09-24 18:37:47 UTC (rev 9405)
@@ -29,7 +29,7 @@
 
 #define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
 
-QgsComposerItem::QgsComposerItem( QgsComposition* composition ): QGraphicsRectItem( 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), mFrame( true )
+QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue): QGraphicsRectItem( 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), mFrame( true )
 {
   setFlag( QGraphicsItem::ItemIsSelectable, true );
   setAcceptsHoverEvents( true );
@@ -41,13 +41,13 @@
   setPen( defaultPen );
 
   //let z-Value be managed by composition
-  if ( mComposition )
+  if ( mComposition && manageZValue)
   {
     mComposition->addItemToZList( this );
   }
 }
 
-QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition ): QGraphicsRectItem( 0, 0, width, height, 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), mFrame( true )
+QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition, bool manageZValue): QGraphicsRectItem( 0, 0, width, height, 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), mFrame( true )
 {
   setFlag( QGraphicsItem::ItemIsSelectable, true );
   setAcceptsHoverEvents( true );
@@ -63,7 +63,7 @@
   setPen( defaultPen );
 
 //let z-Value be managed by composition
-  if ( mComposition )
+  if ( mComposition && manageZValue)
   {
     mComposition->addItemToZList( this );
   }
@@ -222,43 +222,69 @@
   qWarning( "QgsComposerItem::mouseMoveEvent" );
     
   QPointF sceneMovePoint = event->lastScenePos();
-
-  if(composition() && composition()->snapToGridEnabled())
-    {
-      sceneMovePoint = composition()->snapPointToGrid(sceneMovePoint);
-    }
-
   if ( mBoundingResizeRectangle )
   {
-    double diffX = sceneMovePoint.x() - mLastMouseEventPos.x();
-    double diffY = sceneMovePoint.y() - mLastMouseEventPos.y();
+    //move action, change only transform but not rectangle
+    if(mCurrentMouseMoveAction == QgsComposerItem::moveItem) //move action
+      {
+	double diffX = sceneMovePoint.x() - mLastMouseEventPos.x();
+	double diffY = sceneMovePoint.y() - mLastMouseEventPos.y();
 
-    double mx, my, rx, ry;
+	//snap rectangle position but not the move point
+	double mx, my, rx, ry;
+	rectangleChange( diffX, diffY, mx, my, rx, ry );
 
-    rectangleChange( diffX, diffY, mx, my, rx, ry );
+	QTransform oldTransform = mBoundingResizeRectangle->transform();
+	QTransform transform;
+	QPointF upperLeft(oldTransform.dx() + mx, oldTransform.dy() + my);
+	QPointF snappedUpperLeft = upperLeft;
+	if(mx != 0.0 || my != 0.0)
+	  {
+	    snappedUpperLeft = mComposition->snapPointToGrid(upperLeft);
+	  }
+	transform.translate( snappedUpperLeft.x(), snappedUpperLeft.y());
+	mBoundingResizeRectangle->setTransform(transform);
 
-    QRectF r = mBoundingResizeRectangle->rect();
-    double newWidth = r.width() + rx;
-    double newHeight = r.height() + ry;
-
-    QTransform oldTransform = mBoundingResizeRectangle->transform();
-    QTransform transform;
-    transform.translate( oldTransform.dx() + mx, oldTransform.dy() + my );
-
-    QRectF newBoundingRect( 0, 0, newWidth, newHeight );
-
-    mBoundingResizeRectangle->setRect( newBoundingRect );
-    mBoundingResizeRectangle->setTransform( transform );
+	mLastMouseEventPos.setX(sceneMovePoint.x() + snappedUpperLeft.x() - upperLeft.x());
+	mLastMouseEventPos.setY(sceneMovePoint.y() + snappedUpperLeft.y() - upperLeft.y());
+      }
+    else //resize action, change only rect but not transformation
+      {
+	if(composition() && composition()->snapToGridEnabled())
+	  {
+	    sceneMovePoint = composition()->snapPointToGrid(sceneMovePoint);
+	  }
+	
+	double diffX = sceneMovePoint.x() - mLastMouseEventPos.x();
+	double diffY = sceneMovePoint.y() - mLastMouseEventPos.y();
+	
+	double mx, my, rx, ry;
+	
+	rectangleChange( diffX, diffY, mx, my, rx, ry );
+	
+	QRectF r = mBoundingResizeRectangle->rect();
+	double newWidth = r.width() + rx;
+	double newHeight = r.height() + ry;	
+	QRectF newBoundingRect( 0, 0, newWidth, newHeight );
+	
+	mBoundingResizeRectangle->setRect( newBoundingRect );
+	mLastMouseEventPos = sceneMovePoint;
+      }
+    
   }
-  mLastMouseEventPos = sceneMovePoint;
 }
 
 void QgsComposerItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
 {
-  //set current position and type of mouse move action
+  if(!composition())
+    {
+      return;
+    }
+  mCurrentMouseMoveAction = mouseMoveActionForPosition( event->pos() );
+
+  //use snapped position for start point and last event point
   mMouseMoveStartPos = event->lastScenePos();
   mLastMouseEventPos = event->lastScenePos();
-  mCurrentMouseMoveAction = mouseMoveActionForPosition( event->pos() );
 
   //create and show bounding rectangle
   mBoundingResizeRectangle = new QGraphicsRectItem( 0 );
@@ -285,25 +311,46 @@
   }
 
   QPointF mouseMoveStopPoint = event->lastScenePos();
-  if(composition() && composition()->snapToGridEnabled())
+  
+  if(mCurrentMouseMoveAction == QgsComposerItem::moveItem) //move action
     {
-      mouseMoveStopPoint = composition()->snapPointToGrid(mouseMoveStopPoint);
-    }
-  double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
-  double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
+      double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
+      double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
 
-  //it was only a click
-  if ( abs( diffX ) < std::numeric_limits<double>::min() && abs( diffY ) < std::numeric_limits<double>::min() )
-  {
-    return;
-  }
+      //it was only a click
+      if ( abs( diffX ) < std::numeric_limits<double>::min() && abs( diffY ) < std::numeric_limits<double>::min() )
+	{
+	  return;
+	}
 
-  double mx, my, rx, ry;
-  rectangleChange( diffX, diffY, mx, my, rx, ry );
+      QPointF upperLeft(transform().dx() + diffX, transform().dy() + diffY);
+      QPointF snappedUpperLeft = mComposition->snapPointToGrid(upperLeft);
+      QTransform transform;
+      transform.translate(snappedUpperLeft.x(), snappedUpperLeft.y());
+      setTransform(transform);
+    }
+  else //resize action
+    {
+      if(composition() && composition()->snapToGridEnabled())
+	{
+	  mouseMoveStopPoint = composition()->snapPointToGrid(mouseMoveStopPoint);
+	}
+      double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
+      double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
+      
+      //it was only a click
+      if ( abs( diffX ) < std::numeric_limits<double>::min() && abs( diffY ) < std::numeric_limits<double>::min() )
+	{
+	  return;
+	}
+      
+      double mx, my, rx, ry;
+      rectangleChange( diffX, diffY, mx, my, rx, ry );
 
-  QRectF currentRect = rect();
-  QRectF newRect( transform().dx() + mx, transform().dy() + my, currentRect.width() + rx, currentRect.height() + ry );
-  setSceneRect( newRect );
+      QRectF currentRect = rect();
+      QRectF newRect( transform().dx() + mx, transform().dy() + my, currentRect.width() + rx, currentRect.height() + ry );
+      setSceneRect( newRect );
+    }
 
   update();
   scene()->update();

Modified: branches/advanced_printing_branch2/src/core/composer/qgscomposeritem.h
===================================================================
--- branches/advanced_printing_branch2/src/core/composer/qgscomposeritem.h	2008-09-24 16:35:10 UTC (rev 9404)
+++ branches/advanced_printing_branch2/src/core/composer/qgscomposeritem.h	2008-09-24 18:37:47 UTC (rev 9405)
@@ -48,8 +48,12 @@
       resizeDRightDown
     };
 
-    QgsComposerItem( QgsComposition* composition );
-    QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition );
+    /**Constructor 
+     @param manageZValue true if the z-Value of this object should be managed by mComposition*/
+    QgsComposerItem( QgsComposition* composition, bool manageZValue = true);
+    /**Constructor with box position and composer object
+     @param manageZValue true if the z-Value of this object should be managed by mComposition*/
+    QgsComposerItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition, bool manageZValue = true);
     virtual ~QgsComposerItem();
 
     /** \brief Set selected, selected item should be highlighted */

Modified: branches/advanced_printing_branch2/src/core/composer/qgscomposition.cpp
===================================================================
--- branches/advanced_printing_branch2/src/core/composer/qgscomposition.cpp	2008-09-24 16:35:10 UTC (rev 9404)
+++ branches/advanced_printing_branch2/src/core/composer/qgscomposition.cpp	2008-09-24 18:37:47 UTC (rev 9405)
@@ -17,6 +17,7 @@
 #include "qgscomposition.h"
 #include "qgscomposeritem.h"
 #include "qgscomposermap.h"
+#include "qgspaperitem.h"
 #include <QDomDocument>
 #include <QDomElement>
 #include <QGraphicsRectItem>
@@ -26,7 +27,7 @@
   setBackgroundBrush( Qt::gray );
 
   //set paper item
-  mPaperItem = new QGraphicsRectItem( 0, 0, 297, 210, 0 ); //default size A4
+  mPaperItem = new QgsPaperItem( 0, 0, 297, 210, this); //default size A4
   mPaperItem->setBrush( Qt::white );
   addItem( mPaperItem );
   mPaperItem->setZValue( 0 );
@@ -181,7 +182,7 @@
   if ( widthConversionOk && heightConversionOk )
   {
     delete mPaperItem;
-    mPaperItem = new QGraphicsRectItem( 0, 0, paperWidth, paperHeight, 0 );
+    mPaperItem = new QgsPaperItem( 0, 0, paperWidth, paperHeight, this);
     mPaperItem->setBrush( Qt::white );
     addItem( mPaperItem );
     mPaperItem->setZValue( 0 );
@@ -560,10 +561,10 @@
     }
 
   //snap x coordinate //todo: add support for x- and y- offset
-  int xRatio = (int)(scenePoint.x() / mSnapGridResolution + 0.5);
-  int yRatio = (int)(scenePoint.y() / mSnapGridResolution + 0.5);
+  int xRatio = (int)((scenePoint.x() - mSnapGridOffsetX) / mSnapGridResolution + 0.5);
+  int yRatio = (int)((scenePoint.y() - mSnapGridOffsetY) / mSnapGridResolution + 0.5);
 
-  return QPointF(xRatio * mSnapGridResolution, yRatio * mSnapGridResolution);
+  return QPointF(xRatio * mSnapGridResolution + mSnapGridOffsetX, yRatio * mSnapGridResolution + mSnapGridOffsetY);
 }
 
 int QgsComposition::boundingRectOfSelectedItems(QRectF& bRect)
@@ -605,3 +606,39 @@
   bRect.setBottomRight(QPointF(maxX, maxY));
   return 0;
 }
+
+void QgsComposition::setSnapToGridEnabled(bool b)
+{
+  mSnapToGrid = b;
+  if(mPaperItem)
+    {
+      mPaperItem->update();
+    }
+}
+
+void QgsComposition::setSnapGridResolution(double r)
+{
+  mSnapGridResolution = r;
+  if(mPaperItem)
+    {
+      mPaperItem->update();
+    }
+}
+
+void QgsComposition::setSnapGridOffsetX(double offset)
+{
+  mSnapGridOffsetX = offset;
+  if(mPaperItem)
+    {
+      mPaperItem->update();
+    }
+}
+
+void QgsComposition::setSnapGridOffsetY(double offset)
+{
+  mSnapGridOffsetY = offset;
+  if(mPaperItem)
+    {
+      mPaperItem->update();
+    } 
+}

Modified: branches/advanced_printing_branch2/src/core/composer/qgscomposition.h
===================================================================
--- branches/advanced_printing_branch2/src/core/composer/qgscomposition.h	2008-09-24 16:35:10 UTC (rev 9404)
+++ branches/advanced_printing_branch2/src/core/composer/qgscomposition.h	2008-09-24 18:37:47 UTC (rev 9405)
@@ -21,6 +21,7 @@
 
 class QgsComposerItem;
 class QgsComposerMap;
+class QgsPaperItem;
 class QGraphicsRectItem;
 class QgsMapRenderer;
 
@@ -57,16 +58,16 @@
     /**Returns width of paper item*/
     double paperWidth() const;
 
-    void setSnapToGridEnabled(bool b) {mSnapToGrid = b;}
+    void setSnapToGridEnabled(bool b); 
     bool snapToGridEnabled() const {return mSnapToGrid;}
 
-    void setSnapGridResolution(double r){mSnapGridResolution = r;}
+    void setSnapGridResolution(double r);
     double snapGridResolution() const {return mSnapGridResolution;}
 
-    void setSnapGridOffsetX(double offset){mSnapGridOffsetX = offset;}
+    void setSnapGridOffsetX(double offset);
     double snapGridOffsetX() const {return mSnapGridOffsetX;}
 
-    void setSnapGridOffsetY(double offset){mSnapGridOffsetY = offset;}
+    void setSnapGridOffsetY(double offset);
     double snapGridOffsetY() const {return mSnapGridOffsetY;}
 
     /**Returns the topmose composer item. Ignores mPaperItem*/
@@ -139,7 +140,7 @@
     /**Pointer to map renderer of QGIS main map*/
     QgsMapRenderer* mMapRenderer;
     QgsComposition::PlotStyle mPlotStyle;
-    QGraphicsRectItem* mPaperItem;
+    QgsPaperItem* mPaperItem;
 
     /**Maintains z-Order of items. Starts with item at position 1 (position 0 is always paper item)*/
     QLinkedList<QgsComposerItem*> mItemZList;

Modified: branches/advanced_printing_branch2/src/gui/qgscomposerview.cpp
===================================================================
--- branches/advanced_printing_branch2/src/gui/qgscomposerview.cpp	2008-09-24 16:35:10 UTC (rev 9404)
+++ branches/advanced_printing_branch2/src/gui/qgscomposerview.cpp	2008-09-24 18:37:47 UTC (rev 9405)
@@ -41,7 +41,8 @@
     return;
   }
 
-  QPointF scenePoint = mapToScene( e->pos() );
+  QPointF scenePoint = mapToScene( e->pos());
+  QPointF snappedScenePoint = composition()->snapPointToGrid(scenePoint);
 
   switch ( mCurrentTool )
   {
@@ -83,7 +84,7 @@
     {
       QTransform t;
       mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
-      t.translate( scenePoint.x(), scenePoint.y() );
+      t.translate( snappedScenePoint.x(), snappedScenePoint.y() );
       mRubberBandItem->setTransform( t );
       mRubberBandItem->setZValue( 100 );
       scene()->addItem( mRubberBandItem );
@@ -99,7 +100,7 @@
       scene()->addItem( newLabelItem );
       emit composerLabelAdded( newLabelItem );
       scene()->clearSelection();
-      newLabelItem->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), newLabelItem->rect().width(), newLabelItem->rect().height() ) );
+      newLabelItem->setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), newLabelItem->rect().width(), newLabelItem->rect().height() ) );
       newLabelItem->setSelected( true );
       emit selectedItemChanged( newLabelItem );
       emit actionFinished();
@@ -117,7 +118,7 @@
         newScaleBar->setComposerMap( mapItemList.at( 0 ) );
       }
 
-      newScaleBar->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), 20, 20 ) );
+      newScaleBar->setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), 20, 20 ) );
       newScaleBar->applyDefaultSettings(); //4 segments, 1/5 of composer map width
       scene()->addItem( newScaleBar );
       emit composerScaleBarAdded( newScaleBar );
@@ -134,7 +135,7 @@
       scene()->addItem( newLegend );
       emit composerLegendAdded( newLegend );
       scene()->clearSelection();
-      newLegend->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), newLegend->rect().width(), newLegend->rect().height() ) );
+      newLegend->setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), newLegend->rect().width(), newLegend->rect().height() ) );
       newLegend->setSelected( true );
       emit selectedItemChanged( newLegend );
       emit actionFinished();
@@ -146,7 +147,7 @@
       scene()->addItem( newPicture );
       emit composerPictureAdded( newPicture );
       scene()->clearSelection();
-      newPicture->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), 30, 30 ) );
+      newPicture->setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), 30, 30 ) );
       newPicture->setSelected( true );
       emit selectedItemChanged( newPicture );
       emit actionFinished();



More information about the QGIS-commit mailing list