load_resource.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "load_resource.h"
31 
32 #include <boost/filesystem.hpp>
33 #include <ros/package.h>
34 #include <ros/ros.h>
35 
36 #include <QPixmapCache>
37 #include <QPainter>
38 
39 namespace rviz
40 {
41 
42 boost::filesystem::path getPath( QString url )
43 {
44  boost::filesystem::path path;
45 
46  if ( url.indexOf("package://", 0, Qt::CaseInsensitive) == 0 )
47  {
48  QString package_name = url.section('/',2,2);
49  QString file_name = url.section('/',3);
50  path = ros::package::getPath(package_name.toStdString());
51  path = path / file_name.toStdString();
52  }
53  else if ( url.indexOf("file://", 0, Qt::CaseInsensitive) == 0 )
54  {
55  path = url.section('/',2).toStdString();
56  }
57  else
58  {
59  ROS_ERROR( "Invalid or unsupported URL: '%s'", url.toStdString().c_str() );
60  }
61 
62  return path;
63 }
64 
65 
66 QPixmap loadPixmap( QString url, bool fill_cache )
67 {
68  QPixmap pixmap;
69 
70  // if it's in the cache, no need to locate
71  if ( QPixmapCache::find( url, &pixmap ) )
72  {
73  return pixmap;
74  }
75 
76  boost::filesystem::path path = getPath( url );
77 
78  // If something goes wrong here, we go on and store the empty pixmap,
79  // so the error won't appear again anytime soon.
80  if ( boost::filesystem::exists( path ) )
81  {
82  ROS_DEBUG_NAMED( "load_resource", "Loading '%s'", path.string().c_str() );
83  if ( !pixmap.load( QString::fromStdString( path.string() ) ) )
84  {
85  ROS_ERROR( "Could not load pixmap '%s'", path.string().c_str() );
86  }
87  }
88 
89  if ( fill_cache )
90  {
91  QPixmapCache::insert( url, pixmap );
92  }
93 
94  return pixmap;
95 }
96 
97 QCursor getDefaultCursor( bool fill_cache )
98 {
99  return QCursor(Qt::ArrowCursor);
100 }
101 
102 QCursor makeIconCursor( QString url, bool fill_cache )
103 {
104  QPixmap icon = loadPixmap( url, fill_cache );
105  if (icon.width() == 0 || icon.height() == 0)
106  {
107  ROS_ERROR( "Could not load pixmap '%s' -- using default cursor instead.", url.toStdString().c_str() );
108  return getDefaultCursor();
109  }
110  QString cache_key = url + ".cursor";
111  return makeIconCursor( icon, cache_key, fill_cache );
112 }
113 
114 QCursor makeIconCursor( QPixmap icon, QString cache_key, bool fill_cache )
115 {
116  // if it's in the cache, no need to locate
117  QPixmap cursor_img;
118  if ( QPixmapCache::find( cache_key, &cursor_img ) )
119  {
120  return QCursor( cursor_img, 0, 0 );
121  }
122 
123  QPixmap base_cursor = loadPixmap( "package://rviz/icons/cursor.svg", fill_cache );
124 
125  const int cursor_size = 32;
126 
127  cursor_img = QPixmap( cursor_size, cursor_size );
128  cursor_img.fill( QColor(0,0,0,0) );
129 
130  // copy base cursor & icon into one image
131  QPainter painter(&cursor_img);
132 
133  int draw_x = 12;
134  int draw_y = 16;
135 
136  // if the icon is too large, move it to the left
137  if( draw_x+icon.width() > cursor_size )
138  {
139  draw_x = cursor_size-icon.width();
140  }
141  if( draw_y+icon.height() > cursor_size )
142  {
143  draw_y = cursor_size-icon.height();
144  }
145 
146  painter.drawPixmap( 0, 0, base_cursor );
147  painter.drawPixmap( draw_x, draw_y, icon );
148 
149  if ( fill_cache )
150  {
151  QPixmapCache::insert( cache_key, cursor_img );
152  }
153 
154  return QCursor( cursor_img, 1, 1 );
155 }
156 
157 
158 
159 }
boost::filesystem::path getPath(QString url)
QCursor makeIconCursor(QString url, bool fill_cache)
#define ROS_DEBUG_NAMED(name,...)
QCursor getDefaultCursor(bool fill_cache)
ROSLIB_DECL std::string getPath(const std::string &package_name)
#define ROS_ERROR(...)
QPixmap loadPixmap(QString url, bool fill_cache)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51