Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load style from layers panel 55165 #70

Open
wants to merge 3 commits into
base: Repo_artifactComments
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8417,6 +8417,59 @@ void QgisApp::saveStyleFile( QgsMapLayer *layer )
}
}

void QgisApp::loadStyleFile( QgsMapLayer *layer )
{
if ( !layer )
{
layer = activeLayer();
}

if ( !layer || !layer->dataProvider() )
return;

switch ( layer->type() )
{

case Qgis::LayerType::Vector:
QgsVectorLayerProperties( mMapCanvas,
visibleMessageBar(),
qobject_cast<QgsVectorLayer *>( layer ) ).loadStyle();
break;

case Qgis::LayerType::Raster:
QgsRasterLayerProperties( layer, mMapCanvas ).loadStyle();
break;

case Qgis::LayerType::Mesh:
QgsMeshLayerProperties( layer, mMapCanvas ).loadStyleFromFile();
break;

case Qgis::LayerType::VectorTile:
QgsVectorTileLayerProperties( qobject_cast<QgsVectorTileLayer *>( layer ),
mMapCanvas,
visibleMessageBar() ).loadStyle();
break;

case Qgis::LayerType::PointCloud:
QgsPointCloudLayerProperties( qobject_cast<QgsPointCloudLayer *>( layer ),
mMapCanvas,
visibleMessageBar() ).loadStyleFromFile();
break;

case Qgis::LayerType::TiledScene:
QgsTiledSceneLayerProperties( qobject_cast<QgsTiledSceneLayer *>( layer ),
mMapCanvas,
visibleMessageBar() ).loadStyleFromFile();
break;

// Not available for these
case Qgis::LayerType::Annotation:
case Qgis::LayerType::Plugin:
case Qgis::LayerType::Group:
break;
}
}

///@cond PRIVATE

/**
Expand Down
8 changes: 5 additions & 3 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,13 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
*/
void makeMemoryLayerPermanent( QgsVectorLayer *layer );

//! save qml style for the current layer
//! Loads qml style file to the current layer
void loadStyleFile( QgsMapLayer *layer = nullptr );
//! Saves qml style for the current layer
void saveStyleFile( QgsMapLayer *layer = nullptr );
//! save qrl definition for the current layer
//! Saves qlr definition for the current layer
void saveAsLayerDefinition();
//! save current raster layer
//! Saves current raster layer
QString saveAsRasterFile( QgsRasterLayer *layer = nullptr, bool defaultAddToCanvas = true );

/**
Expand Down
12 changes: 10 additions & 2 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
menu->addAction( actionMakePermanent );
}
// save as vector file
QMenu *menuExportVector = new QMenu( tr( "E&xport" ), menu );
QMenu *menuExportVector = new QMenu( tr( "Import/E&xport" ), menu );
menuExportVector->setObjectName( QStringLiteral( "exportMenu" ) );
QAction *actionSaveAs = new QAction( tr( "Save Features &As…" ), menuExportVector );
connect( actionSaveAs, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->saveAsFile(); } );
Expand All @@ -573,6 +573,10 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
QAction *actionSaveStyle = new QAction( tr( "Save as &QGIS Layer Style File…" ), menuExportVector );
connect( actionSaveStyle, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->saveStyleFile(); } );
menuExportVector->addAction( actionSaveStyle );
menuExportVector->addSeparator();
QAction *actionLoadStyle = new QAction( tr( "&Load a QGIS Layer Style File…" ), menuExportVector );
connect( actionLoadStyle, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->loadStyleFile(); } );
menuExportVector->addAction( actionLoadStyle );
}
menu->addMenu( menuExportVector );
}
Expand All @@ -586,18 +590,22 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
{
bool enableSaveAs = ( pcLayer && pcLayer->isValid() && pcLayer->dataProvider()->hasValidIndex() ) ||
( rlayer && rlayer->isValid() );
QMenu *menuExportRaster = new QMenu( tr( "E&xport" ), menu );
QMenu *menuExportRaster = new QMenu( tr( "Import/E&xport" ), menu );
menuExportRaster->setObjectName( QStringLiteral( "exportMenu" ) );
QAction *actionSaveAs = new QAction( tr( "Save &As…" ), menuExportRaster );
QAction *actionSaveAsDefinitionLayer = new QAction( tr( "Save as Layer &Definition File…" ), menuExportRaster );
QAction *actionSaveStyle = new QAction( tr( "Save as &QGIS Layer Style File…" ), menuExportRaster );
QAction *actionLoadStyle = new QAction( tr( "&Load a QGIS Layer Style File…" ), menuExportRaster );
connect( actionSaveAs, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->saveAsFile(); } );
menuExportRaster->addAction( actionSaveAs );
actionSaveAs->setEnabled( enableSaveAs );
connect( actionSaveAsDefinitionLayer, &QAction::triggered, QgisApp::instance(), &QgisApp::saveAsLayerDefinition );
menuExportRaster->addAction( actionSaveAsDefinitionLayer );
connect( actionSaveStyle, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->saveStyleFile(); } );
menuExportRaster->addAction( actionSaveStyle );
menuExportRaster->addSeparator();
connect( actionLoadStyle, &QAction::triggered, QgisApp::instance(), [ = ] { QgisApp::instance()->loadStyleFile(); } );
menuExportRaster->addAction( actionLoadStyle );
menu->addMenu( menuExportRaster );
}
break;
Expand Down
14 changes: 7 additions & 7 deletions src/gui/qgslayerpropertiesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void QgsLayerPropertiesDialog::loadStyleFromFile()

QString fileName = QFileDialog::getOpenFileName(
this,
tr( "Load layer properties from style file" ),
tr( "Load Layer Properties from Style File" ),
lastUsedDir,
tr( "QGIS Layer Style File" ) + " (*.qml)" );
if ( fileName.isEmpty() )
Expand Down Expand Up @@ -198,7 +198,7 @@ void QgsLayerPropertiesDialog::saveStyleToFile()

QString outputFileName = QFileDialog::getSaveFileName(
this,
tr( "Save layer properties as style file" ),
tr( "Save Layer Properties as Style File" ),
lastUsedDir,
tr( "QGIS Layer Style File" ) + " (*.qml)" );
// return dialog focus on Mac
Expand Down Expand Up @@ -369,7 +369,7 @@ void QgsLayerPropertiesDialog::saveDefaultStyle()
QString errorMessage;
if ( QgsProviderRegistry::instance()->styleExists( mLayer->providerType(), mLayer->source(), QString(), errorMessage ) )
{
if ( QMessageBox::question( nullptr, QObject::tr( "Save style in database" ),
if ( QMessageBox::question( nullptr, QObject::tr( "Save Style to Database" ),
QObject::tr( "A matching style already exists in the database for this layer. Do you want to overwrite it?" ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
{
Expand All @@ -378,7 +378,7 @@ void QgsLayerPropertiesDialog::saveDefaultStyle()
}
else if ( !errorMessage.isEmpty() )
{
QMessageBox::warning( nullptr, QObject::tr( "Save style in database" ),
QMessageBox::warning( nullptr, QObject::tr( "Save Style to Database" ),
errorMessage );
return;
}
Expand Down Expand Up @@ -441,13 +441,13 @@ void QgsLayerPropertiesDialog::saveStyleAs()
}
case DatasourceDatabase:
{
QString infoWindowTitle = QObject::tr( "Save style to DB (%1)" ).arg( mLayer->providerType() );
QString infoWindowTitle = QObject::tr( "Save Style to DB (%1)" ).arg( mLayer->providerType() );

QgsMapLayerSaveStyleDialog::SaveToDbSettings dbSettings = dlg.saveToDbSettings();

if ( QgsProviderRegistry::instance()->styleExists( mLayer->providerType(), mLayer->source(), dbSettings.name, errorMessage ) )
{
if ( QMessageBox::question( nullptr, QObject::tr( "Save style in database" ),
if ( QMessageBox::question( nullptr, QObject::tr( "Save Style to Database" ),
QObject::tr( "A matching style already exists in the database for this layer. Do you want to overwrite it?" ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::No )
{
Expand All @@ -474,7 +474,7 @@ void QgsLayerPropertiesDialog::saveStyleAs()
}
case UserDatabase:
{
QString infoWindowTitle = tr( "Save default style to local database" );
QString infoWindowTitle = tr( "Save Default Style to Local Database" );
errorMessage = mLayer->saveDefaultStyle( defaultLoadedFlag, dlg.styleCategories() );
if ( !defaultLoadedFlag )
{
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsmaplayerstylemanagerwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ void QgsMapLayerStyleManagerWidget::loadStyle()
break;

case Qgis::LayerType::Raster:
QgsRasterLayerProperties( mLayer, mMapCanvas ).loadStyleFromFile();
QgsRasterLayerProperties( mLayer, mMapCanvas ).loadStyle();
break;

case Qgis::LayerType::Mesh:
Expand Down