From 47a24ae092ebec330a52488977ca742f3987bab6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 16 Mar 2022 10:39:27 +0530 Subject: [PATCH] Edit metadata dialog: When using the change case operations if some text is selected, only operate on the selected text. Fixes #1963822 [change case doesn't care about selection](https://bugs.launchpad.net/calibre/+bug/1963822) --- src/calibre/gui2/comments_editor.py | 6 ++++++ src/calibre/gui2/widgets.py | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/calibre/gui2/comments_editor.py b/src/calibre/gui2/comments_editor.py index 2180aeb4fe..59590a72f8 100644 --- a/src/calibre/gui2/comments_editor.py +++ b/src/calibre/gui2/comments_editor.py @@ -798,10 +798,16 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{ def text(self): return self.textCursor().selectedText() + selectedText = text def setText(self, text): with self.editing_cursor() as c: c.insertText(text) + insert = setText + + def hasSelectedText(self): + c = self.textCursor() + return c.hasSelection() def contextMenuEvent(self, ev): menu = self.createStandardContextMenu() diff --git a/src/calibre/gui2/widgets.py b/src/calibre/gui2/widgets.py index 49d774b769..ba9caddc77 100644 --- a/src/calibre/gui2/widgets.py +++ b/src/calibre/gui2/widgets.py @@ -492,25 +492,32 @@ class LineEditECM: # {{{ self.create_change_case_menu(menu) menu.exec(event.globalPos()) + def modify_case_operation(self, func): + has_selection = self.hasSelectedText() + text = self.selectedText() if has_selection else self.text() + ntext = func(text) + if ntext != text: + self.insert(ntext) if has_selection else self.setText(ntext) + def upper_case(self): from calibre.utils.icu import upper - self.setText(upper(str(self.text()))) + self.modify_case_operation(upper) def lower_case(self): from calibre.utils.icu import lower - self.setText(lower(str(self.text()))) + self.modify_case_operation(lower) def swap_case(self): from calibre.utils.icu import swapcase - self.setText(swapcase(str(self.text()))) + self.modify_case_operation(swapcase) def title_case(self): from calibre.utils.titlecase import titlecase - self.setText(titlecase(str(self.text()))) + self.modify_case_operation(titlecase) def capitalize(self): from calibre.utils.icu import capitalize - self.setText(capitalize(str(self.text()))) + self.modify_case_operation(capitalize) # }}}