pixelocr/pixelocr/gui/glyphdbedit.py
2014-08-25 16:25:00 +02:00

126 lines
3.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/>.
import sys
from collections import defaultdict
from PyQt4.QtCore import (
Qt,
QAbstractTableModel,
QSize,
)
from PyQt4.QtGui import (
QApplication,
QTableView,
)
from ..glyphdb import GlyphDB
from ..image import Image
class GlyphDBModel(QAbstractTableModel):
def __init__(self, glyphdb, parent=None):
super().__init__(parent)
self.glyphdb = glyphdb
self.keys = list(glyphdb.keys())
self.images = {}
def rowCount(self, parent):
return len(self.keys)
def columnCount(self, parent):
return 1
def _deserialize_image(self, key):
elevation_, serialized_image = key
return Image.deserialize(serialized_image).toqimage()
def get_image(self, key):
try:
image = self.images[key]
except KeyError:
image = self._deserialize_image(key)
self.images[key] = image
return image
def headerData(self, section, orientation, role):
if orientation != Qt.Horizontal or role != Qt.DisplayRole:
return None
if section == 0:
return 'Text'
def data(self, index, role):
if index.column() == 0:
key = self.keys[index.row()]
if role == Qt.DisplayRole:
return self.glyphdb[key]
elif role == Qt.DecorationRole:
return self.get_image(key)
def removeRows(self, row, count, parent=None):
self.beginRemoveRows(parent, row, row + count - 1)
keys = self.keys[row: row + count]
for key in keys:
del self.glyphdb[key]
del self.keys[row]
self.endRemoveRows()
return True
def sort(self, column, order):
if column == 0:
self.layoutAboutToBeChanged.emit()
self.keys.sort(key=lambda key: self.glyphdb[key], reverse = (order == Qt.DescendingOrder))
self.layoutChanged.emit()
class GlyphTableView(QTableView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setSortingEnabled(True)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Delete:
for index in self.selectedIndexes():
self.model().removeRow(index.row())
else:
return super().keyPressEvent(event)
class GlyphDBEdit(GlyphTableView):
def __init__(self, glyphdb, *args, **kwargs):
super().__init__(*args, **kwargs)
model = GlyphDBModel(glyphdb, parent=self)
self.setModel(model)
def sizeHint(self):
return QSize(640, 480)
def main(argv):
dirname = argv[1]
app = QApplication(argv)
glyphdb = GlyphDB(dirname)
win = GlyphDBEdit(glyphdb)
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main(sys.argv)