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)
 
  130         bg = wx.Brush(self.GetBackgroundColour())
 
  135         rgn = self.GetUpdateRegion()
 
  138         self.pdc.DrawToDCClipped(dc,r)
 
  141         """!Init image size to match window size 
  144         self.Map.width, self.Map.height = self.GetClientSize()
 
  149         self.
_buffer = wx.EmptyBitmap(self.Map.width, self.Map.height)
 
  155         if self.
img and self.Map.width + self.Map.height > 0: 
 
  156             self.
img = self.img.Scale(self.Map.width, self.Map.height)
 
  164         """!Only re-render a histogram image from GRASS during idle 
  165         time instead of multiple times during resizing. 
  173         """!This will save the contents of the buffer to the specified 
  174         file. See the wx.Windows docs for wx.Bitmap::SaveFile for the 
  177         busy = wx.BusyInfo(message=_(
"Please wait, exporting image..."),
 
  181         self.Map.ChangeMapSize((width, height))
 
  182         ibuffer = wx.EmptyBitmap(
max(1, width), 
max(1, height))
 
  183         self.Map.Render(force=
True, windres = 
True)
 
  185         self.
Draw(self.
pdc, img, drawid = 99)
 
  186         dc = wx.BufferedPaintDC(self, ibuffer)
 
  190         self.pdc.DrawToDC(dc)
 
  191         ibuffer.SaveFile(FileName, FileType)
 
  196         """!Converts files to wx.Image 
  198         if self.Map.mapfile 
and os.path.isfile(self.Map.mapfile) 
and \
 
  199                 os.path.getsize(self.Map.mapfile):
 
  200             img = wx.Image(self.Map.mapfile, wx.BITMAP_TYPE_ANY)
 
  208         """!Update canvas if histogram options changes or window 
  211         Debug.msg (2, 
"BufferedWindow.UpdateHist(%s): render=%s" % (img, self.
render))
 
  218             if "GRASS_FONT" in os.environ:
 
  219                 oldfont = os.environ[
"GRASS_FONT"]
 
  220             if self.parent.font != 
"": os.environ[
"GRASS_FONT"] = self.parent.font
 
  221             if "GRASS_ENCODING" in os.environ:
 
  222                 oldencoding = os.environ[
"GRASS_ENCODING"]
 
  223             if self.parent.encoding != 
None and self.parent.encoding != 
"ISO-8859-1":
 
  224                 os.environ[GRASS_ENCODING] = self.parent.encoding
 
  227             self.Map.GetRegion(update = 
True)
 
  229             self.Map.width, self.Map.height = self.GetClientSize()
 
  234         if not self.
img: 
return 
  250         self.parent.statusbar.SetStatusText(
"Image/Raster map <%s>" % self.parent.mapname)
 
  254             os.environ[
"GRASS_FONT"] = oldfont
 
  255         if oldencoding != 
"":
 
  256             os.environ[
"GRASS_ENCODING"] = oldencoding
 
  259         """!Erase the map display 
  261         self.
Draw(self.
pdc, pdctype = 
'clear')
 
  264     """!Main frame for hisgram display window. Uses d.histogram 
  267     def __init__(self, parent = None, id = wx.ID_ANY,
 
  268                  title = _(
"GRASS GIS Histogramming Tool (d.histogram)"),
 
  269                  size = wx.Size(500, 350),
 
  270                  style = wx.DEFAULT_FRAME_STYLE, **kwargs):
 
  271         wx.Frame.__init__(self, parent, id, title, size = size, style = style, **kwargs)
 
  272         self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR, 
'grass.ico'), wx.BITMAP_TYPE_ICO))
 
  289         if parent.GetName() == 
"MapWindow" and not parent.IsStandalone():
 
  290             tree = parent.GetLayerManager().GetLayerTree()
 
  292             if tree.layer_selected 
and tree.GetPyData(tree.layer_selected)[0][
'type'] == 
'raster':
 
  293                 self.
mapname = tree.GetPyData(tree.layer_selected)[0][
'maplayer'].name
 
  296         self.
statusbar = self.CreateStatusBar(number = 1, style = 0)
 
  298         hist_frame_statusbar_fields = [
"Histogramming %s" % self.
mapname]
 
  299         for i 
in range(len(hist_frame_statusbar_fields)):
 
  300             self.statusbar.SetStatusText(hist_frame_statusbar_fields[i], i)
 
  315         self.
layer = self.Map.AddLayer(type = 
"command", name = 
'histogram', command = [[
'd.histogram']],
 
  316                                        l_active = 
False, l_hidden = 
False, l_opacity = 1, l_render = 
False)
 
  323         """!Initialize histogram display, set dimensions and region 
  325         self.width, self.
height = self.GetClientSize()
 
  326         self.Map.geom = self.width, self.
height 
  329         """!Change histogram settings""" 
  330         cmd = [
'd.histogram']
 
  332             cmd.append(
'map=%s' % self.
mapname)
 
  333         module = GUI(parent = self)
 
  337         """!Callback method for histogram command generated by dialog 
  338         created in menuform.py 
  342                                               layerType = 
'raster')
 
  344                 GError(parent = propwin,
 
  345                        message = _(
"Raster map <%s> not found") % name)
 
  351         self.HistWindow.UpdateHist()
 
  354         """!Set histogram layer 
  358             cmd = [
'd.histogram',(
'map=%s' % self.
mapname)]
 
  359         self.
layer = self.Map.ChangeLayer(layer = self.
layer,
 
  366         """!Set font for histogram. If not set, font will be default 
  369         dlg = DefaultFontDialog(parent = self, id = wx.ID_ANY,
 
  370                                 title = _(
'Select font for histogram text'))        
 
  371         dlg.fontlb.SetStringSelection(self.
font, 
True)
 
  373         if dlg.ShowModal() == wx.ID_CANCEL:
 
  380         if dlg.encoding != 
None:
 
  384         self.HistWindow.UpdateHist()
 
  387         """!Erase the histogram display 
  389         self.HistWindow.Draw(self.HistWindow.pdc, pdctype = 
'clear')
 
  392         """!Re-render histogram 
  394         self.HistWindow.UpdateHist()
 
  397         """!Get buffered window""" 
  406         dlg = ImageSizeDialog(self)
 
  408         if dlg.ShowModal() != wx.ID_OK:
 
  411         width, height = dlg.GetValues()
 
  415         dlg = wx.FileDialog(parent = self,
 
  416                             message = _(
"Choose a file name to save the image " 
  417                                         "(no need to add extension)"),
 
  419                             style=wx.SAVE | wx.FD_OVERWRITE_PROMPT)
 
  421         if dlg.ShowModal() == wx.ID_OK:
 
  427             base, ext = os.path.splitext(path)
 
  428             fileType = ltype[dlg.GetFilterIndex()][
'type']
 
  429             extType  = ltype[dlg.GetFilterIndex()][
'ext']
 
  431                 path = base + 
'.' + extType
 
  433             self.HistWindow.SaveToFile(path, fileType,
 
  436         self.HistWindow.UpdateHist()
 
  440         """!Print options and output menu 
  442         point = wx.GetMousePosition()
 
  443         printmenu = wx.Menu()
 
  445         setup = wx.MenuItem(printmenu, id = wx.ID_ANY, text = _(
'Page setup'))
 
  446         printmenu.AppendItem(setup)
 
  447         self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup)
 
  449         preview = wx.MenuItem(printmenu, id = wx.ID_ANY, text = _(
'Print preview'))
 
  450         printmenu.AppendItem(preview)
 
  451         self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview)
 
  453         doprint = wx.MenuItem(printmenu, id = wx.ID_ANY, text = _(
'Print display'))
 
  454         printmenu.AppendItem(doprint)
 
  455         self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint)
 
  459         self.PopupMenu(printmenu)
 
  467         Also remove associated rendered images 
  470             self.propwin.Close(
True)
 
  477     """!Histogram toolbar (see histogram.py) 
  480         BaseToolbar.__init__(self, parent)
 
  487     def _toolbarData(self):
 
  489         return self._getToolbarData(((
'histogram', BaseIcons[
"histogramD"],
 
  490                                       self.parent.OnOptions),
 
  491                                      (
'render', BaseIcons[
"display"],
 
  492                                       self.parent.OnRender),
 
  493                                      (
'erase', BaseIcons[
"erase"],
 
  494                                       self.parent.OnErase),
 
  495                                      (
'font', BaseIcons[
"font"],
 
  496                                       self.parent.SetHistFont),
 
  498                                      (
'save', BaseIcons[
"saveFile"],
 
  499                                       self.parent.SaveToFile),
 
  500                                      (
'hprint', BaseIcons[
"print"],
 
  501                                       self.parent.PrintMenu),
 
  503                                      (
'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.