qutebrowser/qutebrowser/models/completion.py

168 lines
6.1 KiB
Python
Raw Normal View History

2014-06-21 22:41:04 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2014-06-19 09:04:37 +02:00
2014-02-06 14:01:23 +01: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/>.
2014-01-29 15:30:19 +01:00
2014-04-25 12:29:00 +02:00
"""CompletionModels for different usages."""
2014-04-17 17:44:27 +02:00
2014-06-16 09:44:11 +02:00
from PyQt5.QtCore import pyqtSlot, Qt, QCoreApplication
2014-05-02 16:23:51 +02:00
2014-05-02 15:22:45 +02:00
import qutebrowser.config.config as config
2014-04-25 12:29:00 +02:00
import qutebrowser.config.configdata as configdata
2014-06-04 07:16:34 +02:00
from qutebrowser.models.basecompletion import BaseCompletionModel
2014-04-25 12:29:00 +02:00
from qutebrowser.commands.utils import cmd_dict
2014-06-20 23:26:19 +02:00
from qutebrowser.utils.log import completion as logger
2014-06-23 07:45:04 +02:00
from qutebrowser.utils.qt import qt_ensure_valid
2014-01-27 21:42:00 +01:00
2014-04-25 12:29:00 +02:00
class SettingSectionCompletionModel(BaseCompletionModel):
2014-01-28 23:04:02 +01:00
2014-04-25 12:29:00 +02:00
"""A CompletionModel filled with settings sections."""
2014-04-09 17:57:00 +02:00
2014-04-25 12:29:00 +02:00
# pylint: disable=abstract-method
2014-01-29 15:30:19 +01:00
2014-01-27 21:42:00 +01:00
def __init__(self, parent=None):
super().__init__(parent)
2014-05-04 18:06:25 +02:00
cat = self.new_category("Sections")
2014-04-25 12:29:00 +02:00
for name in configdata.DATA.keys():
desc = configdata.SECTION_DESC[name].splitlines()[0].strip()
self.new_item(cat, name, desc)
2014-02-07 20:21:50 +01:00
2014-02-19 10:58:32 +01:00
2014-04-25 12:29:00 +02:00
class SettingOptionCompletionModel(BaseCompletionModel):
2014-01-27 21:42:00 +01:00
2014-05-02 16:23:11 +02:00
"""A CompletionModel filled with settings and their descriptions.
Attributes:
2014-05-02 16:53:37 +02:00
_section: The section of this model.
_misc_items: A dict of the misc. column items which will be set later.
2014-05-02 16:23:11 +02:00
"""
2014-01-29 15:30:19 +01:00
2014-04-25 12:29:00 +02:00
# pylint: disable=abstract-method
2014-02-07 20:21:50 +01:00
2014-04-25 12:29:00 +02:00
def __init__(self, section, parent=None):
super().__init__(parent)
2014-05-04 18:06:25 +02:00
cat = self.new_category(section)
2014-04-25 12:29:00 +02:00
sectdata = configdata.DATA[section]
2014-05-02 16:53:37 +02:00
self._misc_items = {}
self._section = section
2014-04-25 12:29:00 +02:00
for name, _ in sectdata.items():
try:
desc = sectdata.descriptions[name]
2014-06-23 07:12:19 +02:00
except (KeyError, AttributeError):
2014-06-22 23:55:48 +02:00
# Some stuff (especially ValueList items) don't have a
# description.
2014-04-25 12:29:00 +02:00
desc = ""
2014-05-02 16:23:11 +02:00
value = config.get(section, name, raw=True)
_valitem, _descitem, miscitem = self.new_item(cat, name, desc,
value)
2014-05-02 16:53:37 +02:00
self._misc_items[name] = miscitem
2014-01-29 15:30:19 +01:00
2014-05-02 16:23:51 +02:00
@pyqtSlot(str, str)
def on_config_changed(self, section, option):
2014-05-02 17:53:59 +02:00
"""Update misc column when config changed."""
2014-05-02 16:53:37 +02:00
if section != self._section:
return
2014-05-02 16:23:51 +02:00
try:
2014-05-02 16:53:37 +02:00
item = self._misc_items[option]
2014-06-21 22:40:31 +02:00
except KeyError:
logger.debug("Couldn't get item {}.{} from model!".format(
section, option))
2014-05-02 16:23:51 +02:00
# changed before init
return
val = config.get(section, option, raw=True)
2014-06-22 23:32:49 +02:00
idx = item.index()
qt_ensure_valid(idx)
2014-06-22 23:33:32 +02:00
ok = self.setData(idx, val, Qt.DisplayRole)
if not ok:
raise ValueError("Setting data failed! (section: {}, option: {}, "
"value: {})".format(section, option, val))
2014-05-02 16:23:51 +02:00
2014-02-19 10:58:32 +01:00
2014-04-25 12:29:00 +02:00
class SettingValueCompletionModel(BaseCompletionModel):
2014-02-19 10:58:32 +01:00
2014-06-23 16:43:04 +02:00
"""A CompletionModel filled with setting values.
Attributes:
_section: The section of this model.
_option: The option of this model.
"""
2014-01-27 21:42:00 +01:00
2014-04-25 12:29:00 +02:00
# pylint: disable=abstract-method
2014-01-29 15:30:19 +01:00
def __init__(self, section, option=None, parent=None):
2014-04-25 12:29:00 +02:00
super().__init__(parent)
2014-06-23 16:43:04 +02:00
self._section = section
self._option = option
cur_cat = self.new_category("Current", sort=0)
value = config.get(section, option, raw=True)
if not value:
value = '""'
self.cur_item, _descitem, _miscitem = self.new_item(cur_cat, value,
"Current value")
if hasattr(configdata.DATA[section], 'valtype'):
# Same type for all values (ValueList)
vals = configdata.DATA[section].valtype.complete()
else:
if option is None:
raise ValueError("option may only be None for ValueList "
"sections, but {} is not!".format(section))
# Different type for each value (KeyValue)
vals = configdata.DATA[section][option].typ.complete()
if vals is not None:
cat = self.new_category("Allowed", sort=1)
for (val, desc) in vals:
self.new_item(cat, val, desc)
2014-02-19 10:58:32 +01:00
@pyqtSlot(str, str)
def on_config_changed(self, section, option):
"""Update current value when config changed."""
2014-06-23 16:43:04 +02:00
if (section, option) != (self._section, self._option):
return
value = config.get(section, option, raw=True)
if not value:
value = '""'
2014-06-22 23:32:49 +02:00
idx = self.cur_item.index()
qt_ensure_valid(idx)
2014-06-22 23:33:32 +02:00
ok = self.setData(idx, value, Qt.DisplayRole)
if not ok:
raise ValueError("Setting data failed! (section: {}, option: {}, "
"value: {})".format(section, option, value))
2014-01-27 21:42:00 +01:00
2014-04-25 12:29:00 +02:00
class CommandCompletionModel(BaseCompletionModel):
2014-02-19 10:58:32 +01:00
2014-04-25 12:29:00 +02:00
"""A CompletionModel filled with all commands and descriptions."""
2014-01-27 21:42:00 +01:00
2014-04-25 12:29:00 +02:00
# pylint: disable=abstract-method
2014-02-19 10:58:32 +01:00
2014-04-25 12:29:00 +02:00
def __init__(self, parent=None):
super().__init__(parent)
assert cmd_dict
cmdlist = []
for obj in set(cmd_dict.values()):
2014-06-16 09:44:11 +02:00
if obj.hide or (obj.debug and not
QCoreApplication.instance().args.debug):
pass
else:
2014-04-25 12:29:00 +02:00
cmdlist.append((obj.name, obj.desc))
2014-05-05 17:56:14 +02:00
for name, cmd in config.section('aliases').items():
2014-05-13 22:18:59 +02:00
cmdlist.append((name, "Alias for '{}'".format(cmd)))
2014-04-25 12:29:00 +02:00
cat = self.new_category("Commands")
for (name, desc) in sorted(cmdlist):
self.new_item(cat, name, desc)