|
40 | 40 | import io
|
41 | 41 |
|
42 | 42 | import numpy as np
|
| 43 | +import matplotlib # temporary )assuming we refactor where marked below) |
43 | 44 | import matplotlib.cbook as cbook
|
44 | 45 | import matplotlib.colors as colors
|
45 | 46 | import matplotlib.transforms as transforms
|
@@ -2534,6 +2535,165 @@ def key_press_handler(event, canvas, toolbar=None):
|
2534 | 2535 | class NonGuiException(Exception):
|
2535 | 2536 | pass
|
2536 | 2537 |
|
| 2538 | +class WindowEvent(object): |
| 2539 | + def __init__(self, name, window): |
| 2540 | + self.name = name |
| 2541 | + self.window = window |
| 2542 | + |
| 2543 | +class WindowBase(object): |
| 2544 | + def __init__(self, title): |
| 2545 | + self._callbacks = cbook.CallbackRegistry() |
| 2546 | + |
| 2547 | + def mpl_connect(self, s, func): |
| 2548 | + return self._callbacks.connect(s, func) |
| 2549 | + |
| 2550 | + def mpl_disconnect(self, cid): |
| 2551 | + return self._callbacks.disconnect(cid) |
| 2552 | + |
| 2553 | + def show(self): |
| 2554 | + """ |
| 2555 | + For GUI backends, show the figure window and redraw. |
| 2556 | + For non-GUI backends, raise an exception to be caught |
| 2557 | + by :meth:`~matplotlib.figure.Figure.show`, for an |
| 2558 | + optional warning. |
| 2559 | + """ |
| 2560 | + raise NonGuiException() |
| 2561 | + |
| 2562 | + def destroy(self): |
| 2563 | + pass |
| 2564 | + |
| 2565 | + def set_fullscreen(self, fullscreen): |
| 2566 | + pass |
| 2567 | + |
| 2568 | + def resize(self, w, h): |
| 2569 | + """"For gui backends, resize the window (in pixels).""" |
| 2570 | + pass |
| 2571 | + |
| 2572 | + def get_window_title(self): |
| 2573 | + """ |
| 2574 | + Get the title text of the window containing the figure. |
| 2575 | + Return None for non-GUI backends (e.g., a PS backend). |
| 2576 | + """ |
| 2577 | + return 'image' |
| 2578 | + |
| 2579 | + def set_window_title(self, title): |
| 2580 | + """ |
| 2581 | + Set the title text of the window containing the figure. Note that |
| 2582 | + this has no effect for non-GUI backends (e.g., a PS backend). |
| 2583 | + """ |
| 2584 | + pass |
| 2585 | + |
| 2586 | + def add_element_to_window(self, element, expand, fill, padding, from_start=False): |
| 2587 | + """ Adds a gui widget to the window. |
| 2588 | + This has no effect for non-GUI backends |
| 2589 | + """ |
| 2590 | + pass |
| 2591 | + |
| 2592 | + def terminate_backend(self): |
| 2593 | + """Method to terminate the usage of the backend |
| 2594 | + """ |
| 2595 | + # TODO refactor me out on second pass |
| 2596 | + pass |
| 2597 | + |
| 2598 | + def destroy_event(self, *args): |
| 2599 | + s = 'window_destroy_event' |
| 2600 | + event = WindowEvent(s, self) |
| 2601 | + self._callbacks.process(s, event) |
| 2602 | + |
| 2603 | + |
| 2604 | +class FigureManager(object): |
| 2605 | + def __init__(self, canvas, num, classes): |
| 2606 | + self._classes = classes |
| 2607 | + self.canvas = canvas |
| 2608 | + canvas.manager = self |
| 2609 | + self.num = num |
| 2610 | + |
| 2611 | + self.key_press_handler_id = self.canvas.mpl_connect('key_press_event', |
| 2612 | + self.key_press) |
| 2613 | + |
| 2614 | + self.window = classes['Window']('Figure %d' % num) |
| 2615 | + self.window.mpl_connect('window_destroy_event', self._destroy) |
| 2616 | + |
| 2617 | + w = int(self.canvas.figure.bbox.width) |
| 2618 | + h = int(self.canvas.figure.bbox.height) |
| 2619 | + |
| 2620 | + self.window.add_element_to_window(self.canvas, True, True, 0, True) |
| 2621 | + |
| 2622 | + self.toolbar = self._get_toolbar(canvas) |
| 2623 | + if self.toolbar is not None: |
| 2624 | + h += self.window.add_element_to_window(self.toolbar, False, False, 0) |
| 2625 | + |
| 2626 | + self.window.set_default_size(w,h) |
| 2627 | + |
| 2628 | + # Refactor this? If so, delete import matplotlib from above. |
| 2629 | + if matplotlib.is_interactive(): |
| 2630 | + self.window.show() |
| 2631 | + |
| 2632 | + def notify_axes_change(fig): |
| 2633 | + 'this will be called whenever the current axes is changed' |
| 2634 | + if self.toolbar is not None: self.toolbar.update() |
| 2635 | + self.canvas.figure.add_axobserver(notify_axes_change) |
| 2636 | + |
| 2637 | + self.canvas.grab_focus() |
| 2638 | + |
| 2639 | + def key_press(self, event): |
| 2640 | + """ |
| 2641 | + Implement the default mpl key bindings defined at |
| 2642 | + :ref:`key-event-handling` |
| 2643 | + """ |
| 2644 | + key_press_handler(event, self.canvas, self.canvas.toolbar) |
| 2645 | + |
| 2646 | + def _destroy(self, event): |
| 2647 | + Gcf.destroy(self.num) # TODO refactor me out of here on second pass! |
| 2648 | + |
| 2649 | + def destroy(self, *args): |
| 2650 | + self.window.destroy() |
| 2651 | + self.canvas.destroy() |
| 2652 | + if self.toolbar: |
| 2653 | + self.toolbar.destroy() |
| 2654 | + |
| 2655 | + # TODO refactor out on second pass |
| 2656 | + if Gcf.get_num_fig_managers()==0 and not matplotlib.is_interactive(): |
| 2657 | + self.window.terminate_backend() |
| 2658 | + |
| 2659 | + def show(self): |
| 2660 | + self.window.show() |
| 2661 | + |
| 2662 | + def full_screen_toggle(self): |
| 2663 | + self._full_screen_flag = not self._full_screen_flag |
| 2664 | + self.window.set_fullscreen(self._full_screen_flag) |
| 2665 | + |
| 2666 | + def resize(self, w, h): |
| 2667 | + self.window.resize(w,h) |
| 2668 | + |
| 2669 | + def get_window_title(self): |
| 2670 | + """ |
| 2671 | + Get the title text of the window containing the figure. |
| 2672 | + Return None for non-GUI backends (e.g., a PS backend). |
| 2673 | + """ |
| 2674 | + return self.window.get_window_title() |
| 2675 | + |
| 2676 | + def set_window_title(self, title): |
| 2677 | + """ |
| 2678 | + Set the title text of the window containing the figure. Note that |
| 2679 | + this has no effect for non-GUI backends (e.g., a PS backend). |
| 2680 | + """ |
| 2681 | + self.window.set_window_title(title) |
| 2682 | + |
| 2683 | + def show_popup(self, msg): |
| 2684 | + """ |
| 2685 | + Display message in a popup -- GUI only |
| 2686 | + """ |
| 2687 | + pass |
| 2688 | + |
| 2689 | + def _get_toolbar(self, canvas): |
| 2690 | + # must be inited after the window, drawingArea and figure |
| 2691 | + # attrs are set |
| 2692 | + if rcParams['toolbar'] == 'toolbar2': |
| 2693 | + toolbar = self._classes['Toolbar2'](canvas, self.window) |
| 2694 | + else: |
| 2695 | + toolbar = None |
| 2696 | + return toolbar |
2537 | 2697 |
|
2538 | 2698 | class FigureManagerBase(object):
|
2539 | 2699 | """
|
|
0 commit comments