Merge branch 'dynamic-column-widths' of git://github.com/acogneau/qutebrowser into acogneau-dynamic-column-widths

This commit is contained in:
Florian Bruhin 2015-08-12 18:20:01 +02:00
commit 6f4141956b
6 changed files with 77 additions and 6 deletions

View File

@ -48,6 +48,8 @@ Changed
mode and is not hidden anymore. mode and is not hidden anymore.
- `minimal_webkit_testbrowser.py` now has a `--webengine` switch to test - `minimal_webkit_testbrowser.py` now has a `--webengine` switch to test
QtWebEngine if it's installed. QtWebEngine if it's installed.
- The column width percentages for the completion view now depend on the
completion model.
Fixed Fixed
~~~~~ ~~~~~

View File

@ -28,6 +28,7 @@ from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QItemSelectionModel
from qutebrowser.config import config, style from qutebrowser.config import config, style
from qutebrowser.completion import completiondelegate, completer from qutebrowser.completion import completiondelegate, completer
from qutebrowser.completion.models import base
from qutebrowser.utils import qtutils, objreg, utils from qutebrowser.utils import qtutils, objreg, utils
@ -38,15 +39,13 @@ class CompletionView(QTreeView):
Based on QTreeView but heavily customized so root elements show as category Based on QTreeView but heavily customized so root elements show as category
headers, and children show as flat list. headers, and children show as flat list.
Class attributes:
COLUMN_WIDTHS: A list of column widths, in percent.
Attributes: Attributes:
enabled: Whether showing the CompletionView is enabled. enabled: Whether showing the CompletionView is enabled.
_win_id: The ID of the window this CompletionView is associated with. _win_id: The ID of the window this CompletionView is associated with.
_height: The height to use for the CompletionView. _height: The height to use for the CompletionView.
_height_perc: Either None or a percentage if height should be relative. _height_perc: Either None or a percentage if height should be relative.
_delegate: The item delegate used. _delegate: The item delegate used.
_column_widths: A list of column widths, in percent.
Signals: Signals:
resize_completion: Emitted when the completion should be resized. resize_completion: Emitted when the completion should be resized.
@ -82,7 +81,6 @@ class CompletionView(QTreeView):
border: 0px; border: 0px;
} }
""" """
COLUMN_WIDTHS = (20, 70, 10)
# FIXME style scrollbar # FIXME style scrollbar
# https://github.com/The-Compiler/qutebrowser/issues/117 # https://github.com/The-Compiler/qutebrowser/issues/117
@ -103,6 +101,8 @@ class CompletionView(QTreeView):
# FIXME handle new aliases. # FIXME handle new aliases.
# objreg.get('config').changed.connect(self.init_command_completion) # objreg.get('config').changed.connect(self.init_command_completion)
self._column_widths = base.BaseCompletionModel.COLUMN_WIDTHS
self._delegate = completiondelegate.CompletionItemDelegate(self) self._delegate = completiondelegate.CompletionItemDelegate(self)
self.setItemDelegate(self._delegate) self.setItemDelegate(self._delegate)
style.set_register_stylesheet(self) style.set_register_stylesheet(self)
@ -128,9 +128,9 @@ class CompletionView(QTreeView):
return utils.get_repr(self) return utils.get_repr(self)
def _resize_columns(self): def _resize_columns(self):
"""Resize the completion columns based on COLUMN_WIDTHS.""" """Resize the completion columns based on column_widths."""
width = self.size().width() width = self.size().width()
pixel_widths = [(width * perc // 100) for perc in self.COLUMN_WIDTHS] pixel_widths = [(width * perc // 100) for perc in self._column_widths]
if self.verticalScrollBar().isVisible(): if self.verticalScrollBar().isVisible():
pixel_widths[-1] -= self.style().pixelMetric( pixel_widths[-1] -= self.style().pixelMetric(
QStyle.PM_ScrollBarExtent) + 5 QStyle.PM_ScrollBarExtent) + 5
@ -203,6 +203,8 @@ class CompletionView(QTreeView):
sel_model.deleteLater() sel_model.deleteLater()
for i in range(model.rowCount()): for i in range(model.rowCount()):
self.expand(model.index(i, 0)) self.expand(model.index(i, 0))
self._column_widths = model.srcmodel.COLUMN_WIDTHS
self._resize_columns() self._resize_columns()
self.maybe_resize_completion() self.maybe_resize_completion()

View File

@ -39,8 +39,14 @@ class BaseCompletionModel(QStandardItemModel):
Used for showing completions later in the CompletionView. Supports setting Used for showing completions later in the CompletionView. Supports setting
marks and adding new categories/items easily. marks and adding new categories/items easily.
Class Attributes:
COLUMN_WIDTHS: The width percentages of the columns used in the
completion view.
""" """
COLUMN_WIDTHS = (30, 70, 0)
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
self.setColumnCount(3) self.setColumnCount(3)

View File

@ -32,6 +32,8 @@ class SettingSectionCompletionModel(base.BaseCompletionModel):
# pylint: disable=abstract-method # pylint: disable=abstract-method
COLUMN_WIDTHS = (20, 70, 10)
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
cat = self.new_category("Sections") cat = self.new_category("Sections")
@ -51,6 +53,8 @@ class SettingOptionCompletionModel(base.BaseCompletionModel):
# pylint: disable=abstract-method # pylint: disable=abstract-method
COLUMN_WIDTHS = (20, 70, 10)
def __init__(self, section, parent=None): def __init__(self, section, parent=None):
super().__init__(parent) super().__init__(parent)
cat = self.new_category(section) cat = self.new_category(section)
@ -104,6 +108,8 @@ class SettingValueCompletionModel(base.BaseCompletionModel):
# pylint: disable=abstract-method # pylint: disable=abstract-method
COLUMN_WIDTHS = (20, 70, 10)
def __init__(self, section, option, parent=None): def __init__(self, section, option, parent=None):
super().__init__(parent) super().__init__(parent)
self._section = section self._section = section

View File

@ -40,6 +40,8 @@ class UrlCompletionModel(base.BaseCompletionModel):
TEXT_COLUMN = 1 TEXT_COLUMN = 1
TIME_COLUMN = 2 TIME_COLUMN = 2
COLUMN_WIDTHS = (40, 50, 10)
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)

View File

@ -0,0 +1,53 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2015 Alexander Cogneau <alexander.cogneau@gmail.com>
#
# 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/>.
"""Tests for qutebrowser.completion.models column widths"""
import pytest
from qutebrowser.completion.models.base import BaseCompletionModel
from qutebrowser.completion.models.configmodel import (
SettingOptionCompletionModel, SettingSectionCompletionModel,
SettingValueCompletionModel)
from qutebrowser.completion.models.miscmodels import (
CommandCompletionModel, HelpCompletionModel, QuickmarkCompletionModel,
BookmarkCompletionModel, SessionCompletionModel)
from qutebrowser.completion.models.urlmodel import UrlCompletionModel
class TestColumnWidths:
"""Tests for the column widths of the completion models"""
CLASSES = [BaseCompletionModel, SettingOptionCompletionModel,
SettingOptionCompletionModel, SettingSectionCompletionModel,
SettingValueCompletionModel, CommandCompletionModel,
HelpCompletionModel, QuickmarkCompletionModel,
BookmarkCompletionModel, SessionCompletionModel,
UrlCompletionModel]
@pytest.mark.parametrize("model", CLASSES)
def test_list_size(self, model):
"""Test if there are 3 items in the COLUMN_WIDTHS property"""
assert len(model.COLUMN_WIDTHS) == 3
@pytest.mark.parametrize("model", CLASSES)
def test_column_width_sum(self, model):
"""Test if the sum of the widths asserts to 100"""
assert sum(model.COLUMN_WIDTHS) == 100