[QGIS Commit] r9146 - in trunk/qgis/src: app app/composer gui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sun Aug 24 06:17:09 EDT 2008


Author: mhugent
Date: 2008-08-24 06:17:08 -0400 (Sun, 24 Aug 2008)
New Revision: 9146

Added:
   trunk/qgis/src/gui/qgscomposerview.cpp
   trunk/qgis/src/gui/qgscomposerview.h
Removed:
   trunk/qgis/src/app/composer/qgscomposerview.cpp
   trunk/qgis/src/app/composer/qgscomposerview.h
Modified:
   trunk/qgis/src/app/CMakeLists.txt
   trunk/qgis/src/gui/CMakeLists.txt
Log:
Moved composer view to gui for potential reuse by plugins

Modified: trunk/qgis/src/app/CMakeLists.txt
===================================================================
--- trunk/qgis/src/app/CMakeLists.txt	2008-08-24 09:06:14 UTC (rev 9145)
+++ trunk/qgis/src/app/CMakeLists.txt	2008-08-24 10:17:08 UTC (rev 9146)
@@ -63,7 +63,6 @@
   composer/qgscomposerscalebarwidget.cpp
   composer/qgscomposerlegenditemdialog.cpp
   composer/qgscomposerlegendwidget.cpp
-  composer/qgscomposerview.cpp
   composer/qgscompositionwidget.cpp
 
   legend/qgslegendgroup.cpp
@@ -127,7 +126,6 @@
   composer/qgscomposermapwidget.h
   composer/qgscomposerpicturewidget.h
   composer/qgscomposerscalebarwidget.h
-  composer/qgscomposerview.h
   composer/qgscompositionwidget.h
 
   legend/qgslegend.h

Deleted: trunk/qgis/src/app/composer/qgscomposerview.cpp
===================================================================
--- trunk/qgis/src/app/composer/qgscomposerview.cpp	2008-08-24 09:06:14 UTC (rev 9145)
+++ trunk/qgis/src/app/composer/qgscomposerview.cpp	2008-08-24 10:17:08 UTC (rev 9146)
@@ -1,419 +0,0 @@
-/***************************************************************************
-                         qgscomposerview.cpp
-                         -------------------
-    begin                : January 2005
-    copyright            : (C) 2005 by Radim Blazek
-    email                : blazek at itc.it
- ***************************************************************************/
-
-/***************************************************************************
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- ***************************************************************************/
-
-#include <QMouseEvent>
-#include <QKeyEvent>
-
-#include "qgscomposerview.h"
-#include "qgscomposerlabel.h"
-#include "qgscomposerlegend.h"
-#include "qgscomposermap.h"
-#include "qgscomposeritemgroup.h"
-#include "qgscomposerpicture.h"
-#include "qgscomposerscalebar.h"
-
-QgsComposerView::QgsComposerView( QWidget* parent, const char* name, Qt::WFlags f ) :
-    QGraphicsView( parent ), mShiftKeyPressed( false ), mRubberBandItem( 0 ), mMoveContentItem( 0 )
-{
-  setResizeAnchor( QGraphicsView::AnchorViewCenter );
-  setMouseTracking( true );
-  viewport()->setMouseTracking( true );
-}
-
-void QgsComposerView::mousePressEvent( QMouseEvent* e )
-{
-  if ( !composition() )
-  {
-    return;
-  }
-
-  QPointF scenePoint = mapToScene( e->pos() );
-
-  switch ( mCurrentTool )
-  {
-      //select/deselect items and pass mouse event further
-    case Select:
-    {
-      if ( !mShiftKeyPressed ) //keep selection if shift key pressed
-      {
-        composition()->clearSelection();
-      }
-
-      //select topmost item at position of event
-      QgsComposerItem* selectedItem = composition()->composerItemAt( scenePoint );
-      if ( selectedItem )
-      {
-        selectedItem->setSelected( true );
-      }
-
-
-      QGraphicsView::mousePressEvent( e );
-      emit selectedItemChanged( selectedItem );
-      break;
-    }
-
-    case MoveItemContent:
-    {
-      //store item as member if it is selected and cursor is over item
-      QgsComposerItem* item = dynamic_cast<QgsComposerItem*>( itemAt( e->pos() ) );
-      if ( item )
-      {
-        mMoveContentStartPos = scenePoint;
-      }
-      mMoveContentItem = item;
-      break;
-    }
-
-    //create rubber band
-    case AddMap:
-    {
-      QTransform t;
-      mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
-      t.translate( scenePoint.x(), scenePoint.y() );
-      mRubberBandItem->setTransform( t );
-      mRubberBandItem->setZValue( 100 );
-
-      scene()->addItem( mRubberBandItem );
-      scene()->update();
-    }
-    break;
-
-    case AddLabel:
-    {
-      QgsComposerLabel* newLabelItem = new QgsComposerLabel( composition() );
-      newLabelItem->setText( "Quantum GIS" );
-      newLabelItem->adjustSizeToText();
-      scene()->addItem( newLabelItem );
-      emit composerLabelAdded( newLabelItem );
-      scene()->clearSelection();
-      newLabelItem->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), newLabelItem->rect().width(), newLabelItem->rect().height() ) );
-      newLabelItem->setZValue( 60 );
-      newLabelItem->setSelected( true );
-      emit selectedItemChanged( newLabelItem );
-      emit actionFinished();
-    }
-    break;
-
-    case AddScalebar:
-    {
-      QgsComposerScaleBar* newScaleBar = new QgsComposerScaleBar( composition() );
-
-      //take first available map...
-      QList<const QgsComposerMap*> mapItemList = composition()->composerMapItems();
-      if ( mapItemList.size() > 0 )
-      {
-        newScaleBar->setComposerMap( mapItemList.at( 0 ) );
-      }
-
-      newScaleBar->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), 20, 20 ) );
-      newScaleBar->applyDefaultSettings(); //4 segments, 1/5 of composer map width
-      scene()->addItem( newScaleBar );
-      emit composerScaleBarAdded( newScaleBar );
-      scene()->clearSelection();
-      newScaleBar->setZValue( 60 );
-      newScaleBar->setSelected( true );
-      emit selectedItemChanged( newScaleBar );
-      emit actionFinished();
-    }
-    break;
-
-    case AddLegend:
-    {
-      QgsComposerLegend* newLegend = new QgsComposerLegend( composition() );
-      scene()->addItem( newLegend );
-      emit composerLegendAdded( newLegend );
-      scene()->clearSelection();
-      newLegend->setZValue( 60 );
-      newLegend->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), newLegend->rect().width(), newLegend->rect().height() ) );
-      newLegend->setSelected( true );
-      emit selectedItemChanged( newLegend );
-      emit actionFinished();
-      break;
-    }
-    case AddPicture:
-    {
-      QgsComposerPicture* newPicture = new QgsComposerPicture( composition() );
-      scene()->addItem( newPicture );
-      emit composerPictureAdded( newPicture );
-      scene()->clearSelection();
-      newPicture->setZValue( 60 );
-      newPicture->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), 30, 30 ) );
-      newPicture->setSelected( true );
-      emit selectedItemChanged( newPicture );
-      emit actionFinished();
-    }
-
-    default:
-      break;
-  }
-}
-
-void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
-{
-  if ( !composition() )
-  {
-    return;
-  }
-
-  QPointF scenePoint = mapToScene( e->pos() );
-
-  switch ( mCurrentTool )
-  {
-    case Select:
-    {
-      QGraphicsView::mouseReleaseEvent( e );
-      break;
-    }
-
-    case MoveItemContent:
-    {
-      if ( mMoveContentItem )
-      {
-        //update map preview if composer map
-        QgsComposerMap* composerMap = dynamic_cast<QgsComposerMap*>( mMoveContentItem );
-        if ( composerMap )
-        {
-          composerMap->setOffset( 0, 0 );
-        }
-
-        double moveX = scenePoint.x() - mMoveContentStartPos.x();
-        double moveY = scenePoint.y() - mMoveContentStartPos.y();
-        mMoveContentItem->moveContent( -moveX, -moveY );
-        mMoveContentItem->update();
-        mMoveContentItem = 0;
-      }
-      break;
-    }
-
-    case AddMap:
-    {
-      if ( !mRubberBandItem || mRubberBandItem->rect().width() < 0.1 || mRubberBandItem->rect().width() < 0.1 )
-      {
-        scene()->removeItem( mRubberBandItem );
-        delete mRubberBandItem;
-        return;
-      }
-
-      QgsComposerMap* composerMap = new QgsComposerMap( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() );
-      composerMap->setPreviewMode( QgsComposerMap::Rectangle );
-
-      emit composerMapAdded( composerMap );
-
-      composerMap->setZValue( 50 );
-      scene()->addItem( composerMap );
-      scene()->clearSelection();
-
-      scene()->removeItem( mRubberBandItem );
-      delete mRubberBandItem;
-
-      composerMap->setSelected( true );
-      emit selectedItemChanged( composerMap );
-      emit actionFinished();
-    }
-    break;
-
-    default:
-      break;
-  }
-}
-
-void QgsComposerView::mouseMoveEvent( QMouseEvent* e )
-{
-  if ( !composition() )
-  {
-    return;
-  }
-
-  if ( e->buttons() == Qt::NoButton )
-  {
-    if ( mCurrentTool == Select )
-    {
-      QGraphicsView::mouseMoveEvent( e );
-    }
-  }
-  else
-  {
-    QPointF scenePoint = mapToScene( e->pos() );
-    double newWidth, newHeight; //for rubber band
-
-    switch ( mCurrentTool )
-    {
-      case Select:
-        QGraphicsView::mouseMoveEvent( e );
-        break;
-
-      case AddMap:
-        //adjust rubber band item
-        newWidth = scenePoint.x() - mRubberBandItem->transform().dx();
-        newHeight = scenePoint.y() - mRubberBandItem->transform().dy();
-        mRubberBandItem->setRect( 0, 0, newWidth, newHeight );
-        break;
-
-      case MoveItemContent:
-      {
-        //update map preview if composer map
-        QgsComposerMap* composerMap = dynamic_cast<QgsComposerMap*>( mMoveContentItem );
-        if ( composerMap )
-        {
-          composerMap->setOffset( scenePoint.x() - mMoveContentStartPos.x(), scenePoint.y() - mMoveContentStartPos.y() );
-          composerMap->update();
-        }
-        break;
-      }
-      default:
-        break;
-    }
-  }
-}
-
-void QgsComposerView::keyPressEvent( QKeyEvent * e )
-{
-  if ( e->key() == Qt::Key_Shift )
-  {
-    mShiftKeyPressed = true;
-  }
-
-  if ( !composition() )
-  {
-    return;
-  }
-
-  QList<QgsComposerItem*> composerItemList = composition()->selectedComposerItems();
-  QList<QgsComposerItem*>::iterator itemIt = composerItemList.begin();
-
-  //delete selected items
-  if ( e->key() == Qt::Key_Delete )
-  {
-    for ( ; itemIt != composerItemList.end(); ++itemIt )
-    {
-      composition()->removeItem( *itemIt );
-      delete( *itemIt );
-      emit itemRemoved( *itemIt );
-    }
-  }
-
-  else if ( e->key() == Qt::Key_Left )
-  {
-    for ( ; itemIt != composerItemList.end(); ++itemIt )
-    {
-      ( *itemIt )->move( -1.0, 0.0 );
-    }
-  }
-  else if ( e->key() == Qt::Key_Right )
-  {
-    for ( ; itemIt != composerItemList.end(); ++itemIt )
-    {
-      ( *itemIt )->move( 1.0, 0.0 );
-    }
-  }
-  else if ( e->key() == Qt::Key_Down )
-  {
-    for ( ; itemIt != composerItemList.end(); ++itemIt )
-    {
-      ( *itemIt )->move( 0.0, 1.0 );
-    }
-  }
-  else if ( e->key() == Qt::Key_Up )
-  {
-    for ( ; itemIt != composerItemList.end(); ++itemIt )
-    {
-      ( *itemIt )->move( 0.0, -1.0 );
-    }
-  }
-}
-
-void QgsComposerView::keyReleaseEvent( QKeyEvent * e )
-{
-  if ( e->key() == Qt::Key_Shift )
-  {
-    mShiftKeyPressed = false;
-  }
-}
-
-void QgsComposerView::setComposition( QgsComposition* c )
-{
-  setScene( c );
-}
-
-QgsComposition* QgsComposerView::composition()
-{
-  if ( scene() )
-  {
-    QgsComposition* c = dynamic_cast<QgsComposition*>( scene() );
-    if ( c )
-    {
-      return c;
-    }
-  }
-  return 0;
-}
-
-void QgsComposerView::groupItems()
-{
-  if ( !composition() )
-  {
-    return;
-  }
-
-  QList<QgsComposerItem*> selectionList = composition()->selectedComposerItems();
-  if ( selectionList.size() < 2 )
-  {
-    return; //not enough items for a group
-  }
-
-  QgsComposerItemGroup* itemGroup = new QgsComposerItemGroup( composition() );
-  //connect signal/slot to let item group tell if child items get removed
-  connect( itemGroup, SIGNAL( childItemDeleted( QgsComposerItem* ) ), this, SLOT( sendItemRemovedSignal( QgsComposerItem* ) ) );
-
-  QList<QgsComposerItem*>::iterator itemIter = selectionList.begin();
-  for ( ; itemIter != selectionList.end(); ++itemIter )
-  {
-    itemGroup->addItem( *itemIter );
-  }
-  composition()->addItem( itemGroup );
-  itemGroup->setSelected( true );
-  emit selectedItemChanged( itemGroup );
-}
-
-void QgsComposerView::ungroupItems()
-{
-  if ( !composition() )
-  {
-    return;
-  }
-
-  QList<QgsComposerItem*> selectionList = composition()->selectedComposerItems();
-  QList<QgsComposerItem*>::iterator itemIter = selectionList.begin();
-  for ( ; itemIter != selectionList.end(); ++itemIter )
-  {
-    QgsComposerItemGroup* itemGroup = dynamic_cast<QgsComposerItemGroup*>( *itemIter );
-    if ( itemGroup )
-    {
-      itemGroup->removeItems();
-      composition()->removeItem( *itemIter );
-      delete( *itemIter );
-      emit itemRemoved( *itemIter );
-    }
-  }
-
-}
-
-void QgsComposerView::sendItemRemovedSignal( QgsComposerItem* item )
-{
-  emit itemRemoved( item );
-}
-

Deleted: trunk/qgis/src/app/composer/qgscomposerview.h
===================================================================
--- trunk/qgis/src/app/composer/qgscomposerview.h	2008-08-24 09:06:14 UTC (rev 9145)
+++ trunk/qgis/src/app/composer/qgscomposerview.h	2008-08-24 10:17:08 UTC (rev 9146)
@@ -1,117 +0,0 @@
-/***************************************************************************
-                         qgscomposerview.h
-                             -------------------
-    begin                : January 2005
-    copyright            : (C) 2005 by Radim Blazek
-    email                : blazek at itc.it
- ***************************************************************************/
-
-/***************************************************************************
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- ***************************************************************************/
-/* $Id$ */
-#ifndef QGSCOMPOSERVIEW_H
-#define QGSCOMPOSERVIEW_H
-
-#include <QGraphicsView>
-
-class QKeyEvent;
-class QMouseEvent;
-class QgsComposition;
-class QgsComposerItem;
-class QgsComposerLabel;
-class QgsComposerLegend;
-class QgsComposerMap;
-class QgsComposerPicture;
-class QgsComposerScaleBar;
-
-/** \ingroup MapComposer
- * Widget to display the composer items. Manages the composer tools and the
- * mouse/key events.
- * Creates the composer items according to the current map tools and keeps track
- * of the rubber band item.
- */
-class QgsComposerView: public QGraphicsView
-{
-    Q_OBJECT
-
-  public:
-
-    /**Current tool*/
-    enum Tool
-    {
-      Select = 0,      // Select/Move item
-      AddMap,          // add new map
-      AddLegend, // add vector legend
-      AddLabel,        // add label
-      AddScalebar,     // add scalebar
-      AddPicture,       // add raster/vector picture
-      MoveItemContent //move content of item (e.g. content of map)
-    };
-
-    QgsComposerView( QWidget* parent = 0, const char* name = 0, Qt::WFlags f = 0 );
-
-    /**Add an item group containing the selected items*/
-    void groupItems();
-
-    /**Ungroups the selected items*/
-    void ungroupItems();
-
-    QgsComposerView::Tool currentTool() const {return mCurrentTool;}
-    void setCurrentTool( QgsComposerView::Tool t ) {mCurrentTool = t;}
-
-    /**Sets composition (derived from QGraphicsScene)*/
-    void setComposition( QgsComposition* c );
-    /**Returns the composition or 0 in case of error*/
-    QgsComposition* composition();
-
-  protected:
-    void mousePressEvent( QMouseEvent* );
-    void mouseReleaseEvent( QMouseEvent* );
-    void mouseMoveEvent( QMouseEvent* );
-
-    void keyPressEvent( QKeyEvent * e );
-    void keyReleaseEvent( QKeyEvent * e );
-
-  private:
-    /**Status of shift key (used for multiple selection)*/
-    bool mShiftKeyPressed;
-    /**Current composer tool*/
-    QgsComposerView::Tool mCurrentTool;
-    /**Rubber band item*/
-    QGraphicsRectItem* mRubberBandItem;
-    /**Item to move content*/
-    QgsComposerItem* mMoveContentItem;
-    /**Start position of content move*/
-    QPointF mMoveContentStartPos;
-
-  public slots:
-    /**For QgsComposerItemGroup to send its signals to QgsComposer (or other classes that keep track of input widgets)*/
-    void sendItemRemovedSignal( QgsComposerItem* item );
-
-  signals:
-    /**Is emitted when selected item changed. If 0, no item is selected*/
-    void selectedItemChanged( const QgsComposerItem* selected );
-    /**Ist emittted when new composer label has been added to the view*/
-    void composerLabelAdded( QgsComposerLabel* label );
-    /**Is emitted when new composer map has been added to the view*/
-    void composerMapAdded( QgsComposerMap* map );
-    /**Is emitted when new composer scale bar has been added*/
-    void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
-    /**Is emitted when a new composer legend has been added*/
-    void composerLegendAdded( QgsComposerLegend* legend );
-    /**Is emitted when a new composer picture has been added*/
-    void composerPictureAdded( QgsComposerPicture* picture );
-    /**Is emitted when a composer item has been removed from the scene*/
-    void itemRemoved( QgsComposerItem* );
-    /**Current action (e.g. adding composer map) has been finished. The purpose of this signal is that
-     QgsComposer may set the selection tool again*/
-    void actionFinished();
-};
-
-#endif

Modified: trunk/qgis/src/gui/CMakeLists.txt
===================================================================
--- trunk/qgis/src/gui/CMakeLists.txt	2008-08-24 09:06:14 UTC (rev 9145)
+++ trunk/qgis/src/gui/CMakeLists.txt	2008-08-24 10:17:08 UTC (rev 9146)
@@ -3,6 +3,7 @@
 qgisgui.cpp
 qgisinterface.cpp
 qgscolorbutton.cpp
+qgscomposerview.cpp
 qgscursors.cpp
 qgsdetaileditemdelegate.cpp
 qgsdetaileditemwidget.cpp
@@ -28,6 +29,7 @@
 )
 
 SET(QGIS_GUI_MOC_HDRS
+qgscomposerview.h
 qgsdetaileditemdelegate.h
 qgsdetaileditemwidget.h
 qgisinterface.h
@@ -44,7 +46,7 @@
 QT4_WRAP_CPP(QGIS_GUI_MOC_SRCS ${QGIS_GUI_MOC_HDRS})
 
 INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ../core
-     ../core/raster ../core/renderer ../core/symbology
+     ../core/composer ../core/raster ../core/renderer ../core/symbology
      ${CMAKE_CURRENT_BINARY_DIR}
      ${CMAKE_CURRENT_BINARY_DIR}/../ui
      ${GEOS_INCLUDE_DIR}

Copied: trunk/qgis/src/gui/qgscomposerview.cpp (from rev 9141, trunk/qgis/src/app/composer/qgscomposerview.cpp)
===================================================================
--- trunk/qgis/src/gui/qgscomposerview.cpp	                        (rev 0)
+++ trunk/qgis/src/gui/qgscomposerview.cpp	2008-08-24 10:17:08 UTC (rev 9146)
@@ -0,0 +1,419 @@
+/***************************************************************************
+                         qgscomposerview.cpp
+                         -------------------
+    begin                : January 2005
+    copyright            : (C) 2005 by Radim Blazek
+    email                : blazek at itc.it
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include <QMouseEvent>
+#include <QKeyEvent>
+
+#include "qgscomposerview.h"
+#include "qgscomposerlabel.h"
+#include "qgscomposerlegend.h"
+#include "qgscomposermap.h"
+#include "qgscomposeritemgroup.h"
+#include "qgscomposerpicture.h"
+#include "qgscomposerscalebar.h"
+
+QgsComposerView::QgsComposerView( QWidget* parent, const char* name, Qt::WFlags f ) :
+    QGraphicsView( parent ), mShiftKeyPressed( false ), mRubberBandItem( 0 ), mMoveContentItem( 0 )
+{
+  setResizeAnchor( QGraphicsView::AnchorViewCenter );
+  setMouseTracking( true );
+  viewport()->setMouseTracking( true );
+}
+
+void QgsComposerView::mousePressEvent( QMouseEvent* e )
+{
+  if ( !composition() )
+  {
+    return;
+  }
+
+  QPointF scenePoint = mapToScene( e->pos() );
+
+  switch ( mCurrentTool )
+  {
+      //select/deselect items and pass mouse event further
+    case Select:
+    {
+      if ( !mShiftKeyPressed ) //keep selection if shift key pressed
+      {
+        composition()->clearSelection();
+      }
+
+      //select topmost item at position of event
+      QgsComposerItem* selectedItem = composition()->composerItemAt( scenePoint );
+      if ( selectedItem )
+      {
+        selectedItem->setSelected( true );
+      }
+
+
+      QGraphicsView::mousePressEvent( e );
+      emit selectedItemChanged( selectedItem );
+      break;
+    }
+
+    case MoveItemContent:
+    {
+      //store item as member if it is selected and cursor is over item
+      QgsComposerItem* item = dynamic_cast<QgsComposerItem*>( itemAt( e->pos() ) );
+      if ( item )
+      {
+        mMoveContentStartPos = scenePoint;
+      }
+      mMoveContentItem = item;
+      break;
+    }
+
+    //create rubber band
+    case AddMap:
+    {
+      QTransform t;
+      mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
+      t.translate( scenePoint.x(), scenePoint.y() );
+      mRubberBandItem->setTransform( t );
+      mRubberBandItem->setZValue( 100 );
+
+      scene()->addItem( mRubberBandItem );
+      scene()->update();
+    }
+    break;
+
+    case AddLabel:
+    {
+      QgsComposerLabel* newLabelItem = new QgsComposerLabel( composition() );
+      newLabelItem->setText( "Quantum GIS" );
+      newLabelItem->adjustSizeToText();
+      scene()->addItem( newLabelItem );
+      emit composerLabelAdded( newLabelItem );
+      scene()->clearSelection();
+      newLabelItem->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), newLabelItem->rect().width(), newLabelItem->rect().height() ) );
+      newLabelItem->setZValue( 60 );
+      newLabelItem->setSelected( true );
+      emit selectedItemChanged( newLabelItem );
+      emit actionFinished();
+    }
+    break;
+
+    case AddScalebar:
+    {
+      QgsComposerScaleBar* newScaleBar = new QgsComposerScaleBar( composition() );
+
+      //take first available map...
+      QList<const QgsComposerMap*> mapItemList = composition()->composerMapItems();
+      if ( mapItemList.size() > 0 )
+      {
+        newScaleBar->setComposerMap( mapItemList.at( 0 ) );
+      }
+
+      newScaleBar->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), 20, 20 ) );
+      newScaleBar->applyDefaultSettings(); //4 segments, 1/5 of composer map width
+      scene()->addItem( newScaleBar );
+      emit composerScaleBarAdded( newScaleBar );
+      scene()->clearSelection();
+      newScaleBar->setZValue( 60 );
+      newScaleBar->setSelected( true );
+      emit selectedItemChanged( newScaleBar );
+      emit actionFinished();
+    }
+    break;
+
+    case AddLegend:
+    {
+      QgsComposerLegend* newLegend = new QgsComposerLegend( composition() );
+      scene()->addItem( newLegend );
+      emit composerLegendAdded( newLegend );
+      scene()->clearSelection();
+      newLegend->setZValue( 60 );
+      newLegend->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), newLegend->rect().width(), newLegend->rect().height() ) );
+      newLegend->setSelected( true );
+      emit selectedItemChanged( newLegend );
+      emit actionFinished();
+      break;
+    }
+    case AddPicture:
+    {
+      QgsComposerPicture* newPicture = new QgsComposerPicture( composition() );
+      scene()->addItem( newPicture );
+      emit composerPictureAdded( newPicture );
+      scene()->clearSelection();
+      newPicture->setZValue( 60 );
+      newPicture->setSceneRect( QRectF( scenePoint.x(), scenePoint.y(), 30, 30 ) );
+      newPicture->setSelected( true );
+      emit selectedItemChanged( newPicture );
+      emit actionFinished();
+    }
+
+    default:
+      break;
+  }
+}
+
+void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
+{
+  if ( !composition() )
+  {
+    return;
+  }
+
+  QPointF scenePoint = mapToScene( e->pos() );
+
+  switch ( mCurrentTool )
+  {
+    case Select:
+    {
+      QGraphicsView::mouseReleaseEvent( e );
+      break;
+    }
+
+    case MoveItemContent:
+    {
+      if ( mMoveContentItem )
+      {
+        //update map preview if composer map
+        QgsComposerMap* composerMap = dynamic_cast<QgsComposerMap*>( mMoveContentItem );
+        if ( composerMap )
+        {
+          composerMap->setOffset( 0, 0 );
+        }
+
+        double moveX = scenePoint.x() - mMoveContentStartPos.x();
+        double moveY = scenePoint.y() - mMoveContentStartPos.y();
+        mMoveContentItem->moveContent( -moveX, -moveY );
+        mMoveContentItem->update();
+        mMoveContentItem = 0;
+      }
+      break;
+    }
+
+    case AddMap:
+    {
+      if ( !mRubberBandItem || mRubberBandItem->rect().width() < 0.1 || mRubberBandItem->rect().width() < 0.1 )
+      {
+        scene()->removeItem( mRubberBandItem );
+        delete mRubberBandItem;
+        return;
+      }
+
+      QgsComposerMap* composerMap = new QgsComposerMap( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() );
+      composerMap->setPreviewMode( QgsComposerMap::Rectangle );
+
+      emit composerMapAdded( composerMap );
+
+      composerMap->setZValue( 50 );
+      scene()->addItem( composerMap );
+      scene()->clearSelection();
+
+      scene()->removeItem( mRubberBandItem );
+      delete mRubberBandItem;
+
+      composerMap->setSelected( true );
+      emit selectedItemChanged( composerMap );
+      emit actionFinished();
+    }
+    break;
+
+    default:
+      break;
+  }
+}
+
+void QgsComposerView::mouseMoveEvent( QMouseEvent* e )
+{
+  if ( !composition() )
+  {
+    return;
+  }
+
+  if ( e->buttons() == Qt::NoButton )
+  {
+    if ( mCurrentTool == Select )
+    {
+      QGraphicsView::mouseMoveEvent( e );
+    }
+  }
+  else
+  {
+    QPointF scenePoint = mapToScene( e->pos() );
+    double newWidth, newHeight; //for rubber band
+
+    switch ( mCurrentTool )
+    {
+      case Select:
+        QGraphicsView::mouseMoveEvent( e );
+        break;
+
+      case AddMap:
+        //adjust rubber band item
+        newWidth = scenePoint.x() - mRubberBandItem->transform().dx();
+        newHeight = scenePoint.y() - mRubberBandItem->transform().dy();
+        mRubberBandItem->setRect( 0, 0, newWidth, newHeight );
+        break;
+
+      case MoveItemContent:
+      {
+        //update map preview if composer map
+        QgsComposerMap* composerMap = dynamic_cast<QgsComposerMap*>( mMoveContentItem );
+        if ( composerMap )
+        {
+          composerMap->setOffset( scenePoint.x() - mMoveContentStartPos.x(), scenePoint.y() - mMoveContentStartPos.y() );
+          composerMap->update();
+        }
+        break;
+      }
+      default:
+        break;
+    }
+  }
+}
+
+void QgsComposerView::keyPressEvent( QKeyEvent * e )
+{
+  if ( e->key() == Qt::Key_Shift )
+  {
+    mShiftKeyPressed = true;
+  }
+
+  if ( !composition() )
+  {
+    return;
+  }
+
+  QList<QgsComposerItem*> composerItemList = composition()->selectedComposerItems();
+  QList<QgsComposerItem*>::iterator itemIt = composerItemList.begin();
+
+  //delete selected items
+  if ( e->key() == Qt::Key_Delete )
+  {
+    for ( ; itemIt != composerItemList.end(); ++itemIt )
+    {
+      composition()->removeItem( *itemIt );
+      delete( *itemIt );
+      emit itemRemoved( *itemIt );
+    }
+  }
+
+  else if ( e->key() == Qt::Key_Left )
+  {
+    for ( ; itemIt != composerItemList.end(); ++itemIt )
+    {
+      ( *itemIt )->move( -1.0, 0.0 );
+    }
+  }
+  else if ( e->key() == Qt::Key_Right )
+  {
+    for ( ; itemIt != composerItemList.end(); ++itemIt )
+    {
+      ( *itemIt )->move( 1.0, 0.0 );
+    }
+  }
+  else if ( e->key() == Qt::Key_Down )
+  {
+    for ( ; itemIt != composerItemList.end(); ++itemIt )
+    {
+      ( *itemIt )->move( 0.0, 1.0 );
+    }
+  }
+  else if ( e->key() == Qt::Key_Up )
+  {
+    for ( ; itemIt != composerItemList.end(); ++itemIt )
+    {
+      ( *itemIt )->move( 0.0, -1.0 );
+    }
+  }
+}
+
+void QgsComposerView::keyReleaseEvent( QKeyEvent * e )
+{
+  if ( e->key() == Qt::Key_Shift )
+  {
+    mShiftKeyPressed = false;
+  }
+}
+
+void QgsComposerView::setComposition( QgsComposition* c )
+{
+  setScene( c );
+}
+
+QgsComposition* QgsComposerView::composition()
+{
+  if ( scene() )
+  {
+    QgsComposition* c = dynamic_cast<QgsComposition*>( scene() );
+    if ( c )
+    {
+      return c;
+    }
+  }
+  return 0;
+}
+
+void QgsComposerView::groupItems()
+{
+  if ( !composition() )
+  {
+    return;
+  }
+
+  QList<QgsComposerItem*> selectionList = composition()->selectedComposerItems();
+  if ( selectionList.size() < 2 )
+  {
+    return; //not enough items for a group
+  }
+
+  QgsComposerItemGroup* itemGroup = new QgsComposerItemGroup( composition() );
+  //connect signal/slot to let item group tell if child items get removed
+  connect( itemGroup, SIGNAL( childItemDeleted( QgsComposerItem* ) ), this, SLOT( sendItemRemovedSignal( QgsComposerItem* ) ) );
+
+  QList<QgsComposerItem*>::iterator itemIter = selectionList.begin();
+  for ( ; itemIter != selectionList.end(); ++itemIter )
+  {
+    itemGroup->addItem( *itemIter );
+  }
+  composition()->addItem( itemGroup );
+  itemGroup->setSelected( true );
+  emit selectedItemChanged( itemGroup );
+}
+
+void QgsComposerView::ungroupItems()
+{
+  if ( !composition() )
+  {
+    return;
+  }
+
+  QList<QgsComposerItem*> selectionList = composition()->selectedComposerItems();
+  QList<QgsComposerItem*>::iterator itemIter = selectionList.begin();
+  for ( ; itemIter != selectionList.end(); ++itemIter )
+  {
+    QgsComposerItemGroup* itemGroup = dynamic_cast<QgsComposerItemGroup*>( *itemIter );
+    if ( itemGroup )
+    {
+      itemGroup->removeItems();
+      composition()->removeItem( *itemIter );
+      delete( *itemIter );
+      emit itemRemoved( *itemIter );
+    }
+  }
+
+}
+
+void QgsComposerView::sendItemRemovedSignal( QgsComposerItem* item )
+{
+  emit itemRemoved( item );
+}
+

Copied: trunk/qgis/src/gui/qgscomposerview.h (from rev 9141, trunk/qgis/src/app/composer/qgscomposerview.h)
===================================================================
--- trunk/qgis/src/gui/qgscomposerview.h	                        (rev 0)
+++ trunk/qgis/src/gui/qgscomposerview.h	2008-08-24 10:17:08 UTC (rev 9146)
@@ -0,0 +1,117 @@
+/***************************************************************************
+                         qgscomposerview.h
+                             -------------------
+    begin                : January 2005
+    copyright            : (C) 2005 by Radim Blazek
+    email                : blazek at itc.it
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+/* $Id$ */
+#ifndef QGSCOMPOSERVIEW_H
+#define QGSCOMPOSERVIEW_H
+
+#include <QGraphicsView>
+
+class QKeyEvent;
+class QMouseEvent;
+class QgsComposition;
+class QgsComposerItem;
+class QgsComposerLabel;
+class QgsComposerLegend;
+class QgsComposerMap;
+class QgsComposerPicture;
+class QgsComposerScaleBar;
+
+/** \ingroup MapComposer
+ * Widget to display the composer items. Manages the composer tools and the
+ * mouse/key events.
+ * Creates the composer items according to the current map tools and keeps track
+ * of the rubber band item.
+ */
+class QgsComposerView: public QGraphicsView
+{
+    Q_OBJECT
+
+  public:
+
+    /**Current tool*/
+    enum Tool
+    {
+      Select = 0,      // Select/Move item
+      AddMap,          // add new map
+      AddLegend, // add vector legend
+      AddLabel,        // add label
+      AddScalebar,     // add scalebar
+      AddPicture,       // add raster/vector picture
+      MoveItemContent //move content of item (e.g. content of map)
+    };
+
+    QgsComposerView( QWidget* parent = 0, const char* name = 0, Qt::WFlags f = 0 );
+
+    /**Add an item group containing the selected items*/
+    void groupItems();
+
+    /**Ungroups the selected items*/
+    void ungroupItems();
+
+    QgsComposerView::Tool currentTool() const {return mCurrentTool;}
+    void setCurrentTool( QgsComposerView::Tool t ) {mCurrentTool = t;}
+
+    /**Sets composition (derived from QGraphicsScene)*/
+    void setComposition( QgsComposition* c );
+    /**Returns the composition or 0 in case of error*/
+    QgsComposition* composition();
+
+  protected:
+    void mousePressEvent( QMouseEvent* );
+    void mouseReleaseEvent( QMouseEvent* );
+    void mouseMoveEvent( QMouseEvent* );
+
+    void keyPressEvent( QKeyEvent * e );
+    void keyReleaseEvent( QKeyEvent * e );
+
+  private:
+    /**Status of shift key (used for multiple selection)*/
+    bool mShiftKeyPressed;
+    /**Current composer tool*/
+    QgsComposerView::Tool mCurrentTool;
+    /**Rubber band item*/
+    QGraphicsRectItem* mRubberBandItem;
+    /**Item to move content*/
+    QgsComposerItem* mMoveContentItem;
+    /**Start position of content move*/
+    QPointF mMoveContentStartPos;
+
+  public slots:
+    /**For QgsComposerItemGroup to send its signals to QgsComposer (or other classes that keep track of input widgets)*/
+    void sendItemRemovedSignal( QgsComposerItem* item );
+
+  signals:
+    /**Is emitted when selected item changed. If 0, no item is selected*/
+    void selectedItemChanged( const QgsComposerItem* selected );
+    /**Ist emittted when new composer label has been added to the view*/
+    void composerLabelAdded( QgsComposerLabel* label );
+    /**Is emitted when new composer map has been added to the view*/
+    void composerMapAdded( QgsComposerMap* map );
+    /**Is emitted when new composer scale bar has been added*/
+    void composerScaleBarAdded( QgsComposerScaleBar* scalebar );
+    /**Is emitted when a new composer legend has been added*/
+    void composerLegendAdded( QgsComposerLegend* legend );
+    /**Is emitted when a new composer picture has been added*/
+    void composerPictureAdded( QgsComposerPicture* picture );
+    /**Is emitted when a composer item has been removed from the scene*/
+    void itemRemoved( QgsComposerItem* );
+    /**Current action (e.g. adding composer map) has been finished. The purpose of this signal is that
+     QgsComposer may set the selection tool again*/
+    void actionFinished();
+};
+
+#endif



More information about the QGIS-commit mailing list