Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float canvas updates #2448

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
43 changes: 26 additions & 17 deletions demo/FloatCanvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@
# numpy isn't there
haveNumpy = False
errorText = (
"The FloatCanvas requires the numpy module, version 1.* \n\n"
"You can get info about it at:\n"
"http://numpy.scipy.org/\n\n"
"The FloatCanvas requires the numpy module, version > 1.24 \n\n"
"You can get info about it from conda or pip"
)

#---------------------------------------------------------------------------


# # uncomment and adjust to use a local copy
# import sys
# sys.path.append("./")
# from floatcanvas import NavCanvas, FloatCanvas, Resources
# print("FloatCanvas at:", FloatCanvas.__file__)

from wx.lib.floatcanvas import NavCanvas, FloatCanvas, Resources
# print("FloatCanvas at:", FloatCanvas.__file__)

import wx.lib.colourdb
import time
import random


def BuildDrawFrame(): # this gets called when needed, rather than on import
try:
from floatcanvas import NavCanvas, FloatCanvas, Resources
except ImportError: # if it's not there locally, try the wxPython lib.
from wx.lib.floatcanvas import NavCanvas, FloatCanvas, Resources
import wx.lib.colourdb
import time, random

class DrawFrame(wx.Frame):

Expand All @@ -32,11 +39,10 @@ class DrawFrame(wx.Frame):

"""

def __init__(self, parent, id, title, position, size):
wx.Frame.__init__(self, parent, id, title, position, size)

def __init__(self,parent, id,title,position,size):
wx.Frame.__init__(self,parent, id,title,position, size)

## Set up the MenuBar
# Set up the MenuBar
MenuBar = wx.MenuBar()

file_menu = wx.Menu()
Expand Down Expand Up @@ -100,7 +106,7 @@ def __init__(self,parent, id,title,position,size):
MenuBar.Append(draw_menu, "&Tests")

view_menu = wx.Menu()
item = view_menu.Append(-1, "Zoom to &Fit","Zoom to fit the window")
item = view_menu.Append(-1, "Zoom to &Fit", "Zoom to fit the window")
self.Bind(wx.EVT_MENU, self.ZoomToFit, item)
MenuBar.Append(view_menu, "&View")

Expand All @@ -118,7 +124,7 @@ def __init__(self,parent, id,title,position,size):
# Add the Canvas
NC = NavCanvas.NavCanvas(self,
Debug = 0,
BackgroundColor = "DARK SLATE BLUE")
BackgroundColor = "MEDIUM SLATE BLUE")

self.Canvas = NC.Canvas # reference the contained FloatCanvas

Expand Down Expand Up @@ -1845,9 +1851,12 @@ def __init__(self, *args, **kwargs):
wx.App.__init__(self, *args, **kwargs)

def OnInit(self):
wx.InitAllImageHandlers()
# wx.InitAllImageHandlers()
DrawFrame = BuildDrawFrame()
frame = DrawFrame(None, -1, "FloatCanvas Demo App",wx.DefaultPosition,(700,700))
frame = DrawFrame(None, -1,
"FloatCanvas Demo App",
wx.DefaultPosition,
(700,700))

self.SetTopWindow(frame)
frame.Show()
Expand Down
6 changes: 4 additions & 2 deletions samples/floatcanvas/BB_HitTest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python
"""
Test of an alternative hit test methoid that used the bounding boxes of the objects instead.
Test of an alternative hit test method that used the bounding boxes of the objects instead.

Poorly tested!
Poorly tested -- and broken in recent versions

NOTE: probably the issue is different event IDs or something.

Edited from code contributed by Benjamin Jessup on the mailing list

Expand Down
39 changes: 20 additions & 19 deletions samples/floatcanvas/BNAEditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

"""
import os, sys
import sets

import numpy as N

Expand Down Expand Up @@ -64,20 +63,20 @@ def Load(self, filename):
self.Names = []
self.Types = []

with open(filename,'rU') as file_:
with open(filename,'r') as file_:
for line in file_:
if not line:
break
line = line.strip()
Name, line = line.split('","')
Name = Name[1:]
Type,line = line.split('",')
Type, line = line.split('",')
num_points = int(line)
self.Types.append(Type)
self.Names.append(Name)
polygon = N.zeros((num_points,2),N.float)
polygon = N.zeros((num_points,2),N.float64)
for i in range(num_points):
polygon[i,:] = map(float, file_.readline().split(','))
polygon[i,:] = [float(p) for p in file_.readline().split(',')]
self.PointsData.append(polygon)

return None
Expand Down Expand Up @@ -127,11 +126,11 @@ def __init__(self,parent, id,title,position,size):
BackgroundColor = "DARK SLATE BLUE"
).Canvas

wx.EVT_CLOSE(self, self.OnCloseWindow)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

FloatCanvas.EVT_MOTION(self.Canvas, self.OnMove )
FloatCanvas.EVT_LEFT_UP(self.Canvas, self.OnLeftUp )
FloatCanvas.EVT_LEFT_DOWN(self.Canvas, self.OnLeftDown)
self.Canvas.Bind(FloatCanvas.EVT_MOTION, self.OnMove)
self.Canvas.Bind(FloatCanvas.EVT_LEFT_UP, self.OnLeftUp)
self.Canvas.Bind(FloatCanvas.EVT_LEFT_DOWN, self.OnLeftDown)

try:
self.FileDialog = wx.FileDialog(self, "Pick a BNA file",".","","*", wx.FD_OPEN)
Expand Down Expand Up @@ -208,7 +207,7 @@ def OnMove(self, event):
dc.SetPen(wx.Pen('WHITE', 2, wx.SHORT_DASH))
dc.SetLogicalFunction(wx.XOR)
if self.SelectedPointNeighbors is None:
self.SelectedPointNeighbors = N.zeros((3,2), N.float)
self.SelectedPointNeighbors = N.zeros((3,2), N.float64)
#fixme: This feels very inelegant!
if Index == 0:
self.SelectedPointNeighbors[0] = self.SelectedPoly.Points[-1]
Expand Down Expand Up @@ -278,6 +277,16 @@ def LoadBNA(self, filename):
try:
AllPolys = []
self.BNAFile = BNAData(filename)
except Exception as err:
raise
dlg = wx.MessageDialog(None,
'There was something wrong with the selected bna file',
'File Loading Error:\n'
f'{err}',
wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()
else:
print("loaded BNAFile:", self.BNAFile.Filename)
for i, shoreline in enumerate(self.BNAFile.PointsData):
Poly = self.Canvas.AddPolygon(shoreline,
Expand All @@ -289,16 +298,8 @@ def LoadBNA(self, filename):
Poly.BNAIndex = i
AllPolys.append(Poly)
self.Canvas.ZoomToBB()
self.ChangedPolys = sets.Set()
self.ChangedPolys = set()
self.AllPolys = AllPolys
except:
#raise
dlg = wx.MessageDialog(None,
'There was something wrong with the selected bna file',
'File Loading Error',
wx.OK | wx.ICON_ERROR)
dlg.ShowModal()
dlg.Destroy()


class BNAEditor(wx.App):
Expand Down
4 changes: 2 additions & 2 deletions samples/floatcanvas/BarPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def YScaleFun(center):
"""

# center gets ignored in this case
return N.array((1, float(NumChannels)/MaxValue), N.float)
return N.array((1, float(NumChannels) / MaxValue), N.float64)

def ScaleWorldToPixel(self, Lengths):
"""
Expand All @@ -41,7 +41,7 @@ def ScaleWorldToPixel(self, Lengths):
Lengths should be a NX2 array of (x,y) coordinates, or
a 2-tuple, or sequence of 2-tuples.
"""
return N.ceil(( (N.asarray(Lengths, N.float)*self.TransformVector) )).astype('i')
return N.ceil(( (N.asarray(Lengths, N.float64)*self.TransformVector) )).astype('i')


class DrawFrame(wx.Frame):
Expand Down
38 changes: 13 additions & 25 deletions samples/floatcanvas/DrawRect.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,30 @@
#!/usr/bin/env python

"""
A simple demo that shows how to use FloatCanvas to draw rectangles on the screen

Note: this is now broken -- the events are not getting to the Rubber Band Box object.
It should be re-factored to use GUIMode
A simple demo that shows how to use FloatCanvas to draw rectangles
on a Canvas.
"""


import wx

## import a local version
#import sys
#sys.path.append("..")
#from floatcanvas import NavCanvas, FloatCanvas, Resources, Utilities, GUIMode
#from floatcanvas.Utilities import GUI

## import the installed version
from wx.lib.floatcanvas import NavCanvas, FloatCanvas, GUIMode
from wx.lib.floatcanvas.Utilities import GUI

import numpy as N

class DrawFrame(wx.Frame):

"""
A frame used for the FloatCanvas Demo

A frame used for the Demo
"""

def __init__(self,parent, id,title,position,size):
wx.Frame.__init__(self,parent, id,title,position, size)
def __init__(self, parent, id, title, position, size):
wx.Frame.__init__(self, parent, id, title, position, size)

self.CreateStatusBar()
# Add the Canvas
NC = NavCanvas.NavCanvas(self,
size= (500,500),
ProjectionFun = None,
Debug = 0,
BackgroundColor = "DARK SLATE BLUE",
size=(500, 500),
ProjectionFun=None,
Debug=0,
BackgroundColor="DARK SLATE BLUE",
)

self.Canvas = NC.Canvas
Expand Down Expand Up @@ -96,11 +82,13 @@ def OnMove(self, event):
Updates the status bar with the world coordinates

"""
self.SetStatusText("%.4f, %.4f"%tuple(event.Coords))
self.SetStatusText(f"{event.Coords[0]:.2f}, {event.Coords[1]:.2f}")
event.Skip()


app = wx.App()
DrawFrame(None, -1, "FloatCanvas Rectangle Drawer", wx.DefaultPosition, (700,700) )
DrawFrame(None, wx.ID_ANY, "FloatCanvas Rectangle Drawer",
wx.DefaultPosition, (700,700) )
app.MainLoop()


Expand Down
Loading