WIconManager.cpp 4.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#include <cassert>
#include <string>

28 29
#include <QtGui/QPixmap>

30
#include "core/common/WAssert.h"
31
#include "core/common/WLogger.h"
32
#include "core/common/exceptions/WFileNotFound.h"
33
#include "core/kernel/WModuleFactory.h"
34
#include "WIconManager.h"
35

36 37 38 39 40 41 42 43 44 45 46 47 48
void WIconManager::addIcon( std::string name, boost::filesystem::path filename )
{
    QPixmap img;
    bool failed = !img.load( QString::fromStdString( filename.string() ) );
    if( failed )
    {
        throw WFileNotFound( "Image file for icon in \"" + filename.string() + "\" could not be loaded." );
    }

    QIcon* icon = new QIcon( img );
    m_iconList[name] = icon;
}

49 50 51 52 53 54
void WIconManager::addIcon( std::string name, const char* const xpm[] )
{
    QIcon* icon = new QIcon( QPixmap( xpm ) );
    m_iconList[name] = icon;
}

55 56 57 58 59 60
void WIconManager::addIcon( std::string name, const QPixmap& pixmap )
{
    QIcon* icon = new QIcon( QPixmap( pixmap ) );
    m_iconList[name] = icon;
}

61 62
QIcon WIconManager::getIcon( const std::string name )
{
63 64
    if( m_iconList.count( name ) != 0 )
    {
65
        return *m_iconList[name];
66 67 68
    }
    else if( WModuleFactory::getModuleFactory()->getPrototypeByName( name ) )
    {
69 70
        // get module icon from meta info if available
        WModuleMetaInformation::ConstSPtr meta = WModuleFactory::getModuleFactory()->getPrototypeByName( name )->getMetaInformation();
71 72 73
        const char** xpm = WModuleFactory::getModuleFactory()->getPrototypeByName( name )->getXPMIcon();

        // prefer meta info icon
74 75 76 77
        if( meta->isIconAvailable() && boost::filesystem::exists( meta->getIcon() ) )
        {
            try
            {
78
                return QIcon( QPixmap( QString::fromStdString( meta->getIcon().string() ) ) );
79 80 81
            }
            catch( ... )
            {
82 83 84 85 86 87 88 89
                if( xpm )
                {
                    return QIcon( QPixmap( xpm ) );
                }
                else
                {
                    return QIcon();
                }
90 91 92 93
            }
        }
        else
        {
94 95 96 97 98 99 100 101
            if( xpm )
            {
                return QIcon( QPixmap( xpm ) );
            }
            else
            {
                return QIcon();
            }
102
        }
103 104
    }
    else
105 106 107
    {
        WAssert( 0, "Found no icon named: " + name );
    }
108
}
109 110 111 112 113 114 115 116 117

QIcon WIconManager::getIcon( const std::string name, const QIcon& defaultIcon )
{
    if( m_iconList.count( name ) != 0 )
    {
        return *m_iconList[name];
    }
    else if( WModuleFactory::getModuleFactory()->getPrototypeByName( name ) )
    {
118 119
        // get module icon from meta info if available
        WModuleMetaInformation::ConstSPtr meta = WModuleFactory::getModuleFactory()->getPrototypeByName( name )->getMetaInformation();
120 121 122
        const char** xpm = WModuleFactory::getModuleFactory()->getPrototypeByName( name )->getXPMIcon();

        // prefer meta info icon
123 124 125 126 127 128 129 130
        if( meta->isIconAvailable() && boost::filesystem::exists( meta->getIcon() ) )
        {
            try
            {
                return QIcon( QPixmap( QString::fromStdString( meta->getIcon().string() ) ) );
            }
            catch( ... )
            {
131 132 133 134 135 136 137 138
                if( xpm )
                {
                    return QIcon( QPixmap( xpm ) );
                }
                else
                {
                    return defaultIcon;
                }
139 140 141 142
            }
        }
        else
        {
143 144 145 146 147 148 149 150
            if( xpm )
            {
                return QIcon( QPixmap( xpm ) );
            }
            else
            {
                return defaultIcon;
            }
151
        }
152 153 154
    }
    else
    {
155
        return defaultIcon;
156 157
    }
}