PaletteDictionary.cpp 5.49 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 127 128 129 130 131
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: F. Larue.
 *
 *	See licence.txt for additional information.
 */


#include "PaletteDictionary.h"




void PaletteDictionary::FromPalette( const QPalette &palette )
{
    clear();

    QPalette::ColorGroup colorGroup[] = { QPalette::Active, QPalette::Disabled, QPalette::Inactive };
    QString colorGroupPrefix[] = { "active_", "disabled_", "inactive_" };

    for( int i=0; i<3; ++i )
    {
        (*this)[ colorGroupPrefix[i]+"window"           ] = palette.color( colorGroup[i], QPalette::Window          );
        (*this)[ colorGroupPrefix[i]+"window-text"      ] = palette.color( colorGroup[i], QPalette::WindowText      );
        (*this)[ colorGroupPrefix[i]+"text"             ] = palette.color( colorGroup[i], QPalette::Text            );
        (*this)[ colorGroupPrefix[i]+"bright-text"      ] = palette.color( colorGroup[i], QPalette::BrightText      );
        (*this)[ colorGroupPrefix[i]+"button"           ] = palette.color( colorGroup[i], QPalette::Button          );
        (*this)[ colorGroupPrefix[i]+"button-text"      ] = palette.color( colorGroup[i], QPalette::ButtonText      );
        (*this)[ colorGroupPrefix[i]+"shadow"           ] = palette.color( colorGroup[i], QPalette::Shadow          );
        (*this)[ colorGroupPrefix[i]+"base"             ] = palette.color( colorGroup[i], QPalette::Base            );
        (*this)[ colorGroupPrefix[i]+"alternate-base"   ] = palette.color( colorGroup[i], QPalette::AlternateBase   );
        (*this)[ colorGroupPrefix[i]+"dark"             ] = palette.color( colorGroup[i], QPalette::Dark            );
        (*this)[ colorGroupPrefix[i]+"mid"              ] = palette.color( colorGroup[i], QPalette::Mid             );
        (*this)[ colorGroupPrefix[i]+"midlight"         ] = palette.color( colorGroup[i], QPalette::Midlight        );
        (*this)[ colorGroupPrefix[i]+"light"            ] = palette.color( colorGroup[i], QPalette::Light           );
        (*this)[ colorGroupPrefix[i]+"highlight"        ] = palette.color( colorGroup[i], QPalette::Highlight       );
        (*this)[ colorGroupPrefix[i]+"highlighted-text" ] = palette.color( colorGroup[i], QPalette::HighlightedText );
        (*this)[ colorGroupPrefix[i]+"link"             ] = palette.color( colorGroup[i], QPalette::Link            );
        (*this)[ colorGroupPrefix[i]+"link-visited"     ] = palette.color( colorGroup[i], QPalette::LinkVisited     );
    }
}


void PaletteDictionary::ToPalette( QPalette &palette ) const
{
    QMap<QString,QPalette::ColorGroup> groupByName;
    groupByName["active"  ] = QPalette::Active;
    groupByName["disabled"] = QPalette::Disabled;
    groupByName["inactive"] = QPalette::Inactive;

    QMap<QString,QPalette::ColorRole> roleByName;
    roleByName["window"          ] = QPalette::Window;
    roleByName["window-text"     ] = QPalette::WindowText;
    roleByName["text"            ] = QPalette::Text;
    roleByName["bright-text"     ] = QPalette::BrightText;
    roleByName["button"          ] = QPalette::Button;
    roleByName["button-text"     ] = QPalette::ButtonText;
    roleByName["shadow"          ] = QPalette::Shadow;
    roleByName["base"            ] = QPalette::Base;
    roleByName["alternate-base"  ] = QPalette::AlternateBase;
    roleByName["dark"            ] = QPalette::Dark;
    roleByName["mid"             ] = QPalette::Mid;
    roleByName["midlight"        ] = QPalette::Midlight;
    roleByName["light"           ] = QPalette::Light;
    roleByName["highlight"       ] = QPalette::Highlight;
    roleByName["highlighted-text"] = QPalette::HighlightedText;
    roleByName["link"            ] = QPalette::Link;
    roleByName["link-visited"    ] = QPalette::LinkVisited;

    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;
}