11import subprocess
2+ from pathlib import Path
23
34from 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
69from src .backend .rule_manager import RuleManager
710from src .backend .tile_handler import TileHandler
811from 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 ()
0 commit comments