Skip to content

Commit 99fec3e

Browse files
committed
v1.0.3 - add column sorting (Qty and PROD_ID only)
1 parent d3b27cf commit 99fec3e

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

SparkFunKiCadBOMGenerator/dialog/dialog.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def __init__(self, parent):
4343
self.SetTitle(_APP_NAME + " - " + _APP_VERSION)
4444

4545
self.Bind(wx.EVT_SIZE, self.OnSize)
46+
47+
self.bomSortCol = -1
48+
self.bomSortAscending = True
49+
self.nonBomSortAscending = False
4650

4751
def OnSize(self, event):
4852
size = self.GetClientSize()
@@ -76,4 +80,79 @@ def OnSize(self, event):
7680
if event is not None:
7781
event.Skip()
7882

83+
def OnBOMGridLabelClicked(self, event):
84+
if event.Col <= 1:
85+
if self.bomSortCol == event.Col:
86+
self.bomSortAscending = not self.bomSortAscending
87+
else:
88+
self.bomSortCol = event.Col
89+
90+
rows = self.bomGrid.GetNumberRows() - 1 # Ignore the empty final row
91+
cols = self.bomGrid.GetNumberCols()
92+
93+
def compareVals(col, ascending, val1, val2):
94+
if col == 0 and ascending:
95+
if val1[0] == val2[0]: # If the Qty is equal, sort on ascending PROD_ID
96+
return (val1[1] < val2[1])
97+
return (int(val1[0]) < int(val2[0]))
98+
elif col == 0 and not ascending:
99+
if val1[0] == val2[0]: # If the Qty is equal, sort on ascending PROD_ID
100+
return (val1[1] < val2[1])
101+
return (int(val1[0]) > int(val2[0]))
102+
elif col == 1 and ascending:
103+
return (val1[1] < val2[1])
104+
elif col == 1 and not ascending:
105+
return (val1[1] > val2[1])
106+
return False
107+
108+
for row1 in range(rows):
109+
for row2 in range(rows):
110+
cells1 = []
111+
cells2 = []
112+
for col in range(cols):
113+
cells1.append(self.bomGrid.GetCellValue(row1, col))
114+
cells2.append(self.bomGrid.GetCellValue(row2, col))
115+
background1 = self.bomGrid.GetCellBackgroundColour(row1, 1)
116+
background2 = self.bomGrid.GetCellBackgroundColour(row2, 1)
117+
if compareVals(self.bomSortCol, self.bomSortAscending, cells1, cells2):
118+
for col in range(cols):
119+
self.bomGrid.SetCellValue(row1, col, cells2[col])
120+
self.bomGrid.SetCellValue(row2, col, cells1[col])
121+
self.bomGrid.SetCellBackgroundColour(row1, 1, background2)
122+
self.bomGrid.SetCellBackgroundColour(row2, 1, background1)
123+
124+
self.bomGrid.Refresh()
125+
126+
def OnNonBOMGridLabelClicked(self, event):
127+
if event.Col == 0:
128+
self.nonBomSortAscending = not self.nonBomSortAscending
129+
130+
rows = self.nonBomGrid.GetNumberRows() - 1 # Ignore the empty final row
131+
cols = self.nonBomGrid.GetNumberCols()
132+
133+
def compareVals(ascending, val1, val2):
134+
if ascending:
135+
return (int(val1[0]) < int(val2[0]))
136+
return (int(val1[0]) > int(val2[0]))
137+
138+
for row1 in range(rows):
139+
for row2 in range(rows):
140+
cells1 = []
141+
cells2 = []
142+
for col in range(cols):
143+
cells1.append(self.nonBomGrid.GetCellValue(row1, col))
144+
cells2.append(self.nonBomGrid.GetCellValue(row2, col))
145+
if compareVals(self.nonBomSortAscending, cells1, cells2):
146+
for col in range(cols):
147+
self.nonBomGrid.SetCellValue(row1, col, cells2[col])
148+
self.nonBomGrid.SetCellValue(row2, col, cells1[col])
149+
150+
self.nonBomGrid.Refresh()
151+
152+
def OnShowEvent(self, event):
153+
self.bomGrid.Refresh() # This may help the cell background color?
154+
155+
156+
157+
79158

SparkFunKiCadBOMGenerator/dialog/dialog_text_base.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,23 @@ def __init__( self, parent ):
169169

170170
self.Centre( wx.BOTH )
171171

172+
# Connect Events
173+
self.Bind( wx.EVT_SHOW, self.OnShowEvent )
174+
self.bomGrid.Bind( wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.OnBOMGridLabelClicked )
175+
self.nonBomGrid.Bind( wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.OnNonBOMGridLabelClicked )
176+
172177
def __del__( self ):
173178
pass
174179

175180

181+
# Virtual event handlers, override them in your derived class
182+
def OnShowEvent( self, event ):
183+
pass
184+
185+
def OnBOMGridLabelClicked( self, event ):
186+
pass
187+
188+
def OnNonBOMGridLabelClicked( self, event ):
189+
pass
190+
191+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0.2"
1+
__version__ = "1.0.3"

text_dialog.fbp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<property name="window_extra_style"></property>
5757
<property name="window_name"></property>
5858
<property name="window_style"></property>
59+
<event name="OnShow">OnShowEvent</event>
5960
<object class="wxFlexGridSizer" expanded="1">
6061
<property name="cols">1</property>
6162
<property name="flexible_direction">wxBOTH</property>
@@ -231,6 +232,7 @@
231232
<property name="window_extra_style"></property>
232233
<property name="window_name"></property>
233234
<property name="window_style"></property>
235+
<event name="OnGridLabelLeftClick">OnBOMGridLabelClicked</event>
234236
</object>
235237
</object>
236238
</object>
@@ -456,6 +458,7 @@
456458
<property name="window_extra_style"></property>
457459
<property name="window_name"></property>
458460
<property name="window_style"></property>
461+
<event name="OnGridLabelLeftClick">OnNonBOMGridLabelClicked</event>
459462
</object>
460463
</object>
461464
</object>

0 commit comments

Comments
 (0)