void QgsComposerView::wheelZoom( QWheelEvent * event ) { //get mouse wheel zoom behaviour settings QSettings mySettings; int wheelAction = mySettings.value( "/qgis/wheel_action", 2 ).toInt(); double zoomFactor = mySettings.value( "/qgis/zoom_factor", 2 ).toDouble(); //caculate zoom scale factor bool zoomIn = event->delta() > 0; double scaleFactor = ( zoomIn ? 1 / zoomFactor : zoomFactor ); //get current visible part of scene QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() ); QgsRectangle visibleRect = QgsRectangle( mapToScene( viewportRect ).boundingRect() ); //transform the mouse pos to scene coordinates QPointF scenePoint = mapToScene( event->pos() ); //zoom composition, respecting wheel action setting switch (( QgsMapCanvas::WheelAction )wheelAction ) { case QgsMapCanvas::WheelZoom: // zoom without changing extent if ( zoomIn ) { scale( zoomFactor, zoomFactor ); } else { scale( 1 / zoomFactor, 1 / zoomFactor ); } break; case QgsMapCanvas::WheelZoomAndRecenter: { visibleRect.scale( scaleFactor, scenePoint.x(), scenePoint.y() ); fitInView( visibleRect.toRectF(), Qt::KeepAspectRatio ); break; } case QgsMapCanvas::WheelZoomToMouseCursor: { QgsPoint oldCenter( visibleRect.center() ); QgsPoint newCenter( scenePoint.x() + (( oldCenter.x() - scenePoint.x() ) * scaleFactor ), scenePoint.y() + (( oldCenter.y() - scenePoint.y() ) * scaleFactor ) ); visibleRect.scale( scaleFactor, newCenter.x(), newCenter.y() ); fitInView( visibleRect.toRectF(), Qt::KeepAspectRatio ); break; } case QgsMapCanvas::WheelNothing: return; } //update composition for new zoom updateRulers(); update(); //redraw cached map items QList<QGraphicsItem *> itemList = composition()->items(); QList<QGraphicsItem *>::iterator itemIt = itemList.begin(); for ( ; itemIt != itemList.end(); ++itemIt ) { QgsComposerMap* mypItem = dynamic_cast<QgsComposerMap *>( *itemIt ); if (( mypItem ) && ( mypItem->previewMode() == QgsComposerMap::Render ) ) { mypItem->updateCachedImage(); } } }
void QgsComposerView::wheelZoom( QWheelEvent * event ) { //get mouse wheel zoom behaviour settings QSettings mySettings; int wheelAction = mySettings.value( "/qgis/wheel_action", 2 ).toInt(); double zoomFactor = mySettings.value( "/qgis/zoom_factor", 2 ).toDouble(); if (( QgsMapCanvas::WheelAction )wheelAction == QgsMapCanvas::WheelNothing ) { return; } if ( event->modifiers() & Qt::ControlModifier ) { //holding ctrl while wheel zooming results in a finer zoom zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 10.0; } //caculate zoom scale factor bool zoomIn = event->delta() > 0; double scaleFactor = ( zoomIn ? 1 / zoomFactor : zoomFactor ); //get current visible part of scene QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() ); QgsRectangle visibleRect = QgsRectangle( mapToScene( viewportRect ).boundingRect() ); //transform the mouse pos to scene coordinates QPointF scenePoint = mapToScene( event->pos() ); //adjust view center according to wheel action setting switch (( QgsMapCanvas::WheelAction )wheelAction ) { case QgsMapCanvas::WheelZoomAndRecenter: { centerOn( scenePoint.x(), scenePoint.y() ); break; } case QgsMapCanvas::WheelZoomToMouseCursor: { QgsPoint oldCenter( visibleRect.center() ); QgsPoint newCenter( scenePoint.x() + (( oldCenter.x() - scenePoint.x() ) * scaleFactor ), scenePoint.y() + (( oldCenter.y() - scenePoint.y() ) * scaleFactor ) ); centerOn( newCenter.x(), newCenter.y() ); break; } default: break; } //zoom composition if ( zoomIn ) { scale( zoomFactor, zoomFactor ); } else { scale( 1 / zoomFactor, 1 / zoomFactor ); } //update composition for new zoom emit zoomLevelChanged(); updateRulers(); update(); //redraw cached map items QList<QGraphicsItem *> itemList = composition()->items(); QList<QGraphicsItem *>::iterator itemIt = itemList.begin(); for ( ; itemIt != itemList.end(); ++itemIt ) { QgsComposerMap* mypItem = dynamic_cast<QgsComposerMap *>( *itemIt ); if (( mypItem ) && ( mypItem->previewMode() == QgsComposerMap::Render ) ) { mypItem->updateCachedImage(); } } }
void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QMap< QgsComposerMap*, int >* mapsToRestore, bool addUndoCommands, QPointF* pos ) { QDomNodeList composerLabelList = elem.elementsByTagName( "ComposerLabel" ); for ( int i = 0; i < composerLabelList.size(); ++i ) { QDomElement currentComposerLabelElem = composerLabelList.at( i ).toElement(); QgsComposerLabel* newLabel = new QgsComposerLabel( this ); newLabel->readXML( currentComposerLabelElem, doc ); if ( pos ) { newLabel->setItemPosition( pos->x(), pos->y() ); } addComposerLabel( newLabel ); if ( addUndoCommands ) { pushAddRemoveCommand( newLabel, tr( "Label added" ) ); } } // map QDomNodeList composerMapList = elem.elementsByTagName( "ComposerMap" ); for ( int i = 0; i < composerMapList.size(); ++i ) { QDomElement currentComposerMapElem = composerMapList.at( i ).toElement(); QgsComposerMap* newMap = new QgsComposerMap( this ); newMap->readXML( currentComposerMapElem, doc ); newMap->assignFreeId(); if ( mapsToRestore ) { mapsToRestore->insert( newMap, ( int )( newMap->previewMode() ) ); newMap->setPreviewMode( QgsComposerMap::Rectangle ); } addComposerMap( newMap, false ); if ( pos ) { newMap->setItemPosition( pos->x(), pos->y() ); } if ( addUndoCommands ) { pushAddRemoveCommand( newMap, tr( "Map added" ) ); } } // arrow QDomNodeList composerArrowList = elem.elementsByTagName( "ComposerArrow" ); for ( int i = 0; i < composerArrowList.size(); ++i ) { QDomElement currentComposerArrowElem = composerArrowList.at( i ).toElement(); QgsComposerArrow* newArrow = new QgsComposerArrow( this ); newArrow->readXML( currentComposerArrowElem, doc ); if ( pos ) { newArrow->setItemPosition( pos->x(), pos->y() ); } addComposerArrow( newArrow ); if ( addUndoCommands ) { pushAddRemoveCommand( newArrow, tr( "Arrow added" ) ); } } // scalebar QDomNodeList composerScaleBarList = elem.elementsByTagName( "ComposerScaleBar" ); for ( int i = 0; i < composerScaleBarList.size(); ++i ) { QDomElement currentComposerScaleBarElem = composerScaleBarList.at( i ).toElement(); QgsComposerScaleBar* newScaleBar = new QgsComposerScaleBar( this ); newScaleBar->readXML( currentComposerScaleBarElem, doc ); if ( pos ) { newScaleBar->setItemPosition( pos->x(), pos->y() ); } addComposerScaleBar( newScaleBar ); if ( addUndoCommands ) { pushAddRemoveCommand( newScaleBar, tr( "Scale bar added" ) ); } } // shape QDomNodeList composerShapeList = elem.elementsByTagName( "ComposerShape" ); for ( int i = 0; i < composerShapeList.size(); ++i ) { QDomElement currentComposerShapeElem = composerShapeList.at( i ).toElement(); QgsComposerShape* newShape = new QgsComposerShape( this ); newShape->readXML( currentComposerShapeElem, doc ); if ( pos ) { newShape->setItemPosition( pos->x(), pos->y() ); } addComposerShape( newShape ); if ( addUndoCommands ) { pushAddRemoveCommand( newShape, tr( "Shape added" ) ); } } // picture QDomNodeList composerPictureList = elem.elementsByTagName( "ComposerPicture" ); for ( int i = 0; i < composerPictureList.size(); ++i ) { QDomElement currentComposerPictureElem = composerPictureList.at( i ).toElement(); QgsComposerPicture* newPicture = new QgsComposerPicture( this ); newPicture->readXML( currentComposerPictureElem, doc ); if ( pos ) { newPicture->setItemPosition( pos->x(), pos->y() ); } addComposerPicture( newPicture ); if ( addUndoCommands ) { pushAddRemoveCommand( newPicture, tr( "Picture added" ) ); } } // legend QDomNodeList composerLegendList = elem.elementsByTagName( "ComposerLegend" ); for ( int i = 0; i < composerLegendList.size(); ++i ) { QDomElement currentComposerLegendElem = composerLegendList.at( i ).toElement(); QgsComposerLegend* newLegend = new QgsComposerLegend( this ); newLegend->readXML( currentComposerLegendElem, doc ); if ( pos ) { newLegend->setItemPosition( pos->x(), pos->y() ); } addComposerLegend( newLegend ); if ( addUndoCommands ) { pushAddRemoveCommand( newLegend, tr( "Legend added" ) ); } } // table QDomNodeList composerTableList = elem.elementsByTagName( "ComposerAttributeTable" ); for ( int i = 0; i < composerTableList.size(); ++i ) { QDomElement currentComposerTableElem = composerTableList.at( i ).toElement(); QgsComposerAttributeTable* newTable = new QgsComposerAttributeTable( this ); newTable->readXML( currentComposerTableElem, doc ); if ( pos ) { newTable->setItemPosition( pos->x(), pos->y() ); } addComposerTable( newTable ); if ( addUndoCommands ) { pushAddRemoveCommand( newTable, tr( "Table added" ) ); } } //html QDomNodeList composerHtmlList = elem.elementsByTagName( "ComposerHtml" ); for ( int i = 0; i < composerHtmlList.size(); ++i ) { QDomElement currentHtmlElem = composerHtmlList.at( i ).toElement(); QgsComposerHtml* newHtml = new QgsComposerHtml( this, false ); newHtml->readXML( currentHtmlElem, doc ); newHtml->setCreateUndoCommands( true ); this->addMultiFrame( newHtml ); } }