Example #1
0
    def testLinkProjectColor(self):
        """
        Test linking to a project color
        """
        project_scheme = [s for s in QgsApplication.colorSchemeRegistry().schemes() if isinstance(s, QgsProjectColorScheme)][0]
        project_scheme.setColors([[QColor(255, 0, 0), 'col1'], [QColor(0, 255, 0), 'col2']])
        button = QgsColorButton()
        spy = QSignalSpy(button.unlinked)
        button.setColor(QColor(0, 0, 255))
        self.assertFalse(button.linkedProjectColorName())

        button.linkToProjectColor('col1')
        self.assertEqual(button.linkedProjectColorName(), 'col1')
        self.assertEqual(button.color().name(), '#ff0000')
        self.assertEqual(len(spy), 0)

        button.unlink()
        self.assertFalse(button.linkedProjectColorName())
        self.assertEqual(button.color().name(), '#0000ff')
        self.assertEqual(len(spy), 1)

        button.linkToProjectColor('col2')
        self.assertEqual(button.linkedProjectColorName(), 'col2')
        self.assertEqual(button.color().name(), '#00ff00')
        self.assertEqual(len(spy), 1)

        project_scheme.setColors([[QColor(255, 0, 0), 'xcol1'], [QColor(0, 255, 0), 'xcol2']])
        # linked color no longer exists
        self.assertFalse(button.linkedProjectColorName())
        self.assertEqual(button.color().name(), '#0000ff')
        self.assertEqual(len(spy), 2)
    def testLinkedColorButton(self):
        definition = QgsPropertyDefinition('test', 'test', QgsPropertyDefinition.ColorWithAlpha)
        button = QgsPropertyOverrideButton()
        button.init(0, QgsProperty(), definition)
        cb = QgsColorButton()
        button.registerLinkedWidget(cb)

        project_scheme = [s for s in QgsApplication.colorSchemeRegistry().schemes() if isinstance(s, QgsProjectColorScheme)][0]
        project_scheme.setColors([[QColor(255, 0, 0), 'col1'], [QColor(0, 255, 0), 'col2']])

        button.setToProperty(QgsProperty.fromValue('#ff0000'))
        self.assertTrue(cb.isEnabled())
        self.assertFalse(cb.linkedProjectColorName())
        button.setActive(False)
        self.assertTrue(cb.isEnabled())
        self.assertFalse(cb.linkedProjectColorName())
        button.setToProperty(QgsProperty.fromExpression('project_color(\'Cthulhu\'s delight\')'))
        self.assertTrue(cb.isEnabled())
        self.assertFalse(cb.linkedProjectColorName())
        button.setToProperty(QgsProperty.fromExpression('project_color(\'col1\')'))
        self.assertTrue(cb.isEnabled())
        self.assertEqual(cb.linkedProjectColorName(), 'col1')
        button.setActive(False)
        self.assertTrue(cb.isEnabled())
        self.assertFalse(cb.linkedProjectColorName())
        button.setActive(True)
        self.assertTrue(cb.isEnabled())
        self.assertEqual(cb.linkedProjectColorName(), 'col1')
    def testProjectColor(self):
        scheme = [s for s in QgsApplication.colorSchemeRegistry().schemes() if isinstance(s, QgsProjectColorScheme)][0]
        scheme.setColors([])

        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.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()])
 def testMembers(self):
     self.assertTrue(QgsApplication.actionScopeRegistry())
     # self.assertTrue(QgsApplication.annotationRegistry()) NOT AVAILABLE IN BINDINGS
     self.assertTrue(QgsApplication.colorSchemeRegistry())
     self.assertTrue(QgsApplication.fieldFormatterRegistry())
     self.assertTrue(QgsApplication.gpsConnectionRegistry())
     self.assertTrue(QgsApplication.messageLog())
     self.assertTrue(QgsApplication.paintEffectRegistry())
     self.assertTrue(QgsApplication.pluginLayerRegistry())
     self.assertTrue(QgsApplication.processingRegistry())
     self.assertTrue(QgsApplication.profiler())
     # self.assertTrue(QgsApplication.rasterRendererRegistry()) NOT AVAILABLE IN BINDINGS
     self.assertTrue(QgsApplication.rendererRegistry())
     self.assertTrue(QgsApplication.svgCache())
     self.assertTrue(QgsApplication.symbolLayerRegistry())
     self.assertTrue(QgsApplication.taskManager())
Example #5
0
    def testColorScheme(self):
        p = QgsProject.instance()
        spy = QSignalSpy(p.projectColorsChanged)
        p.setProjectColors([[QColor(255, 0, 0), 'red'], [QColor(0, 255, 0), 'green']])
        self.assertEqual(len(spy), 1)
        scheme = [s for s in QgsApplication.colorSchemeRegistry().schemes() if isinstance(s, QgsProjectColorScheme)][0]
        self.assertEqual([[c[0].name(), c[1]] for c in scheme.fetchColors()], [['#ff0000', 'red'], ['#00ff00', 'green']])
        # except color changed signal when clearing project
        p.clear()
        self.assertEqual(len(spy), 2)
        self.assertEqual([[c[0].name(), c[1]] for c in scheme.fetchColors()], [])

        # should be no signal on project destruction -- can cause a crash
        p = QgsProject()
        spy = QSignalSpy(p.projectColorsChanged)
        p.deleteLater()
        del p
        self.assertEqual(len(spy), 0)
 def testPopulateFromInstance(self):
     """Test adding schemes from global instance"""
     registry = QgsColorSchemeRegistry()
     self.assertEqual(len(registry.schemes()), 0)
     registry.populateFromInstance()
     self.assertEqual(len(registry.schemes()), len(QgsApplication.colorSchemeRegistry().schemes()))
 def testInstanceHasDefaultScheme(self):
     """Test global color scheme registry has default schemes"""
     registry = QgsApplication.colorSchemeRegistry()
     self.assertGreater(len(registry.schemes()), 0)
 def testCreateInstance(self):
     """Test creating global color scheme registry instance"""
     registry = QgsApplication.colorSchemeRegistry()
     self.assertTrue(registry)
Example #9
0
 def testInstanceHasDefaultScheme(self):
     """Test global color scheme registry has default schemes"""
     registry = QgsApplication.colorSchemeRegistry()
     self.assertGreater(len(registry.schemes()), 0)
Example #10
0
 def testCreateInstance(self):
     """Test creating global color scheme registry instance"""
     registry = QgsApplication.colorSchemeRegistry()
     self.assertTrue(registry)
    def testProjectColor(self):
        scheme = [
            s for s in QgsApplication.colorSchemeRegistry().schemes()
            if isinstance(s, QgsProjectColorScheme)
        ][0]
        scheme.setColors([])

        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.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()])
 def testPopulateFromInstance(self):
     """Test adding schemes from global instance"""
     registry = QgsColorSchemeRegistry()
     self.assertEqual(len(registry.schemes()), 0)
     registry.populateFromInstance()
     self.assertEqual(len(registry.schemes()), len(QgsApplication.colorSchemeRegistry().schemes()))
 def addColorScheme(self):
     #print('add simsColorScheme')
     QgsApplication.colorSchemeRegistry().addColorScheme(self.colorScheme)