pixelocr/pixelocr/gui/window.py
2014-09-09 21:05:28 +02:00

129 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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,
Qt,
QSettings,
QSize,
)
from PyQt4.QtGui import (
qApp,
QAction,
QKeySequence,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
from .pageview import PageScene, PageView
from .glyphedit import GlyphEdit
from .glyphdbedit import GlyphDBEdit
class MainWindow(QMainWindow):
BASE_TITLE = 'PixelOCR'
dbedit = None
glyphEntered = signal([str, bool, bool])
def __init__(self, ocr):
super().__init__()
self.setDocumentTitle(None)
self.ocr = ocr
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
self.pageScene = PageScene(self)
self.page = PageView(self.pageScene, centralWidget)
self.glyphEdit = GlyphEdit(centralWidget)
self.glyphEdit.setEnabled(False)
self.glyphDBEdit = GlyphDBEdit(self.ocr.glyphdb)
self.glyphEdit.glyphEntered.connect(self.unknownGlyphEntered)
self.glyphEdit.glyphEntered.connect(self.pageScene.clearHighlight)
self.glyphEdit.glyphEntered.connect(self.glyphDBEdit.updateData)
ocr.pageChanged.connect(self.pageScene.setPage)
ocr.pageChanged.connect(self.showPageTitle)
ocr.unknownGlyph.connect(self.unknownGlyph)
self.page.setFocusProxy(self.glyphEdit)
layout = QVBoxLayout(centralWidget)
layout.setSpacing(0)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.page)
layout.addWidget(self.glyphEdit)
self.createMenus()
self.readSettings()
def createMenus(self):
fileMenu = self.menuBar().addMenu('&File')
quit = QAction('Quit', self)
quit.setShortcut(QKeySequence.Quit)
quit.triggered.connect(qApp.closeAllWindows)
fileMenu.addAction(quit)
fileMenu = self.menuBar().addMenu('&Tools')
glyphDBEdit = QAction('Edit glyph database', self)
glyphDBEdit.triggered.connect(self.showGlyphDBEdit)
fileMenu.addAction(glyphDBEdit)
def sizeHint(self):
return QSize(800, 600)
def closeEvent(self, event):
self.saveSettings()
super().closeEvent(event)
def saveSettings(self):
settings = QSettings()
settings.setValue('windowGeometry', self.saveGeometry())
settings.setValue('windowState', self.saveState())
def readSettings(self):
settings = QSettings()
self.restoreGeometry(settings.value("windowGeometry") or b'')
self.restoreState(settings.value("windowState") or b'')
def unknownGlyph(self, glyph):
self.pageScene.highlightGlyph(glyph)
self.glyphEdit.setEnabled(True)
self.glyphEdit.text.clear()
self.glyphEdit.text.setFocus()
def unknownGlyphEntered(self, text, bold, italic):
self.glyphEdit.setEnabled(False)
self.glyphEdit.text.clear()
self.glyphEntered.emit(text, bold, italic)
def showGlyphDBEdit(self):
self.glyphDBEdit.show()
def setDocumentTitle(self, title):
if title:
full_title = '{} {}'.format(title, self.BASE_TITLE)
else:
full_title = self.BASE_TITLE
self.setWindowTitle(full_title)
def showPageTitle(self, page):
self.setDocumentTitle(page.filename)