Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # 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 | |
| 16 | import gobject |
| 17 | import gtk |
| 18 | import gtk.gdk |
| 19 | import gtk.keysyms |
| 20 | from . import draw |
| 21 | from .draw import RenderOptions |
| 22 | |
| 23 | class PyBootchartWidget(gtk.DrawingArea): |
| 24 | __gsignals__ = { |
| 25 | 'expose-event': 'override', |
| 26 | 'clicked' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gtk.gdk.Event)), |
| 27 | 'position-changed' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT, gobject.TYPE_INT)), |
| 28 | 'set-scroll-adjustments' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gtk.Adjustment, gtk.Adjustment)) |
| 29 | } |
| 30 | |
| 31 | def __init__(self, trace, options, xscale): |
| 32 | gtk.DrawingArea.__init__(self) |
| 33 | |
| 34 | self.trace = trace |
| 35 | self.options = options |
| 36 | |
| 37 | self.set_flags(gtk.CAN_FOCUS) |
| 38 | |
| 39 | self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK) |
| 40 | self.connect("button-press-event", self.on_area_button_press) |
| 41 | self.connect("button-release-event", self.on_area_button_release) |
| 42 | self.add_events(gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK | gtk.gdk.BUTTON_RELEASE_MASK) |
| 43 | self.connect("motion-notify-event", self.on_area_motion_notify) |
| 44 | self.connect("scroll-event", self.on_area_scroll_event) |
| 45 | self.connect('key-press-event', self.on_key_press_event) |
| 46 | |
| 47 | self.connect('set-scroll-adjustments', self.on_set_scroll_adjustments) |
| 48 | self.connect("size-allocate", self.on_allocation_size_changed) |
| 49 | self.connect("position-changed", self.on_position_changed) |
| 50 | |
| 51 | self.zoom_ratio = 1.0 |
| 52 | self.xscale = xscale |
| 53 | self.x, self.y = 0.0, 0.0 |
| 54 | |
| 55 | self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace) |
| 56 | self.hadj = None |
| 57 | self.vadj = None |
| 58 | self.hadj_changed_signal_id = None |
| 59 | self.vadj_changed_signal_id = None |
| 60 | |
| 61 | def do_expose_event(self, event): |
| 62 | cr = self.window.cairo_create() |
| 63 | |
| 64 | # set a clip region for the expose event |
| 65 | cr.rectangle( |
| 66 | event.area.x, event.area.y, |
| 67 | event.area.width, event.area.height |
| 68 | ) |
| 69 | cr.clip() |
| 70 | self.draw(cr, self.get_allocation()) |
| 71 | return False |
| 72 | |
| 73 | def draw(self, cr, rect): |
| 74 | cr.set_source_rgba(1.0, 1.0, 1.0, 1.0) |
| 75 | cr.paint() |
| 76 | cr.scale(self.zoom_ratio, self.zoom_ratio) |
| 77 | cr.translate(-self.x, -self.y) |
| 78 | draw.render(cr, self.options, self.xscale, self.trace) |
| 79 | |
| 80 | def position_changed(self): |
| 81 | self.emit("position-changed", self.x, self.y) |
| 82 | |
| 83 | ZOOM_INCREMENT = 1.25 |
| 84 | |
| 85 | def zoom_image (self, zoom_ratio): |
| 86 | self.zoom_ratio = zoom_ratio |
| 87 | self._set_scroll_adjustments (self.hadj, self.vadj) |
| 88 | self.queue_draw() |
| 89 | |
| 90 | def zoom_to_rect (self, rect): |
| 91 | zoom_ratio = float(rect.width)/float(self.chart_width) |
| 92 | self.zoom_image(zoom_ratio) |
| 93 | self.x = 0 |
| 94 | self.position_changed() |
| 95 | |
| 96 | def set_xscale(self, xscale): |
| 97 | old_mid_x = self.x + self.hadj.page_size / 2 |
| 98 | self.xscale = xscale |
| 99 | self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace) |
| 100 | new_x = old_mid_x |
| 101 | self.zoom_image (self.zoom_ratio) |
| 102 | |
| 103 | def on_expand(self, action): |
| 104 | self.set_xscale (int(self.xscale * 1.5 + 0.5)) |
| 105 | |
| 106 | def on_contract(self, action): |
| 107 | self.set_xscale (max(int(self.xscale / 1.5), 1)) |
| 108 | |
| 109 | def on_zoom_in(self, action): |
| 110 | self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT) |
| 111 | |
| 112 | def on_zoom_out(self, action): |
| 113 | self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT) |
| 114 | |
| 115 | def on_zoom_fit(self, action): |
| 116 | self.zoom_to_rect(self.get_allocation()) |
| 117 | |
| 118 | def on_zoom_100(self, action): |
| 119 | self.zoom_image(1.0) |
| 120 | self.set_xscale(1.0) |
| 121 | |
| 122 | def show_toggled(self, button): |
| 123 | self.options.app_options.show_all = button.get_property ('active') |
| 124 | self.chart_width, self.chart_height = draw.extents(self.options, self.xscale, self.trace) |
| 125 | self._set_scroll_adjustments(self.hadj, self.vadj) |
| 126 | self.queue_draw() |
| 127 | |
| 128 | POS_INCREMENT = 100 |
| 129 | |
| 130 | def on_key_press_event(self, widget, event): |
| 131 | if event.keyval == gtk.keysyms.Left: |
| 132 | self.x -= self.POS_INCREMENT/self.zoom_ratio |
| 133 | elif event.keyval == gtk.keysyms.Right: |
| 134 | self.x += self.POS_INCREMENT/self.zoom_ratio |
| 135 | elif event.keyval == gtk.keysyms.Up: |
| 136 | self.y -= self.POS_INCREMENT/self.zoom_ratio |
| 137 | elif event.keyval == gtk.keysyms.Down: |
| 138 | self.y += self.POS_INCREMENT/self.zoom_ratio |
| 139 | else: |
| 140 | return False |
| 141 | self.queue_draw() |
| 142 | self.position_changed() |
| 143 | return True |
| 144 | |
| 145 | def on_area_button_press(self, area, event): |
| 146 | if event.button == 2 or event.button == 1: |
| 147 | area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR)) |
| 148 | self.prevmousex = event.x |
| 149 | self.prevmousey = event.y |
| 150 | if event.type not in (gtk.gdk.BUTTON_PRESS, gtk.gdk.BUTTON_RELEASE): |
| 151 | return False |
| 152 | return False |
| 153 | |
| 154 | def on_area_button_release(self, area, event): |
| 155 | if event.button == 2 or event.button == 1: |
| 156 | area.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.ARROW)) |
| 157 | self.prevmousex = None |
| 158 | self.prevmousey = None |
| 159 | return True |
| 160 | return False |
| 161 | |
| 162 | def on_area_scroll_event(self, area, event): |
| 163 | if event.state & gtk.gdk.CONTROL_MASK: |
| 164 | if event.direction == gtk.gdk.SCROLL_UP: |
| 165 | self.zoom_image(self.zoom_ratio * self.ZOOM_INCREMENT) |
| 166 | return True |
| 167 | if event.direction == gtk.gdk.SCROLL_DOWN: |
| 168 | self.zoom_image(self.zoom_ratio / self.ZOOM_INCREMENT) |
| 169 | return True |
| 170 | return False |
| 171 | |
| 172 | def on_area_motion_notify(self, area, event): |
| 173 | state = event.state |
| 174 | if state & gtk.gdk.BUTTON2_MASK or state & gtk.gdk.BUTTON1_MASK: |
| 175 | x, y = int(event.x), int(event.y) |
| 176 | # pan the image |
| 177 | self.x += (self.prevmousex - x)/self.zoom_ratio |
| 178 | self.y += (self.prevmousey - y)/self.zoom_ratio |
| 179 | self.queue_draw() |
| 180 | self.prevmousex = x |
| 181 | self.prevmousey = y |
| 182 | self.position_changed() |
| 183 | return True |
| 184 | |
| 185 | def on_set_scroll_adjustments(self, area, hadj, vadj): |
| 186 | self._set_scroll_adjustments (hadj, vadj) |
| 187 | |
| 188 | def on_allocation_size_changed(self, widget, allocation): |
| 189 | self.hadj.page_size = allocation.width |
| 190 | self.hadj.page_increment = allocation.width * 0.9 |
| 191 | self.vadj.page_size = allocation.height |
| 192 | self.vadj.page_increment = allocation.height * 0.9 |
| 193 | |
| 194 | def _set_adj_upper(self, adj, upper): |
| 195 | changed = False |
| 196 | value_changed = False |
| 197 | |
| 198 | if adj.upper != upper: |
| 199 | adj.upper = upper |
| 200 | changed = True |
| 201 | |
| 202 | max_value = max(0.0, upper - adj.page_size) |
| 203 | if adj.value > max_value: |
| 204 | adj.value = max_value |
| 205 | value_changed = True |
| 206 | |
| 207 | if changed: |
| 208 | adj.changed() |
| 209 | if value_changed: |
| 210 | adj.value_changed() |
| 211 | |
| 212 | def _set_scroll_adjustments(self, hadj, vadj): |
| 213 | if hadj == None: |
| 214 | hadj = gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) |
| 215 | if vadj == None: |
| 216 | vadj = gtk.Adjustment(0.0, 0.0, 0.0, 0.0, 0.0, 0.0) |
| 217 | |
| 218 | if self.hadj_changed_signal_id != None and \ |
| 219 | self.hadj != None and hadj != self.hadj: |
| 220 | self.hadj.disconnect (self.hadj_changed_signal_id) |
| 221 | if self.vadj_changed_signal_id != None and \ |
| 222 | self.vadj != None and vadj != self.vadj: |
| 223 | self.vadj.disconnect (self.vadj_changed_signal_id) |
| 224 | |
| 225 | if hadj != None: |
| 226 | self.hadj = hadj |
| 227 | self._set_adj_upper (self.hadj, self.zoom_ratio * self.chart_width) |
| 228 | self.hadj_changed_signal_id = self.hadj.connect('value-changed', self.on_adjustments_changed) |
| 229 | |
| 230 | if vadj != None: |
| 231 | self.vadj = vadj |
| 232 | self._set_adj_upper (self.vadj, self.zoom_ratio * self.chart_height) |
| 233 | self.vadj_changed_signal_id = self.vadj.connect('value-changed', self.on_adjustments_changed) |
| 234 | |
| 235 | def on_adjustments_changed(self, adj): |
| 236 | self.x = self.hadj.value / self.zoom_ratio |
| 237 | self.y = self.vadj.value / self.zoom_ratio |
| 238 | self.queue_draw() |
| 239 | |
| 240 | def on_position_changed(self, widget, x, y): |
| 241 | self.hadj.value = x * self.zoom_ratio |
| 242 | self.vadj.value = y * self.zoom_ratio |
| 243 | |
| 244 | PyBootchartWidget.set_set_scroll_adjustments_signal('set-scroll-adjustments') |
| 245 | |
| 246 | class PyBootchartShell(gtk.VBox): |
| 247 | ui = ''' |
| 248 | <ui> |
| 249 | <toolbar name="ToolBar"> |
| 250 | <toolitem action="Expand"/> |
| 251 | <toolitem action="Contract"/> |
| 252 | <separator/> |
| 253 | <toolitem action="ZoomIn"/> |
| 254 | <toolitem action="ZoomOut"/> |
| 255 | <toolitem action="ZoomFit"/> |
| 256 | <toolitem action="Zoom100"/> |
| 257 | </toolbar> |
| 258 | </ui> |
| 259 | ''' |
| 260 | def __init__(self, window, trace, options, xscale): |
| 261 | gtk.VBox.__init__(self) |
| 262 | |
| 263 | self.widget = PyBootchartWidget(trace, options, xscale) |
| 264 | |
| 265 | # Create a UIManager instance |
| 266 | uimanager = self.uimanager = gtk.UIManager() |
| 267 | |
| 268 | # Add the accelerator group to the toplevel window |
| 269 | accelgroup = uimanager.get_accel_group() |
| 270 | window.add_accel_group(accelgroup) |
| 271 | |
| 272 | # Create an ActionGroup |
| 273 | actiongroup = gtk.ActionGroup('Actions') |
| 274 | self.actiongroup = actiongroup |
| 275 | |
| 276 | # Create actions |
| 277 | actiongroup.add_actions(( |
| 278 | ('Expand', gtk.STOCK_ADD, None, None, None, self.widget.on_expand), |
| 279 | ('Contract', gtk.STOCK_REMOVE, None, None, None, self.widget.on_contract), |
| 280 | ('ZoomIn', gtk.STOCK_ZOOM_IN, None, None, None, self.widget.on_zoom_in), |
| 281 | ('ZoomOut', gtk.STOCK_ZOOM_OUT, None, None, None, self.widget.on_zoom_out), |
| 282 | ('ZoomFit', gtk.STOCK_ZOOM_FIT, 'Fit Width', None, None, self.widget.on_zoom_fit), |
| 283 | ('Zoom100', gtk.STOCK_ZOOM_100, None, None, None, self.widget.on_zoom_100), |
| 284 | )) |
| 285 | |
| 286 | # Add the actiongroup to the uimanager |
| 287 | uimanager.insert_action_group(actiongroup, 0) |
| 288 | |
| 289 | # Add a UI description |
| 290 | uimanager.add_ui_from_string(self.ui) |
| 291 | |
| 292 | # Scrolled window |
| 293 | scrolled = gtk.ScrolledWindow() |
| 294 | scrolled.add(self.widget) |
| 295 | |
| 296 | # toolbar / h-box |
| 297 | hbox = gtk.HBox(False, 8) |
| 298 | |
| 299 | # Create a Toolbar |
| 300 | toolbar = uimanager.get_widget('/ToolBar') |
| 301 | hbox.pack_start(toolbar, True, True) |
| 302 | |
| 303 | if not options.kernel_only: |
| 304 | # Misc. options |
| 305 | button = gtk.CheckButton("Show more") |
| 306 | button.connect ('toggled', self.widget.show_toggled) |
| 307 | button.set_active(options.app_options.show_all) |
| 308 | hbox.pack_start (button, False, True) |
| 309 | |
| 310 | self.pack_start(hbox, False) |
| 311 | self.pack_start(scrolled) |
| 312 | self.show_all() |
| 313 | |
| 314 | def grab_focus(self, window): |
| 315 | window.set_focus(self.widget) |
| 316 | |
| 317 | |
| 318 | class PyBootchartWindow(gtk.Window): |
| 319 | |
| 320 | def __init__(self, trace, app_options): |
| 321 | gtk.Window.__init__(self) |
| 322 | |
| 323 | window = self |
| 324 | window.set_title("Bootchart %s" % trace.filename) |
| 325 | window.set_default_size(750, 550) |
| 326 | |
| 327 | tab_page = gtk.Notebook() |
| 328 | tab_page.show() |
| 329 | window.add(tab_page) |
| 330 | |
| 331 | full_opts = RenderOptions(app_options) |
| 332 | full_tree = PyBootchartShell(window, trace, full_opts, 1.0) |
| 333 | tab_page.append_page (full_tree, gtk.Label("Full tree")) |
| 334 | |
| 335 | if trace.kernel is not None and len (trace.kernel) > 2: |
| 336 | kernel_opts = RenderOptions(app_options) |
| 337 | kernel_opts.cumulative = False |
| 338 | kernel_opts.charts = False |
| 339 | kernel_opts.kernel_only = True |
| 340 | kernel_tree = PyBootchartShell(window, trace, kernel_opts, 5.0) |
| 341 | tab_page.append_page (kernel_tree, gtk.Label("Kernel boot")) |
| 342 | |
| 343 | full_tree.grab_focus(self) |
| 344 | self.show() |
| 345 | |
| 346 | |
| 347 | def show(trace, options): |
| 348 | win = PyBootchartWindow(trace, options) |
| 349 | win.connect('destroy', gtk.main_quit) |
| 350 | gtk.main() |