QGIS API Documentation 4.3.0-Master (91d4a333d5d)
Loading...
Searching...
No Matches
qgslayoutitemwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemwidget.cpp
3 ------------------------
4 Date : July 2017
5 Copyright : (C) 2017 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgslayoutitemwidget.h"
17
19#include "qgsfilterlineedit.h"
20#include "qgsfontbutton.h"
21#include "qgslayout.h"
22#include "qgslayoutatlas.h"
24#include "qgslayoutframe.h"
25#include "qgslayoutmultiframe.h"
27#include "qgslayoutundostack.h"
28#include "qgsprintlayout.h"
29#include "qgsproject.h"
31#include "qgssymbolbutton.h"
32
33#include <QButtonGroup>
34#include <QString>
35
36#include "moc_qgslayoutitemwidget.cpp"
37
38using namespace Qt::StringLiterals;
39
40//
41// QgsLayoutConfigObject
42//
43
45 : QObject( parent )
46 , mLayoutObject( layoutObject )
47{
48 if ( mLayoutObject->layout() )
49 {
50 connect( &mLayoutObject->layout()->reportContext(), &QgsLayoutReportContext::layerChanged, this, [this] { updateDataDefinedButtons(); } );
51 }
52 if ( layoutAtlas() )
53 {
54 connect( layoutAtlas(), &QgsLayoutAtlas::toggled, this, &QgsLayoutConfigObject::updateDataDefinedButtons );
55 }
56}
57
58void QgsLayoutConfigObject::updateDataDefinedProperty()
59{
60 //match data defined button to item's data defined property
61 QgsPropertyOverrideButton *ddButton = qobject_cast<QgsPropertyOverrideButton *>( sender() );
62 if ( !ddButton )
63 {
64 return;
65 }
67
68 if ( ddButton->propertyKey() >= 0 )
69 key = static_cast<QgsLayoutObject::DataDefinedProperty>( ddButton->propertyKey() );
70
72 {
73 return;
74 }
75
76 const bool propertyAssociatesWithMultiFrame = QgsLayoutObject::propertyAssociatesWithParentMultiframe( key );
77
78 auto undoLayoutObject = dynamic_cast<QgsLayoutUndoObjectInterface *>( mLayoutObject.data() );
79 if ( undoLayoutObject )
80 mLayoutObject->layout()->undoStack()->beginCommand( undoLayoutObject, tr( "Change Data Defined Property" ) );
81
82 //set the data defined property and refresh the item
83 if ( propertyAssociatesWithMultiFrame )
84 {
85 if ( QgsLayoutFrame *frame = dynamic_cast<QgsLayoutFrame *>( mLayoutObject.data() ) )
86 {
87 if ( QgsLayoutMultiFrame *multiFrame = frame->multiFrame() )
88 {
89 multiFrame->dataDefinedProperties().setProperty( key, ddButton->toProperty() );
90 multiFrame->refresh();
91 }
92 }
93 else if ( QgsLayoutMultiFrame *multiFrame = dynamic_cast<QgsLayoutMultiFrame *>( mLayoutObject.data() ) )
94 {
95 multiFrame->dataDefinedProperties().setProperty( key, ddButton->toProperty() );
96 multiFrame->refresh();
97 }
98 }
99 else if ( mLayoutObject )
100 {
101 mLayoutObject->dataDefinedProperties().setProperty( key, ddButton->toProperty() );
102 mLayoutObject->refresh();
103 }
104
105 if ( undoLayoutObject )
106 mLayoutObject->layout()->undoStack()->endCommand();
107}
108
109void QgsLayoutConfigObject::updateDataDefinedButtons()
110{
111 const QList<QgsPropertyOverrideButton *> buttons = findChildren<QgsPropertyOverrideButton *>();
112 for ( QgsPropertyOverrideButton *button : buttons )
113 {
114 button->setVectorLayer( coverageLayer() );
115 }
116}
117
119{
120 button->blockSignals( true );
121 button->init( static_cast<int>( key ), mLayoutObject->dataDefinedProperties(), QgsLayoutObject::propertyDefinitions(), coverageLayer() );
122 connect( button, &QgsPropertyOverrideButton::changed, this, &QgsLayoutConfigObject::updateDataDefinedProperty, Qt::UniqueConnection );
123 button->registerExpressionContextGenerator( mLayoutObject );
124 button->blockSignals( false );
125}
126
128{
129 if ( !button )
130 return;
131
132 if ( button->propertyKey() < 0 || !mLayoutObject )
133 return;
134
136 const bool propertyAssociatesWithMultiFrame = QgsLayoutObject::propertyAssociatesWithParentMultiframe( key );
137
138 //set the data defined property
139 if ( propertyAssociatesWithMultiFrame )
140 {
141 if ( QgsLayoutFrame *frame = dynamic_cast<QgsLayoutFrame *>( mLayoutObject.data() ) )
142 {
143 if ( QgsLayoutMultiFrame *multiFrame = frame->multiFrame() )
144 {
145 whileBlocking( button )->setToProperty( multiFrame->dataDefinedProperties().property( key ) );
146 }
147 }
148 else if ( QgsLayoutMultiFrame *multiFrame = dynamic_cast<QgsLayoutMultiFrame *>( mLayoutObject.data() ) )
149 {
150 whileBlocking( button )->setToProperty( multiFrame->dataDefinedProperties().property( key ) );
151 }
152 }
153 else if ( mLayoutObject )
154 {
155 whileBlocking( button )->setToProperty( mLayoutObject->dataDefinedProperties().property( key ) );
156 }
157
158 // In case the button was initialized to a different config object, we need to reconnect to it here (see https://github.com/qgis/QGIS/issues/26582 )
159 connect( button, &QgsPropertyOverrideButton::changed, this, &QgsLayoutConfigObject::updateDataDefinedProperty, Qt::UniqueConnection );
160 button->registerExpressionContextGenerator( mLayoutObject );
161}
162
164{
165 if ( !mLayoutObject )
166 {
167 return nullptr;
168 }
169
170 QgsPrintLayout *printLayout = qobject_cast<QgsPrintLayout *>( mLayoutObject->layout() );
171
172 if ( !printLayout )
173 {
174 return nullptr;
175 }
176
177 return printLayout->atlas();
178}
179
181{
182 mLayoutObject = object;
183}
184
186{
187 if ( !mLayoutObject )
188 return nullptr;
189
190 QgsLayout *layout = mLayoutObject->layout();
191 if ( !layout )
192 return nullptr;
193
194 return layout->reportContext().layer();
195}
196
197
198//
199// QgsLayoutItemBaseWidget
200//
201
203 : QgsPanelWidget( parent )
204 , mConfigObject( new QgsLayoutConfigObject( this, layoutObject ) )
205 , mObject( layoutObject )
206{}
207
212
214{
215 QgsLayoutObject *oldObject = mObject;
216 QgsLayoutConfigObject *oldConfigObject = mConfigObject;
217 // have to set new mObject/mConfigObject here, because setNewItem methods require access to them
218 mObject = item;
219 mConfigObject = new QgsLayoutConfigObject( this, mObject );
220 if ( setNewItem( item ) )
221 {
222 oldConfigObject->deleteLater();
223 return true;
224 }
225 else
226 {
227 // revert object change since it was unsuccessful
228 mObject = oldObject;
229 mConfigObject->deleteLater();
230 mConfigObject = oldConfigObject;
231 return false;
232 }
233}
234
237
239{
240 const auto symbolButtonWidgets = findChildren<QgsSymbolButton *>();
241 for ( QgsSymbolButton *symbolWidget : symbolButtonWidgets )
242 {
243 symbolWidget->setMessageBar( iface->messageBar() );
244 }
245 const auto fontButtonWidgets = findChildren<QgsFontButton *>();
246 for ( QgsFontButton *fontButton : fontButtonWidgets )
247 {
248 fontButton->setMessageBar( iface->messageBar() );
249 }
250}
251
254
256{
257 mConfigObject->initializeDataDefinedButton( button, property );
258}
259
261{
262 mConfigObject->updateDataDefinedButton( button );
263}
264
266{
267 return mConfigObject->coverageLayer();
268}
269
271{
272 return false;
273}
274
276{
277 return mConfigObject->layoutAtlas();
278}
279
280//
281
282
283//QgsLayoutItemPropertiesWidget
284
286{
287 if ( !mItem )
288 return;
289
290 mBlockVariableUpdates = true;
291 QgsExpressionContext context = mItem->createExpressionContext();
292 mVariableEditor->setContext( &context );
293
294 // here, we prefer to make the multiframe's scope the editable one. That's because most expressions are evaluated
295 // on the multiframe subclass level, not on a frame-by-frame basis. Ideally both would be editable, but for now let's go
296 // with the most useful one.
297 const int multiFrameScopeIndex = context.indexOfScope( tr( "Multiframe Item" ) );
298 const int itemScopeIndex = context.indexOfScope( tr( "Layout Item" ) );
299 if ( multiFrameScopeIndex >= 0 )
300 mVariableEditor->setEditableScopeIndex( multiFrameScopeIndex );
301 else if ( itemScopeIndex >= 0 )
302 mVariableEditor->setEditableScopeIndex( itemScopeIndex );
303 mBlockVariableUpdates = false;
304}
305
307 : QWidget( parent )
308 , mConfigObject( new QgsLayoutConfigObject( this, item ) )
309{
310 setupUi( this );
311
312 mVariableEditor->setMinimumHeight( mVariableEditor->fontMetrics().height() * 15 );
313
314 mItemRotationSpinBox->setClearValue( 0 );
315 mStrokeUnitsComboBox->linkToWidget( mStrokeWidthSpinBox );
316 mStrokeUnitsComboBox->setConverter( &item->layout()->renderContext().measurementConverter() );
317
318 QgsFilterLineEdit *exportGroupLineEdit = new QgsFilterLineEdit();
319 exportGroupLineEdit->setShowClearButton( true );
320 exportGroupLineEdit->setPlaceholderText( tr( "Not set" ) );
321 mExportGroupNameCombo->setLineEdit( exportGroupLineEdit );
322
323 mPosUnitsComboBox->linkToWidget( mXPosSpin );
324 mPosUnitsComboBox->linkToWidget( mYPosSpin );
325 mSizeUnitsComboBox->linkToWidget( mWidthSpin );
326 mSizeUnitsComboBox->linkToWidget( mHeightSpin );
327
328 mPosUnitsComboBox->setConverter( &item->layout()->renderContext().measurementConverter() );
329 mSizeUnitsComboBox->setConverter( &item->layout()->renderContext().measurementConverter() );
330
331 mPosLockAspectRatio->setWidthSpinBox( mXPosSpin );
332 mPosLockAspectRatio->setHeightSpinBox( mYPosSpin );
333 mSizeLockAspectRatio->setWidthSpinBox( mWidthSpin );
334 mSizeLockAspectRatio->setHeightSpinBox( mHeightSpin );
335
336 mItemFrameColorDDBtn->registerLinkedWidget( mFrameColorButton );
337 mItemBackgroundColorDDBtn->registerLinkedWidget( mBackgroundColorButton );
338
339 connect( mFrameColorButton, &QgsColorButton::colorChanged, this, &QgsLayoutItemPropertiesWidget::mFrameColorButton_colorChanged );
340 connect( mBackgroundColorButton, &QgsColorButton::colorChanged, this, &QgsLayoutItemPropertiesWidget::mBackgroundColorButton_colorChanged );
341 connect( mStrokeWidthSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutItemPropertiesWidget::mStrokeWidthSpinBox_valueChanged );
342 connect( mStrokeUnitsComboBox, &QgsLayoutUnitsComboBox::unitChanged, this, &QgsLayoutItemPropertiesWidget::strokeUnitChanged );
343 connect( mFrameGroupBox, &QgsCollapsibleGroupBoxBasic::toggled, this, &QgsLayoutItemPropertiesWidget::mFrameGroupBox_toggled );
344 connect( mFrameJoinStyleCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutItemPropertiesWidget::mFrameJoinStyleCombo_currentIndexChanged );
345 connect( mBackgroundGroupBox, &QgsCollapsibleGroupBoxBasic::toggled, this, &QgsLayoutItemPropertiesWidget::mBackgroundGroupBox_toggled );
346 connect( mItemIdLineEdit, &QLineEdit::editingFinished, this, &QgsLayoutItemPropertiesWidget::mItemIdLineEdit_editingFinished );
347 connect( mExportGroupNameCombo, &QComboBox::currentTextChanged, this, &QgsLayoutItemPropertiesWidget::exportGroupNameEditingFinished );
348 connect( mPageSpinBox, static_cast<void ( QSpinBox::* )( int )>( &QSpinBox::valueChanged ), this, &QgsLayoutItemPropertiesWidget::mPageSpinBox_valueChanged );
349 connect( mXPosSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutItemPropertiesWidget::mXPosSpin_valueChanged );
350 connect( mYPosSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutItemPropertiesWidget::mYPosSpin_valueChanged );
351 connect( mPosUnitsComboBox, &QgsLayoutUnitsComboBox::unitChanged, this, &QgsLayoutItemPropertiesWidget::positionUnitsChanged );
352 connect( mWidthSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutItemPropertiesWidget::mWidthSpin_valueChanged );
353 connect( mHeightSpin, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutItemPropertiesWidget::mHeightSpin_valueChanged );
354 connect( mSizeUnitsComboBox, &QgsLayoutUnitsComboBox::unitChanged, this, &QgsLayoutItemPropertiesWidget::sizeUnitsChanged );
355 connect( mUpperLeftRadioButton, &QRadioButton::toggled, this, &QgsLayoutItemPropertiesWidget::mUpperLeftCheckBox_stateChanged );
356 connect( mUpperMiddleRadioButton, &QRadioButton::toggled, this, &QgsLayoutItemPropertiesWidget::mUpperMiddleCheckBox_stateChanged );
357 connect( mUpperRightRadioButton, &QRadioButton::toggled, this, &QgsLayoutItemPropertiesWidget::mUpperRightCheckBox_stateChanged );
358 connect( mMiddleLeftRadioButton, &QRadioButton::toggled, this, &QgsLayoutItemPropertiesWidget::mMiddleLeftCheckBox_stateChanged );
359 connect( mMiddleRadioButton, &QRadioButton::toggled, this, &QgsLayoutItemPropertiesWidget::mMiddleCheckBox_stateChanged );
360 connect( mMiddleRightRadioButton, &QRadioButton::toggled, this, &QgsLayoutItemPropertiesWidget::mMiddleRightCheckBox_stateChanged );
361 connect( mLowerLeftRadioButton, &QRadioButton::toggled, this, &QgsLayoutItemPropertiesWidget::mLowerLeftCheckBox_stateChanged );
362 connect( mLowerMiddleRadioButton, &QRadioButton::toggled, this, &QgsLayoutItemPropertiesWidget::mLowerMiddleCheckBox_stateChanged );
363 connect( mLowerRightRadioButton, &QRadioButton::toggled, this, &QgsLayoutItemPropertiesWidget::mLowerRightCheckBox_stateChanged );
364 connect( mBlendModeCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutItemPropertiesWidget::mBlendModeCombo_currentIndexChanged );
365 connect( mItemRotationSpinBox, static_cast<void ( QDoubleSpinBox::* )( double )>( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutItemPropertiesWidget::mItemRotationSpinBox_valueChanged );
366 connect( mExcludeFromPrintsCheckBox, &QCheckBox::toggled, this, &QgsLayoutItemPropertiesWidget::mExcludeFromPrintsCheckBox_toggled );
367
368 //make button exclusive
369 QButtonGroup *buttonGroup = new QButtonGroup( this );
370 buttonGroup->addButton( mUpperLeftRadioButton );
371 buttonGroup->addButton( mUpperMiddleRadioButton );
372 buttonGroup->addButton( mUpperRightRadioButton );
373 buttonGroup->addButton( mMiddleLeftRadioButton );
374 buttonGroup->addButton( mMiddleRadioButton );
375 buttonGroup->addButton( mMiddleRightRadioButton );
376 buttonGroup->addButton( mLowerLeftRadioButton );
377 buttonGroup->addButton( mLowerMiddleRadioButton );
378 buttonGroup->addButton( mLowerRightRadioButton );
379 buttonGroup->setExclusive( true );
380
382
383 setItem( item );
384
385 connect( mOpacityWidget, &QgsOpacityWidget::opacityChanged, this, &QgsLayoutItemPropertiesWidget::opacityChanged );
386
388 connect( mVariableEditor, &QgsVariableEditorWidget::scopeChanged, this, [this] {
389 if ( !mBlockVariableUpdates )
390 QgsLayoutItemPropertiesWidget::variablesChanged();
391 } );
392 // listen out for variable edits
396
397 if ( item->layout() )
398 {
402 }
403}
404
406{
407 mBackgroundGroupBox->setVisible( showGroup );
408}
409
411{
412 mFrameGroupBox->setVisible( showGroup );
413}
414
416{
417 if ( mItem )
418 {
419 disconnect( mItem, &QgsLayoutItem::sizePositionChanged, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements );
420 disconnect( mItem, &QgsLayoutObject::changed, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiNonPositionElements );
421 }
422 mItem = item;
423 if ( mItem )
424 {
425 connect( mItem, &QgsLayoutItem::sizePositionChanged, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements );
426 connect( mItem, &QgsLayoutObject::changed, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiNonPositionElements );
427 }
428
429 mConfigObject->setObject( mItem );
430
431 setValuesForGuiElements();
432}
433
435{
436 if ( QgsPrintLayout *printLayout = dynamic_cast<QgsPrintLayout *>( masterLayout ) )
437 {
440 }
441}
442
443//slots
444
445void QgsLayoutItemPropertiesWidget::mFrameColorButton_colorChanged( const QColor &newFrameColor )
446{
447 if ( !mItem )
448 {
449 return;
450 }
451 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Frame Color" ), QgsLayoutItem::UndoStrokeColor );
452 mItem->setFrameStrokeColor( newFrameColor );
453 mItem->layout()->undoStack()->endCommand();
454 mItem->update();
455}
456
457void QgsLayoutItemPropertiesWidget::mBackgroundColorButton_colorChanged( const QColor &newBackgroundColor )
458{
459 if ( !mItem )
460 {
461 return;
462 }
463 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Background Color" ), QgsLayoutItem::UndoBackgroundColor );
464 mItem->setBackgroundColor( newBackgroundColor );
465 mItem->layout()->undoStack()->endCommand();
466 mItem->invalidateCache();
467}
468
469void QgsLayoutItemPropertiesWidget::changeItemPosition()
470{
471 if ( !mItem )
472 return;
473
474 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Move Item" ), QgsLayoutItem::UndoIncrementalMove );
475
476 const QgsLayoutPoint point( mXPosSpin->value(), mYPosSpin->value(), mPosUnitsComboBox->unit() );
477 mItem->attemptMove( point, true, false, mPageSpinBox->value() - 1 );
478
479 mItem->layout()->undoStack()->endCommand();
480}
481
482void QgsLayoutItemPropertiesWidget::changeItemReference( QgsLayoutItem::ReferencePoint point )
483{
484 if ( !mItem )
485 return;
486
487 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Item Reference" ) );
488 mItem->setReferencePoint( point );
489 mItem->layout()->undoStack()->endCommand();
490}
491
492void QgsLayoutItemPropertiesWidget::changeItemSize()
493{
494 if ( !mItem )
495 return;
496
497 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Resize Item" ), QgsLayoutItem::UndoIncrementalResize );
498
499 const QgsLayoutSize size( mWidthSpin->value(), mHeightSpin->value(), mSizeUnitsComboBox->unit() );
500 mItem->attemptResize( size );
501
502 mItem->layout()->undoStack()->endCommand();
503}
504
505void QgsLayoutItemPropertiesWidget::variablesChanged()
506{
507 if ( !mItem )
508 return;
509
510 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Item Variables" ) );
511
512 if ( QgsLayoutFrame *frame = qobject_cast<QgsLayoutFrame *>( mItem ) )
513 {
514 if ( QgsLayoutMultiFrame *mf = frame->multiFrame() )
515 {
516 QgsExpressionContextUtils::setLayoutMultiFrameVariables( mf, mVariableEditor->variablesInActiveScope() );
517 }
518 }
519 else
520 {
521 QgsExpressionContextUtils::setLayoutItemVariables( mItem, mVariableEditor->variablesInActiveScope() );
522 }
523 mItem->layout()->undoStack()->endCommand();
524}
525
527{
528 if ( mUpperLeftRadioButton->isChecked() )
529 {
531 }
532 else if ( mUpperMiddleRadioButton->isChecked() )
533 {
535 }
536 else if ( mUpperRightRadioButton->isChecked() )
537 {
539 }
540 else if ( mMiddleLeftRadioButton->isChecked() )
541 {
543 }
544 else if ( mMiddleRadioButton->isChecked() )
545 {
547 }
548 else if ( mMiddleRightRadioButton->isChecked() )
549 {
551 }
552 else if ( mLowerLeftRadioButton->isChecked() )
553 {
555 }
556 else if ( mLowerMiddleRadioButton->isChecked() )
557 {
559 }
560 else if ( mLowerRightRadioButton->isChecked() )
561 {
563 }
565}
566
567void QgsLayoutItemPropertiesWidget::mStrokeWidthSpinBox_valueChanged( double d )
568{
569 if ( !mItem )
570 {
571 return;
572 }
573
574 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Frame Stroke Width" ), QgsLayoutItem::UndoStrokeWidth );
575 mItem->setFrameStrokeWidth( QgsLayoutMeasurement( d, mStrokeUnitsComboBox->unit() ) );
576 mItem->layout()->undoStack()->endCommand();
577}
578
579void QgsLayoutItemPropertiesWidget::strokeUnitChanged( Qgis::LayoutUnit unit )
580{
581 if ( !mItem )
582 {
583 return;
584 }
585
586 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Frame Stroke Width" ), QgsLayoutItem::UndoStrokeWidth );
587 mItem->setFrameStrokeWidth( QgsLayoutMeasurement( mStrokeWidthSpinBox->value(), unit ) );
588 mItem->layout()->undoStack()->endCommand();
589}
590
591void QgsLayoutItemPropertiesWidget::mFrameJoinStyleCombo_currentIndexChanged( int index )
592{
593 Q_UNUSED( index )
594 if ( !mItem )
595 {
596 return;
597 }
598
599 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Frame Join Style" ) );
600 mItem->setFrameJoinStyle( mFrameJoinStyleCombo->penJoinStyle() );
601 mItem->layout()->undoStack()->endCommand();
602}
603
604void QgsLayoutItemPropertiesWidget::mFrameGroupBox_toggled( bool state )
605{
606 if ( !mItem )
607 {
608 return;
609 }
610
611 mItem->layout()->undoStack()->beginCommand( mItem, state ? tr( "Enable Frame" ) : tr( "Disable Frame" ) );
612 mItem->setFrameEnabled( state );
613 mItem->update();
614 mItem->layout()->undoStack()->endCommand();
615}
616
617void QgsLayoutItemPropertiesWidget::mBackgroundGroupBox_toggled( bool state )
618{
619 if ( !mItem )
620 {
621 return;
622 }
623
624 mItem->layout()->undoStack()->beginCommand( mItem, state ? tr( "Enable Background" ) : tr( "Disable Background" ) );
625 mItem->setBackgroundEnabled( state );
626 mItem->layout()->undoStack()->endCommand();
627 mItem->invalidateCache();
628}
629
630
631void QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements()
632{
633 if ( !mItem )
634 {
635 return;
636 }
637
638 auto block = [this]( bool blocked ) {
639 mXPosSpin->blockSignals( blocked );
640 mYPosSpin->blockSignals( blocked );
641 mPosUnitsComboBox->blockSignals( blocked );
642 mWidthSpin->blockSignals( blocked );
643 mHeightSpin->blockSignals( blocked );
644 mSizeUnitsComboBox->blockSignals( blocked );
645 mUpperLeftRadioButton->blockSignals( blocked );
646 mUpperMiddleRadioButton->blockSignals( blocked );
647 mUpperRightRadioButton->blockSignals( blocked );
648 mMiddleLeftRadioButton->blockSignals( blocked );
649 mMiddleRadioButton->blockSignals( blocked );
650 mMiddleRightRadioButton->blockSignals( blocked );
651 mLowerLeftRadioButton->blockSignals( blocked );
652 mLowerMiddleRadioButton->blockSignals( blocked );
653 mLowerRightRadioButton->blockSignals( blocked );
654 mPageSpinBox->blockSignals( blocked );
655 };
656 block( true );
657
658 const QgsLayoutPoint point = mItem->pagePositionWithUnits();
659
660 if ( !mFreezeXPosSpin )
661 mXPosSpin->setValue( point.x() );
662 if ( !mFreezeYPosSpin )
663 mYPosSpin->setValue( point.y() );
664 mPosUnitsComboBox->setUnit( point.units() );
665
666 switch ( mItem->referencePoint() )
667 {
669 {
670 mUpperLeftRadioButton->setChecked( true );
671 break;
672 }
673
675 {
676 mUpperMiddleRadioButton->setChecked( true );
677 break;
678 }
679
681 {
682 mUpperRightRadioButton->setChecked( true );
683 break;
684 }
685
687 {
688 mMiddleLeftRadioButton->setChecked( true );
689 break;
690 }
691
693 {
694 mMiddleRadioButton->setChecked( true );
695 break;
696 }
697
699 {
700 mMiddleRightRadioButton->setChecked( true );
701 break;
702 }
703
705 {
706 mLowerLeftRadioButton->setChecked( true );
707 break;
708 }
709
711 {
712 mLowerMiddleRadioButton->setChecked( true );
713 break;
714 }
715
717 {
718 mLowerRightRadioButton->setChecked( true );
719 break;
720 }
721 }
722
723 const QgsLayoutSize size = mItem->sizeWithUnits();
724 if ( !mFreezeWidthSpin )
725 mWidthSpin->setValue( size.width() );
726 if ( !mFreezeHeightSpin )
727 mHeightSpin->setValue( size.height() );
728
729 mSizeUnitsComboBox->setUnit( size.units() );
730
731 mSizeLockAspectRatio->resetRatio();
732 mPosLockAspectRatio->resetRatio();
733
734 if ( !mFreezePageSpin )
735 mPageSpinBox->setValue( mItem->page() + 1 );
736
737 block( false );
738}
739
740void QgsLayoutItemPropertiesWidget::setValuesForGuiNonPositionElements()
741{
742 if ( !mItem )
743 {
744 return;
745 }
746
747 whileBlocking( mBackgroundColorButton )->setColor( mItem->backgroundColor( false ) );
748 whileBlocking( mFrameColorButton )->setColor( mItem->frameStrokeColor() );
749 whileBlocking( mStrokeUnitsComboBox )->setUnit( mItem->frameStrokeWidth().units() );
750 whileBlocking( mStrokeWidthSpinBox )->setValue( mItem->frameStrokeWidth().length() );
751 whileBlocking( mFrameJoinStyleCombo )->setPenJoinStyle( mItem->frameJoinStyle() );
752 whileBlocking( mItemIdLineEdit )->setText( mItem->id() );
753 whileBlocking( mFrameGroupBox )->setChecked( mItem->frameEnabled() );
754 whileBlocking( mBackgroundGroupBox )->setChecked( mItem->hasBackground() );
755 whileBlocking( mBlendModeCombo )->setBlendMode( mItem->blendMode() );
756 whileBlocking( mOpacityWidget )->setOpacity( mItem->itemOpacity() );
757 whileBlocking( mItemRotationSpinBox )->setValue( mItem->itemRotation() );
758 whileBlocking( mExcludeFromPrintsCheckBox )->setChecked( mItem->excludeFromExports() );
759 whileBlocking( mExportGroupNameCombo )->setCurrentText( mItem->customProperty( u"pdfExportGroup"_s ).toString() );
760}
761
763{
764 mConfigObject->initializeDataDefinedButton( mXPositionDDBtn, QgsLayoutObject::DataDefinedProperty::PositionX );
765 mConfigObject->initializeDataDefinedButton( mYPositionDDBtn, QgsLayoutObject::DataDefinedProperty::PositionY );
766 mConfigObject->initializeDataDefinedButton( mWidthDDBtn, QgsLayoutObject::DataDefinedProperty::ItemWidth );
767 mConfigObject->initializeDataDefinedButton( mHeightDDBtn, QgsLayoutObject::DataDefinedProperty::ItemHeight );
768 mConfigObject->initializeDataDefinedButton( mItemRotationDDBtn, QgsLayoutObject::DataDefinedProperty::ItemRotation );
769 mConfigObject->initializeDataDefinedButton( mOpacityDDBtn, QgsLayoutObject::DataDefinedProperty::Opacity );
770 mConfigObject->initializeDataDefinedButton( mBlendModeDDBtn, QgsLayoutObject::DataDefinedProperty::BlendMode );
771 mConfigObject->initializeDataDefinedButton( mExcludePrintsDDBtn, QgsLayoutObject::DataDefinedProperty::ExcludeFromExports );
772 mConfigObject->initializeDataDefinedButton( mItemFrameColorDDBtn, QgsLayoutObject::DataDefinedProperty::FrameColor );
773 mConfigObject->initializeDataDefinedButton( mItemBackgroundColorDDBtn, QgsLayoutObject::DataDefinedProperty::BackgroundColor );
774}
775
777{
778 const QList<QgsPropertyOverrideButton *> buttons = findChildren<QgsPropertyOverrideButton *>();
779 for ( QgsPropertyOverrideButton *button : buttons )
780 {
781 mConfigObject->updateDataDefinedButton( button );
782 }
783}
784
785void QgsLayoutItemPropertiesWidget::setValuesForGuiElements()
786{
787 if ( !mItem )
788 {
789 return;
790 }
791
792 mBackgroundColorButton->setColorDialogTitle( tr( "Select Background Color" ) );
793 mBackgroundColorButton->setAllowOpacity( true );
794 mBackgroundColorButton->setContext( u"composer"_s );
795 mFrameColorButton->setColorDialogTitle( tr( "Select Frame Color" ) );
796 mFrameColorButton->setAllowOpacity( true );
797 mFrameColorButton->setContext( u"composer"_s );
798
799 if ( QgsLayout *layout = mItem->layout() )
800 {
801 // collect export groups from layout, so that we can offer auto completion in the PDF export group combo
802 QList<QgsLayoutItem *> items;
803 layout->layoutItems( items );
804 QStringList existingGroups;
805 for ( const QgsLayoutItem *item : std::as_const( items ) )
806 {
807 const QString groupName = item->customProperty( u"pdfExportGroup"_s ).toString();
808 if ( !groupName.isEmpty() && !existingGroups.contains( groupName ) )
809 existingGroups.append( groupName );
810 }
811
812 std::sort( existingGroups.begin(), existingGroups.end(), []( const QString &a, const QString &b ) -> bool { return a.localeAwareCompare( b ) < 0; } );
813
814 whileBlocking( mExportGroupNameCombo )->clear();
815 whileBlocking( mExportGroupNameCombo )->addItems( existingGroups );
816 }
817
818 setValuesForGuiPositionElements();
819 setValuesForGuiNonPositionElements();
821
823}
824
825void QgsLayoutItemPropertiesWidget::mBlendModeCombo_currentIndexChanged( int index )
826{
827 Q_UNUSED( index )
828 if ( mItem )
829 {
830 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Blend Mode" ) );
831 mItem->setBlendMode( mBlendModeCombo->blendMode() );
832 mItem->layout()->undoStack()->endCommand();
833 }
834}
835
836void QgsLayoutItemPropertiesWidget::opacityChanged( double value )
837{
838 if ( mItem )
839 {
840 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Opacity" ), QgsLayoutItem::UndoOpacity );
841 mItem->setItemOpacity( value );
842 mItem->layout()->undoStack()->endCommand();
843 }
844}
845
846void QgsLayoutItemPropertiesWidget::mItemIdLineEdit_editingFinished()
847{
848 if ( mItem )
849 {
850 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Item ID" ), QgsLayoutItem::UndoSetId );
851 mItem->setId( mItemIdLineEdit->text() );
852 mItemIdLineEdit->setText( mItem->id() );
853 mItem->layout()->undoStack()->endCommand();
854 }
855}
856
857void QgsLayoutItemPropertiesWidget::exportGroupNameEditingFinished()
858{
859 if ( mItem )
860 {
861 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Change Export Group Name" ), QgsLayoutItem::UndoExportLayerName );
862 mItem->setCustomProperty( u"pdfExportGroup"_s, mExportGroupNameCombo->currentText() );
863 mItem->layout()->undoStack()->endCommand();
864 }
865}
866
867void QgsLayoutItemPropertiesWidget::mPageSpinBox_valueChanged( int )
868{
869 mFreezePageSpin = true;
870 changeItemPosition();
871 mFreezePageSpin = false;
872}
873
874void QgsLayoutItemPropertiesWidget::mXPosSpin_valueChanged( double )
875{
876 mFreezeXPosSpin = true;
877 changeItemPosition();
878 mFreezeXPosSpin = false;
879}
880
881void QgsLayoutItemPropertiesWidget::mYPosSpin_valueChanged( double )
882{
883 mFreezeYPosSpin = true;
884 changeItemPosition();
885 mFreezeYPosSpin = false;
886}
887
888void QgsLayoutItemPropertiesWidget::positionUnitsChanged( Qgis::LayoutUnit )
889{
890 changeItemPosition();
891}
892
893void QgsLayoutItemPropertiesWidget::mWidthSpin_valueChanged( double )
894{
895 mFreezeWidthSpin = true;
896 changeItemSize();
897 mFreezeWidthSpin = false;
898}
899
900void QgsLayoutItemPropertiesWidget::mHeightSpin_valueChanged( double )
901{
902 mFreezeHeightSpin = true;
903 changeItemSize();
904 mFreezeHeightSpin = false;
905}
906
907void QgsLayoutItemPropertiesWidget::sizeUnitsChanged( Qgis::LayoutUnit )
908{
909 changeItemSize();
910}
911
912void QgsLayoutItemPropertiesWidget::mUpperLeftCheckBox_stateChanged( bool state )
913{
914 if ( !state )
915 return;
916
917 if ( mItem )
918 {
919 changeItemReference( QgsLayoutItem::UpperLeft );
920 }
921 setValuesForGuiPositionElements();
922}
923
924void QgsLayoutItemPropertiesWidget::mUpperMiddleCheckBox_stateChanged( bool state )
925{
926 if ( !state )
927 return;
928 if ( mItem )
929 {
930 changeItemReference( QgsLayoutItem::UpperMiddle );
931 }
932 setValuesForGuiPositionElements();
933}
934
935void QgsLayoutItemPropertiesWidget::mUpperRightCheckBox_stateChanged( bool state )
936{
937 if ( !state )
938 return;
939 if ( mItem )
940 {
941 changeItemReference( QgsLayoutItem::UpperRight );
942 }
943 setValuesForGuiPositionElements();
944}
945
946void QgsLayoutItemPropertiesWidget::mMiddleLeftCheckBox_stateChanged( bool state )
947{
948 if ( !state )
949 return;
950 if ( mItem )
951 {
952 changeItemReference( QgsLayoutItem::MiddleLeft );
953 }
954 setValuesForGuiPositionElements();
955}
956
957void QgsLayoutItemPropertiesWidget::mMiddleCheckBox_stateChanged( bool state )
958{
959 if ( !state )
960 return;
961 if ( mItem )
962 {
963 changeItemReference( QgsLayoutItem::Middle );
964 }
965 setValuesForGuiPositionElements();
966}
967
968void QgsLayoutItemPropertiesWidget::mMiddleRightCheckBox_stateChanged( bool state )
969{
970 if ( !state )
971 return;
972 if ( mItem )
973 {
974 changeItemReference( QgsLayoutItem::MiddleRight );
975 }
976 setValuesForGuiPositionElements();
977}
978
979void QgsLayoutItemPropertiesWidget::mLowerLeftCheckBox_stateChanged( bool state )
980{
981 if ( !state )
982 return;
983 if ( mItem )
984 {
985 changeItemReference( QgsLayoutItem::LowerLeft );
986 }
987 setValuesForGuiPositionElements();
988}
989
990void QgsLayoutItemPropertiesWidget::mLowerMiddleCheckBox_stateChanged( bool state )
991{
992 if ( !state )
993 return;
994 if ( mItem )
995 {
996 changeItemReference( QgsLayoutItem::LowerMiddle );
997 }
998 setValuesForGuiPositionElements();
999}
1000
1001void QgsLayoutItemPropertiesWidget::mLowerRightCheckBox_stateChanged( bool state )
1002{
1003 if ( !state )
1004 return;
1005 if ( mItem )
1006 {
1007 changeItemReference( QgsLayoutItem::LowerRight );
1008 }
1009 setValuesForGuiPositionElements();
1010}
1011
1012void QgsLayoutItemPropertiesWidget::mItemRotationSpinBox_valueChanged( double val )
1013{
1014 if ( mItem )
1015 {
1016 mItem->layout()->undoStack()->beginCommand( mItem, tr( "Rotate" ), QgsLayoutItem::UndoRotation );
1017 mItem->setItemRotation( val, true );
1018 mItem->update();
1019 mItem->layout()->undoStack()->endCommand();
1020 }
1021}
1022
1023void QgsLayoutItemPropertiesWidget::mExcludeFromPrintsCheckBox_toggled( bool checked )
1024{
1025 if ( mItem )
1026 {
1027 mItem->layout()->undoStack()->beginCommand( mItem, checked ? tr( "Exclude from Exports" ) : tr( "Include in Exports" ) );
1028 mItem->setExcludeFromExports( checked );
1029 mItem->layout()->undoStack()->endCommand();
1030 }
1031}
LayoutUnit
Layout measurement units.
Definition qgis.h:5790
void customVariablesChanged()
Emitted whenever a custom global variable changes.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
void colorChanged(const QColor &color)
Emitted whenever a new color is set for the button.
static void setLayoutItemVariables(QgsLayoutItem *item, const QVariantMap &variables)
Sets all layout item context variables for an item.
static void setLayoutMultiFrameVariables(QgsLayoutMultiFrame *frame, const QVariantMap &variables)
Sets all layout multiframe context variables for an frame.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
int indexOfScope(QgsExpressionContextScope *scope) const
Returns the index of the specified scope if it exists within the context.
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
void setShowClearButton(bool visible)
Sets whether the widget's clear button is visible.
A button for customizing QgsTextFormat settings.
Used to render QgsLayout as an atlas, by iterating over the features from an associated vector layer.
void toggled(bool enabled)
Emitted when atlas is enabled or disabled.
void coverageLayerChanged(QgsVectorLayer *layer)
Emitted when the coverage layer for the atlas changes.
An object for property widgets for layout items.
QgsLayoutAtlas * layoutAtlas() const
Returns the atlas for the layout, if available.
void setObject(QgsLayoutObject *object) SIP_SKIP
Links a new layout object to this QgsLayoutConfigObject.
void initializeDataDefinedButton(QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty key)
Registers a data defined button, setting up its initial value, connections and description.
QgsVectorLayer * coverageLayer() const
Returns the current layout context coverage layer (if set).
void updateDataDefinedButton(QgsPropertyOverrideButton *button)
Updates a data defined button to reflect the item's current properties.
QgsLayoutConfigObject(QWidget *parent SIP_TRANSFERTHIS, QgsLayoutObject *layoutObject)
Constructor for QgsLayoutConfigObject, linked with the specified layoutObject.
A common interface for layout designer dialogs and widgets.
virtual QgsMessageBar * messageBar()=0
Returns the designer's message bar.
Base class for frame items, which form a layout multiframe item.
QgsLayoutObject * layoutObject()
Returns the layout object associated with this widget.
void updateDataDefinedButton(QgsPropertyOverrideButton *button)
Updates a previously registered data defined button to reflect the item's current properties.
bool setItem(QgsLayoutItem *item)
Sets the current item to show in the widget.
QgsVectorLayer * coverageLayer() const
Returns the current layout context coverage layer (if set).
virtual bool setNewItem(QgsLayoutItem *item)
Attempts to update the widget to show the properties for the specified item.
void registerDataDefinedButton(QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty property)
Registers a data defined button, setting up its initial value, connections and description.
virtual void setDesignerInterface(QgsLayoutDesignerInterface *iface)
Sets the the layout designer interface in which the widget is being shown.
QgsLayoutAtlas * layoutAtlas() const
Returns the atlas for the layout (if available).
virtual void setMasterLayout(QgsMasterLayoutInterface *masterLayout)
Sets the master layout associated with the item.
QgsLayoutItemBaseWidget(QWidget *parent SIP_TRANSFERTHIS, QgsLayoutObject *layoutObject)
Constructor for QgsLayoutItemBaseWidget, linked with the specified layoutObject.
virtual void setReportTypeString(const QString &string)
Sets the string to use to describe the current report type (e.g.
void setMasterLayout(QgsMasterLayoutInterface *masterLayout)
Sets the master layout associated with the item.
void populateDataDefinedButtons()
Sets data defined button state to match item.
QgsLayoutItemPropertiesWidget(QWidget *parent, QgsLayoutItem *item)
Constructs a QgsLayoutItemPropertiesWidget with a parent and for the given layout item.
void initializeDataDefinedButtons()
Initializes data defined buttons to current atlas coverage layer.
void showFrameGroup(bool showGroup)
Determines if the frame of the group box shall be shown.
void setItem(QgsLayoutItem *item)
Sets the layout item.
QgsLayoutItem::ReferencePoint positionMode() const
Returns the position mode.
void updateVariables()
Updates the variables widget, refreshing the values of variables shown.
void showBackgroundGroup(bool showGroup)
Determines if the background of the group box shall be shown.
Base class for graphical items within a QgsLayout.
@ UndoIncrementalMove
Layout item incremental movement, e.g. as a result of a keypress.
@ UndoBackgroundColor
Background color adjustment.
@ UndoOpacity
Opacity adjustment.
@ UndoIncrementalResize
Incremental resize.
@ UndoRotation
Rotation adjustment.
@ UndoStrokeWidth
Stroke width adjustment.
@ UndoExportLayerName
Export layer name.
@ UndoSetId
Change item ID.
@ UndoStrokeColor
Stroke color adjustment.
ReferencePoint
Fixed position reference point.
@ LowerMiddle
Lower center of item.
@ MiddleLeft
Middle left of item.
@ Middle
Center of item.
@ UpperRight
Upper right corner of item.
@ LowerLeft
Lower left corner of item.
@ UpperLeft
Upper left corner of item.
@ UpperMiddle
Upper center of item.
@ MiddleRight
Middle right of item.
@ LowerRight
Lower right corner of item.
void sizePositionChanged()
Emitted when the item's size or position changes.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
A base class for objects which belong to a layout.
static const QgsPropertiesDefinition & propertyDefinitions()
Returns the layout object property definitions.
const QgsLayout * layout() const
Returns the layout the object is attached to.
void changed()
Emitted when the object's properties change.
DataDefinedProperty
Data defined properties for different item types.
@ ExcludeFromExports
Exclude item from exports.
@ BackgroundColor
Item background color.
static bool propertyAssociatesWithParentMultiframe(DataDefinedProperty property)
Returns true if the specified property key is normally associated with the parent QgsLayoutMultiFrame...
void changed()
Emitted when pages are added or removed from the collection.
double x() const
Returns x coordinate of point.
double y() const
Returns y coordinate of point.
Qgis::LayoutUnit units() const
Returns the units for the point.
void dpiChanged()
Emitted when the context's DPI is changed.
const QgsLayoutMeasurementConverter & measurementConverter() const
Returns the layout measurement converter to be used in the layout.
QgsVectorLayer * layer() const
Returns the vector layer associated with the layout's context.
double height() const
Returns the height of the size.
Qgis::LayoutUnit units() const
Returns the units for the size.
double width() const
Returns the width of the size.
void unitChanged(Qgis::LayoutUnit unit)
Emitted when the unit is changed.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:51
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
void variablesChanged()
Emitted whenever the expression variables stored in the layout have been changed.
QgsLayoutReportContext & reportContext()
Returns a reference to the layout's report context, which stores information relating to the current ...
QgsProject * project() const
The project associated with the layout.
Interface for master layout type objects, such as print layouts and reports.
void opacityChanged(double opacity)
Emitted when the opacity is changed in the widget, where opacity ranges from 0.0 (transparent) to 1....
QgsPanelWidget(QWidget *parent=nullptr)
Base class for any widget that can be shown as an inline panel.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
QgsLayoutAtlas * atlas()
Returns the print layout's atlas.
void nameChanged(const QString &name)
Emitted when the layout's name is changed.
void metadataChanged()
Emitted when the project's metadata is changed.
void customVariablesChanged()
Emitted whenever the expression variables stored in the project have been changed.
A button for controlling property overrides which may apply to a widget.
QgsProperty toProperty() const
Returns a QgsProperty object encapsulating the current state of the widget.
void changed()
Emitted when property definition changes.
void init(int propertyKey, const QgsProperty &property, const QgsPropertiesDefinition &definitions, const QgsVectorLayer *layer=nullptr, bool auxiliaryStorageEnabled=false)
Initialize a newly constructed property button (useful if button was included in a UI layout).
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
int propertyKey() const
Returns the property key linked to the button.
A button for creating and modifying QgsSymbol settings.
void scopeChanged()
Emitted when the user has modified a scope using the widget.
Represents a vector layer which manages a vector based dataset.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:7705