1# -*- coding: utf-8 -*-
2
3"""
4***************************************************************************
5    RenderingStyles.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
25from processing.tools.system import userFolder
26
27
28class RenderingStyles:
29    styles = {}
30
31    @staticmethod
32    def addAlgStylesAndSave(algname, styles):
33        RenderingStyles.styles[algname] = styles
34        RenderingStyles.saveSettings()
35
36    @staticmethod
37    def configFile():
38        return os.path.join(userFolder(), 'processing_qgis_styles.conf')
39
40    @staticmethod
41    def loadStyles():
42        if not os.path.isfile(RenderingStyles.configFile()):
43            return
44        with open(RenderingStyles.configFile()) as lines:
45            line = lines.readline().strip('\n')
46            while line != '':
47                tokens = line.split('|')
48                if tokens[0] in list(RenderingStyles.styles.keys()):
49                    RenderingStyles.styles[tokens[0]][tokens[1]] = tokens[2]
50                else:
51                    alg = {}
52                    alg[tokens[1]] = tokens[2]
53                    RenderingStyles.styles[tokens[0]] = alg
54                line = lines.readline().strip('\n')
55
56    @staticmethod
57    def saveSettings():
58        with open(RenderingStyles.configFile(), 'w') as fout:
59            for alg in list(RenderingStyles.styles.keys()):
60                for out in list(RenderingStyles.styles[alg].keys()):
61                    fout.write(alg + '|' + out + '|' +
62                               RenderingStyles.styles[alg][out] + '\n')
63
64    @staticmethod
65    def getStyle(algname, outputname):
66        if algname in RenderingStyles.styles:
67            if outputname in RenderingStyles.styles[algname]:
68                return RenderingStyles.styles[algname][outputname]
69        return None
70