Skip to content

Commit fd8ce5c

Browse files
committed
impl: Check value ranges for input of spinboxes
1 parent e576977 commit fd8ce5c

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

nbstool.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,11 +498,16 @@ def initVarsAndCallbacksFrom(self, builder: Builder):
498498
builder.import_variables(self)
499499
builder.connect_callbacks(self)
500500

501-
def isInteger(self, value: str) -> bool:
502-
return value == '' or value.isdigit()
501+
def isRequiredInteger(self, value: str, widget_name: str) -> bool:
502+
if not value.isdigit():
503+
return False
504+
widget = self.toplevel.nametowidget(widget_name)
505+
if widget:
506+
return widget['from'] <= int(value) <= widget['to']
507+
return True
503508

504-
def isRequiredInteger(self, value: str) -> bool:
505-
return value.isdigit()
509+
def isInteger(self, value: str, widget_name: str) -> bool:
510+
return value == '' or self.isRequiredInteger(value, widget_name)
506511

507512
def getSelectedFilesVersion(self, selection: Iterable) -> int:
508513
fileVersion = -1
@@ -1513,7 +1518,7 @@ def parseFilePaths(string: str) -> tuple:
15131518

15141519

15151520
class ImportDialog:
1516-
def __init__(self, master, parent, fileExts: tuple, title: Optional[str], progressTitle: str,
1521+
def __init__(self, master: tk.Toplevel, parent, fileExts: tuple, title: Optional[str], progressTitle: str,
15171522
func: ImportDialogFunc, ui_file='ui/importdialog.ui'):
15181523
self.master = master
15191524
self.parent = parent
@@ -1693,6 +1698,14 @@ async def convert(self, filepath: str, dialog: ProgressDialog) -> NbsSong:
16931698
self.importPanning.get(), self.importPitch.get(),
16941699
dialog)
16951700

1701+
def isRequiredInteger(self, value: str, widget_name: str) -> bool:
1702+
if not value.isdigit():
1703+
return False
1704+
widget = self.master.nametowidget(widget_name)
1705+
if widget:
1706+
return widget['from'] <= int(value) <= widget['to']
1707+
return True
1708+
16961709
def autoExpandChanged(self):
16971710
self.builder.get_object('expandScale')[
16981711
'state'] = 'disabled' if self.autoExpand.get() else 'normal'

ui/midiimportdialog.ui

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<interface version="1.4" author="PygubuDesigner 0.39.3">
2+
<interface version="1.4" author="PygubuDesigner 0.40.2">
33
<project>
44
<settings />
55
<customwidgets />
@@ -158,6 +158,8 @@
158158
<property name="text" translatable="yes">1</property>
159159
<property name="textvariable">int:durationSpacing</property>
160160
<property name="to">10</property>
161+
<property name="validate">key</property>
162+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isRequiredInteger</property>
161163
<property name="width">5</property>
162164
<layout manager="grid">
163165
<property name="column">1</property>
@@ -217,6 +219,8 @@
217219
<property name="text" translatable="yes">50</property>
218220
<property name="textvariable">int:trailingVelocity</property>
219221
<property name="to">100</property>
222+
<property name="validate">key</property>
223+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isRequiredInteger</property>
220224
<property name="width">5</property>
221225
<layout manager="grid">
222226
<property name="column">1</property>

ui/toplevel.ui

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<property name="usemousewheel">false</property>
5656
<layout manager="pack">
5757
<property name="expand">true</property>
58+
<property name="fill">both</property>
5859
<property name="side">top</property>
5960
</layout>
6061
<child>
@@ -304,7 +305,7 @@
304305
<property name="textvariable">string:headerAutosaveInterval</property>
305306
<property name="to">255</property>
306307
<property name="validate">key</property>
307-
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P">isInteger</property>
308+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isInteger</property>
308309
<property name="width">4</property>
309310
<layout manager="grid">
310311
<property name="column">1</property>
@@ -391,7 +392,7 @@
391392
<property name="textvariable">string:headerMinuteSpent</property>
392393
<property name="to">2147483647</property>
393394
<property name="validate">key</property>
394-
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P">isInteger</property>
395+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isInteger</property>
395396
<property name="width">11</property>
396397
<layout manager="grid">
397398
<property name="column">1</property>
@@ -409,7 +410,7 @@
409410
<property name="textvariable">string:headerLeftClicks</property>
410411
<property name="to">2147483647</property>
411412
<property name="validate">key</property>
412-
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P">isInteger</property>
413+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isInteger</property>
413414
<property name="width">11</property>
414415
<layout manager="grid">
415416
<property name="column">1</property>
@@ -425,7 +426,7 @@
425426
<property name="textvariable">string:headerRightClicks</property>
426427
<property name="to">2147483647</property>
427428
<property name="validate">key</property>
428-
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P">isInteger</property>
429+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isInteger</property>
429430
<property name="width">11</property>
430431
<layout manager="grid">
431432
<property name="column">1</property>
@@ -441,7 +442,7 @@
441442
<property name="textvariable">string:headerBlockAdded</property>
442443
<property name="to">2147483647</property>
443444
<property name="validate">key</property>
444-
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P">isInteger</property>
445+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isInteger</property>
445446
<property name="width">11</property>
446447
<layout manager="grid">
447448
<property name="column">1</property>
@@ -457,7 +458,7 @@
457458
<property name="textvariable">string:headerBlockRemoved</property>
458459
<property name="to">2147483647</property>
459460
<property name="validate">key</property>
460-
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P">isInteger</property>
461+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isInteger</property>
461462
<property name="width">11</property>
462463
<layout manager="grid">
463464
<property name="column">1</property>
@@ -524,7 +525,7 @@
524525
<property name="textvariable">string:headerLoopCount</property>
525526
<property name="to">255</property>
526527
<property name="validate">key</property>
527-
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P">isInteger</property>
528+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isInteger</property>
528529
<property name="width">4</property>
529530
<layout manager="grid">
530531
<property name="column">1</property>
@@ -541,7 +542,7 @@
541542
<property name="to">32767
542543
</property>
543544
<property name="validate">key</property>
544-
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P">isInteger</property>
545+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isInteger</property>
545546
<property name="width">6</property>
546547
<layout manager="grid">
547548
<property name="column">1</property>
@@ -798,7 +799,7 @@
798799
<property name="title" translatable="yes">Select an image file</property>
799800
<layout manager="grid">
800801
<property name="column">1</property>
801-
<property name="ipadx">100</property>
802+
<property name="ipadx">200</property>
802803
<property name="padx">5</property>
803804
<property name="row">0</property>
804805
<property name="sticky">w</property>
@@ -886,7 +887,7 @@
886887
<property name="textvariable">int:imgInsertPosTickVar</property>
887888
<property name="to">65536</property>
888889
<property name="validate">key</property>
889-
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P">isRequiredInteger</property>
890+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isRequiredInteger</property>
890891
<property name="width">6</property>
891892
<layout manager="pack">
892893
<property name="side">left</property>
@@ -909,7 +910,7 @@
909910
<property name="textvariable">int:imgInsertPosLayerVar</property>
910911
<property name="to">65536</property>
911912
<property name="validate">key</property>
912-
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P">isRequiredInteger</property>
913+
<property name="validatecommand" type="command" cbtype="entry_validate" args="%P %W">isRequiredInteger</property>
913914
<property name="width">6</property>
914915
<layout manager="pack">
915916
<property name="side">left</property>
@@ -942,12 +943,11 @@
942943
<child>
943944
<object class="customwidgets.WrapMessage" id="wrapmessage2">
944945
<property name="text" translatable="yes">Insert an image as slient notes to replicate pixel-art effect, especially for setting thumbnail when uploading songs to Note Block World. The image may be scaled down, but never be scaled up.</property>
945-
<property name="width">550</property>
946946
<layout manager="grid">
947947
<property name="column">0</property>
948948
<property name="columnspan">2</property>
949949
<property name="row">5</property>
950-
<property name="sticky">w</property>
950+
<property name="sticky">nsew</property>
951951
</layout>
952952
</object>
953953
</child>

0 commit comments

Comments
 (0)