mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Implement marking of regions in the text
This commit is contained in:
parent
76d22f8f68
commit
a9d96d8d77
@ -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
|
||||
|
@ -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])
|
||||
|
@ -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
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
||||
|
@ -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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user