Initial commit after ImageAnalyzer and Plotfile2 merge.
[pyplotsuite.git] / common / gtk_generic_windows.py
blob5b2fed88700d6e57e7ecf2ec1dadd838f554c7f4
1 # GktGW -- A set of "template" classes to quick make Glade+pygtk apps.
2 # Version: 0.1
4 # Copyright (C) 2006-2007 Antonio Ingargiola <tritemio@gmail.com>
6 # This library is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 """
12 Abstract classes for Gtk Windows and dialogs described by Glade files.
13 """
15 import sys
17 try:
18 import pygtk
19 pygtk.require("2.0")
20 except:
21 pass
23 try:
24 import gtk
25 import gtk.glade
26 except:
27 print '\n You need to install pyGTK or GTK 2.x\n'
28 sys.exit(1)
30 try:
31 from matplotlib.figure import Figure
32 except:
33 print '\n You need to install Matplotlib (see README file).\n'
34 sys.exit(1)
36 # Import from matplotlib the FigureCanvas with GTKAgg backend
37 from matplotlib.backends.backend_gtkagg \
38 import FigureCanvasGTKAgg as FigureCanvas
40 # Import the matplotlib Toolbar2
41 from matplotlib.backends.backend_gtk \
42 import NavigationToolbar2GTK as NavigationToolbar
44 debug = False
46 class GenericWindow:
47 """
48 Abstract class for a window described in a Glade file.
49 """
50 def __init__(self, windowname, gladefile, autoconnect=True):
51 self.windowname = windowname
52 self.widgetTree = gtk.glade.XML(gladefile, self.windowname)
53 self.window = self.widgetTree.get_widget(self.windowname)
54 if autoconnect: self.widgetTree.signal_autoconnect(self)
57 class GenericMainWindow(GenericWindow):
58 """
59 Abstract class to be enherited by a main window.
60 """
61 def start(self):
62 gtk.main()
63 def on_delete_event(self, widget, *args):
64 if debug: print 'delete_event:', widget
65 return False
66 def on_destroy(self, widget, data=None):
67 if debug: print 'destroy:', widget
68 gtk.main_quit()
71 class GenericPlotWindow(GenericWindow):
72 """
73 This class implements a generic plot window with (optional) matplotlib
74 toolbar.
75 """
76 def __init__(self, windowname, gladefile, autoconnect=True,
77 makeAxis=True, makeToolbar=True):
78 GenericWindow.__init__(self, windowname, gladefile, autoconnect)
80 # Create the figure, the axes and the canvas
81 self.figure = Figure()
82 self.canvas = FigureCanvas(self.figure)
83 if makeAxis:
84 self.axis = self.figure.add_subplot(111)
86 # Add the canvas to the container
87 self.container = self.widgetTree.get_widget('CanvasAlignment')
88 self.container.add(self.canvas)
90 # Set the status bar
91 self.statusBar = self.widgetTree.get_widget('statusbar')
92 self.context = self.statusBar.get_context_id('Messagges')
94 # Create the matplotlib toolbar (if requested)
95 if makeToolbar:
96 self.toolbar = NavigationToolbar(self.canvas, self.window)
97 toolbar_container = self.widgetTree.get_widget('ToolbarAlignment')
98 toolbar_container.add(self.toolbar)
100 def writeStatusBar(self, string):
101 self.statusBar.push(self.context, string)
103 def on_quit_activate(self, widget, *args):
105 Menu exit command callback.
107 self.on_destroy(widget, *args)
110 class GenericMainPlotWindow(GenericPlotWindow, GenericMainWindow):
111 pass
114 class GenericSecondaryWindow(GenericWindow):
116 Abstract class to be enherited by each "non-main" window.
118 def on_delete_event(self, widget, *args):
119 if debug: print 'delete_event:', widget
120 return False
122 def on_destroy(self, widget, data=None):
123 # Instead of quitting from gtk, just destroy the window
124 if debug: print 'destroy:', widget
125 self.window.destroy()
128 class GenericSecondaryPlotWindow(GenericPlotWindow, GenericSecondaryWindow):
129 pass
132 class ChildWindow(GenericSecondaryWindow):
134 Abstract class to be enherited by each window spawed by the main window.
136 def __init__(self, windowname, gladefile, callerApp, autoconnect=True):
137 GenericSecondaryWindow.__init__(self, windowname, gladefile,
138 autoconnect)
139 self.callerApp = callerApp
142 class DialogWindowWithCancel(ChildWindow):
144 Abstract class to be enherited by each dialog window with a cancel button.
146 def on_cancelButton_clicked(self, widget, *args):
147 if debug: print 'Cancel clicked', widget
148 self.window.destroy()
151 class GenericOpenFileDialog(DialogWindowWithCancel):
153 This class implements the generic code for an "Open File" dialog.
155 def __init__(self, windowname, gladefile, callerApp):
156 DialogWindowWithCancel.__init__(self, windowname, gladefile, callerApp)
158 def openSelectedFile(self):
159 # Override this method to load the file in your app
160 # self.filename contains the filename selected by the user
161 pass
163 def on_openButton_clicked(self, widget, *args):
164 if debug: print "Open button clicked", self.window.get_filename()
165 self.filename = self.window.get_filename()
166 if self.filename != None:
167 self.openSelectedFile()
169 def on_openfileDialog_file_activated(self, widget, *args):
170 if debug: print "Open File Dialog: file_activated", \
171 self.window.get_filename()
173 def on_openfileDialog_response(self, widget, *args):
174 print "\nOpen File Dialog: response event"
177 class MyNavigationToolbar(NavigationToolbar):
179 This class is derived from the matplotlib Toolbar2 with one toggle button
180 added at the end.
182 def __init__(self, canvas, window, ButtonCallBack, icon_fname,
183 label=None, tooltip=None):
184 NavigationToolbar.__init__(self, canvas, window)
185 icon = gtk.Image()
186 icon.set_from_file( icon_fname )
187 button = gtk.ToggleToolButton(None)
188 button.set_label(label)
189 button.set_icon_widget(icon)
190 button.connect('toggled', ButtonCallBack)
191 button.show_all()
192 # I don't know why the tooltips don't work here
193 self.set_tooltips(True)
194 tips = gtk.Tooltips()
195 tips.set_tip(button, tooltip)
196 tips.enable()
197 self.insert(button, 8)