[QGIS Commit] r9361 - in branches/advanced_printing_branch2: images/themes/default src/app/composer src/core/composer

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat Sep 20 01:40:30 EDT 2008


Author: mhugent
Date: 2008-09-20 01:40:30 -0400 (Sat, 20 Sep 2008)
New Revision: 9361

Added:
   branches/advanced_printing_branch2/images/themes/default/mActionAlignBottom.png
   branches/advanced_printing_branch2/images/themes/default/mActionAlignHCenter.png
   branches/advanced_printing_branch2/images/themes/default/mActionAlignLeft.png
   branches/advanced_printing_branch2/images/themes/default/mActionAlignRight.png
   branches/advanced_printing_branch2/images/themes/default/mActionAlignTop.png
   branches/advanced_printing_branch2/images/themes/default/mActionAlignVCenter.png
Modified:
   branches/advanced_printing_branch2/src/app/composer/qgscomposer.cpp
   branches/advanced_printing_branch2/src/app/composer/qgscomposer.h
   branches/advanced_printing_branch2/src/core/composer/qgscomposition.cpp
   branches/advanced_printing_branch2/src/core/composer/qgscomposition.h
Log:
support for basic alignment functions in composer

Added: branches/advanced_printing_branch2/images/themes/default/mActionAlignBottom.png
===================================================================
(Binary files differ)


Property changes on: branches/advanced_printing_branch2/images/themes/default/mActionAlignBottom.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: branches/advanced_printing_branch2/images/themes/default/mActionAlignHCenter.png
===================================================================
(Binary files differ)


Property changes on: branches/advanced_printing_branch2/images/themes/default/mActionAlignHCenter.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: branches/advanced_printing_branch2/images/themes/default/mActionAlignLeft.png
===================================================================
(Binary files differ)


Property changes on: branches/advanced_printing_branch2/images/themes/default/mActionAlignLeft.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: branches/advanced_printing_branch2/images/themes/default/mActionAlignRight.png
===================================================================
(Binary files differ)


Property changes on: branches/advanced_printing_branch2/images/themes/default/mActionAlignRight.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: branches/advanced_printing_branch2/images/themes/default/mActionAlignTop.png
===================================================================
(Binary files differ)


Property changes on: branches/advanced_printing_branch2/images/themes/default/mActionAlignTop.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: branches/advanced_printing_branch2/images/themes/default/mActionAlignVCenter.png
===================================================================
(Binary files differ)


Property changes on: branches/advanced_printing_branch2/images/themes/default/mActionAlignVCenter.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Modified: branches/advanced_printing_branch2/src/app/composer/qgscomposer.cpp
===================================================================
--- branches/advanced_printing_branch2/src/app/composer/qgscomposer.cpp	2008-09-19 20:23:11 UTC (rev 9360)
+++ branches/advanced_printing_branch2/src/app/composer/qgscomposer.cpp	2008-09-20 05:40:30 UTC (rev 9361)
@@ -105,6 +105,24 @@
                                      tr( "Send to Back" ), this, SLOT( moveSelectedItemsToBottom() ) );
   moveItemsToBottomAction->setToolTip( tr( "Move selected items to bottom" ) );
 
+  QAction* alignLeftAction = toolBar->addAction( QIcon( QPixmap( myIconPath + "mActionAlignLeft.png")), tr("Align left"), this, SLOT(alignSelectedItemsLeft()));
+  alignLeftAction->setToolTip( tr("Align selected items left"));
+
+  QAction* alignHCenterAction = toolBar->addAction( QIcon( QPixmap( myIconPath + "mActionAlignHCenter.png")), tr("Align center horizontal"), this, SLOT(alignSelectedItemsHCenter()));
+  alignHCenterAction->setToolTip( tr("Align center horizontal"));
+
+  QAction* alignRightAction = toolBar->addAction( QIcon( QPixmap( myIconPath + "mActionAlignRight.png")), tr("Align right"), this, SLOT(alignSelectedItemsRight()));
+  alignRightAction->setToolTip( tr("Align selected items right"));
+
+  QAction* alignTopAction = toolBar->addAction( QIcon( QPixmap( myIconPath + "mActionAlignTop.png")), tr("Align top"), this, SLOT(alignSelectedItemsTop()));
+  alignTopAction->setToolTip( tr("Align selected items to top"));
+
+  QAction* alignVCenterAction = toolBar->addAction( QIcon( QPixmap( myIconPath + "mActionAlignVCenter.png")), tr("Align center vertical"), this, SLOT(alignSelectedItemsVCenter()));
+  alignVCenterAction->setToolTip( tr("Align center vertical"));
+
+  QAction* alignBottomAction = toolBar->addAction( QIcon( QPixmap( myIconPath + "mActionAlignBottom.png")), tr("Align bottom"), this, SLOT(alignSelectedItemsBottom()));
+  alignBottomAction->setToolTip( tr("Align selected items bottom"));
+
   QActionGroup* toggleActionGroup = new QActionGroup( this );
   toggleActionGroup->addAction( moveItemContentAction );
   toggleActionGroup->addAction( mActionAddNewMap );
@@ -805,6 +823,54 @@
   }
 }
 
+void QgsComposer::alignSelectedItemsLeft()
+{
+  if(mComposition)
+    {
+      mComposition->alignSelectedItemsLeft();
+    }
+}
+
+void QgsComposer::alignSelectedItemsHCenter()
+{
+  if(mComposition)
+    {
+      mComposition->alignSelectedItemsHCenter();
+    }
+}
+
+void QgsComposer::alignSelectedItemsRight()
+{
+  if(mComposition)
+    {
+      mComposition->alignSelectedItemsRight();
+    }
+}
+
+void QgsComposer::alignSelectedItemsTop()
+{
+  if(mComposition)
+    {
+      mComposition->alignSelectedItemsTop();
+    }
+}
+
+void QgsComposer::alignSelectedItemsVCenter()
+{
+  if(mComposition)
+    {
+      mComposition->alignSelectedItemsVCenter();
+    }
+}
+
+void QgsComposer::alignSelectedItemsBottom()
+{
+  if(mComposition)
+    {
+      mComposition->alignSelectedItemsBottom();
+    }
+}
+
 void QgsComposer::moveEvent( QMoveEvent *e ) { saveWindowState(); }
 
 void QgsComposer::resizeEvent( QResizeEvent *e )

Modified: branches/advanced_printing_branch2/src/app/composer/qgscomposer.h
===================================================================
--- branches/advanced_printing_branch2/src/app/composer/qgscomposer.h	2008-09-19 20:23:11 UTC (rev 9360)
+++ branches/advanced_printing_branch2/src/app/composer/qgscomposer.h	2008-09-20 05:40:30 UTC (rev 9361)
@@ -150,6 +150,24 @@
     //!Move selected items to bottom
     void moveSelectedItemsToBottom();
 
+    //!Align selected composer items left
+    void alignSelectedItemsLeft();
+
+    //!Align selected composere items horizontally centered
+    void alignSelectedItemsHCenter();
+
+    //!Align selected composer items right
+    void alignSelectedItemsRight();
+
+    //!Align selected composer items to top
+    void alignSelectedItemsTop();
+
+    //!Align selected composer items vertically centered
+    void alignSelectedItemsVCenter();
+
+    //!Align selected composer items to bottom
+    void alignSelectedItemsBottom();
+
     //! Save window state
     void saveWindowState();
 

Modified: branches/advanced_printing_branch2/src/core/composer/qgscomposition.cpp
===================================================================
--- branches/advanced_printing_branch2/src/core/composer/qgscomposition.cpp	2008-09-19 20:23:11 UTC (rev 9360)
+++ branches/advanced_printing_branch2/src/core/composer/qgscomposition.cpp	2008-09-20 05:40:30 UTC (rev 9361)
@@ -319,6 +319,156 @@
   mItemZList.push_front( item );
 }
 
+void QgsComposition::alignSelectedItemsLeft()
+{
+  QList<QgsComposerItem*> selectedItems = selectedComposerItems();
+  if(selectedItems.size() < 2)
+    {
+      return;
+    }
+
+  QRectF selectedItemBBox;
+  if(boundingRectOfSelectedItems(selectedItemBBox) != 0)
+    {
+      return;
+    }
+
+  double minXCoordinate = selectedItemBBox.left();
+
+  //align items left to minimum x coordinate
+  QList<QgsComposerItem*>::iterator align_it = selectedItems.begin();
+  for(; align_it != selectedItems.end(); ++align_it)
+    {
+      QTransform itemTransform = (*align_it)->transform();
+      itemTransform.translate( minXCoordinate - itemTransform.dx(), 0);
+      (*align_it)->setTransform(itemTransform);
+    }
+}
+
+void QgsComposition::alignSelectedItemsHCenter()
+{
+  QList<QgsComposerItem*> selectedItems = selectedComposerItems();
+  if(selectedItems.size() < 2)
+    {
+      return;
+    }
+
+  QRectF selectedItemBBox;
+  if(boundingRectOfSelectedItems(selectedItemBBox) != 0)
+    {
+      return;
+    }
+
+  double averageXCoord = (selectedItemBBox.left() + selectedItemBBox.right()) / 2.0;
+
+  //place items
+  QList<QgsComposerItem*>::iterator align_it = selectedItems.begin();
+  for(; align_it != selectedItems.end(); ++align_it)
+    {
+      QTransform itemTransform = (*align_it)->transform();
+      itemTransform.translate( averageXCoord - itemTransform.dx() - (*align_it)->rect().width() / 2.0, 0);
+      (*align_it)->setTransform(itemTransform);
+    }
+}
+
+void QgsComposition::alignSelectedItemsRight()
+{
+  QList<QgsComposerItem*> selectedItems = selectedComposerItems();
+  if(selectedItems.size() < 2)
+    {
+      return;
+    }
+
+  QRectF selectedItemBBox;
+  if(boundingRectOfSelectedItems(selectedItemBBox) != 0)
+    {
+      return;
+    }
+
+  double maxXCoordinate = selectedItemBBox.right();
+
+  //align items right to maximum x coordinate
+  QList<QgsComposerItem*>::iterator align_it = selectedItems.begin();
+  for(; align_it != selectedItems.end(); ++align_it)
+    {
+      QTransform itemTransform = (*align_it)->transform();
+      itemTransform.translate( maxXCoordinate - itemTransform.dx() - (*align_it)->rect().width(), 0);
+      (*align_it)->setTransform(itemTransform);
+    }
+}
+
+void QgsComposition::alignSelectedItemsTop()
+{
+  QList<QgsComposerItem*> selectedItems = selectedComposerItems();
+  if(selectedItems.size() < 2)
+    {
+      return;
+    }
+
+  QRectF selectedItemBBox;
+  if(boundingRectOfSelectedItems(selectedItemBBox) != 0)
+    {
+      return;
+    }
+
+  double minYCoordinate = selectedItemBBox.top();
+  QList<QgsComposerItem*>::iterator align_it = selectedItems.begin();
+  for(; align_it != selectedItems.end(); ++align_it)
+    {
+      QTransform itemTransform = (*align_it)->transform();
+      itemTransform.translate(0, minYCoordinate - itemTransform.dy());
+      (*align_it)->setTransform(itemTransform);
+    }
+}
+
+void QgsComposition::alignSelectedItemsVCenter()
+{
+  QList<QgsComposerItem*> selectedItems = selectedComposerItems();
+  if(selectedItems.size() < 2)
+    {
+      return;
+    }
+
+  QRectF selectedItemBBox;
+  if(boundingRectOfSelectedItems(selectedItemBBox) != 0)
+    {
+      return;
+    }
+
+  double averageYCoord = (selectedItemBBox.top() + selectedItemBBox.bottom()) / 2.0;
+  QList<QgsComposerItem*>::iterator align_it = selectedItems.begin();
+  for(; align_it != selectedItems.end(); ++align_it)
+    {
+      QTransform itemTransform = (*align_it)->transform();
+      itemTransform.translate(0, averageYCoord - itemTransform.dy() - (*align_it)->rect().height() / 2);
+      (*align_it)->setTransform(itemTransform);
+    }
+}
+
+void QgsComposition::alignSelectedItemsBottom()
+{
+  QList<QgsComposerItem*> selectedItems = selectedComposerItems();
+  if(selectedItems.size() < 2)
+    {
+      return;
+    }
+
+ QRectF selectedItemBBox;
+  if(boundingRectOfSelectedItems(selectedItemBBox) != 0)
+    {
+      return;
+    }
+
+  double maxYCoord = selectedItemBBox.bottom();
+  QList<QgsComposerItem*>::iterator align_it = selectedItems.begin();
+  for(; align_it != selectedItems.end(); ++align_it)
+    {
+      QTransform itemTransform = (*align_it)->transform();
+      itemTransform.translate(0, maxYCoord - itemTransform.dy() - (*align_it)->rect().height());
+      (*align_it)->setTransform(itemTransform);
+    }
+}
+
 void QgsComposition::updateZValues()
 {
   int counter = 1;
@@ -401,3 +551,43 @@
     qWarning( QString::number(( *after_it )->zValue() ).toLocal8Bit().data() );
   }
 }
+
+int QgsComposition::boundingRectOfSelectedItems(QRectF& bRect)
+{
+  QList<QgsComposerItem*> selectedItems = selectedComposerItems();
+  if(selectedItems.size() < 1)
+    {
+      return 1;
+    }
+
+  //set the box to the first item
+  QgsComposerItem* currentItem = selectedItems.at(0);
+  double minX = currentItem->transform().dx();
+  double minY = currentItem->transform().dy();
+  double maxX = minX + currentItem->rect().width();
+  double maxY = minY + currentItem->rect().height();
+
+  double currentMinX, currentMinY, currentMaxX, currentMaxY;
+
+  for(int i = 1; i < selectedItems.size(); ++i)
+    {
+      currentItem = selectedItems.at(i);
+      currentMinX = currentItem->transform().dx();
+      currentMinY = currentItem->transform().dy();
+      currentMaxX = currentMinX + currentItem->rect().width();
+      currentMaxY = currentMinY + currentItem->rect().height();
+
+      if(currentMinX < minX)
+	minX = currentMinX;
+      if(currentMaxX > maxX)
+	maxX = currentMaxX;
+      if(currentMinY < minY)
+	minY = currentMinY;
+      if(currentMaxY > maxY)
+	maxY = currentMaxY;
+    }
+
+  bRect.setTopLeft(QPointF(minX, minY));
+  bRect.setBottomRight(QPointF(maxX, maxY));
+  return 0;
+}

Modified: branches/advanced_printing_branch2/src/core/composer/qgscomposition.h
===================================================================
--- branches/advanced_printing_branch2/src/core/composer/qgscomposition.h	2008-09-19 20:23:11 UTC (rev 9360)
+++ branches/advanced_printing_branch2/src/core/composer/qgscomposition.h	2008-09-20 05:40:30 UTC (rev 9361)
@@ -97,6 +97,7 @@
     /**Removes item from z list. Usually called from destructor of QgsComposerItem*/
     void removeItemFromZList( QgsComposerItem* item );
 
+    //functions to move selected items in hierarchy
     void raiseSelectedItems();
     void raiseItem( QgsComposerItem* item );
     void lowerSelectedItems();
@@ -106,6 +107,14 @@
     void moveSelectedItemsToBottom();
     void moveItemToBottom( QgsComposerItem* item );
 
+    //functions to align selected items
+    void alignSelectedItemsLeft();
+    void alignSelectedItemsHCenter();
+    void alignSelectedItemsRight();
+    void alignSelectedItemsTop();
+    void alignSelectedItemsVCenter();
+    void alignSelectedItemsBottom();
+
     /**Sorts the zList. The only time where this function needs to be called is from QgsComposer
      after reading all the items from xml file*/
     void sortZList();
@@ -127,6 +136,10 @@
 
     /**Reset z-values of items based on position in z list*/
     void updateZValues();
+
+    /**Returns the bounding rectangle of the selected items in scene coordinates
+     @return 0 in case of success*/
+    int boundingRectOfSelectedItems(QRectF& bRect);
 };
 
 #endif



More information about the QGIS-commit mailing list