Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class("TreeWidget", Lua__QTreeWidget)
- icons = {}
- icons[Scene.TYPE_LIGHT] = "editor/icons/icon_light.png"
- icons[Scene.TYPE_MESH] = "editor/icons/icon_mesh.png"
- icons[Scene.TYPE_SCENE] = "editor/icons/icon_node_scene.png"
- icons[Scene.TYPE_BONE] = "editor/icons/icon_bone.png"
- function TreeWidget:load_scene(scene, parent, owned, root)
- local item = QTreeWidgetItem:new(parent)
- local sname = scene:get_name()
- item:setText(0, sname)
- local type = scene:get_type()
- if icons[scene:get_type()] then
- item:setIcon(0, QIcon(icons[scene:get_type()]))
- end
- item.node = scene
- if root and not owned then
- owned = scene:get_foreign_parent() and (scene:get_foreign_parent() == root)
- end
- item.owned = owned
- if item.owned then
- if root then
- item:setFlags(LuaQt.bw_or(Qt.ItemIsSelectable, Qt.ItemIsEnabled, Qt.ItemIsDragEnabled, Qt.ItemIsDropEnabled))
- else
- item:setFlags(LuaQt.bw_or(Qt.ItemIsSelectable, Qt.ItemIsEnabled, Qt.ItemIsDropEnabled))
- end
- else
- item:setTextColor(0, QColor(255, 0, 0))
- item:setFlags(LuaQt.bw_or(Qt.ItemIsSelectable, Qt.ItemIsEnabled, Qt.ItemIsDropEnabled))
- end
- if self.names[sname] then
- table.insert(self.names[sname], item)
- else
- self.names[sname] = {item}
- end
- local pname = Scene.Node:get_node_path(scene)
- self.path_names[pname] = item
- if not root then
- root = scene
- elseif scene:get_type() == Scene.TYPE_SCENE and scene:get_resource() ~= "" then
- -- childs will not be owned
- owned = false
- end
- for i=0,scene:get_child_count()-1 do
- self:load_scene(scene:get_child(i), item, owned, root)
- end
- return item
- end
- function TreeWidget:set_object_menu(menu)
- self.object_menu = menu
- --menu:setParent(self)
- --menu:hide()
- end
- function TreeWidget:contextMenuEvent(e)
- if self.object_menu then
- self.object_menu:popup(QPoint(e:globalX(), e:globalY()))
- e:accept()
- end
- end
- function TreeWidget:dropEvent(e)
- if e:source() == self then
- e:accept()
- local newparent = self:itemAt(e:pos())
- if newparent then
- local node = self:currentItem()
- if node.node ~= newparent.node then
- self:reparent(node, newparent)
- end
- end
- end
- end
- function TreeWidget:reparent(node, parent)
- local mat = node.node:get_global_matrix()
- if parent.node:add_child(node.node) then
- QMessageBox:critical(self, "Error!", "Invalid parent for node")
- return
- end
- self.scene:remove_foreign_node(node.node)
- if not node.node:inside_scene() then
- self.scene:add_foreign_node(node.node)
- end
- node.node:set_global_matrix(mat)
- self:reload_scene()
- end
- function TreeWidget:expand_all()
- for n,l in pairs(self.names) do
- for k,v in ipairs(l) do
- self:expandItem(v)
- end
- end
- end
- function TreeWidget:clear()
- self.names = {}
- self.path_names = {}
- GUI.List.clear(self)
- end
- function TreeWidget:set_scene(scene)
- self:clear()
- self.scene = scene
- local root = self:load_scene(scene, self, true)
- self:setCurrentItem(root)
- end
- function TreeWidget:reload_scene()
- local pname
- local citem = self:currentItem()
- if citem then
- pname = Scene.Node:get_node_path(citem.node)
- end
- self:clear()
- self:load_scene(self.scene, self, true)
- if pname and self.path_names[pname] then
- self:setCurrentItem(self.path_names[pname])
- local item = self.path_names[pname]
- while item do
- self:setItemExpanded(item, true)
- item = item:parent()
- end
- end
- end
- function TreeWidget:item_selected(current, prev)
- self.node_selected:emit(current)
- end
- function TreeWidget:item_changed(item, col)
- --if col == 0 and item.node then
- -- item.node:set_name(item:text(0))
- -- self:reload_scene()
- -- self:expand_all()
- --end
- end
- function TreeWidget:__init__(parent)
- self.names = {}
- self:setColumnCount(1)
- self.node_selected = LuaSignal:new(1)
- self:connect(self, "currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)", self, self.item_selected)
- self:connect(self, "itemChanged(QTreeWidgetItem*, int)", self, self.item_changed)
- self:setMinimumWidth(200)
- self:setProperty("dragEnabled", true)
- self:setProperty("acceptDrops", true)
- self:setDropIndicatorShown(true)
- end
- return TreeWidget
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement