~ubuntu-branches/ubuntu/raring/openwalnut/raring

« back to all changes in this revision

Viewing changes to src/qt4gui/WIconManager.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2012-12-12 11:26:32 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20121212112632-xhiuwkxuz5h0idkh
Tags: 1.3.1+hg5849-1
* Minor changes compared to 1.3.0 but included several bug fixes.
* See http://www.openwalnut.org/versions/4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <QtGui/QPixmap>
 
29
 
 
30
#include "core/common/WAssert.h"
 
31
#include "core/common/WPathHelper.h"
 
32
#include "core/common/WLogger.h"
 
33
#include "core/common/exceptions/WFileNotFound.h"
 
34
#include "core/kernel/WModuleFactory.h"
 
35
 
 
36
#include "WIconManager.h"
 
37
 
 
38
void WIconManager::addMapping( const std::string& newName, const std::string& mapToThis )
 
39
{
 
40
    if( m_iconMappingList.count( newName ) == 0 )
 
41
    {
 
42
        m_iconMappingList.insert( std::make_pair( newName, mapToThis ) );
 
43
    }
 
44
    else
 
45
    {
 
46
        m_iconMappingList[ newName ] = mapToThis;
 
47
    }
 
48
}
 
49
 
 
50
QIcon WIconManager::getIcon( const std::string name )
 
51
{
 
52
    // ensure we have a fallback icon
 
53
    boost::filesystem::path fallback = WPathHelper::getPathHelper()->getSharePath() / "qt4gui" / "default.png";
 
54
    WAssert( boost::filesystem::exists( fallback ), "Found no icon named: " + name + " and no fallback icon. Installation broken?" );
 
55
    return getIcon( name, QIcon( QPixmap( QString::fromStdString( fallback.string() ) ) ) );
 
56
}
 
57
 
 
58
QIcon WIconManager::getIcon( const std::string name, const QIcon& defaultIcon )
 
59
{
 
60
    std::string iconFile = name;
 
61
 
 
62
    // is there a mapping for this icon name?
 
63
    if( m_iconMappingList.count( name ) > 0 )
 
64
    {
 
65
        iconFile = m_iconMappingList[ name ];
 
66
    }
 
67
 
 
68
    // search file
 
69
    boost::filesystem::path p = WPathHelper::getPathHelper()->getSharePath() / "qt4gui" / std::string( iconFile + ".png" );
 
70
    // and an alternative path
 
71
    boost::filesystem::path pAlt = WPathHelper::getPathHelper()->getSharePath() / ".." / "pixmaps" / std::string( iconFile + ".png" );
 
72
    if( boost::filesystem::exists( p ) )
 
73
    {
 
74
        try
 
75
        {
 
76
            return QIcon( QPixmap( QString::fromStdString( p.string() ) ) );
 
77
        }
 
78
        catch( ... )
 
79
        {
 
80
            return defaultIcon;
 
81
        }
 
82
    }
 
83
    else if( boost::filesystem::exists( pAlt ) )
 
84
    {
 
85
        try
 
86
        {
 
87
            return QIcon( QPixmap( QString::fromStdString( pAlt.string() ) ) );
 
88
        }
 
89
        catch( ... )
 
90
        {
 
91
            return defaultIcon;
 
92
        }
 
93
    }
 
94
    else if( WModuleFactory::getModuleFactory()->isPrototypeAvailable( name ) )
 
95
    {
 
96
        // get module icon from meta info if available
 
97
        WModuleMetaInformation::ConstSPtr meta = WModuleFactory::getModuleFactory()->getPrototypeByName( name )->getMetaInformation();
 
98
        const char** xpm = WModuleFactory::getModuleFactory()->getPrototypeByName( name )->getXPMIcon();
 
99
 
 
100
        // prefer meta info icon
 
101
        if( meta->isIconAvailable() && boost::filesystem::exists( meta->getIcon() ) )
 
102
        {
 
103
            try
 
104
            {
 
105
                return QIcon( QPixmap( QString::fromStdString( meta->getIcon().string() ) ) );
 
106
            }
 
107
            catch( ... )
 
108
            {
 
109
                if( xpm )
 
110
                {
 
111
                    return QIcon( QPixmap( xpm ) );
 
112
                }
 
113
                else
 
114
                {
 
115
                    return defaultIcon;
 
116
                }
 
117
            }
 
118
        }
 
119
        else
 
120
        {
 
121
            if( xpm )
 
122
            {
 
123
                return QIcon( QPixmap( xpm ) );
 
124
            }
 
125
            else
 
126
            {
 
127
                return defaultIcon;
 
128
            }
 
129
        }
 
130
    }
 
131
    else
 
132
    {
 
133
        wlog::debug( "WIconManager" ) << pAlt.string();
 
134
        wlog::debug( "WIconManager" ) << "Icon \"" << name << "\" not found. Falling back to default.";
 
135
        return defaultIcon;
 
136
    }
 
137
}