Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. class("TreeWidget", Lua__QTreeWidget)
  2.  
  3. icons = {}
  4. icons[Scene.TYPE_LIGHT] = "editor/icons/icon_light.png"
  5. icons[Scene.TYPE_MESH] = "editor/icons/icon_mesh.png"
  6. icons[Scene.TYPE_SCENE] = "editor/icons/icon_node_scene.png"
  7. icons[Scene.TYPE_BONE] = "editor/icons/icon_bone.png"
  8.  
  9. function TreeWidget:load_scene(scene, parent, owned, root)
  10.  
  11.     local item = QTreeWidgetItem:new(parent)
  12.     local sname = scene:get_name()
  13.     item:setText(0, sname)
  14.  
  15.     local type = scene:get_type()
  16.  
  17.     if icons[scene:get_type()] then
  18.         item:setIcon(0, QIcon(icons[scene:get_type()]))
  19.     end
  20.  
  21.     item.node = scene
  22.     if root and not owned then
  23.         owned = scene:get_foreign_parent() and (scene:get_foreign_parent() == root)
  24.     end
  25.     item.owned = owned
  26.     if item.owned then
  27.         if root then
  28.             item:setFlags(LuaQt.bw_or(Qt.ItemIsSelectable, Qt.ItemIsEnabled, Qt.ItemIsDragEnabled, Qt.ItemIsDropEnabled))
  29.         else
  30.             item:setFlags(LuaQt.bw_or(Qt.ItemIsSelectable, Qt.ItemIsEnabled, Qt.ItemIsDropEnabled))
  31.         end
  32.     else
  33.         item:setTextColor(0, QColor(255, 0, 0))
  34.         item:setFlags(LuaQt.bw_or(Qt.ItemIsSelectable, Qt.ItemIsEnabled, Qt.ItemIsDropEnabled))
  35.     end
  36.  
  37.     if self.names[sname] then
  38.         table.insert(self.names[sname], item)
  39.     else
  40.         self.names[sname] = {item}
  41.     end
  42.    
  43.     local pname = Scene.Node:get_node_path(scene)
  44.     self.path_names[pname] = item
  45.  
  46.     if not root then
  47.         root = scene
  48.     elseif scene:get_type() == Scene.TYPE_SCENE and scene:get_resource() ~= "" then
  49.         -- childs will not be owned
  50.         owned = false
  51.     end
  52.  
  53.     for i=0,scene:get_child_count()-1 do
  54.         self:load_scene(scene:get_child(i), item, owned, root)
  55.     end
  56.     return item
  57. end
  58.  
  59. function TreeWidget:set_object_menu(menu)
  60.  
  61.     self.object_menu = menu
  62.     --menu:setParent(self)
  63.     --menu:hide()
  64. end
  65.  
  66. function TreeWidget:contextMenuEvent(e)
  67.  
  68.     if self.object_menu then
  69.  
  70.         self.object_menu:popup(QPoint(e:globalX(), e:globalY()))
  71.         e:accept()
  72.     end
  73. end
  74.  
  75. function TreeWidget:dropEvent(e)
  76.  
  77.     if e:source() == self then
  78.        
  79.         e:accept()
  80.         local newparent = self:itemAt(e:pos())
  81.         if newparent then
  82.             local node = self:currentItem()
  83.             if node.node ~= newparent.node then
  84.                 self:reparent(node, newparent)
  85.             end
  86.         end
  87.     end
  88. end
  89.  
  90. function TreeWidget:reparent(node, parent)
  91.  
  92.     local mat = node.node:get_global_matrix()
  93.     if parent.node:add_child(node.node) then
  94.         QMessageBox:critical(self, "Error!", "Invalid parent for node")
  95.         return
  96.     end
  97.     self.scene:remove_foreign_node(node.node)
  98.     if not node.node:inside_scene() then
  99.         self.scene:add_foreign_node(node.node)
  100.     end
  101.     node.node:set_global_matrix(mat)
  102.     self:reload_scene()
  103. end
  104.  
  105. function TreeWidget:expand_all()
  106.  
  107.     for n,l in pairs(self.names) do
  108.         for k,v in ipairs(l) do
  109.             self:expandItem(v)
  110.         end
  111.     end
  112. end
  113.  
  114. function TreeWidget:clear()
  115.  
  116.     self.names = {}
  117.     self.path_names = {}
  118.     GUI.List.clear(self)
  119. end
  120.  
  121. function TreeWidget:set_scene(scene)
  122.     self:clear()
  123.     self.scene = scene
  124.     local root = self:load_scene(scene, self, true)
  125.     self:setCurrentItem(root)
  126. end
  127.  
  128. function TreeWidget:reload_scene()
  129.  
  130.     local pname
  131.  
  132.     local citem = self:currentItem()
  133.     if citem then
  134.         pname = Scene.Node:get_node_path(citem.node)
  135.     end
  136.  
  137.     self:clear()
  138.     self:load_scene(self.scene, self, true)
  139.  
  140.     if pname and self.path_names[pname] then
  141.         self:setCurrentItem(self.path_names[pname])
  142.         local item = self.path_names[pname]
  143.         while item do
  144.             self:setItemExpanded(item, true)
  145.             item = item:parent()
  146.         end
  147.     end
  148. end
  149.  
  150. function TreeWidget:item_selected(current, prev)
  151.     self.node_selected:emit(current)
  152. end
  153.  
  154. function TreeWidget:item_changed(item, col)
  155.     --if col == 0 and item.node then
  156.     --  item.node:set_name(item:text(0))
  157.     --  self:reload_scene()
  158.     --  self:expand_all()
  159.     --end
  160. end
  161.  
  162. function TreeWidget:__init__(parent)
  163.  
  164.     self.names = {}
  165.     self:setColumnCount(1)
  166.  
  167.     self.node_selected = LuaSignal:new(1)
  168.     self:connect(self, "currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)", self, self.item_selected)
  169.  
  170.     self:connect(self, "itemChanged(QTreeWidgetItem*, int)", self, self.item_changed)
  171.    
  172.     self:setMinimumWidth(200)
  173.  
  174.     self:setProperty("dragEnabled", true)
  175.     self:setProperty("acceptDrops", true)
  176.  
  177.     self:setDropIndicatorShown(true)
  178. end
  179.  
  180. return TreeWidget
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement