WIconManager.cpp 2.78 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/exceptions/WFileNotFound.h"
32
#include "core/kernel/WModuleFactory.h"
33
#include "WIconManager.h"
34

35 36 37 38 39 40 41 42 43 44 45 46 47
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;
}

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

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

60 61
QIcon WIconManager::getIcon( const std::string name )
{
62 63 64 65 66 67 68
    QIcon icon;
    if( m_iconList.count( name ) != 0 )
    {
        icon = *m_iconList[name];
    }
    else if( WModuleFactory::getModuleFactory()->getPrototypeByName( name ) )
    {
69
        icon = QIcon( QPixmap( WModuleFactory::getModuleFactory()->getPrototypeByName( name )->getXPMIcon() ) );
70 71
    }
    else
72 73 74
    {
        WAssert( 0, "Found no icon named: " + name );
    }
75 76

    return icon;
77
}
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

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 ) )
    {
        return QIcon( QPixmap( WModuleFactory::getModuleFactory()->getPrototypeByName( name )->getXPMIcon() ) );
    }
    else
    {
        return QIcon( defaultIcon );
    }
}