Edit book: File list: Fix pressing Tab key while renaming a file not working

This commit is contained in:
Kovid Goyal 2022-09-22 20:40:20 +05:30
parent fe4ad47e86
commit b45c387260
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -7,7 +7,7 @@ import posixpath
import sys import sys
import textwrap import textwrap
from collections import Counter, OrderedDict, defaultdict from collections import Counter, OrderedDict, defaultdict
from functools import partial, lru_cache from functools import lru_cache, partial
from gettext import pgettext from gettext import pgettext
from qt.core import ( from qt.core import (
QAbstractItemView, QApplication, QCheckBox, QDialog, QDialogButtonBox, QFont, QAbstractItemView, QApplication, QCheckBox, QDialog, QDialogButtonBox, QFont,
@ -19,7 +19,6 @@ from qt.core import (
from calibre import human_readable, sanitize_file_name from calibre import human_readable, sanitize_file_name
from calibre.ebooks.oeb.base import OEB_DOCS, OEB_STYLES from calibre.ebooks.oeb.base import OEB_DOCS, OEB_STYLES
from calibre.ebooks.oeb.polish.utils import OEB_FONTS, guess_type
from calibre.ebooks.oeb.polish.cover import ( from calibre.ebooks.oeb.polish.cover import (
get_cover_page_name, get_raster_cover_name, is_raster_image get_cover_page_name, get_raster_cover_name, is_raster_image
) )
@ -27,6 +26,7 @@ from calibre.ebooks.oeb.polish.css import add_stylesheet_links
from calibre.ebooks.oeb.polish.replace import ( from calibre.ebooks.oeb.polish.replace import (
get_recommended_folders, get_spine_order_for_all_files get_recommended_folders, get_spine_order_for_all_files
) )
from calibre.ebooks.oeb.polish.utils import OEB_FONTS, guess_type
from calibre.gui2 import ( from calibre.gui2 import (
choose_dir, choose_files, choose_save_file, elided_text, error_dialog, choose_dir, choose_files, choose_save_file, elided_text, error_dialog,
make_view_use_window_background, question_dialog make_view_use_window_background, question_dialog
@ -127,7 +127,7 @@ def get_bulk_rename_settings(parent, number, msg=None, sanitize=sanitize_file_na
class ItemDelegate(QStyledItemDelegate): # {{{ class ItemDelegate(QStyledItemDelegate): # {{{
rename_requested = pyqtSignal(object, object) rename_requested = pyqtSignal(object, object, object)
def setEditorData(self, editor, index): def setEditorData(self, editor, index):
name = str(index.data(NAME_ROLE) or '') name = str(index.data(NAME_ROLE) or '')
@ -152,7 +152,7 @@ class ItemDelegate(QStyledItemDelegate): # {{{
newname = str(editor.text()) newname = str(editor.text())
oldname = str(index.data(NAME_ROLE) or '') oldname = str(index.data(NAME_ROLE) or '')
if newname != oldname: if newname != oldname:
self.rename_requested.emit(oldname, newname) self.rename_requested.emit(index, oldname, newname)
def sizeHint(self, option, index): def sizeHint(self, option, index):
ans = QStyledItemDelegate.sizeHint(self, option, index) ans = QStyledItemDelegate.sizeHint(self, option, index)
@ -244,13 +244,14 @@ class FileList(QTreeWidget, OpenWithHandler):
def __init__(self, parent=None): def __init__(self, parent=None):
QTreeWidget.__init__(self, parent) QTreeWidget.__init__(self, parent)
self.pending_renames = {}
make_view_use_window_background(self) make_view_use_window_background(self)
self.categories = {} self.categories = {}
self.ordered_selected_indexes = False self.ordered_selected_indexes = False
set_no_activate_on_click(self) set_no_activate_on_click(self)
self.current_edited_name = None self.current_edited_name = None
self.delegate = ItemDelegate(self) self.delegate = ItemDelegate(self)
self.delegate.rename_requested.connect(self.rename_requested) self.delegate.rename_requested.connect(self.possible_rename_requested, type=Qt.ConnectionType.QueuedConnection)
self.setTextElideMode(Qt.TextElideMode.ElideMiddle) self.setTextElideMode(Qt.TextElideMode.ElideMiddle)
self.setItemDelegate(self.delegate) self.setItemDelegate(self.delegate)
self.setIconSize(QSize(16, 16)) self.setIconSize(QSize(16, 16))
@ -284,6 +285,23 @@ class FileList(QTreeWidget, OpenWithHandler):
})} })}
self.itemActivated.connect(self.item_double_clicked) self.itemActivated.connect(self.item_double_clicked)
def possible_rename_requested(self, index, old, new):
self.pending_renames[old] = new
QTimer.singleShot(10, self.dispatch_pending_renames)
item = self.itemFromIndex(index)
item.setText(0, new)
def dispatch_pending_renames(self):
if self.state() != QAbstractItemView.State.EditingState:
pr, self.pending_renames = self.pending_renames, {}
if len(pr) == 1:
old, new = tuple(pr.items())[0]
self.rename_requested.emit(old, new)
else:
self.bulk_rename_requested.emit(pr)
else:
QTimer.singleShot(10, self.dispatch_pending_renames)
def mimeTypes(self): def mimeTypes(self):
ans = QTreeWidget.mimeTypes(self) ans = QTreeWidget.mimeTypes(self)
ans.append(CONTAINER_DND_MIMETYPE) ans.append(CONTAINER_DND_MIMETYPE)