QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsmodelviewmousehandles.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmodelviewmousehandles.cpp
3 ------------------------
4 begin : March 2020
5 copyright : (C) 2020 by Nyall Dawson
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgis.h"
20#include "qgslogger.h"
21#include "qgsproject.h"
25#include "qgsmodelviewtool.h"
26#include <QGraphicsSceneHoverEvent>
27#include <QPainter>
28#include <QWidget>
29#include <limits>
30
32
33
34QgsModelViewMouseHandles::QgsModelViewMouseHandles( QgsModelGraphicsView *view )
35 : QgsGraphicsViewMouseHandles( view )
36 , mView( view )
37{
38 //listen for selection changes, and update handles accordingly
39 connect( modelScene(), &QGraphicsScene::selectionChanged, this, &QgsModelViewMouseHandles::selectionChanged );
40 setHandleSize( 5 );
41}
42
43QgsModelGraphicsScene *QgsModelViewMouseHandles::modelScene() const
44{
45 return qobject_cast< QgsModelGraphicsScene * >( mView->scene() );
46}
47
48void QgsModelViewMouseHandles::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
49{
50 paintInternal( painter, !( modelScene()->flags() & QgsModelGraphicsScene::FlagHideControls ),
51 true, false, option, widget );
52}
53
54void QgsModelViewMouseHandles::selectionChanged()
55{
56 //listen out for selected items' size and rotation changed signals
57 const QList<QGraphicsItem *> itemList = mView->items();
58 for ( QGraphicsItem *graphicsItem : itemList )
59 {
60 QgsModelComponentGraphicItem *item = dynamic_cast<QgsModelComponentGraphicItem *>( graphicsItem );
61 if ( !item )
62 continue;
63
64 if ( item->isSelected() )
65 {
66 connect( item, &QgsModelComponentGraphicItem::sizePositionChanged, this, &QgsModelViewMouseHandles::selectedItemSizeChanged );
67 }
68 else
69 {
70 disconnect( item, &QgsModelComponentGraphicItem::sizePositionChanged, this, &QgsModelViewMouseHandles::selectedItemSizeChanged );
71 }
72 }
73
74 resetStatusBar();
75 updateHandles();
76}
77
78void QgsModelViewMouseHandles::setViewportCursor( Qt::CursorShape cursor )
79{
80 //workaround qt bug #3732 by setting cursor for QGraphicsView viewport,
81 //rather then setting it directly here
82
83 if ( qobject_cast< QgsModelViewTool *>( mView->tool() ) )
84 {
85 mView->viewport()->setCursor( cursor );
86 }
87}
88
89QList<QGraphicsItem *> QgsModelViewMouseHandles::sceneItemsAtPoint( QPointF scenePoint )
90{
91 return modelScene()->items( scenePoint );
92}
93
94QList<QGraphicsItem *> QgsModelViewMouseHandles::selectedSceneItems( bool ) const
95{
96 QList<QGraphicsItem *> res;
97 const QList<QgsModelComponentGraphicItem *> componentItems = modelScene()->selectedComponentItems();
98 res.reserve( componentItems.size() );
99 for ( QgsModelComponentGraphicItem *item : componentItems )
100 res << item;
101 return res;
102}
103
104QRectF QgsModelViewMouseHandles::itemRect( QGraphicsItem *item ) const
105{
106 if ( QgsModelComponentGraphicItem *componentItem = dynamic_cast<QgsModelComponentGraphicItem *>( item ) )
107 return componentItem->itemRect();
108 else
109 return QRectF();
110}
111
112QRectF QgsModelViewMouseHandles::storedItemRect( QGraphicsItem *item ) const
113{
114 if ( QgsModelComponentGraphicItem *componentItem = dynamic_cast<QgsModelComponentGraphicItem *>( item ) )
115 return componentItem->itemRect( true );
116 else
117 return QRectF();
118}
119
120void QgsModelViewMouseHandles::moveItem( QGraphicsItem *item, double deltaX, double deltaY )
121{
122 if ( QgsModelComponentGraphicItem *componentItem = dynamic_cast<QgsModelComponentGraphicItem *>( item ) )
123 {
124 componentItem->moveComponentBy( deltaX, deltaY );
125 }
126}
127
128void QgsModelViewMouseHandles::previewItemMove( QGraphicsItem *item, double deltaX, double deltaY )
129{
130 if ( QgsModelComponentGraphicItem *componentItem = dynamic_cast<QgsModelComponentGraphicItem *>( item ) )
131 {
132 componentItem->previewItemMove( deltaX, deltaY );
133 }
134}
135
136void QgsModelViewMouseHandles::setItemRect( QGraphicsItem *item, QRectF rect )
137{
138 if ( QgsModelComponentGraphicItem *componentItem = dynamic_cast<QgsModelComponentGraphicItem *>( item ) )
139 {
140 componentItem->finalizePreviewedItemRectChange( rect );
141 }
142}
143
144QRectF QgsModelViewMouseHandles::previewSetItemRect( QGraphicsItem *item, QRectF rect )
145{
146 if ( QgsModelComponentGraphicItem *componentItem = dynamic_cast<QgsModelComponentGraphicItem *>( item ) )
147 {
148 return componentItem->previewItemRectChange( rect );
149 }
150 return rect;
151}
152
153void QgsModelViewMouseHandles::startMacroCommand( const QString &text )
154{
155 mView->startMacroCommand( text );
156}
157
158void QgsModelViewMouseHandles::endMacroCommand()
159{
160 mView->endMacroCommand();
161}
162
163QPointF QgsModelViewMouseHandles::snapPoint( QPointF originalPoint, QgsGraphicsViewMouseHandles::SnapGuideMode mode, bool snapHorizontal, bool snapVertical )
164{
165 bool snapped = false;
166
167 QPointF snappedPoint;
168 switch ( mode )
169 {
170 case Item:
171 case Point:
172 snappedPoint = mView->snapper()->snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal, snapVertical );
173 break;
174 }
175
176 return snapped ? snappedPoint : originalPoint;
177}
178
179