Skip to content
Permalink
Browse files
Merge pull request #46722 from nirvn/label_alg
Brand new extent widget's calculate extent from layout map and bookmark functionality
  • Loading branch information
nirvn committed Jan 6, 2022
2 parents 1c096ce + 678f95e commit 6b6f5eb9ad8b1709ec37869bc9fb83d10de0fae2
Showing with 213 additions and 73 deletions.
  1. +108 −8 src/gui/qgsextentwidget.cpp
  2. +9 −0 src/gui/qgsextentwidget.h
  3. +1 −1 src/gui/qgsscalewidget.cpp
  4. +95 −64 src/ui/qgsextentgroupboxwidget.ui
@@ -15,13 +15,19 @@

#include "qgsextentwidget.h"

#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgscoordinatetransform.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayermodel.h"
#include "qgsexception.h"
#include "qgsproject.h"
#include "qgsdoublevalidator.h"
#include "qgslayoutmanager.h"
#include "qgslayoutitemmap.h"
#include "qgsprintlayout.h"
#include "qgsbookmarkmodel.h"
#include "qgsreferencedgeometry.h"

#include <QMenu>
#include <QAction>
@@ -47,17 +53,35 @@ QgsExtentWidget::QgsExtentWidget( QWidget *parent, WidgetStyle style )
connect( mLayerMenu, &QMenu::aboutToShow, this, &QgsExtentWidget::layerMenuAboutToShow );
mMapLayerModel = new QgsMapLayerModel( this );

mLayoutMenu = new QMenu( tr( "Calculate from Layout Map" ), this );
mButtonCalcFromLayout->setMenu( mLayoutMenu );
connect( mLayoutMenu, &QMenu::aboutToShow, this, &QgsExtentWidget::layoutMenuAboutToShow );

mBookmarkMenu = new QMenu( tr( "Calculate from Bookmark" ), this );
mButtonCalcFromBookmark->setMenu( mBookmarkMenu );
connect( mBookmarkMenu, &QMenu::aboutToShow, this, &QgsExtentWidget::bookmarkMenuAboutToShow );

mMenu = new QMenu( this );
mUseCanvasExtentAction = new QAction( tr( "Use Map Canvas Extent" ), this );
mUseCanvasExtentAction = new QAction( tr( "Use Current Map Canvas Extent" ), this );
mUseCanvasExtentAction->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapIdentification.svg" ) ) );
mUseCanvasExtentAction->setVisible( false );
connect( mUseCanvasExtentAction, &QAction::triggered, this, &QgsExtentWidget::setOutputExtentFromCurrent );

mUseCurrentExtentAction = new QAction( tr( "Use Current Layer Extent" ), this );
mUseCurrentExtentAction = new QAction( tr( "Use Current Layer/Default Extent" ), this );
mUseCurrentExtentAction->setVisible( false );
connect( mUseCurrentExtentAction, &QAction::triggered, this, &QgsExtentWidget::setOutputExtentFromCurrent );

mDrawOnCanvasAction = new QAction( tr( "Draw on Canvas" ), this );
mDrawOnCanvasAction = new QAction( tr( "Draw on Map Canvas" ), this );
mDrawOnCanvasAction->setVisible( false );
connect( mDrawOnCanvasAction, &QAction::triggered, this, &QgsExtentWidget::setOutputExtentFromDrawOnCanvas );

mMenu->addMenu( mLayerMenu );
mMenu->addMenu( mLayoutMenu );
mMenu->addMenu( mBookmarkMenu );
mMenu->addSeparator();
mMenu->addAction( mUseCanvasExtentAction );
mMenu->addAction( mDrawOnCanvasAction );
mMenu->addAction( mUseCurrentExtentAction );

mCondensedToolButton->setMenu( mMenu );
mCondensedToolButton->setPopupMode( QToolButton::InstantPopup );
@@ -115,7 +139,7 @@ void QgsExtentWidget::setCurrentExtent( const QgsRectangle &currentExtent, const
mCurrentCrs = currentCrs;

mCurrentExtentButton->setVisible( true );
mMenu->addAction( mUseCurrentExtentAction );
mUseCurrentExtentAction->setVisible( true );
}

void QgsExtentWidget::setOutputCrs( const QgsCoordinateReferenceSystem &outputCrs )
@@ -336,6 +360,72 @@ void QgsExtentWidget::layerMenuAboutToShow()
}
}

void QgsExtentWidget::layoutMenuAboutToShow()
{
mLayoutMenu->clear();

if ( QgsLayoutManager *manager = QgsProject::instance()->layoutManager() )
{
const QList<QgsPrintLayout *> layouts = manager->printLayouts();
for ( const QgsPrintLayout *layout : layouts )
{
QList< QgsLayoutItemMap * > maps;
layout->layoutItems( maps );
if ( maps.empty() )
continue;

QMenu *layoutMenu = new QMenu( layout->name(), mMenu );
for ( const QgsLayoutItemMap *map : std::as_const( maps ) )
{
QgsRectangle extent = map->extent();
QgsCoordinateReferenceSystem crs = map->crs();
QAction *mapExtentAction = new QAction( tr( "%1" ).arg( map->displayName() ), mLayoutMenu );
connect( mapExtentAction, &QAction::triggered, this, [this, extent, crs] { setOutputExtentFromUser( extent, crs ); } );
layoutMenu->addAction( mapExtentAction );
}
mLayoutMenu->addMenu( layoutMenu );
}
}
}

void QgsExtentWidget::bookmarkMenuAboutToShow()
{
mBookmarkMenu->clear();

if ( !mBookmarkModel )
mBookmarkModel = new QgsBookmarkManagerProxyModel( QgsApplication::bookmarkManager(), QgsProject::instance()->bookmarkManager(), this );

QMap< QString, QMenu * > groupMenus;
for ( int i = 0; i < mBookmarkModel->rowCount(); ++i )
{
const QString group = mBookmarkModel->data( mBookmarkModel->index( i, 0 ), QgsBookmarkManagerModel::RoleGroup ).toString();
QMenu *destMenu = mBookmarkMenu;
if ( !group.isEmpty() )
{
destMenu = groupMenus.value( group );
if ( !destMenu )
{
destMenu = new QMenu( group, mBookmarkMenu );
groupMenus[ group ] = destMenu;
}
}
QAction *action = new QAction( mBookmarkModel->data( mBookmarkModel->index( i, 0 ), QgsBookmarkManagerModel::RoleName ).toString(), mBookmarkMenu );
const QgsReferencedRectangle extent = mBookmarkModel->data( mBookmarkModel->index( i, 0 ), QgsBookmarkManagerModel::RoleExtent ).value< QgsReferencedRectangle >();
connect( action, &QAction::triggered, this, [ = ] { setOutputExtentFromUser( extent, extent.crs() ); } );
destMenu->addAction( action );
}

QStringList groupKeys = groupMenus.keys();
groupKeys.sort( Qt::CaseInsensitive );
for ( int i = 0; i < groupKeys.count(); ++i )
{
if ( mBookmarkMenu->actions().value( i ) )
mBookmarkMenu->insertMenu( mBookmarkMenu->actions().at( i ), groupMenus.value( groupKeys.at( i ) ) );
else
mBookmarkMenu->addMenu( groupMenus.value( groupKeys.at( i ) ) );
}
}

void QgsExtentWidget::setExtentToLayerExtent( const QString &layerId )
{
QgsMapLayer *layer = QgsProject::instance()->mapLayer( layerId );
@@ -442,16 +532,26 @@ void QgsExtentWidget::setMapCanvas( QgsMapCanvas *canvas, bool drawOnCanvasOptio
mButtonDrawOnCanvas->setVisible( drawOnCanvasOption );
mCurrentExtentButton->setVisible( true );

mMenu->addAction( mUseCanvasExtentAction );
mUseCanvasExtentAction->setVisible( true );
if ( drawOnCanvasOption )
mMenu->addAction( mDrawOnCanvasAction );
mDrawOnCanvasAction->setVisible( true );

mCondensedToolButton->setToolTip( tr( "Set to current map canvas extent" ) );
mCondensedToolButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapIdentification.svg" ) ) );
connect( mCondensedToolButton, &QAbstractButton::clicked, this, &QgsExtentWidget::setOutputExtentFromCurrent );
mCondensedToolButton->setPopupMode( QToolButton::MenuButtonPopup );
}
else
{
mButtonDrawOnCanvas->setVisible( false );
mCurrentExtentButton->setVisible( false );
mMenu->removeAction( mUseCanvasExtentAction );
mMenu->removeAction( mDrawOnCanvasAction );
mUseCanvasExtentAction->setVisible( false );
mUseCanvasExtentAction->setVisible( false );

mCondensedToolButton->setToolTip( QString() );
mCondensedToolButton->setIcon( QIcon() );
disconnect( mCondensedToolButton, &QAbstractButton::clicked, this, &QgsExtentWidget::setOutputExtentFromCurrent );
mCondensedToolButton->setPopupMode( QToolButton::InstantPopup );
}
}

@@ -30,6 +30,7 @@
#include <memory>
#include <QRegularExpression>

class QgsBookmarkManagerProxyModel;
class QgsCoordinateReferenceSystem;
class QgsMapLayerModel;
class QgsMapLayer;
@@ -244,6 +245,8 @@ class GUI_EXPORT QgsExtentWidget : public QWidget, private Ui::QgsExtentGroupBox
private slots:

void layerMenuAboutToShow();
void layoutMenuAboutToShow();
void bookmarkMenuAboutToShow();

void extentDrawn( const QgsRectangle &extent );
void mapToolDeactivated();
@@ -264,8 +267,14 @@ class GUI_EXPORT QgsExtentWidget : public QWidget, private Ui::QgsExtentGroupBox
QgsCoordinateReferenceSystem mOriginalCrs;

QMenu *mMenu = nullptr;

QMenu *mLayerMenu = nullptr;
QMenu *mLayoutMenu = nullptr;
QMenu *mBookmarkMenu = nullptr;

QgsMapLayerModel *mMapLayerModel = nullptr;
QgsBookmarkManagerProxyModel *mBookmarkModel = nullptr;

QList< QAction * > mLayerMenuActions;
QAction *mUseCanvasExtentAction = nullptr;
QAction *mUseCurrentExtentAction = nullptr;
@@ -102,7 +102,7 @@ void QgsScaleWidget::menuAboutToShow()

double scale = mCanvas->scale();
QAction *canvasScaleAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionMapIdentification.svg" ) ),
tr( "Current Canvas Scale (1:%1)" ).arg( qgsDoubleToString( scale, 0 ) ), mMenu );
tr( "Use Current Map Canvas Scale (1:%1)" ).arg( qgsDoubleToString( scale, 0 ) ), mMenu );
connect( canvasScaleAction, &QAction::triggered, this, [this, scale] { setScale( scale ); } );
mMenu->addAction( canvasScaleAction );

@@ -114,46 +114,7 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item row="2" column="11">
<widget class="QPushButton" name="mButtonDrawOnCanvas">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Draw on Canvas</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="mOriginalExtentButton">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Current Layer Extent</string>
</property>
</widget>
</item>
<item row="2" column="6">
<widget class="QPushButton" name="mCurrentExtentButton">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Map Canvas Extent</string>
</property>
</widget>
</item>
<item row="2" column="1">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -166,19 +127,6 @@
</property>
</spacer>
</item>
<item row="2" column="5">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="10">
<spacer name="horizontalSpacer_2">
<property name="orientation">
@@ -192,18 +140,101 @@
</property>
</spacer>
</item>
<item row="2" column="3">
<widget class="QPushButton" name="mButtonCalcFromLayer">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Calculate from Layer</string>
</property>
</widget>
<item row="2" column="1" colspan="7">
<layout class="QHBoxLayout" name="horizontalLayout1">
<item>
<widget class="QLabel" name="calculateLabel">
<property name="text">
<string>Calculate from</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="mButtonCalcFromLayer">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Layer</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="mButtonCalcFromLayout">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Layout Map</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="mButtonCalcFromBookmark">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Bookmark</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="1" colspan="7">
<layout class="QHBoxLayout" name="horizontalLayout2">
<item>
<widget class="QPushButton" name="mOriginalExtentButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Current Layer Extent</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="mCurrentExtentButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Map Canvas Extent</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="mButtonDrawOnCanvas">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Draw on Canvas</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>

0 comments on commit 6b6f5eb

Please sign in to comment.