# Overview

MDV (Model-Delegate-View) is a pattern commonly used in `omni.ui` to implement
user interfaces with data-controlling logic. It highlights a separation between
the data and the display logic. This separation of concepts provides for better
maintenance of the code.

It closely follows the MVC pattern with a slightly different separation of
components. Unlike MVC, the View component of MDV takes responsibility of
Controller, takes control of the layout, and routes the Delegate component that
controls the look.

The three parts of the MDV software-design pattern can be described as follows:

 1. Model: The central component of the system. Manages data and logic. It
creates items that are used as pointers to the specific parts of the data
layer.

 2. Delegate: Creates widgets and defines the look.

 3. View: Handles layout, holds the widgets, and coordinates Model and Delegate.

## Model-Delegate-View in TreeView

TreeView is the MDV widget that takes advantage of the MDV pattern. For
simplicity, this document describes how the pattern applies to TreeView, and it
can be similarly applied to any MDV-based widget. The three parts are needed to
create a nice TreeView.

![](../data/mvd.png)

 1. Model: it's necessary to reimplement the class `ui.AbstractItemModel`.

 2. Delegate: it's necessary to reimplement the class `ui.AbstractItemDelegate`.

 3. View: it's necessary to provide the model and the delegate components
to `ui.TreeView`. There is no way to reimplement it.

## MDV pipeline in Stage Widget

To describe the MDV pipeline in production, let's focus on the "visibility"
button of Stage Widget. Stage Widget is a part of the extension `omni.kit.widget.stage`.
It uses a model that is watching the stage, the delegate that is written to
match the design document, and a standard TreeView that coordinates the model
and the delegate.

![](../data/mdv-stage-pipeline.png)

The main idea is that the model doesn't keep the data (visibility value). The
model is an API protocol that is the bridge between UsdStage and TreeView. When
the user changes the visibility, the model changes it in UsdStage, which
triggers UsdNotice. And if something is changed in UsdStage, UsdNotice makes the
model dirty. When TreeView recognizes the model is dirty, it calls the delegate
to recreate the specific piece of layout. And the delegate creates a new widget
and queries the model for visibility, and the model queries it from UsdStage.
