File: theme.py

package info (click to toggle)
pybik 3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,280 kB
  • sloc: python: 11,362; cpp: 5,116; xml: 264; makefile: 50; sh: 2
file content (76 lines) | stat: -rw-r--r-- 2,417 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#-*- coding:utf-8 -*-

#  Copyright © 2009, 2011-2017  B. Clausius <barcc@gmx.de>
#
#  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 3 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.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.


import os
from glob import glob
from collections import defaultdict

from . import config
from .ext import qt


class Textures:
    max_size = 256
    stock_dir = os.path.join(config.UI_DIR, 'images')
    
    def __init__(self):
        self.stock_images = {os.path.basename(f): None for f in glob(os.path.join(self.stock_dir, '*'))}
        self.empty_image = (1, 1, b'\x00\x00\x00\x00')
        
    def _get_stock_image(self, name):
        filename = os.path.join(self.stock_dir, name)
        self.stock_images[name] = image = qt.load_image_from_file(filename, self.max_size)
        return image
            
    def get_icons(self):
        return sorted(self.stock_images.keys())
            
    def image_from_file(self, filename):
        if not filename:
            image = self.empty_image
        elif filename.startswith('/'):
            image = qt.load_image_from_file(filename, self.max_size)
        else:
            try:
                image = self.stock_images[filename]
                if image is None:
                    image = self._get_stock_image(filename)
            except KeyError:
                image = None
        return image
        
        
class FaceTheme:
    __slots__ = 'color imagemode image texrect'.split()
    
    
class Theme:
    textures = Textures()
    
    def __init__(self):
        self.faces = defaultdict(FaceTheme)
        
    def load_face_image(self, facekey, filename):
        image = self.textures.image_from_file(filename)
        if image is None:
            self.faces[facekey].image = self.textures.empty_image
            return False
        self.faces[facekey].image = image
        return True