1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
|
#!/usr/bin/env python
try:
import numpy as N
import numpy.random as RandomArray
haveNumpy = True
#print "Using numpy, version:", N.__version__
except ImportError:
# 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"
)
#---------------------------------------------------------------------------
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):
"""
A frame used for the FloatCanvas Demo
"""
def __init__(self,parent, id,title,position,size):
wx.Frame.__init__(self,parent, id,title,position, size)
## Set up the MenuBar
MenuBar = wx.MenuBar()
file_menu = wx.Menu()
item = file_menu.Append(-1, "&Close","Close this frame")
self.Bind(wx.EVT_MENU, self.OnQuit, item)
item = file_menu.Append(-1, "&SavePNG","Save the current image as a PNG")
self.Bind(wx.EVT_MENU, self.OnSavePNG, item)
MenuBar.Append(file_menu, "&File")
draw_menu = wx.Menu()
item = draw_menu.Append(-1, "&Clear","Clear the Canvas")
self.Bind(wx.EVT_MENU, self.Clear, item)
item = draw_menu.Append(-1, "&Draw Test","Run a test of drawing random components")
self.Bind(wx.EVT_MENU, self.DrawTest, item)
item = draw_menu.Append(-1, "&Line Test","Run a test of drawing random lines")
self.Bind(wx.EVT_MENU, self.LineTest, item)
item = draw_menu.Append(-1, "Draw &Map","Run a test of drawing a map")
self.Bind(wx.EVT_MENU, self.DrawMap, item)
item = draw_menu.Append(-1, "&Text Test","Run a test of text drawing")
self.Bind(wx.EVT_MENU, self.TestText, item)
item = draw_menu.Append(-1, "&ScaledText Test","Run a test of text drawing")
self.Bind(wx.EVT_MENU, self.TestScaledText, item)
item = draw_menu.Append(-1, "&ScaledTextBox Test","Run a test of the Scaled Text Box")
self.Bind(wx.EVT_MENU, self.TestScaledTextBox, item)
item = draw_menu.Append(-1, "&Bitmap Test","Run a test of the Bitmap Object")
self.Bind(wx.EVT_MENU, self.TestBitmap, item)
item = draw_menu.Append(-1, "&Hit Test","Run a test of the hit test code")
self.Bind(wx.EVT_MENU, self.TestHitTest, item)
item = draw_menu.Append(-1, "Hit Test &Foreground","Run a test of the hit test code with a foreground Object")
self.Bind(wx.EVT_MENU, self.TestHitTestForeground, item)
item = draw_menu.Append(-1, "&Animation","Run a test of Animation")
self.Bind(wx.EVT_MENU, self.TestAnimation, item)
#item = draw_menu.Append(-1, "&Speed","Run a test of Drawing Speed")
#self.Bind(wx.EVT_MENU, self.SpeedTest, item)
item = draw_menu.Append(-1, "Change &Properties","Run a test of Changing Object Properties")
self.Bind(wx.EVT_MENU, self.PropertiesChangeTest, item)
item = draw_menu.Append(-1, "&Arrows","Run a test of Arrows")
self.Bind(wx.EVT_MENU, self.ArrowTest, item)
item = draw_menu.Append(-1, "&ArrowLine Test","Run a test of drawing Arrow Lines")
self.Bind(wx.EVT_MENU, self.ArrowLineTest, item)
item = draw_menu.Append(-1, "&Hide","Run a test of hiding and showing objects")
self.Bind(wx.EVT_MENU, self.HideTest, item)
MenuBar.Append(draw_menu, "&Tests")
view_menu = wx.Menu()
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")
help_menu = wx.Menu()
item = help_menu.Append(-1, "&About",
"More information About this program")
self.Bind(wx.EVT_MENU, self.OnAbout, item)
MenuBar.Append(help_menu, "&Help")
self.SetMenuBar(MenuBar)
self.CreateStatusBar()
# Add the Canvas
NC = NavCanvas.NavCanvas(self,
Debug = 0,
BackgroundColor = "DARK SLATE BLUE")
self.Canvas = NC.Canvas # reference the contained FloatCanvas
self.MsgWindow = wx.TextCtrl(self, wx.ID_ANY,
"Look Here for output from events\n",
style = (wx.TE_MULTILINE |
wx.TE_READONLY |
wx.SUNKEN_BORDER)
)
##Create a sizer to manage the Canvas and message window
MainSizer = wx.BoxSizer(wx.VERTICAL)
MainSizer.Add(NC, 4, wx.EXPAND)
MainSizer.Add(self.MsgWindow, 1, wx.EXPAND | wx.ALL, 5)
self.SetSizer(MainSizer)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.Canvas.Bind(FloatCanvas.EVT_MOTION, self.OnMove)
self.Canvas.Bind(FloatCanvas.EVT_MOUSEWHEEL, self.OnWheel)
self.EventsAreBound = False
## getting all the colors for random objects
wx.lib.colourdb.updateColourDB()
self.colors = wx.lib.colourdb.getColourList()
return None
def Log(self, text):
self.MsgWindow.AppendText(text)
if not text[-1] == "\n":
self.MsgWindow.AppendText("\n")
def BindAllMouseEvents(self):
if not self.EventsAreBound:
## Here is how you catch FloatCanvas mouse events
self.Canvas.Bind(FloatCanvas.EVT_LEFT_DOWN, self.OnLeftDown)
self.Canvas.Bind(FloatCanvas.EVT_LEFT_UP, self.OnLeftUp)
self.Canvas.Bind(FloatCanvas.EVT_LEFT_DCLICK, self.OnLeftDouble)
self.Canvas.Bind(FloatCanvas.EVT_MIDDLE_DOWN, self.OnMiddleDown)
self.Canvas.Bind(FloatCanvas.EVT_MIDDLE_UP, self.OnMiddleUp)
self.Canvas.Bind(FloatCanvas.EVT_MIDDLE_DCLICK, self.OnMiddleDouble)
self.Canvas.Bind(FloatCanvas.EVT_RIGHT_DOWN, self.OnRightDown)
self.Canvas.Bind(FloatCanvas.EVT_RIGHT_UP, self.OnRightUp)
self.Canvas.Bind(FloatCanvas.EVT_RIGHT_DCLICK, self.OnRightDouble)
self.EventsAreBound = True
def UnBindAllMouseEvents(self):
## Here is how you unbind FloatCanvas mouse events
self.Canvas.Unbind(FloatCanvas.EVT_LEFT_DOWN)
self.Canvas.Unbind(FloatCanvas.EVT_LEFT_UP)
self.Canvas.Unbind(FloatCanvas.EVT_LEFT_DCLICK)
self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_DOWN)
self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_UP)
self.Canvas.Unbind(FloatCanvas.EVT_MIDDLE_DCLICK)
self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_DOWN)
self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_UP)
self.Canvas.Unbind(FloatCanvas.EVT_RIGHT_DCLICK)
self.EventsAreBound = False
def PrintCoords(self,event):
self.Log("coords are: %s"%(event.Coords,))
self.Log("pixel coords are: %s\n"%(event.GetPosition(),))
def OnSavePNG(self, event=None):
import os
dlg = wx.FileDialog(
self, message="Save file as ...", defaultDir=os.getcwd(),
defaultFile="", wildcard="*.png", style=wx.SAVE
)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
if not(path[-4:].lower() == ".png"):
path = path+".png"
self.Canvas.SaveAsImage(path)
def OnLeftDown(self, event):
self.Log("LeftDown")
self.PrintCoords(event)
def OnLeftUp(self, event):
self.Log("LeftUp")
self.PrintCoords(event)
def OnLeftDouble(self, event):
self.Log("LeftDouble")
self.PrintCoords(event)
def OnMiddleDown(self, event):
self.Log("MiddleDown")
self.PrintCoords(event)
def OnMiddleUp(self, event):
self.Log("MiddleUp")
self.PrintCoords(event)
def OnMiddleDouble(self, event):
self.Log("MiddleDouble")
self.PrintCoords(event)
def OnRightDown(self, event):
self.Log("RightDown")
self.PrintCoords(event)
def OnRightUp(self, event):
self.Log("RightUp")
self.PrintCoords(event)
def OnRightDouble(self, event):
self.Log("RightDouble")
self.PrintCoords(event)
def OnWheel(self, event):
self.Log("Mouse Wheel")
self.PrintCoords(event)
Rot = event.GetWheelRotation()
Rot = Rot / abs(Rot) * 0.1
if event.ControlDown(): # move left-right
self.Canvas.MoveImage( (Rot, 0), "Panel" )
else: # move up-down
self.Canvas.MoveImage( (0, Rot), "Panel" )
def OnMove(self, event):
"""
Updates the status bar with the world coordinates
"""
self.SetStatusText("%.2f, %.2f"%tuple(event.Coords))
event.Skip()
def OnAbout(self, event):
dlg = wx.MessageDialog(self,
"This is a small program to demonstrate\n"
"the use of the FloatCanvas\n",
"About Me",
wx.OK | wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def ZoomToFit(self,event):
self.Canvas.ZoomToBB()
def Clear(self,event = None):
self.UnBindAllMouseEvents()
self.Canvas.InitAll()
self.Canvas.Draw()
def OnQuit(self,event):
self.Close(True)
def OnCloseWindow(self, event):
self.Destroy()
def DrawTest(self,event=None):
"""
This demo draws a few of everything
"""
wx.GetApp().Yield(True)
Range = (-10,10)
colors = self.colors
self.BindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
#
## these set the limits for how much you can zoom in and out
Canvas.MinScale = 14
Canvas.MaxScale = 500
############# Random tests of everything ##############
# Rectangles
for i in range(3):
xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
lw = random.randint(1,5)
cf = random.randint(0,len(colors)-1)
wh = (random.randint(1,5), random.randint(1,5))
Canvas.AddRectangle(xy, wh, LineWidth = lw, FillColor = colors[cf])
# Ellipses
for i in range(3):
xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
lw = random.randint(1,5)
cf = random.randint(0,len(colors)-1)
h = random.randint(1,5)
w = random.randint(1,5)
Canvas.AddEllipse(xy, (h,w), LineWidth = lw,FillColor = colors[cf])
# Points
for i in range(5):
xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
D = random.randint(1,50)
cf = random.randint(0,len(colors)-1)
Canvas.AddPoint(xy, Color = colors[cf], Diameter = D)
# SquarePoints
for i in range(500):
xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
S = random.randint(1, 50)
cf = random.randint(0,len(colors)-1)
Canvas.AddSquarePoint(xy, Color = colors[cf], Size = S)
# Circles
for i in range(5):
xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
D = random.randint(1,5)
lw = random.randint(1,5)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
Canvas.AddCircle(xy, D, LineWidth = lw, LineColor = colors[cl], FillColor = colors[cf])
Canvas.AddText("Circle # %i"%(i), xy, Size = 12, BackgroundColor = None, Position = "cc")
# Lines
for i in range(5):
points = []
for j in range(random.randint(2,10)):
point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
points.append(point)
lw = random.randint(1,10)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl])
# Polygons
for i in range(3):
points = []
for j in range(random.randint(2,6)):
point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
points.append(point)
lw = random.randint(1,6)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
Canvas.AddPolygon(points,
LineWidth = lw,
LineColor = colors[cl],
FillColor = colors[cf],
FillStyle = 'Solid')
## Pointset
for i in range(4):
points = []
points = RandomArray.uniform(Range[0],Range[1],(100,2))
cf = random.randint(0,len(colors)-1)
D = random.randint(1,4)
Canvas.AddPointSet(points, Color = colors[cf], Diameter = D)
# Text
String = "Unscaled text"
for i in range(3):
ts = random.randint(10,40)
cf = random.randint(0,len(colors)-1)
xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
Canvas.AddText(String, xy, Size = ts, Color = colors[cf], Position = "cc")
# Scaled Text
String = "Scaled text"
for i in range(3):
ts = random.random()*3 + 0.2
cf = random.randint(0,len(colors)-1)
Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
Canvas.AddScaledText(String, Point, Size = ts, Color = colors[cf], Position = "cc")
# Arrows
N = 5
Points = RandomArray.uniform(Range[0], Range[1], (N,2) )
for i in range(N):
Canvas.AddArrow(Points[i],
random.uniform(20,100),
Direction = random.uniform(0,360),
LineWidth = random.uniform(1,5),
LineColor = colors[random.randint(0,len(colors)-1)],
ArrowHeadAngle = random.uniform(20,90))
# ArrowLines
for i in range(5):
points = []
for j in range(random.randint(2,10)):
point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
points.append(point)
lw = random.randint(1,10)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
Canvas.AddArrowLine(points, LineWidth = lw, LineColor = colors[cl], ArrowHeadSize= 16)
Canvas.ZoomToBB()
def TestAnimation(self,event=None):
"""
In this test, a relatively complex background is drawn, and
a simple object placed in the foreground is moved over
it. This demonstrates how to use the InForeground attribute
to make an object in the foregorund draw fast, without
having to re-draw the whole background.
"""
wx.GetApp().Yield(True)
Range = (-10,10)
self.Range = Range
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
## Random tests of everything:
colors = self.colors
# Rectangles
for i in range(3):
xy = (random.uniform(Range[0],Range[1]), random.uniform(Range[0],Range[1]))
lw = random.randint(1,5)
cf = random.randint(0,len(colors)-1)
wh = (random.randint(1,5), random.randint(1,5) )
Canvas.AddRectangle(xy, wh, LineWidth = lw, FillColor = colors[cf])
# Ellipses
for i in range(3):
xy = (random.uniform(Range[0],Range[1]), random.uniform(Range[0],Range[1]))
lw = random.randint(1,5)
cf = random.randint(0,len(colors)-1)
wh = (random.randint(1,5), random.randint(1,5) )
Canvas.AddEllipse(xy, wh, LineWidth = lw, FillColor = colors[cf])
# Circles
for i in range(5):
xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
D = random.randint(1,5)
lw = random.randint(1,5)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
Canvas.AddCircle(xy, D, LineWidth = lw, LineColor = colors[cl], FillColor = colors[cf])
Canvas.AddText("Circle # %i"%(i), xy, Size = 12, BackgroundColor = None, Position = "cc")
# Lines
for i in range(5):
points = []
for j in range(random.randint(2,10)):
point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
points.append(point)
lw = random.randint(1,10)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl])
# Polygons
for i in range(3):
points = []
for j in range(random.randint(2,6)):
point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
points.append(point)
lw = random.randint(1,6)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
Canvas.AddPolygon(points,
LineWidth = lw,
LineColor = colors[cl],
FillColor = colors[cf],
FillStyle = 'Solid')
# Scaled Text
String = "Scaled text"
for i in range(3):
ts = random.random()*3 + 0.2
cf = random.randint(0,len(colors)-1)
xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
Canvas.AddScaledText(String, xy, Size = ts, Color = colors[cf], Position = "cc")
# Now the Foreground Object:
C = Canvas.AddCircle((0,0), 7, LineWidth = 2,LineColor = "Black",FillColor = "Red", InForeground = True)
T = Canvas.AddScaledText("Click to Move", (0,0), Size = 0.6, Position = 'cc', InForeground = True)
C.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.MoveMe)
C.Text = T
self.Timer = wx.PyTimer(self.ShowFrame)
self.FrameDelay = 50 # milliseconds
Canvas.ZoomToBB()
def ShowFrame(self):
Object = self.MovingObject
Range = self.Range
if self.TimeStep < self.NumTimeSteps:
x,y = Object.XY
if x > Range[1] or x < Range[0]:
self.dx = -self.dx
if y > Range[1] or y < Range[0]:
self.dy = -self.dy
Object.Move( (self.dx,self.dy) )
Object.Text.Move( (self.dx,self.dy))
self.Canvas.Draw()
self.TimeStep += 1
wx.GetApp().Yield(True)
else:
self.Timer.Stop()
def MoveMe(self, Object):
self.MovingObject = Object
Range = self.Range
self.dx = random.uniform(Range[0]/4,Range[1]/4)
self.dy = random.uniform(Range[0]/4,Range[1]/4)
#import time
#start = time.time()
self.NumTimeSteps = 200
self.TimeStep = 1
self.Timer.Start(self.FrameDelay)
#print "Did %i frames in %f seconds"%(N, (time.time() - start) )
def TestHitTest(self, event=None):
wx.GetApp().Yield(True)
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
#Add a Hit-able rectangle
w, h = 60, 20
dx = 80
dy = 40
x, y = 20, 20
FontSize = 8
#Add one that is not HitAble
Canvas.AddRectangle((x,y), (w, h), LineWidth = 2)
Canvas.AddText("Not Hit-able", (x,y), Size = FontSize, Position = "bl")
x += dx
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2)
R.Name = "Line Rectangle"
R.HitFill = False
R.HitLineWidth = 5 # Makes it a little easier to hit
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
Canvas.AddText("Left Click Line", (x,y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "Red"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + "Rectangle"
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
Canvas.AddText("Left Click Fill", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
color = "LightBlue"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
Canvas.AddText("Right Click Fill", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "Grey"
R = Canvas.AddEllipse((x, y), (w, h),LineWidth = 2,FillColor = color)
R.Name = color +" Ellipse"
R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHit)
Canvas.AddText("Right Click Fill", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "Brown"
R = Canvas.AddCircle((x+dx/2, y+dy/2), dx/4, LineWidth = 2, FillColor = color)
R.Name = color + " Circle"
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DCLICK, self.RectGotHit)
Canvas.AddText("Left D-Click Fill", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
color = "Pink"
R = Canvas.AddCircle((x+dx/2, y+dy/2), dx/4, LineWidth = 2,FillColor = color)
R.Name = color + " Circle"
R.Bind(FloatCanvas.EVT_FC_LEFT_UP, self.RectGotHit)
Canvas.AddText("Left Up Fill", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "White"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_MIDDLE_DOWN, self.RectGotHit)
Canvas.AddText("Middle Down", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "AQUAMARINE"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_MIDDLE_UP, self.RectGotHit)
Canvas.AddText("Middle Up", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
color = "CORAL"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_MIDDLE_DCLICK, self.RectGotHit)
Canvas.AddText("Middle DoubleClick", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "CYAN"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_RIGHT_UP, self.RectGotHit)
Canvas.AddText("Right Up", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "LIME GREEN"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_RIGHT_DCLICK, self.RectGotHit)
Canvas.AddText("Right Double Click", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
color = "MEDIUM GOLDENROD"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color
R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
Canvas.AddText("L and R Click", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "SALMON"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color + " Rectangle"
R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
Canvas.AddText("Mouse Enter", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "MEDIUM VIOLET RED"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color
R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
Canvas.AddText("Mouse Leave", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
color = "SKY BLUE"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color)
R.Name = color
R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
Canvas.AddText("Enter and Leave", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "WHEAT"
R = Canvas.AddRectangle((x, y), (w+12, h), LineColor = None, FillColor = color)
R.Name = color
R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
Canvas.AddText("Mouse Enter&Leave", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "KHAKI"
R = Canvas.AddRectangle((x-12, y), (w+12, h), LineColor = None, FillColor = color)
R.Name = color
R.Bind(FloatCanvas.EVT_FC_ENTER_OBJECT, self.RectMouseOver)
R.Bind(FloatCanvas.EVT_FC_LEAVE_OBJECT, self.RectMouseLeave)
Canvas.AddText("Mouse Enter&Leave", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
L = Canvas.AddLine(( (x, y), (x+10, y+10), (x+w, y+h) ), LineWidth = 2, LineColor = "Red")
L.Name = "A Line"
L.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
Canvas.AddText("Left Down", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(L.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "SEA GREEN"
Points = N.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), N.float_)
R = Canvas.AddPolygon(Points, LineWidth = 2, FillColor = color)
R.Name = color + " Polygon"
R.Bind(FloatCanvas.EVT_FC_RIGHT_DOWN, self.RectGotHitRight)
Canvas.AddText("RIGHT_DOWN", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x += dx
color = "Red"
Points = N.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), N.float_)
R = Canvas.AddPointSet(Points, Diameter = 4, Color = color)
R.Name = "PointSet"
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.PointSetGotHit)
Canvas.AddText("LEFT_DOWN", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Size = FontSize, Position = "tl")
x = 20
y += dy
T = Canvas.AddText("Hit-able Text", (x, y), Size = 15, Color = "Red", Position = 'tl')
T.Name = "Hit-able Text"
T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
Canvas.AddText("Left Down", (x, y), Size = FontSize, Position = "bl")
x += dx
T = Canvas.AddScaledText("Scaled Text", (x, y), Size = 1./2*h, Color = "Pink", Position = 'bl')
Canvas.AddPointSet( (x, y), Diameter = 3)
T.Name = "Scaled Text"
T.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHitLeft)
Canvas.AddText("Left Down", (x, y), Size = FontSize, Position = "tl")
x += dx
color = "Cyan"
Point = (x + w/2, y)
#Points = N.array(( (x, y), (x, y+2.*h/3), (x+w, y+h), (x+w, y+h/2.), (x + 2.*w/3, y+h/2.), (x + 2.*w/3,y) ), N.float_)
R = Canvas.AddSquarePoint(Point, Size = 8, Color = color)
R.Name = "SquarePoint"
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
Canvas.AddText("LEFT_DOWN", (x, y), Size = FontSize, Position = "bl")
Canvas.AddText(R.Name, (x, y), Size = FontSize, Position = "tl")
self.Canvas.ZoomToBB()
def TestHitTestForeground(self,event=None):
wx.GetApp().Yield(True)
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
#Add a Hitable rectangle
w, h = 60, 20
dx = 80
dy = 40
x,y = 20, 20
color = "Red"
R = Canvas.AddRectangle((x, y), (w, h), LineWidth = 2, FillColor = color, InForeground = False)
R.Name = color + "Rectangle"
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectGotHit)
Canvas.AddText("Left Click Fill", (x, y), Position = "bl")
Canvas.AddText(R.Name, (x, y+h), Position = "tl")
## A set of Rectangles that move together
## NOTE: In a real app, it might be better to create a new
## custom FloatCanvas DrawObject
self.MovingRects = []
WH = (w/2, h/2)
x += dx
color = "LightBlue"
R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveLeft)
L = Canvas.AddText("Left", (x + w/4, y + h/4), Position = "cc", InForeground = True)
self.MovingRects.extend( (R,L) )
x += w/2
R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveRight)
L = Canvas.AddText("Right", (x + w/4, y + h/4), Position = "cc", InForeground = True)
self.MovingRects.extend( (R,L) )
x -= w/2
y += h/2
R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveUp)
L = Canvas.AddText("Up", (x + w/4, y + h/4), Position = "cc", InForeground = True)
self.MovingRects.extend( (R,L) )
x += w/2
R = Canvas.AddRectangle((x, y), WH, LineWidth = 2, FillColor = color, InForeground = True)
R.HitFill = True
R.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RectMoveDown)
L = Canvas.AddText("Down", (x + w/4, y + h/4), Position = "cc", InForeground = True)
self.MovingRects.extend( (R,L) )
self.Canvas.ZoomToBB()
def RectMoveLeft(self,Object):
self.MoveRects("left")
def RectMoveRight(self,Object):
self.MoveRects("right")
def RectMoveUp(self,Object):
self.MoveRects("up")
def RectMoveDown(self,Object):
self.MoveRects("down")
def MoveRects(self, Dir):
for Object in self.MovingRects:
X,Y = Object.XY
if Dir == "left": X -= 10
elif Dir == "right": X += 10
elif Dir == "up": Y += 10
elif Dir == "down": Y -= 10
Object.SetPoint((X,Y))
self.Canvas.Draw()
def PointSetGotHit(self, Object):
self.Log(Object.Name + "Got Hit\n")
def RectGotHit(self, Object):
self.Log(Object.Name + "Got Hit\n")
def RectGotHitRight(self, Object):
self.Log(Object.Name + "Got Hit With Right\n")
def RectGotHitLeft(self, Object):
self.Log(Object.Name + "Got Hit with Left\n")
def RectMouseOver(self, Object):
self.Log("Mouse entered:" + Object.Name)
def RectMouseLeave(self, Object):
self.Log("Mouse left " + Object.Name)
def TestText(self, event= None):
wx.GetApp().Yield(True)
self.BindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
DefaultSize = 12
Point = (3, 0)
## Add a non-visible rectangle, just to get a Bounding Box
## Text objects have a zero-size bounding box, because it changes with zoom
Canvas.AddRectangle((-10,-10),
(20,20),
LineWidth = 1,
LineColor = None)
# Text
String = "Some text"
self.Canvas.AddText("Top Left",Point,Size = DefaultSize,Color = "Yellow",BackgroundColor = "Blue", Position = "tl")
self.Canvas.AddText("Bottom Left",Point,Size = DefaultSize,Color = "Cyan",BackgroundColor = "Black",Position = "bl")
self.Canvas.AddText("Top Right",Point,Size = DefaultSize,Color = "Black",BackgroundColor = "Cyan",Position = "tr")
self.Canvas.AddText("Bottom Right",Point,Size = DefaultSize,Color = "Blue",BackgroundColor = "Yellow",Position = "br")
Canvas.AddPointSet((Point), Color = "White", Diameter = 2)
Point = (3, 2)
Canvas.AddPointSet((Point), Color = "White", Diameter = 2)
self.Canvas.AddText("Top Center",Point,Size = DefaultSize,Color = "Black",Position = "tc")
self.Canvas.AddText("Bottom Center",Point,Size = DefaultSize,Color = "White",Position = "bc")
Point = (3, 4)
Canvas.AddPointSet((Point), Color = "White", Diameter = 2)
self.Canvas.AddText("Center Right",Point,Size = DefaultSize,Color = "Black",Position = "cr")
self.Canvas.AddText("Center Left",Point,Size = DefaultSize,Color = "Black",Position = "cl")
Point = (3, -2)
Canvas.AddPointSet((Point), Color = "White", Diameter = 2)
self.Canvas.AddText("Center Center",
Point, Size = DefaultSize,
Color = "Black",
Position = "cc")
self.Canvas.AddText("40 Pixels", (-10,8), Size = 40)
self.Canvas.AddText("20 Pixels", (-10,5), Size = 20)
self.Canvas.AddText("10 Pixels", (-10,3), Size = 10)
self.Canvas.AddText("MODERN Font", (-10, 0), Family = wx.MODERN)
self.Canvas.AddText("DECORATIVE Font", (-10, -1), Family = wx.DECORATIVE)
self.Canvas.AddText("ROMAN Font", (-10, -2), Family = wx.ROMAN)
self.Canvas.AddText("SCRIPT Font", (-10, -3), Family = wx.SCRIPT)
self.Canvas.AddText("ROMAN BOLD Font", (-10, -4), Family = wx.ROMAN, Weight=wx.BOLD)
self.Canvas.AddText("ROMAN ITALIC BOLD Font", (-10, -5), Family = wx.ROMAN, Weight=wx.BOLD, Style=wx.ITALIC)
# NOTE: this font exists on my Linux box..who knows were else you'll find it!
Font = wx.Font(20, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "helvetica")
self.Canvas.AddText("Helvetica Italic", (-10, -6), Font = Font)
self.Canvas.ZoomToBB()
def TestScaledText(self, event= None):
wx.GetApp().Yield(True)
self.BindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
Point = (0, 0)
T = Canvas.AddScaledText("Top Left",
Point,
Size = 5,
Color = "Yellow",
BackgroundColor = "Blue",
Position = "tl")
T = Canvas.AddScaledText("Bottom Left",Point,Size = 5,Color = "Cyan",BackgroundColor = "Black",Position = "bl")
T = Canvas.AddScaledText("Top Right",Point,Size = 5,Color = "Black",BackgroundColor = "Cyan",Position = "tr")
T = Canvas.AddScaledText("Bottom Right",Point,Size = 5,Color = "Blue",BackgroundColor = "Yellow",Position = "br")
Canvas.AddPointSet((Point), Color = "Red", Diameter = 4)
Point = (0, 20)
Canvas.AddScaledText("Top Center",Point,Size = 7,Color = "Black",Position = "tc")
Canvas.AddScaledText("Bottom Center",Point,Size = 7,Color = "White",Position = "bc")
Canvas.AddPointSet((Point), Color = "White", Diameter = 4)
Point = (0, -20)
Canvas.AddScaledText("Center Right",Point,Size = 9,Color = "Black",Position = "cr")
Canvas.AddScaledText("Center Left",Point,Size = 9,Color = "Black",Position = "cl")
Canvas.AddPointSet((Point), Color = "White", Diameter = 4)
x = -200
self.Canvas.AddScaledText("MODERN Font", (x, 0), Size = 7, Family = wx.MODERN, Color = (0,0,0))
self.Canvas.AddScaledText("DECORATIVE Font", (x, -10), Size = 7, Family = wx.DECORATIVE, Color = (0,0,1))
self.Canvas.AddScaledText("ROMAN Font", (x, -20), Size = 7, Family = wx.ROMAN)
self.Canvas.AddScaledText("SCRIPT Font", (x, -30), Size = 7, Family = wx.SCRIPT)
self.Canvas.AddScaledText("ROMAN BOLD Font", (x, -40), Size = 7, Family = wx.ROMAN, Weight=wx.BOLD)
self.Canvas.AddScaledText("ROMAN ITALIC BOLD Font", (x, -50), Size = 7, Family = wx.ROMAN, Weight=wx.BOLD, Style=wx.ITALIC)
Canvas.AddPointSet((x,0), Color = "White", Diameter = 4)
# NOTE: this font exists on my OS-X.who knows were else you'll find it!
Point = (-100, 50)
Font = wx.Font(12, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "helvetica")
T = self.Canvas.AddScaledText("Helvetica Italic", Point, Size = 20, Font = Font, Position = 'bc')
Point = (-50, -50)
Font = wx.Font(12, wx.DEFAULT, wx.ITALIC, wx.NORMAL, False, "times")
T = self.Canvas.AddScaledText("Times Font", Point, Size = 8, Font = Font)
self.Canvas.ZoomToBB()
def TestScaledTextBox(self, event= None):
wx.GetApp().Yield(True)
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
Point = (45,40)
Box = Canvas.AddScaledTextBox("A Two Line\nString",
Point,
2,
Color = "Black",
BackgroundColor = None,
LineColor = "Red",
LineStyle = "Solid",
LineWidth = 1,
Width = None,
PadSize = 5,
Family = wx.ROMAN,
Style = wx.NORMAL,
Weight = wx.NORMAL,
Underlined = False,
Position = 'br',
Alignment = "left",
InForeground = False)
# All defaults
Box = Canvas.AddScaledTextBox("A Two Line\nString",
Point,
2)
Box = Canvas.AddScaledTextBox("A Two Line\nString",
Point,
2,
BackgroundColor = "Yellow",
LineColor = "Red",
LineStyle = "Solid",
PadSize = 5,
Family = wx.TELETYPE,
Position = 'bl')
Box = Canvas.AddScaledTextBox("A String\nThis box is clickable",
Point,
2,
BackgroundColor = "Yellow",
LineColor = "Red",
LineStyle = "Solid",
PadSize = 5,
Family = wx.TELETYPE,
Position = 'tr')
Box.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.binding2)
Canvas.AddPoint(Point, Diameter = 4)
Point = (45,15)
Box = Canvas.AddScaledTextBox("A Two Line\nString",
Point,
2,
Color = "Black",
BackgroundColor = 'Red',
LineColor = "Blue",
LineStyle = "LongDash",
LineWidth = 2,
Width = None,
PadSize = 5,
Family = wx.TELETYPE,
Style = wx.NORMAL,
Weight = wx.NORMAL,
Underlined = False,
Position = 'cr',
Alignment = "left",
InForeground = False)
Box = Canvas.AddScaledTextBox("A Two Line\nString",
Point,
1.5,
Color = "Black",
BackgroundColor = 'Red',
LineColor = "Blue",
LineStyle = "LongDash",
LineWidth = 2,
Width = None,
PadSize = 5,
Family = wx.TELETYPE,
Style = wx.NORMAL,
Weight = wx.NORMAL,
Underlined = False,
Position = 'cl',
Alignment = "left",
InForeground = False)
Canvas.AddPoint(Point, Diameter = 4)
Point = (45,-10)
Box = Canvas.AddScaledTextBox("A Two Line\nString",
Point,
2,
Color = "Black",
BackgroundColor = 'Red',
LineColor = "Blue",
LineStyle = "LongDash",
LineWidth = 2,
Width = None,
PadSize = 3,
Family = wx.TELETYPE,
Style = wx.NORMAL,
Weight = wx.NORMAL,
Underlined = False,
Position = 'tc',
Alignment = "left",
InForeground = False)
Box = Canvas.AddScaledTextBox("A three\nLine\nString",
Point,
1.5,
Color = "Black",
BackgroundColor = 'Red',
LineColor = "Blue",
LineStyle = "LongDash",
LineWidth = 2,
Width = None,
PadSize = 0.5,
Family = wx.TELETYPE,
Style = wx.NORMAL,
Weight = wx.NORMAL,
Underlined = False,
Position = 'bc',
Alignment = "left",
InForeground = False)
Canvas.AddPoint(Point, Diameter = 4)
Box = Canvas.AddScaledTextBox("Some Auto Wrapped Text. There is enough to do.",
(80,40),
2,
BackgroundColor = 'White',
LineWidth = 2,
Width = 20,
PadSize = 0.5,
Family = wx.TELETYPE,
)
Box = Canvas.AddScaledTextBox("Some more auto wrapped text. Wrapped to a different width.\n\nThis is another paragraph.",
(80,20),
2,
BackgroundColor = 'White',
LineWidth = 2,
Width = 40,
PadSize = 0.5,
Family = wx.ROMAN,
Alignment = "right"
)
Point = N.array((100, -20), N.float_)
Box = Canvas.AddScaledTextBox("Here is even more auto wrapped text. This time the line spacing is set to 0.8. \n\nThe Padding is set to 0.",
Point,
Size = 3,
BackgroundColor = 'White',
LineWidth = 1,
Width = 40,
PadSize = 0.0,
Family = wx.ROMAN,
Position = "cc",
LineSpacing = 0.8
)
Canvas.AddPoint(Point, "Red", 2)
Point = N.array((0, -40), N.float_)
# Point = N.array((0, 0), N.float_)
for Position in ["tl", "bl", "tr", "br"]:
# for Position in ["br"]:
Box = Canvas.AddScaledTextBox("Here is a\nfour liner\nanother line\nPosition=%s"%Position,
Point,
Size = 4,
Color = "Red",
BackgroundColor = None,#'LightBlue',
LineWidth = 1,
LineColor = "White",
Width = None,
PadSize = 2,
Family = wx.ROMAN,
Position = Position,
LineSpacing = 0.8
)
Canvas.AddPoint(Point, "Red", 4)
Point = N.array((-20, 60), N.float_)
Box = Canvas.AddScaledTextBox("Here is some\ncentered\ntext",
Point,
Size = 4,
Color = "Red",
BackgroundColor = 'LightBlue',
LineWidth = 1,
LineColor = "White",
Width = None,
PadSize = 2,
Family = wx.ROMAN,
Position = "tl",
Alignment = "center",
LineSpacing = 0.8
)
Point = N.array((-20, 20), N.float_)
Box = Canvas.AddScaledTextBox("Here is some\nright aligned\ntext",
Point,
Size = 4,
Color = "Red",
BackgroundColor = 'LightBlue',
LineColor = None,
Width = None,
PadSize = 2,
Family = wx.ROMAN,
Position = "tl",
Alignment = "right",
LineSpacing = 0.8
)
Point = N.array((100, -60), N.float_)
Box = Canvas.AddScaledTextBox("Here is some auto wrapped text. This time it is centered, rather than right aligned.\n\nThe Padding is set to 2.",
Point,
Size = 3,
BackgroundColor = 'White',
LineWidth = 1,
Width = 40,
PadSize = 2.0,
Family = wx.ROMAN,
Position = "cc",
LineSpacing = 0.8,
Alignment = 'center',
)
self.Canvas.ZoomToBB()
def binding2(self, event):
self.Log("I'm the TextBox")
def TestBitmap(self, event= None):
wx.GetApp().Yield(True)
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
Canvas.AddRectangle((10, 20),
(400, 100),
LineWidth = 3,
LineColor = "Blue",
FillColor = "Red")
bmp = Resources.getMagPlusBitmap()
Canvas.AddText("These are Unscaled Bitmaps:", (140, 90))
Point = (150, 50)
BitMap = Canvas.AddBitmap(bmp, Point, Position = "cc" )
Canvas.AddPoint(Point, Diameter=4, Color="Green")
Point = (200, 50)
BitMap = Canvas.AddBitmap(bmp, Point, Position = "br" )
Canvas.AddPoint(Point, Diameter=4, Color="Green")
Point = (200, 50)
BitMap = Canvas.AddBitmap(bmp, Point, Position = "bl" )
Canvas.AddPoint(Point, Diameter=4, Color="Green")
Point = (200, 50)
BitMap = Canvas.AddBitmap(bmp, Point, Position = "tr" )
Canvas.AddPoint(Point, Diameter=4, Color="Green")
Point = (200, 50)
BitMap = Canvas.AddBitmap(bmp, Point, Position = "tl" )
Canvas.AddPoint(Point, Diameter=4, Color="Green")
Point = (250, 50)
BitMap = Canvas.AddBitmap(bmp, Point, Position = "cr" )
Canvas.AddPoint(Point, Diameter=4, Color="Green")
Point = (250, 50)
BitMap = Canvas.AddBitmap(bmp, Point, Position = "cl" )
Canvas.AddPoint(Point, Diameter=4, Color="Green")
Point = (300, 50)
BitMap = Canvas.AddBitmap(bmp, Point, Position = "tc" )
Canvas.AddPoint(Point, Diameter=4, Color="Green")
Point = (300, 50)
BitMap = Canvas.AddBitmap(bmp, Point, Position = "bc" )
Canvas.AddPoint(Point, Diameter=4, Color="Green")
Canvas.AddScaledText("These are Scaled Bitmaps:", (220, -60), Size = 10, Position = "tr")
Point = (250, -100)
BitMap = Canvas.AddScaledBitmap(bmp, Point, Height = 50, Position = "bc" )
BitMap = Canvas.AddScaledBitmap(bmp, Point, Height = 50, Position = "tc" )
Canvas.AddPoint(Point, Diameter=4, Color="Green")
Point = (300, -100)
BitMap = Canvas.AddScaledBitmap(Resources.getMondrianImage(), Point, Height = 50)
self.Canvas.ZoomToBB()
def DrawMap(self,event = None):
wx.GetApp().Yield(True)
import os, time
self.Canvas.InitAll()
self.Canvas.SetProjectionFun("FlatEarth")
self.BindAllMouseEvents()
## Test of Actual Map Data
#start = time.clock()
self.Log("Loading Map from a File")
wx.GetApp().Yield(True) # so log text will get displayed now.
Shorelines = self.Read_MapGen(os.path.join("data",'world.dat'),stats = 0)
#print "It took %f seconds to load %i shorelines"%(time.clock() - start,len(Shorelines) )
#start = time.clock()
for segment in Shorelines:
self.Canvas.AddLine(segment)
#print "It took %f seconds to add %i shorelines"%(time.clock() - start,len(Shorelines) )
#start = time.clock()
self.Canvas.ZoomToBB()
#print "It took %f seconds to draw %i shorelines"%(time.clock() - start,len(Shorelines) )
def LineTest(self,event = None):
wx.GetApp().Yield(True)
import os, time
# import random
colors = self.colors
Range = (-10,10)
## Test of drawing lots of lines
Canvas = self.Canvas
Canvas.InitAll()
#start = time.clock()
linepoints = []
linecolors = []
linewidths = []
for i in range(2000):
points = (random.randint(Range[0],Range[1]),
random.randint(Range[0],Range[1]),
random.randint(Range[0],Range[1]),
random.randint(Range[0],Range[1]))
linepoints.append(points)
linewidths.append(random.randint(1,10) )
linecolors.append(random.randint(0,len(colors)-1) )
for (points,color,width) in zip(linepoints,linecolors,linewidths):
Canvas.AddLine((points[0:2],points[2:4]), LineWidth = width, LineColor = colors[color])
#print "It took %f seconds to add %i lines"%(time.clock() - start,len(linepoints) )
#start = time.clock()
Canvas.ZoomToBB()
#print "It took %f seconds to draw %i lines"%(time.clock() - start,len(linepoints) )
def ArrowLineTest(self,event = None):
wx.GetApp().Yield(True)
Canvas = self.Canvas
Canvas.InitAll()
# import os, time
## import random
Range = (-100,100)
colors = self.colors
# Lines
for i in range(5):
points = []
for j in range(random.randint(2,10)):
point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
points.append(point)
lw = random.randint(1,4)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
al = random.randint(8,20)
aa = random.randint(20,90)
Canvas.AddArrowLine(points,
LineWidth = lw,
LineColor = colors[cl],
ArrowHeadSize = al,
ArrowHeadAngle = aa)
Canvas.ZoomToBB()
def SpeedTest(self,event=None):
wx.GetApp().Yield(True)
BigRange = (-1000,1000)
colors = self.colors
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
# Pointset
coords = []
for i in range(1000):
Point = (random.uniform(BigRange[0],BigRange[1]),random.uniform(BigRange[0],BigRange[1]))
coords.append( (Point) )
print "Drawing the Points"
start = time.clock()
for Point in coords:
Canvas.AddPoint(Point, Diameter = 4)
print "It took %s seconds to add the points"%(time.clock() - start)
Canvas.ZoomToBB()
def PropertiesChangeTest(self,event=None):
wx.GetApp().Yield(True)
Range = (-10,10)
colors = self.colors
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
self.ColorObjectsAll = []
self.ColorObjectsLine = []
self.ColorObjectsColor = []
self.ColorObjectsText = []
##One of each object:
# Rectangle
Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
lw = random.randint(1,5)
cf = random.randint(0,len(colors)-1)
wh = ( random.randint(1,5), random.randint(1,5) )
self.Rectangle = Canvas.AddRectangle(Point, wh, LineWidth = lw, FillColor = colors[cf])
self.ColorObjectsAll.append(self.Rectangle)
# Ellipse
Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
lw = random.randint(1,5)
cf = random.randint(0,len(colors)-1)
wh = ( random.randint(1,5), random.randint(1,5) )
self.Ellipse = Canvas.AddEllipse(Point, wh, LineWidth = lw, FillColor = colors[cf])
self.ColorObjectsAll.append(self.Ellipse)
# Point
xy = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
D = random.randint(1,50)
lw = random.randint(1,5)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
self.ColorObjectsColor.append(Canvas.AddPoint(xy, colors[cf], D))
# Circle
Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
D = random.randint(1,5)
lw = random.randint(1,5)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
self.Circle = Canvas.AddCircle(Point, D, LineWidth = lw, LineColor = colors[cl], FillColor = colors[cf])
self.ColorObjectsAll.append(self.Circle)
# Line
points = []
for j in range(random.randint(2,10)):
point = (random.randint(Range[0],Range[1]),random.randint(Range[0],Range[1]))
points.append(point)
lw = random.randint(1,10)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
self.ColorObjectsLine.append(Canvas.AddLine(points, LineWidth = lw, LineColor = colors[cl]))
# Polygon
## points = []
## for j in range(random.randint(2,6)):
## point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
## points.append(point)
points = RandomArray.uniform(Range[0],Range[1],(6,2))
lw = random.randint(1,6)
cf = random.randint(0,len(colors)-1)
cl = random.randint(0,len(colors)-1)
self.ColorObjectsAll.append(Canvas.AddPolygon(points,
LineWidth = lw,
LineColor = colors[cl],
FillColor = colors[cf],
FillStyle = 'Solid'))
## Pointset
points = RandomArray.uniform(Range[0],Range[1],(100,2))
cf = random.randint(0,len(colors)-1)
D = random.randint(1,4)
self.PointSet = Canvas.AddPointSet(points, Color = colors[cf], Diameter = D)
self.ColorObjectsColor.append(self.PointSet)
## Point
point = RandomArray.uniform(Range[0],Range[1],(2,))
cf = random.randint(0,len(colors)-1)
D = random.randint(1,4)
self.Point = Canvas.AddPoint(point, Color = colors[cf], Diameter = D)
self.ColorObjectsColor.append(self.Point)
# Text
String = "Unscaled text"
ts = random.randint(10,40)
cf = random.randint(0,len(colors)-1)
Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
self.ColorObjectsText.append(Canvas.AddText(String, Point, Size = ts, Color = colors[cf], Position = "cc"))
# Scaled Text
String = "Scaled text"
ts = random.random()*3 + 0.2
cf = random.randint(0,len(colors)-1)
Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
self.ColorObjectsText.append(Canvas.AddScaledText(String, Point, Size = ts, Color = colors[cf], Position = "cc"))
# A "Button"
Button = Canvas.AddRectangle((-10, -12), (20, 3), LineStyle = None, FillColor = "Red")
Canvas.AddScaledText("Click Here To Change Properties",
(0, -10.5),
Size = 0.7,
Color = "Black",
Position = "cc")
Button.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.ChangeProperties)
Canvas.ZoomToBB()
def ChangeProperties(self, Object = None):
colors = self.colors
Range = (-10,10)
for Object in self.ColorObjectsAll:
pass
Object.SetFillColor(colors[random.randint(0,len(colors)-1)])
Object.SetLineColor(colors[random.randint(0,len(colors)-1)])
Object.SetLineWidth(random.randint(1,7))
Object.SetLineStyle(FloatCanvas.DrawObject.LineStyleList.keys()[random.randint(0,5)])
for Object in self.ColorObjectsLine:
Object.SetLineColor(colors[random.randint(0,len(colors)-1)])
Object.SetLineWidth(random.randint(1,7))
Object.SetLineStyle(FloatCanvas.DrawObject.LineStyleList.keys()[random.randint(0,5)])
for Object in self.ColorObjectsColor:
Object.SetColor(colors[random.randint(0,len(colors)-1)])
for Object in self.ColorObjectsText:
Object.SetColor(colors[random.randint(0,len(colors)-1)])
Object.SetBackgroundColor(colors[random.randint(0,len(colors)-1)])
self.Circle.SetDiameter(random.randint(1,10))
self.PointSet.SetDiameter(random.randint(1,8))
self.Point.SetDiameter(random.randint(1,8))
for Object in (self.Rectangle, self.Ellipse):
Point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
wh = ( random.randint(1,5), random.randint(1,5) )
Object.SetShape(Point, wh)
self.Canvas.Draw(Force = True)
def ArrowTest(self,event=None):
wx.GetApp().Yield(True)
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
Canvas.MinScale = 15
Canvas.MaxScale = 30
# put in a rectangle to get a bounding box
Canvas.AddRectangle((0,0), (20,20), LineColor = None)
# Draw some Arrows
Canvas.AddArrow((10,10),Length = 40, Direction = 0)
Canvas.AddArrow((10,10),Length = 50, Direction = 45 ,LineWidth = 2, LineColor = "Black", ArrowHeadAngle = 20)
Canvas.AddArrow((10,10),Length = 60, Direction = 90 ,LineWidth = 3, LineColor = "Red", ArrowHeadAngle = 30)
Canvas.AddArrow((10,10),Length = 70, Direction = 135,LineWidth = 4, LineColor = "Red", ArrowHeadAngle = 40)
Canvas.AddArrow((10,10),Length = 80, Direction = 180,LineWidth = 5, LineColor = "Blue", ArrowHeadAngle = 50)
Canvas.AddArrow((10,10),Length = 90, Direction = 225,LineWidth = 4, LineColor = "Blue", ArrowHeadAngle = 60)
Canvas.AddArrow((10,10),Length = 100,Direction = 270,LineWidth = 3, LineColor = "Green", ArrowHeadAngle = 70)
Canvas.AddArrow((10,10),Length = 110,Direction = 315,LineWidth = 2, LineColor = "Green", ArrowHeadAngle = 90 )
Canvas.AddText("Clickable Arrow", (4,18), Position = "bc")
Arrow = Canvas.AddArrow((4,18), 80, Direction = 90 ,LineWidth = 3, LineColor = "Red", ArrowHeadAngle = 30)
Arrow.HitLineWidth = 6
Arrow.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.ArrowClicked)
Canvas.AddText("Changable Arrow: try clicking it", (16,4), Position = "tc")
self.RotArrow = Canvas.AddArrow((16,4), 80, Direction = 0 ,LineWidth = 3, LineColor = "Green", ArrowHeadAngle = 30)
self.RotArrow.HitLineWidth = 6
self.RotArrow.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.RotateArrow)
Canvas.ZoomToBB()
def ArrowClicked(self,event):
self.Log("The Arrow was Clicked")
def RotateArrow(self,event):
##print "The Changeable Arrow was Clicked"
## You can do them either one at a time, or both at once
## Doing them both at once prevents the arrow points from being calculated twice
#self.RotArrow.SetDirection(self.RotArrow.Direction + random.uniform(-90,90))
#self.RotArrow.SetLength(self.RotArrow.Length + random.randint(-20,20))
self.RotArrow.SetLengthDirection(self.RotArrow.Length + random.randint(-20,20),
self.RotArrow.Direction + random.uniform(-90,90) )
self.Canvas.Draw(Force = True)
def HideTest(self, event=None):
wx.GetApp().Yield(True)
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
Range = (-10,10)
# Create a couple random Polygons
points = []
for j in range(6):
point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
points.append(point)
Poly = Canvas.AddPolygon(points,
LineWidth = 2,
LineColor = "Black",
FillColor = "LightBlue",
FillStyle = 'Solid')
points = []
for j in range(6):
point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
points.append(point)
Poly2 = Canvas.AddPolygon(points,
LineWidth = 2,
LineColor = "Black",
FillColor = "Purple",
FillStyle = 'Solid',
InForeground = True)
HideButton = Canvas.AddScaledTextBox("Click To Hide\nBackground Polygon",
(-10, 0),
.5,
BackgroundColor="Red",
PadSize = 0.5,
Position = 'tr',
Alignment="center",
)
HideButton.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.HidePoly)
HideButton.HidePoly = Poly
HideButton2 = Canvas.AddScaledTextBox("Click To Hide\nForeground Polygon",
(-10, 5),
.5,
BackgroundColor="Red",
PadSize = 0.5,
Position = 'tr',
Alignment="center",
)
# Put a reference to the Polygon in the Button object
HideButton2.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.HidePoly)
HideButton2.HidePoly = Poly2
Canvas.ZoomToBB()
def HidePoly(self, Button):
Poly = Button.HidePoly
if Poly.Visible:
Poly.Visible = False
Button.SetText(Button.String.replace("Hide","Show"))
else:
Poly.Visible = True
Button.SetText(Button.String.replace("Show", "Hide"))
self.Canvas.Draw(True)
def TempTest(self, event= None):
"""
This is the start of a poly editor test, but it's not complete
so you can only run it through a command line flag:
python FloatCanvasDemo.py --temp
"""
wx.GetApp().Yield(True)
self.UnBindAllMouseEvents()
Canvas = self.Canvas
Canvas.InitAll()
Range = (-10,10)
# Create a random Polygon
points = []
for j in range(6):
point = (random.uniform(Range[0],Range[1]),random.uniform(Range[0],Range[1]))
points.append(point)
Poly = Canvas.AddPolygon(points,
LineWidth = 2,
LineColor = "Black",
FillColor = "LightBlue",
FillStyle = 'Solid')
Poly.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.SelectPoly)
self.SelectedPoly = None
self.SelectPoints = []
self.SelectedPoint = None
Canvas.ZoomToBB()
def SelectPoly(self, Object):
Canvas = self.Canvas
if Object is self.SelectedPoly:
pass
else:
#fixme: Do something to unselect the old one
self.SelectedPoly = Object
Canvas.RemoveObjects(self.SelectPoints)
self.SelectPoints = []
# Draw points on the Vertices of the Selected Poly:
for i, point in enumerate(Object.Points):
P = Canvas.AddPointSet(point, Diameter = 6, Color = "Red")
P.VerticeNum = i
P.Bind(FloatCanvas.EVT_FC_LEFT_DOWN, self.SelectPointHit)
self.SelectPoints.append(P)
#Canvas.ZoomToBB()
Canvas.Draw()
def SelectPointHit(self, Point):
self.Log("Point Num: %i Hit"%Point.VerticeNum)
self.SelectedPoint = Point
def Read_MapGen(self, filename, stats = 0,AllLines=0):
"""
This function reads a MapGen Format file, and
returns a list of NumPy arrays with the line segments in them.
Each NumPy array in the list is an NX2 array of Python Floats.
The demo should have come with a file, "world.dat" that is the
shorelines of the whole world, in MapGen format.
"""
import string
file = open(filename,'rt')
data = file.readlines()
data = map(string.strip,data)
Shorelines = []
segment = []
for line in data:
if line:
if line == "# -b": #New segment beginning
if segment: Shorelines.append(N.array(segment))
segment = []
else:
segment.append(map(float,string.split(line)))
if segment: Shorelines.append(N.array(segment))
if stats:
NumSegments = len(Shorelines)
NumPoints = 0
for segment in Shorelines:
NumPoints = NumPoints + len(segment)
AvgPoints = NumPoints / NumSegments
print "Number of Segments: ", NumSegments
print "Average Number of Points per segment: ",AvgPoints
if AllLines:
Lines = []
for segment in Shorelines:
Lines.append(segment[0])
for point in segment[1:-1]:
Lines.append(point)
Lines.append(point)
Lines.append(segment[-1])
return Lines
else:
return Shorelines
return DrawFrame
#---------------------------------------------------------------------------
if __name__ == "__main__":
# running stand alone, Use wxversion:
# import wxversion
# wxversion.select("2.6")
# wxversion.select("2.8")
import wx
# check options:
import sys, getopt
optlist, args = getopt.getopt(sys.argv[1:],'l',["all",
"text",
"map",
"stext",
"stextbox",
"bitmap",
"hit",
"hitf",
"animate",
"speed",
"temp",
"props",
"arrow",
"arrowline",
"hide"])
if not haveNumpy:
raise ImportError(errorText)
StartUpDemo = "all" # the default
if optlist:
StartUpDemo = optlist[0][0][2:]
class DemoApp(wx.App):
"""
How the demo works:
Under the Draw menu, there are three options:
*Draw Test: will put up a picture of a bunch of randomly generated
objects, of each kind supported.
*Draw Map: will draw a map of the world. Be patient, it is a big map,
with a lot of data, and will take a while to load and draw (about 10 sec
on my 450Mhz PIII). Redraws take about 2 sec. This demonstrates how the
performance is not very good for large drawings.
*Clear: Clears the Canvas.
Once you have a picture drawn, you can zoom in and out and move about
the picture. There is a tool bar with three tools that can be
selected.
The magnifying glass with the plus is the zoom in tool. Once selected,
if you click the image, it will zoom in, centered on where you
clicked. If you click and drag the mouse, you will get a rubber band
box, and the image will zoom to fit that box when you release it.
The magnifying glass with the minus is the zoom out tool. Once selected,
if you click the image, it will zoom out, centered on where you
clicked. (note that this takes a while when you are looking at the map,
as it has a LOT of lines to be drawn. The image is double buffered, so
you don't see the drawing in progress)
The hand is the move tool. Once selected, if you click and drag on the
image, it will move so that the part you clicked on ends up where you
release the mouse. Nothing is changed while you are dragging. The
drawing is too slow for that.
I'd like the cursor to change as you change tools, but the stock
wxCursors didn't include anything I liked, so I stuck with the
pointer. Please let me know if you have any nice cursor images for me to
use.
Any bugs, comments, feedback, questions, and especially code are welcome:
-Chris Barker
Chris.Barker@noaa.gov
"""
def __init__(self, *args, **kwargs):
wx.App.__init__(self, *args, **kwargs)
def OnInit(self):
DrawFrame = BuildDrawFrame()
frame = DrawFrame(None, -1, "FloatCanvas Demo App",wx.DefaultPosition,(700,700))
self.SetTopWindow(frame)
frame.Show()
## check to see if the demo is set to start in a particular mode.
## fixme: should this be in a dict instead?
if StartUpDemo == "text":
frame.TestText()
elif StartUpDemo == "stext":
frame.TestScaledText()
elif StartUpDemo == "stextbox":
frame.TestScaledTextBox()
elif StartUpDemo == "bitmap":
frame.TestBitmap()
elif StartUpDemo == "all":
frame.DrawTest()
elif StartUpDemo == "map":
frame.DrawMap()
elif StartUpDemo == "hit":
frame.TestHitTest()
elif StartUpDemo == "hitf":
frame.TestHitTestForeground()
elif StartUpDemo == "animate":
frame.TestAnimation()
elif StartUpDemo == "speed":
frame.SpeedTest()
elif StartUpDemo == "temp":
frame.TempTest()
elif StartUpDemo == "props":
frame.PropertiesChangeTest()
elif StartUpDemo == "arrow":
frame.ArrowTest()
elif StartUpDemo == "arrowline":
frame.ArrowLineTest()
elif StartUpDemo == "hide":
frame.HideTest()
return True
app = DemoApp(False)# put in True if you want output to go to it's own window.
app.MainLoop()
else:
# It's not running stand-alone, set up for wxPython demo.
# don't neeed wxversion here.
import wx
if not haveNumpy:
## TestPanel and runTest used for integration into wxPython Demo
class TestPanel(wx.Panel):
def __init__(self, parent, log):
self.log = log
wx.Panel.__init__(self, parent, -1)
from wx.lib.floatcanvas.ScreenShot import getScreenShotBitmap
note1 = wx.StaticText(self, -1, errorText)
note2 = wx.StaticText(self, -1, "This is what the FloatCanvas can look like:")
S = wx.BoxSizer(wx.VERTICAL)
S.Add((10, 10), 1)
S.Add(note1, 0, wx.ALIGN_CENTER)
S.Add(note2, 0, wx.ALIGN_CENTER | wx.BOTTOM, 4)
S.Add(wx.StaticBitmap(self,-1,getScreenShotBitmap()),0,wx.ALIGN_CENTER)
S.Add((10, 10), 1)
self.SetSizer(S)
self.Layout()
else:
## TestPanel and runTest used for integration into wxPython Demo
class TestPanel(wx.Panel):
def __init__(self, parent, log):
self.log = log
wx.Panel.__init__(self, parent, -1)
note1 = wx.StaticText(self, -1, "The FloatCanvas Demo needs")
note2 = wx.StaticText(self, -1, "a separate frame")
b = wx.Button(self, -1, "Open Demo Frame Now")
b.Bind(wx.EVT_BUTTON, self.OnButton)
S = wx.BoxSizer(wx.VERTICAL)
S.Add((10, 10), 1)
S.Add(note1, 0, wx.ALIGN_CENTER)
S.Add(note2, 0, wx.ALIGN_CENTER | wx.BOTTOM, 5)
S.Add(b, 0, wx.ALIGN_CENTER | wx.ALL, 5)
S.Add((10, 10), 1)
self.SetSizer(S)
self.Layout()
def OnButton(self, evt):
DrawFrame = BuildDrawFrame()
frame = DrawFrame(None, -1, "FloatCanvas Drawing Window",wx.DefaultPosition,(500,500))
#win = wx.lib.plot.TestFrame(self, -1, "PlotCanvas Demo")
frame.Show()
frame.DrawTest()
def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win
# import to get the doc
from wx.lib import floatcanvas
overview = floatcanvas.__doc__
|