Skip to content

Commit 47f2015

Browse files
committed
update mapper generator combobox when an image is loaded and a rule for it is also loaded and found
1 parent 623fcce commit 47f2015

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

src/backend/rule_manager.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,51 @@ class RuleManager:
1414
def __init__(self):
1515
self._config: Dict[str, List[str]] = {}
1616
self._header: List[str] = []
17+
self._loadedRules = False
1718

1819
def _loadRuleFile(self, filename_base):
19-
filename = f"{filename_base}.rules"
20-
data_path = Path(ConfigManager.config()["data_path"])
21-
if not data_path:
22-
raise ValueError("No editor directory path known")
20+
if not self._loadedRules:
21+
filename = f"{filename_base}.rules"
22+
data_path = Path(ConfigManager.config()["data_path"])
23+
if not data_path:
24+
raise ValueError("No editor directory path known")
2325

24-
automapper_path = data_path.joinpath(Path("editor/automap"))
25-
if not automapper_path.exists():
26-
automapper_path.mkdir()
26+
automapper_path = data_path.joinpath(Path("editor/automap"))
27+
if not automapper_path.exists():
28+
automapper_path.mkdir()
2729

28-
full_file_path = automapper_path.joinpath(Path(filename))
30+
full_file_path = automapper_path.joinpath(Path(filename))
2931

30-
# load file if it exists
31-
if full_file_path.is_file():
32-
self._readRuleFile(full_file_path)
32+
# load file if it exists
33+
if full_file_path.is_file():
34+
self._readRuleFile(full_file_path)
3335

34-
# file doesn't exist, just to be explicit
35-
else:
36-
self._config = {}
37-
self._header = []
36+
# file doesn't exist, just to be explicit
37+
else:
38+
self._config = {}
39+
self._header = []
40+
self._loadedRules = True
41+
42+
def loadRules(self, filename):
43+
if not self._loadedRules:
44+
filename = RuleManager._getFileBase(filename)
45+
self._loadRuleFile(filename)
3846

3947
def saveRule(self, filename, rule_name):
48+
filename = RuleManager._getFileBase(filename)
49+
if not self._loadedRules:
50+
self._loadRuleFile(filename)
51+
self._config[rule_name] = [] # overwrite rules
52+
self._config[rule_name] = RuleManager._createRulesFromTileHandler()
53+
self._writeRuleFile(filename)
4054

55+
def getRules(self) -> List[str]:
56+
if not self._loadedRules:
57+
raise ValueError("Rules are not loaded yet")
58+
return list(self._config.keys())
59+
60+
@staticmethod
61+
def _getFileBase(filename):
4162
# remove mime type and check it if exists
4263
splits = filename.split(".")
4364
if len(splits) > 2:
@@ -46,11 +67,7 @@ def saveRule(self, filename, rule_name):
4667
if splits[1] != "rules":
4768
raise ValueError(f"Unknown rule mime type '{splits[1]}'")
4869
filename = splits[0]
49-
50-
self._loadRuleFile(filename)
51-
self._config[rule_name] = [] # overwrite rules
52-
self._config[rule_name] = RuleManager._createRulesFromTileHandler()
53-
self._writeRuleFile(filename)
70+
return filename
5471

5572
@staticmethod
5673
def _createRulesFromTileHandler():

src/config/app_state.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from src.backend.rule_manager import RuleManager
1010
from src.signals.signal_emitter import ApplicationStatusEmitter, ApplicationStatusEnum
11+
from pathlib import Path
1112

1213

1314
class AppState:
@@ -39,6 +40,7 @@ def ruleManager(cls):
3940
@classmethod
4041
def setImagePath(cls, image_path):
4142
cls.instance().main_image_path = image_path
43+
cls.instance().rule_manager.loadRules(Path(image_path).stem)
4244
cls.instance().signal_emitter.application_status_signal.emit(ApplicationStatusEnum.IMAGE_LOADED, "")
4345

4446
@classmethod

src/main_window.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ def showAbout(self):
6565
def statusUpdateReceived(self, status_type: ApplicationStatusEnum, message: str):
6666
if status_type == ApplicationStatusEnum.IMAGE_LOADED:
6767
self.mapper_generator.setEnabled(True)
68+
self.mapper_generator.widget().rulesLoaded()
6869
# TODO set status dockwidget "Successfully loaded image"
6970
pass

src/widgets/widget_mapper_generator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212

1313
class MapperGeneratorWidget(QWidget):
14-
# noinspection PyUnresolvedReferences
1514
def __init__(self, parent=None):
1615
super().__init__(parent)
1716

@@ -130,3 +129,7 @@ def startDDNetCheck(self):
130129
return
131130
cmd = [client_path, map_name]
132131
subprocess.Popen(cmd, start_new_session=True)
132+
133+
def rulesLoaded(self):
134+
rules = AppState.ruleManager().getRules()
135+
self.existing_mapper_combobox.addItems(rules)

0 commit comments

Comments
 (0)