forked from seeebek/EliteOCR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editordialog.py
76 lines (64 loc) · 3.14 KB
/
editordialog.py
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
# -*- coding: utf-8 -*-
from PyQt4.QtGui import QDialog, QTableWidgetItem
from editorUI import Ui_Editor
import os
import json
import codecs
class EditorDialog(QDialog, Ui_Editor):
def __init__(self, settings):
QDialog.__init__(self)
self.setupUi(self)
self.settings = settings
self.save.clicked.connect(self.saveCommodities)
self.add_button.clicked.connect(self.addCommodity)
self.delete_button.clicked.connect(self.deleteCommodity)
file = codecs.open(self.settings.app_path + ""+ os.sep +"commodities.json", 'r', "utf-8")
file_content = file.read()
commdict = json.loads(file_content)
file.close()
# WTF? Clean it!
titles = []
for k, v in commdict.iteritems():
titles = commdict[k].keys()
break
titles.remove("rare")
if unicode(self.settings["ocr_language"]) in titles:
titles.remove(unicode(self.settings["ocr_language"]))
titles.insert(0,unicode(self.settings["ocr_language"]))
self.table.setColumnCount(len(titles)+2)
self.table.setHorizontalHeaderLabels(["rare", "eng"]+titles)
totable = []
for k, v in commdict.iteritems():
rest = [commdict[k][i] for i in titles]
totable.append([commdict[k]["rare"], k] + rest)
#print totable
totable.sort(key=lambda x: x[1])
self.table.setRowCount(len(totable))
for i in xrange(len(totable)):
for j in xrange(len(totable[i])):
newitem = QTableWidgetItem(unicode(totable[i][j]))
#print totable[i][j]
self.table.setItem(i, j, newitem)
def addCommodity(self):
self.table.setRowCount(self.table.rowCount()+1)
def deleteCommodity(self):
self.table.removeRow(self.table.currentRow())
def saveCommodities(self):
save_dict = {}
all_rows = self.table.rowCount()
all_cols = self.table.columnCount()
for row in xrange(all_rows):
if (not self.table.item(row,0) is None) and (not self.table.item(row,1) is None):
save_dict[unicode(self.table.item(row,1).text())] = {}
save_dict[unicode(self.table.item(row,1).text())][unicode(self.table.horizontalHeaderItem(0).text())] = unicode(self.table.item(row,0).text())
for col in xrange(2,all_cols):
if not self.table.item(row,col) is None:
save_dict[unicode(self.table.item(row,1).text())][unicode(self.table.horizontalHeaderItem(col).text())] = unicode(self.table.item(row,col).text())
else:
save_dict[unicode(self.table.item(row,1).text())][unicode(self.table.horizontalHeaderItem(col).text())] = u""
#line = self.result_table.item(row,9).text()
#print save_dict
file = codecs.open(self.settings.app_path + ""+ os.sep +"commodities.json", 'w', "utf-8")
file.write(json.dumps(save_dict, indent=2, separators=(',', ': '), ensure_ascii=False))
file.close()
self.close()