OpenWalnut  1.5.0dev
WIconManager.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <cassert>
26 #include <string>
27 
28 #include <QPixmap>
29 #include <QImage>
30 
31 #include "core/common/WAssert.h"
32 #include "core/common/WPathHelper.h"
33 #include "core/common/WLogger.h"
34 #include "core/common/exceptions/WFileNotFound.h"
35 #include "core/kernel/WModuleFactory.h"
36 
37 #include "WIconManager.h"
38 #include "WNoIconDefault.xpm"
39 
40 void WIconManager::addMapping( const std::string& newName, const std::string& mapToThis )
41 {
42  if( m_iconMappingList.count( newName ) == 0 )
43  {
44  m_iconMappingList.insert( std::make_pair( newName, mapToThis ) );
45  }
46  else
47  {
48  m_iconMappingList[ newName ] = mapToThis;
49  }
50 }
51 
52 QIcon WIconManager::getIcon( const std::string name )
53 {
54  // ensure we have a fallback icon
55  boost::filesystem::path fallback = WPathHelper::getPathHelper()->getSharePath() / "qtgui" / "default.png";
56  WAssert( boost::filesystem::exists( fallback ), "Found no icon named: " + name + " and no fallback icon. Installation broken?" );
57  return getIcon( name, QIcon( QPixmap( QString::fromStdString( fallback.string() ) ) ) );
58 }
59 
60 QIcon WIconManager::getIcon( const std::string name, const QIcon& defaultIcon )
61 {
62  std::string iconFile = name;
63 
64  // is there a mapping for this icon name?
65  if( m_iconMappingList.count( name ) > 0 )
66  {
67  iconFile = m_iconMappingList[ name ];
68  }
69 
70  // search file
71  boost::filesystem::path p = WPathHelper::getPathHelper()->getSharePath() / "qtgui" / std::string( iconFile + ".png" );
72  // and an alternative path
73  boost::filesystem::path pAlt = WPathHelper::getPathHelper()->getSharePath() / ".." / "pixmaps" / std::string( iconFile + ".png" );
74  if( boost::filesystem::exists( p ) )
75  {
76  try
77  {
78  return QIcon( QPixmap( QString::fromStdString( p.string() ) ) );
79  }
80  catch( ... )
81  {
82  return defaultIcon;
83  }
84  }
85  else if( boost::filesystem::exists( pAlt ) )
86  {
87  try
88  {
89  return QIcon( QPixmap( QString::fromStdString( pAlt.string() ) ) );
90  }
91  catch( ... )
92  {
93  return defaultIcon;
94  }
95  }
96  else if( WModuleFactory::getModuleFactory()->isPrototypeAvailable( name ) )
97  {
98  // get module icon from meta info if available
99  WModuleMetaInformation::ConstSPtr meta = WModuleFactory::getModuleFactory()->getPrototypeByName( name )->getMetaInformation();
100  const char** xpm = WModuleFactory::getModuleFactory()->getPrototypeByName( name )->getXPMIcon();
101 
102  // prefer meta info icon
103  if( meta->isIconAvailable() && boost::filesystem::exists( meta->getIcon() ) )
104  {
105  try
106  {
107  return QIcon( QPixmap( QString::fromStdString( meta->getIcon().string() ) ) );
108  }
109  catch( ... )
110  {
111  if( xpm )
112  {
113  return QIcon( QPixmap( xpm ) );
114  }
115  else
116  {
117  return defaultIcon;
118  }
119  }
120  }
121  else
122  {
123  if( xpm )
124  {
125  return QIcon( QPixmap( xpm ) );
126  }
127  else
128  {
129  return defaultIcon;
130  }
131  }
132  }
133  else
134  {
135  wlog::debug( "WIconManager" ) << pAlt.string();
136  wlog::debug( "WIconManager" ) << "Icon \"" << name << "\" not found. Falling back to default.";
137  return defaultIcon;
138  }
139 }
140 
142 {
143  if( !image )
144  {
145  return getNoIconDefault();
146  }
147 
148  // check some values:
149  if( ( image->getDepth() != 1 ) || ( image->getHeight() == 1 ) )
150  {
151  wlog::error( "WIconManager" ) << "Cannot use specified image as icon. It has to be 2D.";
152  return getNoIconDefault();
153  }
154 
155  // we need to map the GLenum format in WGEImage to Qt's formats
156  QImage img = QImage( image->getWidth(), image->getHeight(), QImage::Format_ARGB32 );
157 
158  // we manually convert the images for several reasons:
159  // 1) it is simply impossible to match all possible osg::image formats with those of QImage
160  // 2) lifetime of image->data() might be over after this call but QImage requires the data to exist until QImage is free'd
161 
162  for( int y = 0; y < image->getHeight(); ++y )
163  {
164  for( int x = 0; x < image->getWidth(); ++x )
165  {
166  WColor col = image->getColor( x, y );
167  QColor qcol = QColor( 255 * col.r(), 255 * col.g(), 255 * col.b(), 255 * col.a() );
168  img.setPixel( x, y, qcol.rgba() );
169  }
170  }
171 
172  // QImage require a top-left origin
173  if( image->getOrigin() == WGEImage::BOTTOM_LEFT )
174  {
175  img = img.mirrored( false, true );
176  }
177 
178  return QIcon( QPixmap::fromImage( img ) );
179 }
180 
182 {
183  return QIcon( QPixmap( WNoIconDefault_xpm ) );
184 }
@ BOTTOM_LEFT
bottom left origin
Definition: WGEImage.h:164
std::shared_ptr< WGEImage > SPtr
Convenience typedef for a std::shared_ptr< WGEImage >.
Definition: WGEImage.h:48
void addMapping(const std::string &newName, const std::string &mapToThis)
Add a mapping for icons.
std::map< std::string, std::string > m_iconMappingList
A map storing icons and the names used to identify them.
Definition: WIconManager.h:86
static QIcon getNoIconDefault()
Return an icon representing a default "No Icon" icon.
static QIcon convertToIcon(WGEImage::SPtr image)
Convert a WGEImage to an QIcon.
QIcon getIcon(const std::string name)
Searches icons in the internal map and all modules for the given icon name.
static SPtr getModuleFactory()
Returns instance of the module factory to use to create modules.
std::shared_ptr< const WModuleMetaInformation > ConstSPtr
Convenience typedef for a std::shared_ptr< const WModuleMetaInformation >.
static std::shared_ptr< WPathHelper > getPathHelper()
Returns instance of the path helper.
Definition: WPathHelper.cpp:52
WStreamedLogger debug(const std::string &source)
Logging a debug message.
Definition: WLogger.h:331
WStreamedLogger error(const std::string &source)
Logging an error message.
Definition: WLogger.h:298