qutebrowser/qutebrowser/models/completion.py

138 lines
4.9 KiB
Python
Raw Normal View History

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-05-02 16:23:51 +02:00
from PyQt5.QtCore import pyqtSlot, Qt
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-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]
except (KeyError, AttributeError):
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-05-02 16:23:51 +02:00
except KeyError:
# changed before init
return
val = config.get(section, option, raw=True)
2014-05-02 16:29:01 +02:00
self.setData(item.index(), val, Qt.DisplayRole)
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-04-25 12:29:00 +02:00
"""A CompletionModel filled with setting values."""
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)
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."""
value = config.get(section, option, raw=True)
if not value:
value = '""'
self.setData(self.cur_item.index(), value, Qt.DisplayRole)
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()):
if not obj.hide:
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)