Skip to content

Commit

Permalink
Merge branch 'poprawa_odczytu_stylow_w_configu' into 'dev'
Browse files Browse the repository at this point in the history
Poprawa odczytu stylow w configu

See merge request polamap/giap_layout!30
  • Loading branch information
gorzelakp committed Jun 29, 2021
2 parents ccbb0ca + ee8aa02 commit 31de53e
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 9 deletions.
3 changes: 2 additions & 1 deletion StyleManager/stylemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from qgis.PyQt.QtWidgets import QDialog, QFileDialog, QInputDialog, QMessageBox
from qgis.PyQt.QtCore import Qt, QCoreApplication
from ..utils import tr
from ..utils import DEFAULT_STYLE

FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'ui_stylemanager.ui'))
Expand Down Expand Up @@ -57,7 +58,7 @@ def delete_style(self):

def set_default(self):
"""Set default qgis style"""
res, msg = self.mn.activate_style('default')
res, msg = self.mn.activate_style(DEFAULT_STYLE)

def change_style(self):
"""Change style to user selected"""
Expand Down
1 change: 0 additions & 1 deletion config.json

This file was deleted.

22 changes: 18 additions & 4 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import os
import json
from qgis._core import QgsMessageLog
from .utils import DEFAULT_STYLE


class Config:
def __init__(self):
"""Constructor"""
# load config from file
self.setts = {}
self.conf_dir = os.path.dirname(__file__)
fl = open(os.path.join(self.conf_dir, 'config.json'), 'r')
conf = fl.read()
fl.close()
self.setts = json.loads(conf)[0]
config_path = os.path.join(self.conf_dir, 'config.json')
if os.path.exists(config_path):
with open(config_path, 'r') as fl:
conf = fl.read()
try:
self.setts = json.loads(conf)[0]
except ValueError:
QgsMessageLog.logMessage('Failed to load config from config.json')

def save_config(self):
"""
Expand Down Expand Up @@ -154,6 +161,13 @@ def set_style(self, style, path):
"""
self.setts['styles'][style] = path

def set_default_style(self, dic_styles):
if 'styles' not in self.setts:
self.setts['styles'] = dic_styles
if 'active_style' not in self.setts:
self.setts['active_style'] = DEFAULT_STYLE
self.save_config()

def set_active_style(self, style):
"""
set name of active style in config
Expand Down
4 changes: 4 additions & 0 deletions giap_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def __init__(self, iface):
self.iface.initializationCompleted.connect(self.load_ribbons)

def initGui(self):
# set default style and active style if config.json doesn't extists
dic_style = self.style_manager.get_style_dictionary()
self.config.set_default_style(dic_style)

style = self.main_widget.styleSheet()
self.iface.mainWindow().statusBar().setStyleSheet(style + """
QSpinBox {
Expand Down
5 changes: 5 additions & 0 deletions i18n/giap_pl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
<TS version="2.1" language="pl" sourcelanguage="en">
<context>
<name>@default</name>
<message>
<location filename="../config.py" line="20"/>
<source>Failed to load config from config.json</source>
<translation>Wystąpił błąd podczas wczytywania pliku konfiguracyjnego config.json</translation>
</message>
<message>
<location filename="../giap_dynamic_layout.py" line="37"/>
<source>New Tab</source>
Expand Down
10 changes: 7 additions & 3 deletions tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from qgis.PyQt.QtCore import QFileSystemWatcher

from .utils import tr
from .utils import DEFAULT_STYLE


class StyleManager:
Expand Down Expand Up @@ -35,6 +36,9 @@ def __init__(self, parent):
def get_style_list(self):
return [x for x in self.styles.keys()]

def get_style_dictionary(self):
return self.styles

def run_last_style(self):
""" load active style on stratup"""
try:
Expand Down Expand Up @@ -86,14 +90,14 @@ def activate_style(self, name):

# if path to style not found set default style
if pth in ['', None]:
self.app.setStyleSheet('')
self.app.setStyleSheet(DEFAULT_STYLE)
pth = ''

# check if file exist
if not os.path.exists(pth):
self.app.setStyleSheet('') # return do default, and save it in con
self.app.setStyleSheet(DEFAULT_STYLE) # return do default, and save it in con
if name == 'default':
self.config.set_active_style('')
self.config.set_active_style(DEFAULT_STYLE)
return False, tr('Default style set')
else:
return False, tr('Path to *.qss not found, load default style')
Expand Down
1 change: 1 addition & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,5 @@ def get_project_config(parameter, key, default=''):

]

DEFAULT_STYLE = "GIAP"
DEFAULT_TABS = ['Main', 'Tools', 'Vector', 'Raster']

0 comments on commit 31de53e

Please sign in to comment.