<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import rospkg
import rospy
import time

from python_qt_binding.QtCore import Qt
from python_qt_binding import loadUi
from rqt_gui_py.plugin import Plugin
from python_qt_binding.QtWidgets import QWidget, QTreeWidget, QTreeWidgetItem, QSlider, QGroupBox, QVBoxLayout, QLabel, QLineEdit
from python_qt_binding.QtGui import QDoubleValidator

from sensor_msgs.msg import JointState
from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint

import os

from .animation_recording import Recorder


class RecordUI(Plugin):
    def __init__(self, context):
        print "super"
        super(RecordUI, self).__init__(context)


        self._widget = QWidget()
        print "thingies"
        rp = rospkg.RosPack()
        ui_file = os.path.join(rp.get_path('bitbots_recordui_rqt'), 'resource', 'RecordUI.ui')
        print "load ui"
        loadUi(ui_file, self._widget, {})

        self._recorder = Recorder()
        self._sliders = {}
        self._textFields = {}
        self._motorValues = {}
        self._motorSwitched = {}

        self._treeItems = {}
        self._motorCheckBody = QTreeWidgetItem(self._widget.motorTree)
        self._motorCheckLegs = QTreeWidgetItem(self._motorCheckBody)
        self._motorCheckArms = QTreeWidgetItem(self._motorCheckBody)
        self._motorCheckHead = QTreeWidgetItem(self._motorCheckBody)
        self._motorCheckLArm = QTreeWidgetItem(self._motorCheckArms)
        self._motorCheckRArm = QTreeWidgetItem(self._motorCheckArms)
        self._motorCheckLLeg = QTreeWidgetItem(self._motorCheckLegs)
        self._motorCheckRLeg = QTreeWidgetItem(self._motorCheckLegs)

        self.setObjectName('Record Animation')

        self._initial_joints = None


        rospy.Subscriber("/joint_states", JointState, self.state_update, queue_size=100)
        self._joint_pub = rospy.Publisher("/motor_goals", JointTrajectory, queue_size=1)

        while not self._initial_joints:
            if not rospy.is_shutdown():
                time.sleep(0.5)
                print "wait"
            else:
                return

        self.initialize()


        context.add_widget(self._widget)

    def initialize(self):
        self.motor_controller()
        self.motor_switcher()
        for i in range(0, len(self._initial_joints.name)):
            self._motorSwitched[self._initial_joints.name[i]] = True
            self._textFields[self._initial_joints.name[i]].setText(str(int(self._initial_joints.position[i]*180/3.14)))
        self.button_connect()
        self.box_ticked()


    def state_update(self, joint_states):
        if not self._initial_joints:
            self._initial_joints = joint_states
            time.sleep(1)

        for i in range(0, len(joint_states.name)):
            self._motorValues[joint_states.name[i]] = joint_states.position[i]

        for k, v in self._motorValues.\
                items():
            if not self._motorSwitched[k]:
                self._sliders[k].setValue(v*180/3.14)

    def motor_controller(self):
        for i in range(0, len(self._initial_joints.name)):
            self._motorValues[self._initial_joints.name[i]] = self._initial_joints.position[i]

        i = 0
        for k, v in self._motorValues.items():
            group = QGroupBox()
            slider = QSlider(Qt.Horizontal)
            slider.setTickInterval(1)
            slider.setMinimum(-180)
            slider.setMaximum(180)
            slider.valueChanged.connect(self.slider_update)
            self._sliders[k] = slider

            textfield = QLineEdit()
            textfield.setText('0')
            textfield.textEdited.connect(self.textfield_update)
            textfield.setValidator(QDoubleValidator(-180.0, 180.0, 2))
            self._textFields[k] = textfield

            label = QLabel()
            label.setText(k)

            layout = QVBoxLayout()
            layout.addWidget(label)
            layout.addWidget(self._sliders[k])
            layout.addWidget(self._textFields[k])
            group.setLayout(layout)
            self._widget.motorControlLayout.addWidget(group, i / 5, i % 5)
            i = i+1

    def button_connect(self):
        self._widget.buttonNew.clicked.connect(self.new)
        self._widget.buttonSave.clicked.connect(self.save)
        self._widget.buttonSaveAs.clicked.connect(self.save_as)
        self._widget.buttonSaveAs.clicked.connect(self.save_as)


        self._widget.buttonPlay.clicked.connect(self.play)
        self._widget.buttonGotoFrame.clicked.connect(self.goto_frame)
        self._widget.buttonRecord.clicked.connect(self.record)

        self._widget.buttonUndo.clicked.connect(self.undo)
        self._widget.buttonRedo.clicked.connect(self.redo)

    def new(self):
        raise NotImplementedError

    def save(self):
        raise NotImplementedError

    def save_as(self):
        raise NotImplementedError

    def open(self):
        raise NotImplementedError

    def play(self):
        raise NotImplementedError

    def goto_frame(self):
        raise NotImplementedError

    def record(self):
        self._recorder.record()

    def undo(self):
        raise NotImplementedError

    def redo(self):
        raise NotImplementedError

    def motor_switcher(self):
        self._widget.motorTree.setHeaderLabel("Motors")
        self._motorCheckBody.setText(0, "Body")
        self._motorCheckBody.setFlags(self._motorCheckBody.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
        self._motorCheckBody.setExpanded(True)
        self._motorCheckHead.setText(0, "Head")<commit_msg>record ui<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import rospkg
import rospy
import time

from python_qt_binding.QtCore import Qt
from python_qt_binding import loadUi
from rqt_gui_py.plugin import Plugin
from python_qt_binding.QtWidgets import QWidget, QTreeWidget, QTreeWidgetItem, QSlider, QGroupBox, QVBoxLayout, QLabel, QLineEdit
from python_qt_binding.QtGui import QDoubleValidator

from sensor_msgs.msg import JointState
from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint

import os

from .animation_recording import Recorder


class RecordUI(Plugin):
    def __init__(self, context):
        print "super"
        super(RecordUI, self).__init__(context)


        self._widget = QWidget()
        print "thingies"
        rp = rospkg.RosPack()
        ui_file = os.path.join(rp.get_path('bitbots_recordui_rqt'), 'resource', 'RecordUI.ui')
        print "load ui"
        loadUi(ui_file, self._widget, {})

        self._recorder = Recorder()
        self._sliders = {}
        self._textFields = {}
        self._motorValues = {}
        self._motorSwitched = {}

        self._treeItems = {}
        self._motorCheckBody = QTreeWidgetItem(self._widget.motorTree)
        self._motorCheckLegs = QTreeWidgetItem(self._motorCheckBody)
        self._motorCheckArms = QTreeWidgetItem(self._motorCheckBody)
        self._motorCheckHead = QTreeWidgetItem(self._motorCheckBody)
        self._motorCheckLArm = QTreeWidgetItem(self._motorCheckArms)
        self._motorCheckRArm = QTreeWidgetItem(self._motorCheckArms)
        self._motorCheckLLeg = QTreeWidgetItem(self._motorCheckLegs)
        self._motorCheckRLeg = QTreeWidgetItem(self._motorCheckLegs)

        self.setObjectName('Record Animation')

        self._initial_joints = None


        rospy.Subscriber("/joint_states", JointState, self.state_update, queue_size=100)
        self._joint_pub = rospy.Publisher("/motor_goals", JointTrajectory, queue_size=1)

        while not self._initial_joints:
            if not rospy.is_shutdown():
                time.sleep(0.5)
                print "wait"
            else:
                return

        self.initialize()


        context.add_widget(self._widget)

    def initialize(self):
        self.motor_controller()
        self.motor_switcher()
        for i in range(0, len(self._initial_joints.name)):
            self._motorSwitched[self._initial_joints.name[i]] = True
            self._textFields[self._initial_joints.name[i]].setText(str(int(self._initial_joints.position[i]*180/3.14)))
        self.button_connect()
        self.box_ticked()


    def state_update(self, joint_states):
        if not self._initial_joints:
            self._initial_joints = joint_states
            time.sleep(1)

        for i in range(0, len(joint_states.name)):
            if not self._motorSwitched[joint_states.name[i]]:
                self._motorValues[joint_states.name[i]] = joint_states.position[i]

        for k, v in self._motorValues.items():
            if not self._motorSwitched[k]:
                self._sliders[k].setValue(v*180/3.14)

    def motor_controller(self):
        for i in range(0, len(self._initial_joints.name)):
            self._motorValues[self._initial_joints.name[i]] = self._initial_joints.position[i]

        i = 0
        for k, v in self._motorValues.items():
            group = QGroupBox()
            slider = QSlider(Qt.Horizontal)
            slider.setTickInterval(1)
            slider.setMinimum(-180)
            slider.setMaximum(180)
            slider.valueChanged.connect(self.slider_update)
            self._sliders[k] = slider

            textfield = QLineEdit()
            textfield.setText('0')
            textfield.textEdited.connect(self.textfield_update)
            textfield.setValidator(QDoubleValidator(-180.0, 180.0, 2))
            self._textFields[k] = textfield

            label = QLabel()
            label.setText(k)

            layout = QVBoxLayout()
            layout.addWidget(label)
            layout.addWidget(self._sliders[k])
            layout.addWidget(self._textFields[k])
            group.setLayout(layout)
            self._widget.motorControlLayout.addWidget(group, i / 5, i % 5)
            i = i+1

    def button_connect(self):
        self._widget.buttonNew.clicked.connect(self.new)
        self._widget.buttonSave.clicked.connect(self.save)
        self._widget.buttonSaveAs.clicked.connect(self.save_as)
        self._widget.buttonSaveAs.clicked.connect(self.save_as)


        self._widget.buttonPlay.clicked.connect(self.play)
        self._widget.buttonGotoFrame.clicked.connect(self.goto_frame)
        self._widget.buttonRecord.clicked.connect(self.record)

        self._widget.buttonUndo.clicked.connect(self.undo)
        self._widget.buttonRedo.clicked.connect(self.redo)

    def new(self):
        raise NotImplementedError

    def save(self):
        raise NotImplementedError

    def save_as(self):
        raise NotImplementedError

    def open(self):
        raise NotImplementedError

    def play(self):
        raise NotImplementedError

    def goto_frame(self):
        raise NotImplementedError

    def record(self):
        print self._motorValues["RAnklePitch"]
        print self._recorder.record(self._motorValues,self._widget.lineFrameName.text(), 1, 0) #todo time and pause

    def undo(self):
        raise NotImplementedError

    def redo(self):
        raise NotImplementedError

    def motor_switcher(self):
        self._widget.motorTree.setHeaderLabel("Motors")
        self._motorCheckBody.setText(0, "Body")
        self._motorCheckBody.setFlags(self._motorCheckBody.flags() | Qt.ItemIsTristate | Qt.ItemIsUserCheckable)
        self._motorCheckBody.setExpanded(True)
        self._motorCheckHead.setText(0, "Head")
{'fraction_non_alphanumeric': 0.0988722790453711, 'fraction_numerical': 0.008392341987936008, 'mean_word_length': 2.6467963021995535, 'pattern_counts': {'":': 0, '<': 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': '7247066', 'n_tokens_mistral': 3618, 'n_tokens_neox': 3389, 'n_words': 555}