18 #include <QApplication>
19 #include <QMainWindow>
20 #include <QMouseEvent>
24 #include <QGridLayout>
48 : QGraphicsView( parent )
49 , mRubberBandItem( 0 )
50 , mRubberBandLineItem( 0 )
51 , mMoveContentItem( 0 )
52 , mMarqueeSelect( false )
53 , mMarqueeZoom( false )
55 , mPaintingEnabled( true )
56 , mHorizontalRuler( 0 )
58 , mToolPanning( false )
59 , mMousePanning( false )
60 , mKeyPanning( false )
61 , mMovingItemContent( false )
66 setResizeAnchor( QGraphicsView::AnchorViewCenter );
67 setMouseTracking(
true );
68 viewport()->setMouseTracking(
true );
69 setFrameShape( QFrame::NoFrame );
120 viewport()->setCursor( Qt::ArrowCursor );
144 QPointF scenePoint = mapToScene( e->pos() );
149 if ( e->button() == Qt::RightButton )
154 bool lock = selectedItem->
positionLock() ?
false :
true;
156 selectedItem->update();
160 else if ( e->button() == Qt::MidButton )
170 viewport()->setCursor( Qt::ClosedHandCursor );
180 if (
composition()->selectionHandles()->isVisible() )
196 if ( e->modifiers() & Qt::ControlModifier )
201 if ( selectedItems.size() > 0 )
203 previousSelectedItem = selectedItems.at( 0 );
207 if ( previousSelectedItem )
232 if (( !selectedItem->
selected() ) &&
233 !( e->modifiers() & Qt::ShiftModifier ) )
238 if (( e->modifiers() & Qt::ShiftModifier ) && ( selectedItem->
selected() ) )
245 if ( selectedItems.size() > 0 )
261 if ( !( e->modifiers() & Qt::ShiftModifier ) )
269 double scaleFactor = 2;
271 QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
275 QPointF scenePoint = mapToScene( e->pos() );
277 visibleRect.
scale( scaleFactor, scenePoint.x(), scenePoint.y() );
278 QRectF boundsRect = visibleRect.
toRectF();
281 fitInView( boundsRect, Qt::KeepAspectRatio );
291 viewport()->setCursor( Qt::ClosedHandCursor );
298 QList<QGraphicsItem *> itemsAtCursorPos = items( e->pos() );
299 if ( itemsAtCursorPos.size() == 0 )
307 QList<QGraphicsItem*>::iterator itemIter = itemsAtCursorPos.begin();
308 for ( ; itemIter != itemsAtCursorPos.end(); ++itemIter )
328 mRubberBandLineItem =
new QGraphicsLineItem( snappedScenePoint.x(), snappedScenePoint.y(), snappedScenePoint.x(), snappedScenePoint.y() );
329 mRubberBandLineItem->setZValue( 1000 );
330 scene()->addItem( mRubberBandLineItem );
345 t.translate( snappedScenePoint.x(), snappedScenePoint.y() );
359 newLabelItem->
setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), newLabelItem->rect().width(), newLabelItem->rect().height() ) );
375 newScaleBar->
setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), 20, 20 ) );
378 if ( mapItemList.size() > 0 )
398 newLegend->
setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), newLegend->rect().width(), newLegend->rect().height() ) );
415 newPicture->
setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), 30, 30 ) );
430 newTable->
setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), 50, 50 ) );
448 switch ( currentTool )
451 return Qt::ArrowCursor;
455 QPixmap myZoomQPixmap = QPixmap((
const char ** )(
zoom_in ) );
456 return QCursor( myZoomQPixmap, 7, 7 );
460 return Qt::OpenHandCursor;
463 return Qt::ArrowCursor;
478 return QCursor( myCrosshairQPixmap, 8, 8 );
481 return Qt::ArrowCursor;
547 t.translate( scenePoint.x(), scenePoint.y() );
558 bool subtractingSelection =
false;
559 if ( e->modifiers() & Qt::ShiftModifier )
563 else if ( e->modifiers() & Qt::ControlModifier )
566 subtractingSelection =
true;
585 Qt::ItemSelectionMode selectionMode = Qt::IntersectsItemShape;
586 if ( e->modifiers() & Qt::AltModifier )
589 selectionMode = Qt::ContainsItemShape;
593 QList<QGraphicsItem *> itemList =
composition()->items( boundsRect, selectionMode );
594 QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
595 for ( ; itemIt != itemList.end(); ++itemIt )
599 if ( mypItem && !paperItem )
603 if ( subtractingSelection )
618 if ( selectedItemList.size() > 0 )
633 t.translate( scenePoint.x(), scenePoint.y() );
649 double scaleFactor = 0.5;
651 QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
655 QPointF scenePoint = mapToScene( e->pos() );
657 visibleRect.
scale( scaleFactor, scenePoint.x(), scenePoint.y() );
658 boundsRect = visibleRect.
toRectF();
670 fitInView( boundsRect, Qt::KeepAspectRatio );
688 if ( e->button() != Qt::LeftButton &&
695 QPoint mousePressStopPoint = e->pos();
700 bool clickOnly =
false;
701 if ( qAbs( diffX ) < 2 && qAbs( diffY ) < 2 )
706 QPointF scenePoint = mapToScene( e->pos() );
713 if ( clickOnly && e->button() == Qt::MidButton )
718 QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
720 visibleRect.
scale( 1, scenePoint.x(), scenePoint.y() );
721 QRectF boundsRect = visibleRect.
toRectF();
724 fitInView( boundsRect, Qt::KeepAspectRatio );
740 if ( e->button() != Qt::LeftButton )
793 QPointF scenePoint = mapToScene( e->pos() );
887 horizontalScrollBar()->setValue( horizontalScrollBar()->value() - ( e->x() -
mMouseLastXY.x() ) );
888 verticalScrollBar()->setValue( verticalScrollBar()->value() - ( e->y() -
mMouseLastXY.y() ) );
892 else if ( e->buttons() == Qt::NoButton )
901 QPointF scenePoint = mapToScene( e->pos() );
942 composerMap->update();
1006 QList<QgsComposerItem*>::iterator itemIt = composerItemList.begin();
1009 QDomElement documentElement = doc.createElement(
"ComposerItemClipboard" );
1010 for ( ; itemIt != composerItemList.end(); ++itemIt )
1016 QSet<QgsComposerItem*> groupedItems = itemGroup->
items();
1017 QSet<QgsComposerItem*>::iterator it = groupedItems.begin();
1018 for ( ; it != groupedItems.end(); ++it )
1020 ( *it )->writeXML( documentElement, doc );
1023 ( *itemIt )->writeXML( documentElement, doc );
1029 doc.appendChild( documentElement );
1035 QDomNodeList composerItemsNodes = doc.elementsByTagName(
"ComposerItem" );
1036 for (
int i = 0; i < composerItemsNodes.count(); ++i )
1038 QDomNode composerItemNode = composerItemsNodes.at( i );
1039 if ( composerItemNode.isElement() )
1041 composerItemNode.toElement().removeAttribute(
"uuid" );
1046 QMimeData *mimeData =
new QMimeData;
1047 mimeData->setData(
"text/xml", doc.toByteArray() );
1048 QClipboard *clipboard = QApplication::clipboard();
1049 clipboard->setMimeData( mimeData );
1060 QClipboard *clipboard = QApplication::clipboard();
1061 if ( doc.setContent( clipboard->mimeData()->data(
"text/xml" ) ) )
1063 QDomElement docElem = doc.documentElement();
1064 if ( docElem.tagName() ==
"ComposerItemClipboard" )
1072 pt = mapToScene( mapFromGlobal( QCursor::pos() ) );
1077 pt = mapToScene( viewport()->rect().center() );
1097 QList<QgsComposerItem*>::iterator itemIt = composerItemList.begin();
1100 for ( ; itemIt != composerItemList.end(); ++itemIt )
1117 QList<QGraphicsItem *> itemList =
composition()->items();
1118 QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
1119 for ( ; itemIt != itemList.end(); ++itemIt )
1123 if ( mypItem && !paperItem )
1157 QList<QGraphicsItem *> itemList =
composition()->items();
1158 QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
1159 for ( ; itemIt != itemList.end(); ++itemIt )
1163 if ( mypItem && !paperItem )
1196 if ( e->isAutoRepeat() )
1202 if ( !( e->modifiers() & Qt::ControlModifier ) && !
mMarqueeZoom )
1208 else if ( !( e->modifiers() & Qt::ControlModifier ) &&
mMarqueeZoom )
1218 QPixmap myZoomQPixmap = QPixmap((
const char ** )( e->modifiers() & Qt::ShiftModifier ?
zoom_out :
zoom_in ) );
1219 QCursor zoomCursor = QCursor( myZoomQPixmap, 7, 7 );
1220 viewport()->setCursor( zoomCursor );
1231 if ( e->key() == Qt::Key_Space && ! e->isAutoRepeat() )
1233 if ( !( e->modifiers() & Qt::ControlModifier ) )
1243 viewport()->setCursor( Qt::ClosedHandCursor );
1253 QPixmap myZoomQPixmap = QPixmap((
const char ** )( e->modifiers() & Qt::ShiftModifier ?
zoom_out :
zoom_in ) );
1254 QCursor zoomCursor = QCursor( myZoomQPixmap, 7, 7 );
1255 viewport()->setCursor( zoomCursor );
1263 if ( ! e->isAutoRepeat() )
1265 QPixmap myZoomQPixmap = QPixmap((
const char ** )( e->modifiers() & Qt::ShiftModifier ?
zoom_out :
zoom_in ) );
1266 QCursor zoomCursor = QCursor( myZoomQPixmap, 7, 7 );
1267 viewport()->setCursor( zoomCursor );
1273 QList<QgsComposerItem*>::iterator itemIt = composerItemList.begin();
1276 double increment = 1.0;
1277 if ( e->modifiers() & Qt::ShiftModifier )
1283 if ( e->key() == Qt::Key_Left )
1285 for ( ; itemIt != composerItemList.end(); ++itemIt )
1288 ( *itemIt )->move( -1 * increment, 0.0 );
1289 ( *itemIt )->endCommand();
1292 else if ( e->key() == Qt::Key_Right )
1294 for ( ; itemIt != composerItemList.end(); ++itemIt )
1297 ( *itemIt )->move( increment, 0.0 );
1298 ( *itemIt )->endCommand();
1301 else if ( e->key() == Qt::Key_Down )
1303 for ( ; itemIt != composerItemList.end(); ++itemIt )
1306 ( *itemIt )->move( 0.0, increment );
1307 ( *itemIt )->endCommand();
1310 else if ( e->key() == Qt::Key_Up )
1312 for ( ; itemIt != composerItemList.end(); ++itemIt )
1315 ( *itemIt )->move( 0.0, -1 * increment );
1316 ( *itemIt )->endCommand();
1323 if ( e->key() == Qt::Key_Space && !e->isAutoRepeat() &&
mKeyPanning )
1359 if ( ! e->isAutoRepeat() )
1361 QPixmap myZoomQPixmap = QPixmap((
const char ** )( e->modifiers() & Qt::ShiftModifier ?
zoom_out :
zoom_in ) );
1362 QCursor zoomCursor = QCursor( myZoomQPixmap, 7, 7 );
1363 viewport()->setCursor( zoomCursor );
1377 if (
composition()->selectionHandles()->isDragging() ||
composition()->selectionHandles()->isResizing() )
1387 QPointF scenePoint = mapToScene( event->pos() );
1392 if ( theItem->isSelected() )
1394 QPointF itemPoint = theItem->mapFromScene( scenePoint );
1396 theItem->zoomContent( event->delta(), itemPoint.x(), itemPoint.y() );
1397 theItem->endCommand();
1411 QSettings mySettings;
1412 int wheelAction = mySettings.value(
"/qgis/wheel_action", 2 ).toInt();
1413 double zoomFactor = mySettings.value(
"/qgis/zoom_factor", 2 ).toDouble();
1420 if ( event->modifiers() & Qt::ControlModifier )
1423 zoomFactor = 1.0 + ( zoomFactor - 1.0 ) / 10.0;
1427 bool zoomIn =
event->delta() > 0;
1428 double scaleFactor = ( zoomIn ? 1 / zoomFactor : zoomFactor );
1431 QRect viewportRect( 0, 0, viewport()->width(), viewport()->height() );
1435 QPointF scenePoint = mapToScene( event->pos() );
1442 centerOn( scenePoint.x(), scenePoint.y() );
1449 QgsPoint newCenter( scenePoint.x() + (( oldCenter.x() - scenePoint.x() ) * scaleFactor ),
1450 scenePoint.y() + (( oldCenter.y() - scenePoint.y() ) * scaleFactor ) );
1451 centerOn( newCenter.x(), newCenter.y() );
1462 scale( zoomFactor, zoomFactor );
1466 scale( 1 / zoomFactor, 1 / zoomFactor );
1474 QList<QGraphicsItem *> itemList =
composition()->items();
1475 QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
1476 for ( ; itemIt != itemList.end(); ++itemIt )
1488 double dpi = QgsApplication::desktop()->logicalDpiX();
1490 if (( dpi < 60 ) || ( dpi > 250 ) )
1494 double scale = zoomLevel * dpi / 25.4;
1495 setTransform( QTransform::fromScale( scale , scale ) );
1574 if ( selectionList.size() < 2 )
1580 QList<QgsComposerItem*>::iterator itemIter = selectionList.begin();
1581 for ( ; itemIter != selectionList.end(); ++itemIter )
1583 itemGroup->
addItem( *itemIter );
1599 QList<QgsComposerItem*>::iterator itemIter = selectionList.begin();
1600 for ( ; itemIter != selectionList.end(); ++itemIter )
1607 delete( *itemIter );
1615 QMainWindow* composerObject = 0;
1616 QObject* currentObject = parent();
1617 if ( !currentObject )
1619 return qobject_cast<QMainWindow *>( currentObject );
1624 composerObject = qobject_cast<QMainWindow*>( currentObject );
1625 if ( composerObject || currentObject->parent() == 0 )
1627 return composerObject;
1629 currentObject = currentObject->parent();