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

# Copyright (c) 2002, 2003 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a widget for displaying logging messages.
"""

from qt import *

from LogView import *

class LogWidget(LogViewWidget):
    """
    A class for displaying logging messages.
    
    LogWidget is a simple class to be plugged in a LogView. 
    It displays lines of text without interpretation of richtext or html tags. 
    Neither are special characters like newlines or tabs interpreted.
    The widget can either store all the strings sent to it (the default) or 
    can limit the number of lines to store. In this case the oldest lines 
    are discarded when new lines arrive.

    To use a LogWidget create an instance of LogView and add the 
    LogWidgets to this object (a LogView can hold more than a single 
    LogWidget).
    """
    def __init__(self, parent=None, name=None):
        """
        Constructor
        
        Arguments
        
            parent -- parent widget (QWidget)
            
            name -- name of this widget (string or QString)
        """
        LogViewWidget.__init__(self, parent, name)
        self.__listLines = QStringList()
        self.__maxLines = -1
        self.__maxLen = 0
        self.setPaletteBackgroundColor(self.colorGroup().base())
        self.setPaletteForegroundColor(self.colorGroup().text())
        
    def preferredBackgroundColor(self):
        """
        Reimplemented to return colorgroup().base().
        
        Returns
        
            preferred background colour (QColor)
        """
        return self.colorGroup().base()
        
    def paintEvent(self, evt):
        """
        Reimplemented for custom painting.
        """
        fm = self.fontMetrics()
        height = fm.height()
        x = 2
        y = height
        p = QPainter(self)
        
        for line in self.__listLines:
            p.drawText(x, y, line)
            y += height
            
    def handleSetMaxLines(self, val):
        """
        Sets the maximum number of lines to be shown. 
        
        Arguments
        
            val -- maximum number of lines to be displayed
                If val is <= 0 then there will be no limit. If the maximum number 
                of lines is appended, the oldest are discarded.
        """
        self.__maxLines = val
        
    def append(self, text):
        """
        Public method to append text to the messages. 
        
        When the LogWidget is already scrolled to the bottom, it will 
        further scroll down to display the newly added line. If the 
        scrolling position is not at the end, this position is not changed.
        
        Arguments
        
            text -- text to be appended (string or QString)
        """
        fm = self.fontMetrics()
        self.__listLines.append(text)
        self.__maxLen = max(self.__maxLen, fm.boundingRect(text).width()+2)
        if self.__maxLines > 0 and self.__listLines.size() > self.__maxLines:
            self.__listLines = self.__listLines[-self.__maxLen:]
        self.setMinimumSize(self.__maxLen, self.__listLines.count()*fm.height() + 2)
        self.emit(PYSIGNAL('sizeChanged'), (self,))
        self.update()
        qApp.processEvents(10)
        
    def clear(self):
        """
        Public method to delete all strings from the internal buffer and clears the display.
        """
        self.__listLines.clear()
        fm = self.fontMetrics()
        self.setMinimumSize(QSize(min(200, self.__maxLen*fm.maxWidth()),
                                min(200, self.__listLines.count()*fm.height() + 2)))
        self.resize(1, 1)
        self.emit(PYSIGNAL('sizeChanged'), (self,))
        self.update()
