Preferences/ConfigurationPages/ConfigurationPageBase.py

Sun, 25 Nov 2012 20:22:02 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 25 Nov 2012 20:22:02 +0100
changeset 2229
78539385a8df
parent 2228
5c59b9393306
child 2230
2b1b9265156c
permissions
-rw-r--r--

Optimized the IRC color selection code a bit.

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

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

"""
Module implementing the base class for all configuration pages.
"""

from PyQt4.QtGui import QWidget, QIcon, QPixmap, QColor, QColorDialog, QFontDialog


class ConfigurationPageBase(QWidget):
    """
    Class implementing the base class for all configuration pages.
    """
    def __init__(self):
        """
        Constructor
        """
        super().__init__()
        
    def polishPage(self):
        """
        Public slot to perform some polishing actions.
        """
        return
        
    def saveState(self):
        """
        Public method to save the current state of the widget.
        """
        return None
        
    def setState(self, state):
        """
        Public method to set the state of the widget.
        
        @param state state data generated by saveState
        """
        return
        
    def initColour(self, colourstr, button, prefMethod):
        """
        Public method to initialize a colour selection button.
        
        @param colourstr colour to be set (string)
        @param button reference to a button to show the colour on (QPushButton)
        @param prefMethod preferences method to get the colour
        @return reference to the created colour (QColor)
        """
        colour = QColor(prefMethod(colourstr))
        size = button.size()
        pm = QPixmap(size.width() / 2, size.height() / 2)
        pm.fill(colour)
        button.setIconSize(pm.size())
        button.setIcon(QIcon(pm))
        return colour
        
    def initColour2(self, colourDict, colourstr, button, prefMethod, selectSlot):
        """
        Public method to initialize a colour selection button.
        
        @param colourDict reference to the dictionary storing the colors
        @param colourstr colour to be set (string)
        @param button reference to a button to show the colour on (QPushButton)
        @param prefMethod preferences method to get the colour
        @param selectSlot method to select the color
        """
        colour = QColor(prefMethod(colourstr))
        size = button.size()
        pm = QPixmap(size.width() / 2, size.height() / 2)
        pm.fill(colour)
        button.setIconSize(pm.size())
        button.setIcon(QIcon(pm))
        button.setProperty("colorName", colourstr)
        button.clicked[()].connect(selectSlot)
        colourDict[colourstr] = colour
        
    def selectColour(self, button, colourVar, showAlpha=False):
        """
        Public method used by the colour selection buttons.
        
        @param button reference to a button to show the colour on (QPushButton)
        @param colourVar reference to the variable containing the colour (QColor)
        @param showAlpha flag indicating to show a selection for the alpha
            channel (boolean)
        @return selected colour (QColor)
        """
        if showAlpha:
            colour = QColorDialog.getColor(colourVar, None, "",
                QColorDialog.ShowAlphaChannel)
        else:
            colour = QColorDialog.getColor(colourVar)
        if colour.isValid():
            size = button.iconSize()
            pm = QPixmap(size.width(), size.height())
            pm.fill(colour)
            button.setIcon(QIcon(pm))
        else:
            colour = colourVar
        return colour
        
    def selectFont(self, fontSample, fontVar, showFontInfo=False):
        """
        Public method used by the font selection buttons.
        
        @param fontSample reference to the font sample widget (QLineEdit)
        @param fontVar reference to the variable containing the font (QFont)
        @param showFontInfo flag indicating to show some font info
            as the sample (boolean)
        @return selected font (QFont)
        """
        font, ok = QFontDialog.getFont(fontVar)
        if ok:
            fontSample.setFont(font)
            if showFontInfo:
                fontSample.setText(
                    "{0} {1}".format(font.family(), font.pointSize()))
        else:
            font = fontVar
        return font

eric ide

mercurial