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 boost::filesystem::path getPath(QString url)
42 {
43  boost::filesystem::path path;
44 
45  if (url.indexOf("package://", 0, Qt::CaseInsensitive) == 0)
46  {
47  QString package_name = url.section('/', 2, 2);
48  QString file_name = url.section('/', 3);
49  path = ros::package::getPath(package_name.toStdString());
50  path = path / file_name.toStdString();
51  }
52  else if (url.indexOf("file://", 0, Qt::CaseInsensitive) == 0)
53  {
54  path = url.section('/', 2).toStdString();
55  }
56  else
57  {
58  ROS_ERROR("Invalid or unsupported URL: '%s'", url.toStdString().c_str());
59  }
60 
61  return path;
62 }
63 
64 
65 QPixmap loadPixmap(QString url, bool fill_cache)
66 {
67  QPixmap pixmap;
68 
69  // if it's in the cache, no need to locate
70  if (QPixmapCache::find(url, &pixmap))
71  {
72  return pixmap;
73  }
74 
75  boost::filesystem::path path = getPath(url);
76 
77  // If something goes wrong here, we go on and store the empty pixmap,
78  // so the error won't appear again anytime soon.
79  if (boost::filesystem::exists(path))
80  {
81  ROS_DEBUG_NAMED("load_resource", "Loading '%s'", path.string().c_str());
82  if (!pixmap.load(QString::fromStdString(path.string())))
83  {
84  ROS_ERROR("Could not load pixmap '%s'", path.string().c_str());
85  }
86  }
87 
88  if (fill_cache)
89  {
90  QPixmapCache::insert(url, pixmap);
91  }
92 
93  return pixmap;
94 }
95 
96 QCursor getDefaultCursor(bool /*fill_cache*/)
97 {
98  return QCursor(Qt::ArrowCursor);
99 }
100 
101 QCursor makeIconCursor(QString url, bool fill_cache)
102 {
103  QPixmap icon = loadPixmap(url, fill_cache);
104  if (icon.width() == 0 || icon.height() == 0)
105  {
106  ROS_ERROR("Could not load pixmap '%s' -- using default cursor instead.", url.toStdString().c_str());
107  return getDefaultCursor();
108  }
109  QString cache_key = url + ".cursor";
110  return makeIconCursor(icon, cache_key, fill_cache);
111 }
112 
113 QCursor makeIconCursor(QPixmap icon, QString cache_key, bool fill_cache)
114 {
115  // if it's in the cache, no need to locate
116  QPixmap cursor_img;
117  if (QPixmapCache::find(cache_key, &cursor_img))
118  {
119  return QCursor(cursor_img, 0, 0);
120  }
121 
122  QPixmap base_cursor = loadPixmap("package://rviz/icons/cursor.svg", fill_cache);
123 
124  const int cursor_size = 32;
125 
126  cursor_img = QPixmap(cursor_size, cursor_size);
127  cursor_img.fill(QColor(0, 0, 0, 0));
128 
129  // copy base cursor & icon into one image
130  QPainter painter(&cursor_img);
131 
132  int draw_x = 12;
133  int draw_y = 16;
134 
135  // if the icon is too large, move it to the left
136  if (draw_x + icon.width() > cursor_size)
137  {
138  draw_x = cursor_size - icon.width();
139  }
140  if (draw_y + icon.height() > cursor_size)
141  {
142  draw_y = cursor_size - icon.height();
143  }
144 
145  painter.drawPixmap(0, 0, base_cursor);
146  painter.drawPixmap(draw_x, draw_y, icon);
147 
148  if (fill_cache)
149  {
150  QPixmapCache::insert(cache_key, cursor_img);
151  }
152 
153  return QCursor(cursor_img, 1, 1);
154 }
155 
156 
157 } // namespace rviz
boost::filesystem::path getPath(QString url)
QCursor makeIconCursor(QString url, bool fill_cache)
QCursor getDefaultCursor(bool)
#define ROS_DEBUG_NAMED(name,...)
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 Mon Aug 15 2022 02:05:17