Source

Fat x Fast / FatxFast / gui / menuwidget.py

Full commit
# This file is part of FatxFast.
#
#    FatxFast 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.
#
#    FatxFast 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 FatxFast.  If not, see <http://www.gnu.org/licenses/>.

# Versioning based on: 
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
__author__ = "dryatu (c) 2013"
__version__ = "1.2.5"


from FatxFast.gui import containerwidget

class MenuWidget(containerwidget.ContainerWidget):
    
    def __init__(self, **kwargs):
        super(MenuWidget, self).__init__(**kwargs)
        
    def add_widget(self, name, widget):
        if widget.auto_y_pos:
            self.init_child_y_pos(widget)
        super(MenuWidget, self).add_widget(name, widget)
        if self.auto_size:
            self.correct_height()
        
    def correct_height(self):
        h = 0
        for widget in self.widgets.values():
            h += widget.rect.height
        self.rect.height = h
        if (isinstance(self.container, MenuWidget) and 
            self.container.auto_size):
                self.container.correct_height()
 
    def init_child_y_pos(self, widget):
        current_list_height = 0
        for w in self.widgets:
            if self.widgets[w].auto_y_pos:
                current_list_height += self.widgets[w].rect.height
        widget.rect.y = current_list_height
        
    def kill(self):
        if super(MenuWidget, self).kill():
            self.container.fix_children_y_pos()

    def fix_children_y_pos(self):
        widgetlist = []
        for w in self.widgets:
            if self.widgets[w].auto_y_pos:
                widgetlist.append(self.widgets[w])
        widgetlist.sort(key=lambda x: x.rect.y)
        for i, w in enumerate(widgetlist):
            w.rect.y = 0
            j = 1
            while i-j >= 0:
                w.rect.y += widgetlist[i-j].rect.height
                j += 1
        if self.auto_size:
            self.correct_height()