Package horizons :: Package gui :: Package widgets :: Module progressbar
[hide private]
[frames] | no frames]

Source Code for Module horizons.gui.widgets.progressbar

  1  # ################################################### 
  2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
  3  # team@unknown-horizons.org 
  4  # This file is part of Unknown Horizons. 
  5  # 
  6  # Unknown Horizons is free software; you can redistribute it and/or modify 
  7  # it under the terms of the GNU General Public License as published by 
  8  # the Free Software Foundation; either version 2 of the License, or 
  9  # (at your option) any later version. 
 10  # 
 11  # This program is distributed in the hope that it will be useful, 
 12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  # GNU General Public License for more details. 
 15  # 
 16  # You should have received a copy of the GNU General Public License 
 17  # along with this program; if not, write to the 
 18  # Free Software Foundation, Inc., 
 19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 20  # ################################################### 
 21   
 22   
 23  from fife.extensions.pychan.widgets import ABox, Icon, Widget 
 24  from fife.extensions.pychan.widgets.common import Attr, IntAttr 
 25   
 26  from horizons.gui.widgets.icongroup import TilingHBox 
 27   
 28   
29 -class ProgressBar(ABox):
30 """The ProgressBar is a pychan widget. It can be used in xml files like this: 31 <ProgressBar /> 32 It is used to display a ProgressBar with a certain progress ;). Set the 33 widgets progress attribute to set the progress. Pretty straight forward. 34 The progress is a value from 0 to 100. Think of it as percent. 35 """ 36 ATTRIBUTES = ABox.ATTRIBUTES + [ 37 IntAttr('progress'), Attr('fill'), Attr('background'), 38 ] 39
40 - def __init__(self, progress=0, fill=None, background=None, **kwargs):
41 super().__init__(**kwargs) 42 if self.max_size == Widget.DEFAULT_MAX_SIZE: 43 self.max_size = (100, 16) 44 self.__progress = progress 45 self.__fill = fill 46 self.__background = background 47 self.tiles = None 48 self.bg_icon = None
49
50 - def _set_progress(self, progress):
51 self.__progress = progress 52 if self.bg_icon is None: 53 self.background = "content/gui/images/background/bar_bg.png" 54 if self.tiles is None: 55 self.fill = "content/gui/images/background/widgets/progressbar_fill.png" 56 self.tiles.size = (int(self.max_size[0] * progress / 100.0), self.max_size[1]) 57 self.adaptLayout()
58
59 - def _get_progress(self):
60 return self.__progress
61
62 - def _set_fill_image(self, image):
63 self.__fill = image 64 self.tiles = Icon(image=image, scale=True) 65 self.addChild(self.tiles)
66
67 - def _get_fill_image(self):
68 return self.__fill
69
70 - def _set_background(self, background):
71 self.__background = background 72 self.bg_icon = Icon(image=background, scale=True) 73 self.bg_icon.min_size = self.bg_icon.size = self.max_size 74 self.addChild(self.bg_icon)
75
76 - def _get_background(self):
77 return self.__background
78 79 progress = property(_get_progress, _set_progress) 80 fill = property(_get_fill_image, _set_fill_image) 81 background = property(_get_background, _set_background)
82 83
84 -class TilingProgressBar(ProgressBar):
85 """ProgressBar that tiles its fill image instead of stretching. 86 87 Also supports distinct left and right border/frame images which count 88 towards the displayed progress value and fill images of width > 1px. 89 """ 90 ATTRIBUTES = ProgressBar.ATTRIBUTES + [Attr('left'), Attr('right')] 91
92 - def __init__(self, left=None, right=None, **kwargs):
93 super().__init__(**kwargs) 94 self.__left = left 95 self.__right = right 96 self.tiles = TilingHBox() 97 self.addChild(self.tiles) 98 self.tiles_width = 1 99 self.left_width = 0 100 self.right_width = 0
101
102 - def _get_progress(self):
103 return self.__progress
104
105 - def _set_progress(self, progress):
106 self.__progress = progress 107 fill_width = (progress / 100.0) * (self.max_size[0] / self.tiles_width) 108 self.tiles.amount = int(fill_width) - self.left_width - self.right_width 109 self.adaptLayout()
110
111 - def _get_left_image(self):
112 return self.__left
113
114 - def _set_left_image(self, image):
115 self.__left = image 116 self.left_width = Icon(image=image).size[0] 117 self.tiles.start_img = image
118
119 - def _get_fill_image(self):
120 return self.__fill
121
122 - def _set_fill_image(self, image):
123 self.__fill = image 124 self.tiles_width = Icon(image=image).size[0] 125 self.tiles.tiles_img = image
126
127 - def _get_right_image(self):
128 return self.__right
129
130 - def _set_right_image(self, image):
131 self.__right = image 132 self.right_width = Icon(image=image).size[0] 133 self.tiles.final_img = image
134 135 progress = property(_get_progress, _set_progress) 136 left = property(_get_left_image, _set_left_image) 137 fill = property(_get_fill_image, _set_fill_image) 138 right = property(_get_right_image, _set_right_image)
139