2024年06月24日

Pyside6を使ったカスタムUI modo17 その111

今まで試して来た並べ替えや絞り込みを組み込んだタブを追加してみました。並べ替えは1列目と2列目の並べ替えボタンだけあるけど、実際の並べ替えはこのボタンを押して並べ替えを有効にした後、列のヘッダ部分をクリックすることで行う事が出来るようになっているので有効になった後はボタンを押す必要はない。今回はとりあえず組み込んだだけなので他の機能との整合性などまだ試していない。それとリストアイテムの選択と頂点の選択の整合性を取る部分について今のところダイアログにメソッドが定義されてるけどQTableView自体に組み込んだ方が自然なのでその辺も修正して行きたいところだね。今回はそのままやっているのでモデルの中で並べ替えなどをした時にテーブルの更新のメソッドを呼び出すためにモデルにダイアログへの参照変数「self.dialog」を設定している。

from math import *
import csv
import modo
import lx
import lxu
import sys
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *

class MyModel(QAbstractTableModel):
	def __init__(self,parent=None):
		super(MyModel,self).__init__(parent)
		
		self.__points = []
		self.__labels = ['x','y','z']
		
		self.update()
		self.filterIndex = range(len(self.__points))
		
	def points(self):
		return self.__points
        
	def dist(self,p1,p2):
		return ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2 + (p1[2]-p2[2])**2 ) ** 0.5
	
	def P2Ldist(self,lp1,lp2,p):
		vx = lp2[0] - lp1[0]
		vy = lp2[1] - lp1[1]
		vz = lp2[2] - lp1[2]
		t = (vx*(p[0]-lp1[0])+vy*(p[1]-lp1[1])+vz*(p[2]-lp1[2]))/(vx*vx+vy*vy+vz*vz)
		cp = [vx*t+lp1[0],vy*t+lp1[1],vz*t+lp1[2]]
		return dist(p,cp)
		
	def P2Fside(self,fp1,fp2,fp3,p,e=1E-8):
		vx1 = fp2[0] - fp1[0]
		vy1 = fp2[1] - fp1[1]
		vz1 = fp2[2] - fp1[2]

		vx2 = fp3[0] - fp1[0]
		vy2 = fp3[1] - fp1[1]
		vz2 = fp3[2] - fp1[2]

		nx = vy1*vz2-vz1*vy2
		ny = vz1*vx2-vx1*vz2
		nz = vx1*vy2-vy1*vx2

		nl = nx*nx + ny*ny + nz*nz
		t = (nx*(fp1[0]-p[0])+ny*(fp1[1]-p[1])+nz*(fp1[2]-p[2]))/nl
		if abs(t) < e :
			return 0
		return (t < 0) - (t > 0)
		
	def P2Fdist(self,fp1,fp2,fp3,p):
		vx1 = fp2[0] - fp1[0]
		vy1 = fp2[1] - fp1[1]
		vz1 = fp2[2] - fp1[2]

		vx2 = fp3[0] - fp1[0]
		vy2 = fp3[1] - fp1[1]
		vz2 = fp3[2] - fp1[2]

		nx = vy1*vz2-vz1*vy2
		ny = vz1*vx2-vx1*vz2
		nz = vx1*vy2-vy1*vx2

		nl = nx*nx + ny*ny + nz*nz
		t = (nx*(fp1[0]-p[0])+ny*(fp1[1]-p[1])+nz*(fp1[2]-p[2]))/nl
		return ((nx*t)**2 + (ny * t)**2 + (nz * t)**2)**0.5

	def update(self):
		self.beginResetModel()
		self.mesh = modo.Mesh()
		self.__points = []
		if self.mesh:
			verts = self.mesh.geometry.vertices
			for v in verts:
				self.__points.append(list(v.position))
		else:
			print('No Mesh Selected')
		self.endResetModel()
		
	def setFilter(self,switch,filterIndex=None):
		self.beginResetModel()
		if switch :
			self.filterIndex = filterIndex
		else:
			self.filterIndex = range(len(self.__points))
		self.endResetModel()
		self.dialog.updateTable()
		
	def rowCount(self,parent=None):
		return len(self.filterIndex)
		
	def columnCount(self,parent):
		return 3
		
	def data(self,index,role):
		if role == Qt.DisplayRole:
			rindex = self.filterIndex[index.row()]
			return self.__points[rindex][index.column()]
			
	def setData(self,index,value):
		i = self.filterIndex[index.row()]
		j = index.column()
		self.__points[i][j] = value
		lx.eval('vertMap.setVertex position position %s %s %s' % (j,i,value))
		
	def sort(self,col,order):
		if order==Qt.SortOrder.DescendingOrder:
			direction = True
		else:
			direction = False
		indexes = sorted(self.filterIndex,key=lambda i:self.points()[i][col],reverse=direction)

		self.setFilter(True,indexes)

	def flags(self, index):
		if not index.isValid():
			return 0
		return Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable	

	def headerData(self,section,orientation,role):
		if role == Qt.DisplayRole:
			if orientation == Qt.Horizontal:
				return self.__labels[section]
			else:
				return str(self.filterIndex[section])
				
	def calc(self,exp,n):
    
		dist = self.dist
		P2Ldist = self.P2Ldist
		P2Fdist = self.P2Fdist
		P2Fside = self.P2Fside
		
		v = self.__points
		p = self.__points[n]
		x = p[0]
		y = p[1]
		z = p[2]
        
		try:
			return eval(exp)
		except Exception as e:
			msgbox = QMessageBox()
			msgbox.setWindowTitle('Error')
			msgbox.setText(e.__str__())
			msgbox.setWindowFlags(Qt.WindowStaysOnTopHint|Qt.WindowCloseButtonHint)
			msgbox.exec()
			return None
			
	def create(self,expx,expy,expz,i,n):
		dist = self.dist
		P2Ldist = self.P2Ldist
		P2Fdist = self.P2Fdist
		P2Fside = self.P2Fside
		
		v = self.__points
		retx = 0.0
		rety = 0.0
		retz = 0.0
        
		try:
			if len(expx)!=0:
				retx = eval(expx)
			if len(expy)!=0:
				rety = eval(expy)
			if len(expz)!=0:
				retz = eval(expz)
			lx.command('vert.new',x=retx,y=rety,z=retz)
			return True
			
		except Exception as e:
			msgbox = QMessageBox()
			msgbox.setWindowTitle('Error')
			msgbox.setText(e.__str__())
			msgbox.setWindowFlags(Qt.WindowStaysOnTopHint|Qt.WindowCloseButtonHint)
			msgbox.exec()
			return False

	def modify(self,expx,expy,expz,n):
    
		dist = self.dist
		P2Ldist = self.P2Ldist
		P2Fdist = self.P2Fdist
		P2Fside = self.P2Fside
		
		v = self.__points
		p = self.__points[n]
		x = p[0]
		y = p[1]
		z = p[2]
        
		try:
			if len(expx)!=0:
				retx = eval(expx)
			else:
				retx = x

			if len(expy)!=0:
				rety = eval(expy)
			else:
				rety = y
			if len(expz)!=0:
				retz = eval(expz)
			else:
				retz = z
			self.__points[n][0] = retx
			lx.eval('vertMap.setVertex position position %s %s %s' % (0,n,retx))
			self.__points[n][1] = rety
			lx.eval('vertMap.setVertex position position %s %s %s' % (1,n,rety))
			self.__points[n][2] = retz
			lx.eval('vertMap.setVertex position position %s %s %s' % (2,n,retz))
		
			return True
			
		except Exception as e:
			msgbox = QMessageBox()
			msgbox.setWindowTitle('Error')
			msgbox.setText(e.__str__())
			msgbox.setWindowFlags(Qt.WindowStaysOnTopHint|Qt.WindowCloseButtonHint)
			msgbox.exec()
			return False
							
class MyItemDelegate(QItemDelegate):
	def __init__(self, parent=None):
		super(MyItemDelegate, self).__init__(parent)
		
	def createEditor(self, parent, option, index):
		return QLineEdit(parent)

	def setEditorData(self, editor, index):
		value = str(index.model().data(index, Qt.DisplayRole))
		editor.setText(value)

	def setModelData(self, editor, model, index):
		try:
			value = float(editor.text())
			model.setData(index, value)
		except ValueError:
			pass

class Tab_Select_Expression(QWidget):
	def __init__(self,parent=None):
		super(Tab_Select_Expression,self).__init__()
		self.parent = parent
		self.leExpression = QLineEdit()
		self.pbClrSelect = QPushButton('Select')
		self.pbAddSelect = QPushButton('Add')
		self.pbSubSelect = QPushButton('Sub')
		
		self.pbClrSelect.clicked.connect(self.clrSelect)
		self.pbAddSelect.clicked.connect(self.addSelect)
		self.pbSubSelect.clicked.connect(self.subSelect)
		layout = QHBoxLayout(self)
		layout.addWidget(self.leExpression)
		layout.addWidget(self.pbClrSelect)
		layout.addWidget(self.pbAddSelect)
		layout.addWidget(self.pbSubSelect)

	def clrSelect(self):
		flag = QItemSelectionModel.Rows|QItemSelectionModel.Select
		self.parent.tableView.selectionModel().clear()
		lx.command('select.clear',type='vertex')
		layer_index = lx.eval('query layerservice layers ? main')
		for i in range(self.parent.model.rowCount()):
			ret = self.parent.model.calc(self.leExpression.text(),i)
			if ret == None : break
			if ret :
				index = self.parent.model.index(i,0)
				self.parent.tableView.selectionModel().select(index,flag)
				lx.command('select.element',layer=layer_index,type='vertex',mode='add',index=i)

	def addSelect(self):
		flag = QItemSelectionModel.Rows|QItemSelectionModel.Select
		layer_index = lx.eval('query layerservice layers ? main')
		for i in range(self.parent.model.rowCount()):
			ret = self.parent.model.calc(self.leExpression.text(),i)
			if ret == None : break
			if ret :
				index = self.parent.model.index(i,0)
				self.parent.tableView.selectionModel().select(index,flag)
				lx.command('select.element',layer=layer_index,type='vertex',mode='add',index=i)

	def subSelect(self):
		flag = QItemSelectionModel.Rows|QItemSelectionModel.Deselect
		layer_index = lx.eval('query layerservice layers ? main')
		for i in range(self.parent.model.rowCount()):
			ret = self.parent.model.calc(self.leExpression.text(),i)
			if ret == None : break
			if ret :
				index = self.parent.model.index(i,0)
				self.parent.tableView.selectionModel().select(index,flag)
				lx.command('select.element',layer=layer_index,type='vertex',mode='remove',index=i)
						
class Tab_Selection_Set(QWidget):
	def __init__(self,parent=None):
		super(Tab_Selection_Set,self).__init__()
		self.parent = parent
		self.cbPick = QComboBox()
		self.cbUpdate()
		self.pbNewPick = QPushButton('New Set')
		self.pbRmvPick = QPushButton('Remove Set')
		self.pbClrPick = QPushButton('Select Set')
		self.pbAddPick = QPushButton('Add Set')
		self.pbSubPick = QPushButton('Sub Set')
		self.pbNewPick.clicked.connect(self.newPick)
		self.pbRmvPick.clicked.connect(self.removePick)
		self.pbClrPick.clicked.connect(self.selectPick)
		self.pbAddPick.clicked.connect(self.addPick)
		self.pbSubPick.clicked.connect(self.subPick)
		
		layout = QHBoxLayout(self)
		layout.addWidget(self.cbPick)
		layout.addWidget(self.pbNewPick)
		layout.addWidget(self.pbRmvPick)
		layout.addWidget(self.pbClrPick)
		layout.addWidget(self.pbAddPick)
		layout.addWidget(self.pbSubPick)

	def newPick(self):
		lx.command('select.editSet')
		self.cbUpdate()
		
	def removePick(self):
		lx.eval('select.deleteSet %s' % self.cbPick.currentText())
		self.cbUpdate()
				
	def selectPick(self):
		lx.eval('select.useSet %s replace' % self.cbPick.currentText())
		self.parent.updateTable()
		
	def addPick(self):
		lx.eval('select.useSet %s select' % self.cbPick.currentText())
		self.parent.updateTable()
		
	def subPick(self):
		lx.eval('select.useSet %s deselect' % self.cbPick.currentText())
		self.parent.updateTable()
		
	def cbUpdate(self):
		self.cbPick.clear()
		lx.eval('query layerservice layer.index ? main')
		vn = lx.eval('query layerservice vmap.N ? all')
		for i in range(vn):
			if lx.eval('query layerservice vmap.type ? %d' % i) == 'pick':
				pickName = lx.eval('query layerservice vmap.name ? %d' % i)
				self.cbPick.addItem(pickName)

class Tab_Set_Value(QWidget):
	def __init__(self,parent=None):
		super(Tab_Set_Value,self).__init__()
		self.parent = parent
		self.cbAxis = QComboBox()
		self.cbAxis.addItems(['X','Y','Z'])
		self.leValue = QDoubleSpinBox()
		self.leValue.setDecimals(3)
		self.leValue.setRange(-sys.float_info.max,sys.float_info.max)
		self.leValue.setSingleStep(0.1)
		self.pbSetValues = QPushButton('Set Value')
		self.pbSetValues.clicked.connect(self.setValues)
		self.pbAddValues = QPushButton('Add Value')
		self.pbAddValues.clicked.connect(self.addValues)
		self.pbSubValues = QPushButton('Sub Value')
		self.pbSubValues.clicked.connect(self.subValues)
		self.pbAvgValues = QPushButton('Avg Value')
		self.pbAvgValues.clicked.connect(self.avgValues)
		self.pbMaxValues = QPushButton('Max Value')
		self.pbMaxValues.clicked.connect(self.maxValues)
		self.pbMinValues = QPushButton('Min Value')
		self.pbMinValues.clicked.connect(self.minValues)	

		layout = QHBoxLayout(self)
		sublayout1 = QVBoxLayout()
		sublayout11 = QHBoxLayout()
		sublayout12 = QHBoxLayout()		
		layout.addWidget(self.cbAxis)
		layout.addWidget(self.leValue)
		sublayout11.addWidget(self.pbSetValues)
		sublayout11.addWidget(self.pbAddValues)
		sublayout11.addWidget(self.pbSubValues)
		sublayout12.addWidget(self.pbAvgValues)
		sublayout12.addWidget(self.pbMaxValues)
		sublayout12.addWidget(self.pbMinValues)
		sublayout1.addLayout(sublayout11)
		sublayout1.addLayout(sublayout12)
		layout.addLayout(sublayout1)

	def setValues(self):
		axis = self.cbAxis.currentText()
		value = self.leValue.value()
		lx.command('vert.set',axis = axis,value = value)
		self.parent.updateTable()

	def addValues(self):
		layer_index = lx.eval('query layerservice layer.index ? main')
		verts = lx.eval('query layerservice verts ? selected')
		index = self.cbAxis.currentIndex()
		axis = self.cbAxis.currentText()
		value = self.leValue.value()
		for i in verts:
			pos = lx.eval('query layerservice vert.pos ? %s' % i)
			lx.eval('vertMap.setVertex position position %s %s %s' % (index,i,pos[index]+value))
		self.parent.updateTable()

	def subValues(self):
		layer_index = lx.eval('query layerservice layer.index ? main')
		verts = lx.eval('query layerservice verts ? selected')
		index = self.cbAxis.currentIndex()
		axis = self.cbAxis.currentText()
		value = self.leValue.value()
		for i in verts:
			pos = lx.eval('query layerservice vert.pos ? %s' % i)
			lx.eval('vertMap.setVertex position position %s %s %s' % (index,i,pos[index]-value))
		self.parent.updateTable()

	def avgValues(self):
		layer_index = lx.eval('query layerservice layer.index ? main')
		verts = lx.eval('query layerservice verts ? selected')
		index = self.cbAxis.currentIndex()
		axis = self.cbAxis.currentText()
		avg = 0
		for i in verts:
			pos = lx.eval('query layerservice vert.pos ? %s' % i)
			avg += pos[index]
		avg /= len(verts)
		lx.command('vert.set',axis = axis,value = avg)
		self.parent.updateTable()

	def maxValues(self):
		layer_index = lx.eval('query layerservice layer.index ? main')
		verts = lx.eval('query layerservice verts ? selected')
		index = self.cbAxis.currentIndex()
		axis = self.cbAxis.currentText()
		pos = lx.eval('query layerservice vert.pos ? %s' % verts[0])
		max = pos[index]
		for i in verts:
			pos = lx.eval('query layerservice vert.pos ? %s' % i)
			if pos[index] > max :
				max = pos[index]
		lx.command('vert.set',axis = axis,value = max)
		self.parent.updateTable()

	def minValues(self):
		layer_index = lx.eval('query layerservice layer.index ? main')
		verts = lx.eval('query layerservice verts ? selected')
		index = self.cbAxis.currentIndex()
		axis = self.cbAxis.currentText()
		pos = lx.eval('query layerservice vert.pos ? %s' % verts[0])
		min = pos[index]
		for i in verts:
			pos = lx.eval('query layerservice vert.pos ? %s' % i)
			if pos[index] < min :
				min = pos[index]
		lx.command('vert.set',axis = axis,value = min)
		self.parent.updateTable()
		
class Tab_Create_Modify_Expression(QWidget):
	def __init__(self,parent=None):
		super(Tab_Create_Modify_Expression,self).__init__()
		self.parent = parent
		self.lbX = QLabel('x=')
		self.leModXExpression = QLineEdit()
		self.lbY = QLabel('y=')
		self.leModYExpression = QLineEdit()	
		self.lbZ = QLabel('z=')
		self.leModZExpression = QLineEdit()
		self.lbStart = QLabel('start n=')
		self.sbStart = QDoubleSpinBox()
		self.sbStart.setDecimals(3)
		self.sbStart.setRange(-sys.float_info.max,sys.float_info.max)
		self.sbStart.setSingleStep(0.1)
		self.lbStep = QLabel('step=')
		self.sbStep = QDoubleSpinBox()
		self.sbStep.setDecimals(3)
		self.sbStep.setRange(-sys.float_info.max,sys.float_info.max)
		self.sbStep.setSingleStep(0.1)
		self.lbCount = QLabel('count=')
		self.sbCount = QSpinBox()
		self.sbCount.setValue(1)
		self.sbCount.setMinimum(1)
		self.pbCreExec = QPushButton('Create')
		self.pbCreExec.clicked.connect(self.creExecute)
		self.pbModExec = QPushButton('Modify')
		self.pbModExec.clicked.connect(self.modExecute)		

		layout = QVBoxLayout(self)
		sublayout1 = QHBoxLayout()
		sublayout2 = QHBoxLayout()
		sublayout3 = QHBoxLayout()
		sublayout4 = QHBoxLayout()
		sublayout5 = QHBoxLayout()
		sublayout1.addWidget(self.lbX)
		sublayout1.addWidget(self.leModXExpression)
		sublayout2.addWidget(self.lbY)
		sublayout2.addWidget(self.leModYExpression)
		sublayout3.addWidget(self.lbZ)
		sublayout3.addWidget(self.leModZExpression)
		sublayout4.addWidget(self.lbStart)
		sublayout4.addWidget(self.sbStart)
		sublayout4.addWidget(self.lbStep)
		sublayout4.addWidget(self.sbStep)
		sublayout4.addWidget(self.lbCount)
		sublayout4.addWidget(self.sbCount)
		sublayout5.addWidget(self.pbCreExec)
		sublayout5.addWidget(self.pbModExec)
		
		layout.addLayout(sublayout1)
		layout.addLayout(sublayout2)
		layout.addLayout(sublayout3)
		layout.addLayout(sublayout4)
		layout.addLayout(sublayout5)

	def modExecute(self):
		layer_index = lx.eval('query layerservice layer.index ? main')
		verts = lx.eval('query layerservice verts ? selected')
		for i in verts:
			ret = self.parent.model.modify(self.leModXExpression.text(),self.leModYExpression.text(),self.leModZExpression.text(),int(i))
			if ret == False : break
		self.parent.updateTable()

	def creExecute(self):
		layer_index = lx.eval('query layerservice layer.index ? main')
		select_start = lx.eval('query layerservice vert.N ? all')
		lx.command('select.drop',type='vertex')
		n = self.sbStart.value()
		step = self.sbStep.value()
		for i in range(self.sbCount.value()):
			n += step 
			ret = self.parent.model.create(self.leModXExpression.text(),self.leModYExpression.text(),self.leModZExpression.text(),i,n)
			if ret == False : break
			lx.command('select.element',layer=layer_index,type='vertex',mode='add',index=(select_start+i))
		self.parent.updateTable()
		
class Tab_File(QWidget):
	def __init__(self,parent=None):
		super(Tab_File,self).__init__()
		self.parent = parent
		
		self.rgroup = QButtonGroup()
		self.rbAll = QRadioButton('all')
		self.rbSel = QRadioButton('Select Only')
		
		self.rgroup.addButton(self.rbAll,1)
		self.rgroup.addButton(self.rbSel,2)
		self.rbAll.setChecked(True)
		self.rgroup.buttonClicked.connect(self.rbuttonClicked)
		
		self.pbCSVWrite = QPushButton('CSV Write')
		self.pbCSVRead = QPushButton('CSV Read')		

		self.pbCSVWrite.clicked.connect(self.csvWrite)
		self.pbCSVRead.clicked.connect(self.csvRead)

		self.lbX = QLabel('x=')
		self.leModXExpression = QLineEdit('px')
		self.leModXExpression.setEnabled(False)
		self.lbY = QLabel('y=')
		self.leModYExpression = QLineEdit('py')	
		self.leModYExpression.setEnabled(False)
		self.lbZ = QLabel('z=')
		self.leModZExpression = QLineEdit('pz')
		self.leModZExpression.setEnabled(False)
		
		radwlayout = QHBoxLayout()
		radwlayout.addWidget(self.rbAll)
		radwlayout.addWidget(self.rbSel)
		layout = QVBoxLayout(self)
		layout.addLayout(radwlayout)
		layout.addWidget(self.pbCSVWrite)
		layout.addWidget(self.pbCSVRead)
		
		sublayout1 = QHBoxLayout()
		sublayout2 = QHBoxLayout()
		sublayout3 = QHBoxLayout()
		sublayout1.addWidget(self.lbX)
		sublayout1.addWidget(self.leModXExpression)
		sublayout2.addWidget(self.lbY)
		sublayout2.addWidget(self.leModYExpression)
		sublayout3.addWidget(self.lbZ)
		sublayout3.addWidget(self.leModZExpression)
		layout.addLayout(sublayout1)
		layout.addLayout(sublayout2)
		layout.addLayout(sublayout3)
		
	def rbuttonClicked(self,rbtn):
		if self.rgroup.checkedId() == 1:
			self.leModXExpression.setEnabled(False)
			self.leModYExpression.setEnabled(False)
			self.leModZExpression.setEnabled(False)
		else:
			self.leModXExpression.setEnabled(True)
			self.leModYExpression.setEnabled(True)
			self.leModZExpression.setEnabled(True)
			
	def csvWrite(self):
		fileName = QFileDialog.getSaveFileName(self,'Write CSV File',filter='CSV(*.csv)')
		if fileName[0] != "":
			points = self.parent.model.points()
			if self.rgroup.checkedId() == 2 :
				layer_index = lx.eval('query layerservice layer.index ? main')
				selverts = [int(v) for v in list(lx.eval('query layerservice verts ? selected'))]
				selverts.sort()
				points = [points[i] for i in selverts]
			with open(fileName[0],"w",newline="") as f:
				writer = csv.writer(f)
				writer.writerows(points) 
				
	def csvRead(self):
		fileName = QFileDialog.getOpenFileName(self,'Read CSV File',filter='CSV(*.csv)')
		if fileName[0] != "":
			layer_index = lx.eval('query layerservice layer.index ? main')

			if self.rgroup.checkedId() == 2 :
				indexes =  [int(v) for v in list(lx.eval('query layerservice verts ? selected'))]
				indexes.sort()
				vertn = len(indexes)
			else:
				vertn = lx.eval('query layerservice vert.N ? all')
				indexes = range(vertn)				
			with open(fileName[0],"r") as f:
				reader = csv.reader(f)
				index = 0
				for pos in reader:
					if self.rgroup.checkedId() == 2 :
						px = float(pos[0])
						py = float(pos[1])
						pz = float(pos[2])
						try:
							x = eval(self.leModXExpression.text())
							y = eval(self.leModYExpression.text())
							z = eval(self.leModZExpression.text())
						except Exception as e:
							msgbox = QMessageBox()
							msgbox.setWindowTitle('Error')
							msgbox.setText(e.__str__())
							msgbox.setWindowFlags(Qt.WindowStaysOnTopHint|Qt.WindowCloseButtonHint)
							msgbox.exec()

							return None
					else:
						x = float(pos[0])
						y = float(pos[1])
						z = float(pos[2])
											
					lx.eval('vertMap.setVertex position position %s %s %s' % (0,indexes[index],x))
					lx.eval('vertMap.setVertex position position %s %s %s' % (1,indexes[index],y))
					lx.eval('vertMap.setVertex position position %s %s %s' % (2,indexes[index],z))
					index = index + 1
					if index >= vertn : break 
			self.parent.updateTable()

class Tab_TableOp(QWidget):
	def __init__(self,parent=None):
		super(Tab_TableOp,self).__init__()
		self.parent = parent
		self.pbNarrow = QPushButton('Narrow down')
		self.pbSort1 = QPushButton('Sort Col1')
		self.pbSort2 = QPushButton('Sort Col2')
		self.pbCancel = QPushButton('Cancel')		
		
		self.pbNarrow.clicked.connect(self.narrowList)
		self.pbSort1.clicked.connect(self.sortColumn)
		self.pbSort2.clicked.connect(self.sortColumn)
		self.pbCancel.clicked.connect(self.narrowCancel)
		
		sublayout = QHBoxLayout()
		sublayout.addWidget(self.pbNarrow)
		sublayout.addWidget(self.pbSort1)
		sublayout.addWidget(self.pbSort2)
		sublayout.addWidget(self.pbCancel)
		
		mainlayout = QVBoxLayout(self)
		mainlayout.addWidget(self.parent.tableView)
		mainlayout.addLayout(sublayout)

	def narrowList(self):
		selIndexes = list(set([index.row() for index in self.parent.tableView.selectionModel().selectedIndexes()]))
		selIndexes.sort()
		indexes = [self.parent.model.filterIndex[i] for i in selIndexes]
		self.parent.model.setFilter(True,indexes)
		self.parent.updateTable()
		
	def sortColumn(self):
		if self.sender() == self.pbSort1:
			col=0
		else:
			col=1
		self.parent.tableView.setSortingEnabled(True)
		self.parent.tableView.sortByColumn(col,Qt.AscendingOrder)
		self.parent.updateTable()

	def narrowCancel(self):
		self.parent.model.setFilter(False)
		self.parent.updateTable()
		
class MyDialog(QDialog):
	def __init__(self,parent=None):
		super(MyDialog,self).__init__(parent)
		
		self.model = MyModel()
		self.tableView = QTableView()
		self.tableView.setModel(self.model)
		self.model.dialog = self
		self.delegate = MyItemDelegate(self.tableView)
		self.tableView.setItemDelegate(self.delegate)
		
		self.updatetable = True
		self.tableView.selectionModel().selectionChanged.connect(self.updateSelect)
		
		tabWidget = QTabWidget()
		tabWidget.addTab(Tab_Select_Expression(parent=self),'Select Expression')
		tabWidget.addTab(Tab_Selection_Set(parent=self),'Selection Set')
		tabWidget.addTab(Tab_Set_Value(parent=self),'Set Value')
		tabWidget.addTab(Tab_Create_Modify_Expression(parent=self),'Create/Modify Expression')
		tabWidget.addTab(Tab_File(parent=self),'File')
		tabWidget.addTab(Tab_TableOp(parent=self),'Table Operation')
		
		mainlayout = QVBoxLayout(self)
		mainlayout.addWidget(self.tableView)
		mainlayout.addWidget(tabWidget)

		self.parent = self.tableView.currentIndex()

		self.resize(320, 230)
		self.setWindowFlags(Qt.WindowStaysOnTopHint)
		self.updateTable()
		
	def updateTable(self):
		self.updatetable = False
		flag = QItemSelectionModel.Rows|QItemSelectionModel.Select
		self.model.update()
		layer_index = lx.eval('query layerservice layers ? main')
		self.tableView.selectionModel().clear()
		selverts = lx.eval('query layerservice verts ? selected')
		if selverts != None:
			for i,fidx in enumerate(self.model.filterIndex): 
				if str(fidx) in list(selverts):
					index = self.model.index(i,0)
					self.tableView.selectionModel().select(index,flag)
		self.updatetable = True
				
	def updateSelect(self):
		if self.updatetable :
			lx.command('select.clear',type='vertex')
			layer_index = lx.eval('query layerservice layers ? main')
			for index in self.tableView.selectionModel().selectedRows():
				lx.command('select.element',layer=layer_index,type='vertex',mode='add',index=self.model.filterIndex[index.row()])

	def keyPressEvent(self, event):
		super().keyPressEvent(event)
		if (event.modifiers() & Qt.ControlModifier) and event.key() == Qt.Key_Z:
			if event.modifiers() & Qt.ShiftModifier :
				lx.command('!app.redo')
			else:				
				lx.command('!app.undo')
			self.updateTable()
			
	def paintEvent(self,event):
		if not self.isActiveWindow():
			self.updateTable()	

w = MyDialog()
w.show()

続きはまた次回。



take_z_ultima at 11:30│Comments(0)modo | CG

この記事にコメントする

名前:
URL:
  情報を記憶: 評価: 顔   
 
 
 

Archives