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

Add an option to delete unused categories in the categorized symbol renderer widget #60641

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

benwirf
Copy link
Contributor

@benwirf benwirf commented Feb 17, 2025

Description

This PR adds an option via a button in the categorized symbol renderer widget to delete unused categories from the model/view. I.e. any categories which are not matched in the attribute field or expression used to categorize the layer.

Example use case:

I have a layer of land systems covering the entire Northern Territory and a .qml style file which categorizes this layer into some 22 classes based on an attribute "CLASS".

Screenshot from 2025-02-16 20-25-53

Screenshot from 2025-02-16 20-24-51

Now I clip this NT-wide layer to a much smaller area. The subset only contains 7 of these classes, however I still want to load the style from the same .qml style file.

Screenshot from 2025-02-16 20-19-59

With the added option, one can quickly delete all the categories which are not used by the smaller subset layer:

remove-unused-categories

In the added void QgsCategorizedSymbolRendererWidget::deleteUnusedCategories() method, I reused a block of code from the existing addCategories() method to build a QList<QVariant> of unique values in the layer based on the current classification attribute or expression. To avoid duplication & to try and keep things tidy, I moved this block to a new protected method: QList<QVariant> QgsCategorizedSymbolRendererWidget::layerUniqueValues( QString attrName ) which returns the list of unique values and is then called in both addCategories() and deleteUnusedCategories().

@github-actions github-actions bot added this to the 3.42.0 milestone Feb 17, 2025
@benwirf
Copy link
Contributor Author

benwirf commented Feb 17, 2025

I note that #60546 also modifies qgscategorizedsymbolrendererwidget.cpp. If the other PR is merged first, I am happy to resolve any conflicts and update this PR.

Copy link

github-actions bot commented Feb 17, 2025

🪟 Windows builds

Download Windows builds of this PR for testing.
Debug symbols for this build are available here.
(Built from commit bb09a74)

🪟 Windows Qt6 builds

Download Windows Qt6 builds of this PR for testing.
(Built from commit bb09a74)

@nyalldawson nyalldawson added Feature Frozen Feature freeze - Do not merge! labels Feb 17, 2025
…to delete

unused categories from the model/view. I.e. any categories which are not matched
in the attribute field or expression used to categorize the layer.
@benwirf benwirf force-pushed the delete_unused_renderer_categories branch from 245a43b to 3c94b36 Compare February 19, 2025 05:07
@benwirf
Copy link
Contributor Author

benwirf commented Feb 19, 2025

@troopa81, thanks for the review- I have rebased & updated with your suggestions. I don't understand the changes to the .ui file though. All I do is add a push button to the horizontal layout below the tree view widget. Also not sure why the layout/documentation checks are failing. Any ideas?

@benwirf
Copy link
Contributor Author

benwirf commented Feb 19, 2025

OK, I think I've sorted out the issues with the .ui file. Files changed is only showing the inserted tags for the new push button. I also made sure the pre-commit hooks were installed as per: https://github.com/qgis/QGIS/blob/master/CONTRIBUTING.md before my last commit, but Code Layout/ documentation checks is still failing. I'm not sure why.

@troopa81
Copy link
Contributor

but Code Layout/ documentation checks is still failing. I'm not sure why.

You have undocumented method. I think it's deleteUnusedCategories() even if it's not exposed to python API. You can reproduce it on your side with the following command in the build directory

ctest -V -R PyQgsDocCoverage

*
* Called by addCategories() and deleteUnusedCategories()
*/
QList<QVariant> layerUniqueValues( const QString attrName );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
QList<QVariant> layerUniqueValues( const QString attrName );
QList<QVariant> layerUniqueValues( const QString& attrName );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Frozen Feature freeze - Do not merge!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants