summaryrefslogtreecommitdiffstats
path: root/lib/kformula/prototype/unicode.py
blob: 585574cf4300ff40f9d0b0b8800ffe04ef3e8a9e (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python

import sys
from qt import *
from xml.sax import saxutils, handler, make_parser

class Form1(QWidget):
    def __init__(self,parent = None,name = None,fl = 0):
        QWidget.__init__(self,parent,name,fl)

        if name == None:
            self.setName('Form1')

        self.setCaption(self.tr('Form1'))
        grid = QGridLayout(self)
        grid.setSpacing(6)
        grid.setMargin(11)

        self.chars = {}
        self.fontName = None

        begin = 32
        end = 256
        for i in range(begin, end):

            charLabel = QLabel(self,'charLabel' + chr(i))
            charLabel.setFont(QFont("symbol", 16))
            charLabel.setText(self.tr(chr(i)))
            grid.addWidget(charLabel, i-begin, 0)
            
            number = QLineEdit(self,'Number' + chr(i))
            grid.addWidget(number, i-begin, 1)

            latexName = QLineEdit(self,'latexName' + chr(i))
            grid.addWidget(latexName, i-begin, 2)

            charClass = QLineEdit(self,'charClass' + chr(i))
            grid.addWidget(charClass, i-begin, 3)
            
            self.chars[i] = (charLabel, number, latexName, charClass)

    def fontList(self):
        list = []
        for i in self.chars:
            charLabel, number, latexName, charClass = self.chars[i]
            if str(number.text()) != "" or str(latexName.text()) != "" or str(charClass.text()) != "":
                list.append((i, str(number.text()), str(latexName.text()), str(charClass.text())))
        return list
    
    def setFont(self, fontName, font):
        fontName = fontName.replace("%20", " ")
        self.fontName = fontName
        for i in self.chars:
            charLabel, number, latexName, charClass = self.chars[i]
            charLabel.setFont(QFont(fontName, 16))
            number.setText("")
            latexName.setText("")
            charClass.setText("")

        for (key, number, latexName, charClass) in font:
            i = int(key)
            charLabel, numberWidget, latexNameWidget, charClassWidget = self.chars[i]
            numberWidget.setText(number)
            latexNameWidget.setText(latexName)
            charClassWidget.setText(charClass)
            

class Widget(QWidget):

    def __init__(self):
        QWidget.__init__(self)

        vbox = QVBoxLayout(self)
        vbox.setSpacing(6)
        vbox.setMargin(0)

        hbox = QHBoxLayout()
        hbox.setSpacing(6)
        hbox.setMargin(0)

        loadButton = QPushButton("load", self)
        saveButton = QPushButton("save", self)

        QObject.connect(loadButton, SIGNAL("pressed()"), self.load)
        QObject.connect(saveButton, SIGNAL("pressed()"), self.save)
        
        hbox.addWidget(loadButton)
        hbox.addWidget(saveButton)
                
        vbox.addLayout(hbox)
        
        splitter = QSplitter(self)
        splitter.setOrientation(Qt.Vertical)
        
        self.listbox = QListBox(splitter)
        
        sv = QScrollView(splitter)
        big_box = QVBox(sv.viewport())
        sv.addChild(big_box, 0, 0)
        self.child = Form1(big_box)

        vbox.addWidget(splitter)

        self.connect(self.listbox, SIGNAL('highlighted( const QString& )'),
                     self.fontHighlighted)

    def fontHighlighted(self, fontStr):
        if self.child.fontName:
            self.fonts[self.child.fontName] = self.child.fontList()
            
        font = str(fontStr)
        self.child.setFont(font, self.fonts[font])
        
    def load(self):
        self.fonts = {}
        parser = make_parser()
        parser.setContentHandler(ContentGenerator(self.fonts))
        parser.parse("symbol.xml")

        self.listbox.clear()
        for font in self.fonts:
            self.listbox.insertItem(font)
        self.listbox.sort()
            
    def save(self):
        if self.child.fontName:
            self.fonts[self.child.fontName] = self.child.fontList()
    
        f = open("symbol.xml", "w")
        print >> f, '<?xml version="1.0" encoding="iso-8859-1"?>'
        print >> f, '<table>'
        for font in self.fonts:
            print >> f, '  <tqunicodetable font="' + font + '">'
            for (key, number, latexName, charClass) in self.fonts[font]:
                if not charClass or charClass == '':
                    charClass = 'ORDINARY'
                print >> f,  '    <entry key="' + str(key) + \
                      '" number="' + str(number) + \
                      '" name="' + str(latexName) + \
                      '" class="' + str(charClass) + \
                      '"/>'
            
            print >> f, '  </tqunicodetable>'
        print >> f, '</table>'
        f.close()


class ContentGenerator(handler.ContentHandler):  
    def __init__(self, fonts):
        handler.ContentHandler.__init__(self)
        self.fonts = fonts
        self.currentFont = None

    def startElement(self, name, attrs):
        if name == 'tqunicodetable':
            for (name, value) in attrs.items():
                if name == "font":
                    self.currentFont = value
                    self.fonts[self.currentFont] = []
        elif name == 'entry':
            if not self.currentFont:
                raise "entry must belong to a font"
            for (name, value) in attrs.items():
                if name == "key":
                    if len(value) > 1 and value[:2] == "0x":
                        key = int(value[2:], 16)
                    else:
                        key = int(value)
                elif name == "number": number = value
                elif name == "name": latexName = value
                elif name == "class": charClass = value

            self.fonts[self.currentFont].append((key, number, latexName, charClass))
            #numberWidget, latexNameWidget, charClassWidget = self.widgets[key]
            #numberWidget.setText(number)
            #latexNameWidget.setText(latexName)
            #charClassWidget.setText(charClass)
            
    
def main():
    a = QApplication(sys.argv)

    mw = Widget()
    mw.setCaption('Unicode mapping util')
    mw.show()

    a.connect(a, SIGNAL('lastWindowClosed()'), a, SLOT('quit()'))
    a.exec_loop()

if __name__ == '__main__':

    main()