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