35#include <QRegularExpression>
41 connect( mXMinLineEdit, &QLineEdit::textEdited,
this, &QgsExtentWidget::setOutputExtentFromLineEdit );
42 connect( mXMaxLineEdit, &QLineEdit::textEdited,
this, &QgsExtentWidget::setOutputExtentFromLineEdit );
43 connect( mYMinLineEdit, &QLineEdit::textEdited,
this, &QgsExtentWidget::setOutputExtentFromLineEdit );
44 connect( mYMaxLineEdit, &QLineEdit::textEdited,
this, &QgsExtentWidget::setOutputExtentFromLineEdit );
46 mCondensedRe = QRegularExpression( QStringLiteral(
"\\s*([\\d\\.\\-]+)\\s*,\\s*([\\d\\.\\-]+)\\s*,\\s*([\\d\\.\\-]+)\\s*,\\s*([\\d\\.\\-]+)\\s*(?:\\[(.*?)\\])?" ) );
47 mCondensedLineEdit->setValidator(
new QRegularExpressionValidator( mCondensedRe,
this ) );
48 mCondensedLineEdit->setShowClearButton(
false );
50 connect( mCondensedLineEdit, &QLineEdit::textEdited,
this, &QgsExtentWidget::setOutputExtentFromCondensedLineEdit );
52 mLayerMenu =
new QMenu( tr(
"Calculate from Layer" ),
this );
53 mButtonCalcFromLayer->setMenu( mLayerMenu );
54 connect( mLayerMenu, &QMenu::aboutToShow,
this, &QgsExtentWidget::layerMenuAboutToShow );
56 mMapLayerModel->
setFilters( QgsMapLayerProxyModel::Filter::SpatialLayer );
58 mLayoutMenu =
new QMenu( tr(
"Calculate from Layout Map" ),
this );
59 mButtonCalcFromLayout->setMenu( mLayoutMenu );
60 connect( mLayoutMenu, &QMenu::aboutToShow,
this, &QgsExtentWidget::layoutMenuAboutToShow );
62 mBookmarkMenu =
new QMenu( tr(
"Calculate from Bookmark" ),
this );
63 mButtonCalcFromBookmark->setMenu( mBookmarkMenu );
64 connect( mBookmarkMenu, &QMenu::aboutToShow,
this, &QgsExtentWidget::bookmarkMenuAboutToShow );
66 mMenu =
new QMenu(
this );
67 mUseCanvasExtentAction =
new QAction( tr(
"Use Current Map Canvas Extent" ),
this );
69 mUseCanvasExtentAction->setVisible(
false );
72 mUseCurrentExtentAction =
new QAction( tr(
"Use Current Layer/Default Extent" ),
this );
73 mUseCurrentExtentAction->setVisible(
false );
76 mDrawOnCanvasAction =
new QAction( tr(
"Draw on Map Canvas" ),
this );
77 mDrawOnCanvasAction->setVisible(
false );
80 mMenu->addMenu( mLayerMenu );
81 mMenu->addMenu( mLayoutMenu );
82 mMenu->addMenu( mBookmarkMenu );
83 mMenu->addSeparator();
84 mMenu->addAction( mUseCanvasExtentAction );
85 mMenu->addAction( mDrawOnCanvasAction );
86 mMenu->addAction( mUseCurrentExtentAction );
88 mCondensedToolButton->setMenu( mMenu );
89 mCondensedToolButton->setPopupMode( QToolButton::InstantPopup );
96 mOriginalExtentButton->setVisible(
false );
97 mButtonDrawOnCanvas->setVisible(
false );
98 mCurrentExtentButton->setVisible(
false );
107 mExpandedWidget->hide();
111 mCondensedFrame->hide();
115 setAcceptDrops(
true );
120 if ( mMapToolExtent )
134 mOriginalExtentButton->setVisible(
true );
143 mCurrentExtentButton->setVisible(
true );
144 mUseCurrentExtentAction->setVisible(
true );
149 mHasFixedOutputCrs =
true;
152 const bool prevExtentEnabled = mIsValid;
153 switch ( mExtentState )
192 if ( !prevExtentEnabled )
200 if ( !mHasFixedOutputCrs )
207 if ( mOutputCrs == srcCrs )
216 ct.setBallparkTransformsAreAppropriate(
true );
217 extent = ct.transformBoundingBox( r );
230 case Qgis::DistanceUnit::Degrees:
231 case Qgis::DistanceUnit::Unknown:
234 case Qgis::DistanceUnit::Meters:
235 case Qgis::DistanceUnit::Kilometers:
236 case Qgis::DistanceUnit::Feet:
237 case Qgis::DistanceUnit::NauticalMiles:
238 case Qgis::DistanceUnit::Yards:
239 case Qgis::DistanceUnit::Miles:
240 case Qgis::DistanceUnit::Centimeters:
241 case Qgis::DistanceUnit::Millimeters:
245 mXMinLineEdit->setText( QLocale().toString( extent.
xMinimum(),
'f', decimals ) );
246 mXMaxLineEdit->setText( QLocale().toString( extent.
xMaximum(),
'f', decimals ) );
247 mYMinLineEdit->setText( QLocale().toString( extent.
yMinimum(),
'f', decimals ) );
248 mYMaxLineEdit->setText( QLocale().toString( extent.
yMaximum(),
'f', decimals ) );
250 QString condensed = QStringLiteral(
"%1,%2,%3,%4" ).arg( QString::number( extent.
xMinimum(),
'f', decimals ),
251 QString::number( extent.
xMaximum(),
'f', decimals ),
252 QString::number( extent.
yMinimum(),
'f', decimals ),
253 QString::number( extent.
yMaximum(),
'f', decimals ) );
255 mCondensedLineEdit->setText( condensed );
257 mExtentState = state;
265void QgsExtentWidget::setOutputExtentFromLineEdit()
271void QgsExtentWidget::setOutputExtentFromCondensedLineEdit()
273 const QString text = mCondensedLineEdit->text();
274 if ( text.isEmpty() )
280 const QRegularExpressionMatch match = mCondensedRe.match( text );
281 if ( match.hasMatch() )
284 whileBlocking( mXMinLineEdit )->setText( QLocale().toString( match.captured( 1 ).toDouble(),
'f', 10 ) );
285 whileBlocking( mXMaxLineEdit )->setText( QLocale().toString( match.captured( 2 ).toDouble(),
'f', 10 ) );
286 whileBlocking( mYMinLineEdit )->setText( QLocale().toString( match.captured( 3 ).toDouble(),
'f', 10 ) );
287 whileBlocking( mYMaxLineEdit )->setText( QLocale().toString( match.captured( 4 ).toDouble(),
'f', 10 ) );
288 if ( !match.captured( 5 ).isEmpty() )
302 const bool prevWasNull = mIsValid;
317 return mExtentLayerName;
327 mCondensedLineEdit->setShowClearButton( allowed );
328 mCondensedLineEdit->setNullValue( notSetText );
331void QgsExtentWidget::setValid(
bool valid )
333 if ( valid == mIsValid )
340void QgsExtentWidget::layerMenuAboutToShow()
342 qDeleteAll( mLayerMenuActions );
343 mLayerMenuActions.clear();
345 for (
int i = 0; i < mMapLayerModel->rowCount(); ++i )
347 const QModelIndex index = mMapLayerModel->index( i, 0 );
348 const QIcon icon = qvariant_cast<QIcon>( mMapLayerModel->data( index, Qt::DecorationRole ) );
349 const QString text = mMapLayerModel->data( index, Qt::DisplayRole ).toString();
350 QAction *act =
new QAction( icon, text, mLayerMenu );
351 act->setToolTip( mMapLayerModel->data( index, Qt::ToolTipRole ).toString() );
353 if ( mExtentState ==
ProjectLayerExtent && mExtentLayer && mExtentLayer->id() == layerId )
355 act->setCheckable(
true );
356 act->setChecked(
true );
358 connect( act, &QAction::triggered,
this, [
this, layerId]
360 setExtentToLayerExtent( layerId );
362 mLayerMenu->addAction( act );
363 mLayerMenuActions << act;
367void QgsExtentWidget::layoutMenuAboutToShow()
369 mLayoutMenu->clear();
373 const QList<QgsPrintLayout *> layouts = manager->printLayouts();
376 QList< QgsLayoutItemMap * > maps;
377 layout->layoutItems( maps );
381 QMenu *layoutMenu =
new QMenu( layout->name(), mMenu );
386 QAction *mapExtentAction =
new QAction( tr(
"%1" ).arg( map->displayName() ), mLayoutMenu );
388 layoutMenu->addAction( mapExtentAction );
390 mLayoutMenu->addMenu( layoutMenu );
395void QgsExtentWidget::bookmarkMenuAboutToShow()
397 mBookmarkMenu->clear();
399 if ( !mBookmarkModel )
402 QMap< QString, QMenu * > groupMenus;
403 for (
int i = 0; i < mBookmarkModel->rowCount(); ++i )
406 QMenu *destMenu = mBookmarkMenu;
407 if ( !group.isEmpty() )
409 destMenu = groupMenus.value( group );
412 destMenu =
new QMenu( group, mBookmarkMenu );
413 groupMenus[ group ] = destMenu;
419 destMenu->addAction( action );
422 QStringList groupKeys = groupMenus.keys();
423 groupKeys.sort( Qt::CaseInsensitive );
424 for (
int i = 0; i < groupKeys.count(); ++i )
426 if ( mBookmarkMenu->actions().value( i ) )
427 mBookmarkMenu->insertMenu( mBookmarkMenu->actions().at( i ), groupMenus.value( groupKeys.at( i ) ) );
429 mBookmarkMenu->addMenu( groupMenus.value( groupKeys.at( i ) ) );
433void QgsExtentWidget::setExtentToLayerExtent(
const QString &layerId )
442QgsMapLayer *QgsExtentWidget::mapLayerFromMimeData(
const QMimeData *data )
const
467 setOutputExtent( mCurrentExtent, mCurrentCrs,
CurrentExtent );
487 mExtentLayer = layer;
488 mExtentLayerName = layer->
name();
497 mMapToolPrevious = mCanvas->
mapTool();
498 if ( !mMapToolExtent )
504 mMapToolExtent->setRatio( mRatio );
511void QgsExtentWidget::extentDrawn(
const QgsRectangle &extent )
516 mMapToolPrevious =
nullptr;
519void QgsExtentWidget::mapToolDeactivated()
522 mMapToolPrevious =
nullptr;
536 mButtonDrawOnCanvas->setVisible( drawOnCanvasOption );
537 mCurrentExtentButton->setVisible(
true );
539 mUseCanvasExtentAction->setVisible(
true );
540 if ( drawOnCanvasOption )
541 mDrawOnCanvasAction->setVisible(
true );
543 mCondensedToolButton->setToolTip( tr(
"Set to current map canvas extent" ) );
546 mCondensedToolButton->setPopupMode( QToolButton::MenuButtonPopup );
550 mButtonDrawOnCanvas->setVisible(
false );
551 mCurrentExtentButton->setVisible(
false );
552 mUseCanvasExtentAction->setVisible(
false );
553 mUseCanvasExtentAction->setVisible(
false );
555 mCondensedToolButton->setToolTip( QString() );
556 mCondensedToolButton->setIcon( QIcon() );
558 mCondensedToolButton->setPopupMode( QToolButton::InstantPopup );
564 if ( !( event->possibleActions() & Qt::CopyAction ) )
570 if ( mapLayerFromMimeData( event->mimeData() ) )
573 event->setDropAction( Qt::CopyAction );
575 mCondensedLineEdit->setHighlighted(
true );
586 if ( mCondensedLineEdit->isHighlighted() )
589 mCondensedLineEdit->setHighlighted(
false );
600 if ( !( event->possibleActions() & Qt::CopyAction ) )
606 if (
QgsMapLayer *layer = mapLayerFromMimeData( event->mimeData() ) )
609 setFocus( Qt::MouseFocusReason );
610 event->setDropAction( Qt::CopyAction );
619 mCondensedLineEdit->setHighlighted(
false );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsBookmarkManager * bookmarkManager()
Returns the application's bookmark manager, used for storing installation-wide bookmarks.
@ RoleExtent
Bookmark extent as a QgsReferencedRectangle.
@ RoleGroup
Bookmark group.
A QSortFilterProxyModel subclass for sorting the entries in a QgsBookmarkManagerModel.
This class represents a coordinate reference system (CRS).
Q_GADGET Qgis::DistanceUnit mapUnits
@ ShortString
A heavily abbreviated string, for use when a compact representation is required.
QString userFriendlyIdentifier(IdentifierType type=MediumString) const
Returns a user friendly identifier for the CRS.
Custom exception class for Coordinate Reference System related exceptions.
QgsDoubleValidator is a QLineEdit Validator that combines QDoubleValidator and QRegularExpressionVali...
static double toDouble(const QString &input, bool *ok)
Converts input string to double value.
void cleared()
Emitted when the widget is cleared.
Layout graphical items for displaying a map.
Manages storage of a set of layouts.
Map canvas is a class for displaying all GIS data types on a canvas.
QgsMapTool * mapTool()
Returns the currently active tool.
void setMapTool(QgsMapTool *mapTool, bool clean=false)
Sets the map tool currently being used on the canvas.
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
@ LayerIdRole
Stores the map layer ID.
The QgsMapLayerProxyModel class provides an easy to use model to display the list of layers in widget...
QgsMapLayerProxyModel * setFilters(QgsMapLayerProxyModel::Filters filters)
Sets filter flags which affect how layers are filtered within the model.
Base class for all map layer types.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
The QgsMapSettings class contains configuration for rendering of the map.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
void setRotation(double rotation)
Sets the rotation of the resulting map image, in degrees clockwise.
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
QList< QgsMimeDataUtils::Uri > UriList
static UriList decodeUriList(const QMimeData *data)
Print layout, a QgsLayout subclass for static or atlas-based layouts.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
A rectangle specified with double values.
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsRectangle with associated coordinate reference system.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
const QgsCoordinateReferenceSystem & outputCrs
const QgsCoordinateReferenceSystem & crs