10 COPYRIGHT: (C) 2007, 2010-2011 by the GRASS Development Team
11 This program is free software under the GNU General Public License
12 (>=v2). Read the file COPYING that comes with GRASS for details.
14 @author Michael Barton
15 @author Various updates by Martin Landa <landa.martin gmail.com>
29 from toolbars
import HistogramToolbar
30 from preferences
import DefaultFontDialog
31 from debug
import Debug
32 from icon
import Icons
33 from gcmd
import GError
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 Histogram of raster map"),
267 style = wx.DEFAULT_FRAME_STYLE, **kwargs):
268 wx.Frame.__init__(self, parent, id, title, style = style, **kwargs)
269 self.SetIcon(wx.Icon(os.path.join(globalvar.ETCICONDIR,
'grass.ico'), wx.BITMAP_TYPE_ICO))
281 self.
toolbar = HistogramToolbar(parent = self)
286 self.
statusbar = self.CreateStatusBar(number = 1, style = 0)
288 hist_frame_statusbar_fields = [
"Histogramming %s" % self.
mapname]
289 for i
in range(len(hist_frame_statusbar_fields)):
290 self.statusbar.SetStatusText(hist_frame_statusbar_fields[i], i)
305 self.
layer = self.Map.AddLayer(type =
"command", name =
'histogram', command = [
'd.histogram'],
306 l_active =
False, l_hidden =
False, l_opacity = 1, l_render =
False)
309 """!Initialize histogram display, set dimensions and region
311 self.width, self.
height = self.GetClientSize()
312 self.Map.geom = self.width, self.
height
315 """!Change histogram settings"""
316 cmd = [
'd.histogram']
318 cmd.append(
'map=%s' % self.
mapname)
324 """!Callback method for histogram command generated by dialog
325 created in menuform.py
328 name, found = utils.GetLayerNameFromCmd(dcmd, fullyQualified =
True,
329 layerType =
'raster')
331 GError(parent = propwin,
332 message = _(
"Raster map <%s> not found") % name)
339 self.HistWindow.UpdateHist()
342 """!Set histogram layer
346 self.
layer = self.Map.ChangeLayer(layer = self.
layer,
347 command = [[
'd.histogram',
'map=%s' % self.
mapname],],
353 """!Set font for histogram. If not set, font will be default
356 dlg = DefaultFontDialog(parent = self, id = wx.ID_ANY,
357 title = _(
'Select font for histogram text'))
358 dlg.fontlb.SetStringSelection(self.
font,
True)
360 if dlg.ShowModal() == wx.ID_CANCEL:
367 if dlg.encoding !=
None:
371 self.HistWindow.UpdateHist()
374 """!Erase the histogram display
376 self.HistWindow.Draw(self.HistWindow.pdc, pdctype =
'clear')
379 """!Re-render histogram
381 self.HistWindow.UpdateHist()
384 """!Get buffered window"""
390 filetype, ltype = gdialogs.GetImageHandlers(self.HistWindow.img)
395 if dlg.ShowModal() != wx.ID_OK:
398 width, height = dlg.GetValues()
402 dlg = wx.FileDialog(parent = self,
403 message = _(
"Choose a file name to save the image "
404 "(no need to add extension)"),
406 style=wx.SAVE | wx.FD_OVERWRITE_PROMPT)
408 if dlg.ShowModal() == wx.ID_OK:
414 base, ext = os.path.splitext(path)
415 fileType = ltype[dlg.GetFilterIndex()][
'type']
416 extType = ltype[dlg.GetFilterIndex()][
'ext']
418 path = base +
'.' + extType
420 self.HistWindow.SaveToFile(path, fileType,
423 self.HistWindow.UpdateHist()
427 """!Print options and output menu
429 point = wx.GetMousePosition()
430 printmenu = wx.Menu()
432 setup = wx.MenuItem(printmenu, id = wx.ID_ANY, text = _(
'Page setup'))
433 printmenu.AppendItem(setup)
434 self.Bind(wx.EVT_MENU, self.printopt.OnPageSetup, setup)
436 preview = wx.MenuItem(printmenu, id = wx.ID_ANY, text = _(
'Print preview'))
437 printmenu.AppendItem(preview)
438 self.Bind(wx.EVT_MENU, self.printopt.OnPrintPreview, preview)
440 doprint = wx.MenuItem(printmenu, id = wx.ID_ANY, text = _(
'Print display'))
441 printmenu.AppendItem(doprint)
442 self.Bind(wx.EVT_MENU, self.printopt.OnDoPrint, doprint)
446 self.PopupMenu(printmenu)
454 Also remove associated rendered images
457 self.propwin.Close(
True)