2018-10-14 13:54:09 +03:00
/*
* Copyright 2018 Michail Vourlakos < mvourlakos @ gmail . com >
*
* This file is part of Latte - Dock
*
* Latte - Dock is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation ; either version 2 of
* the License , or ( at your option ) any later version .
*
* Latte - Dock 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 General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*
*/
# include "schemecolors.h"
2019-03-16 14:44:00 +03:00
// local
2019-05-09 18:12:46 +03:00
# include "../layouts/importer.h"
2021-02-02 23:29:27 +03:00
# include "../tools/commontools.h"
2019-03-16 14:44:00 +03:00
2018-12-02 03:05:52 +03:00
// Qt
2018-10-14 13:54:09 +03:00
# include <QDebug>
# include <QDir>
# include <QFileInfo>
2018-12-02 03:05:52 +03:00
// KDE
2018-10-14 13:54:09 +03:00
# include <KConfigGroup>
2018-10-18 18:19:44 +03:00
# include <KDirWatch>
2018-10-14 13:54:09 +03:00
# include <KSharedConfig>
namespace Latte {
2019-05-11 15:43:10 +03:00
namespace WindowSystem {
2018-10-14 13:54:09 +03:00
2018-11-08 16:04:59 +03:00
SchemeColors : : SchemeColors ( QObject * parent , QString scheme , bool plasmaTheme ) :
QObject ( parent ) ,
m_basedOnPlasmaTheme ( plasmaTheme )
2018-10-14 13:54:09 +03:00
{
QString pSchemeFile = possibleSchemeFile ( scheme ) ;
if ( QFileInfo ( pSchemeFile ) . exists ( ) ) {
2018-10-27 14:27:49 +03:00
setSchemeFile ( pSchemeFile ) ;
2018-11-09 11:44:51 +03:00
m_schemeName = schemeName ( pSchemeFile ) ;
2018-10-18 18:19:44 +03:00
//! track scheme file for changes
KDirWatch : : self ( ) - > addFile ( m_schemeFile ) ;
2018-10-27 11:06:40 +03:00
connect ( KDirWatch : : self ( ) , & KDirWatch : : dirty , this , [ & ] ( const QString & path ) {
if ( path = = m_schemeFile ) {
updateScheme ( ) ;
}
} ) ;
2018-10-14 13:54:09 +03:00
}
updateScheme ( ) ;
}
SchemeColors : : ~ SchemeColors ( )
{
2019-02-22 17:43:31 +03:00
///
2018-10-14 13:54:09 +03:00
}
QColor SchemeColors : : backgroundColor ( ) const
{
2018-11-07 21:20:36 +03:00
return m_activeBackgroundColor ;
2018-10-14 13:54:09 +03:00
}
2018-10-18 18:02:54 +03:00
QColor SchemeColors : : textColor ( ) const
2018-10-14 13:54:09 +03:00
{
2018-11-07 21:20:36 +03:00
return m_activeTextColor ;
}
QColor SchemeColors : : inactiveBackgroundColor ( ) const
{
return m_inactiveBackgroundColor ;
}
QColor SchemeColors : : inactiveTextColor ( ) const
{
return m_inactiveTextColor ;
2018-10-18 18:02:54 +03:00
}
QColor SchemeColors : : highlightColor ( ) const
{
return m_highlightColor ;
}
QColor SchemeColors : : highlightedTextColor ( ) const
{
return m_highlightedTextColor ;
}
2018-11-17 19:12:42 +03:00
QColor SchemeColors : : positiveTextColor ( ) const
2018-10-18 18:02:54 +03:00
{
2018-11-17 19:12:42 +03:00
return m_positiveTextColor ;
2018-10-18 18:02:54 +03:00
}
2018-11-17 19:12:42 +03:00
QColor SchemeColors : : neutralTextColor ( ) const
2018-10-18 18:02:54 +03:00
{
2018-11-17 19:12:42 +03:00
return m_neutralTextColor ;
2018-10-18 18:02:54 +03:00
}
2018-11-17 19:12:42 +03:00
QColor SchemeColors : : negativeTextColor ( ) const
2018-10-18 18:02:54 +03:00
{
2018-11-17 19:12:42 +03:00
return m_negativeTextColor ;
2018-10-14 13:54:09 +03:00
}
2018-10-27 14:27:49 +03:00
QColor SchemeColors : : buttonTextColor ( ) const
{
return m_buttonTextColor ;
}
QColor SchemeColors : : buttonBackgroundColor ( ) const
{
return m_buttonBackgroundColor ;
}
QColor SchemeColors : : buttonHoverColor ( ) const
{
return m_buttonHoverColor ;
}
QColor SchemeColors : : buttonFocusColor ( ) const
{
return m_buttonFocusColor ;
}
QString SchemeColors : : schemeName ( ) const
2018-10-14 13:54:09 +03:00
{
return m_schemeName ;
}
2018-10-27 14:27:49 +03:00
QString SchemeColors : : SchemeColors : : schemeFile ( ) const
2018-10-14 13:54:09 +03:00
{
return m_schemeFile ;
}
2018-10-27 14:27:49 +03:00
void SchemeColors : : setSchemeFile ( QString file )
{
if ( m_schemeFile = = file ) {
return ;
}
m_schemeFile = file ;
emit schemeFileChanged ( ) ;
}
2018-10-14 13:54:09 +03:00
QString SchemeColors : : possibleSchemeFile ( QString scheme )
{
2018-10-27 14:27:49 +03:00
if ( scheme . startsWith ( " / " ) & & scheme . endsWith ( " colors " ) & & QFileInfo ( scheme ) . exists ( ) ) {
2018-10-14 13:54:09 +03:00
return scheme ;
}
QString tempScheme = scheme ;
if ( scheme = = " kdeglobals " ) {
2021-02-02 23:29:27 +03:00
QString settingsFile = Latte : : configPath ( ) + " /kdeglobals " ;
2018-10-14 13:54:09 +03:00
if ( QFileInfo ( settingsFile ) . exists ( ) ) {
KSharedConfigPtr filePtr = KSharedConfig : : openConfig ( settingsFile ) ;
KConfigGroup generalGroup = KConfigGroup ( filePtr , " General " ) ;
tempScheme = generalGroup . readEntry ( " ColorScheme " , " " ) ;
}
}
2019-05-09 17:57:12 +03:00
QString schemePath = Layouts : : Importer : : standardPath ( " color-schemes/ " + tempScheme + " .colors " ) ;
2018-10-14 13:54:09 +03:00
2019-03-16 14:44:00 +03:00
if ( schemePath . isEmpty ( ) | | ! QFileInfo ( schemePath ) . exists ( ) ) {
2019-02-22 17:43:31 +03:00
//! remove all whitespaces and "-" from scheme in order to access correctly its file
QString schemeNameSimplified = tempScheme . simplified ( ) . remove ( " " ) . remove ( " - " ) ;
2019-05-09 17:57:12 +03:00
schemePath = Layouts : : Importer : : standardPath ( " color-schemes/ " + schemeNameSimplified + " .colors " ) ;
2019-02-22 17:43:31 +03:00
}
2018-10-14 13:54:09 +03:00
2019-03-16 14:44:00 +03:00
if ( QFileInfo ( schemePath ) . exists ( ) ) {
return schemePath ;
2018-10-14 13:54:09 +03:00
}
return " " ;
}
2018-11-09 11:44:51 +03:00
QString SchemeColors : : schemeName ( QString originalFile )
{
if ( ! ( originalFile . startsWith ( " / " ) & & originalFile . endsWith ( " colors " ) & & QFileInfo ( originalFile ) . exists ( ) ) ) {
return " " ;
}
QString fileNameNoExt = originalFile ;
int lastSlash = originalFile . lastIndexOf ( " / " ) ;
if ( lastSlash > = 0 ) {
fileNameNoExt . remove ( 0 , lastSlash + 1 ) ;
}
if ( fileNameNoExt . endsWith ( " .colors " ) ) {
fileNameNoExt . remove ( " .colors " ) ;
}
KSharedConfigPtr filePtr = KSharedConfig : : openConfig ( originalFile ) ;
KConfigGroup generalGroup = KConfigGroup ( filePtr , " General " ) ;
return generalGroup . readEntry ( " Name " , fileNameNoExt ) ;
}
2018-10-14 13:54:09 +03:00
void SchemeColors : : updateScheme ( )
{
if ( m_schemeFile . isEmpty ( ) | | ! QFileInfo ( m_schemeFile ) . exists ( ) ) {
return ;
}
KSharedConfigPtr filePtr = KSharedConfig : : openConfig ( m_schemeFile ) ;
KConfigGroup wmGroup = KConfigGroup ( filePtr , " WM " ) ;
2018-10-18 18:02:54 +03:00
KConfigGroup selGroup = KConfigGroup ( filePtr , " Colors:Selection " ) ;
2020-01-12 11:52:45 +03:00
//KConfigGroup viewGroup = KConfigGroup(filePtr, "Colors:View");
KConfigGroup windowGroup = KConfigGroup ( filePtr , " Colors:Window " ) ;
2018-10-27 14:27:49 +03:00
KConfigGroup buttonGroup = KConfigGroup ( filePtr , " Colors:Button " ) ;
2018-10-14 13:54:09 +03:00
2018-11-08 16:04:59 +03:00
if ( ! m_basedOnPlasmaTheme ) {
m_activeBackgroundColor = wmGroup . readEntry ( " activeBackground " , QColor ( ) ) ;
m_activeTextColor = wmGroup . readEntry ( " activeForeground " , QColor ( ) ) ;
m_inactiveBackgroundColor = wmGroup . readEntry ( " inactiveBackground " , QColor ( ) ) ;
m_inactiveTextColor = wmGroup . readEntry ( " inactiveForeground " , QColor ( ) ) ;
} else {
2020-01-12 11:52:45 +03:00
m_activeBackgroundColor = windowGroup . readEntry ( " BackgroundNormal " , QColor ( ) ) ;
m_activeTextColor = windowGroup . readEntry ( " ForegroundNormal " , QColor ( ) ) ;
m_inactiveBackgroundColor = windowGroup . readEntry ( " BackgroundAlternate " , QColor ( ) ) ;
m_inactiveTextColor = windowGroup . readEntry ( " ForegroundInactive " , QColor ( ) ) ;
2018-11-08 16:04:59 +03:00
}
2018-10-18 18:02:54 +03:00
m_highlightColor = selGroup . readEntry ( " BackgroundNormal " , QColor ( ) ) ;
m_highlightedTextColor = selGroup . readEntry ( " ForegroundNormal " , QColor ( ) ) ;
2020-01-12 11:52:45 +03:00
m_positiveTextColor = windowGroup . readEntry ( " ForegroundPositive " , QColor ( ) ) ;
m_neutralTextColor = windowGroup . readEntry ( " ForegroundNeutral " , QColor ( ) ) ; ;
m_negativeTextColor = windowGroup . readEntry ( " ForegroundNegative " , QColor ( ) ) ;
2018-10-14 13:54:09 +03:00
2018-10-27 14:27:49 +03:00
m_buttonTextColor = buttonGroup . readEntry ( " ForegroundNormal " , QColor ( ) ) ;
m_buttonBackgroundColor = buttonGroup . readEntry ( " BackgroundNormal " , QColor ( ) ) ;
m_buttonHoverColor = buttonGroup . readEntry ( " DecorationHover " , QColor ( ) ) ;
m_buttonFocusColor = buttonGroup . readEntry ( " DecorationFocus " , QColor ( ) ) ;
2018-10-14 13:54:09 +03:00
emit colorsChanged ( ) ;
}
}
2019-05-11 15:43:10 +03:00
}