#!/usr/bin/env python
#
# License: BSD
#   https://raw.github.com/robotics-in-concert/rocon_tutorials/license/LICENSE
#
"""
  Simulates a real dude that connects in over the concert via the rocon
  interaction handler with a qt remocon.
"""
##############################################################################
# Imports
##############################################################################

import sys
import signal
from PyQt4 import QtGui

import rospy
import std_msgs.msg as std_msgs
import rocon_python_utils
import time

##############################################################################
# Classes
##############################################################################


class Window(QtGui.QWidget):

    def __init__(self):
        super(Window, self).__init__()
        self._icon = rocon_python_utils.ros.find_resource_from_string('rocon_bubble_icons/rocon.png')
        self.initUI()

    def initUI(self):
        self._list_view = QtGui.QListWidget(self)
        self._list_view.resize(400, 400)

        self.setWindowTitle("Remocon Dude")
        self.setWindowIcon(QtGui.QIcon(self._icon))
        self.putUnderMouse()

    def putUnderMouse(self):
        mouse = QtGui.QCursor.pos()
        self.move(mouse.x() - self._list_view.geometry().width() / 2, mouse.y() - self._list_view.geometry().height() / 2)

    def listener(self, data):
        QtGui.QListWidgetItem("I heard %s" % data.data, self._list_view)
        self._list_view.scrollToBottom()
        #rospy.loginfo(rospy.get_name() + ": I heard %s" % data.data)

##############################################################################
# Main
##############################################################################

if __name__ == '__main__':

    rospy.init_node('remocon_dude')

    signal.signal(signal.SIGINT, signal.SIG_DFL)  # make sure this comes after the rospy call, otherwise it will handle signals.
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    time.sleep(0.5)  # funny, qt window disappears from focus without this.
    rospy.Subscriber('chatter', std_msgs.String, window.listener)
    sys.exit(app.exec_())
