Scroll word list on pageup/dn and arrow up/dn

This commit is contained in:
Kovid Goyal 2014-04-20 09:24:18 +05:30
parent 1ac8f8147f
commit 8acc2c4da1

View File

@ -727,6 +727,30 @@ class WordsModel(QAbstractTableModel):
except ValueError: except ValueError:
return -1 return -1
class WordsView(QTableView):
def __init__(self, parent=None):
QTableView.__init__(self, parent)
self.setSortingEnabled(True), self.setShowGrid(False), self.setAlternatingRowColors(True)
self.setSelectionBehavior(self.SelectRows), self.setSelectionMode(self.SingleSelection)
self.setTabKeyNavigation(False)
self.verticalHeader().close()
def keyPressEvent(self, ev):
ret = QTableView.keyPressEvent(self, ev)
if ev.key() in (Qt.Key_PageUp, Qt.Key_PageDown, Qt.Key_Up, Qt.Key_Down):
idx = self.currentIndex()
if idx.isValid():
self.scrollTo(idx)
return ret
def highlight_row(self, row):
idx = self.model().index(row, 0)
if idx.isValid():
self.selectRow(row)
self.setCurrentIndex(idx)
self.scrollTo(idx)
class SpellCheck(Dialog): class SpellCheck(Dialog):
work_finished = pyqtSignal(object, object) work_finished = pyqtSignal(object, object)
@ -783,16 +807,12 @@ class SpellCheck(Dialog):
m.h2 = h = QHBoxLayout() m.h2 = h = QHBoxLayout()
l.addLayout(h) l.addLayout(h)
self.words_view = w = QTableView(m) self.words_view = w = WordsView(m)
set_no_activate_on_click(w) set_no_activate_on_click(w)
w.activated.connect(self.word_activated) w.activated.connect(self.word_activated)
w.currentChanged = self.current_word_changed w.currentChanged = self.current_word_changed
state = tprefs.get('spell-check-table-state', None) state = tprefs.get('spell-check-table-state', None)
hh = self.words_view.horizontalHeader() hh = self.words_view.horizontalHeader()
w.setSortingEnabled(True), w.setShowGrid(False), w.setAlternatingRowColors(True)
w.setSelectionBehavior(w.SelectRows), w.setSelectionMode(w.SingleSelection)
w.setTabKeyNavigation(False)
w.verticalHeader().close()
h.addWidget(w) h.addWidget(w)
self.words_model = m = WordsModel(self) self.words_model = m = WordsModel(self)
w.setModel(m) w.setModel(m)
@ -941,7 +961,7 @@ class SpellCheck(Dialog):
w = self.words_model.replace_word(w, new_word) w = self.words_model.replace_word(w, new_word)
row = self.words_model.row_for_word(w) row = self.words_model.row_for_word(w)
if row > -1: if row > -1:
self.highlight_row(row) self.words_view.highlight_row(row)
def toggle_ignore(self): def toggle_ignore(self):
current = self.words_view.currentIndex() current = self.words_view.currentIndex()
@ -963,13 +983,6 @@ class SpellCheck(Dialog):
self.words_view.horizontalHeader().setSectionHidden(3, m.show_only_misspelt) self.words_view.horizontalHeader().setSectionHidden(3, m.show_only_misspelt)
self.do_filter() self.do_filter()
def highlight_row(self, row):
idx = self.words_model.index(row, 0)
if idx.isValid():
self.words_view.selectRow(row)
self.words_view.setCurrentIndex(idx)
self.words_view.scrollTo(idx)
def __enter__(self): def __enter__(self):
idx = self.words_view.currentIndex().row() idx = self.words_view.currentIndex().row()
self.__current_word = self.words_model.word_for_row(idx) self.__current_word = self.words_model.word_for_row(idx)
@ -977,7 +990,7 @@ class SpellCheck(Dialog):
def __exit__(self, *args): def __exit__(self, *args):
if self.__current_word is not None: if self.__current_word is not None:
row = self.words_model.row_for_word(self.__current_word) row = self.words_model.row_for_word(self.__current_word)
self.highlight_row(max(0, row)) self.words_view.highlight_row(max(0, row))
self.__current_word = None self.__current_word = None
def do_filter(self): def do_filter(self):
@ -1031,7 +1044,7 @@ class SpellCheck(Dialog):
col, reverse = self.words_model.sort_on col, reverse = self.words_model.sort_on
self.words_view.horizontalHeader().setSortIndicator( self.words_view.horizontalHeader().setSortIndicator(
col, Qt.DescendingOrder if reverse else Qt.AscendingOrder) col, Qt.DescendingOrder if reverse else Qt.AscendingOrder)
self.highlight_row(0) self.words_view.highlight_row(0)
self.update_summary() self.update_summary()
self.initialize_user_dictionaries() self.initialize_user_dictionaries()
if self.words_model.rowCount() > 0: if self.words_model.rowCount() > 0:
@ -1103,5 +1116,5 @@ def find_next(word, locations, current_editor, current_editor_name,
if __name__ == '__main__': if __name__ == '__main__':
app = QApplication([]) app = QApplication([])
dictionaries.initialize() dictionaries.initialize()
ManageUserDictionaries.test() SpellCheck.test()
del app del app