# -*- coding: utf-8 -*-

# Copyright (c) 2006 - 2007 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the Project Browser configuration page.
"""

from PyQt4.QtCore import pyqtSignature
from PyQt4.QtGui import QPixmap, QIcon, QColor

from KdeQt import KQColorDialog

from ConfigurationPageBase import ConfigurationPageBase
from Ui_ProjectBrowserPage import Ui_ProjectBrowserPage

import Preferences

class ProjectBrowserPage(ConfigurationPageBase, Ui_ProjectBrowserPage):
    """
    Class implementing the Project Browser configuration page.
    """
    def __init__(self):
        """
        Constructor
        """
        ConfigurationPageBase.__init__(self)
        self.setupUi(self)
        self.setObjectName("ProjectBrowserPage")
        
        self.projectBrowserColours = {}
        
        # set initial values
        self.__initProjectBrowserColour("Highlighted", 
            self.pbHighlightedButton)
        self.__initProjectBrowserColour("VcsModified", 
            self.pbVcsModifiedButton)
        self.__initProjectBrowserColour("VcsUpdate", 
            self.pbVcsUpdateButton)
        self.__initProjectBrowserColour("VcsAdded", 
            self.pbVcsAddedButton)
        self.__initProjectBrowserColour("VcsConflict", 
            self.pbVcsConflictButton)
        
        self.hideGeneratedCheckBox.setChecked(\
            Preferences.getProject("HideGeneratedForms"))
        
    def save(self):
        """
        Public slot to save the Project Browser configuration.
        """
        for key in self.projectBrowserColours.keys():
            Preferences.setProjectBrowserColour(key, self.projectBrowserColours[key])
        
        Preferences.setProject("HideGeneratedForms", 
            int(self.hideGeneratedCheckBox.isChecked()))
        
    def __initProjectBrowserColour(self, colourstr, button):
        """
        Private method to initialize the colour buttons.
        
        @param colourstr Colour to be set. (string)
        @param button Reference to a QButton to show the colour on.
        """
        colour = QColor(Preferences.getProjectBrowserColour(colourstr))
        self.projectBrowserColours[colourstr] = colour
        size = button.size()
        pm = QPixmap(size.width()/2, size.height()/2)
        pm.fill(colour)
        button.setIconSize(pm.size())
        button.setIcon(QIcon(pm))
        
    def __selectProjectBrowserColour(self, colourstr, button):
        """
        Private method used by the buttons selecting the project browser colours.
        
        @param colourstr Colour to be set. (string)
        @param button Reference to a QButton to show the colour on.
        """
        colour = KQColorDialog.getColor(self.projectBrowserColours[colourstr])
        if colour.isValid():
            self.projectBrowserColours[colourstr] = colour
            size = button.iconSize()
            pm = QPixmap(size.width(), size.height())
            pm.fill(colour)
            button.setIcon(QIcon(pm))
        
    @pyqtSignature("")
    def on_pbHighlightedButton_clicked(self):
        """
        Private slot to set the colour for highlighted entries of the 
        project others browser.
        """
        self.__selectProjectBrowserColour("Highlighted", 
            self.pbHighlightedButton)
        
    @pyqtSignature("")
    def on_pbVcsModifiedButton_clicked(self):
        """
        Private slot to set the background colour for entries with VCS 
        status "modified".
        """
        self.__selectProjectBrowserColour("VcsModified", 
            self.pbVcsModifiedButton)
        
    @pyqtSignature("")
    def on_pbVcsUpdateButton_clicked(self):
        """
        Private slot to set the background colour for entries with VCS 
        status "needs update".
        """
        self.__selectProjectBrowserColour("VcsUpdate", 
            self.pbVcsUpdateButton)
        
    @pyqtSignature("")
    def on_pbVcsAddedButton_clicked(self):
        """
        Private slot to set the background colour for entries with VCS 
        status "added".
        """
        self.__selectProjectBrowserColour("VcsAdded", 
            self.pbVcsAddedButton)
        
    @pyqtSignature("")
    def on_pbVcsConflictButton_clicked(self):
        """
        Private slot to set the background colour for entries with VCS 
        status "conflict".
        """
        self.__selectProjectBrowserColour("VcsConflict", 
            self.pbVcsConflictButton)
    
def create(dlg):
    """
    Module function to create the configuration page.
    
    @param dlg reference to the configuration dialog
    """
    page = ProjectBrowserPage()
    return page
