2 @package modules.histogram
4 Plotting histogram based on d.histogram
7 - histogram::BufferedWindow
8 - histogram::HistogramFrame
9 - histogram::HistogramToolbar
11 (C) 2007, 2010-2011 by the GRASS Development Team
13 This program is free software under the GNU General Public License
14 (>=v2). Read the file COPYING that comes with GRASS for details.
16 @author Michael Barton
17 @author Various updates by Martin Landa <landa.martin gmail.com>
24 from core
import globalvar
26 from gui_core.forms
import GUI
36 """!A Buffered window class.
38 When the drawing needs to change, you app needs to call the
39 UpdateHist() method. Since the drawing is stored in a bitmap, you
40 can also save the drawing to file by calling the
41 SaveToFile(self,file_name,file_type) method.
43 def __init__(self, parent, id = wx.ID_ANY,
44 style = wx.NO_FULL_REPAINT_ON_RESIZE,
45 Map =
None, **kwargs):
47 wx.Window.__init__(self, parent, id = id, style = style, **kwargs)
64 self.Bind(wx.EVT_PAINT, self.
OnPaint)
65 self.Bind(wx.EVT_SIZE, self.
OnSize)
66 self.Bind(wx.EVT_IDLE, self.
OnIdle)
76 self.
pdc = wx.PseudoDC()
80 self.Map.region = self.Map.GetRegion()
83 self.Bind(wx.EVT_ERASE_BACKGROUND,
lambda x:
None)
85 def Draw(self, pdc, img = None, drawid = None, pdctype = 'image', coords = [0,0,0,0]):
86 """!Draws histogram or clears window
89 if pdctype ==
'image' :
90 drawid = imagedict[img]
91 elif pdctype ==
'clear':
100 Debug.msg (3,
"BufferedWindow.Draw(): id=%s, pdctype=%s, coord=%s" % (drawid, pdctype, coords))
102 if pdctype ==
'clear':
104 pdc.SetBackground(bg)
110 if pdctype ==
'image':
111 bg = wx.TRANSPARENT_BRUSH
112 pdc.SetBackground(bg)
113 bitmap = wx.BitmapFromImage(img)
114 w,h = bitmap.GetSize()
115 pdc.DrawBitmap(bitmap, coords[0], coords[1],
True)
116 pdc.SetIdBounds(drawid, (coords[0],coords[1],w,h))
122 """!Draw psuedo DC to buffer
124 dc = wx.BufferedPaintDC(self, self.
_buffer)
129 bg = wx.Brush(self.GetBackgroundColour())
134 rgn = self.GetUpdateRegion()
137 self.pdc.DrawToDCClipped(dc,r)
140 """!Init image size to match window size
143 self.Map.width, self.Map.height = self.GetClientSize()
148 self.
_buffer = wx.EmptyBitmap(self.Map.width, self.Map.height)
154 if self.
img and self.Map.width + self.Map.height > 0:
155 self.
img = self.img.Scale(self.Map.width, self.Map.height)
163 """!Only re-render a histogram image from GRASS during idle
164 time instead of multiple times during resizing.
172 """!This will save the contents of the buffer to the specified
173 file. See the wx.Windows docs for wx.Bitmap::SaveFile for the
176 busy = wx.BusyInfo(message=_(
"Please wait, exporting image..."),
180 self.Map.ChangeMapSize((width, height))
181 ibuffer = wx.EmptyBitmap(
max(1, width),
max(1, height))
182 self.Map.Render(force=
True, windres =
True)
184 self.
Draw(self.
pdc, img, drawid = 99)
185 dc = wx.BufferedPaintDC(self, ibuffer)
188 self.pdc.DrawToDC(dc)
189 ibuffer.SaveFile(FileName, FileType)
194 """!Converts files to wx.Image
196 if self.Map.mapfile
and os.path.isfile(self.Map.mapfile)
and \
197 os.path.getsize(self.Map.mapfile):
198 img = wx.Image(self.Map.mapfile, wx.BITMAP_TYPE_ANY)
206 """!Update canvas if histogram options changes or window
209 Debug.msg (2,
"BufferedWindow.UpdateHist(%s): render=%s" % (img, self.
render))
216 if "GRASS_FONT" in os.environ:
217 oldfont = os.environ[
"GRASS_FONT"]
218 if self.parent.font !=
"": os.environ[
"GRASS_FONT"] = self.parent.font
219 if "GRASS_ENCODING" in os.environ:
220 oldencoding = os.environ[
"GRASS_ENCODING"]
221 if self.parent.encoding !=
None and self.parent.encoding !=
"ISO-8859-1":
222 os.environ[GRASS_ENCODING] = self.parent.encoding
225 self.Map.GetRegion(update =
True)
227 self.Map.width, self.Map.height = self.GetClientSize()
232 if not self.
img:
return
248 self.parent.statusbar.SetStatusText(
"Image/Raster map <%s>" % self.parent.mapname)
252 os.environ[
"GRASS_FONT"] = oldfont
253 if oldencoding !=
"":
254 os.environ[
"GRASS_ENCODING"] = oldencoding
257 """!Erase the map display
259 self.
Draw(self.
pdc, pdctype =
'clear')
262 """!Main frame for hisgram display window. Uses d.histogram
265 def __init__(self, parent = None, id = wx.ID_ANY,
266 title = _(
"GRASS GIS Histogramming Tool (d.histogram)"),
267 size = wx.Size(500, 350),
268 style = wx.DEFAULT_FRAME_STYLE, **kwargs):
269 wx.Frame.__init__(self, parent, id, title, size = size, style = style, **kwargs)
270 self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR,
'grass.ico'), wx.BITMAP_TYPE_ICO))
287 if parent.GetName() ==
"MapWindow" and not parent.IsStandalone():
288 tree = parent.GetLayerManager().GetLayerTree()
290 if tree.layer_selected
and tree.GetPyData(tree.layer_selected)[0][
'type'] ==
'raster':
291 self.
mapname = tree.GetPyData(tree.layer_selected)[0][
'maplayer'].name
294 self.
statusbar = self.CreateStatusBar(number = 1, style = 0)
296 hist_frame_statusbar_fields = [
"Histogramming %s" % self.
mapname]
297 for i
in range(len(hist_frame_statusbar_fields)):
298 self.statusbar.SetStatusText(hist_frame_statusbar_fields[i], i)
313 self.
layer = self.Map.AddLayer(type =
"command", name =
'histogram', command = [[
'd.histogram']],
314 l_active =
False, l_hidden =
False, l_opacity = 1, l_render =
False)
321 """!Initialize histogram display, set dimensions and region
323 self.width, self.
height = self.GetClientSize()
324 self.Map.geom = self.width, self.
height
327 """!Change histogram settings"""
328 cmd = [
'd.histogram']
330 cmd.append(
'map=%s' % self.
mapname)
331 module = GUI(parent = self)
335 """!Callback method for histogram command generated by dialog
336 created in menuform.py
340 layerType =
'raster')
342 GError(parent = propwin,
343 message = _(
"Raster map <%s> not found") % name)
349 self.HistWindow.UpdateHist()
352 """!Set histogram layer
356 cmd = [
'd.histogram',(
'map=%s' % self.
mapname)]
357 self.
layer = self.Map.ChangeLayer(layer = self.
layer,
364 """!Set font for histogram. If not set, font will be default
367 dlg = DefaultFontDialog(parent = self, id = wx.ID_ANY,
368 title = _(
'Select font for histogram text'))
369 dlg.fontlb.SetStringSelection(self.
font,
True)
371 if dlg.ShowModal() == wx.ID_CANCEL:
378 if dlg.encoding !=
None:
382 self.HistWindow.UpdateHist()
385 """!Erase the histogram display
387 self.HistWindow.Draw(self.HistWindow.pdc, pdctype =
'clear')
390 """!Re-render histogram
392 self.HistWindow.UpdateHist()
395 """!Get buffered window"""
404 dlg = ImageSizeDialog(self)
406 if dlg.ShowModal() != wx.ID_OK:
409 width, height = dlg.GetValues()
413 dlg = wx.FileDialog(parent = self,
414 message = _(
"Choose a file name to save the image "
415 "(no need to add extension)"),
417 style=wx.SAVE | wx.FD_OVERWRITE_PROMPT)
419 if dlg.ShowModal() == wx.ID_OK:
425 base, ext = os.path.splitext(path)
426 fileType = ltype[dlg.GetFilterIndex()][
'type']
427 extType = ltype[dlg.GetFilterIndex()][
'ext']
429 path = base +
'.' + extType
431 self.HistWindow.SaveToFile(path, fileType,
434 self.HistWindow.UpdateHist()
438 """!Print options and output menu
440 point = wx.GetMousePosition()
441 printmenu = wx.Menu()
443 setup = wx.MenuItem(printmenu, id = wx.ID_ANY, text = _(
'Page setup'))
444 printmenu.AppendItem(setup)
445 self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup)
447 preview = wx.MenuItem(printmenu, id = wx.ID_ANY, text = _(
'Print preview'))
448 printmenu.AppendItem(preview)
449 self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview)
451 doprint = wx.MenuItem(printmenu, id = wx.ID_ANY, text = _(
'Print display'))
452 printmenu.AppendItem(doprint)
453 self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint)
457 self.PopupMenu(printmenu)
465 Also remove associated rendered images
468 self.propwin.Close(
True)
475 """!Histogram toolbar (see histogram.py)
478 BaseToolbar.__init__(self, parent)
485 def _toolbarData(self):
487 return self._getToolbarData(((
'histogram', BaseIcons[
"histogramD"],
488 self.parent.OnOptions),
489 (
'render', BaseIcons[
"display"],
490 self.parent.OnRender),
491 (
'erase', BaseIcons[
"erase"],
492 self.parent.OnErase),
493 (
'font', BaseIcons[
"font"],
494 self.parent.SetHistFont),
496 (
'save', BaseIcons[
"saveFile"],
497 self.parent.SaveToFile),
498 (
'hprint', BaseIcons[
"print"],
499 self.parent.PrintMenu),
501 (
'quit', BaseIcons[
"quit"],
def GetOptData
Callback method for histogram command generated by dialog created in menuform.py. ...
def OnPaint
Draw psuedo DC to buffer.
def OnSize
Init image size to match window size.
def SetHistLayer
Set histogram layer.
def EraseMap
Erase the map display.
def GetWindow
Get buffered window.
def PrintMenu
Print options and output menu.
def Draw
Draws histogram or clears window.
Main frame for hisgram display window.
Various dialogs used in wxGUI.
Print context and utility functions for printing contents of map display window.
Rendering map layers and overlays into map composition image.
def OnCloseWindow
Window closed Also remove associated rendered images.
def SetHistFont
Set font for histogram.
def OnRender
Re-render histogram.
def OnIdle
Only re-render a histogram image from GRASS during idle time instead of multiple times during resizin...
def GetLayerNameFromCmd
Get map name from GRASS command.
def GetImageHandlers
Get list of supported image handlers.
Misc utilities for wxGUI.
def InitDisplay
Initialize histogram display, set dimensions and region.
def SaveToFile
This will save the contents of the buffer to the specified file.
def SaveToFile
Save to file.
def OnErase
Erase the histogram display.
def GetImage
Converts files to wx.Image.
def OnOptions
Change histogram settings.
def UpdateHist
Update canvas if histogram options changes or window changes geometry.