pixelocr/pixelocr/gui/glyphedit.py

80 lines
2.4 KiB
Python

# Copyright (C) 2014 Andrey Golovizin
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
from PyQt4.QtCore import (
signal,
slot,
)
from PyQt4.QtGui import (
QHBoxLayout,
QLineEdit,
QToolButton,
QWidget,
)
class StyleButton(QToolButton):
def __init__(self, parent):
super().__init__(parent)
self.setCheckable(True)
self.setAutoRaise(True)
def release(self):
self.setChecked(False)
class GlyphEdit(QWidget):
glyphEntered = signal([str, bool, bool])
allowBoldItalic = True
def __init__(self, parent, document):
super().__init__(parent)
self.document = document
self.layout = QHBoxLayout(self)
self.layout.setSpacing(0)
self.layout.setContentsMargins(0, 0, 0, 0)
self.bold = StyleButton(self)
self.bold.setText('B')
self.bold.setShortcut('Ctrl+b')
self.italic = StyleButton(self)
self.italic.setText('I')
self.italic.setShortcut('Ctrl+i')
self.setAllowBoldItalic(self.document.config.allow_bold_italic)
self.text = QLineEdit(self)
self.text.returnPressed.connect(self.sendGlyph)
self.layout.addWidget(self.bold)
self.layout.addWidget(self.italic)
self.layout.addWidget(self.text)
def sendGlyph(self):
self.glyphEntered.emit(self.text.text(), self.bold.isChecked(), self.italic.isChecked())
def setAllowBoldItalic(self, allow=True):
if not allow and self.allowBoldItalic:
self.italic.pressed.connect(self.bold.release)
self.bold.pressed.connect(self.italic.release)
self.allowBoldItalic = False
elif allow and not self.allowBoldItalic:
self.italic.pressed.disconnect(self.bold.release)
self.bold.pressed.disconnect(self.italic.release)
self.allowBoldItalic = True