Skip to content
Permalink
Browse files

[composer] Switch to using data defined button for picture source exp…

…ression
  • Loading branch information
nyalldawson committed Jul 21, 2014
1 parent 023b4cf commit 460cee1bde6b9cbee60f28669750fe4e029f1232
@@ -36,7 +36,9 @@ class QgsComposerObject : QObject
MapXMin, /*< map extent x minimum */
MapYMin, /*< map extent y minimum */
MapXMax, /*< map extent x maximum */
MapYMax /*< map extent y maximum */
MapYMax, /*< map extent y maximum */
//composer picture
PictureSource /*< picture source url */
};

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

/**Sets parameters for a data defined property for the item
* @param property data defined property to set
@@ -39,36 +39,37 @@ class QgsComposerPicture: QgsComposerItem
/**Reimplementation of QCanvasItem::paint*/
void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );

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

/**Returns the path of the source image file. This is only used if
* usePictureExpression() is false.
/**Returns the path of the source image file. Data defined picture source may override
* this value.
* @returns path to the source image
* @see usePictureExpression
* @see setPictureFile
*/
QString pictureFile() const;

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

/** stores state in Dom element
* @param elem is Dom element corresponding to 'Composer' tag
* @param doc is Dom document
*/
/**Stores state in Dom element
* @param elem is Dom element corresponding to 'Composer' tag
* @param doc is Dom document
*/
bool writeXML( QDomElement& elem, QDomDocument & doc ) const;

/** sets state from Dom document
* @param itemElem is Dom node corresponding to item tag
* @param doc is Dom document
*/
/**Sets state from Dom document
* @param itemElem is Dom node corresponding to item tag
* @param doc is Dom document
*/
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

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

/**Returns the expression the item is using for the picture source. This is only
* used if usePictureExpression() is true.
* @returns expression for the picture item's image path
* @note added in 2.3
* @see setPictureExpression
* @see usePictureExpression
* @deprecated use QgsComposerObject::dataDefinedProperty( QgsComposerObject::PictureSource ) instead
*/
QString pictureExpression() const;
QString pictureExpression() const /Deprecated/;

/**Calculates width and hight of the picture (in mm) such that it fits into the item frame with the given rotation
* @deprecated Use bool QgsComposerItem::imageSizeConsideringRotation( double& width, double& height, double rotation )
* instead
*/
bool imageSizeConsideringRotation( double& width, double& height ) const /Deprecated/;

/**Calculates corner point after rotation and scaling
* @deprecated Use QgsComposerItem::cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height, double rotation )
* instead
*/
bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const /Deprecated/;

/**Calculates width / height of the bounding box of a rotated rectangle
* @deprecated Use QgsComposerItem::sizeChangedByRotation( double& width, double& height, double rotation )
* instead
@@ -204,17 +209,19 @@ class QgsComposerPicture: QgsComposerItem
* @see usePictureExpression
* @see setPictureFile
* @see setPictureExpression
* @deprecated use QgsComposerObject::dataDefinedProperty( QgsComposerObject::PictureSource ) instead
*/
virtual void setUsePictureExpression( bool useExpression );
virtual void setUsePictureExpression( bool useExpression ) /Deprecated/;

/**Sets an expression to use for the picture source. This expression is only
* used if usePictureExpression() is true.
* @param expression to use for picture path
* @note added in 2.3
* @see setUsePictureExpression
* @see pictureExpression
* @deprecated use QgsComposerObject::dataDefinedProperty( QgsComposerObject::PictureSource ) instead
*/
virtual void setPictureExpression( QString expression );
virtual void setPictureExpression( QString expression ) /Deprecated/;

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

/**Forces a recalculation of the picture's frame size
* @note added in 2.3
@@ -52,8 +52,20 @@ QgsComposerPictureWidget::QgsComposerPictureWidget( QgsComposerPicture* picture

connect( mPicture, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
connect( mPicture, SIGNAL( pictureRotationChanged( double ) ), this, SLOT( setPicRotationSpinValue( double ) ) );
connect( mPictureExpressionLineEdit, SIGNAL( editingFinished() ), this, SLOT( setPictureExpression() ) );

QgsAtlasComposition* atlas = atlasComposition();
if ( atlas )
{
// repopulate data defined buttons if atlas layer changes
connect( atlas, SIGNAL( coverageLayerChanged( QgsVectorLayer* ) ),
this, SLOT( populateDataDefinedButtons() ) );
connect( atlas, SIGNAL( toggled( bool ) ), this, SLOT( populateDataDefinedButtons() ) );
}

//connections for data defined buttons
connect( mSourceDDBtn, SIGNAL( dataDefinedChanged( const QString& ) ), this, SLOT( updateDataDefinedProperty( ) ) );
connect( mSourceDDBtn, SIGNAL( dataDefinedActivated( bool ) ), this, SLOT( updateDataDefinedProperty( ) ) );
connect( mSourceDDBtn, SIGNAL( dataDefinedActivated( bool ) ), mPictureLineEdit, SLOT( setDisabled( bool ) ) );
}

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

void QgsComposerPictureWidget::on_mPictureExpressionButton_clicked()
{
if ( !mPicture )
{
return;
}

// use the atlas coverage layer, if any
QgsVectorLayer* coverageLayer = atlasCoverageLayer();

QgsExpressionBuilderDialog exprDlg( coverageLayer, mPictureExpressionLineEdit->text(), this );
exprDlg.setWindowTitle( tr( "Expression based image path" ) );
if ( exprDlg.exec() == QDialog::Accepted )
{
QString expression = exprDlg.expressionText();
if ( !expression.isEmpty() )
{
mPictureExpressionLineEdit->setText( expression );
setPictureExpression();
}
}
}

void QgsComposerPictureWidget::on_mPictureRotationSpinBox_valueChanged( double d )
{
if ( mPicture )
@@ -262,52 +251,6 @@ void QgsComposerPictureWidget::on_mAnchorPointComboBox_currentIndexChanged( int
mPicture->endCommand();
}

void QgsComposerPictureWidget::on_mRadioPath_clicked()
{
if ( !mPicture )
{
return;
}

mPicture->beginCommand( tr( "Picture source changed" ) );
mPicture->setUsePictureExpression( false );
mPicture->endCommand();

mPictureLineEdit->setEnabled( true );
mPictureBrowseButton->setEnabled( true );
mPictureExpressionLineEdit->setEnabled( false );
mPictureExpressionButton->setEnabled( false );
}

void QgsComposerPictureWidget::on_mRadioExpression_clicked()
{
if ( !mPicture )
{
return;
}

mPicture->beginCommand( tr( "Picture source changed" ) );
mPicture->setUsePictureExpression( true );
mPicture->endCommand();

mPictureLineEdit->setEnabled( false );
mPictureBrowseButton->setEnabled( false );
mPictureExpressionLineEdit->setEnabled( true );
mPictureExpressionButton->setEnabled( true );
}

void QgsComposerPictureWidget::setPictureExpression()
{
if ( !mPicture )
{
return;
}

mPicture->beginCommand( tr( "Picture source expression" ) );
mPicture->setPictureExpression( mPictureExpressionLineEdit->text() );
mPicture->endCommand();
}

void QgsComposerPictureWidget::on_mRotationFromComposerMapCheckBox_stateChanged( int state )
{
if ( !mPicture )
@@ -438,9 +381,6 @@ void QgsComposerPictureWidget::setGuiElementValues()
mRotationFromComposerMapCheckBox->blockSignals( true );
mResizeModeComboBox->blockSignals( true );
mAnchorPointComboBox->blockSignals( true );
mRadioPath->blockSignals( true );
mRadioExpression->blockSignals( true );
mPictureExpressionLineEdit->blockSignals( true );

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

mRadioPath->setChecked( !( mPicture->usePictureExpression() ) );
mRadioExpression->setChecked( mPicture->usePictureExpression() );
mPictureLineEdit->setEnabled( !( mPicture->usePictureExpression() ) );
mPictureBrowseButton->setEnabled( !( mPicture->usePictureExpression() ) );
mPictureExpressionLineEdit->setEnabled( mPicture->usePictureExpression() );
mPictureExpressionButton->setEnabled( mPicture->usePictureExpression() );

mPictureExpressionLineEdit->setText( mPicture->pictureExpression() );

mRotationFromComposerMapCheckBox->blockSignals( false );
mPictureRotationSpinBox->blockSignals( false );
mPictureLineEdit->blockSignals( false );
mComposerMapComboBox->blockSignals( false );
mResizeModeComboBox->blockSignals( false );
mAnchorPointComboBox->blockSignals( false );
mRadioPath->blockSignals( false );
mRadioExpression->blockSignals( false );
mPictureExpressionLineEdit->blockSignals( false );

populateDataDefinedButtons();
}
}

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

QgsComposerObject::DataDefinedProperty QgsComposerPictureWidget::ddPropertyForWidget( QgsDataDefinedButton *widget )
{
if ( widget == mSourceDDBtn )
{
return QgsComposerObject::PictureSource;
}

return QgsComposerObject::NoProperty;
}

void QgsComposerPictureWidget::populateDataDefinedButtons()
{
QgsVectorLayer* vl = atlasCoverageLayer();

//block signals from data defined buttons
mSourceDDBtn->blockSignals( true );

//initialise buttons to use atlas coverage layer
mSourceDDBtn->init( vl, mPicture->dataDefinedProperty( QgsComposerObject::PictureSource ),
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::anyStringDesc() );

//initial state of controls - disable related controls when dd buttons are active
mPictureLineEdit->setEnabled( !mSourceDDBtn->isActive() );

//unblock signals from data defined buttons
mSourceDDBtn->blockSignals( false );
}

@@ -41,7 +41,6 @@ class QgsComposerPictureWidget: public QgsComposerItemBaseWidget, private Ui::Qg
public slots:
void on_mPictureBrowseButton_clicked();
void on_mPictureLineEdit_editingFinished();
void on_mPictureExpressionButton_clicked();
void on_mPictureRotationSpinBox_valueChanged( double d );
void on_mPreviewListWidget_currentItemChanged( QListWidgetItem* current, QListWidgetItem* previous );
void on_mAddDirectoryButton_clicked();
@@ -50,14 +49,17 @@ class QgsComposerPictureWidget: public QgsComposerItemBaseWidget, private Ui::Qg
void on_mComposerMapComboBox_activated( const QString & text );
void on_mResizeModeComboBox_currentIndexChanged( int index );
void on_mAnchorPointComboBox_currentIndexChanged( int index );
void on_mRadioPath_clicked();
void on_mRadioExpression_clicked();
void setPictureExpression();

protected:
void showEvent( QShowEvent * event );
void resizeEvent( QResizeEvent * event );

QgsComposerObject::DataDefinedProperty ddPropertyForWidget( QgsDataDefinedButton *widget );

protected slots:
/**Initializes data defined buttons to current atlas coverage layer*/
void populateDataDefinedButtons();

private slots:
/**Sets the GUI elements to the values of mPicture*/
void setGuiElementValues();
@@ -1274,8 +1274,6 @@ void QgsComposerItem::refreshDataDefinedProperty( const QgsComposerObject::DataD
refreshBlendMode();
}

//TODO
// QgsComposerBaseItem::refreshDataDefinedPropert
update();
}

@@ -85,7 +85,7 @@ bool QgsComposerObject::readXML( const QDomElement &itemElem, const QDomDocument
return true;
}

QgsDataDefined *QgsComposerObject::dataDefinedProperty( const QgsComposerObject::DataDefinedProperty property )
QgsDataDefined *QgsComposerObject::dataDefinedProperty( const QgsComposerObject::DataDefinedProperty property ) const
{
if ( property == QgsComposerObject::AllProperties || property == QgsComposerObject::NoProperty )
{
@@ -63,7 +63,9 @@ class CORE_EXPORT QgsComposerObject: public QObject
MapXMin, /*< map extent x minimum */
MapYMin, /*< map extent y minimum */
MapXMax, /*< map extent x maximum */
MapYMax /*< map extent y maximum */
MapYMax, /*< map extent y maximum */
//composer picture
PictureSource /*< picture source url */
};

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

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

0 comments on commit 460cee1

Please sign in to comment.