Skip to content

Commit

Permalink
[Feature] Path prefix for attachment widget can be defined by expression
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Mar 6, 2017
1 parent ac53a3d commit c1ede00
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/core/qgseditorwidgetwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class GUI_EXPORT QgsEditorWidgetWrapper : public QgsWidgetWrapper
*
* @param feature The new feature
*/
void setFeature( const QgsFeature &feature ) override;
virtual void setFeature( const QgsFeature &feature ) override;

/**
* Is called, when the value of the widget needs to be changed. Update the widget representation
Expand Down
137 changes: 97 additions & 40 deletions src/gui/editorwidgets/qgsexternalresourceconfigdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,30 @@
#include "qgsexternalresourcewidget.h"
#include "qgsproject.h"
#include "qgssettings.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsapplication.h"
#include "qgsvectorlayer.h"
#include "qgspropertyoverridebutton.h"

#include <QFileDialog>

class QgsExternalResourceWidgetWrapper;

const QgsPropertiesDefinition &QgsExternalResourceConfigDlg::propertyDefinitions()
{
static QgsPropertiesDefinition propertyDefinitions;

if ( propertyDefinitions.isEmpty() )
{
propertyDefinitions = QgsPropertiesDefinition
{
{ RootPath, QgsPropertyDefinition( "propertyRootPath", QgsPropertyDefinition::DataTypeString, QObject::tr( "Root path" ), QString() ) }
};
}

return propertyDefinitions;
}

QgsExternalResourceConfigDlg::QgsExternalResourceConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget *parent )
: QgsEditorConfigWidget( vl, fieldIdx, parent )
{
Expand All @@ -36,14 +55,17 @@ QgsExternalResourceConfigDlg::QgsExternalResourceConfigDlg( QgsVectorLayer *vl,

mRootPath->setPlaceholderText( QgsSettings().value( QStringLiteral( "/UI/lastExternalResourceWidgetDefaultPath" ), QDir::toNativeSeparators( QDir::cleanPath( defpath ) ) ).toString() );

// Add connection to button for choosing default path
connect( mRootPathButton, SIGNAL( clicked() ), this, SLOT( chooseDefaultPath() ) );
connect( mRootPathButton, &QToolButton::clicked, this, &QgsExternalResourceConfigDlg::chooseDefaultPath );

// Activate Relative Default Path option only if Default Path is set
connect( mRootPath, SIGNAL( textChanged( const QString & ) ), this, SLOT( enableRelativeDefault() ) );
mRootPathPropertyOverrideButton->init( RootPath, mPropertyCollection, propertyDefinitions(), vl );

// Dynamic GroupBox for relative paths option
connect( mRelativeGroupBox, SIGNAL( toggled( bool ) ), this, SLOT( enableRelative( bool ) ) );
mRootPathPropertyOverrideButton->setVectorLayer( vl );
connect( mRootPathPropertyOverrideButton, &QgsPropertyOverrideButton::changed, this, &QgsExternalResourceConfigDlg::rootPathPropertyChanged );

// Activate Relative Default Path option only if Default Path is set
connect( mRootPath, &QLineEdit::textChanged, this, &QgsExternalResourceConfigDlg::enableRelativeDefault );
connect( mRootPathExpression, &QLineEdit::textChanged, this, &QgsExternalResourceConfigDlg::enableRelativeDefault );
connect( mRelativeGroupBox, &QGroupBox::toggled, this, &QgsExternalResourceConfigDlg::enableRelativeDefault );

// set ids for StorageTypeButtons
mStorageButtonGroup->setId( mStoreFilesButton, QgsFileWidget::GetFile );
Expand All @@ -59,15 +81,15 @@ QgsExternalResourceConfigDlg::QgsExternalResourceConfigDlg( QgsVectorLayer *vl,
mDocumentViewerContentComboBox->addItem( tr( "Web view" ), QgsExternalResourceWidget::Web );


connect( mFileWidgetGroupBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
connect( mFileWidgetButtonGroupBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
connect( mFileWidgetGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
connect( mFileWidgetButtonGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
connect( mFileWidgetFilterLineEdit, SIGNAL( textChanged( QString ) ), this, SIGNAL( changed() ) );
connect( mUseLink, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
connect( mFullUrl, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
connect( mRootPath, SIGNAL( textChanged( QString ) ), this, SIGNAL( changed() ) );
connect( mUseLink, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
connect( mFullUrl, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
connect( mRootPath, &QLineEdit::textChanged, this, &QgsEditorConfigWidget::changed );
connect( mStorageButtonGroup, SIGNAL( buttonClicked( int ) ), this, SIGNAL( changed() ) );
connect( mRelativeGroupBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
connect( mDocumentViewerGroupBox, SIGNAL( toggled( bool ) ), this, SIGNAL( changed() ) );
connect( mRelativeGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
connect( mDocumentViewerGroupBox, &QGroupBox::toggled, this, &QgsEditorConfigWidget::changed );
connect( mDocumentViewerContentComboBox, SIGNAL( currentIndexChanged( int ) ), this, SIGNAL( changed() ) );
connect( mDocumentViewerHeight, SIGNAL( valueChanged( int ) ), this, SIGNAL( changed() ) );
connect( mDocumentViewerWidth, SIGNAL( valueChanged( int ) ), this, SIGNAL( changed() ) );
Expand All @@ -87,40 +109,44 @@ void QgsExternalResourceConfigDlg::chooseDefaultPath()

QString rootName = QFileDialog::getExistingDirectory( this, tr( "Select a directory" ), dir, QFileDialog::ShowDirsOnly );

if ( rootName.isNull() )
return;

mRootPath->setText( rootName );
if ( !rootName.isNull() )
mRootPath->setText( rootName );
}

void QgsExternalResourceConfigDlg::enableRelativeDefault()
void QgsExternalResourceConfigDlg::rootPathPropertyChanged()
{
// Activate (or not) the RelativeDefault button if default path
if ( mRelativeGroupBox->isChecked() )
mRelativeDefault->setEnabled( !mRootPath->text().isEmpty() );
QgsProperty prop = mRootPathPropertyOverrideButton->toProperty();

// If no default path, RelativeProj button enabled by default
if ( mRootPath->text().isEmpty() )
mRelativeProject->toggle();
setRootPathExpression( prop.expressionString() );

mRootPathExpression->setVisible( prop.isActive() );
mRootPath->setVisible( !prop.isActive() );
mRootPathButton->setEnabled( !prop.isActive() );
}

void QgsExternalResourceConfigDlg::enableRelative( bool state )
void QgsExternalResourceConfigDlg::enableRelativeDefault()
{
if ( state )
bool relativePathActive = false;

if ( mRootPathPropertyOverrideButton->isActive() )
{
mRelativeProject->setEnabled( true );
if ( mRootPath->text().isEmpty() )
mRelativeDefault->setEnabled( false );
else
mRelativeDefault->setEnabled( true );
if ( !mRootPathExpression->text().isEmpty() )
relativePathActive = true;
}
else
{
mRelativeProject->setEnabled( false );
mRelativeDefault->setEnabled( false );
if ( !mRootPath->text().isEmpty() )
relativePathActive = true;
}
}

// Activate (or not) the RelativeDefault button if default path
if ( mRelativeGroupBox->isChecked() )
mRelativeDefault->setEnabled( relativePathActive );

// If no default path, RelativeProj button enabled by default
if ( !relativePathActive )
mRelativeProject->toggle();
}

QVariantMap QgsExternalResourceConfigDlg::config()
{
Expand All @@ -137,10 +163,17 @@ QVariantMap QgsExternalResourceConfigDlg::config()
cfg.insert( QStringLiteral( "FullUrl" ), mFullUrl->isChecked() );
}

if ( mRootPathPropertyOverrideButton->isActive() )
cfg.insert( QStringLiteral( "DefaultRootStyle" ), QStringLiteral( "expression" ) );
else
cfg.insert( QStringLiteral( "DefaultRootStyle" ), QStringLiteral( "path" ) );


if ( !mRootPath->text().isEmpty() )
{
cfg.insert( QStringLiteral( "DefaultRoot" ), mRootPath->text() );
}

if ( !mRootPathExpression->text().isEmpty() )
cfg.insert( QStringLiteral( "DefaultRootExpression" ), mRootPathExpression->toolTip() );

// Save Storage Mode
cfg.insert( QStringLiteral( "StorageMode" ), mStorageButtonGroup->checkedId() );
Expand Down Expand Up @@ -192,10 +225,18 @@ void QgsExternalResourceConfigDlg::setConfig( const QVariantMap &config )
mFullUrl->setChecked( true );
}

if ( config.contains( QStringLiteral( "DefaultRoot" ) ) )
{
mRootPath->setText( config.value( QStringLiteral( "DefaultRoot" ) ).toString() );
}
mRootPath->setText( config.value( QStringLiteral( "DefaultRoot" ) ).toString() );
setRootPathExpression( config.value( QStringLiteral( "DefaultRootExpression" ) ).toString() );

bool rootPathIsExpression = config.value( QStringLiteral( "DefaultRootStyle" ) ) == QStringLiteral( "expression" );

QgsProperty prop = mRootPathPropertyOverrideButton->toProperty();
prop.setActive( rootPathIsExpression );
mRootPathPropertyOverrideButton->setToProperty( prop );
rootPathPropertyChanged();

mRootPathExpression->setVisible( rootPathIsExpression );
mRootPath->setVisible( !rootPathIsExpression );

// relative storage
if ( config.contains( QStringLiteral( "RelativeStorage" ) ) )
Expand Down Expand Up @@ -239,3 +280,19 @@ void QgsExternalResourceConfigDlg::setConfig( const QVariantMap &config )
}
}
}

void QgsExternalResourceConfigDlg::setRootPathExpression( const QString &expression )
{
mRootPathExpression->setToolTip( expression );
mRootPathPropertyOverrideButton->setText( expression );

QgsProperty prop = mRootPathPropertyOverrideButton->toProperty();
prop.setExpressionString( expression );
mRootPathPropertyOverrideButton->setToProperty( prop );

QgsExpression exp( expression );
QgsExpressionContext ctx = layer()->createExpressionContext();

mRootPathExpression->setText( exp.evaluate( &ctx ).toString() );
enableRelativeDefault();
}
15 changes: 13 additions & 2 deletions src/gui/editorwidgets/qgsexternalresourceconfigdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,22 @@ class GUI_EXPORT QgsExternalResourceConfigDlg : public QgsEditorConfigWidget, pr
//! Choose a base directory for rootPath
void chooseDefaultPath();

void rootPathPropertyChanged();

//! Modify RelativeDefault according to mRootPath content
void enableRelativeDefault();

//! Dynamic activation of RelativeGroupBox
void enableRelative( bool state );
private:
enum Properties
{
RootPath
};

void setRootPathExpression( const QString &expression );

const QgsPropertiesDefinition &propertyDefinitions();

QgsPropertyCollection mPropertyCollection;
};

#endif // QGSEXTERNALRESOURCECONFIGDLG_H
62 changes: 43 additions & 19 deletions src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <QSettings>
#include <QLabel>


#include "qgsproject.h"
#include "qgsexternalresourcewidget.h"
#include "qgsfilterlineedit.h"

Expand Down Expand Up @@ -77,6 +77,21 @@ bool QgsExternalResourceWidgetWrapper::valid() const
return mLineEdit || mLabel || mQgsWidget;
}

void QgsExternalResourceWidgetWrapper::setFeature( const QgsFeature &feature )
{
if ( mQgsWidget && mDefaultRootExpression.isValid() )
{
QgsExpressionContext expressionContext = QgsExpressionContextUtils::createFeatureBasedContext( feature, layer()->fields() );

QVariant value = mDefaultRootExpression.evaluate( &expressionContext );
qWarning() << "Default root << " << value.toString();

mQgsWidget->setDefaultRoot( value.toString() );
}

QgsEditorWidgetWrapper::setFeature( feature );
}

QWidget *QgsExternalResourceWidgetWrapper::createWidget( QWidget *parent )
{
return new QgsExternalResourceWidget( parent );
Expand All @@ -102,41 +117,50 @@ void QgsExternalResourceWidgetWrapper::initWidget( QWidget *editor )
if ( mQgsWidget )
{
mQgsWidget->fileWidget()->setStorageMode( QgsFileWidget::GetFile );
if ( config().contains( QStringLiteral( "UseLink" ) ) )

QVariantMap cfg = config();

if ( cfg.contains( QStringLiteral( "UseLink" ) ) )
{
mQgsWidget->fileWidget()->setUseLink( config( QStringLiteral( "UseLink" ) ).toBool() );
mQgsWidget->fileWidget()->setUseLink( cfg.value( QStringLiteral( "UseLink" ) ).toBool() );
}
if ( config().contains( QStringLiteral( "FullUrl" ) ) )
if ( cfg.contains( QStringLiteral( "FullUrl" ) ) )
{
mQgsWidget->fileWidget()->setFullUrl( config( QStringLiteral( "FullUrl" ) ).toBool() );
mQgsWidget->fileWidget()->setFullUrl( cfg.value( QStringLiteral( "FullUrl" ) ).toBool() );
}
if ( config().contains( QStringLiteral( "DefaultRoot" ) ) )

qWarning() << "Default root style " << cfg.value( QStringLiteral( "DefaultRootStyle" ) );
if ( cfg.value( QStringLiteral( "DefaultRootStyle" ) ).toString() == QStringLiteral( "expression" ) )
{
mDefaultRootExpression = QgsExpression( cfg.value( QStringLiteral( "DefaultRootExpression" ) ).toString() );
}
else
{
mQgsWidget->setDefaultRoot( config( QStringLiteral( "DefaultRoot" ) ).toString() );
mQgsWidget->setDefaultRoot( cfg.value( QStringLiteral( "DefaultRoot" ) ).toString() );
}
if ( config().contains( QStringLiteral( "StorageMode" ) ) )
if ( cfg.contains( QStringLiteral( "StorageMode" ) ) )
{
mQgsWidget->fileWidget()->setStorageMode( ( QgsFileWidget::StorageMode )config( QStringLiteral( "StorageMode" ) ).toInt() );
mQgsWidget->fileWidget()->setStorageMode( ( QgsFileWidget::StorageMode )cfg.value( QStringLiteral( "StorageMode" ) ).toInt() );
}
if ( config().contains( QStringLiteral( "RelativeStorage" ) ) )
if ( cfg.contains( QStringLiteral( "RelativeStorage" ) ) )
{
mQgsWidget->setRelativeStorage( ( QgsFileWidget::RelativeStorage )config( QStringLiteral( "RelativeStorage" ) ).toInt() );
mQgsWidget->setRelativeStorage( ( QgsFileWidget::RelativeStorage )cfg.value( QStringLiteral( "RelativeStorage" ) ).toInt() );
}
if ( config().contains( QStringLiteral( "FileWidget" ) ) )
if ( cfg.contains( QStringLiteral( "FileWidget" ) ) )
{
mQgsWidget->setFileWidgetVisible( config( QStringLiteral( "FileWidget" ) ).toBool() );
mQgsWidget->setFileWidgetVisible( cfg.value( QStringLiteral( "FileWidget" ) ).toBool() );
}
if ( config().contains( QStringLiteral( "FileWidgetButton" ) ) )
if ( cfg.contains( QStringLiteral( "FileWidgetButton" ) ) )
{
mQgsWidget->fileWidget()->setFileWidgetButtonVisible( config( QStringLiteral( "FileWidgetButton" ) ).toBool() );
mQgsWidget->fileWidget()->setFileWidgetButtonVisible( cfg.value( QStringLiteral( "FileWidgetButton" ) ).toBool() );
}
if ( config().contains( QStringLiteral( "DocumentViewer" ) ) )
if ( cfg.contains( QStringLiteral( "DocumentViewer" ) ) )
{
mQgsWidget->setDocumentViewerContent( ( QgsExternalResourceWidget::DocumentViewerContent )config( QStringLiteral( "DocumentViewer" ) ).toInt() );
mQgsWidget->setDocumentViewerContent( ( QgsExternalResourceWidget::DocumentViewerContent )cfg.value( QStringLiteral( "DocumentViewer" ) ).toInt() );
}
if ( config().contains( QStringLiteral( "FileWidgetFilter" ) ) )
if ( cfg.contains( QStringLiteral( "FileWidgetFilter" ) ) )
{
mQgsWidget->fileWidget()->setFilter( config( QStringLiteral( "FileWidgetFilter" ) ).toString() );
mQgsWidget->fileWidget()->setFilter( cfg.value( QStringLiteral( "FileWidgetFilter" ) ).toString() );
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/gui/editorwidgets/qgsexternalresourcewidgetwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class GUI_EXPORT QgsExternalResourceWidgetWrapper : public QgsEditorWidgetWrappe
bool valid() const override;

public slots:
void setFeature( const QgsFeature &feature ) override;
void setValue( const QVariant &value ) override;
void setEnabled( bool enabled ) override;

Expand All @@ -61,7 +62,7 @@ class GUI_EXPORT QgsExternalResourceWidgetWrapper : public QgsEditorWidgetWrappe
QLabel *mLabel = nullptr;
QgsExternalResourceWidget *mQgsWidget = nullptr;


QgsExpression mDefaultRootExpression;
};

#endif // QGSEXTERNALRESOURCEWIDGETWRAPPER_H
Loading

0 comments on commit c1ede00

Please sign in to comment.