qskinny/support/SkinnyNamespace.cpp

212 lines
5.3 KiB
C++
Raw Normal View History

2022-04-01 13:56:16 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
2023-04-06 09:23:37 +02:00
* SPDX-License-Identifier: BSD-3-Clause
2022-04-01 13:56:16 +02:00
*****************************************************************************/
#include "SkinnyNamespace.h"
#include <QskSetup.h>
#include <QskSkinManager.h>
#include <QskSkin.h>
#include <QskSkinTransition.h>
#include <QskAnimationHint.h>
#include <QGuiApplication>
2022-07-14 11:52:25 +02:00
#include <QByteArray>
2022-04-01 13:56:16 +02:00
#define STRINGIFY(x) #x
#define STRING(x) STRINGIFY(x)
2022-07-14 11:52:25 +02:00
#if defined( PLUGIN_PATH )
#include <QByteArray>
#include <QDir>
#define STRINGIFY(x) #x
#define STRING(x) STRINGIFY(x)
static int initPluginPath()
{
const char env[] = "QT_PLUGIN_PATH";
QByteArray value = qgetenv( env );
if ( !value.isEmpty() )
{
if ( QChar( value.at( value.size() - 1 ) ) != QDir::listSeparator() )
value += QDir::listSeparator().toLatin1();
}
value += STRING( PLUGIN_PATH );
qputenv( env, value );
return 0;
}
// some plugins are loaded before entering QCoreApplication
static bool pluginPath = initPluginPath();
#endif // PLUGIN_PATH
2022-04-01 13:56:16 +02:00
#if defined( ENSURE_SKINS )
#include <squiek/QskSquiekSkinFactory.h>
2022-07-05 08:36:25 +02:00
#include <material3/QskMaterial3SkinFactory.h>
2023-06-17 12:23:34 +02:00
#include <fluent2/QskFluent2SkinFactory.h>
2022-04-01 13:56:16 +02:00
static void initSkins()
{
auto skinNames = qskSkinManager->skinNames();
if ( skinNames.isEmpty() )
2022-04-01 13:56:16 +02:00
{
/*
To avoid having problems with not finding the skin plugins
we manually add them here.
*/
qskSkinManager->registerFactory( "SquiekFactory", new QskSquiekSkinFactory() );
2022-07-05 08:36:25 +02:00
qskSkinManager->registerFactory( "Material3Factory", new QskMaterial3SkinFactory() );
2023-06-17 12:23:34 +02:00
qskSkinManager->registerFactory( "Fluent2Factory", new QskFluent2SkinFactory() );
2022-04-01 13:56:16 +02:00
qWarning() << "Couldn't find skin plugins, adding some manually.";
skinNames = qskSkinManager->skinNames();
2022-04-01 13:56:16 +02:00
}
if ( !skinNames.isEmpty() )
qskSetup->setSkin( skinNames[0] );
2022-04-01 13:56:16 +02:00
}
Q_COREAPP_STARTUP_FUNCTION( initSkins )
#endif
#define ENSURE_FONTS
#if defined( ENSURE_FONTS )
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
#include <QFontDatabase>
#include <QElapsedTimer>
2022-04-01 13:56:16 +02:00
#endif
static void preloadFonts()
{
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
2022-04-01 13:56:16 +02:00
QElapsedTimer timer;
timer.start();
QFontDatabase(); // deprecated and doing nothing since Qt6
2022-04-01 13:56:16 +02:00
const auto elapsed = timer.elapsed();
if ( elapsed > 20 )
{
qWarning() << "Loading fonts needed" << elapsed << "ms"
<< "- usually because of creating a font cache.";
}
#endif
}
static void initFonts()
{
if ( !qobject_cast< QGuiApplication* >( qApp ) )
return; // no fonts needed
#ifdef FONTCONFIG_FILE
const char env[] = "FONTCONFIG_FILE";
if ( !qEnvironmentVariableIsSet( env ) )
qputenv( env, STRING( FONTCONFIG_FILE ) );
#endif
preloadFonts();
2022-04-01 13:56:16 +02:00
/*
The default initialization in QskSkin sets up its font table
with using the application font for the default font role.
*/
QGuiApplication::setFont( QFont( "DejaVuSans", 12 ) );
}
#endif
Q_COREAPP_STARTUP_FUNCTION( initFonts )
void Skinny::changeSkin( QskAnimationHint hint )
2023-05-16 11:12:11 +02:00
{
const auto names = qskSkinManager->skinNames();
if ( names.size() > 1 )
{
auto index = names.indexOf( qskSetup->skinName() );
index = ( index + 1 ) % names.size();
setSkin( index, hint );
}
}
void Skinny::setSkin( int index, QskAnimationHint hint )
2022-04-01 13:56:16 +02:00
{
const auto names = qskSkinManager->skinNames();
if ( names.size() <= 1 )
return;
2023-05-16 11:12:11 +02:00
if ( index == names.indexOf( qskSetup->skinName() ) )
return;
2022-04-01 13:56:16 +02:00
auto oldSkin = qskSetup->skin();
if ( oldSkin->parent() == qskSetup )
oldSkin->setParent( nullptr ); // otherwise setSkin deletes it
if ( auto newSkin = qskSetup->setSkin( names[ index ] ) )
{
QskSkinTransition transition;
//transition.setMask( QskAspect::Color ); // Metrics are flickering -> TODO
transition.setSourceSkin( oldSkin );
transition.setTargetSkin( newSkin );
transition.setAnimation( hint );
transition.process();
if ( oldSkin->parent() == nullptr )
delete oldSkin;
}
}
void Skinny::changeFonts( int increment )
{
2022-04-01 13:56:16 +02:00
auto skin = qskSetup->skin();
2022-04-01 13:56:16 +02:00
const auto fonts = skin->fonts();
2022-04-01 13:56:16 +02:00
for ( auto it = fonts.begin(); it != fonts.end(); ++it )
{
2022-04-01 13:56:16 +02:00
auto role = it->first;
auto font = it->second;
2022-04-01 13:56:16 +02:00
if ( font.pixelSize() > 0 )
{
2022-04-01 13:56:16 +02:00
const auto newSize = font.pixelSize() + increment;
if ( newSize > 0 )
font.setPixelSize( newSize );
}
else
{
2022-04-01 13:56:16 +02:00
const auto newSize = font.pointSizeF() + increment;
if ( newSize > 0 )
font.setPointSizeF( font.pointSizeF() + increment );
}
2022-04-01 13:56:16 +02:00
skin->setFont( role, font );
}
2022-04-01 13:56:16 +02:00
Q_EMIT qskSetup->skinChanged( skin );
}
2022-04-12 14:13:59 +02:00
void Skinny::init()
{
/*
a dummy call - has no reason beside, that applications can load
the lib and all initializaion take place
*/
}