Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fix transparency widget syncToLayer causes corruption of widget values
Fixes a small bug in the QgsTransparencyWidget which changed a QgsRasterLayers' no-data-values just by the order of calling .syncToLayer or .apply, even withouht changing the no-data value explicitly
- Loading branch information
Showing
5 changed files
with
148 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# -*- coding: utf-8 -*- | ||
"""QGIS Unit tests for QgsRasterRange. | ||
.. 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__ = '07/06/2018' | ||
__copyright__ = 'Copyright 2018, The QGIS Project' | ||
|
||
import qgis # NOQA switch sip api | ||
|
||
import pathlib | ||
from qgis.gui import QgsRasterTransparencyWidget, QgsMapCanvas | ||
from qgis.core import QgsRasterLayer, QgsRasterRange | ||
|
||
from qgis.testing import TestCase, unittest | ||
from qgis.testing.mocked import get_iface | ||
|
||
from utilities import unitTestDataPath | ||
|
||
|
||
class TestQgsRasterTransparencyWidget(TestCase): | ||
@classmethod | ||
def setUpClass(cls) -> None: | ||
cls.iface = get_iface() | ||
|
||
@staticmethod | ||
def no_data_values(layer: QgsRasterLayer): | ||
return [n.min() for n in layer.dataProvider().userNoDataValues(1)] | ||
|
||
def test_transparency_widget(self): | ||
path = pathlib.Path(unitTestDataPath()) / 'landsat_4326.tif' | ||
self.assertTrue(path.is_file()) | ||
layer = QgsRasterLayer(path.as_posix()) | ||
self.assertTrue(layer.isValid()) | ||
canvas = QgsMapCanvas() | ||
canvas.setLayers([layer]) | ||
|
||
no_data_value = -99 | ||
nd_ref = [no_data_value] | ||
layer.dataProvider().setUserNoDataValue(1, [QgsRasterRange(no_data_value, no_data_value)]) | ||
nd0 = self.no_data_values(layer) | ||
self.assertListEqual(nd0, nd_ref) | ||
|
||
w = QgsRasterTransparencyWidget(layer, canvas) | ||
self.assertIsInstance(w, QgsRasterTransparencyWidget) | ||
nd1 = self.no_data_values(layer) | ||
self.assertListEqual(nd1, nd_ref, msg='Widget initialization should not change the "no data value"') | ||
|
||
w.syncToLayer() | ||
nd2 = self.no_data_values(layer) | ||
self.assertListEqual(nd2, nd_ref, msg='syncToLayer changed the "no data value"') | ||
|
||
w.syncToLayer() | ||
nd3 = self.no_data_values(layer) | ||
self.assertListEqual(nd3, nd_ref, msg='repeated syncToLayer changed the "no data value"') | ||
|
||
w.apply() | ||
nd4 = self.no_data_values(layer) | ||
self.assertListEqual(nd4, nd_ref, msg='apply changed the "no data value" but should not') | ||
|
||
w.apply() | ||
nd5 = self.no_data_values(layer) | ||
self.assertListEqual(nd5, nd_ref, msg='repeated apply changed the "no data value" but should not') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
69 changes: 69 additions & 0 deletions
69
tests/src/python/test_qgsrendererrasterpropertieswidget.py
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,69 @@ | ||
import pathlib | ||
from qgis.testing import start_app, unittest, TestCase | ||
from qgis.testing.mocked import get_iface | ||
from qgis.core import QgsRasterLayer, QgsProject, QgsMultiBandColorRenderer, QgsRasterRenderer, QgsSingleBandGrayRenderer | ||
from qgis.gui import QgsRendererRasterPropertiesWidget, QgsMapCanvas, QgsMultiBandColorRendererWidget, QgsRasterRendererWidget | ||
|
||
|
||
class QgsRendererRasterPropertiesTestCases(TestCase): | ||
|
||
def setUp(self): | ||
self.iface = get_iface() | ||
|
||
def multibandRasterLayer(self) -> QgsRasterLayer: | ||
|
||
try: | ||
from utilities import unitTestDataPath | ||
path = pathlib.Path(unitTestDataPath()) / 'landsat_4326.tif' | ||
except ModuleNotFoundError: | ||
path = pathlib.Path(__file__).parent / 'landsat_4326.tif' | ||
|
||
assert isinstance(path, pathlib.Path) and path.is_file() | ||
lyr = QgsRasterLayer(path.as_posix()) | ||
lyr.setName(path.name) | ||
self.assertIsInstance(lyr, QgsRasterLayer) | ||
self.assertTrue(lyr.isValid()) | ||
self.assertTrue(lyr.bandCount() > 1) | ||
|
||
return lyr | ||
|
||
def test_syncToLayer_SingleBandGray(self): | ||
|
||
lyr = self.multibandRasterLayer() | ||
lyr.setRenderer(QgsSingleBandGrayRenderer(lyr.dataProvider(), 1)) | ||
c = QgsMapCanvas() | ||
w = QgsRendererRasterPropertiesWidget(lyr, c) | ||
assert isinstance(w.currentRenderWidget().renderer(), QgsSingleBandGrayRenderer) | ||
assert w.currentRenderWidget().renderer().grayBand() == 1 | ||
lyr.renderer().setGrayBand(2) | ||
w.syncToLayer(lyr) | ||
assert w.currentRenderWidget().renderer().grayBand() == 2 | ||
|
||
def test_syncToLayer_MultiBand(self): | ||
|
||
lyr = self.multibandRasterLayer() | ||
assert isinstance(lyr.renderer(), QgsMultiBandColorRenderer) | ||
lyr.renderer().setRedBand(1) | ||
lyr.renderer().setGreenBand(2) | ||
lyr.renderer().setBlueBand(3) | ||
|
||
c = QgsMapCanvas() | ||
w = QgsRendererRasterPropertiesWidget(lyr, c) | ||
assert isinstance(w.currentRenderWidget().renderer(), QgsMultiBandColorRenderer) | ||
r = w.currentRenderWidget().renderer() | ||
assert isinstance(r, QgsMultiBandColorRenderer) | ||
assert r.usesBands() == [1, 2, 3] | ||
|
||
lyr.renderer().setRedBand(3) | ||
lyr.renderer().setGreenBand(1) | ||
lyr.renderer().setBlueBand(2) | ||
|
||
w.syncToLayer(lyr) | ||
|
||
r = w.currentRenderWidget().renderer() | ||
assert isinstance(r, QgsMultiBandColorRenderer) | ||
assert r.usesBands() == [3, 1, 2] | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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