Skip to content

Commit

Permalink
SpreadsheetUI : Use ContextTracker
Browse files Browse the repository at this point in the history
This lets us highlight the currently active row.
  • Loading branch information
johnhaddon committed Aug 6, 2024
1 parent d6d217c commit 5aee1f1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Improvements
- Improved highlighting of active nodes, with more accurate tracking of Loop node iterations.
- Annotation `{plug}` substitutions are now evaluated in a context determined relative to the focus node.
- The strike-through for disabled nodes is now evaluated in a context determined relative to the focus node.
- Spreadsheet : Added yellow underlining to the currently active row.

Fixes
-----
Expand Down
6 changes: 6 additions & 0 deletions python/GafferUI/SpreadsheetUI/_PlugTableDelegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ def paint( self, painter, option, index ) :
focusColour.setAlpha( 30 )
painter.fillRect( option.rect, focusColour )

if index.data( _PlugTableModel.ActiveRole ) :
pen = QtGui.QPen( QtGui.QColor( 240, 220, 40, 150 ) )
pen.setWidth( 2 )
painter.setPen( pen )
painter.drawLine( option.rect.bottomLeft(), option.rect.bottomRight() )

if enabled and cellPlugEnabled :
return

Expand Down
48 changes: 40 additions & 8 deletions python/GafferUI/SpreadsheetUI/_PlugTableModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import GafferUI

from Qt import QtCore
from Qt import QtGui

from . import _Formatting

Expand All @@ -51,13 +52,13 @@
class _PlugTableModel( QtCore.QAbstractTableModel ) :

CellPlugEnabledRole = QtCore.Qt.UserRole
ActiveRole = CellPlugEnabledRole + 1

def __init__( self, rowsPlug, context, parent = None ) :
def __init__( self, rowsPlug, parent = None ) :

QtCore.QAbstractTableModel.__init__( self, parent )

self.__rowsPlug = rowsPlug
self.__context = context

self.__plugDirtiedConnection = rowsPlug.node().plugDirtiedSignal().connect( Gaffer.WeakMethod( self.__plugDirtied ), scoped = True )
self.__rowAddedConnection = rowsPlug.childAddedSignal().connect( Gaffer.WeakMethod( self.__rowAdded ), scoped = True )
Expand All @@ -66,7 +67,15 @@ def __init__( self, rowsPlug, context, parent = None ) :
self.__columnAddedConnection = rowsPlug.defaultRow()["cells"].childAddedSignal().connect( Gaffer.WeakMethod( self.__columnAdded ), scoped = True )
self.__columnRemovedConnection = rowsPlug.defaultRow()["cells"].childRemovedSignal().connect( Gaffer.WeakMethod( self.__columnRemoved ), scoped = True )
self.__plugMetadataChangedConnection = Gaffer.Metadata.plugValueChangedSignal( rowsPlug.node() ).connect( Gaffer.WeakMethod( self.__plugMetadataChanged ), scoped = True )
self.__contextChangedConnection = self.__context.changedSignal().connect( Gaffer.WeakMethod( self.__contextChanged ), scoped = True )

self.__contextTracker = GafferUI.ContextTracker.acquireForFocus( rowsPlug.node().scriptNode() )
self.__contextTrackerChangedConnection = self.__contextTracker.changedSignal().connect( Gaffer.WeakMethod( self.__contextTrackerChanged ), scoped = True )
self.__context = None
self.__contextTrackerChanged( self.__contextTracker )

self.__spreadsheet = Gaffer.PlugAlgo.findDestination(
self.__rowsPlug, lambda plug : plug.node() if isinstance( plug.node(), Gaffer.Spreadsheet ) else None
)

# Methods of our own
# ------------------
Expand Down Expand Up @@ -270,6 +279,29 @@ def data( self, index, role ) :
return None
return enabled

elif role == self.ActiveRole :

if self.__spreadsheet is not None :
with self.__context :
if index.row() == 0 :
# We don't show active state for the default row because
# our context isn't watertight, and doesn't track things
# like `scene:path`, so the default being active may
# commonly be a false positive. In this case we think
# the best compromise is to show an ambiguous state.
return False
elif index.column() < 2 :
return index.row() == self.__spreadsheet["activeRowIndex"].getValue()
elif index.row() > 0 :
activeInPlug = self.__spreadsheet.activeInPlug(
self.__spreadsheet["out"][index.column() - 2]
)
valuePlug = self.valuePlugForIndex( index )
return Gaffer.PlugAlgo.findSource(
activeInPlug,
lambda plug : plug == valuePlug
)

return None

def setData( self, index, value, role ) :
Expand Down Expand Up @@ -361,12 +393,12 @@ def __plugMetadataChanged( self, plug, key, reason ) :
# when they change. Emitting `dataChanged` is enough to kick a redraw off.
self.dataChanged.emit( index, index )

def __contextChanged( self, context, key ) :

if key.startswith( "ui:" ) :
return
def __contextTrackerChanged( self, contextTracker ) :

self.__emitModelReset()
context = self.__contextTracker.context( self.rowsPlug() )
if self.__context is None or self.__context.hash() != context.hash() :
self.__context = context
self.__emitModelReset()

def __emitModelReset( self ) :

Expand Down
2 changes: 1 addition & 1 deletion python/GafferUI/SpreadsheetUI/_RowsPlugValueWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__( self, plug ) :

GafferUI.PlugValueWidget.__init__( self, self.__grid, plug )

model = _PlugTableModel( plug, self.context(), self._qtWidget() )
model = _PlugTableModel( plug, self._qtWidget() )
selectionModel = QtCore.QItemSelectionModel( model, self._qtWidget() )

with self.__grid :
Expand Down

0 comments on commit 5aee1f1

Please sign in to comment.