##############################################################################
# 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 displays a dialog for the user to export to a raster image
using Python Image Library.
"""
from Tkinter import *
from p_ggen import path
from p_gtkwid import ThanEntry,ThanFile,ThanChoice
import p_gtkuti
thanImageTypes = \
{ "TIFF": ".tif",
"BMP" : ".bmp",
"JPEG": ".jpg",
}
class ThanTkExppil(p_gtkuti.ThanDialog):
"Dialog for the visibility of a layer."
def __init__(self, master, val, *args, **kw):
"Extract initial style."
self.__val = val
p_gtkuti.ThanDialog.__init__(self, master, *args, **kw)
def body(self, fra):
"Create dialog widgets."
w = Label(fra, text="Image type")
w.grid(row=0, column=0, sticky="e")
self.thanType = ThanChoice(fra, labels=sorted(thanImageTypes.keys()), command=self.__typeSet,
width=10, relief=RIDGE, anchor="w")
self.thanType.grid(row=0, column=1, sticky="we")
w = Label(fra, text="Image file")
w.grid(row=1, column=0, sticky="e")
self.thanFile = ThanFile(fra, text=self.__val, extension="*.bmp", mode="w", title="Open image file")
self.thanFile.grid(row=1, column=1, sticky="we")
w = Label(fra, text="width (pixels)", fg="blue")
w.grid(row=2, column=0, sticky="e")
self.thanWidth = ThanEntry(fra)
self.thanWidth.grid(row=2, column=1, sticky="we")
self.thanWidth.thanSet("1024")
w = Label(fra, text="height (pixels)", fg="blue")
w.grid(row=3, column=0, sticky="e")
self.thanHeight = ThanEntry(fra)
self.thanHeight.grid(row=3, column=1, sticky="we")
self.thanHeight.thanSet("1024")
self.thanType.thanSetText("BMP")
del self.__val
fra.columnconfigure(1, weight=1)
return self.thanFile # This widget has the focus
def __typeSet(self, i, imtyp):
"Change the type of the image."
ext = thanImageTypes[imtyp]
self.thanFile.config(extension="*"+ext)
fn = self.thanFile.thanGet().strip()
if fn == "": return
fn = path(fn)
fn = fn.parent / fn.namebase+ext
self.thanFile.thanSet(fn)
def destroy(self):
"Deletes self references."
del self.thanType, self.thanFile, self.thanWidth, self.thanHeight
p_gtkuti.ThanDialog.destroy(self)
def validate(self, strict=True):
"Returns true if the value chosen by the user is valid."
ok = True
try:
height = int(self.thanHeight.thanGet())
if height <= 0: raise ValueError
except:
if strict:
p_gtkuti.thanGudModalMessage(self, "Invalid height", "Error Message")
self.initial_focus = self.thanHeight
return False
height = 1024; ok = False
try:
width = int(self.thanWidth.thanGet())
if width <= 0: raise ValueError
except:
if strict:
p_gtkuti.thanGudModalMessage(self, "Invalid width", "Error Message")
self.initial_focus = self.thanWidth
return False
width = 1024; ok = False
filnam = path(self.thanFile.thanGet())
if filnam.ext == "":
ext = thanImageTypes[self.thanType.thanGetText()]
filnam = filnam.parent / (filnam.namebase+ext)
self.result = self.thanVals = (filnam, width, height)
return ok
def __del__(self):
print "ThanTstyle ThanDialog", self, "dies.."
def test():
root = Tk()
win = ThanTkExppil(root, "q1.png", title="Export to Image: specifications")
print win.result
test()
|