Skip to content

Commit

Permalink
wxGUI: more Python 3.10 fixes (#2050)
Browse files Browse the repository at this point in the history
* wxGUI/gui_core: SpinCtrl widget require integer min, max param arg
* wxGUI/gui_core: add ConvertParamArgToInt mixin class for conversion args, kwargs float value to the int
* wxGUI/gui_core: Rect widget require integer x, y param arg
* wxGUI/gui_core: PseudoDC widget DrawRectangle method require integer x, y, width, height param arg
* wxGUI/psmap: wx.MemoryDC widget DrawBitmap method require integer x, y param arg
* wxGUI/psmap: wx.Rect widget Inflate method require integer dx, dy param arg
* wxGUI/gui_core: PseudoDC widget DrawBitmap method require integer x, y param arg
* wxGUI/gui_core: PseudoDC widget DrawCircle method require integer x, y, radius param arg
* wxGUI/gui_core: PseudoDC widget DrawLinePoint method require integer x1, y1, x2, y2 param arg
* wxGUI/gui_core: Rect widget CenterIn method require wx.Rect r param arg
* wxGUI/gui_core: PseudoDC widget SetIdBounds method require wx.Rect rect param arg

Co-authored-by: Tomas Zigo <tomas.zigo@slovanet.sk>
  • Loading branch information
petrasovaa and tmszi committed Dec 31, 2021
1 parent e4ec953 commit ecd1358
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 128 deletions.
16 changes: 10 additions & 6 deletions gui/wxpython/animation/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import grass.temporal as tgis
from core import globalvar
from gui_core.widgets import IntegerValidator
from gui_core.wrap import StaticText, TextCtrl
from core.gcmd import RunCommand
from gui_core.wrap import StaticText, TextCtrl, Slider
from core.gcmd import RunCommand, GWarning

from animation.mapwindow import AnimationWindow
from animation.provider import BitmapProvider, BitmapPool, \
Expand Down Expand Up @@ -380,10 +380,14 @@ class AnimationSliderBase(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.label1 = StaticText(self, id=wx.ID_ANY)
self.slider = wx.Slider(self, id=wx.ID_ANY, style=wx.SL_HORIZONTAL)
self.indexField = TextCtrl(self, id=wx.ID_ANY, size=(40, -1),
style=wx.TE_PROCESS_ENTER | wx.TE_RIGHT,
validator=IntegerValidator())
self.slider = Slider(self, id=wx.ID_ANY, style=wx.SL_HORIZONTAL)
self.indexField = TextCtrl(
self,
id=wx.ID_ANY,
size=(40, -1),
style=wx.TE_PROCESS_ENTER | wx.TE_RIGHT,
validator=IntegerValidator(),
)

self.callbackSliderChanging = None
self.callbackSliderChanged = None
Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/animation/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ def createNoDataBitmap(imageWidth, imageHeight, text="No data"):
dc.SetFont(wx.Font(pointSize=40, family=wx.FONTFAMILY_SCRIPT,
style=wx.FONTSTYLE_NORMAL, weight=wx.FONTWEIGHT_BOLD))
tw, th = dc.GetTextExtent(text)
dc.DrawText(text, (imageWidth - tw) / 2, (imageHeight - th) / 2)
dc.DrawText(text, (imageWidth - tw) // 2, (imageHeight - th) // 2)
dc.SelectObject(wx.NullBitmap)
return bitmap

Expand Down
2 changes: 1 addition & 1 deletion gui/wxpython/gmodeler/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ def __init__(self, parent):
self.SetDiagram(self.diagram)
self.diagram.SetCanvas(self)

self.SetScrollbars(20, 20, 2000 / 20, 2000 / 20)
self.SetScrollbars(20, 20, 2000 // 20, 2000 // 20)

self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
Expand Down
22 changes: 18 additions & 4 deletions gui/wxpython/gui_core/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,20 @@
MapValidator
from core.settings import UserSettings
from core.debug import Debug
from gui_core.wrap import Button, CheckListBox, EmptyBitmap, HyperlinkCtrl, \
Menu, NewId, SpinCtrl, StaticBox, StaticText, TextCtrl
from core.utils import is_shell_running
from gui_core.wrap import (
Button,
CheckListBox,
EmptyBitmap,
HyperlinkCtrl,
Menu,
NewId,
Slider,
SpinCtrl,
StaticBox,
StaticText,
TextCtrl,
)


class SimpleDialog(wx.Dialog):
Expand Down Expand Up @@ -1791,8 +1803,10 @@ def __init__(self, parent, id=wx.ID_ANY, title=_("Set Map Layer Opacity"),
sizer = wx.BoxSizer(wx.VERTICAL)

box = wx.GridBagSizer(vgap=5, hgap=5)
self.value = wx.Slider(
panel, id=wx.ID_ANY, value=int(self.opacity * 100),
self.value = Slider(
panel,
id=wx.ID_ANY,
value=int(self.opacity * 100),
style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_TOP | wx.SL_LABELS,
minValue=0, maxValue=100, size=(350, -1))

Expand Down
6 changes: 3 additions & 3 deletions gui/wxpython/gui_core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,9 +1344,9 @@ def __init__(self, parent, giface, task, id=wx.ID_ANY,
which_sizer.Add(win, proportion=0,
flag=style, border=5)

elif p.get('type', '') == 'integer':
minValue = -1e9
maxValue = 1e9
elif p.get("type", "") == "integer":
minValue = int(-1e9)
maxValue = int(1e9)
value = self._getValue(p)

win = SpinCtrl(
Expand Down
42 changes: 27 additions & 15 deletions gui/wxpython/gui_core/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,20 @@
from core import globalvar
from core.gcmd import GMessage, GError
from core.debug import Debug
from gui_core.wrap import Button, SearchCtrl, StaticText, StaticBox, \
TextCtrl, Menu, Rect, EmptyBitmap, ListCtrl, NewId, CheckListCtrlMixin
from gui_core.wrap import (
Button,
SearchCtrl,
Slider,
StaticText,
StaticBox,
TextCtrl,
Menu,
Rect,
EmptyBitmap,
ListCtrl,
NewId,
CheckListCtrlMixin,
)


class NotebookController:
Expand Down Expand Up @@ -416,13 +428,13 @@ def SetRange(self, min, max):
pass


class FloatSlider(wx.Slider):
class FloatSlider(Slider):
"""Class derived from wx.Slider for floats"""

def __init__(self, **kwargs):
Debug.msg(1, "FloatSlider.__init__()")
wx.Slider.__init__(self, **kwargs)
self.coef = 1.
Slider.__init__(self, **kwargs)
self.coef = 1.0
# init range
self.minValueOrig = 0
self.maxValueOrig = 1
Expand Down Expand Up @@ -505,7 +517,7 @@ def __init__(self, parent, usage, label, **kwargs):
def DrawRecord(self, dc, size):
"""Draw record symbol"""
dc.SetBrush(wx.Brush(wx.Colour(255, 0, 0)))
dc.DrawCircle(size[0] / 2, size[1] / 2, size[0] / 2)
dc.DrawCircle(size[0] // 2, size[1] // 2, size[0] // 2)

def DrawStop(self, dc, size):
"""Draw stop symbol"""
Expand All @@ -515,15 +527,14 @@ def DrawStop(self, dc, size):
def DrawPlay(self, dc, size):
"""Draw play symbol"""
dc.SetBrush(wx.Brush(wx.Colour(0, 255, 0)))
points = (wx.Point(0, 0), wx.Point(0, size[1]), wx.Point(size[0],
size[1] / 2))
points = (wx.Point(0, 0), wx.Point(0, size[1]), wx.Point(size[0], size[1] // 2))
dc.DrawPolygon(points)

def DrawPause(self, dc, size):
"""Draw pause symbol"""
dc.SetBrush(wx.Brush(wx.Colour(50, 50, 50)))
dc.DrawRectangle(0, 0, 2 * size[0] / 5, size[1])
dc.DrawRectangle(3 * size[0] / 5, 0, 2 * size[0] / 5, size[1])
dc.DrawRectangle(0, 0, 2 * size[0] // 5, size[1])
dc.DrawRectangle(3 * size[0] // 5, 0, 2 * size[0] // 5, size[1])


class StaticWrapText(GenStaticText):
Expand Down Expand Up @@ -1548,14 +1559,15 @@ def OnDrawItem(self, dc, rect, item, flags):
# for painting the items in the popup
bitmap = self.GetPictureBitmap(self.GetString(item))
if bitmap:
dc.DrawBitmap(
bitmap, r.x, r.y + (r.height - bitmap.GetHeight()) / 2)
dc.DrawBitmap(bitmap, r.x, r.y + (r.height - bitmap.GetHeight()) // 2)
width = bitmap.GetWidth() + 10
else:
width = 0
dc.DrawText(self.GetString(item),
r.x + width,
(r.y + 0) + (r.height - dc.GetCharHeight()) / 2)
dc.DrawText(
self.GetString(item),
r.x + width,
(r.y + 0) + (r.height - dc.GetCharHeight()) // 2,
)

def OnMeasureItem(self, item):
"""Overridden from OwnerDrawnComboBox, should return the height.
Expand Down
75 changes: 67 additions & 8 deletions gui/wxpython/gui_core/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,28 @@
if wxPythonPhoenix and CheckWxVersion([4, 0, 3, 0]):
from wx import NewIdRef as NewId
else:
from wx import NewId
from wx import NewId # noqa: F401


def convertToInt(argsOrKwargs, roundVal=False):
"""Convert args, kwargs float value to int
:param tuple/list/dict argsOrKwargs: args or kwargs
:param bool roundVal: True if you want round float value
return list or dict
"""
result = {} if isinstance(argsOrKwargs, dict) else []
j = None
for i in argsOrKwargs:
if isinstance(result, dict):
i, j = argsOrKwargs[i], i
if isinstance(i, float):
if roundVal:
i = round(i)
i = int(i)
result.update({j: i}) if j else result.append(i)
return result


def BitmapFromImage(image, depth=-1):
Expand Down Expand Up @@ -122,13 +143,32 @@ def SetToolTip(self, tip):
wx.Panel.SetToolTipString(self, tip)


class Slider(wx.Slider):
"""Wrapper around wx.Slider to have more control
over the widget on different platforms/wxpython versions"""

def __init__(self, *args, **kwargs):
args = convertToInt(argsOrKwargs=args)
kwargs = convertToInt(argsOrKwargs=kwargs)

wx.Slider.__init__(self, *args, **kwargs)

def SetRange(self, minValue, maxValue):
wx.Slider.SetRange(self, int(minValue), int(maxValue))

def SetValue(self, value):
wx.Slider.SetValue(self, int(value))


class SpinCtrl(wx.SpinCtrl):
"""Wrapper around wx.SpinCtrl to have more control
over the widget on different platforms"""

gtk3MinSize = 130

def __init__(self, *args, **kwargs):
args = convertToInt(argsOrKwargs=args)
kwargs = convertToInt(argsOrKwargs=kwargs)
if gtk3:
if 'size' in kwargs:
kwargs['size'] = wx.Size(max(self.gtk3MinSize, kwargs['size'][0]), kwargs['size'][1])
Expand Down Expand Up @@ -481,11 +521,13 @@ class PseudoDC(wx.adv.PseudoDC if wxPythonPhoenix else wx.PseudoDC):
def __init__(self, *args, **kwargs):
super(PseudoDC, self).__init__(*args, **kwargs)

def DrawLinePoint(self, pt1, pt2):
def DrawLinePoint(self, *args, **kwargs):
args = convertToInt(argsOrKwargs=args, roundVal=True)
kwargs = convertToInt(argsOrKwargs=kwargs, roundVal=True)
if wxPythonPhoenix:
super(PseudoDC, self).DrawLine(pt1, pt2)
super(PseudoDC, self).DrawLine(*args, **kwargs)
else:
super(PseudoDC, self).DrawLinePoint(pt1, pt2)
super(PseudoDC, self).DrawLinePoint(*args, **kwargs)

def DrawRectangleRect(self, rect):
if wxPythonPhoenix:
Expand All @@ -501,6 +543,21 @@ def EndDrawing(self):
if not wxPythonPhoenix:
super(PseudoDC, self).EndDrawing()

def DrawRectangle(self, *args, **kwargs):
args = convertToInt(argsOrKwargs=args, roundVal=True)
kwargs = convertToInt(argsOrKwargs=kwargs, roundVal=True)
super(PseudoDC, self).DrawRectangle(*args, **kwargs)

def DrawBitmap(self, *args, **kwargs):
args = convertToInt(argsOrKwargs=args, roundVal=True)
kwargs = convertToInt(argsOrKwargs=kwargs, roundVal=True)
super(PseudoDC, self).DrawBitmap(*args, **kwargs)

def DrawCircle(self, *args, **kwargs):
args = convertToInt(argsOrKwargs=args, roundVal=True)
kwargs = convertToInt(argsOrKwargs=kwargs, roundVal=True)
super(PseudoDC, self).DrawCircle(*args, **kwargs)


class ClientDC(wx.ClientDC):
"""Wrapper around wx.ClientDC to have more control
Expand All @@ -519,13 +576,15 @@ class Rect(wx.Rect):
"""Wrapper around wx.Rect to have more control
over the widget on different platforms/wxpython versions"""
def __init__(self, *args, **kwargs):
args = convertToInt(argsOrKwargs=args)
kwargs = convertToInt(argsOrKwargs=kwargs)
wx.Rect.__init__(self, *args, **kwargs)

def ContainsXY(self, x, y):
if wxPythonPhoenix:
return wx.Rect.Contains(self, x=x, y=y)
return wx.Rect.Contains(self, x=int(x), y=int(y))
else:
return wx.Rect.ContainsXY(self, x, y)
return wx.Rect.ContainsXY(self, int(x), int(y))

def ContainsRect(self, rect):
if wxPythonPhoenix:
Expand All @@ -535,9 +594,9 @@ def ContainsRect(self, rect):

def OffsetXY(self, dx, dy):
if wxPythonPhoenix:
return wx.Rect.Offset(self, dx, dy)
return wx.Rect.Offset(self, int(dx), int(dy))
else:
return wx.Rect.OffsetXY(self, dx, dy)
return wx.Rect.OffsetXY(self, int(dx), int(dy))


class CheckBox(wx.CheckBox):
Expand Down
13 changes: 7 additions & 6 deletions gui/wxpython/mapswipe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from gui_core.mapdisp import DoubleMapFrame
from gui_core.dialogs import GetImageHandlers
from gui_core.wrap import Slider
from mapwin.base import MapWindowProperties
from core.render import Map
from mapdisp import statusbar as sb
Expand Down Expand Up @@ -52,8 +53,8 @@ def __init__(self, parent=None, giface=None,
#
self.splitter = MapSplitter(parent=self, id=wx.ID_ANY)

self.sliderH = wx.Slider(self, id=wx.ID_ANY, style=wx.SL_HORIZONTAL)
self.sliderV = wx.Slider(self, id=wx.ID_ANY, style=wx.SL_VERTICAL)
self.sliderH = Slider(self, id=wx.ID_ANY, style=wx.SL_HORIZONTAL)
self.sliderV = Slider(self, id=wx.ID_ANY, style=wx.SL_VERTICAL)

self.mapWindowProperties = MapWindowProperties()
self.mapWindowProperties.setValuesFromUserSettings()
Expand Down Expand Up @@ -610,11 +611,11 @@ def SetViewMode(self, mode):
self.GetSecondWindow().SetMode(mode)
# hide/show slider
if self.splitter.GetSplitMode() == wx.SPLIT_HORIZONTAL:
self._mgr.GetPane('sliderV').Show(mode == 'swipe')
size = self.splitter.GetSize()[1] / 2
self._mgr.GetPane("sliderV").Show(mode == "swipe")
size = self.splitter.GetSize()[1] // 2
else:
self._mgr.GetPane('sliderH').Show(mode == 'swipe')
size = self.splitter.GetSize()[0] / 2
self._mgr.GetPane("sliderH").Show(mode == "swipe")
size = self.splitter.GetSize()[0] // 2
# set sash in the middle
self.splitter.SetSashPosition(size)
self.slider.SetValue(size)
Expand Down

0 comments on commit ecd1358

Please sign in to comment.