summaryrefslogtreecommitdiff
path: root/qutebrowser/completion/completionwidget.py
blob: f042be0a1c3f3bfe62383a6d93300f9ee7df464f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# SPDX-FileCopyrightText: Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""Completion view for statusbar command section.

Defines a CompletionView which uses CompletionFiterModel and CompletionModel
subclasses to provide completions.
"""

from typing import TYPE_CHECKING, Optional

from qutebrowser.qt.widgets import QTreeView, QSizePolicy, QStyleFactory, QWidget
from qutebrowser.qt.core import pyqtSlot, pyqtSignal, Qt, QItemSelectionModel, QSize

from qutebrowser.config import config, stylesheet
from qutebrowser.completion import completiondelegate
from qutebrowser.completion.models import completionmodel
from qutebrowser.utils import utils, usertypes, debug, log, qtutils
from qutebrowser.api import cmdutils
if TYPE_CHECKING:
    from qutebrowser.mainwindow.statusbar import command


class CompletionView(QTreeView):

    """The view showing available completions.

    Based on QTreeView but heavily customized so root elements show as category
    headers, and children show as flat list.

    Attributes:
        pattern: Current filter pattern, used for highlighting.
        _win_id: The ID of the window this CompletionView is associated with.
        _height: The height to use for the CompletionView.
        _height_perc: Either None or a percentage if height should be relative.
        _delegate: The item delegate used.
        _column_widths: A list of column widths, in percent.
        _active: Whether a selection is active.
        _cmd: The statusbar Command object.

    Signals:
        update_geometry: Emitted when the completion should be resized.
        selection_changed: Emitted when the completion item selection changes.
    """

    # Drawing the item foreground will be done by CompletionItemDelegate, so we
    # don't define that in this stylesheet.
    STYLESHEET = """
        QTreeView {
            font: {{ conf.fonts.completion.entry }};
            background-color: {{ conf.colors.completion.even.bg }};
            alternate-background-color: {{ conf.colors.completion.odd.bg }};
            outline: 0;
            border: 0px;
        }

        QTreeView::item:disabled {
            background-color: {{ conf.colors.completion.category.bg }};
            border-top: 1px solid
                {{ conf.colors.completion.category.border.top }};
            border-bottom: 1px solid
                {{ conf.colors.completion.category.border.bottom }};
        }

        QTreeView::item:selected, QTreeView::item:selected:hover {
            border-top: 1px solid
                {{ conf.colors.completion.item.selected.border.top }};
            border-bottom: 1px solid
                {{ conf.colors.completion.item.selected.border.bottom }};
            background-color: {{ conf.colors.completion.item.selected.bg }};
        }

        QTreeView:item::hover {
            border: 0px;
        }

        QTreeView QScrollBar {
            width: {{ conf.completion.scrollbar.width }}px;
            background: {{ conf.colors.completion.scrollbar.bg }};
        }

        QTreeView QScrollBar::handle {
            background: {{ conf.colors.completion.scrollbar.fg }};
            border: {{ conf.completion.scrollbar.padding }}px solid
                    {{ conf.colors.completion.scrollbar.bg }};
            min-height: 10px;
        }

        QTreeView QScrollBar::sub-line, QScrollBar::add-line {
            border: none;
            background: none;
        }
    """

    update_geometry = pyqtSignal()
    selection_changed = pyqtSignal(str)

    def __init__(self, *,
                 cmd: 'command.Command',
                 win_id: int,
                 parent: QWidget = None) -> None:
        super().__init__(parent)
        self.pattern: Optional[str] = None
        self._win_id = win_id
        self._cmd = cmd
        self._active = False

        config.instance.changed.connect(self._on_config_changed)

        self._delegate = completiondelegate.CompletionItemDelegate(self)
        self.setItemDelegate(self._delegate)
        self.setStyle(QStyleFactory.create('Fusion'))
        stylesheet.set_register(self)
        self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
        self.setHeaderHidden(True)
        self.setAlternatingRowColors(True)
        self.setIndentation(0)
        self.setItemsExpandable(False)
        self.setExpandsOnDoubleClick(False)
        self.setAnimated(False)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        # WORKAROUND
        # This is a workaround for weird race conditions with invalid
        # item indexes leading to segfaults in Qt.
        #
        # Some background: https://bugs.quassel-irc.org/issues/663
        # The proposed fix there was later reverted because it didn't help.
        self.setUniformRowHeights(True)
        self.hide()
        # FIXME set elidemode
        # https://github.com/qutebrowser/qutebrowser/issues/118

    def __repr__(self):
        return utils.get_repr(self)

    def _model(self) -> completionmodel.CompletionModel:
        """Get the current completion model.

        Ensures the model is not None.
        """
        model = self.model()
        assert isinstance(model, completionmodel.CompletionModel), model
        return model

    def _selection_model(self) -> QItemSelectionModel:
        """Get the current selection model.

        Ensures the model is not None.
        """
        model = self.selectionModel()
        assert model is not None
        return model

    @pyqtSlot(str)
    def _on_config_changed(self, option):
        if option in ['completion.height', 'completion.shrink']:
            self.update_geometry.emit()

    def _resize_columns(self):
        """Resize the completion columns based on column_widths."""
        if self.model() is None:
            return
        width = self.size().width()
        column_widths = self._model().column_widths
        pixel_widths = [(width * perc // 100) for perc in column_widths]

        bar = self.verticalScrollBar()
        assert bar is not None
        delta = bar.sizeHint().width()
        for i, width in reversed(list(enumerate(pixel_widths))):
            if width > delta:
                pixel_widths[i] -= delta
                break

        for i, w in enumerate(pixel_widths):
            assert w >= 0, (i, w)
            self.setColumnWidth(i, w)

    def _next_idx(self, upwards):
        """Get the previous/next QModelIndex displayed in the view.

        Used by tab_handler.

        Args:
            upwards: Get previous item, not next.

        Return:
            A QModelIndex.
        """
        model = self._model()
        idx = self._selection_model().currentIndex()
        if not idx.isValid():
            # No item selected yet
            if upwards:
                return model.last_item()
            else:
                return model.first_item()

        while True:
            idx = self.indexAbove(idx) if upwards else self.indexBelow(idx)
            # wrap around if we arrived at beginning/end
            if not idx.isValid() and upwards:
                return model.last_item()
            elif not idx.isValid() and not upwards:
                idx = model.first_item()
                self.scrollTo(idx.parent())
                return idx
            elif idx.parent().isValid():
                # Item is a real item, not a category header -> success
                return idx

        raise utils.Unreachable

    def _next_page(self, upwards):
        """Return the index a page away from the selected index.

        Args:
            upwards: Get previous item, not next.

        Return:
            A QModelIndex.
        """
        old_idx = self._selection_model().currentIndex()
        idx = old_idx
        model = self._model()

        if not idx.isValid():
            # No item selected yet
            return model.last_item() if upwards else model.first_item()

        # Find height of each CompletionView element
        rect = self.visualRect(idx)
        qtutils.ensure_valid(rect)
        page_length = self.height() // rect.height()

        # Skip one pageful, except leave one old line visible
        offset = -(page_length - 1) if upwards else page_length - 1
        idx = model.sibling(old_idx.row() + offset, old_idx.column(), old_idx)

        # Skip category headers
        while idx.isValid() and not idx.parent().isValid():
            idx = self.indexAbove(idx) if upwards else self.indexBelow(idx)

        if idx.isValid():
            return idx

        border_item = model.first_item() if upwards else model.last_item()

        # Wrap around if we were already at the beginning/end
        if old_idx == border_item:
            return self._next_idx(upwards)

        # Select the first/last item before wrapping around
        if upwards:
            self.scrollTo(border_item.parent())
        return border_item

    def _next_category_idx(self, upwards):
        """Get the index of the previous/next category.

        Args:
            upwards: Get previous item, not next.

        Return:
            A QModelIndex.
        """
        idx = self._selection_model().currentIndex()
        model = self._model()
        if not idx.isValid():
            return self._next_idx(upwards).sibling(0, 0)
        idx = idx.parent()
        direction = -1 if upwards else 1
        while True:
            idx = idx.sibling(idx.row() + direction, 0)

            if idx.isValid():
                child = model.index(0, 0, idx)
                if child.isValid():
                    self.scrollTo(idx)  # scroll to ensure the category is visible
                    return child
            elif upwards:
                # wrap around to the first item of the last category
                return model.last_item().sibling(0, 0)
            else:
                # wrap around to the first item of the first category
                idx = model.first_item()
                self.scrollTo(idx.parent())
                return idx

        raise utils.Unreachable

    @cmdutils.register(instance='completion',
                       modes=[usertypes.KeyMode.command], scope='window')
    @cmdutils.argument('which', choices=['next', 'prev',
                                         'next-category', 'prev-category',
                                         'next-page', 'prev-page'])
    @cmdutils.argument('history', flag='H')
    def completion_item_focus(self, which, history=False):
        """Shift the focus of the completion menu to another item.

        Args:
            which: 'next', 'prev',
                   'next-category', 'prev-category',
                   'next-page', or 'prev-page'.
            history: Navigate through command history if no text was typed.
        """
        if history:
            if (self._cmd.text() == ':' or self._cmd.history.is_browsing() or
                    not self._active):
                if which == 'next':
                    self._cmd.command_history_next()
                    return
                elif which == 'prev':
                    self._cmd.command_history_prev()
                    return
                else:
                    raise cmdutils.CommandError("Can't combine --history with "
                                                "{}!".format(which))

        if not self._active:
            return

        selmodel = self._selection_model()
        indices = {
            'next': lambda: self._next_idx(upwards=False),
            'prev': lambda: self._next_idx(upwards=True),
            'next-category': lambda: self._next_category_idx(upwards=False),
            'prev-category': lambda: self._next_category_idx(upwards=True),
            'next-page': lambda: self._next_page(upwards=False),
            'prev-page': lambda: self._next_page(upwards=True),
        }
        idx = indices[which]()

        if not idx.isValid():
            return

        selmodel.setCurrentIndex(
            idx,
            QItemSelectionModel.SelectionFlag.ClearAndSelect |
            QItemSelectionModel.SelectionFlag.Rows)

        # if the last item is focused, try to fetch more
        next_idx = self.indexBelow(idx)
        if not self.visualRect(next_idx).isValid():
            self.expandAll()

        count = self._model().count()
        if count == 0:
            self.hide()
        elif count == 1 and config.val.completion.quick:
            self.hide()
        elif config.val.completion.show == 'auto':
            self.show()

    def set_model(self, model):
        """Switch completion to a new model.

        Called from on_update_completion().

        Args:
            model: The model to use.
        """
        old_model = self.model()
        if old_model is not None and model is not old_model:
            old_model.deleteLater()
            self._selection_model().deleteLater()

        self.setModel(model)

        if model is None:
            self._active = False
            self.hide()
            return

        model.setParent(self)
        self._active = True
        self.pattern = None
        self._maybe_show()

        self._resize_columns()
        for i in range(model.rowCount()):
            self.expand(model.index(i, 0))

    def set_pattern(self, pattern: str) -> None:
        """Set the pattern on the underlying model."""
        if not self.model():
            return
        if self.pattern == pattern:
            # no changes, abort
            log.completion.debug(
                "Ignoring pattern set request as pattern has not changed.")
            return
        self.pattern = pattern
        with debug.log_time(log.completion, 'Set pattern {}'.format(pattern)):
            self._model().set_pattern(pattern)
            self._selection_model().clear()
            self._maybe_update_geometry()
            self._maybe_show()

    def _maybe_show(self):
        if (config.val.completion.show == 'always' and
                self._model().count() > 0):
            self.show()
        else:
            self.hide()

    def _maybe_update_geometry(self):
        """Emit the update_geometry signal if the config says so."""
        if config.val.completion.shrink:
            self.update_geometry.emit()

    @pyqtSlot()
    def on_clear_completion_selection(self):
        """Clear the selection model when an item is activated."""
        self.hide()
        selmod = self._selection_model()
        if selmod is not None:
            selmod.clearSelection()
            selmod.clearCurrentIndex()

    def sizeHint(self):
        """Get the completion size according to the config."""
        # Get the configured height/percentage.
        confheight = str(config.val.completion.height)
        if confheight.endswith('%'):
            perc = int(confheight.rstrip('%'))
            window = self.window()
            assert window is not None
            height = window.height() * perc // 100
        else:
            height = int(confheight)
        # Shrink to content size if needed and shrinking is enabled
        if config.val.completion.shrink:
            bar = self.horizontalScrollBar()
            assert bar is not None
            contents_height = (
                self.viewportSizeHint().height() +
                bar.sizeHint().height())
            if contents_height <= height:
                height = contents_height
        # The width isn't really relevant as we're expanding anyways.
        return QSize(-1, height)

    def selectionChanged(self, selected, deselected):
        """Extend selectionChanged to call completers selection_changed."""
        if not self._active:
            return
        super().selectionChanged(selected, deselected)
        indexes = selected.indexes()
        if not indexes:
            return
        data = str(self._model().data(indexes[0]))
        self.selection_changed.emit(data)

    def resizeEvent(self, e):
        """Extend resizeEvent to adjust column size."""
        super().resizeEvent(e)
        self._resize_columns()

    def showEvent(self, e):
        """Adjust the completion size and scroll when it's freshly shown."""
        self.update_geometry.emit()
        scrollbar = self.verticalScrollBar()
        if scrollbar is not None:
            scrollbar.setValue(scrollbar.minimum())
        super().showEvent(e)

    @cmdutils.register(instance='completion',
                       modes=[usertypes.KeyMode.command], scope='window')
    def completion_item_del(self):
        """Delete the current completion item."""
        index = self.currentIndex()
        if not index.isValid():
            raise cmdutils.CommandError("No item selected!")
        self._model().delete_cur_item(index)

    @cmdutils.register(instance='completion',
                       modes=[usertypes.KeyMode.command], scope='window')
    def completion_item_yank(self, sel=False):
        """Yank the current completion item into the clipboard.

        Args:
            sel: Use the primary selection instead of the clipboard.
        """
        text = self._cmd.selectedText()
        if not text:
            index = self.currentIndex()
            if not index.isValid():
                raise cmdutils.CommandError("No item selected!")
            text = self._model().data(index)

        if not utils.supports_selection():
            sel = False

        utils.set_clipboard(text, selection=sel)