source: SHX/trunk/SeismicHandler/modules/screen_x.py @ 1226

Revision 1226, 5.3 KB checked in by klaus, 3 years ago (diff)

SHX with X11 and redraw functionality

Line 
1# -*- coding: utf-8 -*-
2
3#    This file is part of Seismic Handler eXtended (SHX). For terms of use and
4#    license information please see license.txt and visit
5#    http://www.seismic-handler.org/portal/wiki/Shx/LicenseTerms
6
7"""
8This class handles all "screen" related information, like length, etc.
9"""
10
11from SeismicHandler.basics import Singleton
12from SeismicHandler.basics.messages import subscribe_event, log_message #, \
13from SeismicHandler.config import get_runtime, set_runtime
14from SeismicHandler.core import Traces
15from SeismicHandler.basics.error import ShxError
16from SeismicHandler.basics.threads import ShxThread
17import fastx
18
19class Screen(object):
20    __metaclass__ = Singleton
21    __init = False
22
23    def __init__(self):
24        if not self.__init:
25            self.setup()
26
27    def setup(self):
28        subscribe_event(self.redraw, 'redraw')
29        log_message("debug.screen", "init")
30        self.__init = True
31
32    @staticmethod
33    def redraw(payload=None):
34        """
35        Called on each redraw event. Calculate extend of traces.
36        """
37        print "xscreen.redraw called"
38        pass
39        #if len(Traces) == 0:
40        #    return
41        #
42        #t = Traces[0]
43        #lo = t.get_info("t-origin")
44        #length = t.get_info("endtime") - t.get_info("starttime")
45        #hi = lo + length
46        #
47        #for t in Traces[1:]:
48        #    tlo = t.get_info("t-origin")
49        #    if tlo < lo:
50        #        lo = tlo
51        #
52        #    length = t.get_info("endtime") - t.get_info("starttime")
53        #    thi = tlo + length
54        #    if thi > hi:
55        #        hi = thi
56        #
57        #set_runtime("extend", (lo, hi))
58
59    @staticmethod
60    def timewdw():
61        """
62        Returns current display extend with respect to time window setting.
63        """
64        s, e = get_runtime("extend")
65        s1, e1 = get_runtime("timewindow", (0., None))
66
67        if e1 == None:
68            s1 = s
69            e1 = e
70
71        return (s1, e1)
72
73
74class WindowManager(object):
75    """
76    Holds list of windows.
77    """
78    __metaclass__ = Singleton
79    wlist = {}
80
81    def create(self, wid, x, y, width, height, traces=False, pixelunits=False):
82        if wid in self.wlist:
83            log_message(
84                "warning.WindowManager",
85                ("Attempt to open window twice, cancelled.\n"
86                 "\t Maybe you want to run 'wdw delete %s'\n"
87                 "\t (this information is only shown once.)") % wid
88            )
89            return
90
91        if not traces:
92            raise ShxError("SHX supports only trace windows.")
93
94        w = TracePlotter( [x, y, width, height], pixelunits=pixelunits )
95
96        # remember instance
97        self.wlist[wid] = w
98
99        # start thread
100        w.start()
101
102    def delete(self, wid):
103        try:
104            window = self.wlist.pop(wid)
105        except:
106            return
107
108        window.stop()
109
110    def top(self, wid):
111        """Documentation"""
112        pass
113
114    def size(self, wid):
115        """Documentation"""
116        pass
117
118
119class TracePlotter(ShxThread):
120    """
121    Encapsulates traces plotting on display. Bound to "redraw" event so that
122    any changes will be reflected instantly.
123    """
124
125    def __init__(self, dimension, auto_redraw=True, pixelunits=False ):
126        ShxThread.__init__(self)
127
128        self.name = "TracePlotter"
129        self.redraw = False
130        self.dimensions = dimension
131        self.pixelunits = pixelunits
132        self.fastx = None
133
134        if auto_redraw:
135            subscribe_event(self.trigger_redraw, 'redraw')
136
137        log_message("debug.traceplotter", "init")
138
139    def trigger_redraw(self, payload=None):
140        if not self.isAlive():
141            return
142
143        if get_runtime("rdnr", 0) > 0:
144            return
145
146        # this will fire the redraw event
147        #wx.CallAfter(self.plotter.redraw)
148        if self.fastx == None:
149            print "dbg: fastx not yet initalized, no redraw trigger possible"
150        else:
151            self.fastx.trigger_all_traces_redraw()
152
153    #@anythread
154    def stop(self):
155        """
156        Exit main loop of wx.
157        """
158        if self.isAlive():
159            self.fastx.close_all_windows()
160        super(TracePlotter, self).stop()
161
162    def setup(self):
163        print "dbg: TracePlotter setup called"
164        log_message("debug.traceplotter", "init x app")
165
166        print "dbg: before fastx.FastX()"
167        self.fastx = fastx.FastX()
168        print "dbg: after fastx.FastX()"
169
170        # original SH calculates uses always 75 dpi
171        # now using true DPI values
172        if self.pixelunits:
173            width = int( self.dimensions[2] )
174            height = int( self.dimensions[3] )
175            posx = int( self.dimensions[0] )
176            posy = int( self.dimensions[1] )
177        else:
178            pixfac = 20.
179            width = int( float(self.dimensions[2])*pixfac )
180            height = int( float(self.dimensions[3])*pixfax )
181            posx = int( float(self.dimensions[0])*pixfac )
182            posy = int( float(self.dimensions[1])*pixfac )
183            print "pixel units preferred"
184
185        self.fastx.open_all_traces_window( posx, posy, width, height )
186        self.fastx.trigger_all_traces_redraw()
187
188    def run(self):
189        # override, no standard ShxThread behaviour
190        log_message("debug.traceplotter", "start x main loop")
191        self.fastx.event_loop()
192
193Screen()
Note: See TracBrowser for help on using the repository browser.