[QGIS Commit] r10775 - in trunk/qgis: images/themes/default src/core/composer src/gui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Tue May 12 09:38:32 EDT 2009


Author: mhugent
Date: 2009-05-12 09:38:32 -0400 (Tue, 12 May 2009)
New Revision: 10775

Added:
   trunk/qgis/images/themes/default/mIconLock.png
Modified:
   trunk/qgis/src/core/composer/qgscomposeritem.cpp
   trunk/qgis/src/core/composer/qgscomposeritem.h
   trunk/qgis/src/core/composer/qgscomposermap.cpp
   trunk/qgis/src/core/composer/qgscomposermap.h
   trunk/qgis/src/gui/qgscomposerview.cpp
Log:
Possibility to lock/unlock composer item position by right mouse click

Added: trunk/qgis/images/themes/default/mIconLock.png
===================================================================
(Binary files differ)


Property changes on: trunk/qgis/images/themes/default/mIconLock.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Modified: trunk/qgis/src/core/composer/qgscomposeritem.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposeritem.cpp	2009-05-12 11:58:25 UTC (rev 10774)
+++ trunk/qgis/src/core/composer/qgscomposeritem.cpp	2009-05-12 13:38:32 UTC (rev 10775)
@@ -16,20 +16,25 @@
  ***************************************************************************/
 #include <QWidget>
 #include <QDomNode>
+#include <QFile>
 #include <QGraphicsScene>
 #include <QGraphicsSceneMouseEvent>
+#include <QGraphicsView>
 #include <QPainter>
 
 #include "qgscomposition.h"
 #include "qgscomposeritem.h"
 
+
 #include <limits>
+#include "qgsapplication.h"
 #include "qgsrectangle.h" //just for debugging
 #include "qgslogger.h"
 
 #define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
 
-QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue ): QGraphicsRectItem( 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), mFrame( true )
+QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue ): QGraphicsRectItem( 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), \
+        mFrame( true ), mItemPositionLocked(false)
 {
   setFlag( QGraphicsItem::ItemIsSelectable, true );
   setAcceptsHoverEvents( true );
@@ -119,6 +124,17 @@
   composerItemElem.setAttribute( "zValue", QString::number( zValue() ) );
   composerItemElem.setAttribute( "outlineWidth", QString::number( pen().widthF() ) );
 
+  //position lock for mouse moves/resizes
+  if(mItemPositionLocked)
+  {
+    composerItemElem.setAttribute( "positionLock", "true");
+  }
+  else
+  {
+    composerItemElem.setAttribute( "positionLock", "false");
+  }
+
+
   //frame color
   QDomElement frameColorElem = doc.createElement( "FrameColor" );
   QColor frameColor = pen().color();
@@ -160,6 +176,17 @@
     mFrame = false;
   }
 
+  //position lock for mouse moves/resizes
+  QString positionLock = itemElem.attribute("positionLock");
+  if(positionLock.compare("true", Qt::CaseInsensitive) == 0)
+  {
+      mItemPositionLocked = true;
+  }
+  else
+  {
+      mItemPositionLocked = false;
+  }
+
   //position
   double x, y, width, height;
   bool xOk, yOk, widthOk, heightOk;
@@ -219,7 +246,11 @@
 
 void QgsComposerItem::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
 {
-  qWarning( "QgsComposerItem::mouseMoveEvent" );
+    if(mItemPositionLocked)
+    {
+        return;
+    }
+
   if ( mBoundingResizeRectangle )
   {
     double diffX = event->lastScenePos().x() - mLastMouseEventPos.x();
@@ -232,6 +263,11 @@
 
 void QgsComposerItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
 {
+    if(mItemPositionLocked)
+    {
+        return;
+    }
+
   //set current position and type of mouse move action
   mMouseMoveStartPos = event->lastScenePos();
   mLastMouseEventPos = event->lastScenePos();
@@ -260,6 +296,12 @@
 
 void QgsComposerItem::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
 {
+
+    if(mItemPositionLocked)
+    {
+        return;
+    }
+
   //delete frame rectangle
   if ( mBoundingResizeRectangle )
   {
@@ -289,6 +331,10 @@
 {
   QgsComposerItem::MouseMoveAction mouseAction = mouseMoveActionForPosition( itemCoordPos );
 
+  if( mouseAction == QgsComposerItem::NoAction)
+  {
+      return Qt::ForbiddenCursor;
+  }
   if ( mouseAction == QgsComposerItem::MoveItem )
   {
     return Qt::ClosedHandCursor;
@@ -314,9 +360,12 @@
 QgsComposerItem::MouseMoveAction QgsComposerItem::mouseMoveActionForPosition( const QPointF& itemCoordPos )
 {
 
-  //move content tool
+  //no action at all if item position is locked for mouse
+  if(mItemPositionLocked)
+  {
+      return QgsComposerItem::NoAction;
+  }
 
-
   bool nearLeftBorder = false;
   bool nearRightBorder = false;
   bool nearLowerBorder = false;
@@ -472,15 +521,53 @@
 
   if ( mComposition->plotStyle() == QgsComposition::Preview )
   {
-    p->setPen( QPen( QColor( 0, 0, 255 ) ) );
-    p->setBrush( QBrush( QColor( 0, 0, 255 ) ) );
+    //size of symbol boxes depends on zoom level in composer view
+    double viewScaleFactor = horizontalViewScaleFactor();
+    double rectHandlerSize = 10.0 / viewScaleFactor;
+    double lockSymbolSize = 20.0 / viewScaleFactor;
 
-    double s = 5;
+    //make sure the boxes don't get too large
+    if(rectHandlerSize > (rect().width() / 3))
+    {
+        rectHandlerSize = rect().width() / 3;
+    }
+    if(rectHandlerSize > (rect().height() / 3))
+    {
+        rectHandlerSize = rect().height() / 3;
+    }
+    if(lockSymbolSize > (rect().width() / 3))
+    {
+        lockSymbolSize = rect().width() / 3;
+    }
+    if(lockSymbolSize > (rect().height() / 3))
+    {
+        lockSymbolSize = rect().height() / 3;
+    }
 
-    p->drawRect( QRectF( 0, 0, s, s ) );
-    p->drawRect( QRectF( rect().width() - s, 0, s, s ) );
-    p->drawRect( QRectF( rect().width() - s, rect().height() - s, s, s ) );
-    p->drawRect( QRectF( 0, rect().height() - s, s, s ) );
+    if(mItemPositionLocked)
+    {
+        //draw lock symbol at upper left edge. Use QImage to be independant of the graphic system
+        QString lockIconPath = QgsApplication::activeThemePath() + "/mIconLock.png";
+        if(!QFile::exists(lockIconPath))
+        {
+            lockIconPath = QgsApplication::defaultThemePath() + "/mIconLock.png";
+        }
+
+        QImage lockImage(lockIconPath);
+        if(!lockImage.isNull())
+        {
+            p->drawImage(QRectF(0, 0, lockSymbolSize, lockSymbolSize), lockImage, QRectF(0, 0, lockImage.width(), lockImage.height()));
+        }
+    }
+    else //draw blue squares
+    {
+        p->setPen( QPen( QColor( 0, 0, 255 ) ) );
+        p->setBrush( QBrush( QColor( 0, 0, 255 ) ) );
+        p->drawRect( QRectF( 0, 0, rectHandlerSize, rectHandlerSize ) );
+        p->drawRect( QRectF( rect().width() - rectHandlerSize, 0, rectHandlerSize, rectHandlerSize ) );
+        p->drawRect( QRectF( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
+        p->drawRect( QRectF( 0, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
+    }
   }
 }
 
@@ -637,3 +724,26 @@
   scaledFont.setPixelSize( pixelSize );
   return scaledFont;
 }
+
+double QgsComposerItem::horizontalViewScaleFactor() const
+{
+  double result = 1;
+  if ( scene() )
+  {
+    QList<QGraphicsView*> viewList = scene()->views();
+    if ( viewList.size() > 0 )
+    {
+      result = viewList.at( 0 )->transform().m11();
+    }
+    else
+    {
+      return 1; //probably called from non-gui code
+    }
+  }
+  return result;
+}
+
+void QgsComposerItem::updateCursor(const QPointF& itemPos)
+{
+    setCursor( cursorForPosition(itemPos) );
+}

Modified: trunk/qgis/src/core/composer/qgscomposeritem.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposeritem.h	2009-05-12 11:58:25 UTC (rev 10774)
+++ trunk/qgis/src/core/composer/qgscomposeritem.h	2009-05-12 13:38:32 UTC (rev 10775)
@@ -45,7 +45,8 @@
       ResizeLeftUp,
       ResizeRightUp,
       ResizeLeftDown,
-      ResizeRightDown
+      ResizeRightDown,
+      NoAction
     };
 
     enum ItemPositionMode
@@ -155,6 +156,18 @@
     /**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
     QFont scaledFontPixelSize( const QFont& font ) const;
 
+    /**Locks / unlocks the item position for mouse drags
+    @note this method was added in version 1.2*/
+    void setPositionLock(bool lock){mItemPositionLocked = lock;}
+
+    /**Returns position lock for mouse drags (true means locked)
+    @note this method was added in version 1.2*/
+    bool positionLock() const {return mItemPositionLocked;}
+
+    /**Update mouse cursor at (item) position
+    @note this method was added in version 1.2*/
+    void updateCursor(const QPointF& itemPos);
+
   protected:
 
     QgsComposition* mComposition;
@@ -171,6 +184,10 @@
     /**True if item fram needs to be painted*/
     bool mFrame;
 
+    /**True if item position  and size cannot be changed with mouse move
+    @note: this member was added in version 1.2*/
+    bool mItemPositionLocked;
+
     //event handlers
     virtual void mouseMoveEvent( QGraphicsSceneMouseEvent * event );
     virtual void mousePressEvent( QGraphicsSceneMouseEvent * event );
@@ -202,6 +219,10 @@
 
     /**Draw background*/
     virtual void drawBackground( QPainter* p );
+
+    /**Returns the zoom factor of the graphics view. If no
+     graphics view exists, the default 1 is returned*/
+    double horizontalViewScaleFactor() const;
 };
 
 #endif

Modified: trunk/qgis/src/core/composer/qgscomposermap.cpp
===================================================================
--- trunk/qgis/src/core/composer/qgscomposermap.cpp	2009-05-12 11:58:25 UTC (rev 10774)
+++ trunk/qgis/src/core/composer/qgscomposermap.cpp	2009-05-12 13:38:32 UTC (rev 10775)
@@ -34,7 +34,6 @@
 #include "qgslabelattributes.h"
 
 #include <QGraphicsScene>
-#include <QGraphicsView>
 #include <QPainter>
 #include <QSettings>
 #include <iostream>
@@ -457,24 +456,6 @@
   return false;
 }
 
-double QgsComposerMap::horizontalViewScaleFactor() const
-{
-  double result = 1;
-  if ( scene() )
-  {
-    QList<QGraphicsView*> viewList = scene()->views();
-    if ( viewList.size() > 0 )
-    {
-      result = viewList.at( 0 )->transform().m11();
-    }
-    else
-    {
-      return 1; //probably called from non-gui code
-    }
-  }
-  return result;
-}
-
 void QgsComposerMap::connectUpdateSlot()
 {
   //connect signal from layer registry to update in case of new or deleted layers

Modified: trunk/qgis/src/core/composer/qgscomposermap.h
===================================================================
--- trunk/qgis/src/core/composer/qgscomposermap.h	2009-05-12 11:58:25 UTC (rev 10774)
+++ trunk/qgis/src/core/composer/qgscomposermap.h	2009-05-12 13:38:32 UTC (rev 10775)
@@ -185,10 +185,6 @@
 
     /**Establishes signal/slot connection for update in case of layer change*/
     void connectUpdateSlot();
-
-    /**Returns the zoom factor of the graphics view. If no
-     graphics view exists, the default 1 is returned*/
-    double horizontalViewScaleFactor() const;
 };
 
 #endif

Modified: trunk/qgis/src/gui/qgscomposerview.cpp
===================================================================
--- trunk/qgis/src/gui/qgscomposerview.cpp	2009-05-12 11:58:25 UTC (rev 10774)
+++ trunk/qgis/src/gui/qgscomposerview.cpp	2009-05-12 13:38:32 UTC (rev 10775)
@@ -45,6 +45,22 @@
   QPointF scenePoint = mapToScene( e->pos() );
   QPointF snappedScenePoint = composition()->snapPointToGrid( scenePoint );
 
+  //lock/unlock position of item with right click
+  if(e->button() == Qt::RightButton)
+  {
+     QgsComposerItem* selectedItem = composition()->composerItemAt( scenePoint );
+     if(selectedItem)
+     {
+         bool lock = selectedItem->positionLock() ? false : true;
+         selectedItem->setPositionLock(lock);
+         selectedItem->update();
+         //make sure the new cursor is correct
+         QPointF itemPoint = selectedItem->mapFromScene( scenePoint );
+         selectedItem->updateCursor(itemPoint);
+     }
+     return;
+  }
+
   switch ( mCurrentTool )
   {
       //select/deselect items and pass mouse event further



More information about the QGIS-commit mailing list