PaletteDictionary.cpp 3.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include "PaletteDictionary.h"




Frédéric Larue's avatar
Frédéric Larue committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
QString PaletteDictionary::s_ColorGroupNames[ QPalette::NColorGroups ] = {
    "active"    ,
    "disabled"  ,
    "inactive"
};


QString PaletteDictionary::s_ColorRoleNames[ QPalette::NColorRoles ] = {
    "window-text"       ,
    "button"            ,
    "light"             ,
    "midlight"          ,
    "dark"              ,
    "mid"               ,
    "text"              ,
    "bright-text"       ,
    "button-text"       ,
    "base"              ,
    "window"            ,
    "shadow"            ,
    "highlight"         ,
    "highlighted-text"  ,
    "link"              ,
    "link-visited"      ,
    "alternate-base"    ,
    "no-role"           ,
    "tool-tip-base"     ,
    "tool-tip-text"
};


45 46 47 48
void PaletteDictionary::FromPalette( const QPalette &palette )
{
    clear();

Frédéric Larue's avatar
Frédéric Larue committed
49 50 51
    for( int group=0; group<QPalette::NColorGroups; ++group )
        for( int role=0; role<QPalette::NColorRoles; ++role )
            (*this)[ s_ColorGroupNames[group] + '_' + s_ColorRoleNames[role] ] = palette.color( (QPalette::ColorGroup) group, (QPalette::ColorRole) role );
52 53 54 55 56 57
}


void PaletteDictionary::ToPalette( QPalette &palette ) const
{
    QMap<QString,QPalette::ColorGroup> groupByName;
Frédéric Larue's avatar
Frédéric Larue committed
58 59
    for( int g=0; g<QPalette::NColorGroups; ++g )
        groupByName[ s_ColorGroupNames[g] ] = (QPalette::ColorGroup) g;
60 61

    QMap<QString,QPalette::ColorRole> roleByName;
Frédéric Larue's avatar
Frédéric Larue committed
62 63
    for( int r=0; r<QPalette::NColorRoles; ++r )
        roleByName[ s_ColorRoleNames[r] ] = (QPalette::ColorRole) r;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    for( auto color=begin(); color!=end(); ++color )
    {
        QStringList colorName = color.key().split( '_' );
        palette.setColor( groupByName[colorName.front()], roleByName[colorName.back()], color.value() );
    }
}


bool PaletteDictionary::Save( const QString &filename ) const
{
    QFile paletteFile( filename );
    if( !paletteFile.open( QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text ) )
        return false;

    QTextStream paletteStream( &paletteFile );

    for( auto c=begin(); c!=end(); ++c )
    {
        paletteStream << c.key() << ": #";
        paletteStream.setFieldWidth( 2 );
        paletteStream.setPadChar( '0' );
        paletteStream << QString::number( c.value().red  (), 16 ).toUpper();
        paletteStream << QString::number( c.value().green(), 16 ).toUpper();
        paletteStream << QString::number( c.value().blue (), 16 ).toUpper();
        paletteStream << QString::number( c.value().alpha(), 16 ).toUpper();
        paletteStream << ";\n";
    }

    paletteFile.close();
    return true;
}


bool PaletteDictionary::Load( const QString &filename )
{
    clear();

    QFile paletteFile( filename );
    if( !paletteFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
        return false;

    QTextStream paletteStream( &paletteFile );

    QString fileContent = paletteStream.readAll();

    QRegExp exp( " *([^: ]+) *: *#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2}) *; *\n*" );
    int found = 0;
    while( (found = exp.indexIn( fileContent, found )) >= 0 )
    {
        bool ok;
        int r = exp.cap(2).toInt( &ok, 16 );
        int g = exp.cap(3).toInt( &ok, 16 );
        int b = exp.cap(4).toInt( &ok, 16 );
        int a = exp.cap(5).toInt( &ok, 16 );

        (*this)[ exp.cap(1) ] = QColor( r, g, b, a );

        found += exp.matchedLength();
    }

    return true;
}