overlay_picker_tool.cpp
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2014, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the JSK Lab nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 #include <QApplication>
00037 #include <QMenu>
00038 #include <QTimer>
00039 #include <ros/ros.h>
00040 #include <rviz/tool_manager.h>
00041 #include <rviz/display_context.h>
00042 #include <rviz/view_manager.h>
00043 #include <rviz/display_group.h>
00044 #include <rviz/display.h>
00045 
00046 #include "overlay_picker_tool.h"
00047 #include "overlay_text_display.h"
00048 #include "plotter_2d_display.h"
00049 #include "pie_chart_display.h"
00050 #include "overlay_image_display.h"
00051 #include "overlay_diagnostic_display.h"
00052 
00053 namespace jsk_rviz_plugins
00054 {
00055   OverlayPickerTool::OverlayPickerTool()
00056     : is_moving_(false), shift_pressing_(false), rviz::Tool()
00057   {
00058 
00059   }
00060 
00061   int OverlayPickerTool::processKeyEvent(QKeyEvent* event, rviz::RenderPanel* panel)
00062   {
00063     if (event->type() == QEvent::KeyPress && event->key() == 16777248) { // sift
00064       shift_pressing_ = true;
00065     }
00066     else if (event->type() == QEvent::KeyRelease && event->key() == 16777248) {
00067       shift_pressing_ = false;
00068     }
00069     return 0;
00070   }
00071   
00072   
00073   int OverlayPickerTool::processMouseEvent(rviz::ViewportMouseEvent& event)
00074   {
00075     if (event.left() && event.leftDown()) {
00076       if (!is_moving_) {
00077         onClicked(event);
00078       }
00079     }
00080     else if (event.left() && is_moving_) {
00081       onMove(event);
00082     }
00083     else if (is_moving_ && !(event.left() && event.leftDown())) {
00084       onRelease(event);
00085     }
00086     return 0;
00087   }
00088 
00089   bool OverlayPickerTool::handleDisplayClick(rviz::Property* property, rviz::ViewportMouseEvent& event)
00090   {
00091     if (isPropertyType<rviz::DisplayGroup>(property)) {
00092       rviz::DisplayGroup* group_property = isPropertyType<rviz::DisplayGroup>(property);
00093       for (int i = 0; i < group_property->numChildren(); i++) {
00094         if (handleDisplayClick(group_property->childAt(i), event)) {
00095           return true;
00096         }
00097       }
00098     }
00099     else {
00100       if (startMovement<OverlayTextDisplay>(property, event, "overlay_text_display")) {
00101         return true;
00102       }
00103       else if (startMovement<Plotter2DDisplay>(property, event, "plotter_2d_display")) {
00104         return true;
00105       }
00106       else if (startMovement<PieChartDisplay>(property, event, "pie_chart_display")) {
00107         return true;
00108       }
00109       else if (startMovement<OverlayImageDisplay>(property, event, "overlay_image_display")) {
00110         return true;
00111       }
00112       else if (startMovement<OverlayDiagnosticDisplay>(property, event, "overlay_diagnostic_display")) {
00113         return true;
00114       }
00115       else {
00116         return false;
00117       }
00118     }
00119     return false;
00120   }
00121 
00122   void OverlayPickerTool::onClicked(rviz::ViewportMouseEvent& event)
00123   {
00124     ROS_DEBUG("onClicked");
00125     is_moving_ = true;
00126     ROS_DEBUG("clicked: (%d, %d)", event.x, event.y);
00127     // check the active overlay plugin
00128     rviz::DisplayGroup* display_group = context_->getRootDisplayGroup();
00129     handleDisplayClick(display_group, event);
00130   }
00131 
00132   void OverlayPickerTool::onMove(rviz::ViewportMouseEvent& event)
00133   {
00134     ROS_DEBUG("onMove");
00135     ROS_DEBUG("moving: (%d, %d)", event.x, event.y);
00136     if (target_property_) {
00137       if (target_property_type_ == "overlay_text_display") {
00138         movePosition<OverlayTextDisplay>(event);
00139       }
00140       else if (target_property_type_ == "plotter_2d_display") {
00141         movePosition<Plotter2DDisplay>(event);
00142       }
00143       else if (target_property_type_ == "pie_chart_display") {
00144         movePosition<PieChartDisplay>(event);
00145       }
00146       else if (target_property_type_ == "overlay_image_display") {
00147         movePosition<OverlayImageDisplay>(event);
00148       }
00149       else if (target_property_type_ == "overlay_diagnostic_display") {
00150         movePosition<OverlayDiagnosticDisplay>(event);
00151       }
00152     }
00153   }
00154   
00155   void OverlayPickerTool::onRelease(rviz::ViewportMouseEvent& event)
00156   {
00157     ROS_DEBUG("onRelease");
00158     is_moving_ = false;
00159     ROS_DEBUG("released: (%d, %d)", event.x, event.y);
00160     if (target_property_) {
00161       if (target_property_type_ == "overlay_text_display") {
00162         setPosition<OverlayTextDisplay>(event);
00163       }
00164       else if (target_property_type_ == "plotter_2d_display") {
00165         setPosition<Plotter2DDisplay>(event);
00166       }
00167       else if (target_property_type_ == "pie_chart_display") {
00168         setPosition<PieChartDisplay>(event);
00169       }
00170       else if (target_property_type_ == "overlay_image_display") {
00171         setPosition<OverlayImageDisplay>(event);
00172       }
00173       else if (target_property_type_ == "overlay_diagnostic_display") {
00174         setPosition<OverlayDiagnosticDisplay>(event);
00175       }
00176     }
00177     // clear cache
00178     target_property_ = NULL;
00179     target_property_type_ = "";
00180   }
00181   
00182 }
00183 
00184 #include <pluginlib/class_list_macros.h>
00185 PLUGINLIB_EXPORT_CLASS( jsk_rviz_plugins::OverlayPickerTool, rviz::Tool )


jsk_rviz_plugins
Author(s): Kei Okada , Yohei Kakiuchi , Shohei Fujii , Ryohei Ueda
autogenerated on Wed May 1 2019 02:40:22