View

As with the document, the view is still a relatively uncomplicated object. It still knows which document it belongs to. I have added close(self) to make it easier to follow the execution flow when closing a window. What is more important, however, is the closeEvent() function — this is subclassed to completely override QWidget's default functionality. Close events are handled by the application itself, not by the view. The view doesn't know anything about the document manager, which ultimately handles the close event for all views.

Example 15-4. The view class

"""
mdiview.py — view component

copyright: (C) 2001, Boudewijn Rempt
email:     boud@rempt.xs4all.nl
"""
from qt import *

class MDIView(QWidget):
    """
    The MDIView class can represent object of class
    MDIDoc on screen.

    slots:
          slotDocModified
    """
    def __init__(self, parent, doc, *args):
        apply(QWidget.__init__,(self, parent) + args)
        self.doc = doc
        self.connect(self.doc, PYSIGNAL("sigDocModified"),
                     self.slotDocModified)
        self.connect(self.doc, PYSIGNAL("sigDocTitleChanged"),
                     self.setCaption)
        # Set initial values
        self.slotDocModified(self.doc.modified())

    def slotDocModified(self, value):
        if value:
            self.setBackgroundColor(QColor("red"))
        else:
            self.setBackgroundColor(QColor("green"))

    def mouseDoubleClickEvent(self, ev):
        self.doc.slotModify() # direct call to the document

    def document(self):
        return self.doc

    def closeEvent(self, e):
        pass

    def close(self, destroy=0):
        return QWidget.close(self, destroy)