diff --git a/KiBOM/columns.py b/KiBOM/columns.py index 782f3d04..f9908fad 100644 --- a/KiBOM/columns.py +++ b/KiBOM/columns.py @@ -1,5 +1,9 @@ -class Columns: +class Column: + + COL_TYPE_KICAD = "KiCAD" #Immutable columns defined within KiCAD + COL_TYPE_FIELD = "Field" #Fields defined within KiCAD, by the user + COL_TYPE_EXT = "External" #Fields defined in external BoM, not stored within KiCAD #default columns (immutable) COL_REFERENCE = 'Reference' @@ -14,61 +18,82 @@ class Columns: #default columns for groups COL_GRP_QUANTITY = 'Quantity' + def __init__(self, title, vis=True, colType=COL_TYPE_FIELD): + self.title = str(title) + self.visible = vis + + #column comparison + def __eq__(self, other): + #match based on string + if type(other) == str: + return other.lower() == self.title.lower() + #match based on Column + if type(other) == Column: + return other.title.lower() == self.title.lower() + #no match + return False + + def __str__(self): + return self.title + + def __repr__(self): + return "'" + self.__str__() + "'" + +class ColumnList: + #all available columns _COLUMNS_ALL = [ - COL_DESCRIPTION, - COL_PART, - COL_PART_LIB, - COL_REFERENCE, - COL_VALUE, - COL_FP, - COL_FP_LIB, - COL_DATASHEET + Column.COL_DESCRIPTION, + Column.COL_PART, + Column.COL_PART_LIB, + Column.COL_REFERENCE, + Column.COL_VALUE, + Column.COL_FP, + Column.COL_FP_LIB, + Column.COL_DATASHEET ] #default columns #these columns are 'immutable' _COLUMNS_DEFAULT = [ - COL_DESCRIPTION, - COL_PART, - COL_REFERENCE, - COL_VALUE, - COL_FP + Column.COL_DESCRIPTION, + Column.COL_PART, + Column.COL_REFERENCE, + Column.COL_VALUE, + Column.COL_FP ] - + + #Columns that only exist for the 'grouped' components _COLUMNS_GROUPED = [ - COL_GRP_QUANTITY, + Column.COL_GRP_QUANTITY, ] - + def __str__(self): - return " ".join(self.columns) + return " ".join(map(str,self.columns)) def __repr__(self): return self.__str__() def __init__(self, cols=_COLUMNS_DEFAULT): + self.columns = [] + #make a copy of the supplied columns - self.columns = [col for col in cols] + for col in cols: + self.AddColumn(col) self._checkDefaultColumns() - def _hasColumn(self, title): - - title = title.lower() - - for c in self.columns: - if c.lower() == title: - return True - - return False + def _hasColumn(self, col): + #col can either be or + return col in self.columns def _checkDefaultColumns(self): #prepend any default columns that don't exist for c in self._COLUMNS_DEFAULT[::-1]: if c not in self.columns: - self.columns = [c] + self.columns + self.columns = [Column(c)] + self.columns """ Remove a column from the list. Specify either the heading or the index @@ -76,48 +101,54 @@ class Columns: def RemoveColumn(self, col): if type(col) is str: self.RemoveColumnByName(col) + if type(col) is Column: + self.RemoveColumnByName(col.title) elif type(col) is int and col >= 0 and col < len(self.columns): self.RemoveColumnByName(self.columns[col]) def RemoveColumnByName(self, name): - name = name.lower() - #first check if this is in an immutable colum - if name in [c.lower() for c in self._COLUMNS_DEFAULT]: + if name in self._COLUMNS_DEFAULT: return - #Obtain a list of all columns for comparison - lCols = [c.lower() for c in self.columns] - #column does not exist, return - if name not in lCols: + if name not in self.columns: return try: - index = lCols.index(name) + index = self.columns.index(name) del self.columns[index] except ValueError: return #add a new column (if it doesn't already exist!) - def AddColumn(self, title, index=None): + def AddColumn(self, col, index=None): - if type(title) is not str: + if type(col) == Column: + pass + elif type(col) == str: + col = Column(col) + else: return - if self._hasColumn(title): + #Already exists? + if self._hasColumn(col): return if type(index) is not int or index < 0 or index >= len(self.columns): #append - self.columns.append(title) + self.columns.append(col) #otherwise, splice the new column in else: - self.columns = self.columns[0:index] + [title] + self.columns[index:] + self.columns = self.columns[0:index] + [col] + self.columns[index:] + + def VisibleColumns(self): + return [col for col in self.columns if col.visible] + if __name__ == '__main__': - c = Columns() + c = ColumnList() c.AddColumn("Test1") c.AddColumn("Test1") diff --git a/KiBOM/component.py b/KiBOM/component.py index 827f252a..782c5b62 100644 --- a/KiBOM/component.py +++ b/KiBOM/component.py @@ -1,4 +1,4 @@ -from columns import Columns +from columns import ColumnList import units @@ -160,8 +160,8 @@ class ComponentGroup(): """ def __init__(self): self.components = [] - self.fields = dict.fromkeys(Columns._COLUMNS_GROUPED) #columns loaded from KiCAD - self.csvFields = dict.fromkeys(Columns._COLUMNS_GROUPED) #columns loaded from .csv file + self.fields = dict.fromkeys(ColumnList._COLUMNS_GROUPED) #columns loaded from KiCAD + self.csvFields = dict.fromkeys(ColumnList._COLUMNS_GROUPED) #columns loaded from .csv file def getField(self, field): if not field in self.fields.keys(): return "" diff --git a/KiBOM_GUI.py b/KiBOM_GUI.py index 868ff0e4..dc514d7e 100644 --- a/KiBOM_GUI.py +++ b/KiBOM_GUI.py @@ -10,7 +10,7 @@ def Debug(*arg): sys.path.append(os.path.dirname(sys.argv[0])) -from KiBOM.columns import Columns +from KiBOM.columns import ColumnList #import bomfunk_netlist_reader @@ -18,13 +18,13 @@ class KiBOMColumnList(wx.CheckListBox): def __init__(self, parent): wx.CheckListBox.__init__(self, parent) - self.InitColumns(Columns._COLUMNS_DEFAULT) - - def InitColumns(self, cols): + def SetColumns(self, columnList): self.Clear() - self.AppendItems(cols) + for i,col in enumerate(columnList.columns): + self.Append(col.title, None) + self.Check(i,check=col.visible) class KiBOMTable(wx.grid.Grid): @@ -44,7 +44,9 @@ class KiBOMTable(wx.grid.Grid): """ Perform a complete refresh of the columns """ - def SetColumns(self, columns): + def SetColumns(self, columnList): + + columns = columnList.VisibleColumns() #add in any required rows if self.GetNumberCols() < len(columns): @@ -54,21 +56,21 @@ class KiBOMTable(wx.grid.Grid): if self.GetNumberCols() > len(columns): self.DeleteCols(self.GetNumberCols() - len(columns)) - for i,h in enumerate(columns): - self.SetColLabelValue(i,h) + for i,col in enumerate(columns): + self.SetColLabelValue(i,col.title) class KiBOMFrame(wx.Frame): def __init__(self, parent, title): wx.Frame.__init__(self, parent,title=title) - self.columns = Columns() + self.columns = ColumnList() self.panel = wx.Panel(self) self.table = KiBOMTable(self.panel) - self.table.SetColumns(self.columns.columns) + self.table.SetColumns(self.columns) #Vertical sizer that separates the "export options" (lower) from the main table and selectors self.hSizer = wx.BoxSizer(wx.HORIZONTAL) @@ -94,6 +96,8 @@ class KiBOMFrame(wx.Frame): self.colList = KiBOMColumnList(self.panel) self.colListSizer.Add(self.colList) + self.colList.SetColumns(self.columns) + #buttons to move/add/delete columns self.colButtons = wx.BoxSizer(wx.HORIZONTAL)