1 # GktGW -- A set of "template" classes to quick make Glade+pygtk apps.
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.
12 Abstract classes for Gtk Windows and dialogs described by Glade files.
27 print '\n You need to install pyGTK or GTK 2.x\n'
31 from matplotlib
.figure
import Figure
33 print '\n You need to install Matplotlib (see README file).\n'
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
48 Abstract class for a window described in a Glade file.
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
):
59 Abstract class to be enherited by a main window.
63 def on_delete_event(self
, widget
, *args
):
64 if debug
: print 'delete_event:', widget
66 def on_destroy(self
, widget
, data
=None):
67 if debug
: print 'destroy:', widget
71 class GenericPlotWindow(GenericWindow
):
73 This class implements a generic plot window with (optional) matplotlib
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
)
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
)
91 self
.statusBar
= self
.widgetTree
.get_widget('statusbar')
92 self
.context
= self
.statusBar
.get_context_id('Messagges')
94 # Create the matplotlib toolbar (if requested)
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
):
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
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
):
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
,
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
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"
176 class GenericSaveFileDialog(DialogWindowWithCancel
):
178 This class implements a generic "Save File" dialog.
180 def __init__(self
, windowname
, gladefile
, callerApp
):
181 DialogWindowWithCancel
.__init
__(self
, windowname
, gladefile
, callerApp
)
183 def saveToSelectedFile(self
):
184 # Override this method to load the file in your app
185 # self.filename contains the filename selected by the user
188 def on_saveButton_clicked(self
, widget
, *args
):
189 if debug
: print "save button clicked", self
.window
.get_filename()
190 self
.filename
= self
.window
.get_filename()
191 if self
.filename
!= None:
192 self
.saveToSelectedFile()
194 def on_openfileDialog_file_activated(self
, widget
, *args
):
196 print "Save File Dialog: file_activated", self
.window
.get_filename()
198 def on_openfileDialog_response(self
, widget
, *args
):
199 if debug
: print "\nSave File Dialog: response event"
201 def on_cancelButton_clicked(self
, widget
, *args
):
202 # Don't destroy the dialog so we remeber the folder next time
206 class MyNavigationToolbar(NavigationToolbar
):
208 This class is derived from the matplotlib Toolbar2 with one toggle button
211 def __init__(self
, canvas
, window
, ButtonCallBack
, icon_fname
,
212 label
=None, tooltip
=None):
213 NavigationToolbar
.__init
__(self
, canvas
, window
)
215 icon
.set_from_file( icon_fname
)
216 button
= gtk
.ToggleToolButton(None)
217 button
.set_label(label
)
218 button
.set_icon_widget(icon
)
219 button
.connect('toggled', ButtonCallBack
)
221 # I don't know why the tooltips don't work here
222 self
.set_tooltips(True)
223 tips
= gtk
.Tooltips()
224 tips
.set_tip(button
, tooltip
)
226 self
.insert(button
, 8)