~popey/+junk/usd

« back to all changes in this revision

Viewing changes to USD/pxr/usdImaging/lib/usdviewq/watchWindow.py

  • Committer: Alan Pope
  • Date: 2016-09-29 12:05:28 UTC
  • Revision ID: alan@popey.com-20160929120528-32j3uk1x0dgaorip
Initial attempt to snap

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#
 
24
from PySide import QtGui, QtCore
 
25
from watchWindowUI import Ui_WatchWindow
 
26
import re
 
27
import os
 
28
 
 
29
# XXX USD WATCH WINDOW DEACTIVATED
 
30
 
 
31
separatorColor = QtGui.QColor(255, 255, 255)
 
32
separatorString = "\n==================================\n"
 
33
 
 
34
class WatchWindow(QtGui.QDialog):
 
35
    def __init__(self, parent):
 
36
        QtGui.QDialog.__init__(self,parent)
 
37
        self._ui = Ui_WatchWindow()
 
38
        self._ui.setupUi(self)
 
39
 
 
40
        self._boxWithFocus = self._ui.unvaryingEdit
 
41
        self._prevDualScrollVal = 0
 
42
 
 
43
        self._parent = parent
 
44
        self._searchString = ""
 
45
 
 
46
        self.addAction(self._ui.actionFind)
 
47
        self.addAction(self._ui.actionFindNext)
 
48
        self.addAction(self._ui.actionFindPrevious)
 
49
 
 
50
        QtCore.QObject.connect(self._ui.doneButton, 
 
51
                               QtCore.SIGNAL('clicked()'),
 
52
                               self.accept)
 
53
        QtCore.QObject.connect(self,
 
54
                               QtCore.SIGNAL('finished(int)'),
 
55
                               self._cleanUpAndClose)
 
56
        QtCore.QObject.connect(self._ui.dualScroller,
 
57
                               QtCore.SIGNAL('valueChanged(int)'),
 
58
                               self._changeBothSliders)
 
59
        QtCore.QObject.connect(self._ui.diffButton,
 
60
                               QtCore.SIGNAL('clicked()'),
 
61
                               self._diff)
 
62
        QtCore.QObject.connect(self._ui.actionFind,
 
63
                               QtCore.SIGNAL('triggered()'),
 
64
                               self._find)
 
65
        QtCore.QObject.connect(self._ui.actionFindNext,
 
66
                               QtCore.SIGNAL('triggered()'),
 
67
                               self._findNext)
 
68
        QtCore.QObject.connect(self._ui.actionFindPrevious,
 
69
                               QtCore.SIGNAL('triggered()'),
 
70
                               self._findPrevious)
 
71
        QtCore.QObject.connect(self._ui.varyingEdit,
 
72
                               QtCore.SIGNAL('cursorPositionChanged()'),
 
73
                               self._varyingCursorChanged)
 
74
        QtCore.QObject.connect(self._ui.unvaryingEdit,
 
75
                               QtCore.SIGNAL('cursorPositionChanged()'),
 
76
                               self._unvaryingCursorChanged)
 
77
        # save splitter state
 
78
        QtCore.QObject.connect(self._ui.splitter,
 
79
                               QtCore.SIGNAL('splitterMoved(int, int)'),
 
80
                               self._splitterMoved)
 
81
 
 
82
        #create a timer for saving splitter state only when it stops moving
 
83
        self._splitterTimer = QtCore.QTimer(self)
 
84
        self._splitterTimer.setInterval(500)
 
85
        QtCore.QObject.connect(self._splitterTimer, QtCore.SIGNAL('timeout()'),
 
86
                               self._saveSplitterState)
 
87
 
 
88
        self._resetSettings()
 
89
 
 
90
    def _splitterMoved(self, pos, index):
 
91
        # reset the timer every time the splitter moves
 
92
        # when the splitter stop moving for half a second, save state
 
93
        self._splitterTimer.stop()
 
94
        self._splitterTimer.start()
 
95
 
 
96
    def _saveSplitterState(self):
 
97
        self._parent._settings.setAndSave(
 
98
                watchWindowSplitter = self._ui.splitter.saveState())
 
99
        self._splitterTimer.stop()
 
100
 
 
101
    def _varyingCursorChanged(self):
 
102
        self._boxWithFocus = self._ui.varyingEdit
 
103
 
 
104
    def _unvaryingCursorChanged(self):
 
105
        self._boxWithFocus = self._ui.unvaryingEdit
 
106
 
 
107
    def _find(self):
 
108
        searchString = QtGui.QInputDialog.getText(self, "Find", 
 
109
            "Enter search string\nUse Ctrl+G to \"Find Next\"\n" + \
 
110
            "Use Ctrl+Shift+G to \"Find Previous\"")
 
111
        if searchString[1]:
 
112
            self._searchString = searchString[0]
 
113
            if (not self._boxWithFocus.find(self._searchString)):
 
114
                self._boxWithFocus.moveCursor(QtGui.QTextCursor.Start)
 
115
                self._boxWithFocus.find(self._searchString) 
 
116
 
 
117
    def _findNext(self):
 
118
        if (self._searchString == ""):
 
119
            return
 
120
        if (not self._boxWithFocus.find(self._searchString)):
 
121
            self._boxWithFocus.moveCursor(QtGui.QTextCursor.Start)
 
122
            self._boxWithFocus.find(self._searchString)
 
123
 
 
124
    def _findPrevious(self):
 
125
        if (self._searchString == ""):
 
126
            return
 
127
        if (not self._boxWithFocus.find(self._searchString,
 
128
            QtGui.QTextDocument.FindBackward)):
 
129
            self._boxWithFocus.moveCursor(QtGui.QTextCursor.End)
 
130
            self._boxWithFocus.find(self._searchString, 
 
131
                                      QtGui.QTextDocument.FindBackward)
 
132
 
 
133
    def _diff(self):
 
134
        import tempfile
 
135
        unvarFile = tempfile.NamedTemporaryFile()
 
136
        unvarFile.write(str(self._ui.unvaryingEdit.toPlainText()))
 
137
        unvarFile.flush()
 
138
        varFile = tempfile.NamedTemporaryFile()
 
139
        varFile.write(str(self._ui.varyingEdit.toPlainText()))
 
140
        varFile.flush()
 
141
 
 
142
        os.system("xxdiff %s %s" % (unvarFile.name, varFile.name))
 
143
                          
 
144
        unvarFile.close()
 
145
        varFile.close()
 
146
 
 
147
    def _changeBothSliders(self, val):
 
148
        delta = val - self._prevDualScrollVal
 
149
        self._ui.varyingEdit.verticalScrollBar().setValue(
 
150
            self._ui.varyingEdit.verticalScrollBar().value() + delta)
 
151
        self._ui.unvaryingEdit.verticalScrollBar().setValue(
 
152
            self._ui.unvaryingEdit.verticalScrollBar().value() + delta)
 
153
        self._prevDualScrollVal = val
 
154
 
 
155
    def appendContents(self, s, color = QtGui.QColor(0,0,0)):
 
156
        self._ui.varyingEdit.setTextColor(color)
 
157
        self._ui.varyingEdit.append(s)
 
158
 
 
159
        self._ui.dualScroller.setMaximum(max(
 
160
            self._ui.unvaryingEdit.verticalScrollBar().maximum(),
 
161
            self._ui.varyingEdit.verticalScrollBar().maximum()))
 
162
        self._ui.dualScroller.setPageStep(min(
 
163
            self._ui.unvaryingEdit.verticalScrollBar().pageStep(),
 
164
            self._ui.varyingEdit.verticalScrollBar().pageStep()))
 
165
 
 
166
        self._boxWithFocus = self._ui.varyingEdit
 
167
 
 
168
    def appendUnvaryingContents(self, s, color = QtGui.QColor(0,0,0)):
 
169
        self._ui.unvaryingEdit.setTextColor(color)
 
170
        self._ui.unvaryingEdit.append(s)
 
171
 
 
172
        self._ui.dualScroller.setMaximum(max(
 
173
            self._ui.unvaryingEdit.verticalScrollBar().maximum(), 
 
174
            self._ui.varyingEdit.verticalScrollBar().maximum()))
 
175
        self._ui.dualScroller.setPageStep(min(
 
176
            self._ui.unvaryingEdit.verticalScrollBar().pageStep(),
 
177
            self._ui.varyingEdit.verticalScrollBar().pageStep()))
 
178
 
 
179
        self._boxWithFocus = self._ui.unvaryingEdit
 
180
 
 
181
    def appendSeparator(self):
 
182
        self.appendContents(separatorString, separatorColor)
 
183
        self.appendUnvaryingContents(separatorString, separatorColor)
 
184
 
 
185
    def clearContents(self):
 
186
        self._ui.varyingEdit.setText("")
 
187
        self._ui.unvaryingEdit.setText("")
 
188
 
 
189
    def _cleanUpAndClose(self, result):
 
190
        self._parent._ui.actionWatch_Window.setChecked(False)
 
191
 
 
192
    def _resetSettings(self):
 
193
        # restore splitter position
 
194
        splitterpos = self._parent._settings.get('watchWindowSplitter')
 
195
        if not splitterpos is None:
 
196
            self._ui.splitter.restoreState(splitterpos)
 
197