2014-04-19 17:50:11 +02:00
|
|
|
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""A HintManager to draw hints over links."""
|
|
|
|
|
2014-04-21 16:59:03 +02:00
|
|
|
import logging
|
2014-04-20 23:58:14 +02:00
|
|
|
import math
|
2014-04-21 15:45:29 +02:00
|
|
|
from collections import namedtuple
|
2014-04-20 19:24:22 +02:00
|
|
|
|
2014-04-22 09:35:59 +02:00
|
|
|
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QEvent, Qt
|
2014-04-21 23:33:36 +02:00
|
|
|
from PyQt5.QtGui import QMouseEvent, QClipboard
|
|
|
|
from PyQt5.QtWidgets import QApplication
|
2014-04-21 15:20:41 +02:00
|
|
|
|
2014-04-20 19:24:22 +02:00
|
|
|
import qutebrowser.config.config as config
|
2014-04-25 12:21:01 +02:00
|
|
|
import qutebrowser.keyinput.modeman as modeman
|
2014-04-21 19:31:28 +02:00
|
|
|
import qutebrowser.utils.message as message
|
2014-04-21 23:44:45 +02:00
|
|
|
import qutebrowser.utils.url as urlutils
|
2014-04-24 16:28:00 +02:00
|
|
|
import qutebrowser.utils.webelem as webelem
|
2014-04-21 15:20:41 +02:00
|
|
|
|
|
|
|
|
2014-04-21 15:45:29 +02:00
|
|
|
ElemTuple = namedtuple('ElemTuple', 'elem, label')
|
|
|
|
|
|
|
|
|
2014-04-21 15:20:41 +02:00
|
|
|
class HintManager(QObject):
|
2014-04-19 17:50:11 +02:00
|
|
|
|
|
|
|
"""Manage drawing hints over links or other elements.
|
|
|
|
|
|
|
|
Class attributes:
|
2014-04-20 23:03:18 +02:00
|
|
|
HINT_CSS: The CSS template to use for hints.
|
2014-04-19 17:50:11 +02:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
_frame: The QWebFrame to use.
|
2014-04-21 15:45:29 +02:00
|
|
|
_elems: A mapping from keystrings to (elem, label) namedtuples.
|
2014-04-21 23:44:45 +02:00
|
|
|
_baseurl: The URL of the current page.
|
2014-04-21 19:29:11 +02:00
|
|
|
_target: What to do with the opened links.
|
2014-04-25 16:53:23 +02:00
|
|
|
'normal'/'tab'/'bgtab': Get passed to BrowserTab.
|
|
|
|
'yank'/'yank_primary': Yank to clipboard/primary selection
|
|
|
|
'cmd'/'cmd_tab'/'cmd_bgtab': Enter link to commandline
|
|
|
|
'rapid': Rapid mode with background tabs
|
2014-04-27 21:59:23 +02:00
|
|
|
_to_follow: The link to follow when enter is pressed.
|
2014-04-21 15:20:41 +02:00
|
|
|
|
|
|
|
Signals:
|
|
|
|
hint_strings_updated: Emitted when the possible hint strings changed.
|
2014-04-21 16:59:03 +02:00
|
|
|
arg: A list of hint strings.
|
|
|
|
mouse_event: Mouse event to be posted in the web view.
|
|
|
|
arg: A QMouseEvent
|
|
|
|
openurl: Open a new url
|
2014-05-01 16:35:26 +02:00
|
|
|
arg 0: URL to open as QUrl.
|
|
|
|
arg 1: True if it should be opened in a new tab, else False.
|
2014-04-21 19:29:11 +02:00
|
|
|
set_open_target: Set a new target to open the links in.
|
2014-04-19 17:50:11 +02:00
|
|
|
"""
|
|
|
|
|
2014-04-19 23:16:39 +02:00
|
|
|
HINT_CSS = """
|
2014-04-20 19:24:22 +02:00
|
|
|
color: {config[colors][hints.fg]};
|
|
|
|
background: {config[colors][hints.bg]};
|
|
|
|
font: {config[fonts][hints]};
|
|
|
|
border: {config[hints][border]};
|
|
|
|
opacity: {config[hints][opacity]};
|
2014-04-20 16:42:55 +02:00
|
|
|
z-index: 100000;
|
2014-04-21 23:32:58 +02:00
|
|
|
pointer-events: none;
|
2014-04-19 23:16:39 +02:00
|
|
|
position: absolute;
|
|
|
|
left: {left}px;
|
|
|
|
top: {top}px;
|
|
|
|
"""
|
|
|
|
|
2014-04-21 15:20:41 +02:00
|
|
|
hint_strings_updated = pyqtSignal(list)
|
2014-04-21 16:59:03 +02:00
|
|
|
mouse_event = pyqtSignal('QMouseEvent')
|
2014-05-01 16:35:26 +02:00
|
|
|
openurl = pyqtSignal('QUrl', bool)
|
2014-04-21 19:29:11 +02:00
|
|
|
set_open_target = pyqtSignal(str)
|
2014-04-21 15:20:41 +02:00
|
|
|
|
2014-04-22 14:23:55 +02:00
|
|
|
def __init__(self, parent=None):
|
2014-04-19 17:50:11 +02:00
|
|
|
"""Constructor.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
frame: The QWebFrame to use for finding elements and drawing.
|
|
|
|
"""
|
2014-04-22 14:23:55 +02:00
|
|
|
super().__init__(parent)
|
2014-04-21 15:45:29 +02:00
|
|
|
self._elems = {}
|
2014-04-22 14:23:55 +02:00
|
|
|
self._frame = None
|
2014-04-21 19:29:11 +02:00
|
|
|
self._target = None
|
2014-04-21 23:53:13 +02:00
|
|
|
self._baseurl = None
|
2014-04-27 21:59:23 +02:00
|
|
|
self._to_follow = None
|
2014-04-25 12:21:01 +02:00
|
|
|
modeman.manager.left.connect(self.on_mode_left)
|
2014-04-19 23:16:39 +02:00
|
|
|
|
2014-04-20 23:58:14 +02:00
|
|
|
def _hint_strings(self, elems):
|
|
|
|
"""Calculate the hint strings for elems.
|
|
|
|
|
2014-04-22 08:45:56 +02:00
|
|
|
Inspired by Vimium.
|
2014-04-21 00:24:08 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
elems: The elements to get hint strings for.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
A list of hint strings, in the same order as the elements.
|
2014-04-20 23:58:14 +02:00
|
|
|
"""
|
2014-05-02 17:53:16 +02:00
|
|
|
if config.get('hints', 'mode') == 'number':
|
|
|
|
chars = '0123456789'
|
|
|
|
else:
|
|
|
|
chars = config.get('hints', 'chars')
|
2014-04-20 23:58:14 +02:00
|
|
|
# Determine how many digits the link hints will require in the worst
|
|
|
|
# case. Usually we do not need all of these digits for every link
|
|
|
|
# single hint, so we can show shorter hints for a few of the links.
|
|
|
|
needed = math.ceil(math.log(len(elems), len(chars)))
|
|
|
|
# Short hints are the number of hints we can possibly show which are
|
|
|
|
# (needed - 1) digits in length.
|
|
|
|
short_count = math.floor((len(chars) ** needed - len(elems)) /
|
|
|
|
len(chars))
|
|
|
|
long_count = len(elems) - short_count
|
|
|
|
|
|
|
|
strings = []
|
|
|
|
|
|
|
|
if needed > 1:
|
|
|
|
for i in range(short_count):
|
|
|
|
strings.append(self._number_to_hint_str(i, chars, needed - 1))
|
|
|
|
|
|
|
|
start = short_count * len(chars)
|
|
|
|
for i in range(start, start + long_count):
|
|
|
|
strings.append(self._number_to_hint_str(i, chars, needed))
|
|
|
|
|
|
|
|
return self._shuffle_hints(strings, len(chars))
|
|
|
|
|
|
|
|
def _shuffle_hints(self, hints, length):
|
|
|
|
"""Shuffle the given set of hints so that they're scattered.
|
|
|
|
|
|
|
|
Hints starting with the same character will be spread evenly throughout
|
|
|
|
the array.
|
|
|
|
|
|
|
|
Inspired by Vimium.
|
2014-04-21 00:24:08 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
hints: A list of hint strings.
|
|
|
|
length: Length of the available charset.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
A list of shuffled hint strings.
|
2014-04-20 23:58:14 +02:00
|
|
|
"""
|
|
|
|
buckets = [[] for i in range(length)]
|
|
|
|
for i, hint in enumerate(hints):
|
|
|
|
buckets[i % len(buckets)].append(hint)
|
|
|
|
result = []
|
|
|
|
for bucket in buckets:
|
|
|
|
result += bucket
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _number_to_hint_str(self, number, chars, digits=0):
|
|
|
|
"""Convert a number like "8" into a hint string like "JK".
|
|
|
|
|
|
|
|
This is used to sequentially generate all of the hint text.
|
|
|
|
The hint string will be "padded with zeroes" to ensure its length is >=
|
|
|
|
digits.
|
|
|
|
|
|
|
|
Inspired by Vimium.
|
2014-04-21 00:24:08 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
number: The hint number.
|
|
|
|
chars: The charset to use.
|
|
|
|
digits: The minimum output length.
|
|
|
|
|
|
|
|
Return:
|
|
|
|
A hint string.
|
2014-04-20 23:58:14 +02:00
|
|
|
"""
|
|
|
|
base = len(chars)
|
|
|
|
hintstr = []
|
|
|
|
remainder = 0
|
|
|
|
while True:
|
|
|
|
remainder = number % base
|
|
|
|
hintstr.insert(0, chars[remainder])
|
|
|
|
number -= remainder
|
|
|
|
number //= base
|
|
|
|
if number <= 0:
|
|
|
|
break
|
|
|
|
# Pad the hint string we're returning so that it matches digits.
|
2014-04-21 00:24:08 +02:00
|
|
|
for _ in range(0, digits - len(hintstr)):
|
2014-04-20 23:58:14 +02:00
|
|
|
hintstr.insert(0, chars[0])
|
|
|
|
return ''.join(hintstr)
|
|
|
|
|
|
|
|
def _draw_label(self, elem, string):
|
2014-04-21 00:24:08 +02:00
|
|
|
"""Draw a hint label over an element.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
elem: The QWebElement to use.
|
|
|
|
string: The hint string to print.
|
2014-04-21 15:45:29 +02:00
|
|
|
|
|
|
|
Return:
|
|
|
|
The newly created label elment
|
2014-04-21 00:24:08 +02:00
|
|
|
"""
|
2014-04-19 23:16:39 +02:00
|
|
|
rect = elem.geometry()
|
2014-04-22 09:35:49 +02:00
|
|
|
css = self.HINT_CSS.format(left=rect.x(), top=rect.y(),
|
|
|
|
config=config.instance)
|
2014-04-20 18:22:11 +02:00
|
|
|
doc = self._frame.documentElement()
|
2014-04-23 14:34:00 +02:00
|
|
|
# It seems impossible to create an empty QWebElement for which isNull()
|
|
|
|
# is false so we can work with it.
|
|
|
|
# As a workaround, we use appendInside() with markup as argument, and
|
|
|
|
# then use lastChild() to get a reference to it.
|
|
|
|
# See: http://stackoverflow.com/q/7364852/2085149
|
2014-04-20 23:58:14 +02:00
|
|
|
doc.appendInside('<span class="qutehint" style="{}">{}</span>'.format(
|
2014-04-22 09:35:49 +02:00
|
|
|
css, string))
|
2014-04-21 15:45:29 +02:00
|
|
|
return doc.lastChild()
|
2014-04-19 17:50:11 +02:00
|
|
|
|
2014-05-01 15:29:18 +02:00
|
|
|
def _click(self, elem):
|
2014-04-21 23:32:44 +02:00
|
|
|
"""Click an element.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
elem: The QWebElement to click.
|
|
|
|
"""
|
2014-05-01 15:29:18 +02:00
|
|
|
if self._target == 'rapid':
|
2014-04-29 09:09:42 +02:00
|
|
|
target = 'bgtab'
|
|
|
|
else:
|
|
|
|
target = self._target
|
2014-04-21 23:32:44 +02:00
|
|
|
self.set_open_target.emit(target)
|
|
|
|
point = elem.geometry().topLeft()
|
2014-05-01 15:29:18 +02:00
|
|
|
scrollpos = self._frame.scrollPosition()
|
2014-04-21 23:32:44 +02:00
|
|
|
logging.debug("Clicking on \"{}\" at {}/{} - {}/{}".format(
|
|
|
|
elem.toPlainText(), point.x(), point.y(), scrollpos.x(),
|
|
|
|
scrollpos.y()))
|
|
|
|
point -= scrollpos
|
|
|
|
events = [
|
|
|
|
QMouseEvent(QEvent.MouseMove, point, Qt.NoButton, Qt.NoButton,
|
|
|
|
Qt.NoModifier),
|
|
|
|
QMouseEvent(QEvent.MouseButtonPress, point, Qt.LeftButton,
|
|
|
|
Qt.NoButton, Qt.NoModifier),
|
|
|
|
QMouseEvent(QEvent.MouseButtonRelease, point, Qt.LeftButton,
|
|
|
|
Qt.NoButton, Qt.NoModifier),
|
|
|
|
]
|
|
|
|
for evt in events:
|
|
|
|
self.mouse_event.emit(evt)
|
|
|
|
|
2014-04-29 09:09:42 +02:00
|
|
|
def _yank(self, link):
|
2014-04-21 23:32:44 +02:00
|
|
|
"""Yank an element to the clipboard or primary selection.
|
|
|
|
|
|
|
|
Args:
|
2014-04-21 23:44:45 +02:00
|
|
|
link: The URL to open.
|
2014-04-21 23:32:44 +02:00
|
|
|
"""
|
2014-04-29 09:09:42 +02:00
|
|
|
sel = self._target == 'yank_primary'
|
2014-04-21 23:32:44 +02:00
|
|
|
mode = QClipboard.Selection if sel else QClipboard.Clipboard
|
2014-04-21 23:44:45 +02:00
|
|
|
QApplication.clipboard().setText(urlutils.urlstring(link), mode)
|
2014-04-25 16:53:23 +02:00
|
|
|
message.info("URL yanked to {}".format("primary selection" if sel
|
|
|
|
else "clipboard"))
|
2014-04-21 23:32:44 +02:00
|
|
|
|
2014-04-29 09:09:42 +02:00
|
|
|
def _preset_cmd_text(self, link):
|
|
|
|
"""Preset a commandline text based on a hint URL.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
link: The link to open.
|
|
|
|
"""
|
|
|
|
commands = {
|
|
|
|
'cmd': 'open',
|
|
|
|
'cmd_tab': 'tabopen',
|
|
|
|
'cmd_bgtab': 'backtabopen',
|
|
|
|
}
|
|
|
|
message.set_cmd_text(':{} {}'.format(commands[self._target],
|
|
|
|
urlutils.urlstring(link)))
|
|
|
|
|
2014-05-01 16:40:14 +02:00
|
|
|
def _resolve_link(self, elem, baseurl=None):
|
2014-04-21 23:53:13 +02:00
|
|
|
"""Resolve a link and check if we want to keep it.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
elem: The QWebElement to get the link of.
|
2014-05-01 16:40:14 +02:00
|
|
|
baseurl: The baseurl of the current tab (overrides self._baseurl).
|
2014-04-21 23:53:13 +02:00
|
|
|
|
|
|
|
Return:
|
|
|
|
A QUrl with the absolute link, or None.
|
|
|
|
"""
|
|
|
|
link = elem.attribute('href')
|
|
|
|
if not link:
|
|
|
|
return None
|
2014-05-01 16:40:14 +02:00
|
|
|
if baseurl is None:
|
|
|
|
baseurl = self._baseurl
|
2014-04-21 23:53:13 +02:00
|
|
|
link = urlutils.qurl(link)
|
|
|
|
if link.isRelative():
|
2014-05-01 16:40:14 +02:00
|
|
|
link = baseurl.resolved(link)
|
2014-04-21 23:53:13 +02:00
|
|
|
return link
|
|
|
|
|
2014-05-01 16:35:26 +02:00
|
|
|
def _find_prevnext(self, frame, prev=False):
|
|
|
|
"""Find a prev/next element in frame."""
|
2014-04-30 18:01:03 +02:00
|
|
|
# First check for <link rel="prev(ious)|next">
|
|
|
|
elems = frame.findAllElements(webelem.SELECTORS['prevnext_rel'])
|
|
|
|
rel_values = ['prev', 'previous'] if prev else ['next']
|
|
|
|
for e in elems:
|
|
|
|
if e.attribute('rel') in rel_values:
|
2014-05-01 16:35:26 +02:00
|
|
|
return e
|
2014-04-30 18:01:03 +02:00
|
|
|
# Then check for regular links
|
|
|
|
elems = frame.findAllElements(webelem.SELECTORS['prevnext'])
|
|
|
|
option = 'prev-regexes' if prev else 'next-regexes'
|
2014-05-01 16:35:26 +02:00
|
|
|
if not elems:
|
|
|
|
return None
|
|
|
|
for regex in config.get('hints', option):
|
|
|
|
for e in elems:
|
|
|
|
if regex.match(e.toPlainText()):
|
|
|
|
return e
|
|
|
|
return None
|
|
|
|
|
2014-05-01 16:40:14 +02:00
|
|
|
def follow_prevnext(self, frame, baseurl, prev=False, newtab=False):
|
2014-05-01 16:35:26 +02:00
|
|
|
"""Click a "previous"/"next" element on the page.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
frame: The frame where the element is in.
|
2014-05-01 16:40:14 +02:00
|
|
|
baseurl: The base URL of the current tab.
|
2014-05-01 16:35:26 +02:00
|
|
|
prev: True to open a "previous" link, False to open a "next" link.
|
|
|
|
newtab: True to open in a new tab, False for the current tab.
|
|
|
|
"""
|
|
|
|
elem = self._find_prevnext(frame, prev)
|
|
|
|
if elem is None:
|
|
|
|
message.error("No {} links found!".format("prev" if prev
|
|
|
|
else "forward"))
|
|
|
|
return
|
2014-05-01 16:40:14 +02:00
|
|
|
link = self._resolve_link(elem, baseurl)
|
2014-05-01 16:35:26 +02:00
|
|
|
if link is None:
|
|
|
|
message.error("No {} links found!".format("prev" if prev
|
|
|
|
else "forward"))
|
|
|
|
return
|
|
|
|
self.openurl.emit(link, newtab)
|
2014-04-30 18:01:03 +02:00
|
|
|
|
2014-04-25 16:53:23 +02:00
|
|
|
def start(self, frame, baseurl, mode='all', target='normal'):
|
2014-04-19 17:50:11 +02:00
|
|
|
"""Start hinting.
|
|
|
|
|
|
|
|
Args:
|
2014-04-22 14:23:55 +02:00
|
|
|
frame: The QWebFrame to place hints in.
|
2014-04-21 23:44:45 +02:00
|
|
|
baseurl: URL of the current page.
|
2014-04-19 17:50:11 +02:00
|
|
|
mode: The mode to be used.
|
2014-04-21 23:32:44 +02:00
|
|
|
target: What to do with the link. See attribute docstring.
|
|
|
|
|
|
|
|
Emit:
|
|
|
|
hint_strings_updated: Emitted to update keypraser.
|
2014-04-19 17:50:11 +02:00
|
|
|
"""
|
2014-05-02 11:25:24 +02:00
|
|
|
try:
|
|
|
|
elems = frame.findAllElements(webelem.SELECTORS[mode])
|
|
|
|
except KeyError:
|
|
|
|
message.error("Hinting mode '{}' does not exist!".format(mode))
|
|
|
|
return
|
2014-04-21 19:29:11 +02:00
|
|
|
self._target = target
|
2014-04-21 23:44:45 +02:00
|
|
|
self._baseurl = baseurl
|
2014-05-02 18:57:59 +02:00
|
|
|
if frame is None:
|
|
|
|
# This should never happen since we check frame before calling
|
|
|
|
# start. But since we had a bug where frame is None in
|
|
|
|
# on_mode_left, we are extra careful here.
|
|
|
|
raise ValueError("start() was called with frame=None")
|
2014-04-22 14:23:55 +02:00
|
|
|
self._frame = frame
|
2014-04-24 16:28:00 +02:00
|
|
|
filterfunc = webelem.FILTERS.get(mode, lambda e: True)
|
2014-04-21 15:45:29 +02:00
|
|
|
visible_elems = []
|
2014-04-20 23:02:27 +02:00
|
|
|
for e in elems:
|
2014-04-24 16:28:00 +02:00
|
|
|
if filterfunc(e) and webelem.is_visible(e, self._frame):
|
|
|
|
visible_elems.append(e)
|
2014-04-21 19:31:28 +02:00
|
|
|
if not visible_elems:
|
|
|
|
message.error("No elements found.")
|
|
|
|
return
|
2014-04-22 09:41:43 +02:00
|
|
|
texts = {
|
2014-04-25 16:53:23 +02:00
|
|
|
'normal': "Follow hint...",
|
|
|
|
'tab': "Follow hint in new tab...",
|
|
|
|
'bgtab': "Follow hint in background tab...",
|
|
|
|
'yank': "Yank hint to clipboard...",
|
|
|
|
'yank_primary': "Yank hint to primary selection...",
|
|
|
|
'cmd': "Set hint in commandline...",
|
|
|
|
'cmd_tab': "Set hint in commandline as new tab...",
|
|
|
|
'cmd_bgtab': "Set hint in commandline as background tab...",
|
|
|
|
'rapid': "Follow hint (rapid mode)...",
|
2014-04-22 09:41:43 +02:00
|
|
|
}
|
2014-05-02 11:25:24 +02:00
|
|
|
try:
|
|
|
|
message.text(texts[target])
|
|
|
|
except KeyError:
|
|
|
|
message.error("Hinting target '{}' does not exist!".format(target))
|
|
|
|
return
|
2014-04-21 15:45:29 +02:00
|
|
|
strings = self._hint_strings(visible_elems)
|
|
|
|
for e, string in zip(visible_elems, strings):
|
|
|
|
label = self._draw_label(e, string)
|
|
|
|
self._elems[string] = ElemTuple(e, label)
|
2014-04-22 14:23:55 +02:00
|
|
|
frame.contentsSizeChanged.connect(self.on_contents_size_changed)
|
2014-04-21 15:20:41 +02:00
|
|
|
self.hint_strings_updated.emit(strings)
|
2014-04-25 16:53:23 +02:00
|
|
|
modeman.enter('hint')
|
2014-04-20 17:25:46 +02:00
|
|
|
|
2014-04-21 15:20:41 +02:00
|
|
|
def handle_partial_key(self, keystr):
|
|
|
|
"""Handle a new partial keypress."""
|
2014-04-21 15:45:29 +02:00
|
|
|
delete = []
|
|
|
|
for (string, elems) in self._elems.items():
|
|
|
|
if string.startswith(keystr):
|
|
|
|
matched = string[:len(keystr)]
|
|
|
|
rest = string[len(keystr):]
|
|
|
|
elems.label.setInnerXml('<font color="{}">{}</font>{}'.format(
|
2014-04-25 16:53:23 +02:00
|
|
|
config.get('colors', 'hints.fg.match'), matched, rest))
|
2014-04-21 15:45:29 +02:00
|
|
|
else:
|
|
|
|
elems.label.removeFromDocument()
|
|
|
|
delete.append(string)
|
|
|
|
for key in delete:
|
|
|
|
del self._elems[key]
|
2014-04-21 15:20:41 +02:00
|
|
|
|
2014-05-02 17:53:16 +02:00
|
|
|
def filter_hints(self, filterstr):
|
|
|
|
"""Filter displayed hints according to a text."""
|
|
|
|
delete = []
|
|
|
|
for (string, elems) in self._elems.items():
|
|
|
|
if not elems.elem.toPlainText().lower().startswith(filterstr):
|
|
|
|
elems.label.removeFromDocument()
|
|
|
|
delete.append(string)
|
|
|
|
for key in delete:
|
|
|
|
del self._elems[key]
|
|
|
|
if not self._elems:
|
|
|
|
# Whoops, filtered all hints
|
|
|
|
modeman.leave('hint')
|
|
|
|
elif len(self._elems) == 1 and config.get('hints', 'auto-follow'):
|
|
|
|
# unpacking gets us the first (and only) key in the dict.
|
|
|
|
self.fire(*self._elems)
|
|
|
|
|
2014-04-27 21:59:23 +02:00
|
|
|
def fire(self, keystr, force=False):
|
|
|
|
"""Fire a completed hint.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
keystr: The keychain string to follow.
|
|
|
|
force: When True, follow even when auto-follow is false.
|
|
|
|
"""
|
|
|
|
if not (force or config.get('hints', 'auto-follow')):
|
|
|
|
self.handle_partial_key(keystr)
|
|
|
|
self._to_follow = keystr
|
|
|
|
return
|
2014-04-29 09:09:42 +02:00
|
|
|
# Handlers which take a QWebElement
|
|
|
|
elem_handlers = {
|
|
|
|
'normal': self._click,
|
|
|
|
'tab': self._click,
|
|
|
|
'bgtab': self._click,
|
|
|
|
'rapid': self._click,
|
|
|
|
}
|
|
|
|
# Handlers which take a link string
|
|
|
|
link_handlers = {
|
|
|
|
'yank': self._yank,
|
|
|
|
'yank_primary': self._yank,
|
|
|
|
'cmd': self._preset_cmd_text,
|
|
|
|
'cmd_tab': self._preset_cmd_text,
|
|
|
|
'cmd_bgtab': self._preset_cmd_text,
|
|
|
|
}
|
2014-04-21 16:59:03 +02:00
|
|
|
elem = self._elems[keystr].elem
|
2014-04-29 09:09:42 +02:00
|
|
|
if self._target in elem_handlers:
|
|
|
|
elem_handlers[self._target](elem)
|
|
|
|
elif self._target in link_handlers:
|
2014-04-21 23:53:13 +02:00
|
|
|
link = self._resolve_link(elem)
|
|
|
|
if link is None:
|
|
|
|
message.error("No suitable link found for this element.")
|
|
|
|
return
|
2014-04-29 09:09:42 +02:00
|
|
|
link_handlers[self._target](link)
|
2014-04-22 14:23:55 +02:00
|
|
|
if self._target != 'rapid':
|
2014-04-25 16:53:23 +02:00
|
|
|
modeman.leave('hint')
|
2014-04-22 09:35:59 +02:00
|
|
|
|
2014-04-27 21:59:23 +02:00
|
|
|
def follow_hint(self):
|
2014-04-28 00:05:14 +02:00
|
|
|
"""Follow the currently selected hint."""
|
2014-04-27 21:59:23 +02:00
|
|
|
if not self._to_follow:
|
|
|
|
message.error("No hint to follow")
|
|
|
|
self.fire(self._to_follow, force=True)
|
|
|
|
|
2014-04-22 09:35:59 +02:00
|
|
|
@pyqtSlot('QSize')
|
2014-04-22 17:53:27 +02:00
|
|
|
def on_contents_size_changed(self, _size):
|
2014-04-22 09:35:59 +02:00
|
|
|
"""Reposition hints if contents size changed."""
|
|
|
|
for elems in self._elems.values():
|
|
|
|
rect = elems.elem.geometry()
|
|
|
|
css = self.HINT_CSS.format(left=rect.x(), top=rect.y(),
|
|
|
|
config=config.instance)
|
2014-04-25 16:53:23 +02:00
|
|
|
elems.label.setAttribute('style', css)
|
2014-04-24 23:47:02 +02:00
|
|
|
|
|
|
|
@pyqtSlot(str)
|
|
|
|
def on_mode_left(self, mode):
|
|
|
|
"""Stop hinting when hinting mode was left."""
|
2014-04-25 16:53:23 +02:00
|
|
|
if mode != 'hint':
|
2014-04-24 23:47:02 +02:00
|
|
|
return
|
|
|
|
for elem in self._elems.values():
|
|
|
|
elem.label.removeFromDocument()
|
|
|
|
self._frame.contentsSizeChanged.disconnect(
|
|
|
|
self.on_contents_size_changed)
|
|
|
|
self._elems = {}
|
2014-04-27 21:59:23 +02:00
|
|
|
self._to_follow = None
|
2014-04-24 23:47:02 +02:00
|
|
|
self._target = None
|
|
|
|
self._frame = None
|
|
|
|
message.clear()
|