Skip to content

Commit 99f3430

Browse files
committed
Add shell classes for layout view tools
Copy the same model as QgsMapCanvas uses, with separate classes for individual interaction tools instead of keeping all the logic in the QGraphicsView subclass (as is done with composer)
1 parent b2a01d8 commit 99f3430

File tree

9 files changed

+555
-5
lines changed

9 files changed

+555
-5
lines changed

python/gui/gui_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@
276276
%Include layertree/qgslayertreeviewdefaultactions.sip
277277
%Include layout/qgslayoutdesignerinterface.sip
278278
%Include layout/qgslayoutview.sip
279+
%Include layout/qgslayoutviewtool.sip
279280
%Include locator/qgslocator.sip
280281
%Include locator/qgslocatorfilter.sip
281282
%Include locator/qgslocatorwidget.sip

python/gui/layout/qgslayoutview.sip

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,42 @@ class QgsLayoutView: QGraphicsView
5050
.. seealso:: layoutSet()
5151
%End
5252

53+
QgsLayoutViewTool *tool();
54+
%Docstring
55+
Returns the currently active tool for the view.
56+
.. seealso:: setTool()
57+
:rtype: QgsLayoutViewTool
58+
%End
59+
60+
void setTool( QgsLayoutViewTool *tool );
61+
%Docstring
62+
Sets the ``tool`` currently being used in the view.
63+
.. seealso:: unsetTool()
64+
.. seealso:: tool()
65+
%End
66+
67+
void unsetTool( QgsLayoutViewTool *tool );
68+
%Docstring
69+
Unsets the current view tool, if it matches the specified ``tool``.
70+
71+
This is called from destructor of view tools to make sure
72+
that the tool won't be used any more.
73+
You don't have to call it manually, QgsLayoutViewTool takes care of it.
74+
%End
75+
5376
signals:
5477

5578
void layoutSet( QgsLayout *layout );
5679
%Docstring
5780
Emitted when a ``layout`` is set for the view.
5881
.. seealso:: currentLayout()
5982
.. seealso:: setCurrentLayout()
83+
%End
84+
85+
void toolSet( QgsLayoutViewTool *tool );
86+
%Docstring
87+
Emitted when the current ``tool`` is changed.
88+
.. seealso:: setTool()
6089
%End
6190

6291
protected:
@@ -68,6 +97,12 @@ class QgsLayoutView: QGraphicsView
6897

6998
virtual void mouseDoubleClickEvent( QMouseEvent *event );
7099

100+
virtual void wheelEvent( QWheelEvent *event );
101+
102+
virtual void keyPressEvent( QKeyEvent *event );
103+
104+
virtual void keyReleaseEvent( QKeyEvent *event );
105+
71106

72107
};
73108

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/layout/qgslayoutviewtool.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsLayoutViewTool : QObject
13+
{
14+
%Docstring
15+
Abstract base class for all layout view tools.
16+
Layout view tools are user interactive tools for manipulating and adding items
17+
to QgsLayoutView widgets.
18+
.. versionadded:: 3.0
19+
%End
20+
21+
%TypeHeaderCode
22+
#include "qgslayoutviewtool.h"
23+
%End
24+
25+
%ConvertToSubClassCode
26+
if ( dynamic_cast<QgsMapToolZoom *>( sipCpp ) != NULL )
27+
sipType = sipType_QgsMapToolZoom;
28+
else
29+
sipType = NULL;
30+
%End
31+
public:
32+
33+
virtual ~QgsLayoutViewTool();
34+
35+
virtual void layoutMoveEvent( QMouseEvent *event );
36+
%Docstring
37+
Mouse move event for overriding. Default implementation does nothing.
38+
%End
39+
40+
virtual void layoutDoubleClickEvent( QMouseEvent *event );
41+
%Docstring
42+
Mouse double click event for overriding. Default implementation does nothing.
43+
%End
44+
45+
virtual void layoutPressEvent( QMouseEvent *event );
46+
%Docstring
47+
Mouse press event for overriding. Default implementation does nothing.
48+
%End
49+
50+
virtual void layoutReleaseEvent( QMouseEvent *event );
51+
%Docstring
52+
Mouse release event for overriding. Default implementation does nothing.
53+
%End
54+
55+
virtual void wheelEvent( QWheelEvent *event );
56+
%Docstring
57+
Mouse wheel event for overriding. Default implementation does nothing.
58+
%End
59+
60+
virtual void keyPressEvent( QKeyEvent *event );
61+
%Docstring
62+
Key press event for overriding. Default implementation does nothing.
63+
%End
64+
65+
virtual void keyReleaseEvent( QKeyEvent *event );
66+
%Docstring
67+
Key release event for overriding. Default implementation does nothing.
68+
%End
69+
70+
void setAction( QAction *action );
71+
%Docstring
72+
Associates an ``action`` with this tool. When the setLayoutTool
73+
method of QgsLayoutView is called the action's state will be set to on.
74+
Usually this will cause a toolbutton to appear pressed in and
75+
the previously used toolbutton to pop out.
76+
.. seealso:: action()
77+
%End
78+
79+
QAction *action();
80+
%Docstring
81+
Returns the action associated with the tool or None if no action is associated.
82+
.. seealso:: setAction()
83+
:rtype: QAction
84+
%End
85+
86+
void setCursor( const QCursor &cursor );
87+
%Docstring
88+
Sets a user defined ``cursor`` for use when the tool is active.
89+
%End
90+
91+
virtual void activate();
92+
%Docstring
93+
Called when tool is set as the currently active layout tool.
94+
Overridden implementations must take care to call the base class implementation.
95+
%End
96+
97+
virtual void deactivate();
98+
%Docstring
99+
Called when tool is deactivated.
100+
Overridden implementations must take care to call the base class implementation.
101+
%End
102+
103+
QString toolName() const;
104+
%Docstring
105+
Returns a user-visible, translated name for the tool.
106+
:rtype: str
107+
%End
108+
109+
signals:
110+
111+
void activated();
112+
%Docstring
113+
Emitted when the tool is activated.
114+
%End
115+
116+
void deactivated();
117+
%Docstring
118+
Emitted when the tool is deactivated.
119+
%End
120+
121+
protected:
122+
123+
QgsLayoutViewTool( QgsLayoutView *view /TransferThis/, const QString &name );
124+
%Docstring
125+
Constructor for QgsLayoutViewTool, taking a layout ``view`` and
126+
tool ``name`` as parameters.
127+
%End
128+
129+
};
130+
131+
/************************************************************************
132+
* This file has been generated automatically from *
133+
* *
134+
* src/gui/layout/qgslayoutviewtool.h *
135+
* *
136+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
137+
************************************************************************/

src/app/layout/qgslayoutdesignerdialog.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ void QgsLayoutDesignerDialog::restoreWindowState()
200200
void QgsLayoutDesignerDialog::activateNewItemCreationTool( int type )
201201
{
202202
QgsLogger::debug( QStringLiteral( "creating new %1 item " ).arg( QgsApplication::layoutItemRegistry()->itemMetadata( type )->visibleName() ) );
203+
if ( mView )
204+
{
205+
//mView->setTool( QgsLayoutView::ToolAddItem );
206+
}
203207
}
204208

205209

src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ SET(QGIS_GUI_SRCS
159159
layertree/qgslayertreeviewdefaultactions.cpp
160160

161161
layout/qgslayoutview.cpp
162+
layout/qgslayoutviewtool.cpp
162163

163164
locator/qgslocator.cpp
164165
locator/qgslocatorfilter.cpp
@@ -623,6 +624,7 @@ SET(QGIS_GUI_MOC_HDRS
623624

624625
layout/qgslayoutdesignerinterface.h
625626
layout/qgslayoutview.h
627+
layout/qgslayoutviewtool.h
626628

627629
locator/qgslocator.h
628630
locator/qgslocatorfilter.h

src/gui/layout/qgslayoutview.cpp

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgslayoutview.h"
1919
#include "qgslayout.h"
20+
#include "qgslayoutviewtool.h"
2021

2122
QgsLayoutView::QgsLayoutView( QWidget *parent )
2223
: QGraphicsView( parent )
@@ -39,22 +40,91 @@ void QgsLayoutView::setCurrentLayout( QgsLayout *layout )
3940
emit layoutSet( layout );
4041
}
4142

43+
QgsLayoutViewTool *QgsLayoutView::tool()
44+
{
45+
return mTool;
46+
}
47+
48+
void QgsLayoutView::setTool( QgsLayoutViewTool *tool )
49+
{
50+
if ( !tool )
51+
return;
52+
53+
if ( mTool )
54+
{
55+
mTool->deactivate();
56+
}
57+
58+
// set new tool and activate it
59+
mTool = tool;
60+
mTool->activate();
61+
62+
emit toolSet( mTool );
63+
}
64+
65+
void QgsLayoutView::unsetTool( QgsLayoutViewTool *tool )
66+
{
67+
if ( mTool && mTool == tool )
68+
{
69+
mTool->deactivate();
70+
mTool = nullptr;
71+
emit toolSet( nullptr );
72+
setCursor( Qt::ArrowCursor );
73+
}
74+
}
75+
4276
void QgsLayoutView::mousePressEvent( QMouseEvent *event )
4377
{
44-
QGraphicsView::mousePressEvent( event );
78+
if ( mTool )
79+
mTool->layoutPressEvent( event );
80+
else
81+
QGraphicsView::mousePressEvent( event );
4582
}
4683

4784
void QgsLayoutView::mouseReleaseEvent( QMouseEvent *event )
4885
{
49-
QGraphicsView::mouseReleaseEvent( event );
86+
if ( mTool )
87+
mTool->layoutReleaseEvent( event );
88+
else
89+
QGraphicsView::mouseReleaseEvent( event );
5090
}
5191

5292
void QgsLayoutView::mouseMoveEvent( QMouseEvent *event )
5393
{
54-
QGraphicsView::mouseMoveEvent( event );
94+
if ( mTool )
95+
mTool->layoutMoveEvent( event );
96+
else
97+
QGraphicsView::mouseMoveEvent( event );
5598
}
5699

57100
void QgsLayoutView::mouseDoubleClickEvent( QMouseEvent *event )
58101
{
59-
QGraphicsView::mouseDoubleClickEvent( event );
102+
if ( mTool )
103+
mTool->layoutDoubleClickEvent( event );
104+
else
105+
QGraphicsView::mouseDoubleClickEvent( event );
106+
}
107+
108+
void QgsLayoutView::wheelEvent( QWheelEvent *event )
109+
{
110+
if ( mTool )
111+
mTool->wheelEvent( event );
112+
else
113+
QGraphicsView::wheelEvent( event );
114+
}
115+
116+
void QgsLayoutView::keyPressEvent( QKeyEvent *event )
117+
{
118+
if ( mTool )
119+
mTool->keyPressEvent( event );
120+
else
121+
QGraphicsView::keyPressEvent( event );
122+
}
123+
124+
void QgsLayoutView::keyReleaseEvent( QKeyEvent *event )
125+
{
126+
if ( mTool )
127+
mTool->keyReleaseEvent( event );
128+
else
129+
QGraphicsView::keyReleaseEvent( event );
60130
}

0 commit comments

Comments
 (0)