-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_calc.py
More file actions
134 lines (124 loc) · 6.07 KB
/
gui_calc.py
File metadata and controls
134 lines (124 loc) · 6.07 KB
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
"""Event handlers for the calculator window."""
##
# Copyright 2013 Chad Spratt
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
import subprocess
import sys
class GUI_Calc(object):
def showcalculator(self, _widget, _data=None):
"""Show the calculator window when the Calc button is clicked."""
# get the selected row from the output list
selection = self.gui['fieldview'].get_selection()
# (model, [(path0,), (path1,), ...])
(fieldlist, selectedrows) = selection.get_selected_rows()
if selectedrows:
lastindex = selectedrows[-1]
lastfield = fieldlist.get_value(fieldlist.get_iter(lastindex), 0)
# select the field in the calc window
# fieldlist = self.gui['fieldlist']
for row in fieldlist:
if lastfield in row:
# this will trigger changecalcfield
self.gui['calcoutputfieldcombo'].set_active_iter(row.iter)
break
# elif fieldlist.get_iter_first():
# self.gui['calcoutputfieldcombo'].set_active(-1)
# init the list of available libraries in the combobox
librarylist = self.gui['librarylist']
librarylist.clear()
for libname in self.calc.getlibs():
if not libname.startswith('_'):
librarylist.append([libname])
# display the calculator window
self.gui['calcwindow'].show_all()
def hidecalculator(self, _widget, _data=None):
"""Hide the calculator window in response to a destroy event."""
self.gui['calcwindow'].hide()
return True
def changecalcfield(self, widget, _data=None):
"""Update calc value box when the calc output field combo changes."""
newfield = widget.get_active_text()
if newfield is not None:
newvalue = self.outputs[newfield]['value']
self.gui['calcvaluelabel'].set_text(newvalue[1:-1] + ' =')
valuebuffer = self.gui['calcvalueview'].get_buffer()
valuebuffer.set_text(newvalue)
def loadfunctionlist(self, widget, _data=None):
"""List the functions when a new library is entered or selected."""
newlib = widget.get_active_text()
functionlist = self.gui['functionlist']
functionlist.clear()
if newlib:
try:
for funcname, funcdoc in self.calc.getfuncs(newlib):
functionlist.append([funcname, funcdoc])
except ImportError:
# this will get called for every letter typed into the box
# many failed imports are to be expected
pass
def insertfieldvalue(self, _widget, path, _view_column):
"""Insert a double-clicked value where the cursor or highlight is."""
inputlist = self.gui['inputlist']
insertvalue = inputlist.get_value(inputlist.get_iter(path), 0)
valuebuffer = self.gui['calcvalueview'].get_buffer()
# if something is highlighted, delete it
valuebuffer.delete_selection(True, True)
# insert the double-clicked value
valuebuffer.insert_at_cursor(insertvalue)
self.gui['calcvalueview'].grab_focus()
def insertfunccall(self, _widget, path, _view_column):
"""Insert a function call at the cursor or around highlighted text."""
modulename = self.gui['calclibrarycomboentry'].get_active_text()
# Get the function name
functionlist = self.gui['functionlist']
functionname = functionlist.get_value(functionlist.get_iter(path), 0)
# Append the module name for everything not in these two modules
if modulename not in ['builtins', 'default']:
functionname = modulename + '.' + functionname
# Get any selected text
valuebuffer = self.gui['calcvalueview'].get_buffer()
if valuebuffer.get_has_selection():
selectionbounds = valuebuffer.get_selection_bounds()
selectedtext = valuebuffer.get_text(selectionbounds[0],
selectionbounds[1])
else:
selectedtext = ''
# Delete any selected text and reinsert it wrapped in the function
valuebuffer.delete_selection(True, True)
inserttext = functionname + '(' + selectedtext + ')'
valuebuffer.insert_at_cursor(inserttext)
# place the cursor inside the closing paren
cursoriter = valuebuffer.get_iter_at_mark(valuebuffer.get_insert())
cursoriter.backward_char()
valuebuffer.place_cursor(cursoriter)
self.gui['calcvalueview'].grab_focus()
def savecalcvalue(self, _widget, _data=None):
"""Save the value back to the field."""
outputcombo = self.gui['calcoutputfieldcombo']
fieldname = outputcombo.get_active_text()
valuebuffer = self.gui['calcvalueview'].get_buffer()
fieldvalue = valuebuffer.get_text(valuebuffer.get_start_iter(),
valuebuffer.get_end_iter())
# update the value in the output manager and the output table
self.outputs[fieldname]['value'] = fieldvalue
self.gui['fieldlist'][outputcombo.get_active_iter()][-1] = fieldvalue
self.processtasks(('sample', None))
def showlibraries(self, _widget, _data=None):
librarypath = 'fieldcalcs'
if sys.platform == 'darwin':
subprocess.check_call(['open', '--', librarypath])
elif sys.platform == 'linux2':
subprocess.check_call(['gnome-open', librarypath])
elif sys.platform == 'windows':
subprocess.check_call(['explorer', librarypath])