QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsvectortilelayerproperties.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectortilelayerproperties.cpp
3 --------------------------------------
4 Date : May 2020
5 Copyright : (C) 2020 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgsapplication.h"
20#include "qgsgui.h"
21#include "qgshelp.h"
22#include "qgsjsonutils.h"
27#include "qgsmetadatawidget.h"
32#include "qgsvectortilelayer.h"
33#include "qgsvectortileutils.h"
34
35#include <QDesktopServices>
36#include <QFileDialog>
37#include <QMenu>
38#include <QMessageBox>
39#include <QString>
40#include <QTextStream>
41
42#include "moc_qgsvectortilelayerproperties.cpp"
43
44using namespace Qt::StringLiterals;
45
46QgsVectorTileLayerProperties::QgsVectorTileLayerProperties( QgsVectorTileLayer *lyr, QgsMapCanvas *canvas, QgsMessageBar *messageBar, QWidget *parent, Qt::WindowFlags flags )
47 : QgsLayerPropertiesDialog( lyr, canvas, u"VectorTileLayerProperties"_s, parent, flags )
48 , mLayer( lyr )
49{
50 setupUi( this );
51
52 mRendererWidget = new QgsVectorTileBasicRendererWidget( nullptr, canvas, messageBar, this );
53 mOptsPage_Style->layout()->addWidget( mRendererWidget );
54 mOptsPage_Style->layout()->setContentsMargins( 0, 0, 0, 0 );
55
56 mLabelingWidget = new QgsVectorTileBasicLabelingWidget( nullptr, canvas, messageBar, this );
57 mOptsPage_Labeling->layout()->addWidget( mLabelingWidget );
58 mOptsPage_Labeling->layout()->setContentsMargins( 0, 0, 0, 0 );
59
60 connect( this, &QDialog::accepted, this, &QgsVectorTileLayerProperties::apply );
61 connect( this, &QDialog::rejected, this, &QgsVectorTileLayerProperties::rollback );
62 connect( buttonBox->button( QDialogButtonBox::Apply ), &QAbstractButton::clicked, this, &QgsVectorTileLayerProperties::apply );
63 connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsVectorTileLayerProperties::showHelp );
64
65 connect( mCrsSelector, &QgsProjectionSelectionWidget::crsChanged, this, &QgsVectorTileLayerProperties::crsChanged );
66
67 // scale based layer visibility related widgets
68 mScaleRangeWidget->setMapCanvas( mCanvas );
69
70 // QgsOptionsDialogBase handles saving/restoring of geometry, splitter and current tab states,
71 // switching vertical tabs between icon/text to icon-only modes (splitter collapsed to left),
72 // and connecting QDialogButtonBox's accepted/rejected signals to dialog's accept/reject slots
73 initOptionsBase( false );
74
75 mSourceGroupBox->hide();
76
77 mOptsPage_Information->setContentsMargins( 0, 0, 0, 0 );
78
79 QVBoxLayout *layout = new QVBoxLayout( metadataFrame );
80 layout->setContentsMargins( 0, 0, 0, 0 );
81 metadataFrame->setContentsMargins( 0, 0, 0, 0 );
82 mMetadataWidget = new QgsMetadataWidget( this, mLayer );
83 mMetadataWidget->layout()->setContentsMargins( 0, 0, 0, 0 );
84 mMetadataWidget->setMapCanvas( mCanvas );
85 layout->addWidget( mMetadataWidget );
86 metadataFrame->setLayout( layout );
87 mOptsPage_Metadata->setContentsMargins( 0, 0, 0, 0 );
88
89 setMetadataWidget( mMetadataWidget, mOptsPage_Metadata );
90
91 mMapLayerServerPropertiesWidget->setHasWfsTitle( false );
92
93 // update based on lyr's current state
95
96 QgsSettings settings;
97 // if dialog hasn't been opened/closed yet, default to Styles tab, which is used most often
98 // this will be read by restoreOptionsBaseUi()
99 if ( !settings.contains( u"/Windows/VectorTileLayerProperties/tab"_s ) )
100 {
101 settings.setValue( u"Windows/VectorTileLayerProperties/tab"_s, mOptStackedWidget->indexOf( mOptsPage_Style ) );
102 }
103
104 mBtnStyle = new QPushButton( tr( "Style" ) );
105 QMenu *menuStyle = new QMenu( this );
106 menuStyle->addAction( tr( "Load Style…" ), this, &QgsVectorTileLayerProperties::loadStyle );
107 menuStyle->addAction( tr( "Save Style…" ), this, &QgsVectorTileLayerProperties::saveStyleToFile );
108 menuStyle->addSeparator();
109 menuStyle->addAction( tr( "Save as Default" ), this, &QgsVectorTileLayerProperties::saveStyleAsDefault );
110 menuStyle->addAction( tr( "Restore Default" ), this, &QgsVectorTileLayerProperties::loadDefaultStyle );
111 mBtnStyle->setMenu( menuStyle );
112 connect( menuStyle, &QMenu::aboutToShow, this, &QgsVectorTileLayerProperties::aboutToShowStyleMenu );
113
114 buttonBox->addButton( mBtnStyle, QDialogButtonBox::ResetRole );
115
116 mBtnMetadata = new QPushButton( tr( "Metadata" ), this );
117 QMenu *menuMetadata = new QMenu( this );
118 mActionLoadMetadata = menuMetadata->addAction( tr( "Load Metadata…" ), this, &QgsVectorTileLayerProperties::loadMetadataFromFile );
119 mActionSaveMetadataAs = menuMetadata->addAction( tr( "Save Metadata…" ), this, &QgsVectorTileLayerProperties::saveMetadataToFile );
120 mBtnMetadata->setMenu( menuMetadata );
121 buttonBox->addButton( mBtnMetadata, QDialogButtonBox::ResetRole );
122
123 initialize();
124}
125
127{
128 if ( mSourceWidget )
129 {
130 const QString newSource = mSourceWidget->sourceUri();
131 if ( newSource != mLayer->source() )
132 {
133 mLayer->setDataSource( newSource, mLayer->name(), mLayer->providerType(), QgsDataProvider::ProviderOptions() );
134 }
135 }
136
137 mLayer->setName( mLayerOrigNameLineEd->text() );
138 mLayer->setCrs( mCrsSelector->crs() );
139
140 mRendererWidget->apply();
141 mLabelingWidget->apply();
142 mMetadataWidget->acceptMetadata();
143
144 mLayer->setScaleBasedVisibility( chkUseScaleDependentRendering->isChecked() );
145 mLayer->setMinimumScale( mScaleRangeWidget->minimumScale() );
146 mLayer->setMaximumScale( mScaleRangeWidget->maximumScale() );
147
148 mMapLayerServerPropertiesWidget->save();
149}
150
152{
153 if ( !mLayer )
154 return;
155
156 /*
157 * Information Tab
158 */
160 // Inject the stylesheet
161 const QString html { mLayer->htmlMetadata().replace( "<head>"_L1, QStringLiteral( R"raw(<head><style type="text/css">%1</style>)raw" ) ).arg( myStyle ) };
162 mMetadataViewer->setHtml( html );
163
164 /*
165 * Source
166 */
167
168 mLayerOrigNameLineEd->setText( mLayer->name() );
169 mCrsSelector->setCrs( mLayer->crs() );
170
171 if ( !mSourceWidget )
172 {
173 mSourceWidget = QgsGui::sourceWidgetProviderRegistry()->createWidget( mLayer );
174 if ( mSourceWidget )
175 {
176 QHBoxLayout *layout = new QHBoxLayout();
177 layout->addWidget( mSourceWidget );
178 mSourceGroupBox->setLayout( layout );
179 if ( !mSourceWidget->groupTitle().isEmpty() )
180 mSourceGroupBox->setTitle( mSourceWidget->groupTitle() );
181 mSourceGroupBox->show();
182
183 connect( mSourceWidget, &QgsProviderSourceWidget::validChanged, this, [this]( bool isValid ) {
184 buttonBox->button( QDialogButtonBox::Apply )->setEnabled( isValid );
185 buttonBox->button( QDialogButtonBox::Ok )->setEnabled( isValid );
186 } );
187 }
188 }
189
190 if ( mSourceWidget )
191 {
192 mSourceWidget->setMapCanvas( mCanvas );
193 mSourceWidget->setSourceUri( mLayer->source() );
194 }
195
196 /*
197 * Symbology Tab
198 */
199 mRendererWidget->syncToLayer( mLayer );
200
201 /*
202 * Labels Tab
203 */
204 mLabelingWidget->setLayer( mLayer );
205
206 /*
207 * Rendering
208 */
209 chkUseScaleDependentRendering->setChecked( mLayer->hasScaleBasedVisibility() );
210 mScaleRangeWidget->setScaleRange( mLayer->minimumScale(), mLayer->maximumScale() );
211
212 mMapLayerServerPropertiesWidget->setServerProperties( mLayer->serverProperties() );
213}
214
219
221{
222 const QgsSettings settings; // where we keep last used filter in persistent state
223
224 QgsMapLayerLoadStyleDialog dlg( mLayer );
225
226 if ( dlg.exec() )
227 {
228 mOldStyle = mLayer->styleManager()->style( mLayer->styleManager()->currentStyle() );
229 const QgsMapLayer::StyleCategories categories = dlg.styleCategories();
230 const QString type = dlg.fileExtension();
231 if ( type.compare( "qml"_L1, Qt::CaseInsensitive ) == 0 )
232 {
233 QString message;
234 bool defaultLoadedFlag = false;
235 const QString filePath = dlg.filePath();
236 message = mLayer->loadNamedStyle( filePath, defaultLoadedFlag, categories );
237
238 //reset if the default style was loaded OK only
239 if ( defaultLoadedFlag )
240 {
241 syncToLayer();
242 }
243 else
244 {
245 //let the user know what went wrong
246 QMessageBox::warning( this, tr( "Load Style" ), message );
247 }
248 }
249 else if ( type.compare( "json"_L1, Qt::CaseInsensitive ) == 0 )
250 {
251 QFile file( dlg.filePath() );
252 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
253 {
254 QMessageBox::warning( this, tr( "Load Style" ), tr( "Could not read %1" ).arg( QDir::toNativeSeparators( dlg.filePath() ) ) );
255 }
256 else
257 {
258 QTextStream in( &file );
259 const QString content = in.readAll();
260
262 // convert automatically from pixel sizes to millimeters, because pixel sizes
263 // are a VERY edge case in QGIS and don't play nice with hidpi map renders or print layouts
265 //assume source uses 96 dpi
266 context.setPixelSizeConversionFactor( 25.4 / 96.0 );
267
268 //load sprites
269 QVariantMap styleDefinition = QgsJsonUtils::parseJson( content ).toMap();
270
271 QFileInfo fi( dlg.filePath() );
272 QgsVectorTileUtils::loadSprites( styleDefinition, context, u"file://"_s + fi.absolutePath() );
273
275
276 if ( converter.convert( content, &context ) != QgsMapBoxGlStyleConverter::Success )
277 {
278 QMessageBox::warning( this, tr( "Load Style" ), converter.errorMessage() );
279 }
280 else
281 {
283 {
284 mLayer->setRenderer( converter.renderer() );
285 }
287 {
288 mLayer->setLabeling( converter.labeling() );
289 }
290 syncToLayer();
291 }
292 }
293 }
294 activateWindow(); // set focus back to properties dialog
295 }
296}
297
302
303void QgsVectorTileLayerProperties::aboutToShowStyleMenu()
304{
305 QMenu *m = qobject_cast<QMenu *>( sender() );
306
308 // re-add style manager actions!
309 m->addSeparator();
311}
312
313void QgsVectorTileLayerProperties::showHelp()
314{
315 const QVariant helpPage = mOptionsStackedWidget->currentWidget()->property( "helpPage" );
316
317 if ( helpPage.isValid() )
318 {
319 QgsHelp::openHelp( helpPage.toString() );
320 }
321 else
322 {
323 QgsHelp::openHelp( u"working_with_vector_tiles/vector_tiles_properties.html"_s );
324 }
325}
326
327void QgsVectorTileLayerProperties::crsChanged( const QgsCoordinateReferenceSystem &crs )
328{
329 QgsDatumTransformDialog::run( crs, QgsProject::instance()->crs(), this, mCanvas, tr( "Select Transformation" ) );
330 mLayer->setCrs( crs );
331 mMetadataWidget->crsChanged();
332}
@ Millimeters
Millimeters.
Definition qgis.h:5291
static QString reportStyleSheet(QgsApplication::StyleSheetType styleSheetType=QgsApplication::StyleSheetType::Qt)
Returns a css style sheet for reports, the styleSheetType argument determines what type of stylesheet...
@ WebBrowser
StyleSheet for embedded browsers (QtWebKit), supports full standard CSS.
Represents a coordinate reference system (CRS).
static bool run(const QgsCoordinateReferenceSystem &sourceCrs=QgsCoordinateReferenceSystem(), const QgsCoordinateReferenceSystem &destinationCrs=QgsCoordinateReferenceSystem(), QWidget *parent=nullptr, QgsMapCanvas *mapCanvas=nullptr, const QString &windowTitle=QString())
Runs the dialog (if required) prompting for the desired transform to use from sourceCrs to destinatio...
static QgsProviderSourceWidgetProviderRegistry * sourceWidgetProviderRegistry()
Returns the registry of provider source widget providers.
Definition qgsgui.cpp:134
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:41
static QVariant parseJson(const std::string &jsonString)
Converts JSON jsonString to a QVariant, in case of parsing error an invalid QVariant is returned and ...
void saveMetadataToFile()
Allows the user to save the layer's metadata as a file.
virtual void rollback()
Rolls back changes made to the layer.
void saveStyleAsDefault()
Saves the current layer style as the default for the layer.
void setMetadataWidget(QgsMetadataWidget *widget, QWidget *page)
Sets the metadata widget and page associated with the dialog.
void loadDefaultStyle()
Reloads the default style for the layer.
virtual void apply()=0
Applies the dialog settings to the layer.
virtual void syncToLayer()=0
Resets the dialog to the current layer state.
void saveStyleToFile()
Allows the user to save the layer's style to a file.
QgsMapCanvas * mCanvas
Associated map canvas.
QgsLayerPropertiesDialog(QgsMapLayer *layer, QgsMapCanvas *canvas, const QString &settingsKey, QWidget *parent=nullptr, Qt::WindowFlags fl=Qt::WindowFlags(), QgsSettings *settings=nullptr)
Constructor for QgsLayerPropertiesDialog.
QgsMapLayerStyle mOldStyle
Previous layer style.
void loadMetadataFromFile()
Allows the user to load layer metadata from a file.
void initialize()
Initialize the dialog.
Context for a MapBox GL style conversion operation.
void setTargetUnit(Qgis::RenderUnit targetUnit)
Sets the target unit type.
void setPixelSizeConversionFactor(double sizeConversionFactor)
Sets the pixel size conversion factor, used to scale the original pixel sizes when converting styles.
Handles conversion of MapBox GL styles to QGIS vector tile renderers and labeling settings.
QgsVectorTileRenderer * renderer() const
Returns a new instance of a vector tile renderer representing the converted style,...
QgsVectorTileLabeling * labeling() const
Returns a new instance of a vector tile labeling representing the converted style,...
Result convert(const QVariantMap &style, QgsMapBoxGlStyleConversionContext *context=nullptr)
Converts a JSON style map, and returns the resultant status of the conversion.
@ Success
Conversion was successful.
QString errorMessage() const
Returns a descriptive error message if an error was encountered during the style conversion,...
Map canvas is a class for displaying all GIS data types on a canvas.
A reusable dialog which allows users to select stored layer styles and categories to load for a map l...
QgsMapLayer::StyleCategories styleCategories() const
Returns the list of selected style categories the user has opted to load.
QString filePath() const
Returns the full path to the selected layer style source file.
QString fileExtension() const
Returns the file extension for the selected layer style source file.
void removesExtraMenuSeparators(QMenu *m)
removes extra separators from the menu
void addStyleManagerActions(QMenu *m, QgsMapLayer *layer)
adds actions to the menu in accordance to the layer
static QgsMapLayerStyleGuiUtils * instance()
returns a singleton instance of this class
QString name
Definition qgsmaplayer.h:87
QString source() const
Returns the source for the layer.
QString providerType() const
Returns the provider type (provider key) for this layer.
void setDataSource(const QString &dataSource, const QString &baseName=QString(), const QString &provider=QString(), bool loadDefaultStyleFlag=false)
Updates the data source of the layer.
QFlags< StyleCategory > StyleCategories
@ Symbology
Symbology.
@ Labeling
Labeling.
A bar for displaying non-blocking messages to the user.
A wizard to edit metadata on a map layer.
QStackedWidget * mOptStackedWidget
void initOptionsBase(bool restoreUi=true, const QString &title=QString())
Set up the base ui connections for vertical tabs.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void crsChanged(const QgsCoordinateReferenceSystem &crs)
Emitted when the selected CRS is changed.
QgsProviderSourceWidget * createWidget(QgsMapLayer *layer, QWidget *parent=nullptr)
Creates a new widget to configure the source of the specified layer.
void validChanged(bool isValid)
Emitted whenever the validation status of the widget changes.
virtual QString sourceUri() const =0
Returns the source URI as currently defined by the widget.
Stores settings for use within QGIS.
Definition qgssettings.h:68
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Q_DECL_DEPRECATED void saveDefaultStyle() SIP_DEPRECATED
Saves the default style when appropriate button is pressed.
Q_DECL_DEPRECATED void saveStyleAs() SIP_DEPRECATED
Saves a style when appriate button is pressed.
void loadStyle()
Loads a saved style when appropriate button is pressed.
QgsVectorTileLayerProperties(QgsVectorTileLayer *lyr, QgsMapCanvas *canvas, QgsMessageBar *messageBar, QWidget *parent=nullptr, Qt::WindowFlags=QgsGuiUtils::ModalDialogFlags)
Constructor.
Implements a map layer that is dedicated to rendering of vector tiles.
static void loadSprites(const QVariantMap &styleDefinition, QgsMapBoxGlStyleConversionContext &context, const QString &styleUrl=QString())
Downloads the sprite image and sets it to the conversion context.
Setting options for creating vector data providers.