Skip to content

Commit ba7fbca

Browse files
committedJan 16, 2023
Move QgsElevationProfileLayerTreeView to GUI library
1 parent 508d96e commit ba7fbca

8 files changed

+160
-176
lines changed
 

‎src/app/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ set(QGIS_APP_SRCS
180180

181181
elevation/qgselevationprofileexportsettingswidget.cpp
182182
elevation/qgselevationprofileimageexportdialog.cpp
183-
elevation/qgselevationprofilelayertreemodel.cpp
184-
elevation/qgselevationprofilelayertreeview.cpp
185183
elevation/qgselevationprofilepdfexportdialog.cpp
186184
elevation/qgselevationprofiletoolidentify.cpp
187185
elevation/qgselevationprofiletoolmeasure.cpp

‎src/app/elevation/qgselevationprofilelayertreeview.cpp

Lines changed: 0 additions & 93 deletions
This file was deleted.

‎src/app/elevation/qgselevationprofilelayertreeview.h

Lines changed: 0 additions & 68 deletions
This file was deleted.

‎src/app/elevation/qgselevationprofilewidget.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ QgsElevationProfileWidget::QgsElevationProfileWidget( const QString &name )
8080

8181
mPanTool = new QgsPlotToolPan( mCanvas );
8282

83-
mLayerTreeView = new QgsElevationProfileLayerTreeView( mLayerTree.get() );
83+
mLayerTreeView = new QgsAppElevationProfileLayerTreeView( mLayerTree.get() );
8484

8585
connect( mLayerTreeView, &QAbstractItemView::doubleClicked, this, [ = ]( const QModelIndex & index )
8686
{
@@ -765,3 +765,31 @@ QgsElevationProfileWidgetSettingsAction::QgsElevationProfileWidgetSettingsAction
765765
w->setLayout( gLayout );
766766
setDefaultWidget( w );
767767
}
768+
769+
QgsAppElevationProfileLayerTreeView::QgsAppElevationProfileLayerTreeView( QgsLayerTree *rootNode, QWidget *parent )
770+
: QgsElevationProfileLayerTreeView( rootNode, parent )
771+
{
772+
773+
}
774+
775+
void QgsAppElevationProfileLayerTreeView::contextMenuEvent( QContextMenuEvent *event )
776+
{
777+
const QModelIndex index = indexAt( event->pos() );
778+
if ( !index.isValid() )
779+
setCurrentIndex( QModelIndex() );
780+
781+
if ( QgsMapLayer *layer = indexToLayer( index ) )
782+
{
783+
QMenu *menu = new QMenu();
784+
785+
QAction *propertiesAction = new QAction( tr( "Properties…" ), menu );
786+
connect( propertiesAction, &QAction::triggered, this, [layer]
787+
{
788+
QgisApp::instance()->showLayerProperties( layer, QStringLiteral( "mOptsPage_Elevation" ) );
789+
} );
790+
menu->addAction( propertiesAction );
791+
792+
menu->exec( mapToGlobal( event->pos() ) );
793+
delete menu;
794+
}
795+
}

‎src/app/elevation/qgselevationprofilewidget.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgsgeometry.h"
2424
#include "qobjectuniqueptr.h"
2525
#include "qgssettingsentryimpl.h"
26+
#include "qgselevationprofilelayertreeview.h"
2627

2728
#include <QWidgetAction>
2829
#include <QElapsedTimer>
@@ -42,14 +43,25 @@ class QgsPlotToolZoom;
4243
class QgsPlotToolXAxisZoom;
4344
class QgsDoubleSpinBox;
4445
class QgsElevationProfileWidgetSettingsAction;
45-
class QgsElevationProfileLayerTreeView;
4646
class QgsLayerTree;
4747
class QgsLayerTreeRegistryBridge;
4848
class QgsElevationProfileToolIdentify;
4949
class QgsElevationProfileToolMeasure;
5050
class QLabel;
5151
class QgsProfilePoint;
5252

53+
class QgsAppElevationProfileLayerTreeView : public QgsElevationProfileLayerTreeView
54+
{
55+
Q_OBJECT
56+
public:
57+
58+
explicit QgsAppElevationProfileLayerTreeView( QgsLayerTree *rootNode, QWidget *parent = nullptr );
59+
60+
protected:
61+
62+
void contextMenuEvent( QContextMenuEvent *event ) override;
63+
};
64+
5365
class QgsElevationProfileWidget : public QWidget
5466
{
5567
Q_OBJECT

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ set(QGIS_GUI_SRCS
230230
editorwidgets/qgsvaluerelationwidgetfactory.cpp
231231

232232
elevation/qgselevationprofilecanvas.cpp
233+
elevation/qgselevationprofilelayertreeview.cpp
233234

234235
history/qgshistoryprovider.cpp
235236
history/qgshistoryproviderregistry.cpp
@@ -1108,6 +1109,7 @@ set(QGIS_GUI_HDRS
11081109
effects/qgspainteffectwidget.h
11091110

11101111
elevation/qgselevationprofilecanvas.h
1112+
elevation/qgselevationprofilelayertreeview.h
11111113

11121114
history/qgshistoryprovider.h
11131115
history/qgshistoryproviderregistry.h

‎src/app/elevation/qgselevationprofilelayertreemodel.cpp renamed to ‎src/gui/elevation/qgselevationprofilelayertreeview.cpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***************************************************************************
2-
qgselevationprofilelayertreemodel.cpp
2+
qgselevationprofilelayertreeview.cpp
33
-----------------
44
begin : April 2022
55
copyright : (C) 2022 by Nyall Dawson
@@ -16,7 +16,9 @@
1616
* *
1717
***************************************************************************/
1818

19-
#include "qgselevationprofilelayertreemodel.h"
19+
#include "qgselevationprofilelayertreeview.h"
20+
#include "qgslayertreenode.h"
21+
#include "qgslayertree.h"
2022
#include "qgslayertreenode.h"
2123
#include "qgslayertree.h"
2224
#include "qgssymbollayerutils.h"
@@ -28,8 +30,12 @@
2830
#include "qgsmarkersymbol.h"
2931
#include "qgsfillsymbol.h"
3032

33+
#include <QHeaderView>
34+
#include <QContextMenuEvent>
35+
#include <QMenu>
3136
#include <QMimeData>
3237

38+
3339
QgsElevationProfileLayerTreeModel::QgsElevationProfileLayerTreeModel( QgsLayerTree *rootNode, QObject *parent )
3440
: QgsLayerTreeModel( rootNode, parent )
3541
{
@@ -324,3 +330,48 @@ bool QgsElevationProfileLayerTreeProxyModel::filterAcceptsRow( int sourceRow, co
324330
return false;
325331
}
326332
}
333+
334+
335+
QgsElevationProfileLayerTreeView::QgsElevationProfileLayerTreeView( QgsLayerTree *rootNode, QWidget *parent )
336+
: QTreeView( parent )
337+
, mLayerTree( rootNode )
338+
{
339+
mModel = new QgsElevationProfileLayerTreeModel( rootNode, this );
340+
mProxyModel = new QgsElevationProfileLayerTreeProxyModel( mModel, this );
341+
342+
setHeaderHidden( true );
343+
344+
setDragEnabled( true );
345+
setAcceptDrops( true );
346+
setDropIndicatorShown( true );
347+
setExpandsOnDoubleClick( false );
348+
349+
// Ensure legend graphics are scrollable
350+
header()->setStretchLastSection( false );
351+
header()->setSectionResizeMode( QHeaderView::ResizeToContents );
352+
353+
// If vertically scrolling by item, legend graphics can get clipped
354+
setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
355+
356+
setDefaultDropAction( Qt::MoveAction );
357+
358+
setModel( mProxyModel );
359+
}
360+
361+
QgsMapLayer *QgsElevationProfileLayerTreeView::indexToLayer( const QModelIndex &index )
362+
{
363+
if ( QgsLayerTreeNode *node = mModel->index2node( mProxyModel->mapToSource( index ) ) )
364+
{
365+
if ( QgsLayerTreeLayer *layerTreeLayerNode = mLayerTree->toLayer( node ) )
366+
{
367+
return layerTreeLayerNode->layer();
368+
}
369+
}
370+
return nullptr;
371+
}
372+
373+
void QgsElevationProfileLayerTreeView::resizeEvent( QResizeEvent *event )
374+
{
375+
header()->setMinimumSectionSize( viewport()->width() );
376+
QTreeView::resizeEvent( event );
377+
}

‎src/app/elevation/qgselevationprofilelayertreemodel.h renamed to ‎src/gui/elevation/qgselevationprofilelayertreeview.h

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***************************************************************************
2-
qgselevationprofilelayertreemodel.h
2+
qgselevationprofilelayertreeview.h
33
---------------
44
begin : April 2022
55
copyright : (C) 2022 by Nyall Dawson
@@ -15,21 +15,34 @@
1515
* *
1616
***************************************************************************/
1717

18-
#ifndef QGSELEVATIONPROFILELAYERTREEMODEL_H
19-
#define QGSELEVATIONPROFILELAYERTREEMODEL_H
18+
#ifndef QGSELEVATIONPROFILELAYERTREEVIEW_H
19+
#define QGSELEVATIONPROFILELAYERTREEVIEW_H
2020

2121
#include "qgsconfig.h"
2222
#include "qgslayertreemodel.h"
23+
#include "qgis_gui.h"
2324

2425
#include <QSortFilterProxyModel>
26+
#include <QTreeView>
27+
28+
#define SIP_NO_FILE
29+
30+
31+
class QgsLayerTree;
32+
class QgsElevationProfileLayerTreeModel;
33+
class QgsElevationProfileLayerTreeProxyModel;
34+
class QgsMapLayer;
35+
2536

2637
/**
27-
* \ingroup app
38+
* \ingroup gui
2839
* \brief A layer tree model subclass for elevation profiles.
2940
*
30-
* \since QGIS 3.26
41+
* \note Not available in Python bindings
42+
*
43+
* \since QGIS 3.30
3144
*/
32-
class QgsElevationProfileLayerTreeModel : public QgsLayerTreeModel
45+
class GUI_EXPORT QgsElevationProfileLayerTreeModel : public QgsLayerTreeModel
3346
{
3447

3548
Q_OBJECT
@@ -57,9 +70,11 @@ class QgsElevationProfileLayerTreeModel : public QgsLayerTreeModel
5770
* \ingroup gui
5871
* \brief A proxy model for elevation profiles.
5972
*
60-
* \since QGIS 3.26
73+
* \note Not available in Python bindings
74+
*
75+
* \since QGIS 3.30
6176
*/
62-
class QgsElevationProfileLayerTreeProxyModel : public QSortFilterProxyModel
77+
class GUI_EXPORT QgsElevationProfileLayerTreeProxyModel : public QSortFilterProxyModel
6378
{
6479

6580
Q_OBJECT
@@ -81,4 +96,43 @@ class QgsElevationProfileLayerTreeProxyModel : public QSortFilterProxyModel
8196

8297
};
8398

84-
#endif // QGSELEVATIONPROFILELAYERTREEMODEL_H
99+
/**
100+
* \ingroup gui
101+
* \brief A layer tree view for elevation profiles.
102+
*
103+
* \note Not available in Python bindings
104+
*
105+
* \since QGIS 3.30
106+
*/
107+
class GUI_EXPORT QgsElevationProfileLayerTreeView : public QTreeView
108+
{
109+
110+
Q_OBJECT
111+
112+
public:
113+
114+
/**
115+
* Construct a new tree view with given layer tree (root node must not be NULLPTR).
116+
* The root node is not transferred by the view.
117+
*/
118+
explicit QgsElevationProfileLayerTreeView( QgsLayerTree *rootNode, QWidget *parent = nullptr );
119+
120+
/**
121+
* Converts a view \a index to a map layer.
122+
*/
123+
QgsMapLayer *indexToLayer( const QModelIndex &index );
124+
125+
protected:
126+
127+
void resizeEvent( QResizeEvent *event ) override;
128+
129+
private:
130+
131+
QgsElevationProfileLayerTreeModel *mModel = nullptr;
132+
QgsElevationProfileLayerTreeProxyModel *mProxyModel = nullptr;
133+
QgsLayerTree *mLayerTree = nullptr;
134+
135+
};
136+
137+
138+
#endif // QGSELEVATIONPROFILELAYERTREEVIEW_H

0 commit comments

Comments
 (0)
Please sign in to comment.