1#
2# Copyright 2016 Pixar
3#
4# Licensed under the Apache License, Version 2.0 (the "Apache License")
5# with the following modification; you may not use this file except in
6# compliance with the Apache License and the following modification to it:
7# Section 6. Trademarks. is deleted and replaced with:
8#
9# 6. Trademarks. This License does not grant permission to use the trade
10#    names, trademarks, service marks, or product names of the Licensor
11#    and its affiliates, except as required to comply with Section 4(c) of
12#    the License and to reproduce the content of the NOTICE file.
13#
14# You may obtain a copy of the Apache License at
15#
16#     http://www.apache.org/licenses/LICENSE-2.0
17#
18# Unless required by applicable law or agreed to in writing, software
19# distributed under the Apache License with the above modification is
20# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21# KIND, either express or implied. See the Apache License for the specific
22# language governing permissions and limitations under the Apache License.
23#
24from pxr import Usd
25from .qt import QtCore, QtWidgets
26from .attributeValueEditorUI import Ui_AttributeValueEditor
27from .common import GetPropertyColor, UIPropertyValueSourceColors
28from .scalarTypes import ToString
29
30# This is the widget that appears when selecting an attribute and
31# opening the "Value" tab.
32
33class AttributeValueEditor(QtWidgets.QWidget):
34    editComplete = QtCore.Signal('QString')
35
36    def __init__(self, parent):
37        QtWidgets.QWidget.__init__(self, parent)
38        self._ui = Ui_AttributeValueEditor()
39        self._ui.setupUi(self)
40
41        self._defaultView = self._ui.valueViewer
42
43        from .arrayAttributeView import ArrayAttributeView
44        self._extraAttrViews = [
45                ArrayAttributeView(self),
46                ]
47
48        for attrView in self._extraAttrViews:
49            self._ui.stackedWidget.addWidget(attrView)
50
51        self.clear()
52
53    def setAppController(self, appController):
54        # pass the appController instance from which to retrieve
55        # variable data.
56        self._appController = appController
57
58    def populate(self, primPath, propName):
59        # called when the selected attribute has changed
60        self._primPath = primPath
61
62        try:
63            self._attribute = self._appController._propertiesDict[propName]
64        except KeyError:
65            self._attribute = None
66
67        self._isSet = True  # an attribute is selected
68
69        self.refresh()  # load the value at the current frame
70
71    def _FindView(self, attr):
72        # Early-out for CustomAttributes and Relationships
73        if not isinstance(attr, Usd.Attribute):
74            return None
75
76        for attrView in self._extraAttrViews:
77            if attrView.CanView(attr):
78                return attrView
79
80        return None
81
82    def refresh(self):
83        # usually called upon frame change or selected attribute change
84        if not self._isSet:
85            return
86
87        # attribute connections and relationship targets have no value to display
88        # in the value viewer.
89        if self._attribute is None:
90            return
91
92        # If the current attribute doesn't belong to the current prim, don't
93        # display its value.
94        if self._attribute.GetPrimPath() != self._primPath:
95            self._ui.valueViewer.setText("")
96            return
97
98        frame = self._appController._dataModel.currentFrame
99
100        # get the value of the attribute
101        if isinstance(self._attribute, Usd.Relationship):
102            self._val = self._attribute.GetTargets()
103        else: # Usd.Attribute or CustomAttribute
104            self._val = self._attribute.Get(frame)
105
106        whichView = self._FindView(self._attribute)
107        if whichView:
108            self._ui.stackedWidget.setCurrentWidget(whichView)
109            whichView.SetAttribute(self._attribute, frame)
110        else:
111            self._ui.stackedWidget.setCurrentWidget(self._defaultView)
112            txtColor = GetPropertyColor(self._attribute, frame)
113
114            # set text and color in the value viewer
115            self._ui.valueViewer.setTextColor(txtColor)
116
117            if isinstance(self._attribute, Usd.Relationship):
118                typeName = None
119            else:
120                typeName = self._attribute.GetTypeName()
121            rowText = ToString(self._val, typeName)
122            self._ui.valueViewer.setText(rowText)
123
124    def clear(self):
125        # set the value editor to 'no attribute selected' mode
126        self._isSet = False
127        self._ui.valueViewer.setText("")
128        # make sure we're showing the default view
129        self._ui.stackedWidget.setCurrentWidget(self._defaultView)
130