Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <rve_qt/render_widget.h>
00031
00032 #include <rve_render_client/render_window.h>
00033 #include <rve_render_client/client_context.h>
00034
00035 #include <rve_msgs/MouseEvent.h>
00036
00037 #include <ros/node_handle.h>
00038
00039 #include <sstream>
00040 #include <QX11Info>
00041 #include <X11/Xlib.h>
00042
00043 namespace rve_qt
00044 {
00045
00046 std::string getWindowHandle(QWidget* widget)
00047 {
00048 std::stringstream ss;
00049 ss << widget->winId();
00050 XSync(QX11Info::display(), false);
00051 return ss.str();
00052 }
00053
00054 RenderWidget::RenderWidget(rve_render_client::ClientContext* context, QWidget* parent)
00055 : QWidget(parent)
00056 {
00057 QSize size = property("size").toSize();
00058 remote_wnd_ = rve_render_client::createRenderWindow(context, getWindowHandle(this), size.width(), size.height());
00059 mouse_pub_ = context->getNodeHandle().advertise<rve_msgs::MouseEvent>(getMouseEventTopic(), 100);
00060 }
00061
00062 RenderWidget::~RenderWidget()
00063 {
00064 remote_wnd_.reset();
00065 }
00066
00067 void RenderWidget::resizeEvent(QResizeEvent* event)
00068 {
00069 remote_wnd_->resize(event->size().width(), event->size().height());
00070 }
00071
00072 std::string RenderWidget::getNamespace()
00073 {
00074 return remote_wnd_->getNamespace();
00075 }
00076
00077 std::string RenderWidget::getMouseEventTopic()
00078 {
00079 return remote_wnd_->getNamespace() + "/mouse_events";
00080 }
00081
00082 uint8_t translateQtButtons(Qt::MouseButtons buttons)
00083 {
00084 uint8_t mask = 0;
00085 if (buttons & Qt::LeftButton)
00086 {
00087 mask |= rve_msgs::MouseEvent::BUTTON_LEFT;
00088 }
00089
00090 if (buttons & Qt::RightButton)
00091 {
00092 mask |= rve_msgs::MouseEvent::BUTTON_RIGHT;
00093 }
00094
00095 if (buttons & Qt::MidButton)
00096 {
00097 mask |= rve_msgs::MouseEvent::BUTTON_MIDDLE;
00098 }
00099
00100 return mask;
00101 }
00102
00103 uint8_t translateQtModifiers(Qt::KeyboardModifiers mod)
00104 {
00105 uint8_t mask = 0;
00106 if (mod & Qt::ShiftModifier)
00107 {
00108 mask |= rve_msgs::MouseEvent::MODIFIER_SHIFT;
00109 }
00110
00111 if (mod & Qt::ControlModifier)
00112 {
00113 mask |= rve_msgs::MouseEvent::MODIFIER_CONTROL;
00114 }
00115
00116 if (mod & Qt::AltModifier)
00117 {
00118 mask |= rve_msgs::MouseEvent::MODIFIER_ALT;
00119 }
00120
00121 if (mod & Qt::MetaModifier)
00122 {
00123 mask |= rve_msgs::MouseEvent::MODIFIER_META;
00124 }
00125
00126 return mask;
00127 }
00128
00129 void fillMessage(QWidget* w, QMouseEvent* evt, rve_msgs::MouseEvent& msg)
00130 {
00131 msg.button = translateQtButtons(evt->button());
00132 msg.buttons = translateQtButtons(evt->buttons());
00133 msg.x = evt->x();
00134 msg.y = evt->y();
00135
00136 QSize size = w->property("size").toSize();
00137 msg.window_width = size.width();
00138 msg.window_height = size.height();
00139
00140 msg.modifiers = translateQtModifiers(evt->modifiers());
00141 }
00142
00143 void RenderWidget::mousePressEvent(QMouseEvent* event)
00144 {
00145 rve_msgs::MouseEventPtr msg(new rve_msgs::MouseEvent);
00146 msg->type = rve_msgs::MouseEvent::EVENT_PRESS;
00147 fillMessage(this, event, *msg);
00148 mouse_pub_.publish(msg);
00149 }
00150
00151 void RenderWidget::mouseReleaseEvent(QMouseEvent* event)
00152 {
00153 rve_msgs::MouseEventPtr msg(new rve_msgs::MouseEvent);
00154 msg->type = rve_msgs::MouseEvent::EVENT_RELEASE;
00155 fillMessage(this, event, *msg);
00156 mouse_pub_.publish(msg);
00157 }
00158
00159 void RenderWidget::mouseMoveEvent(QMouseEvent* event)
00160 {
00161 rve_msgs::MouseEventPtr msg(new rve_msgs::MouseEvent);
00162 msg->type = rve_msgs::MouseEvent::EVENT_MOVE;
00163 fillMessage(this, event, *msg);
00164 mouse_pub_.publish(msg);
00165 }
00166
00167 }