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

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sun May 25 07:38:52 EDT 2008


Author: mhugent
Date: 2008-05-25 07:38:52 -0400 (Sun, 25 May 2008)
New Revision: 8514

Modified:
   branches/advanced_printing_branch/src/app/composer/qgscomposeritem.cpp
   branches/advanced_printing_branch/src/app/composer/qgscomposeritem.h
   branches/advanced_printing_branch/src/app/composer/qgscomposermap.cpp
   branches/advanced_printing_branch/src/app/composer/qgscomposermap.h
   branches/advanced_printing_branch/src/app/composer/qgscomposition.cpp
Log:
Composer map is now resizeable. Other item types will follow soon

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposeritem.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposeritem.cpp	2008-05-24 19:53:45 UTC (rev 8513)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposeritem.cpp	2008-05-25 11:38:52 UTC (rev 8514)
@@ -16,6 +16,7 @@
  ***************************************************************************/
 #include <QWidget>
 #include <QDomNode>
+#include <QGraphicsScene>
 #include <QGraphicsSceneMouseEvent>
 
 #include "qgscomposition.h"
@@ -23,16 +24,18 @@
 
 #include <iostream>
 
-QgsComposerItem::QgsComposerItem(QGraphicsItem* parent): QGraphicsRectItem(0)
+QgsComposerItem::QgsComposerItem(QGraphicsItem* parent): QGraphicsRectItem(0), mBoundingResizeRectangle(0) 
 {
     mSelected = false;
     mPlotStyle = QgsComposition::Preview;
+    setAcceptsHoverEvents(true);
 }
 
-QgsComposerItem::QgsComposerItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem* parent): QGraphicsRectItem(x, y, width, height, parent)
+QgsComposerItem::QgsComposerItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem* parent): QGraphicsRectItem(x, y, width, height, parent), mBoundingResizeRectangle(0)
 {
   mSelected = false;
   mPlotStyle = QgsComposition::Preview;
+  setAcceptsHoverEvents(true);
 }
 
 QgsComposerItem::~QgsComposerItem()
@@ -69,22 +72,215 @@
 
 void QgsComposerItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
 {
-  qWarning("QgsComposerItem::mouseMoveEvent");
+  if(mBoundingResizeRectangle)
+    {
+      double diffX = event->lastPos().x() - mLastMouseEventPos.x();
+      double diffY = event->lastPos().y() - mLastMouseEventPos.y();
+
+      double mx, my, rx, ry;
+
+      rectangleChange(diffX, diffY, mx, my, rx, ry);
+      
+      QRectF r = mBoundingResizeRectangle->rect();
+      r.translate(mx, my);
+      r.setWidth(r.width() + rx);
+      r.setHeight(r.height() + ry);
+      mBoundingResizeRectangle->setRect(r);
+    }
+  mLastMouseEventPos = event->lastPos();
 }
 
 void QgsComposerItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )
 {
-  qWarning("QgsComposerItem::mousePressEvent");
-  setCursor(QCursor(evaluateCursor(event->pos())));
+  //set current position and type of mouse move action
+  mMouseMoveStartPos = event->lastScenePos();
+  mLastMouseEventPos = event->lastPos();
+  mCurrentMouseMoveAction = mouseMoveActionForPosition(event->lastPos());
+  setCursor(QCursor(cursorForPosition(event->pos())));
+
+  //create and show bounding rectangle
+  mBoundingResizeRectangle = new QGraphicsRectItem(QGraphicsRectItem::rect(), 0);
+  scene()->addItem(mBoundingResizeRectangle);
+  mBoundingResizeRectangle->moveBy(x(), y());
+  mBoundingResizeRectangle->setBrush( Qt::NoBrush );
+  mBoundingResizeRectangle->setPen( QPen(QColor(0,0,0), 0) );
+  mBoundingResizeRectangle->setZValue(100);
+  mBoundingResizeRectangle->show();
+
+  //QGraphicsRectItem::setVisible(false);
 }
 
 void QgsComposerItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
 {
-  qWarning("QgsComposerItem::mouseReleseEvent");
+  //delete frame rectangle
+  if(mBoundingResizeRectangle)
+    {
+      scene()->removeItem(mBoundingResizeRectangle);
+      delete mBoundingResizeRectangle;
+      mBoundingResizeRectangle = 0;
+    }
+
+  //QGraphicsRectItem::setVisible(true);
+
+  QPointF mouseMoveStopPoint = event->lastScenePos();
+  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);
+  moveBy(mx, my);
+  resize(rx, ry);
+
+  //reset default action
+  mCurrentMouseMoveAction = QgsComposerItem::moveItem;
   setCursor(Qt::ArrowCursor);
 }
 
-Qt::CursorShape QgsComposerItem::evaluateCursor(const QPointF& itemCoordPos)
+void QgsComposerItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event )
 {
-  return Qt::ClosedHandCursor;
+  qWarning("QgsComposerItem::hoverEnterEvent");
 }
+
+void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
+{
+  qWarning("QgsComposerItem::hoverMoveEvent");
+  setCursor(QCursor(cursorForPosition(event->pos())));
+}
+
+Qt::CursorShape QgsComposerItem::cursorForPosition(const QPointF& itemCoordPos)
+{
+  QgsComposerItem::mouseMoveAction mouseAction = mouseMoveActionForPosition(itemCoordPos);
+  
+  if(mouseAction == QgsComposerItem::moveItem)
+    {
+      return Qt::ClosedHandCursor;
+    }
+  else if(mouseAction == QgsComposerItem::resizeDLeftUp || mouseAction == QgsComposerItem::resizeDRightDown)
+    {
+      return Qt::SizeFDiagCursor;
+    }
+  else if(mouseAction == QgsComposerItem::resizeDLeftDown || mouseAction == QgsComposerItem::resizeDRightUp)
+    {
+      return Qt::SizeBDiagCursor;
+    }
+  else if(mouseAction == QgsComposerItem::resizeUp || mouseAction == QgsComposerItem::resizeDown)
+    {
+      return Qt::SizeVerCursor;
+    }
+  else //if(mouseAction == QgsComposerItem::resizeLeft || mouseAction == QgsComposerItem::resizeRight)
+    {
+      return Qt::SizeHorCursor;
+    }
+}
+
+QgsComposerItem::mouseMoveAction QgsComposerItem::mouseMoveActionForPosition(const QPointF& itemCoordPos)
+{
+  bool nearLeftBorder = false;
+  bool nearRightBorder = false;
+  bool nearLowerBorder = false;
+  bool nearUpperBorder = false;
+
+  if(itemCoordPos.x() < 5)
+    {
+      nearLeftBorder = true;
+    }
+  if(itemCoordPos.y() < 5)
+    {
+      nearUpperBorder = true;
+    }
+  if(itemCoordPos.x() > (rect().right() - 5))
+    {
+      nearRightBorder = true;
+    }
+  if(itemCoordPos.y() > (rect().bottom() - 5))
+    {
+      nearLowerBorder = true;
+    }
+
+  if(nearLeftBorder && nearUpperBorder)
+    {
+      return QgsComposerItem::resizeDLeftUp;
+    }
+  else if(nearLeftBorder && nearLowerBorder)
+    {
+      return QgsComposerItem::resizeDLeftDown;
+    }
+  else if(nearRightBorder && nearUpperBorder)
+    {
+      return QgsComposerItem::resizeDRightUp;
+    }
+  else if(nearRightBorder && nearLowerBorder)
+    {
+      return QgsComposerItem::resizeDRightDown;
+    }
+  else if(nearLeftBorder)
+    {
+      return QgsComposerItem::resizeLeft;
+    }
+  else if(nearRightBorder)
+    {
+      return QgsComposerItem::resizeRight;
+    }
+  else if(nearUpperBorder)
+    {
+      return QgsComposerItem::resizeUp;
+    }
+  else if(nearLowerBorder)
+    {
+      return QgsComposerItem::resizeDown;
+    }
+
+  return QgsComposerItem::moveItem; //default
+}
+
+
+void QgsComposerItem::rectangleChange(double dx, double dy, double& mx, double& my, double& rx, double& ry) const
+{
+  switch(mCurrentMouseMoveAction)
+    {
+      //vertical resize
+    case QgsComposerItem::resizeUp:
+      mx = 0; my = dy; rx = 0; ry = -dy;
+      break;
+
+    case QgsComposerItem::resizeDown:
+      mx = 0; my = 0; rx = 0; ry = dy;
+      break;
+
+      //horizontal resize
+    case QgsComposerItem::resizeLeft:
+      mx = dx, my = 0; rx = -dx; ry = 0;
+      break;
+
+    case QgsComposerItem::resizeRight:
+      mx = 0; my = 0; rx = dx, ry = 0;
+      break;
+
+      //diagonal resize
+    case QgsComposerItem::resizeDLeftUp:
+      mx = dx, my = dy; rx = -dx; ry = -dy;
+      break;
+
+    case QgsComposerItem::resizeDRightDown:
+      mx = 0; my = 0; rx = dx, ry = dy;
+      break;
+
+    case QgsComposerItem::resizeDRightUp:
+      mx = 0; my = dy, rx = dx, ry = -dy;
+      break;
+
+    case QgsComposerItem::resizeDLeftDown:
+      mx = dx, my = 0; rx = -dx; ry = dy;
+      break;
+
+    case QgsComposerItem::moveItem:
+      mx = dx; my = dy;
+      break;
+    }
+}

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposeritem.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposeritem.h	2008-05-24 19:53:45 UTC (rev 8513)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposeritem.h	2008-05-25 11:38:52 UTC (rev 8514)
@@ -30,10 +30,25 @@
 {
 
 public:
+
+  /**Describes the action (move or resize in different directon) to be done during mouse move*/
+  enum mouseMoveAction
+    {
+      moveItem,
+      resizeUp,
+      resizeDown,
+      resizeLeft,
+      resizeRight,
+      resizeDLeftUp,
+      resizeDRightUp,
+      resizeDLeftDown,
+      resizeDRightDown
+    };
+
     QgsComposerItem(QGraphicsItem* parent = 0);
     QgsComposerItem(qreal x, qreal y, qreal width, qreal height,QGraphicsItem* parent = 0); 
     virtual ~QgsComposerItem(); 
-public:
+
     /** \brief Set plot style */
     void setPlotStyle ( QgsComposition::PlotStyle p );
 
@@ -66,6 +81,9 @@
     /** delete settings from project file  */
     virtual bool removeSettings( void );
 
+    /** resizes an item in x- and y direction (canvas coordinates)*/
+    virtual void resize(double dx, double dy){}
+
     /** stores state in DOM node
      * @param node is DOM node corresponding to '???' tag
      * @param temp write template file
@@ -81,16 +99,37 @@
     QgsComposition::PlotStyle mPlotStyle;
     bool mSelected;
     int mId;
+    QgsComposerItem::mouseMoveAction mCurrentMouseMoveAction;
+    /**Start point of the last mouse move action (in scene coordinates)*/
+    QPointF mMouseMoveStartPos;
+    /**Position of the last mouse move event (in item coordinates)*/
+    QPointF mLastMouseEventPos;
 
+    /**Rectangle used during move and resize actions*/
+    QGraphicsRectItem* mBoundingResizeRectangle;
+ 
     //event handlers
     virtual void mouseMoveEvent ( QGraphicsSceneMouseEvent * event );
     virtual void mousePressEvent ( QGraphicsSceneMouseEvent * event );
     virtual void mouseReleaseEvent ( QGraphicsSceneMouseEvent * event );
+    
+    virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event );
+    virtual void hoverMoveEvent ( QGraphicsSceneHoverEvent * event );
 
     /**Finds out the appropriate cursor for the current mouse position in the widget (e.g. move in the middle, resize at border)*/
-    Qt::CursorShape evaluateCursor(const QPointF& itemCoordPos);
+    Qt::CursorShape cursorForPosition(const QPointF& itemCoordPos);
 
-private:
+    /**Finds out which mouse move action to choose depending on the cursor position inside the widget*/
+    QgsComposerItem::mouseMoveAction mouseMoveActionForPosition(const QPointF& itemCoordPos);
+    
+    /**Calculate rectangle changes according to mouse move (dx, dy) and the current mouse move action
+       @param dx x-coordinate move of cursor
+       @param dy y-coordinate move of cursor
+       @param mx out: rectangle should be moved by mx in x-direction
+       @param my out: rectangle should be moved by my in y-direction
+       @param rx out: width of rectangle should be resized by rx
+       @param ry out: height of rectangle should be resized by ry*/
+    void rectangleChange(double dx, double dy, double& mx, double& my, double& rx, double& ry) const;
 };
 
 #endif

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposermap.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposermap.cpp	2008-05-24 19:53:45 UTC (rev 8513)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposermap.cpp	2008-05-25 11:38:52 UTC (rev 8514)
@@ -683,3 +683,17 @@
     return true;
 }
 
+void QgsComposerMap::resize(double dx, double dy)
+{
+  qWarning("QgsComposerMap::resize");
+  qWarning(QString::number(dx).toLatin1());
+  qWarning(QString::number(dy).toLatin1());
+
+  //setRect
+  QRectF currentRect = QGraphicsRectItem::rect();
+  setRect(currentRect.x(), currentRect.y(), currentRect.width() + dx, currentRect.height() + dy);
+
+  recalculate();
+  
+}
+

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposermap.h
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposermap.h	2008-05-24 19:53:45 UTC (rev 8513)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposermap.h	2008-05-25 11:38:52 UTC (rev 8514)
@@ -104,6 +104,9 @@
     /** \brief Font size scale */
     double fontScale ( void );
 
+    /** resizes an item in x- and y direction (canvas coordinates)*/
+    void resize(double dx, double dy);
+
     /** \brief Scale */
     double scale ( void );
 

Modified: branches/advanced_printing_branch/src/app/composer/qgscomposition.cpp
===================================================================
--- branches/advanced_printing_branch/src/app/composer/qgscomposition.cpp	2008-05-24 19:53:45 UTC (rev 8513)
+++ branches/advanced_printing_branch/src/app/composer/qgscomposition.cpp	2008-05-25 11:38:52 UTC (rev 8514)
@@ -186,6 +186,7 @@
   mPaperItem->setBrush( QColor(255,255,255) );
   mPaperItem->setPen( QPen(QColor(0,0,0), 0) ); // 0 line width makes it use a cosmetic pen - 1px, regardless of scale.
   mPaperItem->setZValue(0);
+  mPaperItem->setAcceptedMouseButtons(0);
   mPaperItem->show();
 }
 
@@ -397,8 +398,8 @@
   switch ( mTool ) {
     case Select:
       if ( mSelectedItem ) {
-        mSelectedItem->setPos(p - mGrabPoint);
-        mCanvas->update();
+        //mSelectedItem->setPos(p - mGrabPoint);
+        //mCanvas->update();
       }
       break;
 



More information about the QGIS-commit mailing list