Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions linien/gui/ui/main_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ p, li { white-space: pre-wrap; }
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="sweep_center">
<widget class="CustomDoubleSpinBox" name="sweep_center">
<property name="minimumSize">
<size>
<width>65</width>
Expand Down Expand Up @@ -172,7 +172,7 @@ p, li { white-space: pre-wrap; }
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="sweep_amplitude">
<widget class="CustomDoubleSpinBox" name="sweep_amplitude">
<property name="minimumSize">
<size>
<width>65</width>
Expand Down Expand Up @@ -1054,8 +1054,8 @@ p, li { white-space: pre-wrap; }
<rect>
<x>0</x>
<y>0</y>
<width>338</width>
<height>510</height>
<width>98</width>
<height>28</height>
</rect>
</property>
<attribute name="label">
Expand All @@ -1072,8 +1072,8 @@ p, li { white-space: pre-wrap; }
<rect>
<x>0</x>
<y>0</y>
<width>338</width>
<height>510</height>
<width>98</width>
<height>28</height>
</rect>
</property>
<attribute name="label">
Expand All @@ -1090,8 +1090,8 @@ p, li { white-space: pre-wrap; }
<rect>
<x>0</x>
<y>0</y>
<width>338</width>
<height>510</height>
<width>332</width>
<height>427</height>
</rect>
</property>
<attribute name="label">
Expand All @@ -1108,8 +1108,8 @@ p, li { white-space: pre-wrap; }
<rect>
<x>0</x>
<y>0</y>
<width>338</width>
<height>510</height>
<width>98</width>
<height>28</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -1132,8 +1132,8 @@ p, li { white-space: pre-wrap; }
<rect>
<x>0</x>
<y>0</y>
<width>338</width>
<height>510</height>
<width>98</width>
<height>28</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -1224,6 +1224,11 @@ p, li { white-space: pre-wrap; }
<header location="global">sweep_control</header>
<container>1</container>
</customwidget>
<customwidget>
<class>CustomDoubleSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header location="global">spin_box.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
Expand Down
42 changes: 42 additions & 0 deletions linien/gui/ui/spin_box.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from PyQt5.QtWidgets import QDoubleSpinBox


class CustomDoubleSpinBox(QDoubleSpinBox):
"""
Custom spin box with improved keyboard controls with step size depending on the
cursor position.
"""

def textFromValue(self, value):
# show + sign for positive values
text = super().textFromValue(value)
if value >= 0:
text = "+" + text
return text

def stepBy(self, steps):
cursor_position = self.lineEdit().cursorPosition()
# number of characters before the decimal separator including +/- sign
n_chars_before_sep = len(str(abs(int(self.value())))) + 1
if cursor_position == 0:
# set the cursor right of the +/- sign
self.lineEdit().setCursorPosition(1)
cursor_position = self.lineEdit().cursorPosition()
single_step = 10 ** (n_chars_before_sep - cursor_position)
# Handle decimal separator. Step should be 0.1 if cursor is at `1.|23` or
# `1.2|3`.
if cursor_position >= n_chars_before_sep + 2:
single_step = 10 * single_step
# Change single step and perform the step
self.setSingleStep(single_step)
super().stepBy(steps)
# Undo selection of the whole text.
self.lineEdit().deselect()
# Handle cases where the number of characters before the decimal separator
# changes. Step size should remain the same.
new_n_chars_before_sep = len(str(abs(int(self.value())))) + 1
if new_n_chars_before_sep < n_chars_before_sep:
cursor_position -= 1
elif new_n_chars_before_sep > n_chars_before_sep:
cursor_position += 1
self.lineEdit().setCursorPosition(cursor_position)