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
|
#****************************************************************************
# unitedit.py, provides a line edit for unit entry
#
# ConvertAll, a units conversion program
# Copyright (C) 2016, Douglas W. Bell
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License, either Version 2 or any later
# version. This program is distributed in the hope that it will be useful,
# but WITTHOUT ANY WARRANTY. See the included LICENSE file for details.
#*****************************************************************************
from PyQt5.QtCore import (QEvent, Qt, pyqtSignal)
from PyQt5.QtWidgets import (QLineEdit, QWidget)
class UnitEdit(QLineEdit):
"""Text line editor for unit entry.
"""
unitChanged = pyqtSignal()
currentChanged = pyqtSignal(QWidget) # pass line edit for focus proxy
keyPressed = pyqtSignal(int) # pass key to list view for some key presses
gotFocus = pyqtSignal()
def __init__(self, unitGroup, parent=None):
super().__init__(parent)
self.unitGroup = unitGroup
self.activeEditor = False;
self.textEdited.connect(self.updateGroup)
self.cursorPositionChanged.connect(self.updateCurrentUnit)
def unitUpdate(self):
"""Update text from unit group.
"""
if not self.activeEditor:
return
newText = self.unitGroup.unitString()
cursorPos = len(newText) - len(self.text()) + self.cursorPosition()
if cursorPos < 0: # cursor set to same distance from right end
cursorPos = 0
self.blockSignals(True)
self.setText(newText)
self.setCursorPosition(cursorPos)
self.blockSignals(False)
self.unitChanged.emit()
def updateGroup(self):
"""Update unit based on edit text change (except spacing change).
"""
if (self.text().replace(' ', '') !=
self.unitGroup.unitString().replace(' ', '')):
self.unitGroup.update(self.text(), self.cursorPosition())
self.currentChanged.emit(self) # update listView
self.unitUpdate() # replace text with formatted text
def updateCurrentUnit(self):
"""Change current unit based on cursor movement.
"""
self.unitGroup.updateCurrentUnit(self.text(),
self.cursorPosition())
self.currentChanged.emit(self) # update listView
def keyPressEvent(self, event):
"""Keys for return and up/down.
"""
if event.key() in (Qt.Key_Up, Qt.Key_Down, Qt.Key_PageUp,
Qt.Key_PageDown, Qt.Key_Return, Qt.Key_Enter):
self.keyPressed.emit(event.key())
else:
super().keyPressEvent(event)
def event(self, event):
"""Catch tab press to complete unit.
"""
if (event.type() == QEvent.KeyPress and
event.key() == Qt.Key_Tab):
# self.unitGroup.completePartial()
self.currentChanged.emit(self) # update listView
self.unitUpdate()
return super().event(event)
def setInactive(self):
"""Set inactive based on a signal from another editor.
"""
self.activeEditor = False;
def focusInEvent(self, event):
"""Signal that this unit editor received focus.
"""
super().focusInEvent(event)
if not self.activeEditor:
self.activeEditor = True
self.updateCurrentUnit()
self.gotFocus.emit()
|