Utils.py
Go to the documentation of this file.
00001 ###############################################################################
00002 #
00003 # Package:   RoadNarrows Hekateros Robotic Manipulator Package
00004 #
00005 # Link:      https://github.com/roadnarrows-robotics/hekateros
00006 #
00007 # ROS Node:  hek_*
00008 #
00009 # File:      Utils.py
00010 #
00011 ## \file 
00012 ##
00013 ## $LastChangedDate$
00014 ## $Rev$
00015 ##
00016 ## \brief Utilities.
00017 ##
00018 ## \author Daniel Packard (daniel@roadnarrows.com)
00019 ## \author Robin Knight (robin.knight@roadnarrows.com)
00020 ##  
00021 ## \par Copyright:
00022 ##   (C) 2013-2014.  RoadNarrows LLC.\n
00023 ##   (http://www.roadnarrows.com)\n
00024 ##   All Rights Reserved
00025 ##
00026 # @EulaBegin@
00027 # @EulaEnd@
00028 #
00029 ###############################################################################
00030 
00031 import sys
00032 import os
00033 import time
00034 import math
00035 
00036 from pkg_resources import *
00037 
00038 from Tkinter import *
00039 from Tkconstants import *
00040 from tkFileDialog import *
00041 import tkFont
00042 
00043 from PIL import Image, ImageTk
00044 
00045 # ------------------------------------------------------------------------------
00046 # Class ImageLoader
00047 # ------------------------------------------------------------------------------
00048 
00049 #
00050 ## \brief Class to handle image loading.
00051 #
00052 class ImageLoader:
00053 
00054   #
00055   ## \brief Constructor
00056   ##
00057   ## \param py_pkg      Python resource (e.g. "hekateros_control.images").
00058   ## \param image_paths List of directory paths to search for the image.
00059   #
00060   def __init__(self, py_pkg=None, image_paths=[]):
00061     self.m_pyPkg = py_pkg
00062     if len(image_paths) > 0:
00063       self.m_imagePaths = image_paths
00064     else:
00065       self.m_imagePaths = ['.']
00066   
00067   #
00068   ## \brief Class to handle image loading.
00069   #Load icon image from file name.
00070   ##
00071   ## \param filename    Icon file name.
00072   ##
00073   ## \return Returns icon widget on success, None on failure.
00074   #
00075   def load(self, filename):
00076     # no file name
00077     if filename is None or len(filename) == 0:
00078       return None;
00079     # absolute file name
00080     if filename[0] == os.path.sep:
00081       try:
00082         return ImageTk.PhotoImage(Image.open(filename))
00083       except IOError:
00084         return None
00085     # relative file name - try python resource(s) first
00086     if self.m_pyPkg:
00087       try:
00088         fqname = resource_filename(self.m_pyPkg, filename)
00089         try:
00090           return ImageTk.PhotoImage(Image.open(fqname))
00091         except IOError:
00092           pass
00093       except ImportError:
00094         pass
00095     # relative file name - search path for file
00096     for path in self.m_imagePaths:
00097       fqname = path + os.path.sep + filename
00098       try:
00099         return ImageTk.PhotoImage(Image.open(fqname))
00100       except IOError:
00101         continue
00102     return None
00103 
00104 
00105 # ------------------------------------------------------------------------------
00106 # Misc. Utilities
00107 # ------------------------------------------------------------------------------
00108 
00109 #
00110 #
00111 ## Round to nearest 100th.
00112 #
00113 def round100th(x):
00114   return math.floor((x + 0.005) * 100.0) / 100.0
00115 
00116 #
00117 ## Round to nearest 10th.
00118 #
00119 def round10th(x):
00120   return math.floor((x + 0.05) * 10.0) / 10.0
00121 
00122 #
00123 ## Degrees to radians.
00124 #
00125 def degToRad(deg):
00126   return deg / 180.0 * math.pi
00127 
00128 #
00129 ## Radians to degrees.
00130 #
00131 def radToDeg(rad):
00132   return rad / math.pi * 180.0


hekateros_control
Author(s): Robin Knight , Daniel Packard
autogenerated on Mon Oct 6 2014 00:36:42