Skip to content

Commit a9607c7

Browse files
committed
use app state
1 parent e48f7ca commit a9607c7

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

src/widgets/widget_image_selector.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from PyQt6.QtWidgets import QWidget, QPushButton, QVBoxLayout, QFileDialog
22
from PyQt6.QtGui import QPixmap
3+
4+
from config.app_state import AppState
35
from src.widgets.widget_tile_clicker import TileClicker
46

57

@@ -37,5 +39,6 @@ def select_image(self):
3739
pixmap = QPixmap(image_path)
3840
if not pixmap.isNull():
3941
self.image_label.setPixmap(pixmap)
40-
else:
41-
self.image_label.setText("Invalid image file")
42+
AppState.setImagePath(image_path)
43+
# else:
44+
# self.image_label.setText("Invalid image file")

src/widgets/widget_mapper_generator.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import subprocess
2+
from pathlib import Path
23

34
from PyQt6.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QRadioButton, QLabel, QPushButton, QLineEdit, QComboBox
4-
from PyQt6.QtGui import QPixmap
5+
from PyQt6.QtGui import QPixmap, QRegularExpressionValidator
6+
from PyQt6.QtCore import QRegularExpression
57

8+
from config.app_state import AppState
69
from src.backend.rule_manager import RuleManager
710
from src.backend.tile_handler import TileHandler
811
from src.dialogs.dialog_check_map import CheckMapDialog
@@ -76,6 +79,16 @@ def __init__(self, parent=None):
7679
self.generate_button.clicked.connect(self.openFileDialog)
7780
self.ddnet_push_button.clicked.connect(self.startDDNetCheck)
7881

82+
# some configuration
83+
# configure validator new_mapper_line_edit
84+
# this may break entering utf16 characters. Won't fix until somebody breaks it
85+
pattern = QRegularExpression(r'^[\w\d-]+$') # disallow whitespaces and other special characters
86+
validator = QRegularExpressionValidator(pattern, self)
87+
self.new_mapper_line_edit.setValidator(validator)
88+
89+
# https://github.com/ddnet/ddnet/blob/c7dc7b6a94528040678b7a0fab17ccb447e1d94d/src/game/editor/auto_map.h#L52
90+
self.new_mapper_line_edit.setMaxLength(128) # limit number of characters
91+
7992
def setImage(self, image_path):
8093
pixmap = QPixmap(image_path)
8194
self.image_label.setPixmap(pixmap)
@@ -84,12 +97,15 @@ def setImage(self, image_path):
8497
self.image_label.setMaximumSize(200, 200)
8598

8699
def openFileDialog(self):
100+
rule_name = self.new_mapper_line_edit.text()
101+
if not AppState.imagePath() or not len(rule_name):
102+
return
87103
cmd = CheckMapDialog(self)
88104
ret = cmd.exec()
89105
if ret:
90-
filename = "placeholder.rules"
91-
rule_name = "DummyRule"
92-
RuleManager.saveRule(filename, rule_name)
106+
loaded_image_path = Path(AppState.imagePath())
107+
filename = f"{loaded_image_path.stem}.rules"
108+
AppState.ruleManager().saveRule(filename, rule_name)
93109
"""
94110
options = QFileDialog.Options()
95111
file_path, _ = QFileDialog.getSaveFileName(self, "Save File", "", "All Files (*);;Text Files (*.txt)",
@@ -101,6 +117,7 @@ def openFileDialog(self):
101117
else:
102118
QMessageBox.warning(self, "Warning", "Please select an image first.")
103119
"""
120+
104121
def ruleNameToggle(self):
105122
if self.radio_buttons[0].isChecked():
106123
self.existing_mapper.hide()

src/widgets/widget_tile_clicker.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77

88
class TileClicker(QWidget):
9-
109
def __init__(self, parent=None):
1110
super().__init__(parent)
1211
self.gridLayout = QGridLayout()

0 commit comments

Comments
 (0)