Exemplo n.º 1
0
    def _search(self, direction):
        if self._editor.text() == "":
            self._reset()
            return

        if self._sit is None:
            data = self.parent().data
            case = any(map(str.isupper, self._editor.text()))
            self._sit = SearchIterator(data.subtitles,
                lambda sub, text = self._editor.text(), case = case: matchText(sub, text, case))

        self._updateIteratorPositionFromSelection(direction)

        fn = self._sit.next if direction == self.SearchDirection.Forward else self._sit.prev
        try:
            subNo = fn()
            self.parent().selectRow(subNo)
        except StopIteration:
            self._searchError()
Exemplo n.º 2
0
class SearchBar(QWidget):
    class SearchDirection:
        Forward = 1
        Backward = 2

    def __init__(self, parent):
        super(SearchBar, self).__init__(parent)

        self._sit = None

        self._editor = SearchEdit(self)
        self._prevButton = QPushButton("<", self)
        self._nextButton = QPushButton(">", self)
        self._closeButton = QPushButton(QIcon.fromTheme("window-close"), "", self)

        layout = QHBoxLayout()
        layout.setAlignment(Qt.AlignLeft)
        layout.setSpacing(0)

        layout.addWidget(self._editor)
        layout.addWidget(self._prevButton)
        layout.addWidget(self._nextButton)
        layout.addWidget(self._closeButton)
        layout.setContentsMargins(1, 1, 1, 1)

        self.setLayout(layout)

        self._nextButton.clicked.connect(self.next)
        self._prevButton.clicked.connect(self.prev)
        self._closeButton.clicked.connect(self.hide)
        self._editor.textChanged.connect(self._reset)
        self._editor.returnPressed.connect(self.next)
        self._editor.escapePressed.connect(self.hide)

    def highlight(self):
        self._editor.setFocus()
        self._editor.selectAll()

    def next(self):
        self._search(self.SearchDirection.Forward)

    def prev(self):
        self._search(self.SearchDirection.Backward)

    def _search(self, direction):
        if self._editor.text() == "":
            self._reset()
            return

        if self._sit is None:
            data = self.parent().data
            case = any(map(str.isupper, self._editor.text()))
            self._sit = SearchIterator(data.subtitles,
                lambda sub, text = self._editor.text(), case = case: matchText(sub, text, case))

        self._updateIteratorPositionFromSelection(direction)

        fn = self._sit.next if direction == self.SearchDirection.Forward else self._sit.prev
        try:
            subNo = fn()
            self.parent().selectRow(subNo)
        except StopIteration:
            self._searchError()

    def _updateIteratorPositionFromSelection(self, direction):
        selections = self.parent().selectedRows()
        startRow = selections[-1] if len(selections) > 0 else -1

        if startRow == -1 or len(self._sit.range()) == 0:
            return

        # When iterator points at different row, it means that user changed it
        if startRow != self._sit.last():
            pos = None
            if direction == self.SearchDirection.Forward:
                pos = bisect.bisect_right(self._sit.range(), startRow) - 1
            else:
                pos = bisect.bisect_left(self._sit.range(), startRow)
            self._sit.setpos(pos)

    def _searchError(self):
        self._editor.setStyleSheet("background-color: #CD5555")

    def _reset(self):
        self._sit = None
        self._editor.setStyleSheet("")