<commit_before>
"""
A gui tool for editing.

Now a basic proof-of-concept and a test of the Python API:
The qt integration for ui together with the manually wrapped entity-component data API,
and the viewer non-qt event system for mouse events thru the py plugin system.

Works for selecting an object with the mouse, and then changing the position 
using the qt widgets. Is shown immediately in-world and synched over the net.

TODO (most work is in api additions on the c++ side, then simple usage here):
- local & global movement - select box?
- (WIP, needs network event refactoring) sync changes from over the net to the gui dialog: listen to scene objectupdates
  (this is needed/nice when someone else is moving the same obj at the same time,
   works correctly in slviewer, i.e. the dialogs there update on net updates)
- hilite the selected object
(- list all the objects to allow selection from there)


"""

import rexviewer as r
import PythonQt
from PythonQt.QtGui import QTreeWidgetItem, QInputDialog, QLineEdit, QLabel
from PythonQt.QtUiTools import QUiLoader
from PythonQt.QtCore import QFile, QSize
from circuits import Component

from conversions import * #for euler - quat -euler conversions
from vector3 import Vector3 #for view based editing calcs now that Vector3 not exposed from internals

try:
    import ogre.renderer.OGRE as ogre
except ImportError:
    ogreroot = False
else:
    root = ogre.Root.getSingleton()
    ogreroot = root.isInitialised()
    V3 = ogre.Vector3

#are in Naali QObject QTModule::UICanvas as QEnums, in PythonQt module now
#naali.py: UICanvas = PythontQt.__dict__['QTModule::UICanvas']
#INTERNAL = 1
#EXTERNAL = 0

OIS_KEY_ALT = 256
OIS_KEY_CTRL = 16
OIS_KEY_M = 50
OIS_KEY_S = 31
OIS_KEY_R = 19
OIS_KEY_U = 22
OIS_KEY_D = 32
OIS_KEY_Z = 44
OIS_KEY_ESC = 1
OIS_KEY_DEL = 211

PRIMTYPES = {
    "0": "Texture", 
    "45": "Material"
}

DEV = False #if this is false, the canvas is added to the controlbar

class DragDroppableEditline(QLineEdit):
    def __init__(self, mainedit, *args):
        self.mainedit = mainedit #to be able to query the selected entity at drop
        QLineEdit.__init__(self, *args)

    def accept(self, ev):
        return ev.mimeData().hasFormat("application/vnd.inventory.item")

    def dragEnterEvent(self, ev):
        if self.accept(ev):
            ev.acceptProposedAction()

    def dragMoveEvent(self, ev):
        if self.accept(ev):
            ev.acceptProposedAction()

    def dropEvent(self, ev):
        print "Got meshid_drop:", self, ev
        if not self.accept(ev):
            return

        mimedata = ev.mimeData()
        invitem = mimedata.data("application/vnd.inventory.item")

        #some_qbytearray_thing = invitem[:4] #struct.unpack('i', 
        data = invitem[4:].decode('utf-16-be') #XXX how it happens to be an win: should be explicitly encoded to latin-1 or preferrably utf-8 in the c++ inventory code
        #print data
        asset_type, inv_id, inv_name, asset_ref = data.split(';')

        ent = self.mainedit.sel #is public so no need for getter, can be changed to a property if needs a getter at some point
        if ent is not None:
            self.doaction(ent, asset_ref, asset_type)
        else:
            self.text = "(no scene entity selected)"

        ev.acceptProposedAction()

    def doaction(self, ent, ref):
        pass
        
class MeshAssetidEditline(DragDroppableEditline):
    def doaction(self, ent, ref, asset_type):
        print "doaction in MeshAssetidEditline-class..."
        applymesh(ent, asset_ref)
        self.text = inv_name
    
class UUIDEditLine(DragDroppableEditline):
    def doaction(self, ent, ref, asset_type):
        print "whee"
        
def applymesh(ent, meshuuid):
    ent.mesh = meshuuid
    #r.logDebug("Mesh asset UUID after before sending to server: %s" % ent.mesh)
    r.sendRexPrimData(ent.id)
    r.logDebug("Mesh asset UUID after prim data sent to server: %s" % ent.mesh)

class EditGUI(Component):
    EVENTHANDLED = False
    UIFILE = "pymodules/editgui/editobject.ui"
    
    MANIPULATE_NONE = 0
    MANIPULATE_MOVE = 1
    MANIPULATE_ROTATE = 2
    MANIPULATE_SCALE = 3<commit_msg>Quick typo fix<commit_after>
"""
A gui tool for editing.

Now a basic proof-of-concept and a test of the Python API:
The qt integration for ui together with the manually wrapped entity-component data API,
and the viewer non-qt event system for mouse events thru the py plugin system.

Works for selecting an object with the mouse, and then changing the position 
using the qt widgets. Is shown immediately in-world and synched over the net.

TODO (most work is in api additions on the c++ side, then simple usage here):
- local & global movement - select box?
- (WIP, needs network event refactoring) sync changes from over the net to the gui dialog: listen to scene objectupdates
  (this is needed/nice when someone else is moving the same obj at the same time,
   works correctly in slviewer, i.e. the dialogs there update on net updates)
- hilite the selected object
(- list all the objects to allow selection from there)


"""

import rexviewer as r
import PythonQt
from PythonQt.QtGui import QTreeWidgetItem, QInputDialog, QLineEdit, QLabel
from PythonQt.QtUiTools import QUiLoader
from PythonQt.QtCore import QFile, QSize
from circuits import Component

from conversions import * #for euler - quat -euler conversions
from vector3 import Vector3 #for view based editing calcs now that Vector3 not exposed from internals

try:
    import ogre.renderer.OGRE as ogre
except ImportError:
    ogreroot = False
else:
    root = ogre.Root.getSingleton()
    ogreroot = root.isInitialised()
    V3 = ogre.Vector3

#are in Naali QObject QTModule::UICanvas as QEnums, in PythonQt module now
#naali.py: UICanvas = PythontQt.__dict__['QTModule::UICanvas']
#INTERNAL = 1
#EXTERNAL = 0

OIS_KEY_ALT = 256
OIS_KEY_CTRL = 16
OIS_KEY_M = 50
OIS_KEY_S = 31
OIS_KEY_R = 19
OIS_KEY_U = 22
OIS_KEY_D = 32
OIS_KEY_Z = 44
OIS_KEY_ESC = 1
OIS_KEY_DEL = 211

PRIMTYPES = {
    "0": "Texture", 
    "45": "Material"
}

DEV = False #if this is false, the canvas is added to the controlbar

class DragDroppableEditline(QLineEdit):
    def __init__(self, mainedit, *args):
        self.mainedit = mainedit #to be able to query the selected entity at drop
        QLineEdit.__init__(self, *args)

    def accept(self, ev):
        return ev.mimeData().hasFormat("application/vnd.inventory.item")

    def dragEnterEvent(self, ev):
        if self.accept(ev):
            ev.acceptProposedAction()

    def dragMoveEvent(self, ev):
        if self.accept(ev):
            ev.acceptProposedAction()

    def dropEvent(self, ev):
        print "Got meshid_drop:", self, ev
        if not self.accept(ev):
            return

        mimedata = ev.mimeData()
        invitem = mimedata.data("application/vnd.inventory.item")

        #some_qbytearray_thing = invitem[:4] #struct.unpack('i', 
        data = invitem[4:].decode('utf-16-be') #XXX how it happens to be an win: should be explicitly encoded to latin-1 or preferrably utf-8 in the c++ inventory code
        #print data
        asset_type, inv_id, inv_name, asset_ref = data.split(';')

        ent = self.mainedit.sel #is public so no need for getter, can be changed to a property if needs a getter at some point
        if ent is not None:
            self.doaction(ent, asset_ref, asset_type)
        else:
            self.text = "(no scene entity selected)"

        ev.acceptProposedAction()

    def doaction(self, ent, ref):
        pass
        
class MeshAssetidEditline(DragDroppableEditline):
    def doaction(self, ent, ref, asset_type):
        print "doaction in MeshAssetidEditline-class..."
        applymesh(ent, ref)
        self.text = inv_name
    
class UUIDEditLine(DragDroppableEditline):
    def doaction(self, ent, ref, asset_type):
        print "whee"
        
def applymesh(ent, meshuuid):
    ent.mesh = meshuuid
    #r.logDebug("Mesh asset UUID after before sending to server: %s" % ent.mesh)
    r.sendRexPrimData(ent.id)
    r.logDebug("Mesh asset UUID after prim data sent to server: %s" % ent.mesh)

class EditGUI(Component):
    EVENTHANDLED = False
    UIFILE = "pymodules/editgui/editobject.ui"
    
    MANIPULATE_NONE = 0
    MANIPULATE_MOVE = 1
    MANIPULATE_ROTATE = 2
    MANIPULATE_SCALE = 3
{'fraction_non_alphanumeric': 0.08178438661710037, 'fraction_numerical': 0.009833313346924092, 'mean_word_length': 3.2399593289273003, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5194247', 'n_tokens_mistral': 2762, 'n_tokens_neox': 2639, 'n_words': 931}