load_resource.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, 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 "load_resource.h"
00031 
00032 #include <boost/filesystem.hpp>
00033 #include <ros/package.h>
00034 #include <ros/ros.h>
00035 
00036 #include <QPixmapCache>
00037 #include <QPainter>
00038 
00039 namespace rviz
00040 {
00041 
00042 boost::filesystem::path getPath( QString url )
00043 {
00044   boost::filesystem::path path;
00045 
00046   if ( url.indexOf("package://", 0, Qt::CaseInsensitive) == 0 )
00047   {
00048     QString package_name = url.section('/',2,2);
00049     QString file_name = url.section('/',3);
00050     path = ros::package::getPath(package_name.toStdString());
00051     path = path / file_name.toStdString();
00052   }
00053   else if ( url.indexOf("file://", 0, Qt::CaseInsensitive) == 0 )
00054   {
00055     path = url.section('/',2).toStdString();
00056   }
00057   else
00058   {
00059     ROS_ERROR( "Invalid or unsupported URL: '%s'", url.toStdString().c_str() );
00060   }
00061 
00062   return path;
00063 }
00064 
00065 
00066 QPixmap loadPixmap( QString url, bool fill_cache )
00067 {
00068   QPixmap pixmap;
00069 
00070   // if it's in the cache, no need to locate
00071   if ( QPixmapCache::find( url, &pixmap ) )
00072   {
00073     return pixmap;
00074   }
00075 
00076   boost::filesystem::path path = getPath( url );
00077 
00078   // If something goes wrong here, we go on and store the empty pixmap,
00079   // so the error won't appear again anytime soon.
00080   if ( boost::filesystem::exists( path ) )
00081   {
00082     ROS_DEBUG_NAMED( "load_resource", "Loading '%s'", path.string().c_str() );
00083     if ( !pixmap.load( QString::fromStdString( path.string() ) ) )
00084     {
00085       ROS_ERROR( "Could not load pixmap '%s'", path.string().c_str() );
00086     }
00087   }
00088 
00089   if ( fill_cache )
00090   {
00091     QPixmapCache::insert( url, pixmap );
00092   }
00093 
00094   return pixmap;
00095 }
00096 
00097 QCursor getDefaultCursor( bool fill_cache )
00098 {
00099   return QCursor(Qt::ArrowCursor);
00100 }
00101 
00102 QCursor makeIconCursor( QString url, bool fill_cache )
00103 {
00104   QPixmap icon = loadPixmap( url, fill_cache );
00105   if (icon.width() == 0 || icon.height() == 0)
00106   {
00107     ROS_ERROR( "Could not load pixmap '%s' -- using default cursor instead.", url.toStdString().c_str() );
00108     return getDefaultCursor();
00109   }
00110   QString cache_key = url + ".cursor";
00111   return makeIconCursor( icon, cache_key, fill_cache );
00112 }
00113 
00114 QCursor makeIconCursor( QPixmap icon, QString cache_key, bool fill_cache )
00115 {
00116   // if it's in the cache, no need to locate
00117   QPixmap cursor_img;
00118   if ( QPixmapCache::find( cache_key, &cursor_img ) )
00119   {
00120     return QCursor( cursor_img, 0, 0 );
00121   }
00122 
00123   QPixmap base_cursor = loadPixmap( "package://rviz/icons/cursor.svg", fill_cache );
00124 
00125   const int cursor_size = 32;
00126 
00127   cursor_img = QPixmap( cursor_size, cursor_size );
00128   cursor_img.fill( QColor(0,0,0,0) );
00129 
00130   // copy base cursor & icon into one image
00131   QPainter painter(&cursor_img);
00132 
00133   int draw_x = 12;
00134   int draw_y = 16;
00135 
00136   // if the icon is too large, move it to the left
00137   if( draw_x+icon.width() > cursor_size )
00138   {
00139     draw_x = cursor_size-icon.width();
00140   }
00141   if( draw_y+icon.height() > cursor_size )
00142   {
00143     draw_y = cursor_size-icon.height();
00144   }
00145 
00146   painter.drawPixmap( 0, 0, base_cursor );
00147   painter.drawPixmap( draw_x, draw_y, icon );
00148 
00149   if ( fill_cache )
00150   {
00151     QPixmapCache::insert( cache_key, cursor_img );
00152   }
00153 
00154   return QCursor( cursor_img, 1, 1 );
00155 }
00156 
00157 
00158 
00159 }


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Tue Oct 3 2017 03:19:30