Cleanup cell_changed handling

This commit is contained in:
Kovid Goyal 2022-07-12 20:03:03 +05:30
parent cf3f5f131d
commit 9c0923e110
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -6,6 +6,7 @@ __docformat__ = 'restructuredtext en'
__license__ = 'GPL v3' __license__ = 'GPL v3'
from functools import partial from functools import partial
from contextlib import contextmanager
from qt.core import (Qt, QDialog, QTableWidgetItem, QAbstractItemView, QIcon, from qt.core import (Qt, QDialog, QTableWidgetItem, QAbstractItemView, QIcon,
QDialogButtonBox, QFrame, QLabel, QTimer, QMenu, QApplication, QDialogButtonBox, QFrame, QLabel, QTimer, QMenu, QApplication,
@ -97,6 +98,7 @@ class EditAuthorsDialog(QDialog, Ui_EditAuthorsDialog):
self.table.setColumnWidth(2, 200) self.table.setColumnWidth(2, 200)
# set up the cellChanged signal only after the table is filled # set up the cellChanged signal only after the table is filled
self.ignore_cell_changed = False
self.table.cellChanged.connect(self.cell_changed) self.table.cellChanged.connect(self.cell_changed)
self.recalc_author_sort.clicked.connect(self.do_recalc_author_sort) self.recalc_author_sort.clicked.connect(self.do_recalc_author_sort)
@ -170,6 +172,15 @@ class EditAuthorsDialog(QDialog, Ui_EditAuthorsDialog):
self.link_order = 1 self.link_order = 1
self.show_table(id_to_select, select_sort, select_link, is_first_letter) self.show_table(id_to_select, select_sort, select_link, is_first_letter)
@contextmanager
def no_cell_changed(self):
orig = self.ignore_cell_changed
self.ignore_cell_changed = True
try:
yield
finally:
self.ignore_cell_changed = orig
def use_vl_changed(self, x): def use_vl_changed(self, x):
self.show_table(None, None, None, False) self.show_table(None, None, None, False)
@ -440,31 +451,29 @@ class EditAuthorsDialog(QDialog, Ui_EditAuthorsDialog):
self.result.append((id_, orig['name'], v['name'], v['sort'], v['link'])) self.result.append((id_, orig['name'], v['name'], v['sort'], v['link']))
def do_recalc_author_sort(self): def do_recalc_author_sort(self):
self.table.cellChanged.disconnect() with self.no_cell_changed():
for row in range(0,self.table.rowCount()): for row in range(0,self.table.rowCount()):
item_aut = self.table.item(row, 0) item_aut = self.table.item(row, 0)
id_ = int(item_aut.data(Qt.ItemDataRole.UserRole)) id_ = int(item_aut.data(Qt.ItemDataRole.UserRole))
aut = str(item_aut.text()).strip() aut = str(item_aut.text()).strip()
item_aus = self.table.item(row, 1) item_aus = self.table.item(row, 1)
# Sometimes trailing commas are left by changing between copy algs # Sometimes trailing commas are left by changing between copy algs
aus = str(author_to_author_sort(aut)).rstrip(',') aus = str(author_to_author_sort(aut)).rstrip(',')
item_aus.setText(aus) item_aus.setText(aus)
self.authors[id_]['sort'] = aus self.authors[id_]['sort'] = aus
self.set_icon(item_aus, id_) self.set_icon(item_aus, id_)
self.table.setFocus(Qt.FocusReason.OtherFocusReason) self.table.setFocus(Qt.FocusReason.OtherFocusReason)
self.table.cellChanged.connect(self.cell_changed)
def do_auth_sort_to_author(self): def do_auth_sort_to_author(self):
self.table.cellChanged.disconnect() with self.no_cell_changed():
for row in range(0,self.table.rowCount()): for row in range(0,self.table.rowCount()):
aus = str(self.table.item(row, 1).text()).strip() aus = str(self.table.item(row, 1).text()).strip()
item_aut = self.table.item(row, 0) item_aut = self.table.item(row, 0)
id_ = int(item_aut.data(Qt.ItemDataRole.UserRole)) id_ = int(item_aut.data(Qt.ItemDataRole.UserRole))
item_aut.setText(aus) item_aut.setText(aus)
self.authors[id_]['name'] = aus self.authors[id_]['name'] = aus
self.set_icon(item_aut, id_) self.set_icon(item_aut, id_)
self.table.setFocus(Qt.FocusReason.OtherFocusReason) self.table.setFocus(Qt.FocusReason.OtherFocusReason)
self.table.cellChanged.connect(self.cell_changed)
def set_icon(self, item, id_): def set_icon(self, item, id_):
col_name = self.get_column_name(item.column()) col_name = self.get_column_name(item.column())
@ -474,30 +483,31 @@ class EditAuthorsDialog(QDialog, Ui_EditAuthorsDialog):
item.setIcon(self.empty_icon) item.setIcon(self.empty_icon)
def cell_changed(self, row, col): def cell_changed(self, row, col):
id_ = int(self.table.item(row, 0).data(Qt.ItemDataRole.UserRole)) if self.ignore_cell_changed:
self.table.cellChanged.disconnect(self.cell_changed) return
if col == 0: with self.no_cell_changed():
item = self.table.item(row, 0) id_ = int(self.table.item(row, 0).data(Qt.ItemDataRole.UserRole))
aut = str(item.text()).strip() if col == 0:
aut_list = string_to_authors(aut) item = self.table.item(row, 0)
if len(aut_list) != 1: aut = str(item.text()).strip()
error_dialog(self.parent(), _('Invalid author name'), aut_list = string_to_authors(aut)
_('You cannot change an author to multiple authors.')).exec() if len(aut_list) != 1:
aut = ' % '.join(aut_list) error_dialog(self.parent(), _('Invalid author name'),
self.table.item(row, 0).setText(aut) _('You cannot change an author to multiple authors.')).exec()
item.set_sort_key() aut = ' % '.join(aut_list)
self.authors[id_]['name'] = aut self.table.item(row, 0).setText(aut)
self.set_icon(item, id_) item.set_sort_key()
c = self.table.item(row, 1) self.authors[id_]['name'] = aut
txt = author_to_author_sort(aut) self.set_icon(item, id_)
self.authors[id_]['sort'] = txt c = self.table.item(row, 1)
c.setText(txt) # This triggers another cellChanged event txt = author_to_author_sort(aut)
item = c self.authors[id_]['sort'] = txt
else: c.setText(txt) # This triggers another cellChanged event
item = self.table.item(row, col) item = c
item.set_sort_key() else:
self.set_icon(item, id_) item = self.table.item(row, col)
self.authors[id_][self.get_column_name(col)] = str(item.text()) item.set_sort_key()
self.table.cellChanged.connect(self.cell_changed) self.set_icon(item, id_)
self.authors[id_][self.get_column_name(col)] = str(item.text())
self.table.setCurrentItem(item) self.table.setCurrentItem(item)
self.table.scrollToItem(item) self.table.scrollToItem(item)