[GRASS-SVN] r68933 - grass/branches/releasebranch_7_2/gui/wxpython/gui_core
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Jul 10 07:17:59 PDT 2016
Author: annakrat
Date: 2016-07-10 07:17:59 -0700 (Sun, 10 Jul 2016)
New Revision: 68933
Modified:
grass/branches/releasebranch_7_2/gui/wxpython/gui_core/dialogs.py
grass/branches/releasebranch_7_2/gui/wxpython/gui_core/forms.py
grass/branches/releasebranch_7_2/gui/wxpython/gui_core/preferences.py
Log:
wxGUI: improve font dialog and add button for interactive font selection in dialogs (merge from trunk, r68713, r68757)
Modified: grass/branches/releasebranch_7_2/gui/wxpython/gui_core/dialogs.py
===================================================================
--- grass/branches/releasebranch_7_2/gui/wxpython/gui_core/dialogs.py 2016-07-10 14:17:07 UTC (rev 68932)
+++ grass/branches/releasebranch_7_2/gui/wxpython/gui_core/dialogs.py 2016-07-10 14:17:59 UTC (rev 68933)
@@ -17,6 +17,7 @@
- :class:`SqlQueryFrame`
- :class:`SymbolDialog`
- :class:`QuitDialog`
+ - :class:`DefaultFontDialog`
(C) 2008-2016 by the GRASS Development Team
@@ -34,7 +35,7 @@
import wx
from grass.script import core as grass
-from grass.script.utils import natural_sort
+from grass.script.utils import natural_sort, try_remove
from grass.pydispatch.signal import Signal
@@ -2382,3 +2383,206 @@
def OnQuit(self, event):
self.EndModal(wx.ID_YES)
+
+
+class DefaultFontDialog(wx.Dialog):
+ """
+ Opens a file selection dialog to select default font
+ to use in all GRASS displays
+ """
+
+ def __init__(self, parent, title, id=wx.ID_ANY,
+ style=wx.DEFAULT_DIALOG_STYLE |
+ wx.RESIZE_BORDER,
+ settings=UserSettings,
+ type='font'):
+
+ self.settings = settings
+ self.type = type
+
+ wx.Dialog.__init__(self, parent, id, title, style=style)
+
+ panel = wx.Panel(parent=self, id=wx.ID_ANY)
+ self.tmp_file = grass.tempfile(False) + '.png'
+
+ self.fontdict, fontdict_reverse, self.fontlist = self.GetFonts()
+
+ border = wx.BoxSizer(wx.VERTICAL)
+ box = wx.StaticBox(
+ parent=panel,
+ id=wx.ID_ANY,
+ label=" %s " %
+ _("Font settings"))
+ sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
+
+ gridSizer = wx.GridBagSizer(hgap=5, vgap=5)
+
+ label = wx.StaticText(parent=panel, id=wx.ID_ANY,
+ label=_("Select font:"))
+ gridSizer.Add(item=label,
+ flag=wx.ALIGN_TOP,
+ pos=(0, 0))
+
+ self.fontlb = wx.ListBox(
+ parent=panel,
+ id=wx.ID_ANY,
+ pos=wx.DefaultPosition,
+ choices=self.fontlist,
+ style=wx.LB_SINGLE)
+ self.Bind(wx.EVT_LISTBOX, self.EvtListBox, self.fontlb)
+ self.Bind(wx.EVT_LISTBOX_DCLICK, self.EvtListBoxDClick, self.fontlb)
+
+ gridSizer.Add(item=self.fontlb,
+ flag=wx.EXPAND, pos=(1, 0))
+
+ self.renderfont = wx.StaticBitmap(panel, bitmap=wx.EmptyBitmapRGBA(100, 50, 255, 255, 255))
+ gridSizer.Add(item=self.renderfont,
+ flag=wx.EXPAND, pos=(2, 0))
+
+ if self.type == 'font':
+ if "GRASS_FONT" in os.environ:
+ self.font = os.environ["GRASS_FONT"]
+ else:
+ self.font = self.settings.Get(group='display',
+ key='font', subkey='type')
+ self.encoding = self.settings.Get(group='display',
+ key='font', subkey='encoding')
+
+ label = wx.StaticText(parent=panel, id=wx.ID_ANY,
+ label=_("Character encoding:"))
+ gridSizer.Add(item=label,
+ flag=wx.ALIGN_CENTER_VERTICAL,
+ pos=(3, 0))
+
+ self.textentry = wx.TextCtrl(parent=panel, id=wx.ID_ANY,
+ value=self.encoding)
+ gridSizer.Add(item=self.textentry,
+ flag=wx.EXPAND, pos=(4, 0))
+
+ self.textentry.Bind(wx.EVT_TEXT, self.OnEncoding)
+
+ elif self.type == 'outputfont':
+ self.font = self.settings.Get(group='appearance',
+ key='outputfont', subkey='type')
+ self.fontsize = self.settings.Get(group='appearance',
+ key='outputfont', subkey='size')
+ label = wx.StaticText(parent=panel, id=wx.ID_ANY,
+ label=_("Font size:"))
+ gridSizer.Add(item=label,
+ flag=wx.ALIGN_CENTER_VERTICAL,
+ pos=(2, 0))
+
+ self.spin = wx.SpinCtrl(parent=panel, id=wx.ID_ANY)
+ if self.fontsize:
+ self.spin.SetValue(int(self.fontsize))
+ self.spin.Bind(wx.EVT_SPINCTRL, self.OnSizeSpin)
+ self.spin.Bind(wx.EVT_TEXT, self.OnSizeSpin)
+ gridSizer.Add(item=self.spin,
+ flag=wx.ALIGN_CENTER_VERTICAL,
+ pos=(3, 0))
+
+ else:
+ return
+
+ if self.font:
+ self.fontlb.SetStringSelection(fontdict_reverse[self.font], True)
+
+ gridSizer.AddGrowableCol(0)
+ sizer.Add(item=gridSizer, proportion=1,
+ flag=wx.EXPAND | wx.ALL,
+ border=5)
+
+ border.Add(item=sizer, proportion=1,
+ flag=wx.ALL | wx.EXPAND, border=3)
+
+ btnsizer = wx.StdDialogButtonSizer()
+
+ btn = wx.Button(parent=panel, id=wx.ID_OK)
+ btn.SetDefault()
+ btnsizer.AddButton(btn)
+
+ btn = wx.Button(parent=panel, id=wx.ID_CANCEL)
+ btnsizer.AddButton(btn)
+ btnsizer.Realize()
+
+ border.Add(item=btnsizer, proportion=0,
+ flag=wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL, border=5)
+
+ panel.SetAutoLayout(True)
+ panel.SetSizer(border)
+ border.Fit(self)
+ row, col = gridSizer.GetItemPosition(self.renderfont)
+ self.renderfont.SetSize(gridSizer.GetCellSize(row, col))
+ if self.font:
+ self.RenderText(self.font, _("Example"), size=self.renderfont.GetSize())
+
+ self.Layout()
+
+ def OnEncoding(self, event):
+ self.encoding = event.GetString()
+
+ def EvtListBox(self, event):
+ self.font = self.fontdict[event.GetString()]
+ self.RenderText(self.font, "Example", size=self.renderfont.GetSize())
+ event.Skip()
+
+ def EvtListBoxDClick(self, event):
+ self.font = self.fontdict[event.GetString()]
+ event.Skip()
+
+ def OnSizeSpin(self, event):
+ self.fontsize = self.spin.GetValue()
+ event.Skip()
+
+ def GetFonts(self):
+ """
+ parses fonts directory or fretypecap file to get a list of fonts
+ for the listbox
+ """
+ fontlist = []
+ fontdict = {}
+ fontdict_reverse = {}
+ env = os.environ.copy()
+ driver = UserSettings.Get(group='display', key='driver', subkey='type')
+ if driver == 'png':
+ env['GRASS_RENDER_IMMEDIATE'] = 'png'
+ else:
+ env['GRASS_RENDER_IMMEDIATE'] = 'cairo'
+ ret = RunCommand('d.fontlist', flags='v',
+ read=True,
+ env=env)
+ if not ret:
+ return fontlist
+
+ dfonts = ret.splitlines()
+ for line in dfonts:
+ shortname = line.split('|')[0]
+ longname = line.split('|')[1]
+ # not sure when this happens?
+ if shortname.startswith('#'):
+ continue
+ fontlist.append(longname)
+ fontdict[longname] = shortname
+ fontdict_reverse[shortname] = longname
+ fontlist = natural_sort(list(set(fontlist)))
+
+ return fontdict, fontdict_reverse, fontlist
+
+ def RenderText(self, font, text, size):
+ """Renders an example text with the selected font and resets the bitmap widget"""
+ env = os.environ.copy()
+ driver = UserSettings.Get(group='display', key='driver', subkey='type')
+ if driver == 'png':
+ env['GRASS_RENDER_IMMEDIATE'] = 'png'
+ else:
+ env['GRASS_RENDER_IMMEDIATE'] = 'cairo'
+ env['GRASS_RENDER_WIDTH'] = str(size[0])
+ env['GRASS_RENDER_HEIGHT'] = str(size[1])
+ env['GRASS_RENDER_FILE'] = self.tmp_file
+ ret = RunCommand('d.text', text=text, font=font, align='cc', at='50,50',
+ size=80, color='black', env=env)
+ if ret == 0:
+ self.renderfont.SetBitmap(wx.Bitmap(self.tmp_file))
+ else:
+ self.renderfont.SetBitmap(wx.EmptyBitmapRGBA(size[0], size[1]))
+ try_remove(self.tmp_file)
Modified: grass/branches/releasebranch_7_2/gui/wxpython/gui_core/forms.py
===================================================================
--- grass/branches/releasebranch_7_2/gui/wxpython/gui_core/forms.py 2016-07-10 14:17:07 UTC (rev 68932)
+++ grass/branches/releasebranch_7_2/gui/wxpython/gui_core/forms.py 2016-07-10 14:17:59 UTC (rev 68933)
@@ -1280,7 +1280,7 @@
and p.get('prompt', '') != 'color'):
title_txt.SetLabel(title + ':')
-
+ p['wxId'] = []
if p.get('multiple', False) or \
p.get('type', 'string') == 'string' or \
len(p.get('key_desc', [])) > 1:
@@ -1295,6 +1295,22 @@
win.Bind(wx.EVT_TEXT, self.OnSetValue)
style = wx.EXPAND | wx.BOTTOM | wx.LEFT | wx.RIGHT
+ if p.get('name', '') == 'font':
+ font_btn = wx.Button(parent=which_panel, label=_("Select font"))
+ font_btn.Bind(wx.EVT_BUTTON, self.OnSelectFont)
+ font_sizer = wx.BoxSizer(wx.HORIZONTAL)
+ font_sizer.Add(item=win, proportion=1,
+ flag=style, border=5)
+ font_sizer.Add(item=font_btn, proportion=0,
+ flag=style, border=5)
+
+ which_sizer.Add(item=font_sizer, proportion=0,
+ flag=style, border=5)
+ p['wxId'].append(font_btn.GetId())
+ else:
+ which_sizer.Add(item=win, proportion=0,
+ flag=style, border=5)
+
elif p.get('type', '') == 'integer':
minValue = -1e9
maxValue = 1e9
@@ -1313,22 +1329,23 @@
win.Bind(wx.EVT_SPINCTRL, self.OnSetValue)
style = wx.BOTTOM | wx.LEFT | wx.RIGHT
+ which_sizer.Add(item=win, proportion=0,
+ flag=style, border=5)
else: # float
win = wx.TextCtrl(
parent=which_panel, value=p.get(
'default', ''), validator=FloatValidator())
style = wx.EXPAND | wx.BOTTOM | wx.LEFT | wx.RIGHT
+ which_sizer.Add(item=win, proportion=0,
+ flag=style, border=5)
value = self._getValue(p)
if value:
win.SetValue(str(value)) # parameter previously set
win.Bind(wx.EVT_TEXT, self.OnSetValue)
+ p['wxId'].append(win.GetId())
- which_sizer.Add(item=win, proportion=0,
- flag=style, border=5)
- p['wxId'] = [win.GetId()]
-
#
# element selection tree combobox (maps, icons, regions, etc.)
#
@@ -2633,6 +2650,24 @@
from timeline import frame
frame.run(parent=self, datasets=datasets)
+ def OnSelectFont(self, event):
+ """Select font using font dialog"""
+ myId = event.GetId()
+ for p in self.task.params:
+ if 'wxId' in p and myId in p['wxId']:
+ from gui_core.dialogs import DefaultFontDialog
+ dlg = DefaultFontDialog(parent=self,
+ title=_('Select font'),
+ style=wx.DEFAULT_DIALOG_STYLE,
+ type='font')
+ if dlg.ShowModal() == wx.ID_OK:
+ if dlg.font:
+ p['value'] = dlg.font
+ self.FindWindowById(p['wxId'][1]).SetValue(dlg.font)
+ self.OnUpdateValues(event)
+ dlg.Destroy()
+
+
def OnUpdateSelection(self, event):
"""Update dialog (layers, tables, columns, etc.)
"""
Modified: grass/branches/releasebranch_7_2/gui/wxpython/gui_core/preferences.py
===================================================================
--- grass/branches/releasebranch_7_2/gui/wxpython/gui_core/preferences.py 2016-07-10 14:17:07 UTC (rev 68932)
+++ grass/branches/releasebranch_7_2/gui/wxpython/gui_core/preferences.py 2016-07-10 14:17:59 UTC (rev 68933)
@@ -11,7 +11,6 @@
Classes:
- preferences::PreferencesBaseDialog
- preferences::PreferencesDialog
- - preferences::DefaultFontDialog
- preferences::MapsetAccess
- preferences::CheckListMapset
@@ -48,7 +47,7 @@
from core.gcmd import RunCommand, GError
from core.utils import ListOfMapsets, GetColorTables, ReadEpsgCodes, _
from core.settings import UserSettings
-from gui_core.dialogs import SymbolDialog
+from gui_core.dialogs import SymbolDialog, DefaultFontDialog
from gui_core.widgets import IntegerValidator, ColorTablesComboBox
from core.debug import Debug
@@ -2037,203 +2036,6 @@
self.FindWindowById(scrollId).Enable(enable)
-class DefaultFontDialog(wx.Dialog):
- """
- Opens a file selection dialog to select default font
- to use in all GRASS displays
- """
-
- def __init__(self, parent, title, id=wx.ID_ANY,
- style=wx.DEFAULT_DIALOG_STYLE |
- wx.RESIZE_BORDER,
- settings=UserSettings,
- type='font'):
-
- self.settings = settings
- self.type = type
-
- wx.Dialog.__init__(self, parent, id, title, style=style)
-
- panel = wx.Panel(parent=self, id=wx.ID_ANY)
- self.tmp_file = grass.tempfile(False) + '.png'
-
- self.fontlist = self.GetFonts()
-
- border = wx.BoxSizer(wx.VERTICAL)
- box = wx.StaticBox(
- parent=panel,
- id=wx.ID_ANY,
- label=" %s " %
- _("Font settings"))
- sizer = wx.StaticBoxSizer(box, wx.VERTICAL)
-
- gridSizer = wx.GridBagSizer(hgap=5, vgap=5)
-
- label = wx.StaticText(parent=panel, id=wx.ID_ANY,
- label=_("Select font:"))
- gridSizer.Add(item=label,
- flag=wx.ALIGN_TOP,
- pos=(0, 0))
-
- self.fontlb = wx.ListBox(
- parent=panel,
- id=wx.ID_ANY,
- pos=wx.DefaultPosition,
- choices=self.fontlist,
- style=wx.LB_SINGLE)
- self.Bind(wx.EVT_LISTBOX, self.EvtListBox, self.fontlb)
- self.Bind(wx.EVT_LISTBOX_DCLICK, self.EvtListBoxDClick, self.fontlb)
-
- gridSizer.Add(item=self.fontlb,
- flag=wx.EXPAND, pos=(1, 0))
-
- self.renderfont = wx.StaticBitmap(panel, bitmap=wx.EmptyBitmapRGBA(100, 50, 255, 255, 255))
- gridSizer.Add(item=self.renderfont,
- flag=wx.EXPAND, pos=(2, 0))
-
- if self.type == 'font':
- if "GRASS_FONT" in os.environ:
- self.font = os.environ["GRASS_FONT"]
- else:
- self.font = self.settings.Get(group='display',
- key='font', subkey='type')
- self.encoding = self.settings.Get(group='display',
- key='font', subkey='encoding')
-
- label = wx.StaticText(parent=panel, id=wx.ID_ANY,
- label=_("Character encoding:"))
- gridSizer.Add(item=label,
- flag=wx.ALIGN_CENTER_VERTICAL,
- pos=(3, 0))
-
- self.textentry = wx.TextCtrl(parent=panel, id=wx.ID_ANY,
- value=self.encoding)
- gridSizer.Add(item=self.textentry,
- flag=wx.EXPAND, pos=(4, 0))
-
- self.textentry.Bind(wx.EVT_TEXT, self.OnEncoding)
-
- elif self.type == 'outputfont':
- self.font = self.settings.Get(group='appearance',
- key='outputfont', subkey='type')
- self.fontsize = self.settings.Get(group='appearance',
- key='outputfont', subkey='size')
- label = wx.StaticText(parent=panel, id=wx.ID_ANY,
- label=_("Font size:"))
- gridSizer.Add(item=label,
- flag=wx.ALIGN_CENTER_VERTICAL,
- pos=(2, 0))
-
- self.spin = wx.SpinCtrl(parent=panel, id=wx.ID_ANY)
- if self.fontsize:
- self.spin.SetValue(int(self.fontsize))
- self.spin.Bind(wx.EVT_SPINCTRL, self.OnSizeSpin)
- self.spin.Bind(wx.EVT_TEXT, self.OnSizeSpin)
- gridSizer.Add(item=self.spin,
- flag=wx.ALIGN_CENTER_VERTICAL,
- pos=(3, 0))
-
- else:
- return
-
- if self.font:
- self.fontlb.SetStringSelection(self.font, True)
-
- gridSizer.AddGrowableCol(0)
- sizer.Add(item=gridSizer, proportion=1,
- flag=wx.EXPAND | wx.ALL,
- border=5)
-
- border.Add(item=sizer, proportion=1,
- flag=wx.ALL | wx.EXPAND, border=3)
-
- btnsizer = wx.StdDialogButtonSizer()
-
- btn = wx.Button(parent=panel, id=wx.ID_OK)
- btn.SetDefault()
- btnsizer.AddButton(btn)
-
- btn = wx.Button(parent=panel, id=wx.ID_CANCEL)
- btnsizer.AddButton(btn)
- btnsizer.Realize()
-
- border.Add(item=btnsizer, proportion=0,
- flag=wx.EXPAND | wx.ALIGN_RIGHT | wx.ALL, border=5)
-
- panel.SetAutoLayout(True)
- panel.SetSizer(border)
- border.Fit(self)
- row, col = gridSizer.GetItemPosition(self.renderfont)
- self.renderfont.SetSize(gridSizer.GetCellSize(row, col))
- if self.font:
- self.RenderText(self.font, _("Example"), size=self.renderfont.GetSize())
-
- self.Layout()
-
- def OnEncoding(self, event):
- self.encoding = event.GetString()
-
- def EvtListBox(self, event):
- self.font = event.GetString()
- self.RenderText(self.font, "Example", size=self.renderfont.GetSize())
- event.Skip()
-
- def EvtListBoxDClick(self, event):
- self.font = event.GetString()
- event.Skip()
-
- def OnSizeSpin(self, event):
- self.fontsize = self.spin.GetValue()
- event.Skip()
-
- def GetFonts(self):
- """
- parses fonts directory or fretypecap file to get a list of fonts
- for the listbox
- """
- fontlist = []
- env = os.environ.copy()
- driver = UserSettings.Get(group='display', key='driver', subkey='type')
- if driver == 'png':
- env['GRASS_RENDER_IMMEDIATE'] = 'png'
- else:
- env['GRASS_RENDER_IMMEDIATE'] = 'cairo'
- ret = RunCommand('d.fontlist',
- read=True,
- env=env)
- if not ret:
- return fontlist
-
- dfonts = ret.splitlines()
- dfonts.sort(lambda x, y: cmp(x.lower(), y.lower()))
- for item in range(len(dfonts)):
- # ignore duplicate fonts and those starting with #
- if not dfonts[item].startswith('#') and \
- dfonts[item] != dfonts[item - 1]:
- fontlist.append(dfonts[item])
-
- return fontlist
-
- def RenderText(self, font, text, size):
- """Renders an example text with the selected font and resets the bitmap widget"""
- env = os.environ.copy()
- driver = UserSettings.Get(group='display', key='driver', subkey='type')
- if driver == 'png':
- env['GRASS_RENDER_IMMEDIATE'] = 'png'
- else:
- env['GRASS_RENDER_IMMEDIATE'] = 'cairo'
- env['GRASS_RENDER_WIDTH'] = str(size[0])
- env['GRASS_RENDER_HEIGHT'] = str(size[1])
- env['GRASS_RENDER_FILE'] = self.tmp_file
- ret = RunCommand('d.text', text=text, font=font, align='cc', at='50,50',
- size=80, color='black', env=env)
- if ret == 0:
- self.renderfont.SetBitmap(wx.Bitmap(self.tmp_file))
- else:
- self.renderfont.SetBitmap(wx.EmptyBitmapRGBA(size[0], size[1]))
- grass.try_remove(self.tmp_file)
-
-
class MapsetAccess(wx.Dialog):
"""Controls setting options and displaying/hiding map overlay
decorations
More information about the grass-commit
mailing list