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

View File

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

View File

@ -35,7 +35,13 @@ class WebEngineCaret(tab.AbstractCaret):
## 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):
@ -92,7 +98,7 @@ class WebEngineViewTab(tab.AbstractTab):
widget = QWebEngineView()
self.history = WebEngineHistory(self)
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._set_widget(widget)
self._connect_signals()
@ -144,14 +150,6 @@ class WebEngineViewTab(tab.AbstractTab):
def icon(self):
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):
view = self._widget
page = view.page()

View File

@ -40,7 +40,7 @@ class WebViewCaret(tab.AbstractCaret):
settings = self.widget.settings()
settings.setAttribute(QWebSettings.CaretBrowsingEnabled, True)
self.selection_enabled = bool(self._tab.selection())
self.selection_enabled = bool(self.selection())
if self.widget.isVisible():
# 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
# true in caret mode.
if not self._tab.selection():
if not self.selection():
self.widget.page().currentFrame().evaluateJavaScript(
utils.read_file('javascript/position_caret.js'))
@ -212,6 +212,14 @@ class WebViewCaret(tab.AbstractCaret):
def drop_selection(self):
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):
@ -366,7 +374,7 @@ class WebViewTab(tab.AbstractTab):
widget = webview.WebView(win_id, self.tab_id, tab=self)
self.history = WebViewHistory(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._set_widget(widget)
self._connect_signals()
@ -418,14 +426,6 @@ class WebViewTab(tab.AbstractTab):
def title(self):
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):
view = self._widget
page = view.page()