#!/usr/bin/env python

""" module containing the Tau Widget: <_TauClass_> """

from taurus.qt import QtGui, QtCore, Qt
import tau.core
from tau.widget import TauBaseWidget

class < _TauClass_ > ( < _SuperClass_ > , TauBaseWidget):
    """ <_TauClass_> is a Tau widget designed to represent..."""

    #-------------------------------------------------------------------------
    # Write your own code here to define the signals generated by this widget
    #

    def __init__(self, parent=None, designMode=False):
        name = self.__class__.__name__

        self.call__init__wo_kw(< _SuperClass_ > , parent)
        self.call__init__(TauBaseWidget, name, parent, designMode=designMode)

        self.defineStyle()

    def defineStyle(self):
        """ Defines the initial style for the widget """
        #----------------------------------------------------------------------
        # Write your own code here to set the initial style of your widget
        #
        self.updateStyle()

    def sizeHint(self):
        return < _SuperClass_ > .sizeHint(self)

    def minimumSizeHint(self):
        return < _SuperClass_ > .minimumSizeHint(self)

    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    # TauBaseWidget over writing
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

    def getModelClass(self):
        #----------------------------------------------------------------------
        # [MANDATORY]
        # Replace your own code here
        # ex.: return tau.core.Attribute
        raise RuntimeError("Forgot to overwrite %s.getModelClass" % str(self))

    def attach(self):
        """Attaches the widget to the model"""

        if self.isAttached():
            return True

        #----------------------------------------------------------------------
        # Write your own code here before attaching widget to attribute connect
        # the proper signal so that the first event is correctly received by the
        # widget
        #
        # Typical code is:
        # self.connect(self, QtCore.SIGNAL('valueChangedDueToEvent(QString)'),
        #             self.setTextValue)

        ret = TauBaseWidget.attach(self)

        # by default enable/disable widget according to attach state
        self.setEnabled(ret)
        return ret

    def detach(self):
        """Detaches the widget from the model"""

        TauBaseWidget.detach(self)

        #----------------------------------------------------------------------
        # Write your own code here after detaching the widget from the model
        #
        # Typical code is:
        # self.emit(QtCore.SIGNAL('valueChangedDueToEvent(QString)'),
        #          QtCore.QString(value_str))
        # self.disconnect(self, QtCore.SIGNAL('valueChangedDueToEvent(QString)'),
        #                self.setTextValue)

        # by default disable widget when dettached
        self.setEnabled(False)

    def eventReceived(self, src, type, data):
        """ eventReceived(src, TauEventType type, data) -> None

            Called by the model when an event is fired.

            Parameters:
              src: Source of the event. Usually a tau.core.Attribute or
                   tau.core.Device object
              type: a TauEventType describing the type of event.
              data: A PyTango object with the event data. It can be None.
                    - For TauEventType.change events is a PyTango.AttributeValue
                    object or None;
                    - For TauEventType.attr_conf events is a
                    PyTango.AttrConfEventData object or None.
            Return:
              None
        """
        #----------------------------------------------------------------------
        # Write your own code here to handle event
        pass

    #-------------------------------------------------------------------------
    # [MANDATORY]
    # Uncomment the following method if your superclass does not provide with a
    # isReadOnly() method or if you need to change its behavior

    # def isReadOnly(self):
    #    return True

    def updateStyle(self):
        #----------------------------------------------------------------------
        # Write your own code here to update your widget style

        # send a repaint in the end
        self.repaint()

    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    # QT properties
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

    model = QtCore.pyqtProperty("QString", TauBaseWidget.getModel,
                                TauBaseWidget.setModel,
                                TauBaseWidget.resetModel)

    useParentModel = QtCore.pyqtProperty("bool",
                                         TauBaseWidget.getUseParentModel,
                                         TauBaseWidget.setUseParentModel,
                                         TauBaseWidget.resetUseParentModel)

    #-------------------------------------------------------------------------
    # Write your own code here for your own widget properties
