blob: e1fe915563b3652d7776c49d0012f7456dcea16d [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# This file is part of pybootchartgui.
2
3# pybootchartgui is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7
8# pybootchartgui is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12
13# You should have received a copy of the GNU General Public License
14# along with pybootchartgui. If not, see <http://www.gnu.org/licenses/>.
15
Brad Bishopc342db32019-05-15 21:57:59 -040016import gi
17gi.require_version('Gtk', '3.0')
18from gi.repository import Gtk as gtk
19from gi.repository import Gtk
20from gi.repository import Gdk
21from gi.repository import GObject as gobject
22from gi.repository import GObject
23
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024from . import draw
25from .draw import RenderOptions
26
Brad Bishopc342db32019-05-15 21:57:59 -040027class PyBootchartWidget(gtk.DrawingArea, gtk.Scrollable):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028 __gsignals__ = {
Brad Bishopc342db32019-05-15 21:57:59 -040029 'clicked' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, Gdk.Event)),
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030 'position-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT, gobject.TYPE_INT)),
31 'set-scroll-adjustments' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gtk.Adjustment, gtk.Adjustment))
32 }
33
Brad Bishopc342db32019-05-15 21:57:59 -040034 hadjustment = GObject.property(type=Gtk.Adjustment,
35 default=Gtk.Adjustment(),
36 flags=GObject.PARAM_READWRITE)
37 hscroll_policy = GObject.property(type=Gtk.ScrollablePolicy,
38 default=Gtk.ScrollablePolicy.MINIMUM,
39 flags=GObject.PARAM_READWRITE)
40 vadjustment = GObject.property(type=Gtk.Adjustment,
41 default=Gtk.Adjustment(),
42 flags=GObject.PARAM_READWRITE)
43 vscroll_policy = GObject.property(type=Gtk.ScrollablePolicy,
44 default=Gtk.ScrollablePolicy.MINIMUM,
45 flags=GObject.PARAM_READWRITE)
46
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047 def __init__(self, trace, options, xscale):
48 gtk.DrawingArea.__init__(self)
49
50 self.trace = trace
51 self.options = options
52
Brad Bishopc342db32019-05-15 21:57:59 -040053 self.set_can_focus(True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050054
Brad Bishopc342db32019-05-15 21:57:59 -040055 self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 self.connect("button-press-event", self.on_area_button_press)
57 self.connect("button-release-event", self.on_area_button_release)
Brad Bishopc342db32019-05-15 21:57:59 -040058 self.add_events(Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.POINTER_MOTION_HINT_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 self.connect("motion-notify-event", self.on_area_motion_notify)
60 self.connect("scroll-event", self.on_area_scroll_event)
61 self.connect('key-press-event', self.on_key_press_event)
62
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 self.connect("size-allocate", self.on_allocation_size_changed)
64 self.connect("position-changed", self.on_position_changed)
65
Brad Bishopc342db32019-05-15 21:57:59 -040066 self.connect("draw", self.on_draw)
67
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068 self.zoom_ratio = 1.0
69 self.xscale = xscale
70 self.x, self.y = 0.0, 0.0
71
72 self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace)
Brad Bishopc342db32019-05-15 21:57:59 -040073 self.our_width, self.our_height = self.chart_width, self.chart_height
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074
Brad Bishopc342db32019-05-15 21:57:59 -040075 self.hadj = gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
76 self.vadj = gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
77 self.vadj.connect('value-changed', self.on_adjustments_changed)
78 self.hadj.connect('value-changed', self.on_adjustments_changed)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079
Brad Bishopc342db32019-05-15 21:57:59 -040080 def bound_vals(self):
81 self.x = max(0, self.x)
82 self.y = max(0, self.y)
83 self.x = min(self.chart_width - self.our_width, self.x)
84 self.y = min(self.chart_height - self.our_height, self.y)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
Brad Bishopc342db32019-05-15 21:57:59 -040086 def on_draw(self, darea, cr):
87 # set a clip region
88 #cr.rectangle(
89 # self.x, self.y,
90 # self.chart_width, self.chart_height
91 #)
92 #cr.clip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
94 cr.paint()
95 cr.scale(self.zoom_ratio, self.zoom_ratio)
96 cr.translate(-self.x, -self.y)
97 draw.render(cr, self.options, self.xscale, self.trace)
98
99 def position_changed(self):
100 self.emit("position-changed", self.x, self.y)
101
102 ZOOM_INCREMENT = 1.25
103
104 def zoom_image (self, zoom_ratio):
105 self.zoom_ratio = zoom_ratio
Brad Bishopc342db32019-05-15 21:57:59 -0400106 self._set_scroll_adjustments()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 self.queue_draw()
108
109 def zoom_to_rect (self, rect):
110 zoom_ratio = float(rect.width)/float(self.chart_width)
111 self.zoom_image(zoom_ratio)
112 self.x = 0
113 self.position_changed()
114
115 def set_xscale(self, xscale):
116 old_mid_x = self.x + self.hadj.page_size / 2
117 self.xscale = xscale
118 self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace)
119 new_x = old_mid_x
120 self.zoom_image (self.zoom_ratio)
121
122 def on_expand(self, action):
123 self.set_xscale (int(self.xscale * 1.5 + 0.5))
124
125 def on_contract(self, action):
126 self.set_xscale (max(int(self.xscale / 1.5), 1))
127
128 def on_zoom_in(self, action):
129 self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT)
130
131 def on_zoom_out(self, action):
132 self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT)
133
134 def on_zoom_fit(self, action):
135 self.zoom_to_rect(self.get_allocation())
136
137 def on_zoom_100(self, action):
138 self.zoom_image(1.0)
139 self.set_xscale(1.0)
140
141 def show_toggled(self, button):
142 self.options.app_options.show_all = button.get_property ('active')
143 self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace)
Brad Bishopc342db32019-05-15 21:57:59 -0400144 self._set_scroll_adjustments()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145 self.queue_draw()
146
147 POS_INCREMENT = 100
148
149 def on_key_press_event(self, widget, event):
Brad Bishopc342db32019-05-15 21:57:59 -0400150 if event.keyval == Gdk.keyval_from_name("Left"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151 self.x -= self.POS_INCREMENT/self.zoom_ratio
Brad Bishopc342db32019-05-15 21:57:59 -0400152 elif event.keyval == Gdk.keyval_from_name("Right"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500153 self.x += self.POS_INCREMENT/self.zoom_ratio
Brad Bishopc342db32019-05-15 21:57:59 -0400154 elif event.keyval == Gdk.keyval_from_name("Up"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155 self.y -= self.POS_INCREMENT/self.zoom_ratio
Brad Bishopc342db32019-05-15 21:57:59 -0400156 elif event.keyval == Gdk.keyval_from_name("Down"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157 self.y += self.POS_INCREMENT/self.zoom_ratio
158 else:
159 return False
Brad Bishopc342db32019-05-15 21:57:59 -0400160 self.bound_vals()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161 self.queue_draw()
162 self.position_changed()
163 return True
164
165 def on_area_button_press(self, area, event):
166 if event.button == 2 or event.button == 1:
Brad Bishopc342db32019-05-15 21:57:59 -0400167 window = self.get_window()
168 window.set_cursor(Gdk.Cursor(Gdk.CursorType.FLEUR))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500169 self.prevmousex = event.x
170 self.prevmousey = event.y
Brad Bishopc342db32019-05-15 21:57:59 -0400171 if event.type not in (Gdk.EventType.BUTTON_PRESS, Gdk.EventType.BUTTON_RELEASE):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172 return False
173 return False
174
175 def on_area_button_release(self, area, event):
176 if event.button == 2 or event.button == 1:
Brad Bishopc342db32019-05-15 21:57:59 -0400177 window = self.get_window()
178 window.set_cursor(Gdk.Cursor(Gdk.CursorType.ARROW))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 self.prevmousex = None
180 self.prevmousey = None
181 return True
182 return False
183
184 def on_area_scroll_event(self, area, event):
Brad Bishopc342db32019-05-15 21:57:59 -0400185 if event.state & Gdk.CONTROL_MASK:
186 if event.direction == Gdk.SCROLL_UP:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187 self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT)
188 return True
Brad Bishopc342db32019-05-15 21:57:59 -0400189 if event.direction == Gdk.SCROLL_DOWN:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500190 self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT)
191 return True
192 return False
193
194 def on_area_motion_notify(self, area, event):
195 state = event.state
Brad Bishopc342db32019-05-15 21:57:59 -0400196 if state & Gdk.ModifierType.BUTTON2_MASK or state & Gdk.ModifierType.BUTTON1_MASK:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197 x, y = int(event.x), int(event.y)
198 # pan the image
199 self.x += (self.prevmousex - x)/self.zoom_ratio
200 self.y += (self.prevmousey - y)/self.zoom_ratio
Brad Bishopc342db32019-05-15 21:57:59 -0400201 self.bound_vals()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202 self.queue_draw()
203 self.prevmousex = x
204 self.prevmousey = y
205 self.position_changed()
206 return True
207
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208 def on_allocation_size_changed(self, widget, allocation):
209 self.hadj.page_size = allocation.width
210 self.hadj.page_increment = allocation.width * 0.9
211 self.vadj.page_size = allocation.height
212 self.vadj.page_increment = allocation.height * 0.9
Brad Bishopc342db32019-05-15 21:57:59 -0400213 self.our_width = allocation.width
214 if self.chart_width < self.our_width:
215 self.our_width = self.chart_width
216 self.our_height = allocation.height
217 if self.chart_height < self.our_height:
218 self.our_height = self.chart_height
219 self._set_scroll_adjustments()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500220
221 def _set_adj_upper(self, adj, upper):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222
Brad Bishopc342db32019-05-15 21:57:59 -0400223 if adj.get_upper() != upper:
224 adj.set_upper(upper)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225
Brad Bishopc342db32019-05-15 21:57:59 -0400226 def _set_scroll_adjustments(self):
227 self._set_adj_upper (self.hadj, self.zoom_ratio * (self.chart_width - self.our_width))
228 self._set_adj_upper (self.vadj, self.zoom_ratio * (self.chart_height - self.our_height))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500229
230 def on_adjustments_changed(self, adj):
Brad Bishopc342db32019-05-15 21:57:59 -0400231 self.x = self.hadj.get_value() / self.zoom_ratio
232 self.y = self.vadj.get_value() / self.zoom_ratio
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 self.queue_draw()
234
235 def on_position_changed(self, widget, x, y):
Brad Bishopc342db32019-05-15 21:57:59 -0400236 self.hadj.set_value(x * self.zoom_ratio)
237 #self.hadj.value_changed()
238 self.vadj.set_value(y * self.zoom_ratio)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500239
240class PyBootchartShell(gtk.VBox):
241 ui = '''
242 <ui>
243 <toolbar name="ToolBar">
244 <toolitem action="Expand"/>
245 <toolitem action="Contract"/>
246 <separator/>
247 <toolitem action="ZoomIn"/>
248 <toolitem action="ZoomOut"/>
249 <toolitem action="ZoomFit"/>
250 <toolitem action="Zoom100"/>
251 </toolbar>
252 </ui>
253 '''
254 def __init__(self, window, trace, options, xscale):
255 gtk.VBox.__init__(self)
256
Brad Bishopc342db32019-05-15 21:57:59 -0400257 self.widget2 = PyBootchartWidget(trace, options, xscale)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500258
259 # Create a UIManager instance
260 uimanager = self.uimanager = gtk.UIManager()
261
262 # Add the accelerator group to the toplevel window
263 accelgroup = uimanager.get_accel_group()
264 window.add_accel_group(accelgroup)
265
266 # Create an ActionGroup
267 actiongroup = gtk.ActionGroup('Actions')
268 self.actiongroup = actiongroup
269
270 # Create actions
271 actiongroup.add_actions((
Brad Bishopc342db32019-05-15 21:57:59 -0400272 ('Expand', gtk.STOCK_ADD, None, None, None, self.widget2.on_expand),
273 ('Contract', gtk.STOCK_REMOVE, None, None, None, self.widget2.on_contract),
274 ('ZoomIn', gtk.STOCK_ZOOM_IN, None, None, None, self.widget2.on_zoom_in),
275 ('ZoomOut', gtk.STOCK_ZOOM_OUT, None, None, None, self.widget2.on_zoom_out),
276 ('ZoomFit', gtk.STOCK_ZOOM_FIT, 'Fit Width', None, None, self.widget2.on_zoom_fit),
277 ('Zoom100', gtk.STOCK_ZOOM_100, None, None, None, self.widget2.on_zoom_100),
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500278 ))
279
280 # Add the actiongroup to the uimanager
281 uimanager.insert_action_group(actiongroup, 0)
282
283 # Add a UI description
284 uimanager.add_ui_from_string(self.ui)
285
286 # Scrolled window
Brad Bishopc342db32019-05-15 21:57:59 -0400287 scrolled = gtk.ScrolledWindow(self.widget2.hadj, self.widget2.vadj)
288 scrolled.add(self.widget2)
289
290 #scrolled.set_hadjustment()
291 #scrolled.set_vadjustment(self.widget2.vadj)
292 scrolled.set_policy(gtk.PolicyType.ALWAYS, gtk.PolicyType.ALWAYS)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500293
294 # toolbar / h-box
295 hbox = gtk.HBox(False, 8)
296
297 # Create a Toolbar
298 toolbar = uimanager.get_widget('/ToolBar')
Brad Bishopc342db32019-05-15 21:57:59 -0400299 hbox.pack_start(toolbar, True, True, 0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500300
301 if not options.kernel_only:
302 # Misc. options
303 button = gtk.CheckButton("Show more")
Brad Bishopc342db32019-05-15 21:57:59 -0400304 button.connect ('toggled', self.widget2.show_toggled)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500305 button.set_active(options.app_options.show_all)
Brad Bishopc342db32019-05-15 21:57:59 -0400306 hbox.pack_start (button, False, True, 0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500307
Brad Bishopc342db32019-05-15 21:57:59 -0400308 self.pack_start(hbox, False, True, 0)
309 self.pack_start(scrolled, True, True, 0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500310 self.show_all()
311
312 def grab_focus(self, window):
Brad Bishopc342db32019-05-15 21:57:59 -0400313 window.set_focus(self.widget2)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500314
315
316class PyBootchartWindow(gtk.Window):
317
318 def __init__(self, trace, app_options):
319 gtk.Window.__init__(self)
320
321 window = self
322 window.set_title("Bootchart %s" % trace.filename)
323 window.set_default_size(750, 550)
324
325 tab_page = gtk.Notebook()
326 tab_page.show()
327 window.add(tab_page)
328
329 full_opts = RenderOptions(app_options)
330 full_tree = PyBootchartShell(window, trace, full_opts, 1.0)
331 tab_page.append_page (full_tree, gtk.Label("Full tree"))
332
333 if trace.kernel is not None and len (trace.kernel) > 2:
334 kernel_opts = RenderOptions(app_options)
335 kernel_opts.cumulative = False
336 kernel_opts.charts = False
337 kernel_opts.kernel_only = True
338 kernel_tree = PyBootchartShell(window, trace, kernel_opts, 5.0)
339 tab_page.append_page (kernel_tree, gtk.Label("Kernel boot"))
340
341 full_tree.grab_focus(self)
342 self.show()
343
344
345def show(trace, options):
346 win = PyBootchartWindow(trace, options)
347 win.connect('destroy', gtk.main_quit)
348 gtk.main()