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)

This commit is contained in:
Kovid Goyal 2014-08-06 21:20:03 +05:30
parent ebfdca8c18
commit d39c25b413
2 changed files with 17 additions and 1 deletions

View File

@ -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.html import HTMLSmarts
from calibre.gui2.tweak_book.editor.smart.css import CSSSmarts from calibre.gui2.tweak_book.editor.smart.css import CSSSmarts
from calibre.spell.break_iterator import index_of 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' PARAGRAPH_SEPARATOR = '\u2029'
entity_pat = re.compile(r'&(#{0,1}[a-zA-Z0-9]{1,8});') entity_pat = re.compile(r'&(#{0,1}[a-zA-Z0-9]{1,8});')
@ -826,3 +827,10 @@ class TextEdit(PlainTextEdit):
c = self.textCursor() c = self.textCursor()
c.setPosition(block.position() + col) c.setPosition(block.position() + col)
self.setTextCursor(c) 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)

View File

@ -488,6 +488,14 @@ class Editor(QMainWindow):
m.addSeparator() m.addSeparator()
m.addAction(_('&Select all'), self.editor.select_all) m.addAction(_('&Select all'), self.editor.select_all)
m.addAction(actions['mark-selected-text']) 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': if self.syntax == 'html':
m.addAction(actions['multisplit']) m.addAction(actions['multisplit'])
m.exec_(self.editor.mapToGlobal(pos)) m.exec_(self.editor.mapToGlobal(pos))