Skip to content

Commit

Permalink
Replace QML based ControlColors with C++ version (that handles palett…
Browse files Browse the repository at this point in the history
…e changes better)
  • Loading branch information
timangus committed Oct 21, 2023
1 parent 2d4963b commit e1083e8
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 33 deletions.
1 change: 1 addition & 0 deletions source/app/ui/qml/AddBookmark.qml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

import app.graphia
import app.graphia.Controls
import app.graphia.Shared
import app.graphia.Shared.Controls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import QtQuick
import QtQuick.Controls

import app.graphia
import app.graphia.Shared.Controls

Rectangle
Expand Down
2 changes: 2 additions & 0 deletions source/shared/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ list(APPEND HEADERS
${CMAKE_CURRENT_LIST_DIR}/utils/performancecounter.h
${CMAKE_CURRENT_LIST_DIR}/utils/preferences.h
${CMAKE_CURRENT_LIST_DIR}/utils/progressable.h
${CMAKE_CURRENT_LIST_DIR}/utils/qmlcontrolcolors.h
${CMAKE_CURRENT_LIST_DIR}/utils/qmlenum.h
${CMAKE_CURRENT_LIST_DIR}/utils/qmlutils.h
${CMAKE_CURRENT_LIST_DIR}/utils/random.h
Expand Down Expand Up @@ -170,6 +171,7 @@ list(APPEND SHARED_SOURCES
${CMAKE_CURRENT_LIST_DIR}/utils/modelcompleter.cpp
${CMAKE_CURRENT_LIST_DIR}/utils/palettechangenotifier.cpp
${CMAKE_CURRENT_LIST_DIR}/utils/performancecounter.cpp
${CMAKE_CURRENT_LIST_DIR}/utils/qmlcontrolcolors.cpp
${CMAKE_CURRENT_LIST_DIR}/utils/random.cpp
${CMAKE_CURRENT_LIST_DIR}/utils/scopetimer.cpp
${CMAKE_CURRENT_LIST_DIR}/utils/showinfolder.cpp
Expand Down
31 changes: 0 additions & 31 deletions source/shared/ui/qml/app/graphia/Shared/Controls/ControlColors.qml

This file was deleted.

2 changes: 2 additions & 0 deletions source/shared/ui/qml/app/graphia/Shared/Controls/Outline.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import QtQuick

import app.graphia

Rectangle
{
property bool outlineVisible: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import QtQuick
import QtQuick.Controls

import app.graphia
import app.graphia.Shared.Controls

Rectangle
Expand Down
1 change: 0 additions & 1 deletion source/shared/ui/qml/app/graphia/Shared/Controls/qmldir
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ ToolBarSeparator 1.0 ToolBarSeparator.qml
Wizard 1.0 Wizard.qml

singleton FontPointSize 1.0 FontPointSize.qml
singleton ControlColors 1.0 ControlColors.qml
1 change: 0 additions & 1 deletion source/shared/ui/shared.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@
<file>qml/app/graphia/Shared/Utils.js</file>
<file>qml/app/graphia/Shared/qmldir</file>
<file>qml/app/graphia/Shared/Templates.qml</file>
<file>qml/app/graphia/Shared/Controls/ControlColors.qml</file>
</qresource>
</RCC>
85 changes: 85 additions & 0 deletions source/shared/utils/qmlcontrolcolors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* Copyright © 2013-2023 Graphia Technologies Ltd.
*
* This file is part of Graphia.
*
* Graphia 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 3 of the License, or
* (at your option) any later version.
*
* Graphia 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 Graphia. If not, see <http://www.gnu.org/licenses/>.
*/

#include "qmlcontrolcolors.h"

#include "shared/utils/static_block.h"

#include <QGuiApplication>
#include <QQmlEngine>
#include <QStyleHints>

QmlControlColors::QmlControlColors(QObject* parent) :
QObject(parent)
{
QCoreApplication::instance()->installEventFilter(this);

connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged,
this, &QmlControlColors::paletteChanged);

connect(this, &QmlControlColors::paletteChanged, [this]
{
_palette = QGuiApplication::palette();
_palette.setCurrentColorGroup(QPalette::ColorGroup::Active);
});
}

QObject* QmlControlColors::qmlInstance(QQmlEngine*, QJSEngine*)
{
return new QmlControlColors;
}

bool QmlControlColors::eventFilter(QObject* watched, QEvent* event)
{
bool watchedQObjectIsApplication = (watched == QCoreApplication::instance());

if(watchedQObjectIsApplication && event->type() == QEvent::ApplicationPaletteChange)
emit paletteChanged();

return QObject::eventFilter(watched, event);
}

QColor QmlControlColors::outline() const
{
auto colorScheme = QGuiApplication::styleHints()->colorScheme();

return colorScheme == Qt::ColorScheme::Light ?
_palette.color(QPalette::Mid) : _palette.color(QPalette::Dark);
}

QColor QmlControlColors::background() const
{
return _palette.color(QPalette::Light);
}

QColor QmlControlColors::tableRow1() const
{
return _palette.color(QPalette::Light);
}

QColor QmlControlColors::tableRow2() const
{
return _palette.color(QPalette::Button);
}

static_block
{
qmlRegisterSingletonType<QmlControlColors>(
APP_URI, APP_MAJOR_VERSION, APP_MINOR_VERSION, "ControlColors", &QmlControlColors::qmlInstance);
}

58 changes: 58 additions & 0 deletions source/shared/utils/qmlcontrolcolors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Copyright © 2013-2023 Graphia Technologies Ltd.
*
* This file is part of Graphia.
*
* Graphia 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 3 of the License, or
* (at your option) any later version.
*
* Graphia 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 Graphia. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef QMLCONTROLCOLORS_H
#define QMLCONTROLCOLORS_H

#include <QObject>
#include <QColor>
#include <QPalette>

class QQmlEngine;
class QJSEngine;

class QmlControlColors : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(QmlControlColors)

Q_PROPERTY(QColor outline READ outline NOTIFY paletteChanged)
Q_PROPERTY(QColor background READ background NOTIFY paletteChanged)
Q_PROPERTY(QColor tableRow1 READ tableRow1 NOTIFY paletteChanged)
Q_PROPERTY(QColor tableRow2 READ tableRow2 NOTIFY paletteChanged)

public:
explicit QmlControlColors(QObject* parent = nullptr);
static QObject* qmlInstance(QQmlEngine*, QJSEngine*);

QColor outline() const;
QColor background() const;
QColor tableRow1() const;
QColor tableRow2() const;

protected:
bool eventFilter(QObject* watched, QEvent* event) override;

private:
QPalette _palette;

signals:
void paletteChanged();
};

#endif // QMLCONTROLCOLORS_H

0 comments on commit e1083e8

Please sign in to comment.