1# -*- coding: utf-8 -*-
2
3"""
4***************************************************************************
5    EditRenderingStylesDialog.py
6    ---------------------
7    Date                 : August 2012
8    Copyright            : (C) 2012 by Victor Olaya
9    Email                : volayaf 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__ = 'Victor Olaya'
21__date__ = 'August 2012'
22__copyright__ = '(C) 2012, Victor Olaya'
23
24import os
25import warnings
26
27from qgis.PyQt import uic
28from qgis.PyQt.QtCore import Qt
29from qgis.PyQt.QtWidgets import QDialog, QHeaderView, QTableWidgetItem
30
31from qgis.core import (QgsProcessingOutputRasterLayer,
32                       QgsProcessingOutputVectorLayer)
33
34from processing.gui.RenderingStyles import RenderingStyles
35from processing.gui.RenderingStyleFilePanel import RenderingStyleFilePanel
36
37pluginPath = os.path.split(os.path.dirname(__file__))[0]
38
39with warnings.catch_warnings():
40    warnings.filterwarnings("ignore", category=DeprecationWarning)
41    WIDGET, BASE = uic.loadUiType(
42        os.path.join(pluginPath, 'ui', 'DlgRenderingStyles.ui'))
43
44
45class EditRenderingStylesDialog(BASE, WIDGET):
46
47    def __init__(self, alg):
48        super(EditRenderingStylesDialog, self).__init__(None)
49        self.setupUi(self)
50
51        self.alg = alg
52
53        self.tblStyles.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
54        self.setWindowTitle(self.alg.displayName())
55
56        self.valueItems = {}
57        self.dependentItems = {}
58        self.setTableContent()
59
60    def setTableContent(self):
61        numOutputs = 0
62        for output in self.alg.outputDefinitions():
63            if isinstance(output, (QgsProcessingOutputVectorLayer, QgsProcessingOutputRasterLayer)):
64                numOutputs += 1
65        self.tblStyles.setRowCount(numOutputs)
66
67        i = 0
68        for output in self.alg.outputDefinitions():
69            if isinstance(output, (QgsProcessingOutputVectorLayer, QgsProcessingOutputRasterLayer)):
70                item = QTableWidgetItem(output.description() + '<' +
71                                        output.__class__.__name__ + '>')
72                item.setFlags(Qt.ItemIsEnabled)
73                self.tblStyles.setItem(i, 0, item)
74                item = RenderingStyleFilePanel()
75                style = \
76                    RenderingStyles.getStyle(self.alg.id(),
77                                             output.name())
78                if style:
79                    item.setText(str(style))
80                self.valueItems[output.name()] = item
81                self.tblStyles.setCellWidget(i, 1, item)
82                self.tblStyles.setRowHeight(i, 22)
83            i += 1
84
85    def accept(self):
86        styles = {}
87        for key in list(self.valueItems.keys()):
88            styles[key] = str(self.valueItems[key].getValue())
89        RenderingStyles.addAlgStylesAndSave(self.alg.id(), styles)
90
91        QDialog.accept(self)
92
93    def reject(self):
94        QDialog.reject(self)
95