Skip to content

Commit 9ff4280

Browse files
authored
Merge pull request #3358 from tangentaudio/qtvcp_calc_fix
QtVcp calculator and operator value line improvements
2 parents dd710ba + dc72e7a commit 9ff4280

3 files changed

Lines changed: 50 additions & 45 deletions

File tree

docs/src/gui/qtvcp-widgets.adoc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,7 @@ It is based on PyQt's _QDialog_.
27382738
.QtVCP `CalculatorDialog`: Calculator Dialog Widget
27392739
image::images/qtvcp_calculator.png["QtVCP CalculatorDialog: Calculator Dialog Widget",scale="25%"]
27402740

2741-
This is a dialog to *display a calculator for numeric entry*, such as origin offset.
2741+
This is a dialog to *display a calculator for numeric entry*, such as origin offset, spindle RPM, etc.
27422742

27432743
It returns the entry via `STATUS` messages using a Python `DICT`.
27442744

@@ -2748,12 +2748,15 @@ When using ``STATUS``'s `request-dialog` function, the default launch name is *`
27482748

27492749
It is based on PyQt's _QDialog_.
27502750

2751-
==== INI file options for CALCULATOR
2751+
==== Preferences file options
27522752

2753-
In the `DISPLAY` section of the INI file the following options may be set:
2753+
In the `CALCULATOR` section of the preferences file the following options may be set:
27542754

2755-
* `CALCULATOR_CONST_VALUES` - a comma-delimited list of common values you might enter, that will appear on a dedicated row of buttons at the bottom of the calculator. e.g. setting to `0.100,-0.100` would provide two buttons for +0.100 and -0.100 which are commonly used when edge-finding on inch mills. Up to six (6) values may be entered, beyond that the list will be truncated. Values must be valid floating point or integer.
2756-
* `CALCULATOR_ON_SHOW` - optionally set to `CLEAR_ALL` to issue a "Clear All" each time the calculator is shown. This will clear any previously entered values from the last time the calculator was used and open with the display value set to `0`
2755+
* `constValuesList` - A comma-delimited list of common values you might enter, that will appear on a dedicated row of buttons at the bottom of the calculator. e.g. setting to `0.100, -0.100` would provide two buttons for +0.100 and -0.100 which are commonly used when edge-finding on inch mills. Up to six (6) values may be entered, beyond that the list will be truncated. Values must be valid floating point or integer.
2756+
* `onShowBehavior` - A list of optional behaviors that will be triggered when the calculator dialog is shown. Each option must be separated by a comma.
2757+
- `CLEAR_ALL` to issue a *Clear All* each time the calculator is shown. This will clear any previously entered values from the last time the calculator was used and open with the display value set to `0`
2758+
- `FORCE_FOCUS` will force the focus to the calculator input field when the widget is shown. This will allow a physical keyboard to provide input to the widget properly without additional clicks. Also, it has the side-effect of selecting the current value, such that typing from a physical keyboard will replace the existing value unless the text selection is changed.
2759+
* `acceptOnReturnKey` - If set to `True`, the calculator will accept the current value and close the dialog when the return key is pressed. If set to `False`, the return key will be ignored and the *Apply* button must be clicked.
27572760

27582761
[[sub:qtvcp:widgets:runfromlinedialog]]
27592762
=== `RunFromLine` - Run-From-Line Dialog Widget

lib/python/qtvcp/widgets/calculator.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class Calculator(QDialog):
2020
def __init__(self, parent=None):
2121
super(Calculator, self).__init__(parent)
2222

23+
try:
24+
self.PREFS_ = self.QTVCP_INSTANCE_.PREFS_
25+
self.PREF_SECTION = 'CALCULATOR'
26+
except:
27+
self.PREFS_ = None
28+
2329
self.pendingAdditiveOperator = ''
2430
self.pendingMultiplicativeOperator = ''
2531

@@ -76,7 +82,6 @@ def __init__(self, parent=None):
7682
mainLayout = QGridLayout()
7783
mainLayout.setSizeConstraint(QLayout.SetFixedSize)
7884

79-
mainLayout.addWidget(self.display, 0, 0, 1, 6)
8085
mainLayout.addWidget(self.backspaceButton, 1, 0, 1, 1)
8186
mainLayout.addWidget(self.axisButton, 1, 1, 1, 2)
8287
mainLayout.addWidget(self.clearButton, 1, 3, 1, 1)
@@ -108,17 +113,21 @@ def __init__(self, parent=None):
108113
mainLayout.addWidget(self.to_mm_btn, 6, 0)
109114
mainLayout.addWidget(self.to_inch_btn, 6, 1)
110115
mainLayout.addWidget(self.tpi_btn, 6, 2)
116+
117+
if self.PREFS_:
118+
constValues = self.PREFS_.getpref('constValuesList', None, str, self.PREF_SECTION)
119+
if constValues is not None:
120+
self.constButtons = []
121+
constValues = ''.join(constValues.split())
122+
for value in constValues.split(',')[:6]:
123+
constButton = QPushButton(value)
124+
constButton.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
125+
constButton.clicked.connect(self.constClicked)
126+
mainLayout.addWidget(constButton, len(self.constButtons) + 1, 6)
127+
self.constButtons.append(constButton)
128+
129+
mainLayout.addWidget(self.display, 0, 0, 1, mainLayout.columnCount())
111130

112-
self.constButtons = []
113-
constValues = INFO.get_error_safe_setting('DISPLAY', 'CALCULATOR_CONST_VALUES', None, warning=False)
114-
if constValues is not None:
115-
constValues = ''.join(constValues.split())
116-
for value in constValues.split(',')[:6]:
117-
constButton = QPushButton(value)
118-
constButton.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
119-
constButton.clicked.connect(self.constClicked)
120-
mainLayout.addWidget(constButton, 7, len(self.constButtons))
121-
self.constButtons.append(constButton)
122131

123132
self.backButton = QPushButton('Back')
124133
self.backButton.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
@@ -141,6 +150,10 @@ def __init__(self, parent=None):
141150
self.bBox.rejected.connect(self.reject)
142151
self.bBox.accepted.connect(self.accept)
143152

153+
if self.PREFS_:
154+
if self.PREFS_.getpref('acceptOnReturnKey', False, bool, self.PREF_SECTION):
155+
self.display.returnPressed.connect(self.accept)
156+
144157
calc_layout = QVBoxLayout()
145158
calc_layout.addLayout(mainLayout)
146159
calc_layout.addWidget(self.bBox)
@@ -157,12 +170,17 @@ def __init__(self, parent=None):
157170
STATUS.connect('all-homed', lambda w: self.axisButton.setEnabled(True))
158171
STATUS.connect('not-all-homed', lambda w, data: self.axisButton.setEnabled(False))
159172

160-
self.behaviorOnShow = INFO.get_error_safe_setting('DISPLAY', 'CALCULATOR_ON_SHOW', None, warning=None)
173+
if self.PREFS_:
174+
self.behaviorOnShow = self.PREFS_.getpref('onShowBehavior', None, str, self.PREF_SECTION)
175+
else:
176+
self.behaviorOnShow = None
161177

162178
def showEvent(self, event):
163179
if self.behaviorOnShow is not None:
164-
if self.behaviorOnShow.upper() == 'CLEAR_ALL':
180+
if 'CLEAR_ALL' in self.behaviorOnShow.upper():
165181
self.clearAll()
182+
if 'FORCE_FOCUS' in self.behaviorOnShow.upper():
183+
self.display.setFocus()
166184

167185
def digitClicked(self):
168186
clickedButton = self.sender()

lib/python/qtvcp/widgets/operator_value_line.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,6 @@ def __init__(self, parent=None):
5656
STATUS.connect('all-homed', lambda w: self.setEnabled(STATUS.machine_is_on()))
5757
STATUS.connect('general',self.return_value)
5858

59-
def getSpeedText(self):
60-
text = str(self.text()).strip()
61-
return text
62-
63-
64-
def keyPressEvent(self, event):
65-
super(OperatorValue, self).keyPressEvent(event)
66-
if event.key() == Qt.Key_Up:
67-
self.increase()
68-
if event.key() == Qt.Key_Down:
69-
self.decrease()
70-
71-
def increase(self):
72-
LOG.debug('increase')
73-
STATUS.emit('spindle-increase')
74-
75-
def decrease(self):
76-
LOG.debug('decrease')
77-
STATUS.emit('spindle-decrease')
78-
79-
8059
class OperatorValueLine(OperatorValue):
8160
def __init__(self, parent=None):
8261
super(OperatorValueLine, self).__init__(parent)
@@ -85,25 +64,21 @@ def __init__(self, parent=None):
8564
self.soft_keyboard = False
8665
self.dialog_keyboard = False
8766
self.issue_mdi_on_submit = False
67+
self.issue_mdi_on_return = False
8868
self.mdi_command_format = "M3 S{value}"
8969
self.pending_value = False
9070
self._input_panel_full = SoftInputWidget(self, 'default')
9171
self.installEventFilter(self)
9272

9373
self.returnPressed.connect(self.handleReturnKey)
9474

95-
9675
def handleReturnKey(self):
9776
self.setFocus()
9877
self.setCursorPosition(len(self.text())+1)
99-
if self.issue_mdi_on_submit:
78+
if self.issue_mdi_on_return:
10079
self.pending_value = False
10180
self._style_polish('isPendingValue', self.pending_value)
10281
self.submit(self.mdi_command_format)
103-
else:
104-
self.pending_value = True
105-
self._style_polish('isPendingValue', self.pending_value)
106-
10782

10883
def submit(self, mdi_format):
10984
value = str(self.text()).strip()
@@ -174,6 +149,15 @@ def reset_issue_mdi_on_submit(self):
174149

175150
issue_mdi_on_submit_option = pyqtProperty(bool, get_issue_mdi_on_submit, set_issue_mdi_on_submit, reset_issue_mdi_on_submit)
176151

152+
def set_issue_mdi_on_return(self, data):
153+
self.issue_mdi_on_return = data
154+
def get_issue_mdi_on_return(self):
155+
return self.issue_mdi_on_return
156+
def reset_issue_mdi_on_return(self):
157+
self.issue_mdi_on_return = False
158+
159+
issue_mdi_on_return_option = pyqtProperty(bool, get_issue_mdi_on_return, set_issue_mdi_on_return, reset_issue_mdi_on_return)
160+
177161
def set_mdi_command_format(self, data):
178162
self.mdi_command_format = data
179163
def get_mdi_command_format(self):

0 commit comments

Comments
 (0)