Refactor different LineEdit classes.

This commit is contained in:
Florian Bruhin 2014-08-13 06:53:36 +02:00
parent b9e4dd282c
commit 5b61487130
3 changed files with 65 additions and 36 deletions

View File

@ -20,14 +20,16 @@
"""Misc. widgets used at different places."""
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtGui import QValidator
from qutebrowser.models.cmdhistory import History
class MinimalLineEdit(QLineEdit):
class MinimalLineEditMixin:
"""A LineEdit with a minimal look (without borders/background)."""
"""A mixin to give a QLineEdit a minimal look and nicer repr()."""
def __init__(self, parent=None):
super().__init__(parent)
def __init__(self):
self.setStyleSheet("""
QLineEdit {
border: 0px;
@ -38,3 +40,46 @@ class MinimalLineEdit(QLineEdit):
def __repr__(self):
return '<{} "{}">'.format(self.__class__.__name__, self.text())
class CommandLineEdit(QLineEdit):
"""A QLineEdit with a history and prompt chars.
Attributes:
history: The command history object.
_validator: The current command validator.
"""
def __init__(self, parent, prompts):
super().__init__(parent)
self.history = History()
self._validator = _CommandValidator(prompts, parent=self)
self.setValidator(self._validator)
def __repr__(self):
return '<{} "{}">'.format(self.__class__.__name__, self.text())
class _CommandValidator(QValidator):
"""Validator to prevent the : from getting deleted."""
def __init__(self, prompts, parent=None):
super().__init__(parent)
self.prompts = prompts
def validate(self, string, pos):
"""Override QValidator::validate.
Args:
string: The string to validate.
pos: The current curser position.
Return:
A tuple (status, string, pos) as a QValidator should.
"""
if any(string.startswith(c) for c in self.prompts):
return (QValidator.Acceptable, string, pos)
else:
return (QValidator.Invalid, string, pos)

View File

@ -25,26 +25,24 @@ from PyQt5.QtGui import QValidator
import qutebrowser.keyinput.modeman as modeman
import qutebrowser.commands.utils as cmdutils
from qutebrowser.widgets.misc import MinimalLineEdit
from qutebrowser.widgets.misc import MinimalLineEditMixin, CommandLineEdit
from qutebrowser.commands.runners import CommandRunner
from qutebrowser.keyinput.modeparsers import STARTCHARS
from qutebrowser.utils.log import completion as logger
from qutebrowser.models.cmdhistory import (History, HistoryEmptyError,
from qutebrowser.models.cmdhistory import (HistoryEmptyError,
HistoryEndReachedError)
from qutebrowser.commands.exceptions import CommandError
from qutebrowser.utils.usertypes import KeyMode
class Command(MinimalLineEdit):
class Command(MinimalLineEditMixin, CommandLineEdit):
"""The commandline part of the statusbar.
Attributes:
history: The command history object.
cursor_part: The part the cursor is currently over.
parts: A list of strings with the split commandline
prefix: The prefix currently entered.
_validator: The current command validator.
Signals:
got_cmd: Emitted when a command is triggered by the user.
@ -79,12 +77,11 @@ class Command(MinimalLineEdit):
# for a possible fix.
def __init__(self, parent=None):
super().__init__(parent)
CommandLineEdit.__init__(self, parent, STARTCHARS)
MinimalLineEditMixin.__init__(self)
self.cursor_part = 0
self.history = History(QApplication.instance().cmd_history.data)
self._validator = _CommandValidator(self)
self.history.history = QApplication.instance().cmd_history.data
self._empty_item_idx = None
self.setValidator(self._validator)
self.textEdited.connect(self.on_text_edited)
self.cursorPositionChanged.connect(self._update_cursor_part)
self.cursorPositionChanged.connect(self.on_cursor_position_changed)
@ -301,23 +298,3 @@ class Command(MinimalLineEdit):
"""Extend focusInEvent to enter command mode."""
modeman.maybe_enter(KeyMode.command, 'cmd focus')
super().focusInEvent(e)
class _CommandValidator(QValidator):
"""Validator to prevent the : from getting deleted."""
def validate(self, string, pos):
"""Override QValidator::validate.
Args:
string: The string to validate.
pos: The current curser position.
Return:
A tuple (status, string, pos) as a QValidator should.
"""
if any(string.startswith(c) for c in STARTCHARS):
return (QValidator.Acceptable, string, pos)
else:
return (QValidator.Invalid, string, pos)

View File

@ -20,13 +20,20 @@
"""Prompt shown in the statusbar."""
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QHBoxLayout, QWidget
from PyQt5.QtWidgets import QHBoxLayout, QWidget, QLineEdit
from qutebrowser.widgets.misc import MinimalLineEdit
from qutebrowser.widgets.misc import MinimalLineEditMixin
from qutebrowser.widgets.statusbar.textbase import TextBase
from qutebrowser.widgets.statusbar.prompter import Prompter
class PromptLineEdit(MinimalLineEditMixin, QLineEdit):
def __init__(self, parent=None):
QLineEdit.__init__(self, parent)
MinimalLineEditMixin.__init__(self)
class Prompt(QWidget):
"""The prompt widget shown in the statusbar.
@ -54,7 +61,7 @@ class Prompt(QWidget):
self.txt = TextBase()
self._hbox.addWidget(self.txt)
self.lineedit = MinimalLineEdit()
self.lineedit = PromptLineEdit()
self._hbox.addWidget(self.lineedit)
self.prompter = Prompter(self)