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 makeIconCursor( QPixmap(), "rviz_default_cursor", fill_cache );
00100 }
00101
00102 QCursor makeIconCursor( QString url, bool fill_cache )
00103 {
00104 QPixmap icon = loadPixmap( url, fill_cache );
00105 QString cache_key = url + ".cursor";
00106 return makeIconCursor( icon, cache_key, fill_cache );
00107 }
00108
00109 QCursor makeIconCursor( QPixmap icon, QString cache_key, bool fill_cache )
00110 {
00111
00112 QPixmap cursor_img;
00113 if ( QPixmapCache::find( cache_key, &cursor_img ) )
00114 {
00115 return QCursor( cursor_img, 0, 0 );
00116 }
00117
00118 QPixmap base_cursor = loadPixmap( "package://rviz/icons/cursor.svg", fill_cache );
00119
00120 const int cursor_size = 32;
00121
00122 cursor_img = QPixmap( cursor_size, cursor_size );
00123 cursor_img.fill( QColor(0,0,0,0) );
00124
00125
00126 QPainter painter(&cursor_img);
00127
00128 int draw_x = 12;
00129 int draw_y = 16;
00130
00131
00132 if( draw_x+icon.width() > cursor_size )
00133 {
00134 draw_x = cursor_size-icon.width();
00135 }
00136 if( draw_y+icon.height() > cursor_size )
00137 {
00138 draw_y = cursor_size-icon.height();
00139 }
00140
00141 painter.drawPixmap( 0, 0, base_cursor );
00142 painter.drawPixmap( draw_x, draw_y, icon );
00143
00144 if ( fill_cache )
00145 {
00146 QPixmapCache::insert( cache_key, cursor_img );
00147 }
00148
00149 return QCursor( cursor_img, 1, 1 );
00150 }
00151
00152
00153
00154 }