-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraint_function_widget.py
More file actions
582 lines (496 loc) · 20.5 KB
/
Copy pathconstraint_function_widget.py
File metadata and controls
582 lines (496 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# constraint_function_widget.py
from __future__ import annotations
from typing import Optional, Dict
from PyQt6.QtWidgets import (
QWidget, QVBoxLayout, QHBoxLayout, QApplication,
QSizePolicy, QGroupBox, QPushButton, QLineEdit
)
from PyQt6.QtCore import pyqtSignal, Qt
from xml.etree import ElementTree as ET
import sys, os, re
from string_field import StringField
from string_options_field import StringOptionsField
from file_path_field import FilePathField
from directory_path_field import DirectoryPathField
from remote_server_widget import RemoteServerWidget
class ConstraintFunction(QWidget):
"""
Widget for configuring a Constraint Function.
"""
changed = pyqtSignal()
def __init__(
self,
*,
label_width: int = 180,
field_width: int = 360,
button_size: int = 40,
parent: Optional[QWidget] = None,
):
super().__init__(parent)
# --------------------------------------------------
# Defaults
# --------------------------------------------------
self._default_name = "ConstraintFunction"
self._default_execution_location = "local"
self._default_paths = ["", "", "", ""]
self._default_workdir = os.getcwd()
# name validation state + style
self._name_valid = True
self._invalid_name_style = """
QLineEdit {
border: 2px solid red;
border-radius: 3px;
}
"""
# --------------------------------------------------
# Group box
# --------------------------------------------------
self.group_box = QGroupBox("Constraint Function", self)
approx_width = label_width + field_width + 230
self.group_box.setMinimumWidth(approx_width)
self.group_box.setMaximumWidth(approx_width + 230)
self.group_box.setSizePolicy(
QSizePolicy.Policy.Preferred,
QSizePolicy.Policy.Fixed
)
# --------------------------------------------------
# Fields
# --------------------------------------------------
self.name_field = StringField(
"Name",
default=self._default_name,
label_width=label_width,
field_width=field_width,
parent=self.group_box,
)
self.name_field.textChanged.connect(self._on_name_changed)
# NEW: Alias field
self.alias_field = StringField(
"Alias",
default="",
label_width=label_width,
field_width=field_width,
parent=self.group_box,
)
# CHANGED: call our handler (still emits changed)
self.alias_field.textChanged.connect(self._on_alias_changed)
self.execution_location_field = StringOptionsField(
"Execution location",
value=self._default_execution_location,
options=["local", "remote"],
label_width=label_width,
field_width=field_width,
parent=self.group_box,
)
self.definition_field = StringField(
"Definition",
default="> 0.0",
label_width=label_width,
field_width=field_width,
parent=self.group_box,
)
self.file_fields = FilePathField(
["Executable file name",
"Training data file",
"Design variables file",
"Output file"],
path=list(self._default_paths),
select_mode="open_file",
dialog_title="Select File",
filters=[
["Python (*.py)", "Executables (*.sh *.bin *.exe)", "All files (*)"],
["Data (*.csv *.dat *.txt)", "All files (*)"],
["Data (*.csv *.dat *.txt *.xml)", "All files (*)"],
["Data (*.dat *.txt *.csv)", "All files (*)"],
],
label_width=label_width,
field_width=field_width,
parent=self.group_box,
)
# --- NEW: training-data field hint + validation ---
if hasattr(self.file_fields, "_fields") and len(self.file_fields._fields) >= 2:
# index 1 == "Training data file"
_lbl_tr, edit_tr, _btn_tr = self.file_fields._fields[1]
edit_tr.setPlaceholderText("Specify a CSV file (*.csv)")
# connect change signal once; we re-validate on any change
edit_tr.textChanged.connect(self._on_training_file_changed)
# NEW: validate filename for Design variables file (index 2) and Output file (index 3)
if hasattr(self.file_fields, "_fields") and self.file_fields._fields:
if len(self.file_fields._fields) > 2:
_lbl, edit, _btn = self.file_fields._fields[2]
if isinstance(edit, QLineEdit):
edit.textChanged.connect(lambda _t: self._validate_filename_field(2))
if len(self.file_fields._fields) > 3:
_lbl, edit, _btn = self.file_fields._fields[3]
if isinstance(edit, QLineEdit):
edit.textChanged.connect(lambda _t: self._validate_filename_field(3))
# NEW: apply initial state
self._sync_executable_with_alias()
# --------------------------------------------------
# Working dir, remote server, etc.
# --------------------------------------------------
self.working_dir_field = DirectoryPathField(
"Working directory",
path=self._default_workdir,
label_width=label_width,
field_width=field_width,
parent=self.group_box,
)
if hasattr(self.working_dir_field, "layout"):
self.working_dir_field.layout().setAlignment(
Qt.AlignmentFlag.AlignLeft
)
self.remote_server_widget = RemoteServerWidget(
label_width=label_width,
field_width=field_width,
parent=self.group_box,
)
self.remote_server_widget.setVisible(False)
self.clear_button = QPushButton("Clear", self.group_box)
self.clear_button.clicked.connect(self.clear_fields)
# --------------------------------------------------
# Layout
# --------------------------------------------------
inner_layout = QVBoxLayout(self.group_box)
inner_layout.setContentsMargins(8, 16, 8, 8)
inner_layout.setSpacing(5)
inner_layout.setAlignment(
Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft
)
inner_layout.addWidget(self.name_field)
inner_layout.addWidget(self.alias_field) # NEW: Add alias field to layout
inner_layout.addWidget(self.execution_location_field)
inner_layout.addWidget(self.definition_field)
inner_layout.addWidget(self.file_fields)
inner_layout.addWidget(self.working_dir_field)
inner_layout.addWidget(self.remote_server_widget)
btn_row = QHBoxLayout()
btn_row.addStretch(1)
btn_row.addWidget(self.clear_button)
inner_layout.addLayout(btn_row)
outer_layout = QVBoxLayout(self)
outer_layout.addWidget(self.group_box)
outer_layout.setAlignment(
Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignLeft
)
self.setLayout(outer_layout)
self.setSizePolicy(
QSizePolicy.Policy.Expanding,
QSizePolicy.Policy.Fixed
)
# --------------------------------------------------
# Signals
# --------------------------------------------------
self.name_field.textChanged.connect(self.changed.emit)
self.execution_location_field.valueChanged.connect(
self._on_execution_location_changed
)
self.definition_field.textChanged.connect(self._on_definition_changed)
if hasattr(self.file_fields, "pathChanged"):
self.file_fields.pathChanged.connect(lambda _: self.changed.emit())
self.working_dir_field.pathChanged.connect(lambda _: self.changed.emit())
self.remote_server_widget.changed.connect(self.changed.emit)
# initial validation
self._on_definition_changed(self.definition_field.text)
# ==================================================
# Validation
# ==================================================
def _is_valid_definition(self, text: str) -> bool:
text = text.strip()
if not text:
return False
pattern = r'^[<>]\s*[+-]?\d+(\.\d+)?$'
return re.match(pattern, text) is not None
def _on_definition_changed(self, text: str) -> None:
valid = self._is_valid_definition(text)
self.definition_field.set_valid(
valid,
tooltip="Expected format: > 10.0 or < -3"
)
self.changed.emit()
def _on_name_changed(self, text: str) -> None:
"""
Validate constraint name:
- must not be empty
- must not contain ANY whitespace
- only letters, digits, underscore allowed
"""
import re
name = text # do NOT strip; "Constraint1 " should be invalid
if not name or any(ch.isspace() for ch in name):
is_valid = False
else:
is_valid = bool(re.fullmatch(r"[A-Za-z0-9_]+", name))
self._set_name_valid(is_valid)
def _set_name_valid(self, valid: bool) -> None:
if self._name_valid == valid:
return
self._name_valid = valid
# underlying QLineEdit from StringField
edit = self.name_field._edit # adapt if your StringField exposes it differently
if valid:
edit.setStyleSheet("")
edit.setToolTip("")
else:
edit.setStyleSheet(self._invalid_name_style)
edit.setToolTip(
"Invalid name. Use only letters, digits, and underscore; "
"no spaces or special characters."
)
# --------------------------------------------------
# Training CSV validation (like ObjectiveFunction)
# --------------------------------------------------
def _on_training_file_changed(self, text: str) -> None:
"""
Validate that the Training data file is a .csv and show a red border if not.
"""
# find the training-data widgets (2nd entry)
if not hasattr(self.file_fields, "_fields") or len(self.file_fields._fields) < 2:
return
_lbl_tr, edit_tr, _btn_tr = self.file_fields._fields[1]
filename = (text or "").strip()
if not filename:
# empty is considered invalid
self._set_training_file_valid(
valid=False,
tooltip="Training data file is required and must be a .csv file.",
edit=edit_tr,
)
return
if filename.lower().endswith(".csv"):
self._set_training_file_valid(valid=True, tooltip="", edit=edit_tr)
else:
self._set_training_file_valid(
valid=False,
tooltip="Training data file must have .csv extension.",
edit=edit_tr,
)
def _set_training_file_valid(self, *, valid: bool, tooltip: str, edit) -> None:
"""
Simple visual validation: red border on invalid, normal on valid.
"""
if valid:
edit.setStyleSheet("")
else:
edit.setStyleSheet(
"QLineEdit { border: 2px solid red; border-radius: 3px; }"
)
edit.setToolTip(tooltip)
# --------------------------------------------------
# Filename validation for design/output files
# --------------------------------------------------
def _validate_filename_field(self, index: int) -> None:
"""
Validate file name (basename) for:
- index 2: Design variables file
- index 3: Output file
Regex:
^[A-Za-z0-9][A-Za-z0-9 _.-]{0,198}[A-Za-z0-9]$
"""
if not hasattr(self, "file_fields") or not hasattr(self.file_fields, "_fields"):
return
if index >= len(self.file_fields._fields):
return
_lbl, edit, _btn = self.file_fields._fields[index]
if not isinstance(edit, QLineEdit):
return
path = (edit.text() or "").strip()
# allow empty (no error styling)
if not path:
edit.setStyleSheet("")
edit.setToolTip("")
return
name = os.path.basename(path)
pattern = re.compile(r"^[A-Za-z0-9][A-Za-z0-9 _\.-]{0,198}[A-Za-z0-9]$")
ok = bool(pattern.fullmatch(name))
if ok:
edit.setStyleSheet("")
edit.setToolTip("")
else:
edit.setStyleSheet(self._invalid_name_style)
edit.setToolTip(
"Invalid filename. Allowed: letters/digits; may include space, underscore, dot, hyphen; "
"must start and end with a letter/digit; length 2-200."
)
# ==================================================
# Core logic
# ==================================================
def clear_fields(self) -> None:
self.name_field.text = self._default_name
self.alias_field.text = "" # NEW: Clear alias field
self.execution_location_field.value = self._default_execution_location
self.definition_field.text = ""
self.file_fields.set_paths(list(self._default_paths))
self.working_dir_field.path = self._default_workdir
self.remote_server_widget.setVisible(False)
self.remote_server_widget.set_values(
hostname="", username="", port="22"
)
self.changed.emit()
def snapshot(self) -> Dict[str, str | Dict[str, str]]:
paths = self.file_fields.paths
data: Dict[str, str | Dict[str, str]] = {
"name": self.name_field.text.strip(),
"alias": self.alias_field.text.strip(), # NEW: Include alias in snapshot
"execution_location": self.execution_location_field.value.strip(),
"definition": self.definition_field.text.strip(),
"executable_filename": paths[0],
"training_data_filename": paths[1],
"design_vector_filename": paths[2],
"output_filename": paths[3],
"working_directory": self.working_dir_field.path.strip(),
}
if self.execution_location_field.value.lower() == "remote":
data["remote_server"] = self.remote_server_widget.snapshot()
return data
# ==================================================
# XML
# ==================================================
def to_xml(self, *, include_empty: bool = False) -> ET.Element:
root = ET.Element("constraint_function")
def add(tag: str, text: str):
if text or include_empty:
ET.SubElement(root, tag).text = text
add("name", self.name_field.text.strip())
add("alias", self.alias_field.text.strip()) # NEW: Add alias to XML
add("execution_location", self.execution_location_field.value.strip())
s = self.definition_field.text.strip()
if self._is_valid_definition(s):
m = re.match(r'^([<>])\s*([+-]?\d+(\.\d+)?)$', s)
op, val = m.group(1), m.group(2)
add("constraint_type", "gt" if op == ">" else "lt")
add("constraint_value", val)
tags = [
"executable_filename",
"training_data_filename",
"design_vector_filename",
"output_filename",
]
for tag, val in zip(tags, self.file_fields.paths):
add(tag, val.strip())
add("working_directory", self.working_dir_field.path.strip())
if self.execution_location_field.value.lower() == "remote":
root.append(self.remote_server_widget.to_xml("remote_server"))
return root
def from_xml(self, element: ET.Element) -> None:
def get(tag: str) -> str:
el = element.find(tag)
return el.text.strip() if el is not None and el.text else ""
self.name_field.text = get("name") or self._default_name
self.alias_field.text = get("alias") # NEW: Load alias from XML
loc = get("execution_location") or self._default_execution_location
self.execution_location_field.value = loc
self.remote_server_widget.setVisible(loc.lower() == "remote")
ctype = get("constraint_type")
cval = get("constraint_value")
if ctype and cval:
# accept both legacy and new forms
if ctype.lower() == "gt":
op = ">"
elif ctype.lower() == "lt":
op = "<"
elif ctype in (">", "<"):
op = ctype
else:
op = ""
if op:
self.definition_field.text = f"{op} {cval}"
self.file_fields.set_paths([
get("executable_filename"),
get("training_data_filename"),
get("design_vector_filename"),
get("output_filename"),
])
wd = get("working_directory")
if wd:
self.working_dir_field.path = wd
rs_el = element.find("remote_server")
if rs_el is not None:
self.remote_server_widget.from_xml(rs_el)
self.remote_server_widget.setVisible(True)
self._on_definition_changed(self.definition_field.text)
# --------------------------------------------------
def _on_execution_location_changed(self, value: str) -> None:
self.remote_server_widget.setVisible(
value.strip().lower() == "remote"
)
self.changed.emit()
# --------------------------------------------------
# Alias behavior: disables executable when alias is set
# --------------------------------------------------
def _on_alias_changed(self, text: str) -> None:
"""
Alias rules:
- empty is allowed
- if non-empty: only letters/digits/underscore; no whitespace
Invalid alias is shown with a red border.
"""
alias = text # do NOT strip; "abc " should be invalid
if not alias:
valid = True
elif any(ch.isspace() for ch in alias):
valid = False
else:
valid = bool(re.fullmatch(r"[A-Za-z0-9_]+", alias))
# apply red-border feedback to alias field's QLineEdit
edit = self.alias_field.findChild(QLineEdit)
if edit is not None:
if valid:
edit.setStyleSheet("")
edit.setToolTip("")
else:
edit.setStyleSheet(self._invalid_name_style)
edit.setToolTip(
"Invalid alias. Use only letters, digits, and underscore; "
"no spaces or special characters."
)
self._sync_executable_with_alias()
self.changed.emit()
def _sync_executable_with_alias(self) -> None:
"""
If alias is non-empty:
- clear & disable 'Executable file name' (index 0)
- clear & disable 'Design variables file' (index 2)
Else:
- re-enable both
"""
if not hasattr(self, "file_fields") or not hasattr(self.file_fields, "_fields"):
return
if not self.file_fields._fields:
return
alias_active = bool((self.alias_field.text or "").strip())
inactive_style = "QLineEdit { background: #f0f0f0; color: #666; }"
def _toggle_index(i: int) -> None:
if i >= len(self.file_fields._fields):
return
_lbl, edit, btn = self.file_fields._fields[i]
if alias_active:
edit.blockSignals(True)
edit.setText("")
edit.blockSignals(False)
edit.setEnabled(False)
btn.setEnabled(False)
edit.setStyleSheet(inactive_style)
else:
edit.setEnabled(True)
btn.setEnabled(True)
edit.setStyleSheet("")
# index 0 == "Executable file name"
_toggle_index(0)
# NEW: index 2 == "Design variables file"
_toggle_index(2)
# --- public API for Study ---
@property
def name(self) -> str:
return self.name_field.text.strip()
@name.setter
def name(self, value: str) -> None:
self.name_field.text = value.strip() or self._default_name
# --------------------------------------------------
# Demo
if __name__ == "__main__":
app = QApplication(sys.argv)
w = ConstraintFunction()
w.setWindowTitle("ConstraintFunction — Demo")
w.show()
sys.exit(app.exec())