importLink.FCMacro 3.09 KB
Newer Older
Anton Fosselius's avatar
Anton Fosselius committed
1 2 3 4 5 6 7 8 9 10 11 12 13
from PySide import QtGui, QtCore

class ExampleModalGuiClass(QtGui.QDialog):
	""""""
	def __init__(self):
		super(ExampleModalGuiClass, self).__init__()
		self.initUI()

	def reloadList(self):
		# Find all PartDesign::Body inside all open documents
		self.model.clear()
		self.bodyList = []
		for documents in App.listDocuments().values():
Anton Fosselius's avatar
Anton Fosselius committed
14 15
			objectList = documents.findObjects("PartDesign::Body")
			objectList += documents.findObjects("App::Part")
Anton Fosselius's avatar
Anton Fosselius committed
16
			#print documents.Label
Anton Fosselius's avatar
Anton Fosselius committed
17
			for body in objectList:
Anton Fosselius's avatar
Anton Fosselius committed
18 19 20
				#print documents.Label + " " + body.Label 
				self.bodyList.append(body)
				item = QtGui.QStandardItem(documents.Label + "->" + body.Label)
Anton Fosselius's avatar
Anton Fosselius committed
21
				item.setIcon(body.ViewObject.Icon)
Anton Fosselius's avatar
Anton Fosselius committed
22 23 24 25 26 27 28 29 30 31
			    # Add a checkbox to it
				item.setCheckable(False)
			 
			    # Add the item to the model
				self.model.appendRow(item)

	def initUI(self):

		# Our main window will be a QListView
		self.list = QtGui.QListView(self)
Anton Fosselius's avatar
Anton Fosselius committed
32 33 34
		self.list.move(10,40)
		self.setWindowTitle('Create Link')
		self.setMinimumSize(400, 400)
Anton Fosselius's avatar
Anton Fosselius committed
35 36 37
 
		# Create an empty model for the list's data
		self.model = QtGui.QStandardItemModel(self)
Anton Fosselius's avatar
Anton Fosselius committed
38
		self.list.setMinimumSize(380, 300)
Anton Fosselius's avatar
Anton Fosselius committed
39
		self.textInput = QtGui.QLineEdit(self)
Anton Fosselius's avatar
Anton Fosselius committed
40 41
		self.textInput.setMinimumSize(380, 0)
		self.textInput.move(10,10)
Anton Fosselius's avatar
Anton Fosselius committed
42 43
		self.reloadList()

Anton Fosselius's avatar
Anton Fosselius committed
44 45 46 47
		self.linkNameInput = QtGui.QLineEdit(self)
		self.linkNameInput.setMinimumSize(200, 0)
		self.linkNameInput.move(10, 350)

Anton Fosselius's avatar
Anton Fosselius committed
48 49 50 51
		#create link button
		# toggle visibility button
		self.createLinkButton = QtGui.QPushButton('Create Link', self)
		self.createLinkButton.setAutoDefault(False)
Anton Fosselius's avatar
Anton Fosselius committed
52
		self.createLinkButton.move(210, 350)
Anton Fosselius's avatar
Anton Fosselius committed
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

		self.model.itemChanged.connect(self.on_item_changed)
		self.textInput.textChanged.connect(self.on_text_changed)
		self.createLinkButton.clicked.connect(self.onCreateLink)

		# Apply the model to the list view
		self.list.setModel(self.model)
		# Show the window and run the app
		self.list.show()

	def onCreateLink(self):
		print "create link"
		#for item in self.model.data():
		#	print item
		selected = self.list.selectedIndexes()
		for sel in selected:
			itemSelected = self.model.item(sel.row()).text()
			document,body = itemSelected.split('->')
			print "import " + body + " from " + document
				# create the App::Link to the previously selected model
			#App.activeDocument().getObject('Model').newObject( 'App::Link', linkName ).LinkedObject = model
			
				# create an App::Placement for that object
			#App.ActiveDocument.getObject('Constraints').newObject( 'App::Placement', 'PLMT_'+linkName )

	def on_item_changed(self,item):
	    # If the changed item is not checked, don't bother checking others
		if not item.checkState():
			return
	 
	    # Loop through the items until you get None, which
	    # means you've passed the end of the list
		i = 0
		while self.model.item(i):
			if not self.model.item(i).checkState():
				return
			i += 1
		 
	def on_text_changed(self,text):
		#todo filter on text
		self.reloadList()
		filteredText = self.model.findItems(text,QtCore.Qt.MatchFlag.MatchContains)
		if len(filteredText):
			self.model.clear()
			for label in filteredText:
				self.model.appendRow(label)


form = ExampleModalGuiClass()
form.exec_()