Skip to content

Commit 3724f9e

Browse files
authoredSep 14, 2018
use a model for select map layer style categories (#7907)
this avoids cluttering QgsMapLayer and reduces a bit code redundancy
1 parent efa7277 commit 3724f9e

13 files changed

+357
-196
lines changed
 

‎python/core/auto_generated/qgsmaplayer.sip.in

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,6 @@ This is the base class for all map layer types (vector, raster).
9494
typedef QFlags<QgsMapLayer::StyleCategory> StyleCategories;
9595

9696

97-
struct ReadableStyleCategory
98-
{
99-
public:
100-
ReadableStyleCategory( const QString &name, const QString &toolTip = QString() );
101-
%Docstring
102-
Create a ReadableStyleCategory
103-
%End
104-
ReadableStyleCategory( const QString &name, const QIcon &icon, const QString &toolTip = QString() );
105-
%Docstring
106-
Create a ReadableStyleCategory
107-
%End
108-
QString name() const;
109-
%Docstring
110-
Return the translated name of the category
111-
%End
112-
QString toolTip() const;
113-
%Docstring
114-
Return the translated tooltip of the category
115-
%End
116-
QIcon icon() const;
117-
%Docstring
118-
Return the icon of the category
119-
%End
120-
};
121-
12297
QgsMapLayer( QgsMapLayer::LayerType type = VectorLayer, const QString &name = QString(), const QString &source = QString() );
12398
%Docstring
12499
Constructor for QgsMapLayer
@@ -181,15 +156,6 @@ Returns the extension of a Property.
181156
.. versionadded:: 3.0
182157
%End
183158

184-
static ReadableStyleCategory readableStyleCategory( StyleCategory category );
185-
%Docstring
186-
Readable and Translated category
187-
188-
.. versionadded:: 3.4
189-
%End
190-
191-
192-
193159
QString id() const;
194160
%Docstring
195161
Returns the layer's unique ID, which is used to access this layer from :py:class:`QgsProject`.

‎src/app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ SET(QGIS_APP_SRCS
6161
qgslayertreeviewmemoryindicator.cpp
6262
qgslayertreeviewnonremovableindicator.cpp
6363
qgsmapcanvasdockwidget.cpp
64+
qgsmaplayerstylecategoriesmodel.cpp
6465
qgsmaplayerstyleguiutils.cpp
6566
qgsmapsavedialog.cpp
6667
qgspuzzlewidget.cpp
@@ -285,6 +286,7 @@ SET (QGIS_APP_MOC_HDRS
285286
qgslayertreeviewfilterindicator.h
286287
qgslayertreeviewnonremovableindicator.h
287288
qgsmapcanvasdockwidget.h
289+
qgsmaplayerstylecategoriesmodel.h
288290
qgsmaplayerstyleguiutils.h
289291
qgsmapsavedialog.h
290292
qgspuzzlewidget.h

‎src/app/qgsapplayertreeviewmenuprovider.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* (at your option) any later version. *
1313
* *
1414
***************************************************************************/
15+
1516
#include "qgsapplayertreeviewmenuprovider.h"
1617

1718

@@ -37,6 +38,8 @@
3738
#include "qgslayertreeregistrybridge.h"
3839
#include "qgssymbolselectordialog.h"
3940
#include "qgssinglesymbolrenderer.h"
41+
#include "qgsmaplayerstylecategoriesmodel.h"
42+
4043

4144
QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView *view, QgsMapCanvas *canvas )
4245
: mView( view )
@@ -320,13 +323,17 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
320323
{
321324
QMenu *copyStyleMenu = menuStyleManager->addMenu( tr( "Copy Style" ) );
322325
copyStyleMenu->setToolTipsVisible( true );
323-
QList<QgsMapLayer::StyleCategory> categories = qgsEnumMap<QgsMapLayer::StyleCategory>().keys();
324-
categories.move( categories.indexOf( QgsMapLayer::AllStyleCategories ), 0 ); // move All categories to top
325-
for ( QgsMapLayer::StyleCategory category : categories )
326+
QgsMapLayerStyleCategoriesModel *model = new QgsMapLayerStyleCategoriesModel( copyStyleMenu );
327+
model->setShowAllCategories( true );
328+
for ( int row = 0; row < model->rowCount(); ++row )
326329
{
327-
QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );
328-
QAction *copyAction = new QAction( readableCategory.icon(), readableCategory.name(), copyStyleMenu );
329-
copyAction->setToolTip( readableCategory.toolTip() );
330+
QModelIndex index = model->index( row, 0 );
331+
QString name = model->data( index, Qt::DisplayRole ).toString();
332+
QString tooltip = model->data( index, Qt::ToolTipRole ).toString();
333+
QIcon icon = model->data( index, Qt::DecorationRole ).value<QIcon>();
334+
QgsMapLayer::StyleCategory category = model->index2category( index );
335+
QAction *copyAction = new QAction( icon, name, copyStyleMenu );
336+
copyAction->setToolTip( tooltip );
330337
connect( copyAction, &QAction::triggered, this, [ = ]() {app->copyStyle( layer, category );} );
331338
copyStyleMenu->addAction( copyAction );
332339
if ( category == QgsMapLayer::AllStyleCategories )
@@ -342,19 +349,24 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
342349
{
343350
if ( layer->type() == QgsMapLayer::VectorLayer )
344351
{
345-
QMenu *copyStyleMenu = menuStyleManager->addMenu( tr( "Paste Style" ) );
346-
copyStyleMenu->setToolTipsVisible( true );
347-
QList<QgsMapLayer::StyleCategory> categories = qgsEnumMap<QgsMapLayer::StyleCategory>().keys();
348-
categories.move( categories.indexOf( QgsMapLayer::AllStyleCategories ), 0 ); // move All categories to top
349-
for ( QgsMapLayer::StyleCategory category : categories )
352+
QMenu *pasteStyleMenu = menuStyleManager->addMenu( tr( "Paste Style" ) );
353+
pasteStyleMenu->setToolTipsVisible( true );
354+
355+
QgsMapLayerStyleCategoriesModel *model = new QgsMapLayerStyleCategoriesModel( pasteStyleMenu );
356+
model->setShowAllCategories( true );
357+
for ( int row = 0; row < model->rowCount(); ++row )
350358
{
351-
QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );
352-
QAction *copyAction = new QAction( readableCategory.icon(), readableCategory.name() );
353-
copyAction->setToolTip( readableCategory.toolTip() );
354-
connect( copyAction, &QAction::triggered, this, [ = ]() {app->pasteStyle( layer, category );} );
355-
copyStyleMenu->addAction( copyAction );
359+
QModelIndex index = model->index( row, 0 );
360+
QString name = model->data( index, Qt::DisplayRole ).toString();
361+
QString tooltip = model->data( index, Qt::ToolTipRole ).toString();
362+
QIcon icon = model->data( index, Qt::DecorationRole ).value<QIcon>();
363+
QgsMapLayer::StyleCategory category = model->index2category( index );
364+
QAction *copyAction = new QAction( icon, name, pasteStyleMenu );
365+
copyAction->setToolTip( tooltip );
366+
connect( copyAction, &QAction::triggered, this, [ = ]() {app->copyStyle( layer, category );} );
367+
pasteStyleMenu->addAction( copyAction );
356368
if ( category == QgsMapLayer::AllStyleCategories )
357-
copyStyleMenu->addSeparator();
369+
pasteStyleMenu->addSeparator();
358370
}
359371
}
360372
else
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/***************************************************************************
2+
qgsmaplayerstylecategoriesmodel.cpp
3+
--------------------------------------
4+
Date : September 2018
5+
Copyright : (C) 2018 by Denis Rouzaud
6+
Email : denis@opengis.ch
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+
16+
#include "qgsmaplayerstylecategoriesmodel.h"
17+
18+
QgsMapLayerStyleCategoriesModel::QgsMapLayerStyleCategoriesModel( QObject *parent )
19+
: QAbstractListModel( parent )
20+
{
21+
mCategoryList = qgsEnumMap<QgsMapLayer::StyleCategory>().keys();
22+
// move All categories to top
23+
mCategoryList.move( mCategoryList.indexOf( QgsMapLayer::AllStyleCategories ), 0 );
24+
}
25+
26+
void QgsMapLayerStyleCategoriesModel::setCategories( QgsMapLayer::StyleCategories categories )
27+
{
28+
if ( mCategories == categories )
29+
return;
30+
31+
beginResetModel();
32+
mCategories = categories;
33+
endResetModel();
34+
}
35+
36+
QgsMapLayer::StyleCategories QgsMapLayerStyleCategoriesModel::categories() const
37+
{
38+
return mCategories;
39+
}
40+
41+
void QgsMapLayerStyleCategoriesModel::setShowAllCategories( bool showAll )
42+
{
43+
beginResetModel();
44+
mShowAllCategories = showAll;
45+
endResetModel();
46+
}
47+
48+
QgsMapLayer::StyleCategory QgsMapLayerStyleCategoriesModel::index2category( const QModelIndex &index ) const
49+
{
50+
return mCategoryList.at( index.row() - ( mShowAllCategories ? 0 : 1 ) );
51+
}
52+
53+
int QgsMapLayerStyleCategoriesModel::rowCount( const QModelIndex & ) const
54+
{
55+
int count = mCategoryList.count();
56+
if ( !mShowAllCategories )
57+
count--;
58+
return count;
59+
}
60+
61+
int QgsMapLayerStyleCategoriesModel::columnCount( const QModelIndex & ) const
62+
{
63+
return 1;
64+
}
65+
66+
QVariant QgsMapLayerStyleCategoriesModel::data( const QModelIndex &index, int role ) const
67+
{
68+
if ( !index.isValid() || index.row() >= rowCount() )
69+
return QVariant();
70+
71+
QgsMapLayer::StyleCategory category = index2category( index );
72+
73+
switch ( category )
74+
{
75+
case QgsMapLayer::LayerConfiguration:
76+
switch ( role )
77+
{
78+
case Qt::DisplayRole:
79+
return tr( "Layer Configuration" );
80+
case Qt::ToolTipRole:
81+
return tr( "Identifiable, removable, searchable, display expression, read-only" );
82+
case Qt::DecorationRole:
83+
return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/system.svg" ) );
84+
}
85+
case QgsMapLayer::Symbology :
86+
switch ( role )
87+
{
88+
case Qt::DisplayRole:
89+
return tr( "Symbology" );
90+
case Qt::ToolTipRole:
91+
return QVariant();
92+
case Qt::DecorationRole:
93+
return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/symbology.svg" ) );
94+
}
95+
case QgsMapLayer::Symbology3D:
96+
switch ( role )
97+
{
98+
case Qt::DisplayRole:
99+
return tr( "3D Symbology" );
100+
case Qt::ToolTipRole:
101+
return QVariant();
102+
case Qt::DecorationRole:
103+
return QgsApplication::getThemeIcon( QStringLiteral( "/3d.svg" ) );
104+
}
105+
case QgsMapLayer::Labeling :
106+
switch ( role )
107+
{
108+
case Qt::DisplayRole:
109+
return tr( "Labels" );
110+
case Qt::ToolTipRole:
111+
return QVariant();
112+
case Qt::DecorationRole:
113+
return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/labels.svg" ) );
114+
}
115+
case QgsMapLayer::Fields :
116+
switch ( role )
117+
{
118+
case Qt::DisplayRole:
119+
return tr( "Fields" );
120+
case Qt::ToolTipRole:
121+
return tr( "Aliases, widgets, WMS/WFS, expressions, constraints, virtual fields" );
122+
case Qt::DecorationRole:
123+
return QgsApplication::getThemeIcon( QStringLiteral( "/mSourceFields" ) );
124+
}
125+
case QgsMapLayer::Forms :
126+
switch ( role )
127+
{
128+
case Qt::DisplayRole:
129+
return tr( "Forms" );
130+
case Qt::ToolTipRole:
131+
return QVariant();
132+
case Qt::DecorationRole:
133+
return QgsApplication::getThemeIcon( QStringLiteral( "/mActionFormView" ) );
134+
}
135+
case QgsMapLayer::Actions :
136+
switch ( role )
137+
{
138+
case Qt::DisplayRole:
139+
return tr( "Actions" );
140+
case Qt::ToolTipRole:
141+
return QVariant();
142+
case Qt::DecorationRole:
143+
return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/action.svg" ) );
144+
}
145+
case QgsMapLayer::MapTips :
146+
switch ( role )
147+
{
148+
case Qt::DisplayRole:
149+
return tr( "Map Tips" );
150+
case Qt::ToolTipRole:
151+
return QVariant();
152+
case Qt::DecorationRole:
153+
return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/display.svg" ) );
154+
}
155+
case QgsMapLayer::Diagrams :
156+
switch ( role )
157+
{
158+
case Qt::DisplayRole:
159+
return tr( "Diagrams" );
160+
case Qt::ToolTipRole:
161+
return QVariant();
162+
case Qt::DecorationRole:
163+
return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/diagram.svg" ) );
164+
}
165+
case QgsMapLayer::AttributeTable :
166+
switch ( role )
167+
{
168+
case Qt::DisplayRole:
169+
return tr( "Attribute Table Settings" );
170+
case Qt::ToolTipRole:
171+
return tr( "Choice and order of columns, conditional styling" );
172+
case Qt::DecorationRole:
173+
return QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable" ) );
174+
}
175+
case QgsMapLayer::Rendering :
176+
switch ( role )
177+
{
178+
case Qt::DisplayRole:
179+
return tr( "Rendering" );
180+
case Qt::ToolTipRole:
181+
return tr( "Scale visibility, simplify method, opacity" );
182+
case Qt::DecorationRole:
183+
return QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/rendering.svg" ) );
184+
}
185+
case QgsMapLayer::CustomProperties :
186+
switch ( role )
187+
{
188+
case Qt::DisplayRole:
189+
return tr( "Custom Properties" );
190+
case Qt::ToolTipRole:
191+
return QVariant();
192+
case Qt::DecorationRole:
193+
return QgsApplication::getThemeIcon( QStringLiteral( "/mActionOptions.svg" ) );
194+
}
195+
case QgsMapLayer::AllStyleCategories :
196+
switch ( role )
197+
{
198+
case Qt::DisplayRole:
199+
return tr( "All Style Categories" );
200+
case Qt::ToolTipRole:
201+
return QVariant();
202+
case Qt::DecorationRole:
203+
return QVariant();
204+
}
205+
}
206+
return QVariant();
207+
}
208+
209+
bool QgsMapLayerStyleCategoriesModel::setData( const QModelIndex &index, const QVariant &value, int role )
210+
{
211+
if ( !index.isValid() || index.row() >= rowCount() )
212+
return false;
213+
214+
if ( role == Qt::CheckStateRole )
215+
{
216+
QgsMapLayer::StyleCategory category = index2category( index );
217+
if ( value.value<Qt::CheckState>() == Qt::Checked )
218+
{
219+
mCategories |= category;
220+
emit dataChanged( index, index );
221+
return true;
222+
}
223+
else if ( value.value<Qt::CheckState>() == Qt::Unchecked )
224+
{
225+
mCategories &= category;
226+
emit dataChanged( index, index );
227+
return true;
228+
}
229+
}
230+
return false;
231+
}
232+
233+
234+
Qt::ItemFlags QgsMapLayerStyleCategoriesModel::flags( const QModelIndex & ) const
235+
{
236+
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
237+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/***************************************************************************
2+
qgsmaplayerstylecategoriesmodel.h
3+
--------------------------------------
4+
Date : September 2018
5+
Copyright : (C) 2018 by Denis Rouzaud
6+
Email : denis@opengis.ch
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+
16+
#ifndef QGSMAPLAYERSTYLECATEGORIESMODEL_H
17+
#define QGSMAPLAYERSTYLECATEGORIESMODEL_H
18+
19+
#include <QAbstractListModel>
20+
21+
#include "qgsmaplayer.h"
22+
23+
class QgsMapLayerStyleCategoriesModel : public QAbstractListModel
24+
{
25+
public:
26+
explicit QgsMapLayerStyleCategoriesModel( QObject *parent = nullptr );
27+
28+
//! reset the model data
29+
void setCategories( QgsMapLayer::StyleCategories categories );
30+
31+
//! return the categories as defined in the model
32+
QgsMapLayer::StyleCategories categories() const;
33+
34+
//! defines if the model should list the AllStyleCategories entry
35+
void setShowAllCategories( bool showAll );
36+
37+
//! return the category for the given index
38+
QgsMapLayer::StyleCategory index2category( const QModelIndex &index ) const;
39+
40+
int rowCount( const QModelIndex & = QModelIndex() ) const override;
41+
int columnCount( const QModelIndex & = QModelIndex() ) const override;
42+
QVariant data( const QModelIndex &index, int role ) const override;
43+
bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
44+
Qt::ItemFlags flags( const QModelIndex & ) const override;
45+
46+
private:
47+
//! current data as flags
48+
QgsMapLayer::StyleCategories mCategories;
49+
//! map of existing categories
50+
QList<QgsMapLayer::StyleCategory> mCategoryList;
51+
//! display All categories on first line
52+
bool mShowAllCategories = false;
53+
};
54+
55+
#endif // QGSMAPLAYERSTYLECATEGORIESMODEL_H

‎src/app/qgsvectorlayerloadstyledialog.cpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgisapp.h"
2222
#include "qgssettings.h"
2323
#include "qgsvectorlayerproperties.h"
24+
#include "qgsmaplayerstylecategoriesmodel.h"
2425

2526

2627

@@ -50,35 +51,26 @@ QgsVectorLayerLoadStyleDialog::QgsVectorLayerLoadStyleDialog( QgsVectorLayer *la
5051
mFromFileWidget->setVisible( type != QgsVectorLayerProperties::DB );
5152
mFromDbWidget->setVisible( type == QgsVectorLayerProperties::DB );
5253
mDeleteButton->setVisible( type == QgsVectorLayerProperties::DB && mLayer->dataProvider()->isDeleteStyleFromDatabaseSupported() );
53-
mStyleCategoriesListWidget->setEnabled( currentStyleType() != QgsVectorLayerProperties::SLD );
54+
mStyleCategoriesListView->setEnabled( currentStyleType() != QgsVectorLayerProperties::SLD );
5455
updateLoadButtonState();
5556
} );
5657
mStyleTypeComboBox->addItem( tr( "from file" ), QgsVectorLayerProperties::QML ); // QML is used as entry, but works for SLD too, see currentStyleType()
5758
if ( mLayer->dataProvider()->isSaveAndLoadStyleToDatabaseSupported() )
5859
mStyleTypeComboBox->addItem( tr( "from database (%1)" ).arg( providerName ), QgsVectorLayerProperties::DB );
5960

6061
// fill style categories
62+
mModel = new QgsMapLayerStyleCategoriesModel( this );
6163
QgsMapLayer::StyleCategories lastStyleCategories = settings.flagValue( QStringLiteral( "style/lastStyleCategories" ), QgsMapLayer::AllStyleCategories );
62-
for ( QgsMapLayer::StyleCategory category : qgsEnumMap<QgsMapLayer::StyleCategory>().keys() )
63-
{
64-
if ( category == QgsMapLayer::AllStyleCategories )
65-
continue;
66-
67-
QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );
68-
69-
QListWidgetItem *item = new QListWidgetItem( readableCategory.icon(), readableCategory.name(), mStyleCategoriesListWidget );
70-
item->setFlags( ( item->flags() | Qt::ItemIsUserCheckable ) & ~Qt::ItemIsSelectable );
71-
item->setCheckState( lastStyleCategories.testFlag( category ) ? Qt::Checked : Qt::Unchecked );
72-
item->setData( Qt::UserRole, category );
73-
}
64+
mModel->setCategories( lastStyleCategories );
65+
mStyleCategoriesListView->setModel( mModel );
7466

7567
// load from file setup
7668
mFileWidget->setFilter( tr( "QGIS Layer Style File, SLD File" ) + QStringLiteral( " (*.qml *.sld)" ) );
7769
mFileWidget->setStorageMode( QgsFileWidget::GetFile );
7870
mFileWidget->setDefaultRoot( myLastUsedDir );
7971
connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & )
8072
{
81-
mStyleCategoriesListWidget->setEnabled( currentStyleType() != QgsVectorLayerProperties::SLD );
73+
mStyleCategoriesListView->setEnabled( currentStyleType() != QgsVectorLayerProperties::SLD );
8274
updateLoadButtonState();
8375
} );
8476

@@ -103,7 +95,7 @@ QgsVectorLayerLoadStyleDialog::QgsVectorLayerLoadStyleDialog( QgsVectorLayer *la
10395
setTabOrder( mRelatedTable, mOthersTable );
10496

10597
restoreGeometry( settings.value( QStringLiteral( "Windows/vectorLayerLoadStyle/geometry" ) ).toByteArray() );
106-
mStyleCategoriesListWidget->adjustSize();
98+
mStyleCategoriesListView->adjustSize();
10799
}
108100

109101
QgsVectorLayerLoadStyleDialog::~QgsVectorLayerLoadStyleDialog()
@@ -114,14 +106,7 @@ QgsVectorLayerLoadStyleDialog::~QgsVectorLayerLoadStyleDialog()
114106

115107
QgsMapLayer::StyleCategories QgsVectorLayerLoadStyleDialog::styleCategories() const
116108
{
117-
QgsMapLayer::StyleCategories categories;
118-
for ( int row = 0; row < mStyleCategoriesListWidget->count(); ++row )
119-
{
120-
QListWidgetItem *item = mStyleCategoriesListWidget->item( row );
121-
if ( item->checkState() == Qt::Checked )
122-
categories |= item->data( Qt::UserRole ).value<QgsMapLayer::StyleCategory>();
123-
}
124-
return categories;
109+
return mModel->categories();
125110
}
126111

127112
QgsVectorLayerProperties::StyleType QgsVectorLayerLoadStyleDialog::currentStyleType() const

‎src/app/qgsvectorlayerloadstyledialog.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "qgsvectorlayerproperties.h"
2323
#include "qgsmaplayer.h"
2424

25+
class QgsMapLayerStyleCategoriesModel;
26+
2527
class APP_EXPORT QgsVectorLayerLoadStyleDialog : public QDialog, private Ui::QgsVectorLayerLoadStyleDialog
2628
{
2729
Q_OBJECT
@@ -51,6 +53,7 @@ class APP_EXPORT QgsVectorLayerLoadStyleDialog : public QDialog, private Ui::Qgs
5153

5254
private:
5355
QgsVectorLayer *mLayer = nullptr;
56+
QgsMapLayerStyleCategoriesModel *mModel;
5457
QString mSelectedStyleId;
5558
QString mSelectedStyleName;
5659
int mSectionLimit = 0;

‎src/app/qgsvectorlayersavestyledialog.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgsvectorlayer.h"
2121
#include "qgssettings.h"
2222
#include "qgshelp.h"
23+
#include "qgsmaplayerstylecategoriesmodel.h"
2324

2425
QgsVectorLayerSaveStyleDialog::QgsVectorLayerSaveStyleDialog( QgsVectorLayer *layer, QWidget *parent )
2526
: QDialog( parent )
@@ -44,7 +45,7 @@ QgsVectorLayerSaveStyleDialog::QgsVectorLayerSaveStyleDialog( QgsVectorLayer *la
4445
QgsVectorLayerProperties::StyleType type = currentStyleType();
4546
mSaveToFileWidget->setVisible( type != QgsVectorLayerProperties::DB );
4647
mSaveToDbWidget->setVisible( type == QgsVectorLayerProperties::DB );
47-
mStyleCategoriesListWidget->setEnabled( type == QgsVectorLayerProperties::QML );
48+
mStyleCategoriesListView->setEnabled( type == QgsVectorLayerProperties::QML );
4849
mFileWidget->setFilter( type == QgsVectorLayerProperties::QML ? tr( "QGIS Layer Style File (*.qml)" ) : tr( "SLD File (*.sld)" ) );
4950
updateSaveButtonState();
5051
} );
@@ -69,22 +70,13 @@ QgsVectorLayerSaveStyleDialog::QgsVectorLayerSaveStyleDialog( QgsVectorLayer *la
6970
mFileWidget->setDefaultRoot( myLastUsedDir );
7071

7172
// fill style categories
73+
mModel = new QgsMapLayerStyleCategoriesModel( this );
7274
QgsMapLayer::StyleCategories lastStyleCategories = settings.flagValue( QStringLiteral( "style/lastStyleCategories" ), QgsMapLayer::AllStyleCategories );
73-
for ( QgsMapLayer::StyleCategory category : qgsEnumMap<QgsMapLayer::StyleCategory>().keys() )
74-
{
75-
if ( category == QgsMapLayer::AllStyleCategories )
76-
continue;
77-
78-
QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );
79-
80-
QListWidgetItem *item = new QListWidgetItem( readableCategory.icon(), readableCategory.name(), mStyleCategoriesListWidget );
81-
item->setFlags( ( item->flags() | Qt::ItemIsUserCheckable ) & ~Qt::ItemIsSelectable );
82-
item->setCheckState( lastStyleCategories.testFlag( category ) ? Qt::Checked : Qt::Unchecked );
83-
item->setData( Qt::UserRole, category );
84-
}
75+
mModel->setCategories( lastStyleCategories );
76+
mStyleCategoriesListView->setModel( mModel );
8577

8678
restoreGeometry( settings.value( QStringLiteral( "Windows/vectorLayerSaveStyle/geometry" ) ).toByteArray() );
87-
mStyleCategoriesListWidget->adjustSize();
79+
mStyleCategoriesListView->adjustSize();
8880
}
8981

9082
void QgsVectorLayerSaveStyleDialog::accept()
@@ -123,14 +115,7 @@ QString QgsVectorLayerSaveStyleDialog::outputFilePath() const
123115

124116
QgsMapLayer::StyleCategories QgsVectorLayerSaveStyleDialog::styleCategories() const
125117
{
126-
QgsMapLayer::StyleCategories categories;
127-
for ( int row = 0; row < mStyleCategoriesListWidget->count(); ++row )
128-
{
129-
QListWidgetItem *item = mStyleCategoriesListWidget->item( row );
130-
if ( item->checkState() == Qt::Checked )
131-
categories |= item->data( Qt::UserRole ).value<QgsMapLayer::StyleCategory>();
132-
}
133-
return categories;
118+
return mModel->categories();
134119
}
135120

136121
QgsVectorLayerProperties::StyleType QgsVectorLayerSaveStyleDialog::currentStyleType() const

‎src/app/qgsvectorlayersavestyledialog.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "qgsvectorlayerproperties.h"
2222

2323
class QgsVectorLayer;
24+
class QgsMapLayerStyleCategoriesModel;
2425

2526

2627
class QgsVectorLayerSaveStyleDialog : public QDialog, private Ui::QgsVectorLayerSaveStyleDialog
@@ -56,7 +57,8 @@ class QgsVectorLayerSaveStyleDialog : public QDialog, private Ui::QgsVectorLayer
5657
void readUiFileContent( const QString &filePath );
5758

5859
private:
59-
QgsVectorLayer *mLayer = nullptr;;
60+
QgsVectorLayer *mLayer = nullptr;
61+
QgsMapLayerStyleCategoriesModel *mModel;
6062
QString mUiFileContent;
6163
};
6264

‎src/core/qgsmaplayer.cpp

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -67,56 +67,6 @@ QString QgsMapLayer::extensionPropertyType( QgsMapLayer::PropertyType type )
6767
return QString();
6868
}
6969

70-
QgsMapLayer::ReadableStyleCategory QgsMapLayer::readableStyleCategory( QgsMapLayer::StyleCategory category )
71-
{
72-
switch ( category )
73-
{
74-
case LayerConfiguration:
75-
return ReadableStyleCategory( tr( "Layer Configuration" ),
76-
QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/system.svg" ) ),
77-
tr( "Identifiable, removable, searchable, display expression, read-only" ) );
78-
case Symbology :
79-
return ReadableStyleCategory( tr( "Symbology" ),
80-
QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/symbology.svg" ) ) );
81-
case Symbology3D:
82-
return ReadableStyleCategory( tr( "3D Symbology" ),
83-
QgsApplication::getThemeIcon( QStringLiteral( "/3d.svg" ) ) );
84-
case Labeling :
85-
return ReadableStyleCategory( tr( "Labels" ),
86-
QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/labels.svg" ) ) );
87-
case Fields :
88-
return ReadableStyleCategory( tr( "Fields" ),
89-
QgsApplication::getThemeIcon( QStringLiteral( "/mSourceFields.svg" ) ),
90-
tr( "Aliases, widgets, WMS/WFS, expressions, constraints, virtual fields" ) );
91-
case Forms :
92-
return ReadableStyleCategory( tr( "Forms" ),
93-
QgsApplication::getThemeIcon( QStringLiteral( "/mActionFormView.svg" ) ) );
94-
case Actions :
95-
return ReadableStyleCategory( tr( "Actions" ),
96-
QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/action.svg" ) ) );
97-
case MapTips :
98-
return ReadableStyleCategory( tr( "Map Tips" ),
99-
QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/display.svg" ) ) );
100-
case Diagrams :
101-
return ReadableStyleCategory( tr( "Diagrams" ),
102-
QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/diagram.svg" ) ) );
103-
case AttributeTable :
104-
return ReadableStyleCategory( tr( "Attribute Table Settings" ),
105-
QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable.svg" ) ),
106-
tr( "Choice and order of columns, conditional styling" ) );
107-
case Rendering :
108-
return ReadableStyleCategory( tr( "Rendering" ),
109-
QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/rendering.svg" ) ),
110-
tr( "Scale visibility, simplify method, opacity" ) );
111-
case CustomProperties :
112-
return ReadableStyleCategory( tr( "Custom Properties" ),
113-
QgsApplication::getThemeIcon( QStringLiteral( "/mActionOptions.svg" ) ) );
114-
case AllStyleCategories :
115-
return ReadableStyleCategory( tr( "All Categories" ) );
116-
}
117-
return ReadableStyleCategory( tr( "All Categories" ) ); // no warnings
118-
}
119-
12070
QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
12171
const QString &lyrname,
12272
const QString &source )

‎src/core/qgsmaplayer.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -160,34 +160,6 @@ class CORE_EXPORT QgsMapLayer : public QObject
160160
Q_DECLARE_FLAGS( StyleCategories, StyleCategory )
161161
Q_FLAG( StyleCategories )
162162

163-
/**
164-
* Style category with its name, tooltip and icon.
165-
* Text are translated and readable
166-
* \since QGIS 3.4
167-
*/
168-
struct ReadableStyleCategory
169-
{
170-
public:
171-
//! Create a ReadableStyleCategory
172-
ReadableStyleCategory( const QString &name, const QString &toolTip = QString() )
173-
: mName( name ), mToolTip( toolTip )
174-
{}
175-
//! Create a ReadableStyleCategory
176-
ReadableStyleCategory( const QString &name, const QIcon &icon, const QString &toolTip = QString() )
177-
: mName( name ), mToolTip( toolTip ), mIcon( icon )
178-
{}
179-
//! Return the translated name of the category
180-
QString name() const {return mName;}
181-
//! Return the translated tooltip of the category
182-
QString toolTip() const {return mToolTip;}
183-
//! Return the icon of the category
184-
QIcon icon() const {return mIcon;}
185-
private:
186-
QString mName;
187-
QString mToolTip;
188-
QIcon mIcon;
189-
};
190-
191163
/**
192164
* Constructor for QgsMapLayer
193165
* \param type layer type
@@ -241,14 +213,6 @@ class CORE_EXPORT QgsMapLayer : public QObject
241213
*/
242214
static QString extensionPropertyType( PropertyType type );
243215

244-
/**
245-
* Readable and Translated category
246-
* \since QGIS 3.4
247-
*/
248-
static ReadableStyleCategory readableStyleCategory( StyleCategory category );
249-
250-
251-
252216
//! Returns the layer's unique ID, which is used to access this layer from QgsProject.
253217
QString id() const;
254218

‎src/ui/qgsvectorlayerloadstyledialog.ui

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>475</width>
10-
<height>390</height>
10+
<height>546</height>
1111
</rect>
1212
</property>
1313
<property name="sizePolicy">
@@ -79,16 +79,6 @@
7979
</property>
8080
</widget>
8181
</item>
82-
<item row="1" column="1">
83-
<widget class="QListWidget" name="mStyleCategoriesListWidget">
84-
<property name="sizePolicy">
85-
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
86-
<horstretch>0</horstretch>
87-
<verstretch>0</verstretch>
88-
</sizepolicy>
89-
</property>
90-
</widget>
91-
</item>
9282
<item row="5" column="0" colspan="2">
9383
<layout class="QGridLayout" name="gridLayout_4">
9484
<item row="0" column="0">
@@ -199,6 +189,16 @@
199189
</property>
200190
</spacer>
201191
</item>
192+
<item row="1" column="1">
193+
<widget class="QListView" name="mStyleCategoriesListView">
194+
<property name="sizePolicy">
195+
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
196+
<horstretch>0</horstretch>
197+
<verstretch>0</verstretch>
198+
</sizepolicy>
199+
</property>
200+
</widget>
201+
</item>
202202
</layout>
203203
</widget>
204204
<layoutdefault spacing="6" margin="11"/>

‎src/ui/qgsvectorlayersavestyledialog.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>491</width>
10-
<height>527</height>
10+
<height>535</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -137,7 +137,7 @@
137137
</widget>
138138
</item>
139139
<item row="1" column="1">
140-
<widget class="QListWidget" name="mStyleCategoriesListWidget">
140+
<widget class="QListView" name="mStyleCategoriesListView">
141141
<property name="sizePolicy">
142142
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
143143
<horstretch>0</horstretch>

0 commit comments

Comments
 (0)
Please sign in to comment.