Commit 20b6103f authored by Frédéric Larue's avatar Frédéric Larue

Palette now managed in the loading of the application style sheet.

parent 8cb731c2
/*
* (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;
}
/*
* (c) LSIIT, UMR CNRS/UdS
* Authors: F. Larue.
*
* See licence.txt for additional information.
*/
#ifndef __PALETTEDICTIONARY__
#define __PALETTEDICTIONARY__
#include <QtGui>
class PaletteDictionary : public QMap<QString,QColor>
{
public:
inline PaletteDictionary() : QMap<QString,QColor>() {}
inline PaletteDictionary( const QPalette &palette ) : QMap<QString,QColor>() { FromPalette( palette ); }
void FromPalette( const QPalette &palette );
void ToPalette( QPalette &palette ) const;
bool Save( const QString &filename ) const;
bool Load( const QString &filename );
};
#endif
......@@ -7,6 +7,7 @@
#include "UILaunch.h"
#include "PaletteDictionary.h"
......@@ -15,30 +16,6 @@ void UILoadApplicationStyle( QApplication &app, QSettings *userSettings )
{
app.setFont( QFont("Calibri",9) );
QPalette palette = app.palette();
for( int i=0; i<3; i+=2 )
{
palette.setColor( (QPalette::ColorGroup)i, QPalette::Window , QColor( 64, 64, 72) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::WindowText , QColor(160,192,255) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::Text , QColor(160,192,255) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::BrightText , QColor(200,230,255) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::ButtonText , QColor(200,230,255) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::Button , QColor( 48, 48, 64) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::Shadow , QColor( 0, 0, 0) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::Base , QColor( 48, 48, 64) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::Dark , QColor( 32, 32, 48) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::Mid , QColor( 56, 56, 80) );
//palette.setColor( (QPalette::ColorGroup)i, QPalette::Midlight , QColor(255, 0, 0) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::Light , QColor( 96, 96,112) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::Highlight , QColor( 0,120,215) );
palette.setColor( (QPalette::ColorGroup)i, QPalette::HighlightedText, QColor(255,255,255) );
}
palette.setColor( QPalette::Disabled, QPalette::Base , QColor( 48, 48, 64) );
palette.setColor( QPalette::Disabled, QPalette::Shadow , QColor( 48, 48, 64, 0 ) );
app.setPalette( palette );
//if( !userSettings->contains("uiStyle") )
{
......@@ -50,39 +27,17 @@ void UILoadApplicationStyle( QApplication &app, QSettings *userSettings )
QString styleBasePath = QFileInfo(QApplication::applicationFilePath()).absolutePath() + "/styles/iron/";
QDir().mkpath( styleBasePath );
// Load the palette template from resources.
PaletteDictionary colorDict;
colorDict.Load( ":/style/Palette.css" );
// Load the template CSS file from resources.
QFile cssTemplate( ":/style/Default.css" );
cssTemplate.open( QIODevice::ReadOnly | QIODevice::Text );
QString styleSheetTemplate = QTextStream(&cssTemplate).readAll();
// Create a dictionary to map every palette color name to the corresponding QColor.
QPalette::ColorGroup colorGroup[] = { QPalette::Active, QPalette::Disabled, QPalette::Inactive };
QString colorGroupPrefix[] = { "active_", "disabled_", "inactive_" };
QMap<QString,QColor> colorDict;
for( int i=0; i<3; ++i )
{
colorDict[ colorGroupPrefix[i]+"window" ] = palette.color( colorGroup[i], QPalette::Window );
colorDict[ colorGroupPrefix[i]+"window-text" ] = palette.color( colorGroup[i], QPalette::WindowText );
colorDict[ colorGroupPrefix[i]+"text" ] = palette.color( colorGroup[i], QPalette::Text );
colorDict[ colorGroupPrefix[i]+"bright-text" ] = palette.color( colorGroup[i], QPalette::BrightText );
colorDict[ colorGroupPrefix[i]+"button" ] = palette.color( colorGroup[i], QPalette::Button );
colorDict[ colorGroupPrefix[i]+"button-text" ] = palette.color( colorGroup[i], QPalette::ButtonText );
colorDict[ colorGroupPrefix[i]+"shadow" ] = palette.color( colorGroup[i], QPalette::Shadow );
colorDict[ colorGroupPrefix[i]+"base" ] = palette.color( colorGroup[i], QPalette::Base );
colorDict[ colorGroupPrefix[i]+"alternate-base" ] = palette.color( colorGroup[i], QPalette::AlternateBase );
colorDict[ colorGroupPrefix[i]+"dark" ] = palette.color( colorGroup[i], QPalette::Dark );
colorDict[ colorGroupPrefix[i]+"mid" ] = palette.color( colorGroup[i], QPalette::Mid );
colorDict[ colorGroupPrefix[i]+"midlight" ] = palette.color( colorGroup[i], QPalette::Midlight );
colorDict[ colorGroupPrefix[i]+"light" ] = palette.color( colorGroup[i], QPalette::Light );
colorDict[ colorGroupPrefix[i]+"highlight" ] = palette.color( colorGroup[i], QPalette::Highlight );
colorDict[ colorGroupPrefix[i]+"highlighted-text" ] = palette.color( colorGroup[i], QPalette::HighlightedText );
colorDict[ colorGroupPrefix[i]+"link" ] = palette.color( colorGroup[i], QPalette::Link );
colorDict[ colorGroupPrefix[i]+"link-visited" ] = palette.color( colorGroup[i], QPalette::LinkVisited );
}
// Parse the template file to collect every occurrence of pattern "[IconImageName:PaletteColorName]".
QMap<QString, QSet<QString>> foundIcons;
......@@ -92,7 +47,7 @@ QString styleBasePath = QFileInfo(QApplication::applicationFilePath()).absoluteP
{
QString colorDesc;
if( expIconId.cap(4).isEmpty() )
colorDesc = colorGroupPrefix[0] + expIconId.cap(2);
colorDesc = "active_" + expIconId.cap(2);
else
colorDesc = expIconId.cap(2) + "_" + expIconId.cap(4);
......@@ -152,6 +107,10 @@ QString styleBasePath = QFileInfo(QApplication::applicationFilePath()).absoluteP
}
}
// Store the palette file.
colorDict.Save( styleBasePath + "Palette.css" );
// Store the modified CSS template to the style destination directory.
QString cssFileName = styleBasePath + "Style.css";
......@@ -162,11 +121,20 @@ QString styleBasePath = QFileInfo(QApplication::applicationFilePath()).absoluteP
// Update the settings file to set the new created style as the current one.
userSettings->setValue( "uiStyle", QVariant(cssFileName) );
userSettings->setValue( "uiStyle", QVariant(styleBasePath) );
}
QFile cssFile( userSettings->value("uiStyle").toString() );
QString styleBasePath = userSettings->value("uiStyle").toString();
PaletteDictionary colorDict;
colorDict.Load( styleBasePath + "Palette.css" );
QPalette palette;
colorDict.ToPalette( palette );
app.setPalette( palette );
QFile cssFile( styleBasePath + "Style.css" );
if( cssFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
app.setStyleSheet( QTextStream(&cssFile).readAll() );
......
......@@ -28,6 +28,7 @@
</qresource>
<qresource prefix="/style">
<file alias="Default.css">resources/style/Default.css</file>
<file alias="Palette.css">resources/style/Palette.css</file>
<file alias="QCheckBox.png">resources/style/QCheckBox.png</file>
<file alias="QRadioButton.png">resources/style/QRadioButton.png</file>
<file alias="QHeaderViewUpArrow.png">resources/style/QHeaderViewUpArrow.png</file>
......
active_alternate-base: #F6F6F6FF;
active_base: #303040FF;
active_bright-text: #C8E6FFFF;
active_button: #303040FF;
active_button-text: #C8E6FFFF;
active_dark: #202030FF;
active_highlight: #0078D7FF;
active_highlighted-text: #FFFFFFFF;
active_light: #606070FF;
active_link: #0000FFFF;
active_link-visited: #FF00FFFF;
active_mid: #383850FF;
active_midlight: #E3E3E3FF;
active_shadow: #000000FF;
active_text: #A0C0FFFF;
active_window: #404048FF;
active_window-text: #A0C0FFFF;
disabled_alternate-base: #F6F6F6FF;
disabled_base: #303040FF;
disabled_bright-text: #FFFFFFFF;
disabled_button: #F0F0F0FF;
disabled_button-text: #787878FF;
disabled_dark: #A0A0A0FF;
disabled_highlight: #0078D7FF;
disabled_highlighted-text: #FFFFFFFF;
disabled_light: #FFFFFFFF;
disabled_link: #0000FFFF;
disabled_link-visited: #FF00FFFF;
disabled_mid: #A0A0A0FF;
disabled_midlight: #F7F7F7FF;
disabled_shadow: #30304000;
disabled_text: #787878FF;
disabled_window: #F0F0F0FF;
disabled_window-text: #787878FF;
inactive_alternate-base: #F6F6F6FF;
inactive_base: #303040FF;
inactive_bright-text: #C8E6FFFF;
inactive_button: #303040FF;
inactive_button-text: #C8E6FFFF;
inactive_dark: #202030FF;
inactive_highlight: #0078D7FF;
inactive_highlighted-text: #FFFFFFFF;
inactive_light: #606070FF;
inactive_link: #0000FFFF;
inactive_link-visited: #FF00FFFF;
inactive_mid: #383850FF;
inactive_midlight: #E3E3E3FF;
inactive_shadow: #000000FF;
inactive_text: #A0C0FFFF;
inactive_window: #404048FF;
inactive_window-text: #A0C0FFFF;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment