Skip to content

Commit 58ef5c2

Browse files
authored
Merge pull request #25 from vladimir-poleh/dynamic_mdi_commands_list
Allow to use more than 10 MDI Commands.
2 parents cf79f6e + e962b6c commit 58ef5c2

File tree

7 files changed

+151
-170
lines changed

7 files changed

+151
-170
lines changed

mesact/src/libmesact/buildini.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
from PyQt5.QtWidgets import QSpinBox
55

6+
from libmesact import mdi
7+
8+
69
def build(parent):
710
buildErrors = []
811
iniFilePath = os.path.join(parent.configPath, parent.configNameUnderscored + '.ini')
@@ -155,9 +158,10 @@ def build(parent):
155158
# build the [HALUI] section
156159
if parent.haluiCB.isChecked():
157160
iniContents.append('\n[HALUI]\n')
158-
for i in range(10):
159-
if getattr(parent, f"mdiCmdLE_{i}").text():
160-
iniContents.append(f'MDI_COMMAND = {getattr(parent, f"mdiCmdLE_{i}").text()}\n')
161+
for i in range(mdi.get_mdi_commands_count(parent)):
162+
cmd = mdi.get_mdi_command(parent, i)
163+
if cmd:
164+
iniContents.append(f'MDI_COMMAND = {cmd}\n')
161165

162166
# build the axes and joints
163167
axes = [] # use only one axis letter with multiple joint axis

mesact/src/libmesact/check.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from libmesact import mdi
2+
13

24
def checkit(parent):
35
parent.mainTW.setCurrentIndex(11)
@@ -230,13 +232,13 @@ def checkit(parent):
230232
# end of SS Cards Tab
231233

232234
# check the Options Tab for errors
233-
mdi = []
234-
for i in range(9):
235-
cmd = getattr(parent, f'mdiCmdLE_{i}').text()
235+
mdi_commands = []
236+
for i in range(mdi.get_mdi_commands_count(parent)):
237+
cmd = mdi.get_mdi_command(parent, i)
236238
if len(cmd) > 0:
237-
mdi.append(i)
238-
if len(mdi) > 0:
239-
if len(mdi) != max(mdi) + 1:
239+
mdi_commands.append(i)
240+
if len(mdi_commands) > 0:
241+
if len(mdi_commands) != max(mdi_commands) + 1:
240242
tabError = True
241243
configErrors.append(f'\tMDI commands must start at 0 and not skip any')
242244
if not parent.haluiCB.isChecked():

mesact/src/libmesact/mdi.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from PyQt5.QtWidgets import QGridLayout, QLabel, QLineEdit
2+
3+
4+
def get_mdi_commands_count(parent):
5+
# Workaround for QGridLayout.rowCount() that returns 1 for empty grid
6+
grid_layout: QGridLayout = parent.mdiCommandsListGL
7+
rows = 0
8+
grid_layout.rowCount()
9+
for i in range(grid_layout.count()):
10+
row, _, span, _ = grid_layout.getItemPosition(i)
11+
rows = max(1, row + span)
12+
return rows
13+
14+
15+
def add_mdi_command_row(parent):
16+
grid_layout: QGridLayout = parent.mdiCommandsListGL
17+
row = get_mdi_commands_count(parent)
18+
__add_mdi_command_row(grid_layout, row)
19+
20+
21+
def set_mdi_command(parent, command_number, value):
22+
row_count = get_mdi_commands_count(parent)
23+
i = row_count
24+
while i <= command_number:
25+
add_mdi_command_row(parent)
26+
i += 1
27+
text_box: QLineEdit = parent.findChild(QLineEdit, __get_mdi_command_control_name(command_number))
28+
text_box.setText(value)
29+
30+
31+
def get_mdi_command(parent, command_number):
32+
rows_count = get_mdi_commands_count(parent)
33+
if rows_count == 0 or command_number >= rows_count:
34+
return ""
35+
text_box: QLineEdit = parent.findChild(QLineEdit, __get_mdi_command_control_name(command_number))
36+
return text_box.text().strip()
37+
38+
39+
def cleanup_mdi_commands(parent):
40+
grid_layout: QGridLayout = parent.mdiCommandsListGL
41+
for i in reversed(range(grid_layout.count())):
42+
widget = grid_layout.itemAt(i).widget()
43+
grid_layout.removeWidget(widget)
44+
widget.setParent(None)
45+
for i in range(10):
46+
__add_mdi_command_row(grid_layout, i)
47+
48+
49+
def __get_mdi_command_control_name(command_number):
50+
return f'mdiCmdLE_{command_number}'
51+
52+
53+
def __add_mdi_command_row(grid_layout, row):
54+
label = QLabel(f'MDI Command {row}')
55+
grid_layout.addWidget(label, row, 0)
56+
text_box = QLineEdit()
57+
text_box.setObjectName(__get_mdi_command_control_name(row))
58+
grid_layout.addWidget(text_box, row, 1)

mesact/src/libmesact/openini.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from libmesact import utilities
88
from libmesact import dialogs
9+
from libmesact import mdi
10+
911

1012
class loadini:
1113
def __init__(self):
@@ -185,13 +187,14 @@ def loadini(self, parent, iniFile):
185187
start = self.sections['[HALUI]'][0]
186188
end = self.sections['[HALUI]'][1]
187189
mdicmd = []
190+
mdi.cleanup_mdi_commands(parent)
188191
for item in self.content[start:end]:
189192
if item != '\n' and item.startswith('MDI_COMMAND'):
190193
item = item.split('=')
191194
item = item[1].strip()
192195
mdicmd.append(item)
193196
for i, item in enumerate(mdicmd):
194-
getattr(parent, f'mdiCmdLE_{i}').setText(item)
197+
mdi.set_mdi_command(parent, i, item)
195198

196199
for section in self.sections.items():
197200
if section[0].startswith('[JOINT'):

mesact/src/libmesact/updateini.py

+14-26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
from PyQt5.QtWidgets import QSpinBox
55

6+
from libmesact import mdi
7+
8+
69
class updateini:
710
generated_hal_items = [
811
'main.hal',
@@ -316,35 +319,20 @@ def update(self, parent, iniFile):
316319
if '[HALUI]' in self.sections:
317320
index = self.sections['[HALUI]']
318321
if len(index) == 2:
319-
ini_mdi = []
320-
for i in range(index[0], index[1]):
322+
start = index[0]
323+
end = index[1]
324+
# remove all existing MDI commands
325+
for i in reversed(range(start, end)):
321326
if self.content[i].startswith('MDI_COMMAND'):
322-
ini_mdi.append(self.content[i].split('=')[1].strip())
327+
del self.content[i]
323328
tool_mdi = []
324-
for i in range(10):
325-
mdi_text = f'{getattr(parent, f"mdiCmdLE_{i}").text()}'
329+
for i in range(mdi.get_mdi_commands_count(parent)):
330+
mdi_text = mdi.get_mdi_command(parent, i)
326331
if mdi_text:
327-
tool_mdi.append(f'{getattr(parent, f"mdiCmdLE_{i}").text()}')
328-
329-
if len(ini_mdi) == len(tool_mdi):
330-
for i, j in enumerate(range(index[0] + 1, index[1])):
331-
if self.content[j].startswith('MDI_COMMAND'):
332-
self.content[j] = f'MDI_COMMAND = {getattr(parent, f"mdiCmdLE_{i}").text()}\n'
333-
elif len(ini_mdi) > len(tool_mdi):
334-
remove = len(ini_mdi) - len(tool_mdi)
335-
for i in reversed(range(index[0] + 1, index[1])):
336-
if self.content[i].startswith('MDI_COMMAND') and remove > 0:
337-
del self.content[i]
338-
remove -= 1
339-
self.get_sections() # update section start/end
340-
elif len(ini_mdi) < len(tool_mdi):
341-
add = len(tool_mdi) - len(ini_mdi)
342-
for i, j in enumerate(range(index[0] + 1, index[1] + add)):
343-
if self.content[j].startswith('MDI_COMMAND'): # replace it
344-
self.content[j] = f'MDI_COMMAND = {getattr(parent, f"mdiCmdLE_{i}").text()}\n'
345-
elif self.content[j].strip() == '': # insert it
346-
self.content.insert(j, f'MDI_COMMAND = {getattr(parent, f"mdiCmdLE_{i}").text()}\n')
347-
self.get_sections() # update section start/end
332+
tool_mdi.append(mdi_text)
333+
for i in reversed(range(len(tool_mdi))):
334+
self.content.insert(start + 1, f'MDI_COMMAND = {tool_mdi[i]}\n')
335+
self.get_sections() # update section start/end
348336

349337
############ Massive rework needed here
350338
# if the section exists and is in the tool update it

mesact/src/mesact

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ from libmesact import settings
4646
from libmesact import spindle
4747
from libmesact import shutdown
4848
from libmesact import updates
49+
from libmesact import mdi
4950

5051
# testing imports
5152
from libmesact import testing
@@ -226,6 +227,10 @@ class MainWindow(QMainWindow):
226227
#self.checkMesaflashCB.clicked.connect(partial(settings.update_value, self))
227228
#self.newUserCB.clicked.connect(partial(settings.update_value, self))
228229

230+
# Options Tab - MDI
231+
mdi.cleanup_mdi_commands(self)
232+
self.addMdiCommandPB.clicked.connect(partial(mdi.add_mdi_command_row, self))
233+
229234
def changed(self):
230235
#if isinstance(self.sender(), QComboBox):
231236
# if self.sender().currentData():

0 commit comments

Comments
 (0)