2019-04-05 19:59:15 +03:00
/*
* Copyright 2019 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 "abstractlayout.h"
2019-04-05 20:39:19 +03:00
// Qt
# include <QDir>
# include <QDebug>
# include <QFile>
2019-04-05 19:59:15 +03:00
2019-04-06 18:56:24 +03:00
// KDE
# include <KSharedConfig>
2019-04-05 19:59:15 +03:00
namespace Latte {
namespace Layout {
2019-04-05 20:39:19 +03:00
AbstractLayout : : AbstractLayout ( QObject * parent , QString layoutFile , QString assignedName )
: QObject ( parent )
2019-04-05 19:59:15 +03:00
{
2019-04-05 20:39:19 +03:00
qDebug ( ) < < " Layout file to create object: " < < layoutFile < < " with name: " < < assignedName ;
if ( QFile ( layoutFile ) . exists ( ) ) {
if ( assignedName . isEmpty ( ) ) {
assignedName = layoutName ( layoutFile ) ;
}
//!this order is important because setFile initializes also the m_layoutGroup
setFile ( layoutFile ) ;
setName ( assignedName ) ;
loadConfig ( ) ;
init ( ) ;
m_loadedCorrectly = true ;
}
2019-04-05 19:59:15 +03:00
}
AbstractLayout : : ~ AbstractLayout ( )
{
}
2019-04-05 20:39:19 +03:00
void AbstractLayout : : init ( )
{
2020-03-25 02:30:30 +03:00
connect ( this , & AbstractLayout : : backgroundStyleChanged , this , & AbstractLayout : : backgroundChanged ) ;
connect ( this , & AbstractLayout : : backgroundStyleChanged , this , & AbstractLayout : : textColorChanged ) ;
connect ( this , & AbstractLayout : : customBackgroundChanged , this , & AbstractLayout : : backgroundChanged ) ;
connect ( this , & AbstractLayout : : customTextColorChanged , this , & AbstractLayout : : textColorChanged ) ;
connect ( this , & AbstractLayout : : colorChanged , this , & AbstractLayout : : backgroundChanged ) ;
2019-04-05 20:39:19 +03:00
connect ( this , & AbstractLayout : : colorChanged , this , & AbstractLayout : : textColorChanged ) ;
2020-03-25 02:30:30 +03:00
connect ( this , & AbstractLayout : : customBackgroundChanged , this , & AbstractLayout : : saveConfig ) ;
connect ( this , & AbstractLayout : : customTextColorChanged , this , & AbstractLayout : : saveConfig ) ;
connect ( this , & AbstractLayout : : colorChanged , this , & AbstractLayout : : saveConfig ) ;
2020-07-30 23:51:50 +03:00
connect ( this , & AbstractLayout : : iconChanged , this , & AbstractLayout : : saveConfig ) ;
2019-04-06 02:00:37 +03:00
connect ( this , & AbstractLayout : : lastUsedActivityChanged , this , & AbstractLayout : : saveConfig ) ;
connect ( this , & AbstractLayout : : launchersChanged , this , & AbstractLayout : : saveConfig ) ;
connect ( this , & AbstractLayout : : preferredForShortcutsTouchedChanged , this , & AbstractLayout : : saveConfig ) ;
connect ( this , & AbstractLayout : : versionChanged , this , & AbstractLayout : : saveConfig ) ;
2019-04-05 20:39:19 +03:00
}
int AbstractLayout : : version ( ) const
{
return m_version ;
}
void AbstractLayout : : setVersion ( int ver )
{
if ( m_version = = ver ) {
return ;
}
m_version = ver ;
emit versionChanged ( ) ;
}
2019-04-06 02:00:37 +03:00
bool AbstractLayout : : preferredForShortcutsTouched ( ) const
{
return m_preferredForShortcutsTouched ;
}
void AbstractLayout : : setPreferredForShortcutsTouched ( bool touched )
{
if ( m_preferredForShortcutsTouched = = touched ) {
return ;
}
m_preferredForShortcutsTouched = touched ;
emit preferredForShortcutsTouchedChanged ( ) ;
}
2019-04-05 20:39:19 +03:00
QString AbstractLayout : : background ( ) const
{
2020-04-23 19:35:26 +03:00
if ( m_backgroundStyle = = ColorBackgroundStyle ) {
2020-03-25 02:30:30 +03:00
return m_color ;
} else {
return m_customBackground ;
}
2019-04-05 20:39:19 +03:00
}
2020-03-25 02:30:30 +03:00
QString AbstractLayout : : textColor ( ) const
2019-04-05 20:39:19 +03:00
{
2020-04-23 19:35:26 +03:00
if ( m_backgroundStyle = = ColorBackgroundStyle ) {
2020-03-25 02:30:30 +03:00
return predefinedTextColor ( ) ;
} else {
return m_customTextColor ;
2019-04-05 20:39:19 +03:00
}
2020-03-25 02:30:30 +03:00
}
2020-04-23 19:35:26 +03:00
BackgroundStyle AbstractLayout : : backgroundStyle ( ) const
2020-03-25 02:30:30 +03:00
{
return m_backgroundStyle ;
}
2019-04-05 20:39:19 +03:00
2020-04-23 19:35:26 +03:00
void AbstractLayout : : setBackgroundStyle ( const BackgroundStyle & style )
2020-03-25 02:30:30 +03:00
{
if ( m_backgroundStyle = = style ) {
2019-04-05 20:39:19 +03:00
return ;
}
2020-03-25 02:30:30 +03:00
m_backgroundStyle = style ;
emit backgroundStyleChanged ( ) ;
}
2019-04-05 20:39:19 +03:00
2020-03-25 02:30:30 +03:00
QString AbstractLayout : : customBackground ( ) const
{
return m_customBackground ;
2019-04-05 20:39:19 +03:00
}
2020-03-25 02:30:30 +03:00
void AbstractLayout : : setCustomBackground ( const QString & background )
{
if ( m_customBackground = = background ) {
return ;
}
m_customBackground = background ;
emit customBackgroundChanged ( ) ;
}
2019-04-05 20:39:19 +03:00
QString AbstractLayout : : file ( ) const
{
return m_layoutFile ;
}
void AbstractLayout : : setFile ( QString file )
{
if ( m_layoutFile = = file ) {
return ;
}
qDebug ( ) < < " Layout file: " < < file ;
m_layoutFile = file ;
KSharedConfigPtr filePtr = KSharedConfig : : openConfig ( m_layoutFile ) ;
m_layoutGroup = KConfigGroup ( filePtr , " LayoutSettings " ) ;
emit fileChanged ( ) ;
}
QString AbstractLayout : : name ( ) const
{
return m_layoutName ;
}
void AbstractLayout : : setName ( QString name )
{
if ( m_layoutName = = name ) {
return ;
}
qDebug ( ) < < " Layout name: " < < name ;
m_layoutName = name ;
emit nameChanged ( ) ;
}
QString AbstractLayout : : color ( ) const
{
return m_color ;
}
void AbstractLayout : : setColor ( QString color )
{
if ( m_color = = color ) {
return ;
}
m_color = color ;
emit colorChanged ( ) ;
}
2020-07-30 23:51:50 +03:00
QString AbstractLayout : : icon ( ) const
{
return m_icon ;
}
void AbstractLayout : : setIcon ( const QString & icon )
{
if ( m_icon = = icon ) {
return ;
}
m_icon = icon ;
emit iconChanged ( ) ;
}
2019-04-06 02:00:37 +03:00
QString AbstractLayout : : lastUsedActivity ( )
{
return m_lastUsedActivity ;
}
void AbstractLayout : : clearLastUsedActivity ( )
{
m_lastUsedActivity = " " ;
emit lastUsedActivityChanged ( ) ;
}
2020-07-30 18:09:49 +03:00
QString AbstractLayout : : defaultCustomTextColor ( )
{
return " #3C1C00 " ;
}
QString AbstractLayout : : defaultCustomBackground ( )
{
return " defaultcustom " ;
}
2020-04-14 16:33:55 +03:00
QString AbstractLayout : : defaultTextColor ( const QString & color )
2019-04-05 20:39:19 +03:00
{
//! the user is in default layout theme
2020-04-14 16:33:55 +03:00
if ( color = = " blue " ) {
2020-03-25 02:30:30 +03:00
return " #D7E3FF " ;
2020-04-14 16:33:55 +03:00
} else if ( color = = " brown " ) {
2020-03-25 02:30:30 +03:00
return " #F1DECB " ;
2020-04-14 16:33:55 +03:00
} else if ( color = = " darkgrey " ) {
2020-03-25 02:30:30 +03:00
return " #ECECEC " ;
2020-04-14 16:33:55 +03:00
} else if ( color = = " gold " ) {
2020-03-25 02:30:30 +03:00
return " #7C3636 " ;
2020-04-14 16:33:55 +03:00
} else if ( color = = " green " ) {
2020-03-25 02:30:30 +03:00
return " #4D7549 " ;
2020-04-14 16:33:55 +03:00
} else if ( color = = " lightskyblue " ) {
2020-03-25 02:30:30 +03:00
return " #0C2A43 " ;
2020-04-14 16:33:55 +03:00
} else if ( color = = " orange " ) {
2020-03-25 02:30:30 +03:00
return " #6F3902 " ;
2020-04-14 16:33:55 +03:00
} else if ( color = = " pink " ) {
2020-03-25 02:30:30 +03:00
return " #743C46 " ;
2020-04-14 16:33:55 +03:00
} else if ( color = = " purple " ) {
2020-03-25 02:30:30 +03:00
return " #ECD9FF " ;
2020-04-14 16:33:55 +03:00
} else if ( color = = " red " ) {
2020-03-25 02:30:30 +03:00
return " #F3E4E4 " ;
2020-04-14 16:33:55 +03:00
} else if ( color = = " wheat " ) {
2020-03-25 02:30:30 +03:00
return " #6A4E25 " ;
} else {
return " #FCFCFC " ;
2019-04-05 20:39:19 +03:00
}
2020-03-25 02:30:30 +03:00
}
2019-04-05 20:39:19 +03:00
2020-04-14 16:33:55 +03:00
QString AbstractLayout : : predefinedTextColor ( ) const
{
return AbstractLayout : : defaultTextColor ( m_color ) ;
}
2020-03-25 02:30:30 +03:00
QString AbstractLayout : : customTextColor ( ) const
{
return m_customTextColor ;
2019-04-05 20:39:19 +03:00
}
2020-03-25 02:30:30 +03:00
void AbstractLayout : : setCustomTextColor ( const QString & customColor )
2019-04-05 20:39:19 +03:00
{
2020-03-25 02:30:30 +03:00
QString cuColor = customColor ;
2019-04-05 20:39:19 +03:00
//! remove # if someone is trying to set it this way
2020-03-25 02:30:30 +03:00
if ( cuColor . startsWith ( " # " ) ) {
cuColor . remove ( 0 , 1 ) ;
2019-04-05 20:39:19 +03:00
}
2020-03-25 02:30:30 +03:00
if ( m_customTextColor = = cuColor ) {
2019-04-05 20:39:19 +03:00
return ;
}
2020-03-25 02:30:30 +03:00
m_customTextColor = cuColor ;
emit customTextColorChanged ( ) ;
2019-04-05 20:39:19 +03:00
}
2019-04-06 02:00:37 +03:00
QStringList AbstractLayout : : launchers ( ) const
{
return m_launchers ;
}
void AbstractLayout : : setLaunchers ( QStringList launcherList )
{
if ( m_launchers = = launcherList )
return ;
m_launchers = launcherList ;
emit launchersChanged ( ) ;
}
2019-05-02 12:58:56 +03:00
Type AbstractLayout : : type ( ) const
{
return Type : : Abstract ;
}
2019-04-07 23:02:13 +03:00
QList < Plasma : : Types : : Location > combinedFreeEdges ( const QList < Plasma : : Types : : Location > & edges1 , const QList < Plasma : : Types : : Location > & edges2 )
{
QList < Plasma : : Types : : Location > validFreeEdges ;
for ( int i = 0 ; i < edges1 . count ( ) ; + + i ) {
if ( edges2 . contains ( edges1 [ i ] ) ) {
validFreeEdges < < edges1 [ i ] ;
}
}
return validFreeEdges ;
}
2019-04-05 20:39:19 +03:00
QString AbstractLayout : : layoutName ( const QString & fileName )
{
int lastSlash = fileName . lastIndexOf ( " / " ) ;
QString tempLayoutFile = fileName ;
QString layoutName = tempLayoutFile . remove ( 0 , lastSlash + 1 ) ;
int ext = layoutName . lastIndexOf ( " .layout.latte " ) ;
layoutName = layoutName . remove ( ext , 13 ) ;
return layoutName ;
}
void AbstractLayout : : loadConfig ( )
{
m_version = m_layoutGroup . readEntry ( " version " , 2 ) ;
2019-04-06 02:00:37 +03:00
m_launchers = m_layoutGroup . readEntry ( " launchers " , QStringList ( ) ) ;
m_lastUsedActivity = m_layoutGroup . readEntry ( " lastUsedActivity " , QString ( ) ) ;
m_preferredForShortcutsTouched = m_layoutGroup . readEntry ( " preferredForShortcutsTouched " , false ) ;
2019-04-05 20:39:19 +03:00
2020-03-25 02:30:30 +03:00
m_color = m_layoutGroup . readEntry ( " color " , QString ( " blue " ) ) ;
2020-04-23 19:35:26 +03:00
m_backgroundStyle = static_cast < BackgroundStyle > ( m_layoutGroup . readEntry ( " backgroundStyle " , ( int ) ColorBackgroundStyle ) ) ;
2019-04-05 20:39:19 +03:00
2020-07-30 18:38:12 +03:00
QString deprecatedTextColor = m_layoutGroup . readEntry ( " textColor " , QString ( ) ) ;
2020-03-25 02:30:30 +03:00
QString deprecatedBackground = m_layoutGroup . readEntry ( " background " , QString ( ) ) ;
if ( deprecatedBackground . startsWith ( " / " ) ) {
m_customBackground = deprecatedBackground ;
m_customTextColor = deprecatedTextColor ;
2020-04-23 19:35:26 +03:00
setBackgroundStyle ( PatternBackgroundStyle ) ;
2019-04-05 20:39:19 +03:00
2020-03-25 02:30:30 +03:00
m_layoutGroup . writeEntry ( " background " , QString ( ) ) ;
m_layoutGroup . writeEntry ( " textColor " , QString ( ) ) ;
saveConfig ( ) ;
} else {
2020-07-30 23:51:50 +03:00
m_customBackground = m_layoutGroup . readEntry ( " customBackground " , QString ( ) ) ;
m_customTextColor = m_layoutGroup . readEntry ( " customTextColor " , QString ( ) ) ;
2020-03-25 02:30:30 +03:00
}
2020-07-30 23:51:50 +03:00
m_icon = m_layoutGroup . readEntry ( " icon " , QString ( ) ) ;
2019-04-05 20:39:19 +03:00
}
void AbstractLayout : : saveConfig ( )
{
qDebug ( ) < < " abstract layout is saving... for layout: " < < m_layoutName ;
m_layoutGroup . writeEntry ( " version " , m_version ) ;
m_layoutGroup . writeEntry ( " color " , m_color ) ;
2019-04-06 02:00:37 +03:00
m_layoutGroup . writeEntry ( " launchers " , m_launchers ) ;
2020-03-25 02:30:30 +03:00
m_layoutGroup . writeEntry ( " backgroundStyle " , ( int ) m_backgroundStyle ) ;
m_layoutGroup . writeEntry ( " customBackground " , m_customBackground ) ;
m_layoutGroup . writeEntry ( " customTextColor " , m_customTextColor ) ;
2020-07-30 23:51:50 +03:00
m_layoutGroup . writeEntry ( " icon " , m_icon ) ;
2019-04-06 02:00:37 +03:00
m_layoutGroup . writeEntry ( " lastUsedActivity " , m_lastUsedActivity ) ;
m_layoutGroup . writeEntry ( " preferredForShortcutsTouched " , m_preferredForShortcutsTouched ) ;
2019-04-05 20:39:19 +03:00
}
2019-04-05 19:59:15 +03:00
}
}