From a9d96d8d771917c5a2e099544275d1207e7b4313 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 11 Nov 2013 17:48:20 +0530 Subject: [PATCH] Implement marking of regions in the text --- src/calibre/gui2/tweak_book/boss.py | 6 +++++ src/calibre/gui2/tweak_book/editor/text.py | 24 +++++++++++++++++++- src/calibre/gui2/tweak_book/editor/themes.py | 3 +++ src/calibre/gui2/tweak_book/editor/widget.py | 3 +++ src/calibre/gui2/tweak_book/search.py | 5 ++++ src/calibre/gui2/tweak_book/ui.py | 3 +++ 6 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index f3c908235e..85b3c492fe 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -259,6 +259,12 @@ class Boss(QObject): self.update_global_history_actions() # }}} + def mark_selected_text(self): + ed = self.gui.central.current_editor + if ed is not None: + ed.mark_selected_text() + self.gui.central.search_panel.set_where('selected-text') + def search(self, action, overrides=None): ' Run a search/replace ' sp = self.gui.central.search_panel diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index cbd5f2115d..35bc7d6259 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -49,6 +49,8 @@ class TextEdit(QPlainTextEdit): def __init__(self, parent=None): QPlainTextEdit.__init__(self, parent) + self.current_cursor_line = None + self.current_search_mark = None self.highlighter = SyntaxHighlighter(self) self.apply_settings() self.setMouseTracking(True) @@ -107,6 +109,7 @@ class TextEdit(QPlainTextEdit): w = self.fontMetrics() self.number_width = max(map(lambda x:w.width(str(x)), xrange(10))) self.size_hint = QSize(100 * w.averageCharWidth(), 50 * w.height()) + self.highlight_color = theme_color(theme, 'HighlightRegion', 'bg') # }}} def load_text(self, text, syntax='html'): @@ -127,6 +130,24 @@ class TextEdit(QPlainTextEdit): self.setTextCursor(c) self.ensureCursorVisible() + def update_extra_selections(self): + sel = [] + if self.current_cursor_line is not None: + sel.append(self.current_cursor_line) + if self.current_search_mark is not None: + sel.append(self.current_search_mark) + self.setExtraSelections(sel) + + def mark_selected_text(self): + sel = QTextEdit.ExtraSelection() + sel.format.setBackground(self.highlight_color) + sel.cursor = self.textCursor() + self.current_search_mark = sel + self.update_extra_selections() + c = self.textCursor() + c.clearSelection() + self.setTextCursor(c) + # Line numbers and cursor line {{{ def highlight_cursor_line(self): sel = QTextEdit.ExtraSelection() @@ -134,7 +155,8 @@ class TextEdit(QPlainTextEdit): sel.format.setProperty(QTextFormat.FullWidthSelection, True) sel.cursor = self.textCursor() sel.cursor.clearSelection() - self.setExtraSelections([sel]) + self.current_cursor_line = sel + self.update_extra_selections() # Update the cursor line's line number in the line number area try: self.line_number_area.update(0, self.last_current_lnum[0], self.line_number_area.width(), self.last_current_lnum[1]) diff --git a/src/calibre/gui2/tweak_book/editor/themes.py b/src/calibre/gui2/tweak_book/editor/themes.py index ac94e9b134..b54a24d8cf 100644 --- a/src/calibre/gui2/tweak_book/editor/themes.py +++ b/src/calibre/gui2/tweak_book/editor/themes.py @@ -34,6 +34,7 @@ SOLARIZED = \ CursorLine bg={base02} CursorColumn bg={base02} ColorColumn bg={base02} + HighlightRegion bg={base00} MatchParen fg={red} bg={base01} bold Pmenu fg={base0} bg={base02} PmenuSel fg={base01} bg={base2} @@ -68,6 +69,7 @@ THEMES = { CursorLine bg={cursor_loc} CursorColumn bg={cursor_loc} ColorColumn bg={cursor_loc} + HighlightRegion bg=323232 MatchParen fg=f6f3e8 bg=857b6f bold Pmenu fg=f6f3e8 bg=444444 PmenuSel fg=yellow bg={identifier} @@ -106,6 +108,7 @@ THEMES = { CursorLine bg={cursor_loc} CursorColumn bg={cursor_loc} ColorColumn bg={cursor_loc} + HighlightRegion bg=E3F988 MatchParen fg=white bg=80a090 bold Pmenu fg=white bg=808080 PmenuSel fg=white bg=808080 diff --git a/src/calibre/gui2/tweak_book/editor/widget.py b/src/calibre/gui2/tweak_book/editor/widget.py index c8db288195..321a1095fe 100644 --- a/src/calibre/gui2/tweak_book/editor/widget.py +++ b/src/calibre/gui2/tweak_book/editor/widget.py @@ -67,6 +67,9 @@ class Editor(QMainWindow): def redo(self): self.editor.redo() + def mark_selected_text(self): + self.editor.mark_selected_text() + @dynamic_property def is_modified(self): def fget(self): diff --git a/src/calibre/gui2/tweak_book/search.py b/src/calibre/gui2/tweak_book/search.py index 0e1e6b380d..4943f9abed 100644 --- a/src/calibre/gui2/tweak_book/search.py +++ b/src/calibre/gui2/tweak_book/search.py @@ -219,6 +219,8 @@ class SearchWidget(QWidget): def restore_state(self): self.state = tprefs.get('find-widget-state', self.DEFAULT_STATE) + if self.where == 'selected-text': + self.where = self.DEFAULT_STATE['where'] def save_state(self): tprefs.set('find-widget-state', self.state) @@ -261,3 +263,6 @@ class SearchPanel(QWidget): ans['replace'] = self.widget.replace return ans + def set_where(self, val): + self.widget.where = val + diff --git a/src/calibre/gui2/tweak_book/ui.py b/src/calibre/gui2/tweak_book/ui.py index 7075b0caa0..7e3325088c 100644 --- a/src/calibre/gui2/tweak_book/ui.py +++ b/src/calibre/gui2/tweak_book/ui.py @@ -233,6 +233,7 @@ class Main(MainWindow): 'replace-all', keys=('Ctrl+A'), description=_('Replace all matches')) self.action_count = sreg('count-matches', _('&Count all'), 'count', keys=('Ctrl+N'), description=_('Count number of matches')) + self.action_mark = reg(None, _('&Mark selected text'), self.boss.mark_selected_text, 'mark-selected-text', ('Ctrl+Shift+M',), _('Mark selected text')) def create_menubar(self): b = self.menuBar() @@ -281,6 +282,8 @@ class Main(MainWindow): a(self.action_replace_all) e.addSeparator() a(self.action_count) + e.addSeparator() + a(self.action_mark) def create_toolbars(self): def create(text, name):