27 #include <QDoubleValidator>
28 #include <QRegularExpression>
34 connect( mXMinLineEdit, &QLineEdit::textEdited,
this, &QgsExtentWidget::setOutputExtentFromLineEdit );
35 connect( mXMaxLineEdit, &QLineEdit::textEdited,
this, &QgsExtentWidget::setOutputExtentFromLineEdit );
36 connect( mYMinLineEdit, &QLineEdit::textEdited,
this, &QgsExtentWidget::setOutputExtentFromLineEdit );
37 connect( mYMaxLineEdit, &QLineEdit::textEdited,
this, &QgsExtentWidget::setOutputExtentFromLineEdit );
39 mCondensedRe = QRegularExpression( QStringLiteral(
"\\s*([\\d\\.\\-]+)\\s*,\\s*([\\d\\.\\-]+)\\s*,\\s*([\\d\\.\\-]+)\\s*,\\s*([\\d\\.\\-]+)\\s*(?:\\[(.*?)\\])?" ) );
40 mCondensedLineEdit->setValidator(
new QRegularExpressionValidator( mCondensedRe,
this ) );
41 mCondensedLineEdit->setShowClearButton(
false );
43 connect( mCondensedLineEdit, &QLineEdit::textEdited,
this, &QgsExtentWidget::setOutputExtentFromCondensedLineEdit );
45 mLayerMenu =
new QMenu( tr(
"Calculate from Layer" ) );
46 mButtonCalcFromLayer->setMenu( mLayerMenu );
47 connect( mLayerMenu, &QMenu::aboutToShow,
this, &QgsExtentWidget::layerMenuAboutToShow );
50 mMenu =
new QMenu(
this );
51 mUseCanvasExtentAction =
new QAction( tr(
"Use Map Canvas Extent" ),
this );
54 mUseCurrentExtentAction =
new QAction( tr(
"Use Current Layer Extent" ),
this );
57 mDrawOnCanvasAction =
new QAction( tr(
"Draw on Canvas" ),
this );
60 mMenu->addMenu( mLayerMenu );
62 mCondensedToolButton->setMenu( mMenu );
63 mCondensedToolButton->setPopupMode( QToolButton::InstantPopup );
65 mXMinLineEdit->setValidator(
new QDoubleValidator(
this ) );
66 mXMaxLineEdit->setValidator(
new QDoubleValidator(
this ) );
67 mYMinLineEdit->setValidator(
new QDoubleValidator(
this ) );
68 mYMaxLineEdit->setValidator(
new QDoubleValidator(
this ) );
70 mOriginalExtentButton->setVisible(
false );
71 mButtonDrawOnCanvas->setVisible(
false );
72 mCurrentExtentButton->setVisible(
false );
81 mExpandedWidget->hide();
85 mCondensedFrame->hide();
89 setAcceptDrops(
true );
97 mOriginalExtentButton->setVisible(
true );
106 mCurrentExtentButton->setVisible(
true );
107 mMenu->addAction( mUseCurrentExtentAction );
112 mHasFixedOutputCrs =
true;
115 bool prevExtentEnabled = mIsValid;
116 switch ( mExtentState )
154 if ( !prevExtentEnabled )
162 if ( !mHasFixedOutputCrs )
169 if ( mOutputCrs == srcCrs )
178 extent = ct.transformBoundingBox( r );
206 mXMinLineEdit->setText( QString::number( extent.
xMinimum(),
'f', decimals ) );
207 mXMaxLineEdit->setText( QString::number( extent.
xMaximum(),
'f', decimals ) );
208 mYMinLineEdit->setText( QString::number( extent.
yMinimum(),
'f', decimals ) );
209 mYMaxLineEdit->setText( QString::number( extent.
yMaximum(),
'f', decimals ) );
211 QString condensed = QStringLiteral(
"%1,%2,%3,%4" ).arg( mXMinLineEdit->text(),
212 mXMaxLineEdit->text(),
213 mYMinLineEdit->text(),
214 mYMaxLineEdit->text() );
216 mCondensedLineEdit->setText( condensed );
218 mExtentState = state;
226 void QgsExtentWidget::setOutputExtentFromLineEdit()
232 void QgsExtentWidget::setOutputExtentFromCondensedLineEdit()
234 const QString text = mCondensedLineEdit->text();
235 if ( text.isEmpty() )
241 const QRegularExpressionMatch match = mCondensedRe.match( text );
242 if ( match.hasMatch() )
244 whileBlocking( mXMinLineEdit )->setText( match.captured( 1 ) );
245 whileBlocking( mXMaxLineEdit )->setText( match.captured( 2 ) );
246 whileBlocking( mYMinLineEdit )->setText( match.captured( 3 ) );
247 whileBlocking( mYMaxLineEdit )->setText( match.captured( 4 ) );
248 if ( !match.captured( 5 ).isEmpty() )
262 bool prevWasNull = mIsValid;
277 return mExtentLayerName;
287 mCondensedLineEdit->setShowClearButton( allowed );
288 mCondensedLineEdit->setNullValue( notSetText );
291 void QgsExtentWidget::setValid(
bool valid )
293 if ( valid == mIsValid )
300 void QgsExtentWidget::layerMenuAboutToShow()
302 qDeleteAll( mLayerMenuActions );
303 mLayerMenuActions.clear();
305 for (
int i = 0; i < mMapLayerModel->
rowCount(); ++i )
307 QModelIndex index = mMapLayerModel->
index( i, 0 );
308 QIcon icon = qvariant_cast<QIcon>( mMapLayerModel->
data( index, Qt::DecorationRole ) );
309 QString text = mMapLayerModel->
data( index, Qt::DisplayRole ).toString();
310 QAction *act =
new QAction( icon, text, mLayerMenu );
311 act->setToolTip( mMapLayerModel->
data( index, Qt::ToolTipRole ).toString() );
313 if ( mExtentState ==
ProjectLayerExtent && mExtentLayer && mExtentLayer->id() == layerId )
315 act->setCheckable(
true );
316 act->setChecked(
true );
318 connect( act, &QAction::triggered,
this, [
this, layerId]
320 setExtentToLayerExtent( layerId );
322 mLayerMenu->addAction( act );
323 mLayerMenuActions << act;
327 void QgsExtentWidget::setExtentToLayerExtent(
const QString &layerId )
336 QgsMapLayer *QgsExtentWidget::mapLayerFromMimeData(
const QMimeData *data )
const
361 setOutputExtent( mCurrentExtent, mCurrentCrs,
CurrentExtent );
381 mExtentLayer = layer;
382 mExtentLayerName = layer->
name();
391 mMapToolPrevious = mCanvas->
mapTool();
392 if ( !mMapToolExtent )
398 emit toggleDialogVisibility( true );
399 mMapToolPrevious = nullptr;
402 mMapToolExtent->setRatio( mRatio );
409 void QgsExtentWidget::extentDrawn(
const QgsRectangle &extent )
414 mMapToolPrevious =
nullptr;
419 return QgsRectangle( mXMinLineEdit->text().toDouble(), mYMinLineEdit->text().toDouble(),
420 mXMaxLineEdit->text().toDouble(), mYMaxLineEdit->text().toDouble() );
428 mButtonDrawOnCanvas->setVisible(
true );
429 mCurrentExtentButton->setVisible(
true );
431 mMenu->addAction( mUseCanvasExtentAction );
432 mMenu->addAction( mDrawOnCanvasAction );
436 mButtonDrawOnCanvas->setVisible(
false );
437 mCurrentExtentButton->setVisible(
false );
438 mMenu->removeAction( mUseCanvasExtentAction );
439 mMenu->removeAction( mDrawOnCanvasAction );
445 if ( !( event->possibleActions() & Qt::CopyAction ) )
451 if ( mapLayerFromMimeData( event->mimeData() ) )
454 event->setDropAction( Qt::CopyAction );
456 mCondensedLineEdit->setHighlighted(
true );
467 if ( mCondensedLineEdit->isHighlighted() )
470 mCondensedLineEdit->setHighlighted(
false );
481 if ( !( event->possibleActions() & Qt::CopyAction ) )
487 if (
QgsMapLayer *layer = mapLayerFromMimeData( event->mimeData() ) )
490 setFocus( Qt::MouseFocusReason );
491 event->setDropAction( Qt::CopyAction );
500 mCondensedLineEdit->setHighlighted(
false );