objdictgen/commondialogs.py
changeset 327 b904d9a99e28
parent 314 68e83c3ffbb5
child 328 474aa35daa95
equal deleted inserted replaced
326:98a83c4194b5 327:b904d9a99e28
    22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 
    23 
    24 import wx
    24 import wx
    25 
    25 
    26 import os
    26 import os
       
    27 
       
    28 from node import BE_to_LE, LE_to_BE
    27 
    29 
    28 ScriptDirectory = os.path.split(__file__)[0]
    30 ScriptDirectory = os.path.split(__file__)[0]
    29 
    31 
    30 #-------------------------------------------------------------------------------
    32 #-------------------------------------------------------------------------------
    31 #                          Editing Communication Dialog
    33 #                          Editing Communication Dialog
  1255             values["slaveNodeID"] = int(nodeid, 16)
  1257             values["slaveNodeID"] = int(nodeid, 16)
  1256         else:
  1258         else:
  1257             values["slaveNodeID"] = int(nodeid)
  1259             values["slaveNodeID"] = int(nodeid)
  1258         values["edsFile"] = self.EDSFile.GetStringSelection()
  1260         values["edsFile"] = self.EDSFile.GetStringSelection()
  1259         return values
  1261         return values
       
  1262 
       
  1263 #-------------------------------------------------------------------------------
       
  1264 #                            Editing DCF Entry Dialog
       
  1265 #-------------------------------------------------------------------------------
       
  1266 
       
  1267 class DCFEntryValuesTable(wx.grid.PyGridTableBase):
       
  1268     
       
  1269     """
       
  1270     A custom wxGrid Table using user supplied data
       
  1271     """
       
  1272     def __init__(self, parent, data, colnames):
       
  1273         # The base class must be initialized *first*
       
  1274         wx.grid.PyGridTableBase.__init__(self)
       
  1275         self.data = data
       
  1276         self.colnames = colnames
       
  1277         self.Parent = parent
       
  1278         # XXX
       
  1279         # we need to store the row length and collength to
       
  1280         # see if the table has changed size
       
  1281         self._rows = self.GetNumberRows()
       
  1282         self._cols = self.GetNumberCols()
       
  1283     
       
  1284     def GetNumberCols(self):
       
  1285         return len(self.colnames)
       
  1286         
       
  1287     def GetNumberRows(self):
       
  1288         return len(self.data)
       
  1289 
       
  1290     def GetColLabelValue(self, col):
       
  1291         if col < len(self.colnames):
       
  1292             return self.colnames[col]
       
  1293 
       
  1294     def GetRowLabelValues(self, row):
       
  1295         return row
       
  1296 
       
  1297     def GetValue(self, row, col):
       
  1298         if row < self.GetNumberRows():
       
  1299             return str(self.data[row].get(self.GetColLabelValue(col), ""))
       
  1300             
       
  1301     def GetEditor(self, row, col):
       
  1302         if row < self.GetNumberRows():
       
  1303             return self.editors[row].get(self.GetColLabelValue(col), "")
       
  1304     
       
  1305     def GetValueByName(self, row, colname):
       
  1306         return self.data[row].get(colname)
       
  1307 
       
  1308     def SetValue(self, row, col, value):
       
  1309         if col < len(self.colnames):
       
  1310             self.data[row][self.GetColLabelValue(col)] = value
       
  1311         
       
  1312     def ResetView(self, grid):
       
  1313         """
       
  1314         (wx.grid.Grid) -> Reset the grid view.   Call this to
       
  1315         update the grid if rows and columns have been added or deleted
       
  1316         """
       
  1317         grid.BeginBatch()
       
  1318         for current, new, delmsg, addmsg in [
       
  1319             (self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
       
  1320             (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED),
       
  1321         ]:
       
  1322             if new < current:
       
  1323                 msg = wx.grid.GridTableMessage(self,delmsg,new,current-new)
       
  1324                 grid.ProcessTableMessage(msg)
       
  1325             elif new > current:
       
  1326                 msg = wx.grid.GridTableMessage(self,addmsg,new-current)
       
  1327                 grid.ProcessTableMessage(msg)
       
  1328                 self.UpdateValues(grid)
       
  1329         grid.EndBatch()
       
  1330 
       
  1331         self._rows = self.GetNumberRows()
       
  1332         self._cols = self.GetNumberCols()
       
  1333         # update the column rendering scheme
       
  1334         self._updateColAttrs(grid)
       
  1335 
       
  1336         # update the scrollbars and the displayed part of the grid
       
  1337         grid.AdjustScrollbars()
       
  1338         grid.ForceRefresh()
       
  1339 
       
  1340 
       
  1341     def UpdateValues(self, grid):
       
  1342         """Update all displayed values"""
       
  1343         # This sends an event to the grid table to update all of the values
       
  1344         msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
       
  1345         grid.ProcessTableMessage(msg)
       
  1346 
       
  1347     def _updateColAttrs(self, grid):
       
  1348         """
       
  1349         wx.grid.Grid -> update the column attributes to add the
       
  1350         appropriate renderer given the column name.
       
  1351 
       
  1352         Otherwise default to the default renderer.
       
  1353         """
       
  1354         
       
  1355         for row in range(self.GetNumberRows()):
       
  1356             for col in range(self.GetNumberCols()):
       
  1357                 editor = wx.grid.GridCellTextEditor()
       
  1358                 renderer = wx.grid.GridCellStringRenderer()
       
  1359                     
       
  1360                 grid.SetCellEditor(row, col, editor)
       
  1361                 grid.SetCellRenderer(row, col, renderer)
       
  1362                 
       
  1363                 grid.SetCellBackgroundColour(row, col, wx.WHITE)
       
  1364     
       
  1365     def SetData(self, data):
       
  1366         self.data = data
       
  1367     
       
  1368     def AppendRow(self, row_content):
       
  1369         self.data.append(row_content)
       
  1370 
       
  1371     def Empty(self):
       
  1372         self.data = []
       
  1373         self.editors = []
       
  1374 
       
  1375 [ID_DCFENTRYVALUESDIALOG, ID_DCFENTRYVALUESDIALOGVALUESGRID, 
       
  1376  ID_DCFENTRYVALUESDIALOGADDBUTTON, ID_DCFENTRYVALUESDIALOGDELETEBUTTON, 
       
  1377  ID_DCFENTRYVALUESDIALOGUPBUTTON, ID_DCFENTRYVALUESDIALOGDOWNBUTTON, 
       
  1378  ID_VARIABLEEDITORPANELSTATICTEXT1, 
       
  1379 ] = [wx.NewId() for _init_ctrls in range(7)]
       
  1380 
       
  1381 class DCFEntryValuesDialog(wx.Dialog):
       
  1382     
       
  1383     if wx.VERSION < (2, 6, 0):
       
  1384         def Bind(self, event, function, id = None):
       
  1385             if id is not None:
       
  1386                 event(self, id, function)
       
  1387             else:
       
  1388                 event(self, function)
       
  1389     
       
  1390     def _init_coll_MainSizer_Items(self, parent):
       
  1391         parent.AddWindow(self.staticText1, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
  1392         parent.AddWindow(self.ValuesGrid, 0, border=20, flag=wx.GROW|wx.TOP|wx.LEFT|wx.RIGHT)
       
  1393         parent.AddSizer(self.ButtonPanelSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.LEFT|wx.RIGHT)
       
  1394         parent.AddSizer(self.ButtonSizer, 0, border=20, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT)
       
  1395     
       
  1396     def _init_coll_MainSizer_Growables(self, parent):
       
  1397         parent.AddGrowableCol(0)
       
  1398         parent.AddGrowableRow(1)
       
  1399     
       
  1400     def _init_coll_ButtonPanelSizer_Items(self, parent):
       
  1401         parent.AddWindow(self.UpButton, 0, border=5, flag=wx.ALL)
       
  1402         parent.AddWindow(self.AddButton, 0, border=5, flag=wx.ALL)
       
  1403         parent.AddWindow(self.DownButton, 0, border=5, flag=wx.ALL)
       
  1404         parent.AddWindow(self.DeleteButton, 0, border=5, flag=wx.ALL)
       
  1405         
       
  1406     def _init_sizers(self):
       
  1407         self.MainSizer = wx.FlexGridSizer(cols=1, hgap=0, rows=3, vgap=0)
       
  1408         self.ButtonPanelSizer = wx.BoxSizer(wx.HORIZONTAL)
       
  1409         
       
  1410         self._init_coll_MainSizer_Items(self.MainSizer)
       
  1411         self._init_coll_MainSizer_Growables(self.MainSizer)
       
  1412         self._init_coll_ButtonPanelSizer_Items(self.ButtonPanelSizer)
       
  1413         
       
  1414         self.SetSizer(self.MainSizer)
       
  1415 
       
  1416     def _init_ctrls(self, prnt):
       
  1417         wx.Dialog.__init__(self, id=ID_DCFENTRYVALUESDIALOG,
       
  1418               name='DCFEntryValuesDialog', parent=prnt, pos=wx.Point(376, 223),
       
  1419               size=wx.Size(400, 300), style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER,
       
  1420               title='Edit DCF Entry Values')
       
  1421         self.SetClientSize(wx.Size(400, 300))
       
  1422 
       
  1423         self.staticText1 = wx.StaticText(id=ID_VARIABLEEDITORPANELSTATICTEXT1,
       
  1424               label='Entry Values:', name='staticText1', parent=self,
       
  1425               pos=wx.Point(0, 0), size=wx.Size(95, 17), style=0)
       
  1426 
       
  1427         self.ValuesGrid = wx.grid.Grid(id=ID_DCFENTRYVALUESDIALOGVALUESGRID,
       
  1428               name='ValuesGrid', parent=self, pos=wx.Point(0, 0), 
       
  1429               size=wx.Size(0, 150), style=wx.VSCROLL)
       
  1430         self.ValuesGrid.SetFont(wx.Font(12, 77, wx.NORMAL, wx.NORMAL, False,
       
  1431               'Sans'))
       
  1432         self.ValuesGrid.SetLabelFont(wx.Font(10, 77, wx.NORMAL, wx.NORMAL,
       
  1433               False, 'Sans'))
       
  1434         self.ValuesGrid.SetRowLabelSize(0)
       
  1435         self.ValuesGrid.SetSelectionBackground(wx.WHITE)
       
  1436         self.ValuesGrid.SetSelectionForeground(wx.BLACK)
       
  1437         if wx.VERSION >= (2, 6, 0):
       
  1438             self.ValuesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnValuesGridCellChange)
       
  1439         else:
       
  1440             wx.grid.EVT_GRID_CELL_CHANGE(self.ValuesGrid, self.OnValuesGridCellChange)
       
  1441         
       
  1442         self.AddButton = wx.Button(id=ID_DCFENTRYVALUESDIALOGADDBUTTON, label='Add',
       
  1443               name='AddButton', parent=self, pos=wx.Point(0, 0),
       
  1444               size=wx.Size(72, 32), style=0)
       
  1445         self.Bind(wx.EVT_BUTTON, self.OnAddButton, id=ID_DCFENTRYVALUESDIALOGADDBUTTON)
       
  1446 
       
  1447         self.DeleteButton = wx.Button(id=ID_DCFENTRYVALUESDIALOGDELETEBUTTON, label='Delete',
       
  1448               name='DeleteButton', parent=self, pos=wx.Point(0, 0),
       
  1449               size=wx.Size(72, 32), style=0)
       
  1450         self.Bind(wx.EVT_BUTTON, self.OnDeleteButton, id=ID_DCFENTRYVALUESDIALOGDELETEBUTTON)
       
  1451 
       
  1452         self.UpButton = wx.Button(id=ID_DCFENTRYVALUESDIALOGUPBUTTON, label='^',
       
  1453               name='UpButton', parent=self, pos=wx.Point(0, 0),
       
  1454               size=wx.Size(32, 32), style=0)
       
  1455         self.Bind(wx.EVT_BUTTON, self.OnUpButton, id=ID_DCFENTRYVALUESDIALOGUPBUTTON)
       
  1456 
       
  1457         self.DownButton = wx.Button(id=ID_DCFENTRYVALUESDIALOGDOWNBUTTON, label='v',
       
  1458               name='DownButton', parent=self, pos=wx.Point(0, 0),
       
  1459               size=wx.Size(32, 32), style=0)
       
  1460         self.Bind(wx.EVT_BUTTON, self.OnDownButton, id=ID_DCFENTRYVALUESDIALOGDOWNBUTTON)
       
  1461 
       
  1462         self.ButtonSizer = self.CreateButtonSizer(wx.OK|wx.CANCEL|wx.CENTRE)
       
  1463         
       
  1464         self._init_sizers()
       
  1465 
       
  1466     def __init__(self, parent):
       
  1467         self._init_ctrls(parent)
       
  1468         
       
  1469         self.Values = []
       
  1470         self.DefaultValue = {"Index" : 0, "Subindex" : 0, "Size" : 1, "Value" : 0}
       
  1471         
       
  1472         self.Table = DCFEntryValuesTable(self, [], ["Index", "Subindex", "Size", "Value"])
       
  1473         self.ValuesGrid.SetTable(self.Table)
       
  1474         
       
  1475     def OnValuesGridCellChange(self, event):
       
  1476         row, col = event.GetRow(), event.GetCol()
       
  1477         colname = self.Table.GetColLabelValue(col)
       
  1478         value = self.Table.GetValue(row, col)
       
  1479         try:
       
  1480             self.Values[row][colname] = int(value, 16)
       
  1481         except:
       
  1482             message = wx.MessageDialog(self, "\"%s\" is not a valid value!"%value, "Error", wx.OK|wx.ICON_ERROR)
       
  1483             message.ShowModal()
       
  1484             message.Destroy()
       
  1485         finally:
       
  1486             wx.CallAfter(self.RefreshValues)
       
  1487             event.Skip()
       
  1488     
       
  1489     def OnAddButton(self, event):
       
  1490         new_row = self.DefaultValue.copy()
       
  1491         self.Values.append(new_row)
       
  1492         self.RefreshValues()
       
  1493         event.Skip()
       
  1494 
       
  1495     def OnDeleteButton(self, event):
       
  1496         row = self.Table.GetRow(self.ValuesGrid.GetGridCursorRow())
       
  1497         self.Values.remove(row)
       
  1498         self.RefreshValues()
       
  1499         event.Skip()
       
  1500 
       
  1501     def OnUpButton(self, event):
       
  1502         self.MoveValue(self.ValuesGrid.GetGridCursorRow(), -1)
       
  1503         event.Skip()
       
  1504 
       
  1505     def OnDownButton(self, event):
       
  1506         self.MoveValue(self.ValuesGrid.GetGridCursorRow(), 1)
       
  1507         event.Skip()
       
  1508 
       
  1509     def MoveValue(self, value_index, move):
       
  1510         new_index = max(0, min(value_index + move, len(self.Values) - 1))
       
  1511         if new_index != value_index:
       
  1512             self.Values.insert(new_index, self.Values.pop(value_index))
       
  1513             self.RefreshValues()
       
  1514             self.ValuesGrid.SetGridCursor(new_index, self.ValuesGrid.GetGridCursorCol())
       
  1515         else:
       
  1516             self.RefreshValues()
       
  1517 
       
  1518     def SetValues(self, values):
       
  1519         self.Values = []
       
  1520         if values != "":
       
  1521             data = values[4:]
       
  1522             current = 0
       
  1523             for i in xrange(BE_to_LE(values[:4])):
       
  1524                 value = {}
       
  1525                 value["Index"] = BE_to_LE(data[current:current+2])
       
  1526                 value["Subindex"] = BE_to_LE(data[current+2:current+3])
       
  1527                 size = BE_to_LE(data[current+3:current+7])
       
  1528                 value["Size"] = size
       
  1529                 value["Value"] = BE_to_LE(data[current+7:current+7+size])
       
  1530                 current += 7 + size
       
  1531                 self.Values.append(value)
       
  1532         self.RefreshValues()
       
  1533     
       
  1534     def GetValues(self):
       
  1535         value = LE_to_BE(len(self.Values), 4)
       
  1536         for row in self.Values:
       
  1537             value += LE_to_BE(row["Index"], 2)
       
  1538             value += LE_to_BE(row["Subindex"], 1)
       
  1539             value += LE_to_BE(row["Size"], 4)
       
  1540             value += LE_to_BE(row["Value"], row["Size"])
       
  1541         return value
       
  1542     
       
  1543     def RefreshValues(self):
       
  1544         if len(self.Table.data) > 0:
       
  1545             self.ValuesGrid.SetGridCursor(0, 1)
       
  1546         data = []
       
  1547         for value in self.Values:
       
  1548             row = {}
       
  1549             row["Index"] = "%04X"%value["Index"]
       
  1550             row["Subindex"] = "%02X"%value["Subindex"]
       
  1551             row["Size"] = "%08X"%value["Size"]
       
  1552             row["Value"] = ("%0"+"%d"%(value["Size"] * 2)+"X")%value["Value"]
       
  1553             data.append(row)
       
  1554         self.Table.SetData(data)
       
  1555         self.Table.ResetView(self.ValuesGrid)