diff --git a/examples/colorswitch/Theme.cpp b/examples/colorswitch/Theme.cpp new file mode 100644 index 00000000..0a1f8766 --- /dev/null +++ b/examples/colorswitch/Theme.cpp @@ -0,0 +1,116 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#include "Theme.h" + +#include +#include +#include +#include +#include +#include + +static void qskResetColors( QskSkin* skin, const QColor& accent ) +{ + skin->resetColors( accent ); + + /* + The current implementation of the skins is not that good + and we don't have support for customizing them by a minimal set + of attributes. So we do some manual extra work here + */ + skin->setColor( QskListView::CellSelected, accent.darker( 130 ) ); + skin->setColor( QskFocusIndicator::Panel | QskAspect::Border, accent.darker( 150 ) ); +} + +namespace +{ + class SkinTransition : public QskSkinTransition + { + public: + SkinTransition( const QColor& accent ): + m_accent( accent ) + { + } + + protected: + virtual void updateSkin( QskSkin*, QskSkin* newSkin ) override final + { + qskResetColors( newSkin, m_accent ); + } + + private: + const QColor m_accent; + }; +} + +Theme::Theme( QObject* parent ): + QObject( parent ), + m_accent( qskSetup->skin()->color( QskAspect::Color ) ) +{ + connect( qskSetup, &QskSetup::skinChanged, + [this]( QskSkin* ) { updateColors(); } ); + + connect( qskSetup, &QskSetup::skinChanged, + [this]( QskSkin* ) { Q_EMIT skinChanged(); } ); +} + +void Theme::setAccent( QColor color ) +{ + if ( m_accent != color ) + { + m_accent = color; + updateColors(); + + Q_EMIT accentChanged(); + } +} + +QColor Theme::accent() const +{ + return m_accent; +} + +void Theme::setSkin( const QString& skinName ) +{ + if ( skinName == qskSetup->skinName() ) + return; + + QskSkin* oldSkin = qskSetup->skin(); + if ( oldSkin->parent() == qskSetup ) + oldSkin->setParent( nullptr ); // otherwise setSkin deletes it + + QskSkin* newSkin = qskSetup->setSkin( skinName ); + + SkinTransition transition( m_accent ); + + transition.setSourceSkin( oldSkin ); + transition.setTargetSkin( newSkin ); + transition.setAnimation( 500 ); + + transition.process(); + + if ( oldSkin->parent() == nullptr ) + delete oldSkin; +} + +QString Theme::skin() const +{ + return qskSetup->skinName(); +} + +void Theme::updateColors() +{ + SkinTransition transition( m_accent ); + + transition.setMask( SkinTransition::Color ); + transition.setSourceSkin( qskSetup->skin() ); + transition.setTargetSkin( qskSetup->skin() ); + transition.setAnimation( 500 ); + + transition.process(); +} + +#include "moc_Theme.cpp" diff --git a/examples/colorswitch/Theme.h b/examples/colorswitch/Theme.h new file mode 100644 index 00000000..9bfb45cb --- /dev/null +++ b/examples/colorswitch/Theme.h @@ -0,0 +1,38 @@ +/****************************************************************************** + * QSkinny - Copyright (C) 2016 Uwe Rathmann + * This file may be used under the terms of the 3-clause BSD License + *****************************************************************************/ + +#ifndef THEME_H +#define THEME_H 1 + +#include +#include + +class Theme : public QObject +{ + Q_OBJECT + + Q_PROPERTY( QColor accent READ accent WRITE setAccent NOTIFY accentChanged ) + Q_PROPERTY( QString skin READ skin WRITE setSkin NOTIFY skinChanged ) + +public: + Theme( QObject* parent = nullptr ); + + void setAccent( QColor color ); + QColor accent() const; + + void setSkin( const QString& ); + QString skin() const; + +Q_SIGNALS: + void accentChanged(); + void skinChanged(); + +private: + void updateColors(); + + QColor m_accent; +}; + +#endif diff --git a/examples/colorswitch/colorswitch.pro b/examples/colorswitch/colorswitch.pro index e19c3bf9..580c52cb 100644 --- a/examples/colorswitch/colorswitch.pro +++ b/examples/colorswitch/colorswitch.pro @@ -5,5 +5,9 @@ TARGET = colorswitch RESOURCES += \ colorswitch.qrc +HEADERS += \ + Theme.h + SOURCES += \ + Theme.cpp \ main.cpp diff --git a/examples/colorswitch/colorswitch.qml b/examples/colorswitch/colorswitch.qml index 120dd9b8..9896dc73 100644 --- a/examples/colorswitch/colorswitch.qml +++ b/examples/colorswitch/colorswitch.qml @@ -6,14 +6,14 @@ Main { id: main - property var accentColors: [ "red", "lightgreen", "#66336699" ] - - skin: listBox.entries[ listBox.selectedRow ] inputPanel: embeddedInputPanel + property var accentColors: [ "red", "lightgreen", "#66336699" ] + Theme { accent: accentColors[ tabBar.currentIndex < 0 ? 0 : tabBar.currentIndex ] + skin: listBox.entries[ listBox.selectedRow ] } Window diff --git a/examples/colorswitch/main.cpp b/examples/colorswitch/main.cpp index fb18a2e8..288cbe93 100644 --- a/examples/colorswitch/main.cpp +++ b/examples/colorswitch/main.cpp @@ -3,92 +3,17 @@ * This file may be used under the terms of the 3-clause BSD License *****************************************************************************/ +#include "Theme.h" + #include #include #include -#include -#include -#include -#include #include -#include -#include -#include - #include #include -class SkinTransition: public QskSkinTransition -{ -public: - SkinTransition( const QColor& accent ): - m_accent( accent ) - { - } - -protected: - virtual void updateSkin( QskSkin*, QskSkin* skin ) override final - { - skin->resetColors( m_accent ); - skin->setColor( QskListView::CellSelected, m_accent.darker( 130 ) ); - skin->setColor( QskFocusIndicator::Panel | QskAspect::Border, m_accent.darker( 150 ) ); - } - -private: - const QColor m_accent; -}; - -class Theme: public QObject -{ - Q_OBJECT - Q_PROPERTY( QColor accent READ accent WRITE setAccent NOTIFY accentChanged ) - -public: - Theme( QObject* parent = nullptr ): - QObject( parent ), - m_accent( qskSetup->skin()->color( QskAspect::Color ) ) - { - connect( qskSetup, &QskSetup::skinChanged, - [this]( QskSkin* ) { updateColors(); } ); - } - - void setAccent( QColor color ) - { - if ( m_accent != color ) - { - m_accent = color; - updateColors(); - - Q_EMIT accentChanged(); - } - } - - QColor accent() const - { - return m_accent; - } - -Q_SIGNALS: - void accentChanged(); - -private: - void updateColors() - { - SkinTransition transition( m_accent ); - - transition.setMask( SkinTransition::Color ); - transition.setSourceSkin( qskSetup->skin() ); - transition.setTargetSkin( qskSetup->skin() ); - transition.setAnimation( 500 ); - - transition.process(); - } - - QColor m_accent; -}; - int main( int argc, char* argv[] ) { #ifdef ITEM_STATISTICS @@ -110,5 +35,3 @@ int main( int argc, char* argv[] ) return app.exec(); } - -#include "main.moc"