Tizen 2.1 base
[platform/upstream/hplip.git] / ui / ui_utils.py
1 # -*- coding: utf-8 -*-
2 #
3 # (c) Copyright 2001-2007 Hewlett-Packard Development Company, L.P.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # Author: Don Welch
20 #
21 # Thanks to Henrique M. Holschuh <hmh@debian.org> for various security patches
22 #
23
24 # Std Lib
25 import os.path
26
27 # Local
28 from base.g import *
29 from base import utils
30
31 # Qt
32 try:
33     from qt import *
34 except ImportError:
35     log.error("Unable to load qt3. Is python-qt3 installed?")
36
37 # TODO: Cache pixmaps
38
39 def load_pixmap(name, subdir=None, resize_to=None): # Qt3 only
40     name = ''.join([os.path.splitext(name)[0], '.png'])
41     
42     if subdir is None:
43         dir = prop.image_dir
44         ldir = os.path.join(os.getcwd(), 'data', 'images')
45     else:
46         dir = os.path.join(prop.image_dir, subdir)
47         ldir = os.path.join(os.getcwd(), 'data', 'images', subdir)
48     
49     for d in [dir, ldir]:
50         f = os.path.join(d, name)
51     
52         if os.path.exists(f):
53             if resize_to is not None:
54                 img = QImage(f)
55                 pm = QPixmap()
56                 pm.convertFromImage(img.smoothScale(*resize_to), 0)
57                 return pm
58             else:
59                 return QPixmap(f)
60         
61         for w in utils.walkFiles(dir, recurse=True, abs_paths=True, return_folders=False, pattern=name):
62             if resize_to is not None:
63                 img = QImage(w)
64                 pm = QPixmap()
65                 pm.convertFromImage(img.smoothScale(*resize_to), 0)
66                 return pm
67             else:
68                 return QPixmap(w)
69
70     log.error("Pixmap '%s' not found!" % name)
71     return QPixmap()
72