]> git.phdru.name Git - m_librarian.git/blobdiff - m_librarian/wx/ListBooks.py
Feat(wx/books): Toggle all/autor/series checkboxes
[m_librarian.git] / m_librarian / wx / ListBooks.py
index 514301bcee967fe32bc3a5089077bd7223b71ea4..58f910d840afd359d5351cbc0dec9dc5e5dab600 100644 (file)
@@ -15,7 +15,7 @@ class BooksDataTable(wx.grid.GridTableBase):
         self.rows_count = rows_count
         self.column_names = column_names
         self.data = []
-        for row in range(rows_count):
+        for row in range(rows_count + 1):
             row_data = []
             self.data.append(row_data)
             for col in range(len(column_names)):
@@ -55,7 +55,23 @@ class BooksDataTable(wx.grid.GridTableBase):
     # default, doesn't necessarily have to be the same type used
     # natively by the editor/renderer if they know how to convert.
     def GetTypeName(self, row, col):
-        return wx.grid.GRID_VALUE_STRING
+        if col == 0:
+            return wx.grid.GRID_VALUE_BOOL
+        else:
+            return wx.grid.GRID_VALUE_STRING
+
+    # Called to determine how the data can be fetched and stored by the
+    # editor and renderer.  This allows you to enforce some type-safety
+    # in the grid.
+    def CanGetValueAs(self, row, col, typeName):
+        colType = self.GetTypeName(row, col)
+        if typeName == colType:
+            return True
+        else:
+            return False
+
+    def CanSetValueAs(self, row, col, typeName):
+        return self.CanGetValueAs(row, col, typeName)
 
 
 class ListBooksPanel(GridPanel):
@@ -63,25 +79,39 @@ class ListBooksPanel(GridPanel):
     def InitGrid(self):
         books_by_author = self.param['books_by_author']
         columns = self.param['columns']
+        columns.insert(0, u'Выбрать')
         total_rows = 0
         for author in books_by_author:
             books = books_by_author[author]
             series = {book.series for book in books}
             total_rows += len(books) + len(series) + 1
         grid = self.grid
-        grid.SetTable(BooksDataTable(total_rows, columns), takeOwnership=True)
+        grid.SetTable(
+            BooksDataTable(total_rows+1, columns),
+            takeOwnership=True,
+        )
         grid.EnableEditing(False)
         for col, col_name in enumerate(columns):
             grid.AutoSizeColLabelSize(col)
-            if col_name in ('ser_no', 'size'):
+            if col == 0:
+                cell_attr = wx.grid.GridCellAttr()
+                cell_attr.SetAlignment(wx.ALIGN_CENTRE, wx. ALIGN_CENTRE)
+                grid.SetColAttr(col, cell_attr)
+            elif col_name in ('ser_no', 'size'):
                 cell_attr = wx.grid.GridCellAttr()
                 cell_attr.SetAlignment(wx.ALIGN_RIGHT, wx. ALIGN_CENTRE)
                 grid.SetColAttr(col, cell_attr)
         row = 0
+        grid.SetCellAlignment(row, 1, wx.ALIGN_CENTRE, wx. ALIGN_CENTRE)
+        grid.SetCellSize(row, 1, 1, len(columns)-1)
+        row = 1
+        self.toggle_rows = toggle_rows = {}  # map {row: [list of subrows]}
         for author in sorted(books_by_author):
-            grid.SetCellAlignment(row, 0, wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
-            grid.SetCellSize(row, 0, 1, len(columns))
-            grid.SetCellValue(row, 0, u'%s' % (author,))
+            grid.SetCellAlignment(row, 1, wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
+            grid.SetCellSize(row, 1, 1, len(columns)-1)
+            grid.SetCellValue(row, 1, u'%s' % (author,))
+            author_row = row
+            toggle_rows[author_row] = []
             row += 1
             books = books_by_author[author]
             series = None
@@ -91,26 +121,52 @@ class ListBooksPanel(GridPanel):
                         value = book.series
                     else:
                         value = u'Вне серий'
-                    grid.SetCellAlignment(row, 0,
+                    grid.SetCellAlignment(row, 1,
                                           wx.ALIGN_LEFT, wx. ALIGN_CENTRE)
-                    grid.SetCellSize(row, 0, 1, len(columns))
-                    grid.SetCellValue(row, 0,
+                    grid.SetCellSize(row, 1, 1, len(columns)-1)
+                    grid.SetCellValue(row, 1,
                                       u'%s — %s' % (book.author1, value))
+                    series_row = row
+                    toggle_rows[author_row].append(row)
+                    toggle_rows[series_row] = []
                     row += 1
                     series = book.series
-                for col, col_name in enumerate(columns):
+                for col, col_name in enumerate(columns[1:]):
                     value = getattr(book, col_name)
                     if value is None:
                         value = u''
                     elif not isinstance(value, (string_type, unicode_type)):
                         value = str(value)
-                    grid.SetCellValue(row, col, value)
+                    grid.SetCellValue(row, col+1, value)
+                toggle_rows[author_row].append(row)
+                toggle_rows[series_row].append(row)
                 row += 1
+        toggle_rows[0] = [row_ for row_ in range(1, row)]
         grid.AutoSizeColumns()
         grid.AutoSizeRows()
+        grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.OnClick)
+
+    def toggleCB(self, row):
+        value = self.grid.GetCellValue(row, 0)
+        if value:
+            value = ''
+        else:
+            value = '1'
+        self.grid.SetCellValue(row, 0, value)
+        toggle_rows = self.toggle_rows
+        if row in toggle_rows:
+            for row_ in toggle_rows[row]:
+                self.grid.SetCellValue(row_, 0, value)
+
+    def OnClick(self, event):
+        if event.GetCol() > 0:
+            return
+        self.toggleCB(event.GetRow())
 
     def OnDClick(self, event):
-        pass
+        if event.GetCol() == 0:
+            return
+        self.toggleCB(event.GetRow())
 
     def OnKeyDown(self, event):
         if event.GetKeyCode() == wx.WXK_ESCAPE: