##############################################################################
# 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",
"PNG": ".png",
}
thanImageModes = \
{ "Full RGB" : "RGB",
"Gray scale": "L",
"Bitmap" : "1",
}
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."
ir = 0
w = Label(fra, text="Image type")
w.grid(row=ir, 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=ir, column=1, sticky="we")
ir += 1
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=ir, column=1, sticky="we")
ir += 1
w = Label(fra, text="Image mode")
w.grid(row=ir, column=0, sticky="e")
self.thanMode = ThanChoice(fra, labels=sorted(thanImageModes.keys()),
width=10, relief=RIDGE, anchor="w")
self.thanMode.grid(row=ir, column=1, sticky="we")
ir += 1
w = Label(fra, text="width (pixels)", fg="blue")
w.grid(row=ir, column=0, sticky="e")
self.thanWidth = ThanEntry(fra, width=35)
self.thanWidth.grid(row=ir, column=1, sticky="we")
self.thanWidth.thanSet("1024")
ir += 1
w = Label(fra, text="height (pixels)", fg="blue")
w.grid(row=ir, column=0, sticky="e")
self.thanHeight = ThanEntry(fra)
self.thanHeight.grid(row=ir, column=1, sticky="we")
self.thanHeight.thanSet("1024")
ir += 1
w = Label(fra, text="Plot Area", fg="darkgreen")
w.grid(row=ir, column=0, sticky="e")
self.thanWin = ThanEntry(fra)
self.thanWin.grid(row=ir, column=1, sticky="we")
self.thanWin.thanSet("Display")
self.thanWin.config(state=DISABLED)
self.thanType.thanSetText("BMP")
self.thanMode.thanSetText("Full RGB")
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.thanMode, self.thanWidth
del self.thanHeight, self.thanWin
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)
mode = thanImageModes[self.thanMode.thanGetText()]
self.result = self.thanVals = (filnam, mode, width, height, self.thanWin.thanGet().lower())
return ok
def __del__(self):
print "ThanExppil ThanDialog", self, "dies.."
def test():
root = Tk()
win = ThanTkExppil(root, "q1.png", title="Export to Image: specifications")
print win.result
if __name__ == "__main__": print __doc__; test()
|