2 from pyfbsdk_additions
import *
4 from PySide2
import QtCore, QtGui, QtWidgets
5 import NamespaceUpgradeDialog
6 import DialogSwapRefFile
8 class NamespaceTableModel( QtCore.QAbstractTableModel ):
9 namespaceRenamed = QtCore.Signal( str, str, name=
'namespaceRenamed' )
11 def __init__( self, pParentDialog ):
12 super( NamespaceTableModel, self ).__init__()
14 self.mParentDialog = pParentDialog
15 self.mRefFilePath = {}
16 self.mRefFileReload = {}
19 self.mWatcher = QtCore.QFileSystemWatcher()
28 self.mWatcher.removePaths( self.mWatcher.files() )
29 self.removeRows( 0, len(self.mSys.Scene.Namespaces) )
30 self.mRefFilePath = {}
31 self.mRefFileReload = {}
34 QtCore.QObject.connect(self.mWatcher, QtCore.SIGNAL(
"fileChanged(const QString&)"), self.OnFileChanged )
36 def Disconnect( self ):
37 QtCore.QObject.disconnect( self.mWatcher, QtCore.SIGNAL(
"fileChanged(const QString&)"), self.OnFileChanged )
39 def removeRows( self, pRow, pCount, pParentIndex = QtCore.QModelIndex() ):
40 if pCount <= 0
or pRow < 0
or pRow + pCount > self.rowCount( pParentIndex ):
44 for lNSIndex
in range( pRow, pRow + pCount ):
45 if self.mSys.Scene.Namespaces[lNSIndex].Is( FBFileReference.TypeInfo ):
46 lList.Add( self.mSys.Scene.Namespaces[lNSIndex].LongName )
48 self.beginRemoveRows( pParentIndex, pRow, pRow + pCount - 1 )
50 self.mSys.Scene.NamespaceDelete( lNSName )
55 def AddFileFromWatcher( self, pNSObj ):
56 if not pNSObj.ReferenceFilePath
in self.mRefFilePath:
57 self.mRefFilePath[pNSObj.ReferenceFilePath] = []
58 self.mRefFileReload[pNSObj.ReferenceFilePath] =
False
60 if not pNSObj.LongName
in self.mRefFilePath[pNSObj.ReferenceFilePath]:
61 self.mRefFilePath[pNSObj.ReferenceFilePath].append( pNSObj.LongName )
63 self.UpdateFileWatcher()
65 def RemoveFileFromWatcher( self, pNSObj ):
66 if pNSObj.TypeInfo == FBNamespace.TypeInfo:
return
67 if not pNSObj.ReferenceFilePath
in self.mRefFilePath:
return
69 if pNSObj.LongName
in self.mRefFilePath[pNSObj.ReferenceFilePath]:
70 self.mRefFilePath[pNSObj.ReferenceFilePath].remove( pNSObj.LongName )
72 if len( self.mRefFilePath[pNSObj.ReferenceFilePath] ) == 0:
73 del self.mRefFilePath[pNSObj.ReferenceFilePath]
74 del self.mRefFileReload[pNSObj.ReferenceFilePath]
76 self.UpdateFileWatcher()
78 def UpdateFileWatcher( self ):
79 self.mWatcher.removePaths( self.mWatcher.files() )
81 if len( self.mRefFilePath.keys() ) == 0:
return
83 for lFilePath
in self.mRefFilePath.keys():
84 self.mWatcher.addPath( lFilePath )
86 def OnFileChanged( self, pFile ):
87 self.mRefFileReload[str(pFile)] =
True
89 def Refresh( self, pIndex = QtCore.QModelIndex() ):
91 self.dataChanged.emit( pIndex, pIndex )
95 def rowCount( self, pIndex = QtCore.QModelIndex() ):
96 if not pIndex.isValid():
97 return len( self.mSys.Scene.Namespaces )
101 def columnCount( self, pIndex = QtCore.QModelIndex() ):
102 if not pIndex.isValid():
107 def IsRefFileMonitored( self, pFileRefObj ):
108 if pFileRefObj.TypeInfo == FBNamespace.TypeInfo:
return False
110 if not pFileRefObj.ReferenceFilePath
in self.mRefFilePath:
113 if not pFileRefObj.LongName
in self.mRefFilePath[pFileRefObj.ReferenceFilePath]:
118 def data( self, pIndex, pRole = QtCore.Qt.DisplayRole ):
119 if not pIndex.isValid():
122 if pIndex.row() >= len( self.mSys.Scene.Namespaces )
or pIndex.row() < 0:
125 lNSObj = self.mSys.Scene.Namespaces[pIndex.row()]
127 if pRole == QtCore.Qt.DisplayRole:
128 if pIndex.column() == 0:
129 if lNSObj.TypeInfo == FBFileReference.TypeInfo:
130 if lNSObj.IsLoaded
and lNSObj.GetContentModified( FBPlugModificationFlag.kFBContentAllModifiedMask ):
131 return lNSObj.LongName +
'*'
132 return lNSObj.LongName
133 elif pIndex.column() == 1:
134 return lNSObj.TypeInfo == FBFileReference.TypeInfo
135 elif pIndex.column() == 2:
136 if self.IsRefFileMonitored(lNSObj):
140 elif pIndex.column() == 3:
141 if lNSObj.TypeInfo == FBFileReference.TypeInfo:
142 return lNSObj.ReferenceFilePath
145 elif pRole == QtCore.Qt.ForegroundRole:
146 if lNSObj.TypeInfo == FBFileReference.TypeInfo:
148 return QtGui.QBrush( QtCore.Qt.white )
150 return QtGui.QBrush( QtCore.Qt.darkGray )
152 elif pRole == QtCore.Qt.CheckStateRole:
154 if pIndex.column() == 1:
155 if lNSObj.TypeInfo == FBFileReference.TypeInfo:
156 return QtCore.Qt.Checked
158 return QtCore.Qt.Unchecked
161 if pIndex.column() == 2:
162 if self.IsRefFileMonitored(lNSObj):
163 return QtCore.Qt.Checked
165 return QtCore.Qt.Unchecked
167 def headerData( self, pSection, pOrientation, pRole = QtCore.Qt.DisplayRole ):
168 if pRole <> QtCore.Qt.DisplayRole:
171 if pOrientation == QtCore.Qt.Horizontal:
175 return 'Is Reference'
177 return 'File Monitored'
179 return 'Reference File Path'
181 elif pOrientation == QtCore.Qt.Vertical:
186 def flags( self, pIndex ):
187 lNSObj = self.mSys.Scene.Namespaces[pIndex.row()]
189 lFlag = QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
192 if pIndex.column() == 2:
193 lFlag = lFlag | QtCore.Qt.ItemIsUserCheckable
195 if pIndex.column() == 0:
196 lFlag = lFlag | QtCore.Qt.ItemIsEditable
198 if pIndex.column() == 3:
199 if lNSObj.Is( FBFileReference.TypeInfo ):
200 lFlag = lFlag | QtCore.Qt.ItemIsEditable
204 def setData( self, pIndex, pValue, pRole = QtCore.Qt.EditRole ):
205 if not pIndex.isValid():
return False
207 if pRole == QtCore.Qt.EditRole:
208 lNSObj = self.mSys.Scene.Namespaces[pIndex.row()]
209 if pIndex.column() == 0:
210 if str(pValue.toString()) ==
'':
return False
211 lMsgBox = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Information,
'Rename',
'Namespace %s will be renamed as %s. Do you want to proceed?' % ( lNSObj.LongName, str(pValue.toString()) ), QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, self.mParentDialog )
212 if lMsgBox.exec_() == QtWidgets.QMessageBox.Yes:
213 self.mSys.Scene.NamespaceRename( lNSObj.LongName, str(pValue.toString()) )
215 self.namespaceRenamed.emit( lNSObj.LongName, pValue.toString() )
216 self.dataChanged.emit( pIndex, pIndex )
218 elif pIndex.column() == 3:
219 lFileToLoad = pValue.toString()
220 if lFileToLoad ==
'':
221 lFileToLoad = QtWidgets.QFileDialog.getOpenFileName( self.mParentDialog,
"Pick FBX to reference", self.mParentDialog.mDefaultPath,
"*.fbx" )
223 if lFileToLoad <>
'':
224 lQFileInfo = QtCore.QFileInfo( lFileToLoad )
225 if not lQFileInfo.exists()
or not lQFileInfo.suffix() ==
'fbx':
227 lSwapDlg = DialogSwapRefFile.DialogSwapRefFile( self.mParentDialog )
229 lNSObj.SwapReferenceFilePath( str(lFileToLoad), lSwapDlg.uiCbApplyTargetEdit.checkState() == QtCore.Qt.Checked, lSwapDlg.uiCbMergeCurrentEdit.checkState() == QtCore.Qt.Checked )
230 self.dataChanged.emit( pIndex, pIndex )
233 elif pRole == QtCore.Qt.CheckStateRole:
235 if pIndex.column() == 1:
236 lNSObj = self.mSys.Scene.Namespaces[pIndex.row()]
238 if lNSObj.TypeInfo == FBFileReference.TypeInfo:
239 if pValue == QtCore.Qt.Unchecked:
240 lMsgBox = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Question, 'Namespace Downgrading', 'File Reference will be downgraded. Do you want to proceed?', QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, self.mParentDialog )
241 if lMsgBox.exec_() == QtWidgets.QMessageBox.Yes:
242 self.RemoveFileFromWatcher( lNSObj )
243 self.mSys.Scene.NamespaceDowngradeFromFileReference( lNSObj.LongName )
245 if pValue == QtCore.Qt.Checked:
246 lDlg = NamespaceUpgradeDialog.NamespaceUpgradeDialog( self.mParentDialog )
247 if lNSObj.GetOwnerFileReference() is not None:
248 lMsgBox = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Warning, 'Namespace Upgrading', 'Referenced namespace is not allowed to be upgraded!', QtWidgets.QMessageBox.Ok, self.mParentDialog )
250 elif lDlg.exec_() == QtWidgets.QMessageBox.Ok:
251 if lDlg.uiCbSaveAsText.checkState() == QtCore.Qt.Checked:
252 lRet = self.mSys.Scene.NamespaceUpgradeToFileReference( lNSObj.LongName, str(lDlg.uiEditFilePath.text()), True )
254 lRet = self.mSys.Scene.NamespaceUpgradeToFileReference( lNSObj.LongName, str(lDlg.uiEditFilePath.text()), False )
256 self.dataChanged.emit( pIndex, pIndex )
260 if pIndex.column() == 2:
261 lNSObj = self.mSys.Scene.Namespaces[pIndex.row()]
262 if lNSObj.TypeInfo == FBFileReference.TypeInfo:
263 if pValue == QtCore.Qt.Checked:
267 self.AddFileFromWatcher( lNSObj )
269 elif pValue == QtCore.Qt.Unchecked:
273 self.RemoveFileFromWatcher( lNSObj )
275 self.dataChanged.emit( pIndex, pIndex )