Skip to content

Commit

Permalink
Merge pull request #966 from opengisch/categoriesexporter
Browse files Browse the repository at this point in the history
Topping Exporter: Consider the layer type for style categories
  • Loading branch information
signedav authored Oct 10, 2024
2 parents 8ca8117 + acbd713 commit a8f2cf1
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 114 deletions.
199 changes: 107 additions & 92 deletions QgisModelBaker/gui/topping_wizard/layer_style_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,117 +21,132 @@
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtWidgets import QDialog

if Qgis.QGIS_VERSION_INT >= 34000:
from qgis.gui import (
QgsCategoryDisplayLabelDelegate,
QgsMapLayerStyleCategoriesModel,
)

from QgisModelBaker.utils import gui_utils

DIALOG_UI = gui_utils.get_ui_class("topping_wizard/layer_style_categories.ui")

if Qgis.QGIS_VERSION_INT < 34000:

class LayerStyleCategoriesModel(gui_utils.CheckEntriesModel):

CATEGORY_MAP = (
{
"LayerConfiguration": QgsMapLayer.StyleCategory.LayerConfiguration,
"Symbology": QgsMapLayer.StyleCategory.Symbology,
"3D Symbology": QgsMapLayer.StyleCategory.Symbology3D,
"Labeling": QgsMapLayer.StyleCategory.Labeling,
"Fields": QgsMapLayer.StyleCategory.Fields,
"Forms": QgsMapLayer.StyleCategory.Forms,
"Actions": QgsMapLayer.StyleCategory.Actions,
"Map Tips": QgsMapLayer.StyleCategory.MapTips,
"Diagrams": QgsMapLayer.StyleCategory.Diagrams,
"Attribute Table Settings": QgsMapLayer.StyleCategory.AttributeTable,
"Rendering": QgsMapLayer.StyleCategory.Rendering,
"Custom Properties": QgsMapLayer.StyleCategory.CustomProperties,
"Geometry Options": QgsMapLayer.StyleCategory.GeometryOptions,
"Relations": QgsMapLayer.StyleCategory.Relations,
"Temporal": QgsMapLayer.StyleCategory.Temporal,
"Legend": QgsMapLayer.StyleCategory.Legend,
"Elevation": QgsMapLayer.StyleCategory.Elevation,
"Notes": QgsMapLayer.StyleCategory.Notes,
}
if Qgis.QGIS_VERSION_INT > 31800
else {
"LayerConfiguration": QgsMapLayer.StyleCategory.LayerConfiguration,
"Symbology": QgsMapLayer.StyleCategory.Symbology,
"3D Symbology": QgsMapLayer.StyleCategory.Symbology3D,
"Labeling": QgsMapLayer.StyleCategory.Labeling,
"Fields": QgsMapLayer.StyleCategory.Fields,
"Forms": QgsMapLayer.StyleCategory.Forms,
"Actions": QgsMapLayer.StyleCategory.Actions,
"Map Tips": QgsMapLayer.StyleCategory.MapTips,
"Diagrams": QgsMapLayer.StyleCategory.Diagrams,
"Attribute Table Settings": QgsMapLayer.StyleCategory.AttributeTable,
"Rendering": QgsMapLayer.StyleCategory.Rendering,
"Custom Properties": QgsMapLayer.StyleCategory.CustomProperties,
"Geometry Options": QgsMapLayer.StyleCategory.GeometryOptions,
"Relations": QgsMapLayer.StyleCategory.Relations,
"Temporal": QgsMapLayer.StyleCategory.Temporal,
"Legend": QgsMapLayer.StyleCategory.Legend,
}
)

class LayerStyleCategoriesModel(gui_utils.CheckEntriesModel):

CATEGORY_MAP = (
{
"LayerConfiguration": QgsMapLayer.StyleCategory.LayerConfiguration,
"Symbology": QgsMapLayer.StyleCategory.Symbology,
"3D Symbology": QgsMapLayer.StyleCategory.Symbology3D,
"Labeling": QgsMapLayer.StyleCategory.Labeling,
"Fields": QgsMapLayer.StyleCategory.Fields,
"Forms": QgsMapLayer.StyleCategory.Forms,
"Actions": QgsMapLayer.StyleCategory.Actions,
"Map Tips": QgsMapLayer.StyleCategory.MapTips,
"Diagrams": QgsMapLayer.StyleCategory.Diagrams,
"Attribute Table Settings": QgsMapLayer.StyleCategory.AttributeTable,
"Rendering": QgsMapLayer.StyleCategory.Rendering,
"Custom Properties": QgsMapLayer.StyleCategory.CustomProperties,
"Geometry Options": QgsMapLayer.StyleCategory.GeometryOptions,
"Relations": QgsMapLayer.StyleCategory.Relations,
"Temporal": QgsMapLayer.StyleCategory.Temporal,
"Legend": QgsMapLayer.StyleCategory.Legend,
"Elevation": QgsMapLayer.StyleCategory.Elevation,
"Notes": QgsMapLayer.StyleCategory.Notes,
}
if Qgis.QGIS_VERSION_INT > 31800
else {
"LayerConfiguration": QgsMapLayer.StyleCategory.LayerConfiguration,
"Symbology": QgsMapLayer.StyleCategory.Symbology,
"3D Symbology": QgsMapLayer.StyleCategory.Symbology3D,
"Labeling": QgsMapLayer.StyleCategory.Labeling,
"Fields": QgsMapLayer.StyleCategory.Fields,
"Forms": QgsMapLayer.StyleCategory.Forms,
"Actions": QgsMapLayer.StyleCategory.Actions,
"Map Tips": QgsMapLayer.StyleCategory.MapTips,
"Diagrams": QgsMapLayer.StyleCategory.Diagrams,
"Attribute Table Settings": QgsMapLayer.StyleCategory.AttributeTable,
"Rendering": QgsMapLayer.StyleCategory.Rendering,
"Custom Properties": QgsMapLayer.StyleCategory.CustomProperties,
"Geometry Options": QgsMapLayer.StyleCategory.GeometryOptions,
"Relations": QgsMapLayer.StyleCategory.Relations,
"Temporal": QgsMapLayer.StyleCategory.Temporal,
"Legend": QgsMapLayer.StyleCategory.Legend,
}
)

def __init__(self):
super().__init__()
self.setStringList(LayerStyleCategoriesModel.CATEGORY_MAP.keys())

def set_categories(self, categories: QgsMapLayer.StyleCategories):
self.beginResetModel()
self._checked_entries = {}
for category_name in LayerStyleCategoriesModel.CATEGORY_MAP.keys():
self._checked_entries[category_name] = (
Qt.Checked
if categories & LayerStyleCategoriesModel.CATEGORY_MAP[category_name]
else Qt.Unchecked
)
self.endResetModel()

def categories(self):
categories = 0
for name in self._checked_entries.keys():
if self._checked_entries[name] == Qt.Checked:
categories |= LayerStyleCategoriesModel.CATEGORY_MAP[name]
return categories
def __init__(self):
super().__init__()
self.setStringList(LayerStyleCategoriesModel.CATEGORY_MAP.keys())

def setCategories(self, categories: QgsMapLayer.StyleCategories):
self.beginResetModel()
self._checked_entries = {}
for category_name in LayerStyleCategoriesModel.CATEGORY_MAP.keys():
self._checked_entries[category_name] = (
Qt.Checked
if categories
& LayerStyleCategoriesModel.CATEGORY_MAP[category_name]
else Qt.Unchecked
)
self.endResetModel()

def categories(self):
categories = 0
for name in self._checked_entries.keys():
if self._checked_entries[name] == Qt.Checked:
categories |= LayerStyleCategoriesModel.CATEGORY_MAP[name]
return categories


class LayerStyleCategoriesDialog(QDialog, DIALOG_UI):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.setupUi(self)

self.model = LayerStyleCategoriesModel()
self.style_categories_list_view.setModel(self.model)
self.style_categories_list_view.clicked.connect(
self.style_categories_list_view.model().check
)
self.style_categories_list_view.space_pressed.connect(
self.style_categories_list_view.model().check
)
if Qgis.QGIS_VERSION_INT < 34000:
self.model = LayerStyleCategoriesModel()
self.style_categories_list_view.setModel(self.model)
self.style_categories_list_view.clicked.connect(
self.style_categories_list_view.model().check
)
else:
self.model = QgsMapLayerStyleCategoriesModel(QgsMapLayer.VectorLayer)
self.style_categories_list_view.setModel(self.model)
self.style_categories_list_view.setWordWrap(True)
self.style_categories_list_view.setItemDelegate(
QgsCategoryDisplayLabelDelegate(self)
)

self.select_all_checkbox.stateChanged.connect(self._select_all_items)
self.model.dataChanged.connect(lambda: self._set_select_all_checkbox())
self.selectall_button.clicked.connect(lambda: self.select_all_items(True))
self.deselectall_button.clicked.connect(lambda: self.select_all_items(False))

self.ok_button.clicked.connect(self.accept)

def _set_select_all_checkbox(self):
self.select_all_checkbox.setCheckState(self._evaluated_check_state(self.model))

def _evaluated_check_state(self, model):
nbr_of_checked = len(model.checked_entries())
if nbr_of_checked:
if nbr_of_checked == model.rowCount():
return Qt.Checked
return Qt.PartiallyChecked
return Qt.Unchecked
def set_layer_type(self, type):
if Qgis.QGIS_VERSION_INT < 34000:
return
# we need to reset the model with new type
self.model = QgsMapLayerStyleCategoriesModel(type)
self.style_categories_list_view.setModel(self.model)

def _select_all_items(self, state):
if state != Qt.PartiallyChecked and state != self._evaluated_check_state(
self.model
):
self.model.check_all(state)
def select_all_items(self, state):
if Qgis.QGIS_VERSION_INT < 34000:
self.model.check_all(Qt.Checked if state else Qt.Unchecked)
else:
for i in range(self.model.rowCount()):
self.model.setData(
self.model.index(i, 0),
Qt.Checked if state else Qt.Unchecked,
Qt.CheckStateRole,
)

def set_categories(self, categories: QgsMapLayer.StyleCategories):
self.style_categories_list_view.model().set_categories(categories)
self._set_select_all_checkbox()
self.style_categories_list_view.model().setCategories(
QgsMapLayer.StyleCategories(categories)
)

@property
def categories(self):
Expand Down
13 changes: 13 additions & 0 deletions QgisModelBaker/gui/topping_wizard/layers_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class LayerModel(QgsLayerTreeModel):

class Roles(IntEnum):
CATEGORIES = Qt.UserRole + 1
LAYERTYPE = Qt.UserRole + 2

class Columns(IntEnum):
NAME = 0
Expand Down Expand Up @@ -205,6 +206,15 @@ def data(self, index, role):
"categories", QgsMapLayer.StyleCategory.AllStyleCategories
)

if (
role == LayerModel.Roles.LAYERTYPE
and index.column() == LayerModel.Columns.USE_STYLE
):
node = self.index2node(index)
if not QgsLayerTree.isGroup(node):
layer = QgsProject.instance().mapLayersByName(node.name())[0]
return layer.type()

return QgsLayerTreeModel.data(self, index, role)

# this is unusual that it's not first data and then role (could be changed)
Expand Down Expand Up @@ -561,6 +571,9 @@ def open_categories_dialog(self, index):
self.categories_dialog.setWindowTitle(
self.tr(f"Layer Style Categories of {layername}")
)
self.categories_dialog.set_layer_type(
index.data(int(LayerModel.Roles.LAYERTYPE))
)
categories = index.data(int(LayerModel.Roles.CATEGORIES))
self.categories_dialog.set_categories(categories)
if self.categories_dialog.exec_():
Expand Down
51 changes: 29 additions & 22 deletions QgisModelBaker/ui/topping_wizard/layer_style_categories.ui
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
<string>Layer Style Categories</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" colspan="2">
<widget class="SpaceCheckListView" name="style_categories_list_view" native="true"/>
<item row="0" column="0" colspan="3">
<widget class="QListView" name="style_categories_list_view"/>
</item>
<item row="3" column="1">
<item row="2" column="2">
<widget class="QPushButton" name="ok_button">
<property name="text">
<string>Ok</string>
Expand All @@ -27,7 +27,21 @@
</property>
</widget>
</item>
<item row="3" column="0">
<item row="1" column="2">
<widget class="QPushButton" name="deselectall_button">
<property name="text">
<string>Deselect All</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="selectall_button">
<property name="text">
<string>Select All</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QDialogButtonBox" name="button_box">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -37,28 +51,21 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="SemiTristateCheckbox" name="select_all_checkbox">
<property name="text">
<string>Select all</string>
<item row="1" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>SpaceCheckListView</class>
<extends></extends>
<header>QgisModelBaker.utils.gui_utils</header>
<container>1</container>
</customwidget>
<customwidget>
<class>SemiTristateCheckbox</class>
<extends>QWidget</extends>
<header>QgisModelBaker.utils.gui_utils</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
Expand Down

0 comments on commit a8f2cf1

Please sign in to comment.