gui.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. # This file is part of pybootchartgui.
  2. # pybootchartgui is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # pybootchartgui is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with pybootchartgui. If not, see <http://www.gnu.org/licenses/>.
  12. import gi
  13. gi.require_version('Gtk', '3.0')
  14. from gi.repository import Gtk as gtk
  15. from gi.repository import Gtk
  16. from gi.repository import Gdk
  17. from gi.repository import GObject as gobject
  18. from gi.repository import GObject
  19. from . import draw
  20. from .draw import RenderOptions
  21. class PyBootchartWidget(gtk.DrawingArea, gtk.Scrollable):
  22. __gsignals__ = {
  23. 'clicked' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, Gdk.Event)),
  24. 'position-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT, gobject.TYPE_INT)),
  25. 'set-scroll-adjustments' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gtk.Adjustment, gtk.Adjustment))
  26. }
  27. hadjustment = GObject.property(type=Gtk.Adjustment,
  28. default=Gtk.Adjustment(),
  29. flags=GObject.PARAM_READWRITE)
  30. hscroll_policy = GObject.property(type=Gtk.ScrollablePolicy,
  31. default=Gtk.ScrollablePolicy.MINIMUM,
  32. flags=GObject.PARAM_READWRITE)
  33. vadjustment = GObject.property(type=Gtk.Adjustment,
  34. default=Gtk.Adjustment(),
  35. flags=GObject.PARAM_READWRITE)
  36. vscroll_policy = GObject.property(type=Gtk.ScrollablePolicy,
  37. default=Gtk.ScrollablePolicy.MINIMUM,
  38. flags=GObject.PARAM_READWRITE)
  39. def __init__(self, trace, options, xscale):
  40. gtk.DrawingArea.__init__(self)
  41. self.trace = trace
  42. self.options = options
  43. self.set_can_focus(True)
  44. self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK)
  45. self.connect("button-press-event", self.on_area_button_press)
  46. self.connect("button-release-event", self.on_area_button_release)
  47. self.add_events(Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.POINTER_MOTION_HINT_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK)
  48. self.connect("motion-notify-event", self.on_area_motion_notify)
  49. self.connect("scroll-event", self.on_area_scroll_event)
  50. self.connect('key-press-event', self.on_key_press_event)
  51. self.connect("size-allocate", self.on_allocation_size_changed)
  52. self.connect("position-changed", self.on_position_changed)
  53. self.connect("draw", self.on_draw)
  54. self.zoom_ratio = 1.0
  55. self.xscale = xscale
  56. self.x, self.y = 0.0, 0.0
  57. self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace)
  58. self.our_width, self.our_height = self.chart_width, self.chart_height
  59. self.hadj = gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  60. self.vadj = gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
  61. self.vadj.connect('value-changed', self.on_adjustments_changed)
  62. self.hadj.connect('value-changed', self.on_adjustments_changed)
  63. def bound_vals(self):
  64. self.x = max(0, self.x)
  65. self.y = max(0, self.y)
  66. self.x = min(self.chart_width - self.our_width, self.x)
  67. self.y = min(self.chart_height - self.our_height, self.y)
  68. def on_draw(self, darea, cr):
  69. # set a clip region
  70. #cr.rectangle(
  71. # self.x, self.y,
  72. # self.chart_width, self.chart_height
  73. #)
  74. #cr.clip()
  75. cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
  76. cr.paint()
  77. cr.scale(self.zoom_ratio, self.zoom_ratio)
  78. cr.translate(-self.x, -self.y)
  79. draw.render(cr, self.options, self.xscale, self.trace)
  80. def position_changed(self):
  81. self.emit("position-changed", self.x, self.y)
  82. ZOOM_INCREMENT = 1.25
  83. def zoom_image (self, zoom_ratio):
  84. self.zoom_ratio = zoom_ratio
  85. self._set_scroll_adjustments()
  86. self.queue_draw()
  87. def zoom_to_rect (self, rect):
  88. zoom_ratio = float(rect.width)/float(self.chart_width)
  89. self.zoom_image(zoom_ratio)
  90. self.x = 0
  91. self.position_changed()
  92. def set_xscale(self, xscale):
  93. old_mid_x = self.x + self.hadj.page_size / 2
  94. self.xscale = xscale
  95. self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace)
  96. new_x = old_mid_x
  97. self.zoom_image (self.zoom_ratio)
  98. def on_expand(self, action):
  99. self.set_xscale (int(self.xscale * 1.5 + 0.5))
  100. def on_contract(self, action):
  101. self.set_xscale (max(int(self.xscale / 1.5), 1))
  102. def on_zoom_in(self, action):
  103. self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT)
  104. def on_zoom_out(self, action):
  105. self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT)
  106. def on_zoom_fit(self, action):
  107. self.zoom_to_rect(self.get_allocation())
  108. def on_zoom_100(self, action):
  109. self.zoom_image(1.0)
  110. self.set_xscale(1.0)
  111. def show_toggled(self, button):
  112. self.options.app_options.show_all = button.get_property ('active')
  113. self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace)
  114. self._set_scroll_adjustments()
  115. self.queue_draw()
  116. POS_INCREMENT = 100
  117. def on_key_press_event(self, widget, event):
  118. if event.keyval == Gdk.keyval_from_name("Left"):
  119. self.x -= self.POS_INCREMENT/self.zoom_ratio
  120. elif event.keyval == Gdk.keyval_from_name("Right"):
  121. self.x += self.POS_INCREMENT/self.zoom_ratio
  122. elif event.keyval == Gdk.keyval_from_name("Up"):
  123. self.y -= self.POS_INCREMENT/self.zoom_ratio
  124. elif event.keyval == Gdk.keyval_from_name("Down"):
  125. self.y += self.POS_INCREMENT/self.zoom_ratio
  126. else:
  127. return False
  128. self.bound_vals()
  129. self.queue_draw()
  130. self.position_changed()
  131. return True
  132. def on_area_button_press(self, area, event):
  133. if event.button == 2 or event.button == 1:
  134. window = self.get_window()
  135. window.set_cursor(Gdk.Cursor(Gdk.CursorType.FLEUR))
  136. self.prevmousex = event.x
  137. self.prevmousey = event.y
  138. if event.type not in (Gdk.EventType.BUTTON_PRESS, Gdk.EventType.BUTTON_RELEASE):
  139. return False
  140. return False
  141. def on_area_button_release(self, area, event):
  142. if event.button == 2 or event.button == 1:
  143. window = self.get_window()
  144. window.set_cursor(Gdk.Cursor(Gdk.CursorType.ARROW))
  145. self.prevmousex = None
  146. self.prevmousey = None
  147. return True
  148. return False
  149. def on_area_scroll_event(self, area, event):
  150. if event.state & Gdk.CONTROL_MASK:
  151. if event.direction == Gdk.SCROLL_UP:
  152. self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT)
  153. return True
  154. if event.direction == Gdk.SCROLL_DOWN:
  155. self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT)
  156. return True
  157. return False
  158. def on_area_motion_notify(self, area, event):
  159. state = event.state
  160. if state & Gdk.ModifierType.BUTTON2_MASK or state & Gdk.ModifierType.BUTTON1_MASK:
  161. x, y = int(event.x), int(event.y)
  162. # pan the image
  163. self.x += (self.prevmousex - x)/self.zoom_ratio
  164. self.y += (self.prevmousey - y)/self.zoom_ratio
  165. self.bound_vals()
  166. self.queue_draw()
  167. self.prevmousex = x
  168. self.prevmousey = y
  169. self.position_changed()
  170. return True
  171. def on_allocation_size_changed(self, widget, allocation):
  172. self.hadj.page_size = allocation.width
  173. self.hadj.page_increment = allocation.width * 0.9
  174. self.vadj.page_size = allocation.height
  175. self.vadj.page_increment = allocation.height * 0.9
  176. self.our_width = allocation.width
  177. if self.chart_width < self.our_width:
  178. self.our_width = self.chart_width
  179. self.our_height = allocation.height
  180. if self.chart_height < self.our_height:
  181. self.our_height = self.chart_height
  182. self._set_scroll_adjustments()
  183. def _set_adj_upper(self, adj, upper):
  184. if adj.get_upper() != upper:
  185. adj.set_upper(upper)
  186. def _set_scroll_adjustments(self):
  187. self._set_adj_upper (self.hadj, self.zoom_ratio * (self.chart_width - self.our_width))
  188. self._set_adj_upper (self.vadj, self.zoom_ratio * (self.chart_height - self.our_height))
  189. def on_adjustments_changed(self, adj):
  190. self.x = self.hadj.get_value() / self.zoom_ratio
  191. self.y = self.vadj.get_value() / self.zoom_ratio
  192. self.queue_draw()
  193. def on_position_changed(self, widget, x, y):
  194. self.hadj.set_value(x * self.zoom_ratio)
  195. #self.hadj.value_changed()
  196. self.vadj.set_value(y * self.zoom_ratio)
  197. class PyBootchartShell(gtk.VBox):
  198. ui = '''
  199. <ui>
  200. <toolbar name="ToolBar">
  201. <toolitem action="Expand"/>
  202. <toolitem action="Contract"/>
  203. <separator/>
  204. <toolitem action="ZoomIn"/>
  205. <toolitem action="ZoomOut"/>
  206. <toolitem action="ZoomFit"/>
  207. <toolitem action="Zoom100"/>
  208. </toolbar>
  209. </ui>
  210. '''
  211. def __init__(self, window, trace, options, xscale):
  212. gtk.VBox.__init__(self)
  213. self.widget2 = PyBootchartWidget(trace, options, xscale)
  214. # Create a UIManager instance
  215. uimanager = self.uimanager = gtk.UIManager()
  216. # Add the accelerator group to the toplevel window
  217. accelgroup = uimanager.get_accel_group()
  218. window.add_accel_group(accelgroup)
  219. # Create an ActionGroup
  220. actiongroup = gtk.ActionGroup('Actions')
  221. self.actiongroup = actiongroup
  222. # Create actions
  223. actiongroup.add_actions((
  224. ('Expand', gtk.STOCK_ADD, None, None, None, self.widget2.on_expand),
  225. ('Contract', gtk.STOCK_REMOVE, None, None, None, self.widget2.on_contract),
  226. ('ZoomIn', gtk.STOCK_ZOOM_IN, None, None, None, self.widget2.on_zoom_in),
  227. ('ZoomOut', gtk.STOCK_ZOOM_OUT, None, None, None, self.widget2.on_zoom_out),
  228. ('ZoomFit', gtk.STOCK_ZOOM_FIT, 'Fit Width', None, None, self.widget2.on_zoom_fit),
  229. ('Zoom100', gtk.STOCK_ZOOM_100, None, None, None, self.widget2.on_zoom_100),
  230. ))
  231. # Add the actiongroup to the uimanager
  232. uimanager.insert_action_group(actiongroup, 0)
  233. # Add a UI description
  234. uimanager.add_ui_from_string(self.ui)
  235. # Scrolled window
  236. scrolled = gtk.ScrolledWindow(self.widget2.hadj, self.widget2.vadj)
  237. scrolled.add(self.widget2)
  238. #scrolled.set_hadjustment()
  239. #scrolled.set_vadjustment(self.widget2.vadj)
  240. scrolled.set_policy(gtk.PolicyType.ALWAYS, gtk.PolicyType.ALWAYS)
  241. # toolbar / h-box
  242. hbox = gtk.HBox(False, 8)
  243. # Create a Toolbar
  244. toolbar = uimanager.get_widget('/ToolBar')
  245. hbox.pack_start(toolbar, True, True, 0)
  246. if not options.kernel_only:
  247. # Misc. options
  248. button = gtk.CheckButton("Show more")
  249. button.connect ('toggled', self.widget2.show_toggled)
  250. button.set_active(options.app_options.show_all)
  251. hbox.pack_start (button, False, True, 0)
  252. self.pack_start(hbox, False, True, 0)
  253. self.pack_start(scrolled, True, True, 0)
  254. self.show_all()
  255. def grab_focus(self, window):
  256. window.set_focus(self.widget2)
  257. class PyBootchartWindow(gtk.Window):
  258. def __init__(self, trace, app_options):
  259. gtk.Window.__init__(self)
  260. window = self
  261. window.set_title("Bootchart %s" % trace.filename)
  262. window.set_default_size(750, 550)
  263. tab_page = gtk.Notebook()
  264. tab_page.show()
  265. window.add(tab_page)
  266. full_opts = RenderOptions(app_options)
  267. full_tree = PyBootchartShell(window, trace, full_opts, 1.0)
  268. tab_page.append_page (full_tree, gtk.Label("Full tree"))
  269. if trace.kernel is not None and len (trace.kernel) > 2:
  270. kernel_opts = RenderOptions(app_options)
  271. kernel_opts.cumulative = False
  272. kernel_opts.charts = False
  273. kernel_opts.kernel_only = True
  274. kernel_tree = PyBootchartShell(window, trace, kernel_opts, 5.0)
  275. tab_page.append_page (kernel_tree, gtk.Label("Kernel boot"))
  276. full_tree.grab_focus(self)
  277. self.show()
  278. def show(trace, options):
  279. win = PyBootchartWindow(trace, options)
  280. win.connect('destroy', gtk.main_quit)
  281. gtk.main()