Skip to content

Commit

Permalink
Correctly expose metadata tab for mesh layers
Browse files Browse the repository at this point in the history
Just like all other map layer types, meshes CAN have metadata set,
so expose this via a metadata tab in their layer properties window
just like any other layer type.
  • Loading branch information
nyalldawson committed Oct 20, 2020
1 parent f81aee4 commit 9482b1b
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 10 deletions.
2 changes: 2 additions & 0 deletions python/gui/auto_generated/mesh/qgsmeshlayerproperties.sip.in
Expand Up @@ -38,6 +38,8 @@ Adds properties page from a factory
.. versionadded:: 3.16
%End

protected slots:

};


Expand Down
101 changes: 96 additions & 5 deletions src/gui/mesh/qgsmeshlayerproperties.cpp
Expand Up @@ -40,15 +40,16 @@
#include "qgsmaplayerconfigwidgetfactory.h"
#include "qgsgui.h"
#include "qgsnative.h"
#include "qgsmetadatawidget.h"

#include <QDesktopServices>
#include <QFileDialog>
#include <QMessageBox>

QgsMeshLayerProperties::QgsMeshLayerProperties( QgsMapLayer *lyr, QgsMapCanvas *canvas, QWidget *parent, Qt::WindowFlags fl )
: QgsOptionsDialogBase( QStringLiteral( "MeshLayerProperties" ), parent, fl )
, mMeshLayer( qobject_cast<QgsMeshLayer *>( lyr ) ),
mCanvas( canvas )
, mMeshLayer( qobject_cast<QgsMeshLayer *>( lyr ) )
, mCanvas( canvas )
{
Q_ASSERT( mMeshLayer );

Expand Down Expand Up @@ -93,6 +94,16 @@ QgsMeshLayerProperties::QgsMeshLayerProperties( QgsMapLayer *lyr, QgsMapCanvas *
mComboBoxTemporalDatasetMatchingMethod->addItem( tr( "Find Closest Dataset From Requested Time (After or Before)" ),
QgsMeshDataProviderTemporalCapabilities::FindClosestDatasetFromStartRangeTime );

QVBoxLayout *layout = new QVBoxLayout( metadataFrame );
layout->setContentsMargins( 0, 0, 0, 0 );
metadataFrame->setContentsMargins( 0, 0, 0, 0 );
mMetadataWidget = new QgsMetadataWidget( this, mMeshLayer );
mMetadataWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
mMetadataWidget->setMapCanvas( mCanvas );
layout->addWidget( mMetadataWidget );
metadataFrame->setLayout( layout );
mOptsPage_Metadata->setContentsMargins( 0, 0, 0, 0 );

// update based on lyr's current state
syncToLayer();

Expand All @@ -118,17 +129,24 @@ QgsMeshLayerProperties::QgsMeshLayerProperties( QgsMapLayer *lyr, QgsMapCanvas *
mOptsPage_Rendering->setProperty( "helpPage", QStringLiteral( "working_with_mesh/mesh_properties.html#rendering-properties" ) );


QPushButton *btnStyle = new QPushButton( tr( "Style" ) );
mBtnStyle = new QPushButton( tr( "Style" ) );
QMenu *menuStyle = new QMenu( this );
menuStyle->addAction( tr( "Load Style…" ), this, &QgsMeshLayerProperties::loadStyle );
menuStyle->addAction( tr( "Save Style…" ), this, &QgsMeshLayerProperties::saveStyleAs );
menuStyle->addSeparator();
menuStyle->addAction( tr( "Save as Default" ), this, &QgsMeshLayerProperties::saveDefaultStyle );
menuStyle->addAction( tr( "Restore Default" ), this, &QgsMeshLayerProperties::loadDefaultStyle );
btnStyle->setMenu( menuStyle );
mBtnStyle->setMenu( menuStyle );
connect( menuStyle, &QMenu::aboutToShow, this, &QgsMeshLayerProperties::aboutToShowStyleMenu );

buttonBox->addButton( btnStyle, QDialogButtonBox::ResetRole );
buttonBox->addButton( mBtnStyle, QDialogButtonBox::ResetRole );

mBtnMetadata = new QPushButton( tr( "Metadata" ), this );
QMenu *menuMetadata = new QMenu( this );
mActionLoadMetadata = menuMetadata->addAction( tr( "Load Metadata…" ), this, &QgsMeshLayerProperties::loadMetadata );
mActionSaveMetadataAs = menuMetadata->addAction( tr( "Save Metadata…" ), this, &QgsMeshLayerProperties::saveMetadataAs );
mBtnMetadata->setMenu( menuMetadata );
buttonBox->addButton( mBtnMetadata, QDialogButtonBox::ResetRole );
}

void QgsMeshLayerProperties::addPropertiesPageFactory( QgsMapLayerConfigWidgetFactory *factory )
Expand All @@ -153,6 +171,15 @@ void QgsMeshLayerProperties::addPropertiesPageFactory( QgsMapLayerConfigWidgetFa

}

void QgsMeshLayerProperties::optionsStackedWidget_CurrentChanged( int index )
{
QgsOptionsDialogBase::optionsStackedWidget_CurrentChanged( index );

bool isMetadataPanel = ( index == mOptStackedWidget->indexOf( mOptsPage_Metadata ) );
mBtnStyle->setVisible( ! isMetadataPanel );
mBtnMetadata->setVisible( isMetadataPanel );
}

void QgsMeshLayerProperties::syncToLayer()
{
Q_ASSERT( mRendererMeshPropertiesWidget );
Expand Down Expand Up @@ -361,6 +388,8 @@ void QgsMeshLayerProperties::apply()
mMeshLayer->setTemporalMatchingMethod( static_cast<QgsMeshDataProviderTemporalCapabilities::MatchingTemporalDatasetMethod>(
mComboBoxTemporalDatasetMatchingMethod->currentData().toInt() ) );

mMetadataWidget->acceptMetadata();

if ( needMeshUpdating )
mMeshLayer->reload();

Expand Down Expand Up @@ -461,3 +490,65 @@ void QgsMeshLayerProperties::urlClicked( const QUrl &url )
else
QDesktopServices::openUrl( url );
}

void QgsMeshLayerProperties::loadMetadata()
{
QgsSettings myQSettings; // where we keep last used filter in persistent state
QString myLastUsedDir = myQSettings.value( QStringLiteral( "style/lastStyleDir" ), QDir::homePath() ).toString();

QString myFileName = QFileDialog::getOpenFileName( this, tr( "Load layer metadata from metadata file" ), myLastUsedDir,
tr( "QGIS Layer Metadata File" ) + " (*.qmd)" );
if ( myFileName.isNull() )
{
return;
}

QString myMessage;
bool defaultLoadedFlag = false;
myMessage = mMeshLayer->loadNamedMetadata( myFileName, defaultLoadedFlag );

//reset if the default style was loaded OK only
if ( defaultLoadedFlag )
{
mMetadataWidget->setMetadata( &mMeshLayer->metadata() );
}
else
{
//let the user know what went wrong
QMessageBox::warning( this, tr( "Load Metadata" ), myMessage );
}

QFileInfo myFI( myFileName );
QString myPath = myFI.path();
myQSettings.setValue( QStringLiteral( "style/lastStyleDir" ), myPath );

activateWindow(); // set focus back to properties dialog
}

void QgsMeshLayerProperties::saveMetadataAs()
{
QgsSettings myQSettings; // where we keep last used filter in persistent state
QString myLastUsedDir = myQSettings.value( QStringLiteral( "style/lastStyleDir" ), QDir::homePath() ).toString();

QString myOutputFileName = QFileDialog::getSaveFileName( this, tr( "Save Layer Metadata as QMD" ),
myLastUsedDir, tr( "QMD File" ) + " (*.qmd)" );
if ( myOutputFileName.isNull() ) //dialog canceled
{
return;
}

mMetadataWidget->acceptMetadata();

//ensure the user never omitted the extension from the file name
if ( !myOutputFileName.endsWith( QgsMapLayer::extensionPropertyType( QgsMapLayer::Metadata ), Qt::CaseInsensitive ) )
{
myOutputFileName += QgsMapLayer::extensionPropertyType( QgsMapLayer::Metadata );
}

bool defaultLoadedFlag = false;
QString message = mMeshLayer->saveNamedMetadata( myOutputFileName, defaultLoadedFlag );
if ( defaultLoadedFlag )
myQSettings.setValue( QStringLiteral( "style/lastStyleDir" ), QFileInfo( myOutputFileName ).absolutePath() );
else
QMessageBox::information( this, tr( "Save Metadata" ), message );
}
14 changes: 13 additions & 1 deletion src/gui/mesh/qgsmeshlayerproperties.h
Expand Up @@ -32,6 +32,7 @@ class QgsMapLayerConfigWidget;
class QgsMeshLayer3DRendererWidget;
class QgsMeshStaticDatasetWidget;
class QgsMapLayerConfigWidgetFactory;
class QgsMetadataWidget;

/**
* Property sheet for a mesh map layer.
Expand All @@ -58,6 +59,9 @@ class GUI_EXPORT QgsMeshLayerProperties : public QgsOptionsDialogBase, private U
*/
void addPropertiesPageFactory( QgsMapLayerConfigWidgetFactory *factory );

protected slots:
void optionsStackedWidget_CurrentChanged( int index ) override SIP_SKIP ;

private slots:
//! Synchronizes widgets state with associated mesh layer
void syncToLayer();
Expand Down Expand Up @@ -88,6 +92,8 @@ class GUI_EXPORT QgsMeshLayerProperties : public QgsOptionsDialogBase, private U
void onStaticDatasetCheckBoxChanged();

void urlClicked( const QUrl &url );
void loadMetadata();
void saveMetadataAs();

private:
//! Pointer to the mesh styling widget
Expand All @@ -107,7 +113,13 @@ class GUI_EXPORT QgsMeshLayerProperties : public QgsOptionsDialogBase, private U
*/
QgsMapLayerStyle mOldStyle;

QgsMapCanvas *mCanvas;
QPushButton *mBtnStyle = nullptr;
QPushButton *mBtnMetadata = nullptr;
QAction *mActionLoadMetadata = nullptr;
QAction *mActionSaveMetadataAs = nullptr;

QgsMapCanvas *mCanvas = nullptr;
QgsMetadataWidget *mMetadataWidget = nullptr;

bool mIsMapSettingsTemporal = false;

Expand Down
55 changes: 51 additions & 4 deletions src/ui/mesh/qgsmeshlayerpropertiesbase.ui
Expand Up @@ -102,6 +102,9 @@
<property name="text">
<string>Information</string>
</property>
<property name="toolTip">
<string>Information</string>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/propertyicons/metadata.svg</normaloff>:/images/themes/default/propertyicons/metadata.svg</iconset>
Expand All @@ -111,6 +114,9 @@
<property name="text">
<string>Source</string>
</property>
<property name="toolTip">
<string>Source</string>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/propertyicons/system.svg</normaloff>:/images/themes/default/propertyicons/system.svg</iconset>
Expand All @@ -132,6 +138,9 @@
<property name="text">
<string>Rendering</string>
</property>
<property name="toolTip">
<string>Rendering</string>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/propertyicons/rendering.svg</normaloff>:/images/themes/default/propertyicons/rendering.svg</iconset>
Expand All @@ -149,6 +158,18 @@
<normaloff>:/images/themes/default/propertyicons/temporal.svg</normaloff>:/images/themes/default/propertyicons/temporal.svg</iconset>
</property>
</item>
<item>
<property name="text">
<string>Metadata</string>
</property>
<property name="toolTip">
<string>Metadata</string>
</property>
<property name="icon">
<iconset resource="../../../images/images.qrc">
<normaloff>:/images/themes/default/propertyicons/editmetadata.svg</normaloff>:/images/themes/default/propertyicons/editmetadata.svg</iconset>
</property>
</item>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -239,8 +260,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>661</width>
<height>506</height>
<width>470</width>
<height>383</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
Expand Down Expand Up @@ -452,8 +473,8 @@ border-radius: 2px;</string>
<rect>
<x>0</x>
<y>0</y>
<width>661</width>
<height>506</height>
<width>100</width>
<height>30</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_10">
Expand Down Expand Up @@ -748,6 +769,32 @@ border-radius: 2px;</string>
</item>
</layout>
</widget>
<widget class="QWidget" name="mOptsPage_Metadata">
<layout class="QVBoxLayout" name="verticalLayout_8">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QFrame" name="metadataFrame">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
Expand Down

0 comments on commit 9482b1b

Please sign in to comment.