##############################################################################
# ThanCad 0.0.9 "DoesSomething": 2dimensional CAD with raster support for engineers.
#
# Copyright (c) 2001-2009 Thanasis Stamos, August 23, 2009
# URL: http://thancad.sourceforge.net
# e-mail: cyberthanasis@excite.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details (www.gnu.org/licenses/gpl.html).
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
"""\
ThanCad 0.0.9 "DoesSomething": 2dimensional CAD with raster support for engineers.
This module defines various croshairs for ThanCad's canvas.
"""
class ThanCrosHairs:
"An object which deals with croshairs."
def __init__(self, proj):
"Define which croshairs are going to be used."
self.thanProj = proj
self.thanCrosHairs = [ (CrosHair,),
(CrosHairCros, 60),
(CrosHairCros, 40),
(CrosHairCros, 20),
(CrosHairRect, proj[2].BSEL),
(CrosHairDummy,),
]
self.thanIcrosHair = [0] # Set big croshair
def thanRotate(self, x1=None, y1=None):
"Changes the croshair."
self.thanIcrosHair[-1] = (self.thanIcrosHair[-1] + 1) % len(self.thanCrosHairs)
self.thanSet(self.thanIcrosHair[-1], x1, y1)
def thanPush(self, i, x1=None, y1=None):
"Change the cursor temporarily."
i %= len(self.thanCrosHairs)
self.thanIcrosHair.append(i)
self.thanSet(self.thanIcrosHair[-1], x1, y1)
def thanPop(self, x1=None, y1=None):
"Change to the previous cursor."
if len(self.thanIcrosHair) < 1: return
del self.thanIcrosHair[-1]
self.thanSet(self.thanIcrosHair[-1], x1, y1)
def thanSet(self, i, x1=None, y1=None):
"Set the croshair."
dc = self.thanProj[2].thanCanvas
i %= len(self.thanCrosHairs)
if x1 == None:
x1 = dc.thanCh.thanX1p
y1 = dc.thanCh.thanY1p
clas = self.thanCrosHairs[i][0]
args = self.thanCrosHairs[i][1:]
dc.thanCh.thanDisable()
dc.thanCh = clas(dc, *args)
dc.thanCh.resize(x1, y1)
#############################################################################
#############################################################################
class CrosHair(object):
"Draws, moves, deletes and maintains a croshair in a canvas."
def __init__(self, dc):
"Initialise the croshair."
self.thanDc = dc
self.thanExists = 0
self.thanX1p = None
self.thanY1p = None
self.thanOn = True
self.resize()
def destroy(self):
"Break circular references."
del self.thanDc
def resize(self, x1=None, y1=None):
"Recomputes the size of the croshair."
dc = self.thanDc
dc.update_idletasks() # _idletasks breaks WinDoze (98?) support. Skotistika
maxx = dc.winfo_width() - 1
maxy = dc.winfo_height() - 1
minx = 0
miny = 0
self.thanMinx = dc.canvasx(minx)
self.thanMiny = dc.canvasy(miny)
self.thanMaxx = dc.canvasx(maxx)
self.thanMaxy = dc.canvasy(maxy)
if self.thanExists:
# self.thanX1p = dc.coords(self.thanCh1)[0]
# self.thanY1p = dc.coords(self.thanCh2)[1]
dc.delete(self.thanCh1)
dc.delete(self.thanCh2)
self.thanExists = 0
if x1 == None: x1 = self.thanX1p; y1 = self.thanY1p
if x1 < self.thanMinx or x1 > self.thanMaxx or \
y1 < self.thanMiny or y1 > self.thanMaxy:
x1 = self.thanMinx + (self.thanMaxx - self.thanMinx) / 2
y1 = self.thanMiny + (self.thanMaxy - self.thanMiny) / 2
self.draw(x1, y1)
self.thanX1p = x1; self.thanY1p = y1
def draw(self, x1, y1):
"Draws a croshair and deletes previous croshair."
if self.thanOn:
dc = self.thanDc
if self.thanExists:
# dc.move(self.thanCh1, x1-self.thanX1p, 0)
# dc.move(self.thanCh2, 0, y1-self.thanY1p)
dc.coords(self.thanCh1, x1, self.thanMiny, x1, self.thanMaxy,)
dc.coords(self.thanCh2, self.thanMinx, y1, self.thanMaxx, y1)
dc.lift(self.thanCh1)
dc.lift(self.thanCh2)
else:
self.thanCh1 = dc.create_line(x1, self.thanMiny, x1, self.thanMaxy, fill="red")
self.thanCh2 = dc.create_line(self.thanMinx, y1, self.thanMaxx, y1, fill="green")
self.thanExists = 1
self.thanX1p = x1
self.thanY1p = y1
def delete(self):
"Deletes the croshair from the canvas - if it exists."
if self.thanExists:
dc = self.thanDc
dc.delete(self.thanCh1)
dc.delete(self.thanCh2)
self.thanExists = 0
def thanEnable(self, x1=None, y1=None):
"Enable the croshair."
self.thanOn = True
self.resize(x1, y1)
def thanDisable(self):
"Disable the croshair."
self.thanOn = False
self.delete()
#############################################################################
#############################################################################
class CrosHairRect(CrosHair):
"Draws, moves, deletes and maintains a limited croshair in a canvas."
def __init__(self, dc, size):
"Take size as an argument."
self.dx = self.dy = size/2
CrosHair.__init__(self, dc)
def draw(self, x1, y1):
"Draws a croshair and deletes previous croshair."
if self.thanOn:
dc = self.thanDc
if self.thanExists:
dc.coords(self.thanCh1, x1-self.dx, y1-self.dy, x1+self.dx, y1+self.dy)
dc.lift(self.thanCh1)
else:
self.thanCh1 = dc.create_rectangle(x1-self.dx, y1-self.dy, x1+self.dx, y1+self.dy, outline="red")
self.thanCh2 = None
self.thanExists = 1
self.thanX1p = x1
self.thanY1p = y1
#############################################################################
#############################################################################
class CrosHairCros(CrosHair):
"Draws, moves, deletes and maintains a limited croshair in a canvas."
def __init__(self, dc, size=40):
"Take size as an argument."
self.dx = self.dy = size
CrosHair.__init__(self, dc)
def draw(self, x1, y1):
"Draws a croshair and deletes previous croshair."
if self.thanOn:
dc = self.thanDc
if self.thanExists:
# dc.move(self.thanCh1, x1-self.thanX1p, 0)
# dc.move(self.thanCh2, 0, y1-self.thanY1p)
dc.coords(self.thanCh1, x1, y1-self.dy, x1, y1+self.dy)
dc.coords(self.thanCh2, x1-self.dx, y1, x1+self.dx, y1)
dc.lift(self.thanCh1)
dc.lift(self.thanCh2)
else:
self.thanCh1 = dc.create_line(x1, y1-self.dy, x1, y1+self.dy, fill="red")
self.thanCh2 = dc.create_line(x1-self.dx, y1, x1+self.dx, y1, fill="green")
self.thanExists = 1
self.thanX1p = x1
self.thanY1p = y1
#############################################################################
#############################################################################
class CrosHairDummy(CrosHair):
"A croshair that does nothing."
def draw(self, x1, y1):
"Draws a croshair and deletes previous croshair."
if self.thanOn:
if self.thanExists:
pass
else:
self.thanCh1 = None
self.thanCh2 = None
self.thanExists = 1
self.thanX1p = x1
self.thanY1p = y1
|