-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Show Project Colors in color bound data defined buttons
This adds a new "Project Colors" section in data defined buttons which are linked to a color value. The color menu contains all colors defined as part of the current project's Project Color Scheme (which is defined through project properties). When a project color is selected from the button, the property becomes linked to that color. It will automatically follow any future changes to the color when made through project properties. This allows users to define common colors for a project once, and then "bind" symbol, label, layout, etc colors to these preset colors. The link is live, so you change it once, and the change is reflected EVERYWHERE. Sure beats updating a color 100 times when it's use has been scattered throughout a project's symbols, labels, etc... (Basically, this is just adding a shortcut to setting a data defined expression "project_color(...)" for the property. The project_color function has been around a LOOONG time, but it's only really been usable by power users before this change)
- Loading branch information
1 parent
79d01a9
commit eecfe50
Showing
5 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsPropertyOverrideButton. | ||
.. note:: This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
""" | ||
__author__ = 'Nyall Dawson' | ||
__date__ = '11/01/2019' | ||
__copyright__ = 'Copyright 2019, The QGIS Project' | ||
# This will get replaced with a git SHA1 when you do a git archive | ||
__revision__ = '$Format:%H$' | ||
|
||
import qgis # NOQA | ||
|
||
from qgis.core import (QgsPropertyDefinition, | ||
QgsProperty, | ||
QgsApplication, | ||
QgsProjectColorScheme) | ||
|
||
from qgis.gui import (QgsColorButton, | ||
QgsPropertyOverrideButton) | ||
|
||
from qgis.testing import start_app, unittest | ||
from qgis.PyQt.QtGui import QColor | ||
|
||
|
||
start_app() | ||
|
||
|
||
class TestQgsPropertyOverrideButton(unittest.TestCase): | ||
|
||
def testProjectColor(self): | ||
definition = QgsPropertyDefinition('test', 'test', QgsPropertyDefinition.ColorWithAlpha) | ||
button = QgsPropertyOverrideButton() | ||
button.init(0, QgsProperty(), definition) | ||
|
||
button.aboutToShowMenu() | ||
|
||
self.assertIn('Project Color', [a.text() for a in button.menu().actions()]) | ||
self.assertIn('Color', [a.text() for a in button.menu().actions()]) | ||
color_action = [a for a in button.menu().actions() if a.text() == 'Color'][0] | ||
self.assertEqual([a.text() for a in color_action.menu().actions()][0], 'No colors set') | ||
|
||
# add some project colors | ||
scheme = [s for s in QgsApplication.colorSchemeRegistry().schemes() if isinstance(s, QgsProjectColorScheme)][0] | ||
scheme.setColors([[QColor(255, 0, 0), 'color 1'], [QColor(255, 255, 0), 'burnt marigold']]) | ||
|
||
button.aboutToShowMenu() | ||
self.assertIn('Project Color', [a.text() for a in button.menu().actions()]) | ||
self.assertIn('Color', [a.text() for a in button.menu().actions()]) | ||
color_action = [a for a in button.menu().actions() if a.text() == 'Color'][0] | ||
self.assertEqual([a.text() for a in color_action.menu().actions()], ['color 1', 'burnt marigold']) | ||
|
||
button.menuActionTriggered(color_action.menu().actions()[1]) | ||
self.assertTrue(button.toProperty().isActive()) | ||
self.assertEqual(button.toProperty().asExpression(), 'project_color(\'burnt marigold\')') | ||
|
||
button.menuActionTriggered(color_action.menu().actions()[0]) | ||
self.assertTrue(button.toProperty().isActive()) | ||
self.assertEqual(button.toProperty().asExpression(), 'project_color(\'color 1\')') | ||
|
||
button.setToProperty(QgsProperty.fromExpression('project_color(\'burnt marigold\')')) | ||
button.aboutToShowMenu() | ||
color_action = [a for a in button.menu().actions() if a.text() == 'Color'][0] | ||
self.assertTrue(color_action.isChecked()) | ||
self.assertEqual([a.isChecked() for a in color_action.menu().actions()], [False, True]) | ||
|
||
# should also see color menu for ColorNoAlpha properties | ||
definition = QgsPropertyDefinition('test', 'test', QgsPropertyDefinition.ColorNoAlpha) | ||
button = QgsPropertyOverrideButton() | ||
button.init(0, QgsProperty(), definition) | ||
|
||
button.aboutToShowMenu() | ||
self.assertIn('Project Color', [a.text() for a in button.menu().actions()]) | ||
self.assertIn('Color', [a.text() for a in button.menu().actions()]) | ||
|
||
# but no color menu for other types | ||
definition = QgsPropertyDefinition('test', 'test', QgsPropertyDefinition.Double) | ||
button = QgsPropertyOverrideButton() | ||
button.init(0, QgsProperty(), definition) | ||
|
||
button.aboutToShowMenu() | ||
self.assertNotIn('Project Color', [a.text() for a in button.menu().actions()]) | ||
self.assertNotIn('Color', [a.text() for a in button.menu().actions()]) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |