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 "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
00071 if ( QPixmapCache::find( url, &pixmap ) )
00072 {
00073 return pixmap;
00074 }
00075
00076 boost::filesystem::path path = getPath( url );
00077
00078
00079
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
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
00131 QPainter painter(&cursor_img);
00132
00133 int draw_x = 12;
00134 int draw_y = 16;
00135
00136
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 }