28 #include <QDoubleValidator>
29 #include <QFileDialog>
31 #include <QImageReader>
32 #include <QMessageBox>
34 #include <QProgressDialog>
35 #include <QSvgRenderer>
59 connect( mPictureRotationSpinBox,
static_cast < void ( QDoubleSpinBox::* )(
double )
> ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutPictureWidget::mPictureRotationSpinBox_valueChanged );
60 connect( mRotationFromComposerMapCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutPictureWidget::mRotationFromComposerMapCheckBox_stateChanged );
61 connect( mResizeModeComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsLayoutPictureWidget::mResizeModeComboBox_currentIndexChanged );
62 connect( mAnchorPointComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsLayoutPictureWidget::mAnchorPointComboBox_currentIndexChanged );
65 connect( mStrokeWidthSpinBox,
static_cast < void ( QDoubleSpinBox::* )(
double )
> ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutPictureWidget::mStrokeWidthSpinBox_valueChanged );
66 connect( mPictureRotationOffsetSpinBox,
static_cast < void ( QDoubleSpinBox::* )(
double )
> ( &QDoubleSpinBox::valueChanged ),
this, &QgsLayoutPictureWidget::mPictureRotationOffsetSpinBox_valueChanged );
67 connect( mNorthTypeComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
this, &QgsLayoutPictureWidget::mNorthTypeComboBox_currentIndexChanged );
68 connect( mRadioSVG, &QRadioButton::toggled,
this, &QgsLayoutPictureWidget::modeChanged );
69 connect( mRadioRaster, &QRadioButton::toggled,
this, &QgsLayoutPictureWidget::modeChanged );
73 mSvgSourceLineEdit->setLastPathSettingsKey( QStringLiteral(
"/UI/lastComposerPictureDir" ) );
77 mFillColorButton->setAllowOpacity(
true );
78 mFillColorButton->setColorDialogTitle( tr(
"Select Fill Color" ) );
79 mFillColorButton->setContext( QStringLiteral(
"composer" ) );
80 mStrokeColorButton->setAllowOpacity(
true );
81 mStrokeColorButton->setColorDialogTitle( tr(
"Select Stroke Color" ) );
82 mStrokeColorButton->setContext( QStringLiteral(
"composer" ) );
84 mFillColorDDBtn->registerLinkedWidget( mFillColorButton );
85 mStrokeColorDDBtn->registerLinkedWidget( mStrokeColorButton );
87 mNorthTypeComboBox->blockSignals(
true );
90 mNorthTypeComboBox->blockSignals(
false );
91 mPictureRotationOffsetSpinBox->setClearValue( 0.0 );
92 mPictureRotationSpinBox->setClearValue( 0.0 );
94 viewGroups->setHeaderHidden(
true );
95 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
96 mIconSize = std::max( 30,
static_cast< int >( std::round(
Qgis::UI_SCALE_FACTOR * fontMetrics().width(
'X' ) * 4 ) ) );
98 mIconSize = std::max( 30,
static_cast< int >( std::round(
Qgis::UI_SCALE_FACTOR * fontMetrics().horizontalAdvance(
'X' ) * 4 ) ) );
100 viewImages->setGridSize( QSize( mIconSize * 1.2, mIconSize * 1.2 ) );
103 connect( viewImages->selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsLayoutPictureWidget::setSvgName );
104 connect( viewGroups->selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsLayoutPictureWidget::populateIcons );
109 mainLayout->addWidget( mItemPropertiesWidget );
111 if ( mPicture->layout() )
113 mComposerMapComboBox->setCurrentLayout( mPicture->layout() );
118 setGuiElementValues();
124 mSourceDDBtn->registerEnabledWidget( mImageSourceLineEdit,
false );
125 mSourceDDBtn->registerEnabledWidget( mSvgSourceLineEdit,
false );
132 updatePictureTypeWidgets();
137 if ( mItemPropertiesWidget )
141 void QgsLayoutPictureWidget::mPictureRotationSpinBox_valueChanged(
double d )
146 mPicture->setPictureRotation( d );
147 mPicture->endCommand();
151 void QgsLayoutPictureWidget::mResizeModeComboBox_currentIndexChanged(
int )
158 mPicture->beginCommand( tr(
"Change Resize Mode" ) );
160 mPicture->endCommand();
170 mAnchorPointComboBox->setEnabled(
true );
174 mAnchorPointComboBox->setEnabled(
false );
178 void QgsLayoutPictureWidget::mAnchorPointComboBox_currentIndexChanged(
int )
185 mPicture->beginCommand( tr(
"Change Placement" ) );
187 mPicture->endCommand();
201 mPicture = qobject_cast< QgsLayoutItemPicture * >( item );
202 mItemPropertiesWidget->
setItem( mPicture );
210 setGuiElementValues();
215 void QgsLayoutPictureWidget::mRotationFromComposerMapCheckBox_stateChanged(
int state )
222 mPicture->beginCommand( tr(
"Toggle Rotation Sync" ) );
223 if ( state == Qt::Unchecked )
225 mPicture->setLinkedMap(
nullptr );
226 mPictureRotationSpinBox->setEnabled(
true );
227 mComposerMapComboBox->setEnabled(
false );
228 mNorthTypeComboBox->setEnabled(
false );
229 mPictureRotationOffsetSpinBox->setEnabled(
false );
230 mPicture->setPictureRotation( mPictureRotationSpinBox->value() );
234 QgsLayoutItemMap *map = qobject_cast< QgsLayoutItemMap * >( mComposerMapComboBox->currentItem() );
235 mPicture->setLinkedMap( map );
236 mPictureRotationSpinBox->setEnabled(
false );
237 mNorthTypeComboBox->setEnabled(
true );
238 mPictureRotationOffsetSpinBox->setEnabled(
true );
239 mComposerMapComboBox->setEnabled(
true );
241 mPicture->endCommand();
244 void QgsLayoutPictureWidget::mapChanged(
QgsLayoutItem *item )
252 const QgsLayout *layout = mPicture->layout();
264 mPicture->beginCommand( tr(
"Change Rotation Map" ) );
265 mPicture->setLinkedMap( map );
267 mPicture->endCommand();
270 void QgsLayoutPictureWidget::setPicRotationSpinValue(
double r )
272 mPictureRotationSpinBox->blockSignals(
true );
273 mPictureRotationSpinBox->setValue( r );
274 mPictureRotationSpinBox->blockSignals(
false );
277 void QgsLayoutPictureWidget::setGuiElementValues()
282 mPictureRotationSpinBox->blockSignals(
true );
283 mComposerMapComboBox->blockSignals(
true );
284 mRotationFromComposerMapCheckBox->blockSignals(
true );
285 mNorthTypeComboBox->blockSignals(
true );
286 mPictureRotationOffsetSpinBox->blockSignals(
true );
287 mResizeModeComboBox->blockSignals(
true );
288 mAnchorPointComboBox->blockSignals(
true );
289 mFillColorButton->blockSignals(
true );
290 mStrokeColorButton->blockSignals(
true );
291 mStrokeWidthSpinBox->blockSignals(
true );
293 mPictureRotationSpinBox->setValue( mPicture->pictureRotation() );
295 mComposerMapComboBox->setItem( mPicture->linkedMap() );
297 if ( mPicture->linkedMap() )
299 mRotationFromComposerMapCheckBox->setCheckState( Qt::Checked );
300 mPictureRotationSpinBox->setEnabled(
false );
301 mComposerMapComboBox->setEnabled(
true );
302 mNorthTypeComboBox->setEnabled(
true );
303 mPictureRotationOffsetSpinBox->setEnabled(
true );
307 mRotationFromComposerMapCheckBox->setCheckState( Qt::Unchecked );
308 mPictureRotationSpinBox->setEnabled(
true );
309 mComposerMapComboBox->setEnabled(
false );
310 mNorthTypeComboBox->setEnabled(
false );
311 mPictureRotationOffsetSpinBox->setEnabled(
false );
313 mNorthTypeComboBox->setCurrentIndex( mNorthTypeComboBox->findData( mPicture->northMode() ) );
314 mPictureRotationOffsetSpinBox->setValue( mPicture->northOffset() );
316 mResizeModeComboBox->setCurrentIndex( mResizeModeComboBox->findData( mPicture->resizeMode() ) );
321 mAnchorPointComboBox->setCurrentIndex( mAnchorPointComboBox->findData( mPicture->pictureAnchor() ) );
326 mAnchorPointComboBox->setEnabled(
true );
330 mAnchorPointComboBox->setEnabled(
false );
335 updatePictureTypeWidgets();
337 if ( mRadioSVG->isChecked() )
339 whileBlocking( mSvgSourceLineEdit )->setSource( mPicture->picturePath() );
341 mBlockSvgModelChanges++;
342 QAbstractItemModel *m = viewImages->model();
343 QItemSelectionModel *selModel = viewImages->selectionModel();
344 for (
int i = 0; i < m->rowCount(); i++ )
346 QModelIndex idx( m->index( i, 0 ) );
347 if ( m->data( idx ).toString() == mPicture->picturePath() )
349 selModel->select( idx, QItemSelectionModel::SelectCurrent );
350 selModel->setCurrentIndex( idx, QItemSelectionModel::SelectCurrent );
354 mBlockSvgModelChanges--;
356 else if ( mRadioRaster->isChecked() )
358 whileBlocking( mImageSourceLineEdit )->setSource( mPicture->picturePath() );
361 updateSvgParamGui(
false );
362 mFillColorButton->setColor( mPicture->svgFillColor() );
363 mStrokeColorButton->setColor( mPicture->svgStrokeColor() );
364 mStrokeWidthSpinBox->setValue( mPicture->svgStrokeWidth() );
366 mRotationFromComposerMapCheckBox->blockSignals(
false );
367 mPictureRotationSpinBox->blockSignals(
false );
368 mComposerMapComboBox->blockSignals(
false );
369 mNorthTypeComboBox->blockSignals(
false );
370 mPictureRotationOffsetSpinBox->blockSignals(
false );
371 mResizeModeComboBox->blockSignals(
false );
372 mAnchorPointComboBox->blockSignals(
false );
373 mFillColorButton->blockSignals(
false );
374 mStrokeColorButton->blockSignals(
false );
375 mStrokeWidthSpinBox->blockSignals(
false );
381 void QgsLayoutPictureWidget::updateSvgParamGui(
bool resetValues )
386 QString picturePath = mPicture->picturePath();
389 bool hasFillParam, hasFillOpacityParam, hasStrokeParam, hasStrokeWidthParam, hasStrokeOpacityParam;
390 QColor defaultFill, defaultStroke;
391 double defaultStrokeWidth, defaultFillOpacity, defaultStrokeOpacity;
392 bool hasDefaultFillColor, hasDefaultFillOpacity, hasDefaultStrokeColor, hasDefaultStrokeWidth, hasDefaultStrokeOpacity;
394 hasFillOpacityParam, hasDefaultFillOpacity, defaultFillOpacity,
395 hasStrokeParam, hasDefaultStrokeColor, defaultStroke,
396 hasStrokeWidthParam, hasDefaultStrokeWidth, defaultStrokeWidth,
397 hasStrokeOpacityParam, hasDefaultStrokeOpacity, defaultStrokeOpacity );
401 QColor fill = mFillColorButton->color();
402 double newOpacity = hasFillOpacityParam ? fill.alphaF() : 1.0;
403 if ( hasDefaultFillColor )
407 fill.setAlphaF( hasDefaultFillOpacity ? defaultFillOpacity : newOpacity );
408 mFillColorButton->setColor( fill );
410 mFillColorButton->setEnabled( hasFillParam );
411 mFillColorButton->setAllowOpacity( hasFillOpacityParam );
414 QColor stroke = mStrokeColorButton->color();
415 double newOpacity = hasStrokeOpacityParam ? stroke.alphaF() : 1.0;
416 if ( hasDefaultStrokeColor )
418 stroke = defaultStroke;
420 stroke.setAlphaF( hasDefaultStrokeOpacity ? defaultStrokeOpacity : newOpacity );
421 mStrokeColorButton->setColor( stroke );
423 mStrokeColorButton->setEnabled( hasStrokeParam );
424 mStrokeColorButton->setAllowOpacity( hasStrokeOpacityParam );
425 if ( hasDefaultStrokeWidth && resetValues )
427 mStrokeWidthSpinBox->setValue( defaultStrokeWidth );
429 mStrokeWidthSpinBox->setEnabled( hasStrokeWidthParam );
432 void QgsLayoutPictureWidget::mFillColorButton_colorChanged(
const QColor &color )
435 mPicture->setSvgFillColor( color );
436 mPicture->endCommand();
440 void QgsLayoutPictureWidget::mStrokeColorButton_colorChanged(
const QColor &color )
443 mPicture->setSvgStrokeColor( color );
444 mPicture->endCommand();
448 void QgsLayoutPictureWidget::mStrokeWidthSpinBox_valueChanged(
double d )
451 mPicture->setSvgStrokeWidth( d );
452 mPicture->endCommand();
456 void QgsLayoutPictureWidget::mPictureRotationOffsetSpinBox_valueChanged(
double d )
459 mPicture->setNorthOffset( d );
460 mPicture->endCommand();
464 void QgsLayoutPictureWidget::mNorthTypeComboBox_currentIndexChanged(
int index )
466 mPicture->beginCommand( tr(
"Change Picture North Mode" ) );
468 mPicture->endCommand();
472 void QgsLayoutPictureWidget::modeChanged()
475 if ( mPicture && mPicture->mode() != newFormat )
478 whileBlocking( mImageSourceLineEdit )->setSource( QString() );
479 mPicture->beginCommand( tr(
"Change Picture Type" ) );
480 mPicture->setPicturePath( QString(), newFormat );
481 mPicture->endCommand();
483 updatePictureTypeWidgets();
486 void QgsLayoutPictureWidget::updatePictureTypeWidgets()
488 mRasterFrame->setVisible( mRadioRaster->isChecked() );
489 mSVGFrame->setVisible( mRadioSVG->isChecked() );
490 mSVGParamsGroupBox->setVisible( mRadioSVG->isChecked() );
493 if ( mRadioSVG->isChecked() )
494 mSvgDDBtnFrame->layout()->addWidget( mSourceDDBtn );
496 mRasterDDBtnFrame->layout()->addWidget( mSourceDDBtn );
499 void QgsLayoutPictureWidget::populateList()
501 QAbstractItemModel *oldModel = viewGroups->model();
503 viewGroups->setModel( g );
507 int rows = g->rowCount( g->indexFromItem( g->invisibleRootItem() ) );
508 for (
int i = 0; i < rows; i++ )
510 viewGroups->setExpanded( g->indexFromItem( g->item( i ) ),
true );
514 oldModel = viewImages->model();
516 viewImages->setModel( m );
521 void QgsLayoutPictureWidget::populateIcons(
const QModelIndex &idx )
523 QString path = idx.data( Qt::UserRole + 1 ).toString();
525 QAbstractItemModel *oldModel = viewImages->model();
527 viewImages->setModel( m );
530 connect( viewImages->selectionModel(), &QItemSelectionModel::currentChanged,
this, &QgsLayoutPictureWidget::setSvgName );
533 void QgsLayoutPictureWidget::setSvgName(
const QModelIndex &idx )
535 if ( mBlockSvgModelChanges )
538 QString name = idx.data( Qt::UserRole ).toString();
540 svgSourceChanged( name );
543 void QgsLayoutPictureWidget::svgSourceChanged(
const QString &source )
547 mPicture->beginCommand( tr(
"Change Picture" ) );
550 mPicture->endCommand();
555 void QgsLayoutPictureWidget::rasterSourceChanged(
const QString &source )
559 mPicture->beginCommand( tr(
"Change Picture" ) );
562 mPicture->endCommand();
574 mImageSourceLineEdit->setEnabled( !mSourceDDBtn->isActive() );
575 mSvgSourceLineEdit->setEnabled( !mSourceDDBtn->isActive() );