Move selection()/has_selection() to caret

This commit is contained in:
Florian Bruhin 2016-07-04 07:52:49 +02:00
parent 9f130c6b27
commit 515d16f137
4 changed files with 36 additions and 38 deletions

View File

@ -1027,9 +1027,9 @@ class CommandDispatcher:
if webview is None: if webview is None:
mainframe = None mainframe = None
else: else:
if webview.hasSelection(): if webview.caret.has_selection():
env['QUTE_SELECTED_TEXT'] = webview.selection() env['QUTE_SELECTED_TEXT'] = webview.caret.selection()
env['QUTE_SELECTED_HTML'] = webview.selection(html=True) env['QUTE_SELECTED_HTML'] = webview.caret.selection(html=True)
mainframe = webview.page().mainFrame() mainframe = webview.page().mainFrame()
try: try:
@ -1108,7 +1108,7 @@ class CommandDispatcher:
tab: Load the selected link in a new tab. tab: Load the selected link in a new tab.
""" """
widget = self._current_widget() widget = self._current_widget()
if not widget.has_selection(): if not widget.caret.has_selection():
return return
if QWebSettings.globalSettings().testAttribute( if QWebSettings.globalSettings().testAttribute(
QWebSettings.JavascriptEnabled): QWebSettings.JavascriptEnabled):
@ -1117,9 +1117,10 @@ class CommandDispatcher:
widget.run_js_async( widget.run_js_async(
'window.getSelection().anchorNode.parentNode.click()') 'window.getSelection().anchorNode.parentNode.click()')
else: else:
selection = widget.caret.selection(html=True)
try: try:
selected_element = xml.etree.ElementTree.fromstring( selected_element = xml.etree.ElementTree.fromstring(
'<html>' + widget.selection(html=True) + '</html>').find('a') '<html>{}</html>'.format(selection)).find('a')
except xml.etree.ElementTree.ParseError: except xml.etree.ElementTree.ParseError:
raise cmdexc.CommandError('Could not parse selected element!') raise cmdexc.CommandError('Could not parse selected element!')
@ -1646,9 +1647,9 @@ class CommandDispatcher:
sel: Use the primary selection instead of the clipboard. sel: Use the primary selection instead of the clipboard.
keep: If given, stay in visual mode after yanking. keep: If given, stay in visual mode after yanking.
""" """
tab = self._current_widget() caret = self._current_widget().caret
s = tab.selection() s = caret.selection()
if not tab.has_selection() or len(s) == 0: if not caret.has_selection() or len(s) == 0:
message.info(self._win_id, "Nothing to yank") message.info(self._win_id, "Nothing to yank")
return return

View File

@ -148,9 +148,8 @@ class AbstractCaret(QObject):
"""Attribute of AbstractTab for caret browsing.""" """Attribute of AbstractTab for caret browsing."""
def __init__(self, win_id, tab, parent=None): def __init__(self, win_id, parent=None):
super().__init__(parent) super().__init__(parent)
self._tab = tab
self._win_id = win_id self._win_id = win_id
self.widget = None self.widget = None
self.selection_enabled = False self.selection_enabled = False
@ -216,6 +215,12 @@ class AbstractCaret(QObject):
def drop_selection(self): def drop_selection(self):
raise NotImplementedError raise NotImplementedError
def has_selection(self):
raise NotImplementedError
def selection(self, html=False):
raise NotImplementedError
class AbstractScroller(QObject): class AbstractScroller(QObject):
@ -350,7 +355,7 @@ class AbstractTab(QWidget):
super().__init__(parent) super().__init__(parent)
# self.history = AbstractHistory(self) # self.history = AbstractHistory(self)
# self.scroll = AbstractScroller(parent=self) # self.scroll = AbstractScroller(parent=self)
# self.caret = AbstractCaret(win_id=win_id, tab=self, parent=self) # self.caret = AbstractCaret(win_id=win_id, parent=self)
# self.zoom = AbstractZoom(win_id=win_id) # self.zoom = AbstractZoom(win_id=win_id)
self._layout = None self._layout = None
self._widget = None self._widget = None
@ -412,12 +417,6 @@ class AbstractTab(QWidget):
def icon(self): def icon(self):
raise NotImplementedError raise NotImplementedError
def has_selection(self):
raise NotImplementedError
def selection(self, html=False):
raise NotImplementedError
def __repr__(self): def __repr__(self):
url = utils.elide(self.cur_url.toDisplayString(QUrl.EncodeUnicode), url = utils.elide(self.cur_url.toDisplayString(QUrl.EncodeUnicode),
100) 100)

View File

@ -35,7 +35,13 @@ class WebEngineCaret(tab.AbstractCaret):
## TODO ## TODO
pass def has_selection(self):
return self.widget.hasSelection()
def selection(self, html=False):
if html:
raise NotImplementedError
return self.widget.selectedText()
class WebEngineScroller(tab.AbstractScroller): class WebEngineScroller(tab.AbstractScroller):
@ -92,7 +98,7 @@ class WebEngineViewTab(tab.AbstractTab):
widget = QWebEngineView() widget = QWebEngineView()
self.history = WebEngineHistory(self) self.history = WebEngineHistory(self)
self.scroll = WebEngineScroller() self.scroll = WebEngineScroller()
self.caret = WebEngineCaret(win_id=win_id, tab=self, parent=self) self.caret = WebEngineCaret(win_id=win_id, parent=self)
self.zoom = WebEngineZoom(win_id=win_id, parent=self) self.zoom = WebEngineZoom(win_id=win_id, parent=self)
self._set_widget(widget) self._set_widget(widget)
self._connect_signals() self._connect_signals()
@ -144,14 +150,6 @@ class WebEngineViewTab(tab.AbstractTab):
def icon(self): def icon(self):
return self._widget.icon() return self._widget.icon()
def has_selection(self):
return self._widget.hasSelection()
def selection(self, html=False):
if html:
raise NotImplementedError
return self._widget.selectedText()
def _connect_signals(self): def _connect_signals(self):
view = self._widget view = self._widget
page = view.page() page = view.page()

View File

@ -40,7 +40,7 @@ class WebViewCaret(tab.AbstractCaret):
settings = self.widget.settings() settings = self.widget.settings()
settings.setAttribute(QWebSettings.CaretBrowsingEnabled, True) settings.setAttribute(QWebSettings.CaretBrowsingEnabled, True)
self.selection_enabled = bool(self._tab.selection()) self.selection_enabled = bool(self.selection())
if self.widget.isVisible(): if self.widget.isVisible():
# Sometimes the caret isn't immediately visible, but unfocusing # Sometimes the caret isn't immediately visible, but unfocusing
@ -53,7 +53,7 @@ class WebViewCaret(tab.AbstractCaret):
# #
# Note: We can't use hasSelection() here, as that's always # Note: We can't use hasSelection() here, as that's always
# true in caret mode. # true in caret mode.
if not self._tab.selection(): if not self.selection():
self.widget.page().currentFrame().evaluateJavaScript( self.widget.page().currentFrame().evaluateJavaScript(
utils.read_file('javascript/position_caret.js')) utils.read_file('javascript/position_caret.js'))
@ -212,6 +212,14 @@ class WebViewCaret(tab.AbstractCaret):
def drop_selection(self): def drop_selection(self):
self.widget.triggerPageAction(QWebPage.MoveToNextChar) self.widget.triggerPageAction(QWebPage.MoveToNextChar)
def has_selection(self):
return self.widget.hasSelection()
def selection(self, html=False):
if html:
return self.widget.selectedHtml()
return self.widget.selectedText()
class WebViewZoom(tab.AbstractZoom): class WebViewZoom(tab.AbstractZoom):
@ -366,7 +374,7 @@ class WebViewTab(tab.AbstractTab):
widget = webview.WebView(win_id, self.tab_id, tab=self) widget = webview.WebView(win_id, self.tab_id, tab=self)
self.history = WebViewHistory(self) self.history = WebViewHistory(self)
self.scroll = WebViewScroller(parent=self) self.scroll = WebViewScroller(parent=self)
self.caret = WebViewCaret(win_id=win_id, tab=self, parent=self) self.caret = WebViewCaret(win_id=win_id, parent=self)
self.zoom = WebViewZoom(win_id=win_id, parent=self) self.zoom = WebViewZoom(win_id=win_id, parent=self)
self._set_widget(widget) self._set_widget(widget)
self._connect_signals() self._connect_signals()
@ -418,14 +426,6 @@ class WebViewTab(tab.AbstractTab):
def title(self): def title(self):
return self._widget.title() return self._widget.title()
def has_selection(self):
return self._widget.hasSelection()
def selection(self, html=False):
if html:
return self._widget.selectedHtml()
return self._widget.selectedText()
def _connect_signals(self): def _connect_signals(self):
view = self._widget view = self._widget
page = view.page() page = view.page()