Samples/Referencing/MBFileRefDemo/NamespaceTableModel.py

Samples/Referencing/MBFileRefDemo/NamespaceTableModel.py
1 from pyfbsdk import *
2 from pyfbsdk_additions import *
3 
4 from PySide2 import QtCore, QtGui, QtWidgets
5 import NamespaceUpgradeDialog
6 import DialogSwapRefFile
7 
8 class NamespaceTableModel( QtCore.QAbstractTableModel ):
9  namespaceRenamed = QtCore.Signal( str, str, name='namespaceRenamed' )
10 
11  def __init__( self, pParentDialog ):
12  super( NamespaceTableModel, self ).__init__()
13 
14  self.mParentDialog = pParentDialog
15  self.mRefFilePath = {}
16  self.mRefFileReload = {}
17 
18  self.mSys = FBSystem()
19  self.mWatcher = QtCore.QFileSystemWatcher()
20 
21  self.Init()
22 
23  def Init( self ):
24  self.Connect()
25 
26  def Fini( self ):
27  self.Disconnect()
28  self.mWatcher.removePaths( self.mWatcher.files() )
29  self.removeRows( 0, len(self.mSys.Scene.Namespaces) )
30  self.mRefFilePath = {}
31  self.mRefFileReload = {}
32 
33  def Connect( self ):
34  QtCore.QObject.connect(self.mWatcher, QtCore.SIGNAL("fileChanged(const QString&)"), self.OnFileChanged )
35 
36  def Disconnect( self ):
37  QtCore.QObject.disconnect( self.mWatcher, QtCore.SIGNAL("fileChanged(const QString&)"), self.OnFileChanged )
38 
39  def removeRows( self, pRow, pCount, pParentIndex = QtCore.QModelIndex() ):
40  if pCount <= 0 or pRow < 0 or pRow + pCount > self.rowCount( pParentIndex ):
41  return False
42 
43  lList = FBStringList()
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 )
47 
48  self.beginRemoveRows( pParentIndex, pRow, pRow + pCount - 1 )
49  for lNSName in lList:
50  self.mSys.Scene.NamespaceDelete( lNSName )
51  self.endRemoveRows()
52 
53  return True
54 
55  def AddFileFromWatcher( self, pNSObj ):
56  if not pNSObj.ReferenceFilePath in self.mRefFilePath:
57  self.mRefFilePath[pNSObj.ReferenceFilePath] = []
58  self.mRefFileReload[pNSObj.ReferenceFilePath] = False
59 
60  if not pNSObj.LongName in self.mRefFilePath[pNSObj.ReferenceFilePath]:
61  self.mRefFilePath[pNSObj.ReferenceFilePath].append( pNSObj.LongName )
62 
63  self.UpdateFileWatcher()
64 
65  def RemoveFileFromWatcher( self, pNSObj ):
66  if pNSObj.TypeInfo == FBNamespace.TypeInfo: return
67  if not pNSObj.ReferenceFilePath in self.mRefFilePath: return
68 
69  if pNSObj.LongName in self.mRefFilePath[pNSObj.ReferenceFilePath]:
70  self.mRefFilePath[pNSObj.ReferenceFilePath].remove( pNSObj.LongName )
71 
72  if len( self.mRefFilePath[pNSObj.ReferenceFilePath] ) == 0:
73  del self.mRefFilePath[pNSObj.ReferenceFilePath]
74  del self.mRefFileReload[pNSObj.ReferenceFilePath]
75 
76  self.UpdateFileWatcher()
77 
78  def UpdateFileWatcher( self ):
79  self.mWatcher.removePaths( self.mWatcher.files() )
80 
81  if len( self.mRefFilePath.keys() ) == 0: return
82 
83  for lFilePath in self.mRefFilePath.keys():
84  self.mWatcher.addPath( lFilePath )
85 
86  def OnFileChanged( self, pFile ):
87  self.mRefFileReload[str(pFile)] = True
88 
89  def Refresh( self, pIndex = QtCore.QModelIndex() ):
90  if pIndex.isValid():
91  self.dataChanged.emit( pIndex, pIndex )
92  else:
93  self.reset()
94 
95  def rowCount( self, pIndex = QtCore.QModelIndex() ):
96  if not pIndex.isValid():
97  return len( self.mSys.Scene.Namespaces )
98  else:
99  return 0
100 
101  def columnCount( self, pIndex = QtCore.QModelIndex() ):
102  if not pIndex.isValid():
103  return 4
104  else:
105  return 0
106 
107  def IsRefFileMonitored( self, pFileRefObj ):
108  if pFileRefObj.TypeInfo == FBNamespace.TypeInfo: return False
109 
110  if not pFileRefObj.ReferenceFilePath in self.mRefFilePath:
111  return False
112 
113  if not pFileRefObj.LongName in self.mRefFilePath[pFileRefObj.ReferenceFilePath]:
114  return False
115 
116  return True
117 
118  def data( self, pIndex, pRole = QtCore.Qt.DisplayRole ):
119  if not pIndex.isValid():
120  return None
121 
122  if pIndex.row() >= len( self.mSys.Scene.Namespaces ) or pIndex.row() < 0:
123  return None
124 
125  lNSObj = self.mSys.Scene.Namespaces[pIndex.row()]
126 
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):
137  return True
138  else:
139  return False
140  elif pIndex.column() == 3:
141  if lNSObj.TypeInfo == FBFileReference.TypeInfo:
142  return lNSObj.ReferenceFilePath
143  else:
144  return ''
145  elif pRole == QtCore.Qt.ForegroundRole:
146  if lNSObj.TypeInfo == FBFileReference.TypeInfo:
147  if lNSObj.IsLoaded:
148  return QtGui.QBrush( QtCore.Qt.white )
149  else:
150  return QtGui.QBrush( QtCore.Qt.darkGray )
151 
152  elif pRole == QtCore.Qt.CheckStateRole:
153  '''
154  if pIndex.column() == 1:
155  if lNSObj.TypeInfo == FBFileReference.TypeInfo:
156  return QtCore.Qt.Checked
157  else:
158  return QtCore.Qt.Unchecked
159  elif
160  '''
161  if pIndex.column() == 2:
162  if self.IsRefFileMonitored(lNSObj):
163  return QtCore.Qt.Checked
164  else:
165  return QtCore.Qt.Unchecked
166 
167  def headerData( self, pSection, pOrientation, pRole = QtCore.Qt.DisplayRole ):
168  if pRole <> QtCore.Qt.DisplayRole:
169  return None
170 
171  if pOrientation == QtCore.Qt.Horizontal:
172  if pSection == 0:
173  return 'Namespace'
174  elif pSection == 1:
175  return 'Is Reference'
176  elif pSection == 2:
177  return 'File Monitored'
178  elif pSection == 3:
179  return 'Reference File Path'
180 
181  elif pOrientation == QtCore.Qt.Vertical:
182  return pSection + 1
183 
184  return None
185 
186  def flags( self, pIndex ):
187  lNSObj = self.mSys.Scene.Namespaces[pIndex.row()]
188 
189  lFlag = QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
190 
191  #if pIndex.column() == 1 or pIndex.column() == 2:
192  if pIndex.column() == 2:
193  lFlag = lFlag | QtCore.Qt.ItemIsUserCheckable
194 
195  if pIndex.column() == 0:
196  lFlag = lFlag | QtCore.Qt.ItemIsEditable
197 
198  if pIndex.column() == 3:
199  if lNSObj.Is( FBFileReference.TypeInfo ):
200  lFlag = lFlag | QtCore.Qt.ItemIsEditable
201 
202  return lFlag
203 
204  def setData( self, pIndex, pValue, pRole = QtCore.Qt.EditRole ):
205  if not pIndex.isValid(): return False
206 
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()) )
214  #self.Refresh( pIndex, pIndex )
215  self.namespaceRenamed.emit( lNSObj.LongName, pValue.toString() )
216  self.dataChanged.emit( pIndex, pIndex )
217  return True
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" )
222 
223  if lFileToLoad <> '':
224  lQFileInfo = QtCore.QFileInfo( lFileToLoad )
225  if not lQFileInfo.exists() or not lQFileInfo.suffix() == 'fbx':
226  return False
227  lSwapDlg = DialogSwapRefFile.DialogSwapRefFile( self.mParentDialog )
228  lSwapDlg.exec_()
229  lNSObj.SwapReferenceFilePath( str(lFileToLoad), lSwapDlg.uiCbApplyTargetEdit.checkState() == QtCore.Qt.Checked, lSwapDlg.uiCbMergeCurrentEdit.checkState() == QtCore.Qt.Checked )
230  self.dataChanged.emit( pIndex, pIndex )
231  return True
232 
233  elif pRole == QtCore.Qt.CheckStateRole:
234  '''
235  if pIndex.column() == 1:
236  lNSObj = self.mSys.Scene.Namespaces[pIndex.row()]
237  print lNSObj
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 )
244  else:
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 )
249  lMsgBox.exec_()
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 )
253  else:
254  lRet = self.mSys.Scene.NamespaceUpgradeToFileReference( lNSObj.LongName, str(lDlg.uiEditFilePath.text()), False )
255 
256  self.dataChanged.emit( pIndex, pIndex )
257  return True
258  elif
259  '''
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:
264  #lMsgBox = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Information, 'File Monitoring', 'File Reference is added to monitor.', QtWidgets.QMessageBox.Ok, self.mParentDialog )
265  #lMsgBox.exec_()
266 
267  self.AddFileFromWatcher( lNSObj )
268 
269  elif pValue == QtCore.Qt.Unchecked:
270  #lMsgBox = QtWidgets.QMessageBox( QtWidgets.QMessageBox.Information, 'File Monitoring', 'File Reference is removed from monitor.', QtWidgets.QMessageBox.Ok, self.mParentDialog )
271  #lMsgBox.exec_()
272 
273  self.RemoveFileFromWatcher( lNSObj )
274 
275  self.dataChanged.emit( pIndex, pIndex )
276  return True
277  return False