(self)
Add or remove lines if needed and fix the tab order.
Add or remove lines if needed and fix the tab order.
Add or remove lines if needed and fix the tab order .
def adjust_n_rule_rows(self):
        """Add or remove lines if needed and fix the tab order."""
        def _add_line():
            self.line_edits.append([])
            n_lines = len(self.line_edits)
            for coli in range(1, 3):
                edit = QLineEdit()
                self.line_edits[-1].append(edit)
                self.rules_box.addWidget(edit, n_lines, coli)
                edit.textChanged.connect(self.sync_edit)
            button = gui.button(
                None, self, label='×', width=33,
                autoDefault=False, callback=self.remove_row,
                sizePolicy=(QSizePolicy.Maximum,
                            QSizePolicy.Maximum)
            )
            self.remove_buttons.append(button)
            self.rules_box.addWidget(button, n_lines, 0)
            self.counts.append([])
            for coli, kwargs in enumerate(
                    (dict(),
                     dict(styleSheet="color: gray"))):
                label = QLabel(alignment=Qt.AlignCenter, **kwargs)
                self.counts[-1].append(label)
                self.rules_box.addWidget(label, n_lines, 3 + coli)

        def _remove_line():
            for edit in self.line_edits.pop():
                edit.deleteLater()
            self.remove_buttons.pop().deleteLater()
            for label in self.counts.pop():
                label.deleteLater()

        def _fix_tab_order():
            prev = None
            for row, rule in zip(self.line_edits, self.active_rules):
                for col_idx, edit in enumerate(row):
                    edit.row, edit.col_idx = rule, col_idx
                    if prev is not None:
                        self.setTabOrder(prev, edit)
                    prev = edit

        n = len(self.active_rules)
        while n > len(self.line_edits):
            _add_line()
        while len(self.line_edits) > n:
            _remove_line()
        _fix_tab_order()
def adjust_n_rule_rows ( self ) : def _add_line ( ) : self . line_edits . append ( [ ] ) n_lines = len ( self . line_edits ) for coli in range ( 1 , 3 ) : edit = QLineEdit ( ) self . line_edits [ - 1 ] . append ( edit ) self . rules_box . addWidget ( edit , n_lines , coli ) edit . textChanged . connect ( self . sync_edit ) button = gui . button ( None , self , label = '×', idth= 3 3, autoDefault = False , callback = self . remove_row , sizePolicy = ( QSizePolicy . Maximum , QSizePolicy . Maximum ) ) self . remove_buttons . append ( button ) self . rules_box . addWidget ( button , n_lines , 0 ) self . counts . append ( [ ] ) for coli , kwargs in enumerate ( ( dict ( ) , dict ( styleSheet = "color: gray" ) ) ) : label = QLabel ( alignment = Qt . AlignCenter , * * kwargs ) self . counts [ - 1 ] . append ( label ) self . rules_box . addWidget ( label , n_lines , 3 + coli ) def _remove_line ( ) : for edit in self . line_edits . pop ( ) : edit . deleteLater ( ) self . remove_buttons . pop ( ) . deleteLater ( ) for label in self . counts . pop ( ) : label . deleteLater ( ) def _fix_tab_order ( ) : prev = None for row , rule in zip ( self . line_edits , self . active_rules ) : for col_idx , edit in enumerate ( row ) : edit . row , edit . col_idx = rule , col_idx if prev is not None : self . setTabOrder ( prev , edit ) prev = edit n = len ( self . active_rules ) while n > len ( self . line_edits ) : _add_line ( ) while len ( self . line_edits ) > n : _remove_line ( ) _fix_tab_order ( )