Skip to content

Commit 460cee1

Browse files
committed
[composer] Switch to using data defined button for picture source expression
1 parent 023b4cf commit 460cee1

10 files changed

Lines changed: 204 additions & 323 deletions

python/core/composer/qgscomposerobject.sip

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class QgsComposerObject : QObject
3636
MapXMin, /*< map extent x minimum */
3737
MapYMin, /*< map extent y minimum */
3838
MapXMax, /*< map extent x maximum */
39-
MapYMax /*< map extent y maximum */
39+
MapYMax, /*< map extent y maximum */
40+
//composer picture
41+
PictureSource /*< picture source url */
4042
};
4143

4244
/** Specifies whether the value returned by a function should be the original, user
@@ -76,7 +78,7 @@ class QgsComposerObject : QObject
7678
* @param property data defined property to return
7779
* @note this method was added in version 2.5
7880
*/
79-
QgsDataDefined* dataDefinedProperty( const DataDefinedProperty property );
81+
QgsDataDefined* dataDefinedProperty( const DataDefinedProperty property ) const;
8082

8183
/**Sets parameters for a data defined property for the item
8284
* @param property data defined property to set

python/core/composer/qgscomposerpicture.sip

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,37 @@ class QgsComposerPicture: QgsComposerItem
3939
/**Reimplementation of QCanvasItem::paint*/
4040
void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
4141

42-
/**Sets the source file of the image (may be svg or a raster format). This is only used if
43-
* usePictureExpression() is false.
42+
/**Sets the source file of the image (may be svg or a raster format). Data defined
43+
* picture source may override this value.
4444
* @param path full path to the source image
4545
* @see usePictureExpression
4646
* @see pictureFile
4747
*/
4848
void setPictureFile( const QString& path );
4949

50-
/**Returns the path of the source image file. This is only used if
51-
* usePictureExpression() is false.
50+
/**Returns the path of the source image file. Data defined picture source may override
51+
* this value.
5252
* @returns path to the source image
5353
* @see usePictureExpression
5454
* @see setPictureFile
5555
*/
5656
QString pictureFile() const;
5757

5858
/**Sets this items bound in scene coordinates such that 1 item size units
59-
corresponds to 1 scene size unit and resizes the svg symbol / image*/
59+
* corresponds to 1 scene size unit and resizes the svg symbol / image
60+
*/
6061
void setSceneRect( const QRectF& rectangle );
6162

62-
/** stores state in Dom element
63-
* @param elem is Dom element corresponding to 'Composer' tag
64-
* @param doc is Dom document
65-
*/
63+
/**Stores state in Dom element
64+
* @param elem is Dom element corresponding to 'Composer' tag
65+
* @param doc is Dom document
66+
*/
6667
bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
6768

68-
/** sets state from Dom document
69-
* @param itemElem is Dom node corresponding to item tag
70-
* @param doc is Dom document
71-
*/
69+
/**Sets state from Dom document
70+
* @param itemElem is Dom node corresponding to item tag
71+
* @param doc is Dom document
72+
*/
7273
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
7374

7475
/**Returns the rotation used for drawing the picture within the composer item
@@ -141,28 +142,32 @@ class QgsComposerPicture: QgsComposerItem
141142
* @see setUsePictureExpression
142143
* @see pictureFile
143144
* @see pictureExpression
145+
* @deprecated use QgsComposerObject::dataDefinedProperty( QgsComposerObject::PictureSource ) instead
144146
*/
145-
bool usePictureExpression() const;
147+
bool usePictureExpression() const /Deprecated/;
146148

147149
/**Returns the expression the item is using for the picture source. This is only
148150
* used if usePictureExpression() is true.
149151
* @returns expression for the picture item's image path
150152
* @note added in 2.3
151153
* @see setPictureExpression
152154
* @see usePictureExpression
155+
* @deprecated use QgsComposerObject::dataDefinedProperty( QgsComposerObject::PictureSource ) instead
153156
*/
154-
QString pictureExpression() const;
157+
QString pictureExpression() const /Deprecated/;
155158

156159
/**Calculates width and hight of the picture (in mm) such that it fits into the item frame with the given rotation
157160
* @deprecated Use bool QgsComposerItem::imageSizeConsideringRotation( double& width, double& height, double rotation )
158161
* instead
159162
*/
160163
bool imageSizeConsideringRotation( double& width, double& height ) const /Deprecated/;
164+
161165
/**Calculates corner point after rotation and scaling
162166
* @deprecated Use QgsComposerItem::cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height, double rotation )
163167
* instead
164168
*/
165169
bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const /Deprecated/;
170+
166171
/**Calculates width / height of the bounding box of a rotated rectangle
167172
* @deprecated Use QgsComposerItem::sizeChangedByRotation( double& width, double& height, double rotation )
168173
* instead
@@ -204,17 +209,19 @@ class QgsComposerPicture: QgsComposerItem
204209
* @see usePictureExpression
205210
* @see setPictureFile
206211
* @see setPictureExpression
212+
* @deprecated use QgsComposerObject::dataDefinedProperty( QgsComposerObject::PictureSource ) instead
207213
*/
208-
virtual void setUsePictureExpression( bool useExpression );
214+
virtual void setUsePictureExpression( bool useExpression ) /Deprecated/;
209215

210216
/**Sets an expression to use for the picture source. This expression is only
211217
* used if usePictureExpression() is true.
212218
* @param expression to use for picture path
213219
* @note added in 2.3
214220
* @see setUsePictureExpression
215221
* @see pictureExpression
222+
* @deprecated use QgsComposerObject::dataDefinedProperty( QgsComposerObject::PictureSource ) instead
216223
*/
217-
virtual void setPictureExpression( QString expression );
224+
virtual void setPictureExpression( QString expression ) /Deprecated/;
218225

219226
/**Recalculates the source image (if using an expression for picture's source)
220227
* and reloads and redraws the picture.
@@ -225,8 +232,9 @@ class QgsComposerPicture: QgsComposerItem
225232
/**Prepares the picture's source expression after it is altered or the compositions
226233
* atlas coverage layer changes.
227234
* @note added in 2.3
235+
* @deprecated no longer required
228236
*/
229-
void updatePictureExpression();
237+
void updatePictureExpression() /Deprecated/;
230238

231239
/**Forces a recalculation of the picture's frame size
232240
* @note added in 2.3

src/app/composer/qgscomposerpicturewidget.cpp

Lines changed: 43 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,20 @@ QgsComposerPictureWidget::QgsComposerPictureWidget( QgsComposerPicture* picture
5252

5353
connect( mPicture, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
5454
connect( mPicture, SIGNAL( pictureRotationChanged( double ) ), this, SLOT( setPicRotationSpinValue( double ) ) );
55-
connect( mPictureExpressionLineEdit, SIGNAL( editingFinished() ), this, SLOT( setPictureExpression() ) );
5655

56+
QgsAtlasComposition* atlas = atlasComposition();
57+
if ( atlas )
58+
{
59+
// repopulate data defined buttons if atlas layer changes
60+
connect( atlas, SIGNAL( coverageLayerChanged( QgsVectorLayer* ) ),
61+
this, SLOT( populateDataDefinedButtons() ) );
62+
connect( atlas, SIGNAL( toggled( bool ) ), this, SLOT( populateDataDefinedButtons() ) );
63+
}
64+
65+
//connections for data defined buttons
66+
connect( mSourceDDBtn, SIGNAL( dataDefinedChanged( const QString& ) ), this, SLOT( updateDataDefinedProperty( ) ) );
67+
connect( mSourceDDBtn, SIGNAL( dataDefinedActivated( bool ) ), this, SLOT( updateDataDefinedProperty( ) ) );
68+
connect( mSourceDDBtn, SIGNAL( dataDefinedActivated( bool ) ), mPictureLineEdit, SLOT( setDisabled( bool ) ) );
5769
}
5870

5971
QgsComposerPictureWidget::~QgsComposerPictureWidget()
@@ -124,29 +136,6 @@ void QgsComposerPictureWidget::on_mPictureLineEdit_editingFinished()
124136
}
125137
}
126138

127-
void QgsComposerPictureWidget::on_mPictureExpressionButton_clicked()
128-
{
129-
if ( !mPicture )
130-
{
131-
return;
132-
}
133-
134-
// use the atlas coverage layer, if any
135-
QgsVectorLayer* coverageLayer = atlasCoverageLayer();
136-
137-
QgsExpressionBuilderDialog exprDlg( coverageLayer, mPictureExpressionLineEdit->text(), this );
138-
exprDlg.setWindowTitle( tr( "Expression based image path" ) );
139-
if ( exprDlg.exec() == QDialog::Accepted )
140-
{
141-
QString expression = exprDlg.expressionText();
142-
if ( !expression.isEmpty() )
143-
{
144-
mPictureExpressionLineEdit->setText( expression );
145-
setPictureExpression();
146-
}
147-
}
148-
}
149-
150139
void QgsComposerPictureWidget::on_mPictureRotationSpinBox_valueChanged( double d )
151140
{
152141
if ( mPicture )
@@ -262,52 +251,6 @@ void QgsComposerPictureWidget::on_mAnchorPointComboBox_currentIndexChanged( int
262251
mPicture->endCommand();
263252
}
264253

265-
void QgsComposerPictureWidget::on_mRadioPath_clicked()
266-
{
267-
if ( !mPicture )
268-
{
269-
return;
270-
}
271-
272-
mPicture->beginCommand( tr( "Picture source changed" ) );
273-
mPicture->setUsePictureExpression( false );
274-
mPicture->endCommand();
275-
276-
mPictureLineEdit->setEnabled( true );
277-
mPictureBrowseButton->setEnabled( true );
278-
mPictureExpressionLineEdit->setEnabled( false );
279-
mPictureExpressionButton->setEnabled( false );
280-
}
281-
282-
void QgsComposerPictureWidget::on_mRadioExpression_clicked()
283-
{
284-
if ( !mPicture )
285-
{
286-
return;
287-
}
288-
289-
mPicture->beginCommand( tr( "Picture source changed" ) );
290-
mPicture->setUsePictureExpression( true );
291-
mPicture->endCommand();
292-
293-
mPictureLineEdit->setEnabled( false );
294-
mPictureBrowseButton->setEnabled( false );
295-
mPictureExpressionLineEdit->setEnabled( true );
296-
mPictureExpressionButton->setEnabled( true );
297-
}
298-
299-
void QgsComposerPictureWidget::setPictureExpression()
300-
{
301-
if ( !mPicture )
302-
{
303-
return;
304-
}
305-
306-
mPicture->beginCommand( tr( "Picture source expression" ) );
307-
mPicture->setPictureExpression( mPictureExpressionLineEdit->text() );
308-
mPicture->endCommand();
309-
}
310-
311254
void QgsComposerPictureWidget::on_mRotationFromComposerMapCheckBox_stateChanged( int state )
312255
{
313256
if ( !mPicture )
@@ -438,9 +381,6 @@ void QgsComposerPictureWidget::setGuiElementValues()
438381
mRotationFromComposerMapCheckBox->blockSignals( true );
439382
mResizeModeComboBox->blockSignals( true );
440383
mAnchorPointComboBox->blockSignals( true );
441-
mRadioPath->blockSignals( true );
442-
mRadioExpression->blockSignals( true );
443-
mPictureExpressionLineEdit->blockSignals( true );
444384

445385
mPictureLineEdit->setText( mPicture->pictureFile() );
446386
mPictureRotationSpinBox->setValue( mPicture->pictureRotation() );
@@ -482,24 +422,14 @@ void QgsComposerPictureWidget::setGuiElementValues()
482422
mAnchorPointComboBox->setEnabled( false );
483423
}
484424

485-
mRadioPath->setChecked( !( mPicture->usePictureExpression() ) );
486-
mRadioExpression->setChecked( mPicture->usePictureExpression() );
487-
mPictureLineEdit->setEnabled( !( mPicture->usePictureExpression() ) );
488-
mPictureBrowseButton->setEnabled( !( mPicture->usePictureExpression() ) );
489-
mPictureExpressionLineEdit->setEnabled( mPicture->usePictureExpression() );
490-
mPictureExpressionButton->setEnabled( mPicture->usePictureExpression() );
491-
492-
mPictureExpressionLineEdit->setText( mPicture->pictureExpression() );
493-
494425
mRotationFromComposerMapCheckBox->blockSignals( false );
495426
mPictureRotationSpinBox->blockSignals( false );
496427
mPictureLineEdit->blockSignals( false );
497428
mComposerMapComboBox->blockSignals( false );
498429
mResizeModeComboBox->blockSignals( false );
499430
mAnchorPointComboBox->blockSignals( false );
500-
mRadioPath->blockSignals( false );
501-
mRadioExpression->blockSignals( false );
502-
mPictureExpressionLineEdit->blockSignals( false );
431+
432+
populateDataDefinedButtons();
503433
}
504434
}
505435

@@ -671,3 +601,31 @@ void QgsComposerPictureWidget::resizeEvent( QResizeEvent * event )
671601
mSearchDirectoriesComboBox->setMinimumWidth( mPreviewListWidget->sizeHint().width() );
672602
}
673603

604+
QgsComposerObject::DataDefinedProperty QgsComposerPictureWidget::ddPropertyForWidget( QgsDataDefinedButton *widget )
605+
{
606+
if ( widget == mSourceDDBtn )
607+
{
608+
return QgsComposerObject::PictureSource;
609+
}
610+
611+
return QgsComposerObject::NoProperty;
612+
}
613+
614+
void QgsComposerPictureWidget::populateDataDefinedButtons()
615+
{
616+
QgsVectorLayer* vl = atlasCoverageLayer();
617+
618+
//block signals from data defined buttons
619+
mSourceDDBtn->blockSignals( true );
620+
621+
//initialise buttons to use atlas coverage layer
622+
mSourceDDBtn->init( vl, mPicture->dataDefinedProperty( QgsComposerObject::PictureSource ),
623+
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::anyStringDesc() );
624+
625+
//initial state of controls - disable related controls when dd buttons are active
626+
mPictureLineEdit->setEnabled( !mSourceDDBtn->isActive() );
627+
628+
//unblock signals from data defined buttons
629+
mSourceDDBtn->blockSignals( false );
630+
}
631+

src/app/composer/qgscomposerpicturewidget.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class QgsComposerPictureWidget: public QgsComposerItemBaseWidget, private Ui::Qg
4141
public slots:
4242
void on_mPictureBrowseButton_clicked();
4343
void on_mPictureLineEdit_editingFinished();
44-
void on_mPictureExpressionButton_clicked();
4544
void on_mPictureRotationSpinBox_valueChanged( double d );
4645
void on_mPreviewListWidget_currentItemChanged( QListWidgetItem* current, QListWidgetItem* previous );
4746
void on_mAddDirectoryButton_clicked();
@@ -50,14 +49,17 @@ class QgsComposerPictureWidget: public QgsComposerItemBaseWidget, private Ui::Qg
5049
void on_mComposerMapComboBox_activated( const QString & text );
5150
void on_mResizeModeComboBox_currentIndexChanged( int index );
5251
void on_mAnchorPointComboBox_currentIndexChanged( int index );
53-
void on_mRadioPath_clicked();
54-
void on_mRadioExpression_clicked();
55-
void setPictureExpression();
5652

5753
protected:
5854
void showEvent( QShowEvent * event );
5955
void resizeEvent( QResizeEvent * event );
6056

57+
QgsComposerObject::DataDefinedProperty ddPropertyForWidget( QgsDataDefinedButton *widget );
58+
59+
protected slots:
60+
/**Initializes data defined buttons to current atlas coverage layer*/
61+
void populateDataDefinedButtons();
62+
6163
private slots:
6264
/**Sets the GUI elements to the values of mPicture*/
6365
void setGuiElementValues();

src/core/composer/qgscomposeritem.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,8 +1274,6 @@ void QgsComposerItem::refreshDataDefinedProperty( const QgsComposerObject::DataD
12741274
refreshBlendMode();
12751275
}
12761276

1277-
//TODO
1278-
// QgsComposerBaseItem::refreshDataDefinedPropert
12791277
update();
12801278
}
12811279

src/core/composer/qgscomposerobject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ bool QgsComposerObject::readXML( const QDomElement &itemElem, const QDomDocument
8585
return true;
8686
}
8787

88-
QgsDataDefined *QgsComposerObject::dataDefinedProperty( const QgsComposerObject::DataDefinedProperty property )
88+
QgsDataDefined *QgsComposerObject::dataDefinedProperty( const QgsComposerObject::DataDefinedProperty property ) const
8989
{
9090
if ( property == QgsComposerObject::AllProperties || property == QgsComposerObject::NoProperty )
9191
{

src/core/composer/qgscomposerobject.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ class CORE_EXPORT QgsComposerObject: public QObject
6363
MapXMin, /*< map extent x minimum */
6464
MapYMin, /*< map extent y minimum */
6565
MapXMax, /*< map extent x maximum */
66-
MapYMax /*< map extent y maximum */
66+
MapYMax, /*< map extent y maximum */
67+
//composer picture
68+
PictureSource /*< picture source url */
6769
};
6870

6971
/** Specifies whether the value returned by a function should be the original, user
@@ -104,7 +106,7 @@ class CORE_EXPORT QgsComposerObject: public QObject
104106
* @param property data defined property to return
105107
* @note this method was added in version 2.5
106108
*/
107-
QgsDataDefined* dataDefinedProperty( const DataDefinedProperty property );
109+
QgsDataDefined* dataDefinedProperty( const DataDefinedProperty property ) const;
108110

109111
/**Sets parameters for a data defined property for the item
110112
* @param property data defined property to set

0 commit comments

Comments
 (0)