Skip to content

Commit e836fc4

Browse files
committedNov 7, 2017
When selecting a different item, but the item is the same
type of item, just update the existing panel to show the new item's properties This means that flicking between selecting items of the same type will not create a new properties widget, so scroll bar positions, focused widgets, etc are all maintained. Makes using layouts less annoying.
1 parent 29dfcc0 commit e836fc4

File tree

6 files changed

+113
-14
lines changed

6 files changed

+113
-14
lines changed
 

‎python/gui/layout/qgslayoutitemwidget.sip

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ class QgsLayoutItemBaseWidget: QgsPanelWidget
8181
:rtype: QgsLayoutObject
8282
%End
8383

84+
bool setItem( QgsLayoutItem *item );
85+
%Docstring
86+
Sets the current ``item`` to show in the widget. If true is returned, ``item``
87+
was an acceptable type for display in this widget and the widget has been
88+
updated to match ``item``'s properties.
89+
90+
If false is returned, then the widget could not be successfully updated
91+
to show the properties of ``item``.
92+
:rtype: bool
93+
%End
94+
8495
protected:
8596

8697
void registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty property );
@@ -100,6 +111,19 @@ class QgsLayoutItemBaseWidget: QgsPanelWidget
100111
:rtype: QgsVectorLayer
101112
%End
102113

114+
virtual bool setNewItem( QgsLayoutItem *item );
115+
%Docstring
116+
Attempts to update the widget to show the properties
117+
for the specified ``item``.
118+
119+
Subclasses can override this if they support changing items in place.
120+
121+
Implementations must return true if the item was accepted and
122+
the widget was updated.
123+
:rtype: bool
124+
%End
125+
126+
103127

104128
};
105129

@@ -127,6 +151,8 @@ class QgsLayoutItemPropertiesWidget: QWidget
127151

128152
void showFrameGroup( bool showGroup );
129153

154+
void setItem( QgsLayoutItem *item );
155+
130156
protected slots:
131157
void initializeDataDefinedButtons();
132158
%Docstring

‎src/app/layout/qgslayoutdesignerdialog.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,17 @@ void QgsLayoutDesignerDialog::showItemOptions( QgsLayoutItem *item, bool bringPa
630630

631631
return;
632632
}
633+
else
634+
{
635+
// try to reuse
636+
if ( widget->setItem( item ) )
637+
{
638+
if ( bringPanelToFront )
639+
mItemDock->setUserVisible( true );
640+
641+
return;
642+
}
643+
}
633644
}
634645

635646
std::unique_ptr< QgsLayoutItemBaseWidget > widget( QgsGui::layoutItemGuiRegistry()->createItemWidget( item ) );

‎src/app/layout/qgslayoutmapwidget.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ QgsLayoutMapWidget::QgsLayoutMapWidget( QgsLayoutItemMap *item )
6868
mMapRotationSpinBox->setClearValue( 0 );
6969

7070
//add widget for general composer item properties
71-
QgsLayoutItemPropertiesWidget *itemPropertiesWidget = new QgsLayoutItemPropertiesWidget( this, item );
72-
mainLayout->addWidget( itemPropertiesWidget );
71+
mItemPropertiesWidget = new QgsLayoutItemPropertiesWidget( this, item );
72+
mainLayout->addWidget( mItemPropertiesWidget );
7373

7474
mScaleLineEdit->setValidator( new QDoubleValidator( mScaleLineEdit ) );
7575

@@ -141,6 +141,17 @@ QgsLayoutMapWidget::QgsLayoutMapWidget( QgsLayoutItemMap *item )
141141
blockAllSignals( false );
142142
}
143143

144+
bool QgsLayoutMapWidget::setNewItem( QgsLayoutItem *item )
145+
{
146+
if ( item->type() != QgsLayoutItemRegistry::LayoutMap )
147+
return false;
148+
149+
mMapItem = qobject_cast< QgsLayoutItemMap * >( item );
150+
mItemPropertiesWidget->setItem( mMapItem );
151+
152+
return true;
153+
}
154+
144155
void QgsLayoutMapWidget::populateDataDefinedButtons()
145156
{
146157
updateDataDefinedButton( mScaleDDBtn );

‎src/app/layout/qgslayoutmapwidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class QgsLayoutMapWidget: public QgsLayoutItemBaseWidget, private Ui::QgsLayoutM
8989
void blockOverviewItemsSignals( bool block );
9090

9191
protected:
92+
bool setNewItem( QgsLayoutItem *item ) override;
9293

9394
void addPageToToolbox( QWidget *widget, const QString &name );
9495

@@ -124,6 +125,7 @@ class QgsLayoutMapWidget: public QgsLayoutItemBaseWidget, private Ui::QgsLayoutM
124125

125126
private:
126127
QgsLayoutItemMap *mMapItem = nullptr;
128+
QgsLayoutItemPropertiesWidget *mItemPropertiesWidget = nullptr;
127129

128130
//! Sets extent of composer map from line edits
129131
void updateComposerExtentFromGui();

‎src/gui/layout/qgslayoutitemwidget.cpp

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ QgsLayoutObject *QgsLayoutItemBaseWidget::layoutObject()
139139
return mObject;
140140
}
141141

142+
bool QgsLayoutItemBaseWidget::setItem( QgsLayoutItem *item )
143+
{
144+
if ( setNewItem( item ) )
145+
{
146+
mObject = item;
147+
return true;
148+
}
149+
150+
return false;
151+
}
152+
142153
void QgsLayoutItemBaseWidget::registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsLayoutObject::DataDefinedProperty property )
143154
{
144155
mConfigObject->initializeDataDefinedButton( button, property );
@@ -154,6 +165,11 @@ QgsVectorLayer *QgsLayoutItemBaseWidget::coverageLayer() const
154165
return mConfigObject->coverageLayer();
155166
}
156167

168+
bool QgsLayoutItemBaseWidget::setNewItem( QgsLayoutItem * )
169+
{
170+
return false;
171+
}
172+
157173
#if 0 //TODO
158174
QgsAtlasComposition *QgsLayoutItemBaseWidget::atlasComposition() const
159175
{
@@ -178,28 +194,26 @@ void QgsLayoutItemPropertiesWidget::updateVariables()
178194

179195
QgsLayoutItemPropertiesWidget::QgsLayoutItemPropertiesWidget( QWidget *parent, QgsLayoutItem *item )
180196
: QWidget( parent )
181-
, mItem( item )
182197
, mConfigObject( new QgsLayoutConfigObject( this, item ) )
183198
, mFreezeXPosSpin( false )
184199
, mFreezeYPosSpin( false )
185200
, mFreezeWidthSpin( false )
186201
, mFreezeHeightSpin( false )
187202
, mFreezePageSpin( false )
188203
{
189-
190204
setupUi( this );
191205

192206
mItemRotationSpinBox->setClearValue( 0 );
193207
mStrokeUnitsComboBox->linkToWidget( mStrokeWidthSpinBox );
194-
mStrokeUnitsComboBox->setConverter( &mItem->layout()->context().measurementConverter() );
208+
mStrokeUnitsComboBox->setConverter( &item->layout()->context().measurementConverter() );
195209

196210
mPosUnitsComboBox->linkToWidget( mXPosSpin );
197211
mPosUnitsComboBox->linkToWidget( mYPosSpin );
198212
mSizeUnitsComboBox->linkToWidget( mWidthSpin );
199213
mSizeUnitsComboBox->linkToWidget( mHeightSpin );
200214

201-
mPosUnitsComboBox->setConverter( &mItem->layout()->context().measurementConverter() );
202-
mSizeUnitsComboBox->setConverter( &mItem->layout()->context().measurementConverter() );
215+
mPosUnitsComboBox->setConverter( &item->layout()->context().measurementConverter() );
216+
mSizeUnitsComboBox->setConverter( &item->layout()->context().measurementConverter() );
203217

204218
mPosLockAspectRatio->setWidthSpinBox( mXPosSpin );
205219
mPosLockAspectRatio->setHeightSpinBox( mYPosSpin );
@@ -249,25 +263,22 @@ QgsLayoutItemPropertiesWidget::QgsLayoutItemPropertiesWidget( QWidget *parent, Q
249263

250264
initializeDataDefinedButtons();
251265

252-
setValuesForGuiElements();
253-
254266
#if 0 //TODO
255267
connect( mItem->composition(), &QgsComposition::paperSizeChanged, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements );
256268
#endif
257269

258-
connect( mItem, &QgsLayoutItem::sizePositionChanged, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements );
259-
connect( mItem, &QgsLayoutObject::changed, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiNonPositionElements );
270+
setItem( item );
260271

261272
connect( mOpacityWidget, &QgsOpacityWidget::opacityChanged, this, &QgsLayoutItemPropertiesWidget::opacityChanged );
262273

263274
updateVariables();
264275
connect( mVariableEditor, &QgsVariableEditorWidget::scopeChanged, this, &QgsLayoutItemPropertiesWidget::variablesChanged );
265276
// listen out for variable edits
266277
connect( QgsApplication::instance(), &QgsApplication::customVariablesChanged, this, &QgsLayoutItemPropertiesWidget::updateVariables );
267-
connect( mItem->layout()->project(), &QgsProject::customVariablesChanged, this, &QgsLayoutItemPropertiesWidget::updateVariables );
278+
connect( item->layout()->project(), &QgsProject::customVariablesChanged, this, &QgsLayoutItemPropertiesWidget::updateVariables );
268279

269-
if ( mItem->layout() )
270-
connect( mItem->layout(), &QgsLayout::variablesChanged, this, &QgsLayoutItemPropertiesWidget::updateVariables );
280+
if ( item->layout() )
281+
connect( item->layout(), &QgsLayout::variablesChanged, this, &QgsLayoutItemPropertiesWidget::updateVariables );
271282
}
272283

273284
void QgsLayoutItemPropertiesWidget::showBackgroundGroup( bool showGroup )
@@ -280,6 +291,20 @@ void QgsLayoutItemPropertiesWidget::showFrameGroup( bool showGroup )
280291
mFrameGroupBox->setVisible( showGroup );
281292
}
282293

294+
void QgsLayoutItemPropertiesWidget::setItem( QgsLayoutItem *item )
295+
{
296+
if ( mItem )
297+
{
298+
disconnect( mItem, &QgsLayoutItem::sizePositionChanged, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements );
299+
disconnect( mItem, &QgsLayoutObject::changed, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiNonPositionElements );
300+
}
301+
mItem = item;
302+
connect( mItem, &QgsLayoutItem::sizePositionChanged, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements );
303+
connect( mItem, &QgsLayoutObject::changed, this, &QgsLayoutItemPropertiesWidget::setValuesForGuiNonPositionElements );
304+
305+
setValuesForGuiElements();
306+
}
307+
283308
//slots
284309

285310
void QgsLayoutItemPropertiesWidget::mFrameColorButton_colorChanged( const QColor &newFrameColor )

‎src/gui/layout/qgslayoutitemwidget.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ class GUI_EXPORT QgsLayoutItemBaseWidget: public QgsPanelWidget
126126
*/
127127
QgsLayoutObject *layoutObject();
128128

129+
/**
130+
* Sets the current \a item to show in the widget. If true is returned, \a item
131+
* was an acceptable type for display in this widget and the widget has been
132+
* updated to match \a item's properties.
133+
*
134+
* If false is returned, then the widget could not be successfully updated
135+
* to show the properties of \a item.
136+
*/
137+
bool setItem( QgsLayoutItem *item );
138+
129139
protected:
130140

131141
/**
@@ -144,6 +154,18 @@ class GUI_EXPORT QgsLayoutItemBaseWidget: public QgsPanelWidget
144154
*/
145155
QgsVectorLayer *coverageLayer() const;
146156

157+
/**
158+
* Attempts to update the widget to show the properties
159+
* for the specified \a item.
160+
*
161+
* Subclasses can override this if they support changing items in place.
162+
*
163+
* Implementations must return true if the item was accepted and
164+
* the widget was updated.
165+
*/
166+
virtual bool setNewItem( QgsLayoutItem *item );
167+
168+
147169
#if 0 //TODO
148170
//! Returns the atlas for the composition
149171
QgsAtlasComposition *atlasComposition() const;
@@ -176,6 +198,8 @@ class GUI_EXPORT QgsLayoutItemPropertiesWidget: public QWidget, private Ui::QgsL
176198

177199
void showFrameGroup( bool showGroup );
178200

201+
void setItem( QgsLayoutItem *item );
202+
179203
protected slots:
180204
//! Initializes data defined buttons to current atlas coverage layer
181205
void initializeDataDefinedButtons();

0 commit comments

Comments
 (0)
Please sign in to comment.