render_widget.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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 } // namespace rve_qt


rve_qt
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:32:03