ยปCore Development>Code coverage>Tools/pynche/TypeinViewer.py

Python code coverage for Tools/pynche/TypeinViewer.py

#countcontent
1n/a"""TypeinViewer class.
2n/a
3n/aThe TypeinViewer is what you see at the lower right of the main Pynche
4n/awidget. It contains three text entry fields, one each for red, green, blue.
5n/aInput into these windows is highly constrained; it only allows you to enter
6n/avalues that are legal for a color axis. This usually means 0-255 for decimal
7n/ainput and 0x0 - 0xff for hex input.
8n/a
9n/aYou can toggle whether you want to view and input the values in either decimal
10n/aor hex by clicking on Hexadecimal. By clicking on Update while typing, the
11n/acolor selection will be made on every change to the text field. Otherwise,
12n/ayou must hit Return or Tab to select the color.
13n/a"""
14n/a
15n/afrom tkinter import *
16n/a
17n/a
18n/a
19n/aclass TypeinViewer:
20n/a def __init__(self, switchboard, master=None):
21n/a # non-gui ivars
22n/a self.__sb = switchboard
23n/a optiondb = switchboard.optiondb()
24n/a self.__hexp = BooleanVar()
25n/a self.__hexp.set(optiondb.get('HEXTYPE', 0))
26n/a self.__uwtyping = BooleanVar()
27n/a self.__uwtyping.set(optiondb.get('UPWHILETYPE', 0))
28n/a # create the gui
29n/a self.__frame = Frame(master, relief=RAISED, borderwidth=1)
30n/a self.__frame.grid(row=3, column=1, sticky='NSEW')
31n/a # Red
32n/a self.__xl = Label(self.__frame, text='Red:')
33n/a self.__xl.grid(row=0, column=0, sticky=E)
34n/a subframe = Frame(self.__frame)
35n/a subframe.grid(row=0, column=1)
36n/a self.__xox = Label(subframe, text='0x')
37n/a self.__xox.grid(row=0, column=0, sticky=E)
38n/a self.__xox['font'] = 'courier'
39n/a self.__x = Entry(subframe, width=3)
40n/a self.__x.grid(row=0, column=1)
41n/a self.__x.bindtags(self.__x.bindtags() + ('Normalize', 'Update'))
42n/a self.__x.bind_class('Normalize', '<Key>', self.__normalize)
43n/a self.__x.bind_class('Update' , '<Key>', self.__maybeupdate)
44n/a # Green
45n/a self.__yl = Label(self.__frame, text='Green:')
46n/a self.__yl.grid(row=1, column=0, sticky=E)
47n/a subframe = Frame(self.__frame)
48n/a subframe.grid(row=1, column=1)
49n/a self.__yox = Label(subframe, text='0x')
50n/a self.__yox.grid(row=0, column=0, sticky=E)
51n/a self.__yox['font'] = 'courier'
52n/a self.__y = Entry(subframe, width=3)
53n/a self.__y.grid(row=0, column=1)
54n/a self.__y.bindtags(self.__y.bindtags() + ('Normalize', 'Update'))
55n/a # Blue
56n/a self.__zl = Label(self.__frame, text='Blue:')
57n/a self.__zl.grid(row=2, column=0, sticky=E)
58n/a subframe = Frame(self.__frame)
59n/a subframe.grid(row=2, column=1)
60n/a self.__zox = Label(subframe, text='0x')
61n/a self.__zox.grid(row=0, column=0, sticky=E)
62n/a self.__zox['font'] = 'courier'
63n/a self.__z = Entry(subframe, width=3)
64n/a self.__z.grid(row=0, column=1)
65n/a self.__z.bindtags(self.__z.bindtags() + ('Normalize', 'Update'))
66n/a # Update while typing?
67n/a self.__uwt = Checkbutton(self.__frame,
68n/a text='Update while typing',
69n/a variable=self.__uwtyping)
70n/a self.__uwt.grid(row=3, column=0, columnspan=2, sticky=W)
71n/a # Hex/Dec
72n/a self.__hex = Checkbutton(self.__frame,
73n/a text='Hexadecimal',
74n/a variable=self.__hexp,
75n/a command=self.__togglehex)
76n/a self.__hex.grid(row=4, column=0, columnspan=2, sticky=W)
77n/a
78n/a def __togglehex(self, event=None):
79n/a red, green, blue = self.__sb.current_rgb()
80n/a if self.__hexp.get():
81n/a label = '0x'
82n/a else:
83n/a label = ' '
84n/a self.__xox['text'] = label
85n/a self.__yox['text'] = label
86n/a self.__zox['text'] = label
87n/a self.update_yourself(red, green, blue)
88n/a
89n/a def __normalize(self, event=None):
90n/a ew = event.widget
91n/a contents = ew.get()
92n/a icursor = ew.index(INSERT)
93n/a if contents and contents[0] in 'xX' and self.__hexp.get():
94n/a contents = '0' + contents
95n/a # Figure out the contents in the current base.
96n/a try:
97n/a if self.__hexp.get():
98n/a v = int(contents, 16)
99n/a else:
100n/a v = int(contents)
101n/a except ValueError:
102n/a v = None
103n/a # If value is not legal, or empty, delete the last character inserted
104n/a # and ring the bell. Don't ring the bell if the field is empty (it'll
105n/a # just equal zero.
106n/a if v is None:
107n/a pass
108n/a elif v < 0 or v > 255:
109n/a i = ew.index(INSERT)
110n/a if event.char:
111n/a contents = contents[:i-1] + contents[i:]
112n/a icursor -= 1
113n/a ew.bell()
114n/a elif self.__hexp.get():
115n/a contents = hex(v)[2:]
116n/a else:
117n/a contents = int(v)
118n/a ew.delete(0, END)
119n/a ew.insert(0, contents)
120n/a ew.icursor(icursor)
121n/a
122n/a def __maybeupdate(self, event=None):
123n/a if self.__uwtyping.get() or event.keysym in ('Return', 'Tab'):
124n/a self.__update(event)
125n/a
126n/a def __update(self, event=None):
127n/a redstr = self.__x.get() or '0'
128n/a greenstr = self.__y.get() or '0'
129n/a bluestr = self.__z.get() or '0'
130n/a if self.__hexp.get():
131n/a base = 16
132n/a else:
133n/a base = 10
134n/a red, green, blue = [int(x, base) for x in (redstr, greenstr, bluestr)]
135n/a self.__sb.update_views(red, green, blue)
136n/a
137n/a def update_yourself(self, red, green, blue):
138n/a if self.__hexp.get():
139n/a sred, sgreen, sblue = [hex(x)[2:] for x in (red, green, blue)]
140n/a else:
141n/a sred, sgreen, sblue = red, green, blue
142n/a x, y, z = self.__x, self.__y, self.__z
143n/a xicursor = x.index(INSERT)
144n/a yicursor = y.index(INSERT)
145n/a zicursor = z.index(INSERT)
146n/a x.delete(0, END)
147n/a y.delete(0, END)
148n/a z.delete(0, END)
149n/a x.insert(0, sred)
150n/a y.insert(0, sgreen)
151n/a z.insert(0, sblue)
152n/a x.icursor(xicursor)
153n/a y.icursor(yicursor)
154n/a z.icursor(zicursor)
155n/a
156n/a def hexp_var(self):
157n/a return self.__hexp
158n/a
159n/a def save_options(self, optiondb):
160n/a optiondb['HEXTYPE'] = self.__hexp.get()
161n/a optiondb['UPWHILETYPE'] = self.__uwtyping.get()