forked from veusz/veusz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
veusz_main.py
executable file
·268 lines (224 loc) · 8.76 KB
/
veusz_main.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
# Copyright (C) 2004 Jeremy S. Sanders
# Email: Jeremy Sanders <[email protected]>
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
##############################################################################
'''Main Veusz executable.'''
import sys
import os.path
import signal
import optparse
# Allow veusz to be run even if not installed into PYTHONPATH
try:
import veusz
except ImportError:
# load in the veusz module, but change its path to
# the veusz directory, and insert it into sys.modules
import __init__ as veusz
thisdir = os.path.dirname( os.path.abspath(__file__) )
veusz.__path__ = [thisdir]
veusz.__name__ = 'veusz'
sys.modules['veusz'] = veusz
import veusz.qtall as qt4
import veusz.utils as utils
copyr='''Veusz %s
Copyright (C) Jeremy Sanders 2003-2012 <[email protected]> and contributors
Licenced under the GNU General Public Licence (version 2 or greater)
'''
splashcopyr='''<b><font color="purple">Veusz %s<br></font></b>
Copyright (C) Jeremy Sanders 2003-2012 and contributors<br>
Licenced under the GPL (version 2 or greater)
'''
def handleIntSignal(signum, frame):
'''Ask windows to close if Ctrl+C pressed.'''
qt4.qApp.closeAllWindows()
def makeSplashLogo():
'''Make a splash screen logo.'''
border = 16
xw, yw = 520, 240
pix = qt4.QPixmap(xw, yw)
pix.fill()
p = qt4.QPainter(pix)
# draw logo on pixmap
logo = utils.getPixmap('logo.png')
p.drawPixmap( xw/2 - logo.width()/2, border, logo )
# add copyright text
doc = qt4.QTextDocument()
doc.setPageSize( qt4.QSizeF(xw, yw - 3*border - logo.height()) )
f = qt4.qApp.font()
f.setPointSize(14)
doc.setDefaultFont(f)
doc.setDefaultTextOption( qt4.QTextOption(qt4.Qt.AlignCenter) )
doc.setHtml(splashcopyr % utils.version())
p.translate(0, 2*border + logo.height())
doc.drawContents(p)
p.end()
return pix
def excepthook(excepttype, exceptvalue, tracebackobj):
'''Show exception dialog if an exception occurs.'''
from veusz.dialogs.exceptiondialog import ExceptionDialog
if not isinstance(exceptvalue, utils.IgnoreException):
# next exception is ignored to clear out the stack frame of the
# previous exception - yuck
d = ExceptionDialog((excepttype, exceptvalue, tracebackobj), None)
d.exec_()
def listen(args, quiet):
'''For running with --listen option.'''
from veusz.veusz_listen import openWindow
openWindow(args, quiet=quiet)
def export(exports, args):
'''A shortcut to load a set of files and export them.'''
import veusz.document as document
for expfn, vsz in zip(exports, args[1:]):
doc = document.Document()
ci = document.CommandInterpreter(doc)
ci.Load(vsz)
ci.run('Export(%s)' % repr(expfn))
def mainwindow(args):
'''Open the main window with any loaded files.'''
from veusz.windows.mainwindow import MainWindow
if len(args) > 1:
# load in filenames given
for filename in args[1:]:
MainWindow.CreateWindow(filename)
else:
# create blank window
MainWindow.CreateWindow()
def convertArgsUnicode(args):
'''Convert set of arguments to unicode.
Arguments in argv use current file system encoding
'''
enc = sys.getfilesystemencoding()
# bail out if not supported
if enc is None:
return args
out = []
for a in args:
if isinstance(a, str):
out.append( a.decode(enc) )
else:
out.append(a)
return out
class ImportThread(qt4.QThread):
'''Do import of main code within another thread.
Main application runs when this is done
'''
def run(self):
import veusz.setting
import veusz.widgets
import veusz.utils.vzdbus as vzdbus
vzdbus.setup()
class AppRunner(qt4.QObject):
'''Object to run application. We have to do this to get an
event loop while importing, etc.'''
def __init__(self, options, args):
qt4.QObject.__init__(self)
self.splash = None
if not (options.listen or options.export):
# show the splash screen on normal start
self.splash = qt4.QSplashScreen(makeSplashLogo())
self.splash.show()
self.options = options
self.args = args
# optionally load a translation
if options.translation:
trans = qt4.QTranslator()
trans.load(options.translation)
qt4.qApp.installTranslator(trans)
self.thread = ImportThread()
self.connect( self.thread, qt4.SIGNAL('finished()'),
self.slotStartApplication )
self.thread.start()
def slotStartApplication(self):
'''Main start of application.'''
options = self.options
args = self.args
import veusz.document
import veusz.setting
# install exception hook after thread has finished
sys.excepthook = excepthook
# for people who want to run any old script
veusz.setting.transient_settings['unsafe_mode'] = bool(
options.unsafe_mode)
# load any requested plugins
if options.plugin:
veusz.document.Document.loadPlugins(pluginlist=options.plugin)
# different modes
if options.listen:
# listen to incoming commands
listen(args, quiet=options.quiet)
elif options.export:
# export files to make images
if len(options.export) != len(args)-1:
parser.error(
'export option needs same number of documents and '
'output files')
export(options.export, args)
qt4.qApp.quit()
else:
# standard start main window
mainwindow(args)
# clear splash when startup done
if self.splash is not None:
self.splash.finish(qt4.qApp.topLevelWidgets()[0])
def run():
'''Run the main application.'''
# jump to the embedding client entry point if required
if len(sys.argv) == 2 and sys.argv[1] == '--embed-remote':
from veusz.embed_remote import runremote
runremote()
return
# this function is spaghetti-like and has nasty code paths.
# the idea is to postpone the imports until the splash screen
# is shown
app = qt4.QApplication(sys.argv)
app.connect(app, qt4.SIGNAL('lastWindowClosed()'),
app, qt4.SLOT('quit()'))
# register a signal handler to catch ctrl+C
signal.signal(signal.SIGINT, handleIntSignal)
# parse command line options
parser = optparse.OptionParser(
usage='%prog [options] filename.vsz ...',
version=copyr % utils.version())
parser.add_option('--unsafe-mode', action='store_true',
help='disable safety checks when running documents'
' or scripts')
parser.add_option('--listen', action='store_true',
help='read and execute Veusz commands from stdin,'
' replacing veusz_listen')
parser.add_option('--quiet', action='store_true',
help='if in listening mode, do not open a window but'
' execute commands quietly')
parser.add_option('--export', action='append', metavar='FILE',
help='export the next document to this'
' output image file, exiting when finished')
parser.add_option('--embed-remote', action='store_true',
help=optparse.SUPPRESS_HELP)
parser.add_option('--plugin', action='append', metavar='FILE',
help='load the plugin from the file given for '
'the session')
parser.add_option('--translation', metavar='FILE',
help='load the translation .qm file given')
options, args = parser.parse_args( app.argv() )
# convert args to unicode from filesystem strings
args = convertArgsUnicode(args)
s = AppRunner(options, args)
app.exec_()
# if ran as a program
if __name__ == '__main__':
#import cProfile
#cProfile.run('run()', 'outprofile.dat')
run()