From d39c25b413e8a3380bb6566dd0bfb672be848050 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 6 Aug 2014 21:20:03 +0530 Subject: [PATCH] Edit Book: Allow changing the case of selected text by right clicking and choosing the appropriate change case action. Fixes #1353263 [[Enhancement] - add Change Case functionality to Edit Book](https://bugs.launchpad.net/calibre/+bug/1353263) --- src/calibre/gui2/tweak_book/editor/text.py | 10 +++++++++- src/calibre/gui2/tweak_book/editor/widget.py | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index a6812cdb78..44094260a8 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -28,7 +28,8 @@ from calibre.gui2.tweak_book.editor.smart import NullSmarts from calibre.gui2.tweak_book.editor.smart.html import HTMLSmarts from calibre.gui2.tweak_book.editor.smart.css import CSSSmarts from calibre.spell.break_iterator import index_of -from calibre.utils.icu import safe_chr, string_length +from calibre.utils.icu import safe_chr, string_length, capitalize, upper, lower, swapcase +from calibre.utils.titlecase import titlecase PARAGRAPH_SEPARATOR = '\u2029' entity_pat = re.compile(r'&(#{0,1}[a-zA-Z0-9]{1,8});') @@ -826,3 +827,10 @@ class TextEdit(PlainTextEdit): c = self.textCursor() c.setPosition(block.position() + col) self.setTextCursor(c) + + def change_case(self, action, cursor=None): + cursor = cursor or self.textCursor() + text = self.selected_text_from_cursor(cursor) + text = {'lower':lower, 'upper':upper, 'capitalize':capitalize, 'title':titlecase, 'swap':swapcase}[action](text) + cursor.insertText(text) + self.setTextCursor(cursor) diff --git a/src/calibre/gui2/tweak_book/editor/widget.py b/src/calibre/gui2/tweak_book/editor/widget.py index 6fcccb0a75..9f633f94bb 100644 --- a/src/calibre/gui2/tweak_book/editor/widget.py +++ b/src/calibre/gui2/tweak_book/editor/widget.py @@ -488,6 +488,14 @@ class Editor(QMainWindow): m.addSeparator() m.addAction(_('&Select all'), self.editor.select_all) m.addAction(actions['mark-selected-text']) + if self.syntax != 'css' and actions['editor-cut'].isEnabled(): + cm = QMenu(_('Change &case'), m) + for ac, text in ( + ('upper', _('&Upper case')), ('lower', _('&Lower case')), ('swap', _('&Swap case')), + ('title', _('&Title case')), ('capitalize', _('&Capitalize')) + ): + cm.addAction(text, partial(self.editor.change_case, ac)) + m.addMenu(cm) if self.syntax == 'html': m.addAction(actions['multisplit']) m.exec_(self.editor.mapToGlobal(pos))