|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + ReliefColorsWidget.py |
| 6 | + --------------------- |
| 7 | + Date : December 2016 |
| 8 | + Copyright : (C) 2016 by Alexander Bruy |
| 9 | + Email : alexander dot bruy at gmail dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Alexander Bruy' |
| 21 | +__date__ = 'December 2016' |
| 22 | +__copyright__ = '(C) 2016, Alexander Bruy' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +import os |
| 29 | +import codecs |
| 30 | + |
| 31 | +from qgis.PyQt import uic |
| 32 | +from qgis.PyQt.QtCore import pyqtSlot, QDir |
| 33 | +from qgis.PyQt.QtGui import (QIcon, |
| 34 | + QBrush, |
| 35 | + QColor) |
| 36 | +from qgis.PyQt.QtWidgets import (QTreeWidgetItem, |
| 37 | + QFileDialog, |
| 38 | + QMessageBox, |
| 39 | + QInputDialog, |
| 40 | + QColorDialog |
| 41 | + ) |
| 42 | +from qgis.PyQt.QtXml import QDomDocument |
| 43 | + |
| 44 | +from qgis.core import QgsApplication, QgsMapLayer |
| 45 | +from qgis.analysis import QgsRelief |
| 46 | + |
| 47 | +from processing.gui.wrappers import WidgetWrapper |
| 48 | +from processing.tools import system |
| 49 | + |
| 50 | +pluginPath = os.path.dirname(__file__) |
| 51 | +WIDGET, BASE = uic.loadUiType(os.path.join(pluginPath, 'reliefcolorswidgetbase.ui')) |
| 52 | + |
| 53 | + |
| 54 | +class ReliefColorsWidget(BASE, WIDGET): |
| 55 | + |
| 56 | + def __init__(self): |
| 57 | + super(ReliefColorsWidget, self).__init__(None) |
| 58 | + self.setupUi(self) |
| 59 | + |
| 60 | + self.btnAdd.setIcon(QgsApplication.getThemeIcon('/symbologyAdd.svg')) |
| 61 | + self.btnRemove.setIcon(QgsApplication.getThemeIcon('/symbologyRemove.svg')) |
| 62 | + self.btnUp.setIcon(QgsApplication.getThemeIcon('/symbologyUp.svg')) |
| 63 | + self.btnDown.setIcon(QgsApplication.getThemeIcon('/symbologyDown.svg')) |
| 64 | + self.btnLoad.setIcon(QgsApplication.getThemeIcon('/mActionFileOpen.svg')) |
| 65 | + self.btnSave.setIcon(QgsApplication.getThemeIcon('/mActionFileSave.svg')) |
| 66 | + self.btnAuto.setIcon(QgsApplication.getThemeIcon('/mActionDraw.svg')) |
| 67 | + |
| 68 | + self.layer = None |
| 69 | + |
| 70 | + @pyqtSlot() |
| 71 | + def on_btnAdd_clicked(self): |
| 72 | + item = QTreeWidgetItem() |
| 73 | + item.setText(0, '0.00') |
| 74 | + item.setText(1, '0.00') |
| 75 | + item.setBackground(2, QBrush(QColor(127, 127, 127))) |
| 76 | + self.reliefClassTree.addTopLevelItem(item) |
| 77 | + |
| 78 | + @pyqtSlot() |
| 79 | + def on_btnRemove_clicked(self): |
| 80 | + selectedItems = self.reliefClassTree.selectedItems() |
| 81 | + for item in selectedItems: |
| 82 | + self.reliefClassTree.invisibleRootItem().removeChild(item) |
| 83 | + item = None |
| 84 | + |
| 85 | + @pyqtSlot() |
| 86 | + def on_btnDown_clicked(self): |
| 87 | + selectedItems = self.reliefClassTree.selectedItems() |
| 88 | + for item in selectedItems: |
| 89 | + currentIndex = self.reliefClassTree.indexOfTopLevelItem(item) |
| 90 | + if currentIndex < self.reliefClassTree.topLevelItemCount() - 1: |
| 91 | + self.reliefClassTree.takeTopLevelItem(currentIndex) |
| 92 | + self.reliefClassTree.insertTopLevelItem(currentIndex + 1, item) |
| 93 | + self.reliefClassTree.setCurrentItem(item) |
| 94 | + |
| 95 | + @pyqtSlot() |
| 96 | + def on_btnUp_clicked(self): |
| 97 | + selectedItems = self.reliefClassTree.selectedItems() |
| 98 | + for item in selectedItems: |
| 99 | + currentIndex = self.reliefClassTree.indexOfTopLevelItem(item) |
| 100 | + if currentIndex > 0: |
| 101 | + self.reliefClassTree.takeTopLevelItem(currentIndex) |
| 102 | + self.reliefClassTree.insertTopLevelItem(currentIndex - 1, item) |
| 103 | + self.reliefClassTree.setCurrentItem(item) |
| 104 | + |
| 105 | + @pyqtSlot() |
| 106 | + def on_btnLoad_clicked(self): |
| 107 | + fileName, _ = QFileDialog.getOpenFileName(None, |
| 108 | + self.tr('Import Colors and elevations from XML'), |
| 109 | + QDir.homePath(), |
| 110 | + self.tr('XML files (*.xml *.XML)')) |
| 111 | + if fileName == '': |
| 112 | + return |
| 113 | + |
| 114 | + doc = QDomDocument() |
| 115 | + with codecs.open(fileName, 'r', encoding='utf-8') as f: |
| 116 | + content = f.read() |
| 117 | + |
| 118 | + if not doc.setContent(content): |
| 119 | + QMessageBox.critical(None, |
| 120 | + self.tr('Error parsing XML'), |
| 121 | + self.tr('The XML file could not be loaded')) |
| 122 | + return |
| 123 | + |
| 124 | + self.reliefClassTree.clear() |
| 125 | + reliefColorList = doc.elementsByTagName('ReliefColor') |
| 126 | + for i in range(reliefColorList.length()): |
| 127 | + elem = reliefColorList.at(i).toElement() |
| 128 | + item = QTreeWidgetItem() |
| 129 | + item.setText(0, elem.attribute('MinElevation')) |
| 130 | + item.setText(1, elem.attribute('MaxElevation')) |
| 131 | + item.setBackground(2, QBrush(QColor(int(elem.attribute('red')), |
| 132 | + int(elem.attribute('green')), |
| 133 | + int(elem.attribute('blue'))))) |
| 134 | + self.reliefClassTree.addTopLevelItem(item) |
| 135 | + |
| 136 | + @pyqtSlot() |
| 137 | + def on_btnSave_clicked(self): |
| 138 | + fileName, _ = QFileDialog.getSaveFileName(None, |
| 139 | + self.tr('Export Colors and elevations as XML'), |
| 140 | + QDir.homePath(), |
| 141 | + self.tr('XML files (*.xml *.XML)')) |
| 142 | + |
| 143 | + if fileName == '': |
| 144 | + return |
| 145 | + |
| 146 | + if not fileName.lower().endswith('.xml'): |
| 147 | + fileName += '.xml' |
| 148 | + |
| 149 | + doc = QDomDocument() |
| 150 | + colorsElem = doc.createElement('ReliefColors') |
| 151 | + doc.appendChild(colorsElem) |
| 152 | + |
| 153 | + colors = self.reliefColors() |
| 154 | + for c in colors: |
| 155 | + elem = doc.createElement('ReliefColor') |
| 156 | + elem.setAttribute('MinElevation', str(c.minElevation)) |
| 157 | + elem.setAttribute('MaxElevation', str(c.maxElevation)) |
| 158 | + elem.setAttribute('red', str(c.color.red())) |
| 159 | + elem.setAttribute('green', str(c.color.green())) |
| 160 | + elem.setAttribute('blue', str(c.color.blue())) |
| 161 | + colorsElem.appendChild(elem) |
| 162 | + |
| 163 | + with codecs.open(fileName, 'w', encoding='utf-8') as f: |
| 164 | + f.write(doc.toString(2)) |
| 165 | + |
| 166 | + @pyqtSlot() |
| 167 | + def on_btnAuto_clicked(self): |
| 168 | + if self.layer is None: |
| 169 | + return |
| 170 | + |
| 171 | + relief = QgsRelief(self.layer, system.getTempFilename(), 'GTiff') |
| 172 | + colors = relief.calculateOptimizedReliefClasses() |
| 173 | + self.populateColors(colors) |
| 174 | + |
| 175 | + @pyqtSlot(QTreeWidgetItem, int) |
| 176 | + def on_reliefClassTree_itemDoubleClicked(self, item, column): |
| 177 | + if not item: |
| 178 | + return |
| 179 | + |
| 180 | + if column == 0: |
| 181 | + d, ok = QInputDialog.getDouble(None, |
| 182 | + self.tr('Enter lower elevation class bound'), |
| 183 | + self.tr('Elevation'), |
| 184 | + float(item.text(0)), |
| 185 | + decimals=2) |
| 186 | + if ok: |
| 187 | + item.setText(0, str(d)) |
| 188 | + elif column == 1: |
| 189 | + d, ok = QInputDialog.getDouble(None, |
| 190 | + self.tr('Enter upper elevation class bound'), |
| 191 | + self.tr('Elevation'), |
| 192 | + float(item.text(1)), |
| 193 | + decimals=2) |
| 194 | + if ok: |
| 195 | + item.setText(1, str(d)) |
| 196 | + elif column == 2: |
| 197 | + c = QColorDialog.getColor(item.background(2).color(), |
| 198 | + None, |
| 199 | + self.tr('Select color for relief class')) |
| 200 | + if c.isValid(): |
| 201 | + item.setBackground(2, QBrush(c)) |
| 202 | + |
| 203 | + def reliefColors(self): |
| 204 | + colors = [] |
| 205 | + for i in range(self.reliefClassTree.topLevelItemCount()): |
| 206 | + item = self.reliefClassTree.topLevelItem(i) |
| 207 | + if item: |
| 208 | + c = QgsRelief.ReliefColor(item.background(2).color(), |
| 209 | + float(item.text(0)), |
| 210 | + float(item.text(1))) |
| 211 | + colors.append(c) |
| 212 | + return colors |
| 213 | + |
| 214 | + def populateColors(self, colors): |
| 215 | + self.reliefClassTree.clear() |
| 216 | + for c in colors: |
| 217 | + item = QTreeWidgetItem() |
| 218 | + item.setText(0, str(c.minElevation)) |
| 219 | + item.setText(1, str(c.maxElevation)) |
| 220 | + item.setBackground(2, QBrush(c.color)) |
| 221 | + self.reliefClassTree.addTopLevelItem(item) |
| 222 | + |
| 223 | + def setLayer(self, layer): |
| 224 | + self.layer = layer |
| 225 | + |
| 226 | + def setValue(self, value): |
| 227 | + self.reliefClassTree.clear() |
| 228 | + rows = value.split(';') |
| 229 | + for r in rows: |
| 230 | + v = r.split(',') |
| 231 | + item = QTreeWidgetItem() |
| 232 | + item.setText(0, v[0]) |
| 233 | + item.setText(1, v[1]) |
| 234 | + color = QColor(int(v[2]), int(v[3]), int(v[4])) |
| 235 | + item.setBackground(2, QBrush(color)) |
| 236 | + self.reliefClassTree.addTopLevelItem(item) |
| 237 | + |
| 238 | + def value(self): |
| 239 | + rColors = self.reliefColors() |
| 240 | + colors = '' |
| 241 | + for c in rColors: |
| 242 | + colors += '{:.2f}, {:.2f}, {:d}, {:d}, {:d};'.format(c.minElevation, |
| 243 | + c.maxElevation, |
| 244 | + c.color.red(), |
| 245 | + c.color.green(), |
| 246 | + c.color.blue()) |
| 247 | + return colors[:-1] |
| 248 | + |
| 249 | + |
| 250 | +class ReliefColorsWidgetWrapper(WidgetWrapper): |
| 251 | + |
| 252 | + def createWidget(self): |
| 253 | + return ReliefColorsWidget() |
| 254 | + |
| 255 | + def postInitialize(self, wrappers): |
| 256 | + for wrapper in wrappers: |
| 257 | + if wrapper.param.name == self.param.parent: |
| 258 | + self.setLayer(wrapper.value()) |
| 259 | + wrapper.widgetValueHasChanged.connect(self.parentValueChanged) |
| 260 | + break |
| 261 | + |
| 262 | + def parentValueChanged(self, wrapper): |
| 263 | + self.setLayer(wrapper.value()) |
| 264 | + |
| 265 | + def setLayer(self, layer): |
| 266 | + if isinstance(layer, QgsMapLayer): |
| 267 | + layer = layer.source() |
| 268 | + self.widget.setLayer(layer) |
| 269 | + |
| 270 | + def setValue(self, value): |
| 271 | + self.widget.setValue(value) |
| 272 | + |
| 273 | + def value(self): |
| 274 | + return self.widget.value() |
0 commit comments