-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy path_RowsPlugValueWidget.py
executable file
·519 lines (399 loc) · 18.7 KB
/
_RowsPlugValueWidget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
##########################################################################
#
# Copyright (c) 2019, Image Engine Design Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with
# the distribution.
#
# * Neither the name of John Haddon nor the names of
# any other contributors to this software may be used to endorse or
# promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################
import functools
import traceback
import imath
import IECore
import Gaffer
import GafferUI
from Qt import QtCore
from Qt import QtWidgets
from . import _Algo
from ._LinkedScrollBar import _LinkedScrollBar
from ._PlugTableModel import _PlugTableModel
from ._PlugTableView import _PlugTableView
from ._SectionChooser import _SectionChooser
# _RowsPlugValueWidget
# ====================
#
# This is the main top-level widget that forms the spreadsheet UI.
class _RowsPlugValueWidget( GafferUI.PlugValueWidget ) :
def __init__( self, plug ) :
self.__grid = GafferUI.GridContainer( spacing = 4 )
GafferUI.PlugValueWidget.__init__( self, self.__grid, plug )
model = _PlugTableModel( plug, self._qtWidget() )
selectionModel = QtCore.QItemSelectionModel( model, self._qtWidget() )
with self.__grid :
self.__sectionChooser = _SectionChooser(
plug,
parenting = {
"index" : ( 1, 0 ),
}
)
self.__sectionChooser.currentSectionChangedSignal().connect( Gaffer.WeakMethod( self.__currentSectionChanged ) )
with GafferUI.ListContainer(
GafferUI.ListContainer.Orientation.Horizontal,
spacing = 4,
parenting = {
"index" : ( 0, 1 ),
# Set the horizontal alignment to None_ so the ListContainer continues to occupy the
# entire width while we set the vertical alignment.
"alignment" : ( GafferUI.HorizontalAlignment.None_, GafferUI.VerticalAlignment.Bottom ),
}
) :
self.__toggleFilterButton = GafferUI.Button( image = "search.png", hasFrame = False )
self.__toggleFilterButton.clickedSignal().connect( Gaffer.WeakMethod( self.__toggleFilterButtonClicked ) )
self.__patternWidget = GafferUI.TextWidget( toolTip = "Row filter pattern" )
self.__patternWidget.setText( Gaffer.Metadata.value( plug, "spreadsheet:rowFilter" ) )
self.__patternWidget.setPlaceholderText( "Filter..." )
# Ignore the width in X so that the widget is sized based on the width dictated by `rowNamesTable`.
self.__patternWidget._qtWidget().setSizePolicy( QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Fixed )
self.__patternWidget.editingFinishedSignal().connect( Gaffer.WeakMethod( self.__patternEditingFinished ) )
self.__refreshFilterButton = GafferUI.Button( image = "refresh.png", hasFrame = False, toolTip = "Click to refresh row filter" )
self.__refreshFilterButton.clickedSignal().connect( Gaffer.WeakMethod( self.__refreshFilterButtonClicked ) )
self.__defaultLabel = GafferUI.Label( "<h4>Default</h4>", horizontalAlignment = GafferUI.HorizontalAlignment.Right )
self.__defaultLabel._qtWidget().setObjectName( "gafferDefaultRowLabel" )
# Ignore the width in X so that the label is sized based on the width dictated by `rowNamesTable`.
self.__defaultLabel._qtWidget().setSizePolicy( QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Fixed )
GafferUI.Spacer( imath.V2i( 1, 20 ), maximumSize = imath.V2i( 1, 20 ) )
self.__defaultTable = _PlugTableView(
selectionModel, _PlugTableView.Mode.Defaults,
parenting = {
"index" : ( 1, 1 ),
}
)
with GafferUI.ListContainer(
parenting = {
"index" : ( 2, 1 ),
"alignment" : ( GafferUI.HorizontalAlignment.Left, GafferUI.VerticalAlignment.Top )
},
) :
GafferUI.Spacer( imath.V2i( 1, 4 ), maximumSize = imath.V2i( 1, 4 ) )
self.__addColumnButton = GafferUI.MenuButton(
image="plus.png", hasFrame=False, toolTip = "Click to add column, or drop plug to connect",
menu = GafferUI.Menu( Gaffer.WeakMethod( self.__addColumnMenuDefinition ) )
)
self.__addColumnButton.dragEnterSignal().connect( Gaffer.WeakMethod( self.__addColumnButtonDragEnter ) )
self.__addColumnButton.dragLeaveSignal().connect( Gaffer.WeakMethod( self.__addColumnButtonDragLeave ) )
self.__addColumnButton.dropSignal().connect( Gaffer.WeakMethod( self.__addColumnButtonDrop ) )
self.__rowNamesTable = _PlugTableView(
selectionModel, _PlugTableView.Mode.RowNames,
parenting = {
"index" : ( 0, 2 ),
}
)
self.__cellsTable = _PlugTableView(
selectionModel, _PlugTableView.Mode.Cells,
parenting = {
"index" : ( 1, 2 ),
}
)
# Reverse the row colour order so the colour of the first row of the
# rowNamesTable and cellsTable differ from the Default row's colour.
self.__rowNamesTable._qtWidget().setProperty( "gafferReverseRowColors", True )
self.__cellsTable._qtWidget().setProperty( "gafferReverseRowColors", True )
_LinkedScrollBar(
GafferUI.ListContainer.Orientation.Vertical, [ self.__cellsTable, self.__rowNamesTable ],
parenting = {
"index" : ( 2, 2 ),
"alignment" : ( GafferUI.HorizontalAlignment.Left, GafferUI.VerticalAlignment.None_ )
}
)
with GafferUI.ListContainer(
spacing = 4,
parenting = {
"index" : ( slice( 1, 3 ), 3 ),
}
) :
_LinkedScrollBar(
GafferUI.ListContainer.Orientation.Horizontal, [ self.__cellsTable, self.__defaultTable ],
)
self.__statusLabel = GafferUI.Label( "" )
# The status label occupies the same column as `cellsTable`, and has a dynamic width based on the length
# of the status text. Ignore the width in X so that the column width is dictated solely by `cellsTable`,
# otherwise large status labels can force cells off the screen.
self.__statusLabel._qtWidget().setSizePolicy( QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Fixed )
self.__addRowButton = GafferUI.Button(
image="plus.png", hasFrame=False, toolTip = "Click to add row, or drop new row names",
parenting = {
"index" : ( 0, 3 ),
"alignment" : ( GafferUI.HorizontalAlignment.Left, GafferUI.VerticalAlignment.Top ),
}
)
self.__addRowButton.clickedSignal().connect( Gaffer.WeakMethod( self.__addRowButtonClicked ) )
self.__addRowButton.dragEnterSignal().connect( Gaffer.WeakMethod( self.__addRowButtonDragEnter ) )
self.__addRowButton.dragLeaveSignal().connect( Gaffer.WeakMethod( self.__addRowButtonDragLeave ) )
self.__addRowButton.dropSignal().connect( Gaffer.WeakMethod( self.__addRowButtonDrop ) )
if isinstance( plug.node(), Gaffer.Reference ) :
# Currently we only allow new rows to be added to references
# that had no rows when they were exported. We don't want to
# get into merge hell trying to combine user-added and referenced
# rows, especially given the row-reordering feature.
for row in plug.children()[1:] :
if not plug.node().isChildEdit( row ) :
self.__addRowButton.setVisible( False )
break
# Likewise, we don't support the addition of new columns at all.
self.__addColumnButton.setVisible( False )
if Gaffer.Metadata.value( plug, "spreadsheet:columnsNeedSerialisation" ) == False :
# This metadata is set by custom nodes which create their
# columns in a constructor. If users were to add their own
# columns, they wouldn't be serialised correctly, so we hide the
# button.
self.__addColumnButton.setVisible( False )
for widget in [ self.__addRowButton, self.__addColumnButton, self.__patternWidget, self.__refreshFilterButton, self.__toggleFilterButton ] :
widget.enterSignal().connect( Gaffer.WeakMethod( self.__enterToolTippedWidget ) )
widget.leaveSignal().connect( Gaffer.WeakMethod( self.__leaveToolTippedWidget ) )
for widget in [ self.__defaultTable, self.__cellsTable ] :
widget.mouseMoveSignal().connect( Gaffer.WeakMethod( self.__cellsMouseMove ) )
widget.leaveSignal().connect( Gaffer.WeakMethod( self.__cellsLeave ) )
Gaffer.Metadata.plugValueChangedSignal( plug.node() ).connect( Gaffer.WeakMethod( self.__plugMetadataChanged ) )
self.__rowFilterEnabled = Gaffer.Metadata.value( plug, "spreadsheet:rowFilterEnabled" )
self.__updateVisibleSections()
self.__updateDefaultRowVisibility()
self.__updateRowFilterWidgets()
self.__applyRowFilter()
def hasLabel( self ) :
return True
def getToolTip( self ) :
# The generic auto-generated PlugValueWidget tooltip is distracting
# rather than useful, so we disable it. Calling `Widget.getToolTip()`
# means we continue to support tooltips added explicitly with
# `setToolTip()`.
return GafferUI.Widget.getToolTip( self )
__addRowButtonMenuSignal = None
## This signal is emitted whenever the add row button is clicked.
# If the resulting menu definition has been populated with items,
# a popup menu will be presented from the button.
# If only a single item is present, its command will be called
# immediately instead of presenting a menu.
# If no items are present, then the default behaviour is to
# add a single row to the end of the spreadsheet.
# The signal is called with the corresponding spreadsheet rows plug
# value widget.
@classmethod
def addRowButtonMenuSignal( cls ) :
if cls.__addRowButtonMenuSignal is None :
cls.__addRowButtonMenuSignal = _AddButtonMenuSignal()
return cls.__addRowButtonMenuSignal
__addColumnButtonMenuSignal = None
@classmethod
def addColumnButtonMenuSignal( cls ) :
if cls.__addColumnButtonMenuSignal is None :
cls.__addColumnButtonMenuSignal = _AddButtonMenuSignal()
return cls.__addColumnButtonMenuSignal
def _updateFromEditable( self ) :
# Not using `_editable()` as it considers the whole plug to be non-editable if
# any child has an input connection, but that shouldn't prevent us adding new
# rows or columns.
editable = self.getPlug().getInput() is None and not Gaffer.MetadataAlgo.readOnly( self.getPlug() )
self.__addRowButton.setEnabled( editable )
self.__addColumnButton.setEnabled( editable )
def __addRowButtonClicked( self, *unused ) :
menuDefinition = IECore.MenuDefinition()
self.addRowButtonMenuSignal()( menuDefinition, self )
if menuDefinition.size() == 0 :
with Gaffer.UndoScope( self.getPlug().ancestor( Gaffer.ScriptNode ) ) :
row = self.getPlug().addRow()
# Select new row for editing. Have to do this on idle as otherwise it doesn't scroll
# right to the bottom.
GafferUI.EventLoop.addIdleCallback( functools.partial( self.__rowNamesTable.editPlugs, [ row["name"] ] ) )
elif menuDefinition.size() == 1 :
_, item = menuDefinition.items()[0]
item.command()
else :
self.__popupMenu = GafferUI.Menu( menuDefinition )
self.__popupMenu.popup( parent = self )
def __addRowButtonDragEnter( self, addButton, event ) :
if isinstance( event.data, ( IECore.StringData, IECore.StringVectorData ) ) :
addButton.setHighlighted( True )
return True
return False
def __addRowButtonDragLeave( self, addButton, event ) :
addButton.setHighlighted( False )
return True
def __addRowButtonDrop( self, addButton, event ) :
addButton.setHighlighted( False )
with Gaffer.UndoScope( self.getPlug().ancestor( Gaffer.ScriptNode ) ) :
strings = event.data if isinstance( event.data, IECore.StringVectorData ) else [ event.data.value ]
for s in strings :
self.getPlug().addRow()["name"].setValue( s )
return True
def __addColumnMenuDefinition( self ) :
menuDefinition = IECore.MenuDefinition()
## \todo Centralise a standard mechanism for building plug
# creation menus. We have similar code in UserPlugs, CompoundDataPlugValueWidget,
# UIEditor, ShaderTweaksUI etc.
for label, plugType in [
( "Bool", Gaffer.BoolPlug ),
( "Int", Gaffer.IntPlug ),
( "Float", Gaffer.FloatPlug ),
( "NumericDivider", None ),
( "String", Gaffer.StringPlug ),
( "StringDivider", None ),
( "V2i", Gaffer.V2iPlug ),
( "V3i", Gaffer.V3iPlug ),
( "V2f", Gaffer.V2fPlug ),
( "V3f", Gaffer.V3fPlug ),
( "VectorDivider", None ),
( "Color3f", Gaffer.Color3fPlug ),
( "Color4f", Gaffer.Color4fPlug ),
] :
if plugType is None :
menuDefinition.append( label, { "divider" : True } )
else :
menuDefinition.append(
label, { "command" : functools.partial( Gaffer.WeakMethod( self.__addColumn ), plugType = plugType ), "active" : True }
)
self.addColumnButtonMenuSignal()( menuDefinition, self )
return menuDefinition
def __addColumn( self, menu, plugType ) :
d = GafferUI.TextInputDialogue( initialText = "column", title = "Enter name", confirmLabel = "Add Column" )
name = d.waitForText( parentWindow = menu.ancestor( GafferUI.Window ) )
if not name :
return
plug = plugType( name )
with Gaffer.UndoScope( self.getPlug().ancestor( Gaffer.ScriptNode ) ) :
columnIndex = self.getPlug().addColumn( plug )
if self.__sectionChooser.currentSection() :
self.__sectionChooser.setSection(
self.getPlug().defaultRow()["cells"][columnIndex],
self.__sectionChooser.currentSection()
)
def __addColumnButtonDragEnter( self, addButton, event ) :
if not isinstance( event.data, Gaffer.ValuePlug ) or event.data.getInput() is not None :
return False
if not isinstance( self.getPlug().node(), Gaffer.Spreadsheet ) :
# Dropping plugs involves making an output connection from
# the spreadsheet, which we don't want to do for a promoted
# plug.
return False
addButton.setHighlighted( True )
return True
def __addColumnButtonDragLeave( self, addButton, event ) :
addButton.setHighlighted( False )
return True
def __addColumnButtonDrop( self, addButton, event ) :
addButton.setHighlighted( False )
_Algo.addToSpreadsheet( event.data, self.getPlug().node(), self.__sectionChooser.currentSection() )
return True
def __enterToolTippedWidget( self, widget ) :
self.__statusLabel.setText( widget.getToolTip() )
def __leaveToolTippedWidget( self, widget ) :
self.__statusLabel.setText( "" )
def __cellsMouseMove( self, widget, event ) :
status = ""
plug = widget.plugAt( event.line.p0 )
if plug is not None :
rowPlug = plug.ancestor( Gaffer.Spreadsheet.RowPlug )
if rowPlug == rowPlug.parent().defaultRow() :
rowName = "Default"
else :
with self.context() :
rowName = rowPlug["name"].getValue() or "unnamed"
columnPlug = self.getPlug().defaultRow()["cells"][plug.getName()]
columnName = Gaffer.Metadata.value( columnPlug, "spreadsheet:columnLabel" )
if not columnName :
columnName = IECore.CamelCase.toSpaced( columnPlug.getName() )
status = "Row : {}, Column : {}".format(
rowName,
columnName
)
self.__statusLabel.setText( status )
def __cellsLeave( self, widget ) :
self.__statusLabel.setText( "" )
def __updateDefaultRowVisibility( self ) :
visible = Gaffer.Metadata.value( self.getPlug(), "spreadsheet:defaultRowVisible" )
if visible is None :
visible = True
self.__defaultLabel.setVisible( visible and not self.__rowFilterEnabled )
self.__defaultTable.setRowFilter( "" if visible else "__hideDefaultRow__" )
def __plugMetadataChanged( self, plug, key, reason ) :
if plug == self.getPlug() and key == "spreadsheet:defaultRowVisible" :
self.__updateDefaultRowVisibility()
def __currentSectionChanged( self, tabBar ) :
self.__updateVisibleSections()
def __updateVisibleSections( self ) :
section = self.__sectionChooser.currentSection()
self.__defaultTable.setVisibleSection( section )
self.__cellsTable.setVisibleSection( section )
def __updateRowFilterWidgets( self ) :
self.__patternWidget.setVisible( self.__rowFilterEnabled )
self.__refreshFilterButton.setVisible( self.__rowFilterEnabled )
self.__toggleFilterButton.setImage( "searchOn.png" if self.__rowFilterEnabled else "search.png" )
self.__toggleFilterButton.setToolTip( "Click to disable row filter" if self.__rowFilterEnabled else "Click to enable row filter" )
def __toggleFilterButtonClicked( self, *unused ) :
self.__rowFilterEnabled = not self.__rowFilterEnabled
Gaffer.Metadata.registerValue( self.getPlug(), "spreadsheet:rowFilterEnabled", self.__rowFilterEnabled, persistent = False )
self.__updateDefaultRowVisibility()
self.__updateRowFilterWidgets()
self.__applyRowFilter()
def __refreshFilterButtonClicked( self, *unused ) :
self.__applyRowFilter()
def __patternEditingFinished( self, textWidget ) :
pattern = self.__patternWidget.getText()
# Remember the user's filter pattern so we can apply
# it next time we construct a widget for this plug.
Gaffer.Metadata.registerValue( self.getPlug(), "spreadsheet:rowFilter", pattern, persistent = False )
self.__applyRowFilter()
def __applyRowFilter( self ) :
pattern = ""
if self.__rowFilterEnabled :
pattern = self.__patternWidget.getText()
pattern = " ".join( [ x if IECore.StringAlgo.hasWildcards( x ) else f"*{x}*" for x in pattern.split() ] )
self.__rowNamesTable.setRowFilter( pattern )
self.__cellsTable.setRowFilter( pattern )
GafferUI.PlugValueWidget.registerType( Gaffer.Spreadsheet.RowsPlug, _RowsPlugValueWidget )
# Signal with custom result combiner to prevent bad
# slots blocking the execution of others.
class _AddButtonMenuSignal( Gaffer.Signals.Signal2 ) :
def __init__( self ) :
Gaffer.Signals.Signal2.__init__( self, self.__combiner )
@staticmethod
def __combiner( results ) :
while True :
try :
next( results )
except StopIteration :
return
except Exception as e :
# Print message but continue to execute other slots
IECore.msg(
IECore.Msg.Level.Error,
"Spreadsheet Add Button menu", traceback.format_exc()
)
# Remove circular references that would keep the widget in limbo.
e.__traceback__ = None