diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a9a25730..184f7389 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,113 +1,113 @@ project(kirigami) if (NOT STATIC_LIBRARY) ecm_create_qm_loader(kirigami_QM_LOADER libkirigami2plugin_qt) else() set(KIRIGAMI_STATIC_FILES libkirigami/basictheme.cpp libkirigami/platformtheme.cpp libkirigami/tabletmodewatcher.cpp libkirigami/kirigamipluginfactory.cpp) endif() include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libkirigami ${CMAKE_CURRENT_BINARY_DIR}/libkirigami) set(kirigami_SRCS kirigamiplugin.cpp columnview.cpp enums.cpp delegaterecycler.cpp icon.cpp settings.cpp formlayoutattached.cpp pagepool.cpp scenepositionattached.cpp mnemonicattached.cpp wheelhandler.cpp - elevatedrectangle.cpp - scenegraph/elevatedrectanglenode.cpp - scenegraph/elevatedrectanglematerial.cpp + shadowedrectangle.cpp + scenegraph/shadowedrectanglenode.cpp + scenegraph/shadowedrectanglematerial.cpp ${kirigami_QM_LOADER} ${KIRIGAMI_STATIC_FILES} ) qt5_add_resources(SHADERS scenegraph/shaders.qrc) add_subdirectory(libkirigami) if(STATIC_LIBRARY) # `rcc` is a bit dumb and isn't designed to use auto generated files, to # avoid poluting the source directory, use absolute paths set(kirigami_QML_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../) # First, pre-process the QRC to add the files associated with the right Qt # version. configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/../kirigami.qrc.in ${CMAKE_CURRENT_BINARY_DIR}/../kirigami.qrc @ONLY ) # When using the static library, all QML files need to be shipped within the # .a file. qt5_add_resources( RESOURCES ${CMAKE_CURRENT_BINARY_DIR}/../kirigami.qrc ) if (UNIX AND NOT ANDROID AND NOT(APPLE) AND NOT(DISABLE_DBUS)) qt5_add_dbus_interface(kirigami_SRCS libkirigami/org.kde.KWin.TabletModeManager.xml tabletmodemanager_interface) endif() endif(STATIC_LIBRARY) add_library(kirigamiplugin ${kirigami_SRCS} ${RESOURCES} ${SHADERS}) if(STATIC_LIBRARY) SET_TARGET_PROPERTIES(kirigamiplugin PROPERTIES AUTOMOC_MOC_OPTIONS -Muri=org.kde.kirigami) if (UNIX AND NOT ANDROID AND NOT(APPLE) AND NOT(DISABLE_DBUS)) set(Kirigami_EXTRA_LIBS Qt5::DBus) else() set(Kirigami_EXTRA_LIBS "") endif() else(STATIC_LIBRARY) set(Kirigami_EXTRA_LIBS KF5::Kirigami2) endif(STATIC_LIBRARY) target_link_libraries(kirigamiplugin PUBLIC Qt5::Core PRIVATE ${Kirigami_EXTRA_LIBS} Qt5::Qml Qt5::Quick Qt5::QuickControls2 ) if (NOT STATIC_LIBRARY) add_custom_target(copy_to_bin ALL COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/bin/org/kde/kirigami.2/ COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/controls ${CMAKE_BINARY_DIR}/bin/org/kde/kirigami.2/ COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/styles ${CMAKE_BINARY_DIR}/bin/org/kde/kirigami.2/styles COMMAND ${CMAKE_COMMAND} -E copy $ ${CMAKE_BINARY_DIR}/bin/org/kde/kirigami.2/ ) install(DIRECTORY controls/ DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/kirigami.2) if (DESKTOP_ENABLED) install(DIRECTORY styles/org.kde.desktop DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/kirigami.2/styles) endif() install(DIRECTORY styles/Material DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/kirigami.2/styles) install(FILES ${platformspecific} DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/kirigami.2) include(ECMGeneratePriFile) ecm_generate_pri_file(BASE_NAME Kirigami2 LIB_NAME KF5Kirigami2 DEPS "core qml quick svg" FILENAME_VAR PRI_FILENAME ) install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) endif(NOT STATIC_LIBRARY) install(TARGETS kirigamiplugin DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/kirigami.2) diff --git a/src/kirigamiplugin.cpp b/src/kirigamiplugin.cpp index 83de9b0f..39c251c6 100644 --- a/src/kirigamiplugin.cpp +++ b/src/kirigamiplugin.cpp @@ -1,251 +1,251 @@ /* * SPDX-FileCopyrightText: 2009 Alan Alpert * SPDX-FileCopyrightText: 2010 Ménard Alexis * SPDX-FileCopyrightText: 2010 Marco Martin * * SPDX-License-Identifier: LGPL-2.0-or-later */ #include "kirigamiplugin.h" #include "columnview.h" #include "enums.h" #include "icon.h" #include "settings.h" #include "formlayoutattached.h" #include "mnemonicattached.h" #include "delegaterecycler.h" #include "pagepool.h" #include "scenepositionattached.h" #include "wheelhandler.h" -#include "elevatedrectangle.h" +#include "shadowedrectangle.h" #include #include #include #include #include #include #include "libkirigami/platformtheme.h" static QString s_selectedStyle; //Q_INIT_RESOURCE(kirigami); #ifdef KIRIGAMI_BUILD_TYPE_STATIC #include #endif class CopyHelperPrivate : public QObject { Q_OBJECT public: Q_INVOKABLE static void copyTextToClipboard(const QString& text) { qGuiApp->clipboard()->setText(text); } }; // we can't do this in the plugin object directly, as that can live in a different thread // and event filters are only allowed in the same thread as the filtered object class LanguageChangeEventFilter : public QObject { Q_OBJECT public: bool eventFilter(QObject *receiver, QEvent *event) override { if (event->type() == QEvent::LanguageChange && receiver == QCoreApplication::instance()) { emit languageChangeEvent(); } return QObject::eventFilter(receiver, event); } Q_SIGNALS: void languageChangeEvent(); }; KirigamiPlugin::KirigamiPlugin(QObject *parent) : QQmlExtensionPlugin(parent) { auto filter = new LanguageChangeEventFilter; filter->moveToThread(QCoreApplication::instance()->thread()); QCoreApplication::instance()->installEventFilter(filter); connect(filter, &LanguageChangeEventFilter::languageChangeEvent, this, &KirigamiPlugin::languageChangeEvent); } QUrl KirigamiPlugin::componentUrl(const QString &fileName) const { for (const QString &style : qAsConst(m_stylesFallbackChain)) { const QString candidate = QStringLiteral("styles/") + style + QLatin1Char('/') + fileName; if (QFile::exists(resolveFilePath(candidate))) { #ifdef KIRIGAMI_BUILD_TYPE_STATIC return QUrl(QStringLiteral("qrc:/org/kde/kirigami/styles/") + style + QLatin1Char('/') + fileName); #else return QUrl(resolveFileUrl(candidate)); #endif } } #ifdef KIRIGAMI_BUILD_TYPE_STATIC return QUrl(QStringLiteral("qrc:/org/kde/kirigami/") + fileName); #else return QUrl(resolveFileUrl(fileName)); #endif } void KirigamiPlugin::registerTypes(const char *uri) { #if defined(Q_OS_ANDROID) && QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) QResource::registerResource(QStringLiteral("assets:/android_rcc_bundle.rcc")); #endif Q_ASSERT(QLatin1String(uri) == QLatin1String("org.kde.kirigami")); const QString style = QQuickStyle::name(); if (QIcon::themeName().isEmpty() && !qEnvironmentVariableIsSet("XDG_CURRENT_DESKTOP")) { QIcon::setThemeSearchPaths({resolveFilePath(QStringLiteral(".")), QStringLiteral(":/icons")}); QIcon::setThemeName(QStringLiteral("breeze-internal")); } #if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS) //org.kde.desktop.plasma is a couple of files that fall back to desktop by purpose if ((style.isEmpty() || style == QStringLiteral("org.kde.desktop.plasma")) && QFile::exists(resolveFilePath(QStringLiteral("/styles/org.kde.desktop")))) { m_stylesFallbackChain.prepend(QStringLiteral("org.kde.desktop")); } #elif defined(Q_OS_ANDROID) if (!m_stylesFallbackChain.contains(QLatin1String("Material"))) { m_stylesFallbackChain.prepend(QStringLiteral("Material")); } #else // do we have an iOS specific style? if (!m_stylesFallbackChain.contains(QLatin1String("Material"))) { m_stylesFallbackChain.prepend(QStringLiteral("Material")); } #endif if (!style.isEmpty() && QFile::exists(resolveFilePath(QStringLiteral("/styles/") + style)) && !m_stylesFallbackChain.contains(style)) { m_stylesFallbackChain.prepend(style); //if we have plasma deps installed, use them for extra integration if (style == QStringLiteral("org.kde.desktop") && QFile::exists(resolveFilePath(QStringLiteral("/styles/org.kde.desktop.plasma")))) { m_stylesFallbackChain.prepend(QStringLiteral("org.kde.desktop.plasma")); } } else { #if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS) m_stylesFallbackChain.prepend(QStringLiteral("org.kde.desktop")); #endif } //At this point the fallback chain will be selected->org.kde.desktop->Fallback s_selectedStyle = m_stylesFallbackChain.first(); qmlRegisterSingletonType(uri, 2, 0, "Settings", [](QQmlEngine *e, QJSEngine*) -> QObject* { Settings *settings = Settings::self(); //singleton managed internally, qml should never delete it e->setObjectOwnership(settings, QQmlEngine::CppOwnership); settings->setStyle(s_selectedStyle); return settings; } ); qmlRegisterUncreatableType(uri, 2, 0, "ApplicationHeaderStyle", QStringLiteral("Cannot create objects of type ApplicationHeaderStyle")); //old legacy retrocompatible Theme qmlRegisterSingletonType(componentUrl(QStringLiteral("Theme.qml")), uri, 2, 0, "Theme"); qmlRegisterSingletonType(componentUrl(QStringLiteral("Units.qml")), uri, 2, 0, "Units"); qmlRegisterType(componentUrl(QStringLiteral("Action.qml")), uri, 2, 0, "Action"); qmlRegisterType(componentUrl(QStringLiteral("AbstractApplicationHeader.qml")), uri, 2, 0, "AbstractApplicationHeader"); qmlRegisterType(componentUrl(QStringLiteral("AbstractApplicationWindow.qml")), uri, 2, 0, "AbstractApplicationWindow"); qmlRegisterType(componentUrl(QStringLiteral("AbstractListItem.qml")), uri, 2, 0, "AbstractListItem"); qmlRegisterType(componentUrl(QStringLiteral("ApplicationHeader.qml")), uri, 2, 0, "ApplicationHeader"); qmlRegisterType(componentUrl(QStringLiteral("ToolBarApplicationHeader.qml")), uri, 2, 0, "ToolBarApplicationHeader"); qmlRegisterType(componentUrl(QStringLiteral("ApplicationWindow.qml")), uri, 2, 0, "ApplicationWindow"); qmlRegisterType(componentUrl(QStringLiteral("BasicListItem.qml")), uri, 2, 0, "BasicListItem"); qmlRegisterType(componentUrl(QStringLiteral("OverlayDrawer.qml")), uri, 2, 0, "OverlayDrawer"); qmlRegisterType(componentUrl(QStringLiteral("ContextDrawer.qml")), uri, 2, 0, "ContextDrawer"); qmlRegisterType(componentUrl(QStringLiteral("GlobalDrawer.qml")), uri, 2, 0, "GlobalDrawer"); qmlRegisterType(componentUrl(QStringLiteral("Heading.qml")), uri, 2, 0, "Heading"); qmlRegisterType(componentUrl(QStringLiteral("Separator.qml")), uri, 2, 0, "Separator"); qmlRegisterType(componentUrl(QStringLiteral("PageRow.qml")), uri, 2, 0, "PageRow"); qmlRegisterType(uri, 2, 0, "Icon"); qmlRegisterType(componentUrl(QStringLiteral("Label.qml")), uri, 2, 0, "Label"); //TODO: uncomment for 2.3 release //qmlRegisterTypeNotAvailable(uri, 2, 3, "Label", "Label type not supported anymore, use QtQuick.Controls.Label 2.0 instead"); qmlRegisterType(componentUrl(QStringLiteral("OverlaySheet.qml")), uri, 2, 0, "OverlaySheet"); qmlRegisterType(componentUrl(QStringLiteral("Page.qml")), uri, 2, 0, "Page"); qmlRegisterType(componentUrl(QStringLiteral("ScrollablePage.qml")), uri, 2, 0, "ScrollablePage"); qmlRegisterType(componentUrl(QStringLiteral("SplitDrawer.qml")), uri, 2, 0, "SplitDrawer"); qmlRegisterType(componentUrl(QStringLiteral("SwipeListItem.qml")), uri, 2, 0, "SwipeListItem"); //2.1 qmlRegisterType(componentUrl(QStringLiteral("AbstractItemViewHeader.qml")), uri, 2, 1, "AbstractItemViewHeader"); qmlRegisterType(componentUrl(QStringLiteral("ItemViewHeader.qml")), uri, 2, 1, "ItemViewHeader"); qmlRegisterType(componentUrl(QStringLiteral("AbstractApplicationItem.qml")), uri, 2, 1, "AbstractApplicationItem"); qmlRegisterType(componentUrl(QStringLiteral("ApplicationItem.qml")), uri, 2, 1, "ApplicationItem"); //2.2 //Theme changed from a singleton to an attached property qmlRegisterUncreatableType(uri, 2, 2, "Theme", QStringLiteral("Cannot create objects of type Theme, use it as an attached property")); //2.3 qmlRegisterType(componentUrl(QStringLiteral("FormLayout.qml")), uri, 2, 3, "FormLayout"); qmlRegisterUncreatableType(uri, 2, 3, "FormData", QStringLiteral("Cannot create objects of type FormData, use it as an attached property")); qmlRegisterUncreatableType(uri, 2, 3, "MnemonicData", QStringLiteral("Cannot create objects of type MnemonicData, use it as an attached property")); //2.4 qmlRegisterType(componentUrl(QStringLiteral("AbstractCard.qml")), uri, 2, 4, "AbstractCard"); qmlRegisterType(componentUrl(QStringLiteral("Card.qml")), uri, 2, 4, "Card"); qmlRegisterType(componentUrl(QStringLiteral("CardsListView.qml")), uri, 2, 4, "CardsListView"); qmlRegisterType(componentUrl(QStringLiteral("CardsGridView.qml")), uri, 2, 4, "CardsGridView"); qmlRegisterType(componentUrl(QStringLiteral("CardsLayout.qml")), uri, 2, 4, "CardsLayout"); qmlRegisterType(componentUrl(QStringLiteral("InlineMessage.qml")), uri, 2, 4, "InlineMessage"); qmlRegisterUncreatableType(uri, 2, 4, "MessageType", QStringLiteral("Cannot create objects of type MessageType")); qmlRegisterType(uri, 2, 4, "DelegateRecycler"); //2.5 qmlRegisterType(componentUrl(QStringLiteral("ListItemDragHandle.qml")), uri, 2, 5, "ListItemDragHandle"); qmlRegisterType(componentUrl(QStringLiteral("ActionToolBar.qml")), uri, 2, 5, "ActionToolBar"); qmlRegisterUncreatableType(uri, 2, 5, "ScenePosition", QStringLiteral("Cannot create objects of type ScenePosition, use it as an attached property")); //2.6 qmlRegisterType(componentUrl(QStringLiteral("AboutPage.qml")), uri, 2, 6, "AboutPage"); qmlRegisterType(componentUrl(QStringLiteral("LinkButton.qml")), uri, 2, 6, "LinkButton"); qmlRegisterType(componentUrl(QStringLiteral("UrlButton.qml")), uri, 2, 6, "UrlButton"); qmlRegisterSingletonType("org.kde.kirigami.private", 2, 6, "CopyHelperPrivate", [] (QQmlEngine*, QJSEngine*) -> QObject* { return new CopyHelperPrivate; }); //2.7 qmlRegisterType(uri, 2, 7, "ColumnView"); qmlRegisterType(componentUrl(QStringLiteral("ActionTextField.qml")), uri, 2, 7, "ActionTextField"); //2.8 qmlRegisterType(componentUrl(QStringLiteral("SearchField.qml")), uri, 2, 8, "SearchField"); qmlRegisterType(componentUrl(QStringLiteral("PasswordField.qml")), uri, 2, 8, "PasswordField"); //2.9 qmlRegisterType(uri, 2, 9, "WheelHandler"); qmlRegisterUncreatableType(uri, 2, 9, "WheelEvent", QStringLiteral("Cannot create objects of type WheelEvent.")); //2.10 qmlRegisterType(componentUrl(QStringLiteral("ListSectionHeader.qml")), uri, 2, 10, "ListSectionHeader"); // 2.11 qmlRegisterType(uri, 2, 11, "PagePool"); qmlRegisterType(componentUrl(QStringLiteral("PagePoolAction.qml")), uri, 2, 11, "PagePoolAction"); //TODO: remove qmlRegisterType(componentUrl(QStringLiteral("SwipeListItem2.qml")), uri, 2, 11, "SwipeListItem2"); // 2.12 - qmlRegisterType(uri, 2, 12, "ElevatedRectangle"); + qmlRegisterType(uri, 2, 12, "ShadowedRectangle"); qmlProtectModule(uri, 2); } void KirigamiPlugin::initializeEngine(QQmlEngine *engine, const char *uri) { Q_UNUSED(uri); connect(this, &KirigamiPlugin::languageChangeEvent, engine, &QQmlEngine::retranslate); } #include "kirigamiplugin.moc" diff --git a/src/scenegraph/shaders.qrc b/src/scenegraph/shaders.qrc index 28556fd0..f0f8f22c 100644 --- a/src/scenegraph/shaders.qrc +++ b/src/scenegraph/shaders.qrc @@ -1,11 +1,11 @@ header_es.glsl header_desktop.glsl header_desktop_core.glsl - elevatedrectangle.vert - elevatedrectangle.frag + shadowedrectangle.vert + shadowedrectangle.frag diff --git a/src/scenegraph/elevatedrectangle.frag b/src/scenegraph/shadowedrectangle.frag similarity index 100% rename from src/scenegraph/elevatedrectangle.frag rename to src/scenegraph/shadowedrectangle.frag diff --git a/src/scenegraph/elevatedrectangle.vert b/src/scenegraph/shadowedrectangle.vert similarity index 100% rename from src/scenegraph/elevatedrectangle.vert rename to src/scenegraph/shadowedrectangle.vert diff --git a/src/scenegraph/elevatedrectanglematerial.cpp b/src/scenegraph/shadowedrectanglematerial.cpp similarity index 86% rename from src/scenegraph/elevatedrectanglematerial.cpp rename to src/scenegraph/shadowedrectanglematerial.cpp index ec8061b8..a2501865 100644 --- a/src/scenegraph/elevatedrectanglematerial.cpp +++ b/src/scenegraph/shadowedrectanglematerial.cpp @@ -1,114 +1,114 @@ /* * Copyright 2020 Arjen Hiemstra * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see */ -#include "elevatedrectanglematerial.h" +#include "shadowedrectanglematerial.h" #include -ElevatedRectangleMaterial::ElevatedRectangleMaterial() +ShadowedRectangleMaterial::ShadowedRectangleMaterial() { setFlag(QSGMaterial::Blending, true); } -QSGMaterialShader* ElevatedRectangleMaterial::createShader() const +QSGMaterialShader* ShadowedRectangleMaterial::createShader() const { return new ElevatedRectangleShader{}; } -QSGMaterialType* ElevatedRectangleMaterial::type() const +QSGMaterialType* ShadowedRectangleMaterial::type() const { static QSGMaterialType type; return &type; } -int ElevatedRectangleMaterial::compare(const QSGMaterial *other) const +int ShadowedRectangleMaterial::compare(const QSGMaterial *other) const { - auto material = static_cast(other); + auto material = static_cast(other); if (material->color == color && material->shadowColor == shadowColor && material->offset == offset && material->aspect == aspect && qFuzzyCompare(material->size, size) && qFuzzyCompare(material->radius, radius)) { return 0; } return QSGMaterial::compare(other); } ElevatedRectangleShader::ElevatedRectangleShader() { auto header = QOpenGLContext::currentContext()->isOpenGLES() ? QStringLiteral("header_es.glsl") : QStringLiteral("header_desktop.glsl"); auto shaderRoot = QStringLiteral(":/org/kde/kirigami/shaders/"); setShaderSourceFiles(QOpenGLShader::Vertex, { shaderRoot + header, - shaderRoot + QStringLiteral("elevatedrectangle.vert") + shaderRoot + QStringLiteral("shadowedrectangle.vert") }); setShaderSourceFiles(QOpenGLShader::Fragment, { shaderRoot + header, - shaderRoot + QStringLiteral("elevatedrectangle.frag") + shaderRoot + QStringLiteral("shadowedrectangle.frag") }); } const char *const * ElevatedRectangleShader::attributeNames() const { static char const *const names[] = {"in_vertex", "in_uv", nullptr}; return names; } void ElevatedRectangleShader::initialize() { QSGMaterialShader::initialize(); m_matrixLocation = program()->uniformLocation("matrix"); m_aspectLocation = program()->uniformLocation("aspect"); m_opacityLocation = program()->uniformLocation("opacity"); m_sizeLocation = program()->uniformLocation("size"); m_radiusLocation = program()->uniformLocation("radius"); m_colorLocation = program()->uniformLocation("color"); m_shadowColorLocation = program()->uniformLocation("shadowColor"); m_offsetLocation = program()->uniformLocation("offset"); } void ElevatedRectangleShader::updateState(const QSGMaterialShader::RenderState& state, QSGMaterial* newMaterial, QSGMaterial* oldMaterial) { auto p = program(); if (state.isMatrixDirty()) { p->setUniformValue(m_matrixLocation, state.combinedMatrix()); } if (state.isOpacityDirty()) { p->setUniformValue(m_opacityLocation, state.opacity()); } if (!oldMaterial || newMaterial->compare(oldMaterial) != 0 || state.isCachedMaterialDataDirty()) { - auto material = static_cast(newMaterial); + auto material = static_cast(newMaterial); p->setUniformValue(m_aspectLocation, material->aspect); p->setUniformValue(m_sizeLocation, material->size); p->setUniformValue(m_radiusLocation, material->radius); p->setUniformValue(m_colorLocation, material->color); p->setUniformValue(m_shadowColorLocation, material->shadowColor); p->setUniformValue(m_offsetLocation, material->offset); } } diff --git a/src/scenegraph/elevatedrectanglematerial.h b/src/scenegraph/shadowedrectanglematerial.h similarity index 90% rename from src/scenegraph/elevatedrectanglematerial.h rename to src/scenegraph/shadowedrectanglematerial.h index a85adcb0..79fb0d45 100644 --- a/src/scenegraph/elevatedrectanglematerial.h +++ b/src/scenegraph/shadowedrectanglematerial.h @@ -1,65 +1,62 @@ /* * Copyright 2020 Arjen Hiemstra * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see */ -#ifndef ELEVATEDRECTANGLEMATERIAL_H -#define ELEVATEDRECTANGLEMATERIAL_H +#pragma once #include #include -class ElevatedRectangleMaterial : public QSGMaterial +class ShadowedRectangleMaterial : public QSGMaterial { public: - ElevatedRectangleMaterial(); + ShadowedRectangleMaterial(); QSGMaterialShader* createShader() const override; QSGMaterialType* type() const override; int compare(const QSGMaterial* other) const override; QVector2D aspect = QVector2D{1.0, 1.0}; float size = 0.0; float radius = 0.0; QColor color = Qt::white; QColor shadowColor = Qt::black; QVector2D offset; }; class ElevatedRectangleShader : public QSGMaterialShader { public: ElevatedRectangleShader(); char const *const *attributeNames() const override; void initialize() override; void updateState(const QSGMaterialShader::RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; private: int m_matrixLocation = -1; int m_opacityLocation = -1; int m_aspectLocation = -1; int m_sizeLocation = -1; int m_radiusLocation = -1; int m_colorLocation = -1; int m_shadowColorLocation = -1; int m_offsetLocation = -1; }; - -#endif // ELEVATEDRECTANGLEMATERIAL_H diff --git a/src/scenegraph/elevatedrectanglenode.cpp b/src/scenegraph/shadowedrectanglenode.cpp similarity index 85% rename from src/scenegraph/elevatedrectanglenode.cpp rename to src/scenegraph/shadowedrectanglenode.cpp index ed4b75f0..2d22ac1b 100644 --- a/src/scenegraph/elevatedrectanglenode.cpp +++ b/src/scenegraph/shadowedrectanglenode.cpp @@ -1,136 +1,133 @@ /* * Copyright 2020 Arjen Hiemstra * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see */ -#include "elevatedrectanglenode.h" - -#include "elevatedrectanglematerial.h" - -#include +#include "shadowedrectanglenode.h" +#include "shadowedrectanglematerial.h" QColor premultiply(const QColor &color) { return QColor::fromRgbF( color.redF() * color.alphaF(), color.greenF() * color.greenF(), color.blueF() * color.blueF(), color.alphaF() ); } -ElevatedRectangleNode::ElevatedRectangleNode(const QRectF& rect) +ShadowedRectangleNode::ShadowedRectangleNode(const QRectF& rect) { m_geometry = new QSGGeometry{QSGGeometry::defaultAttributes_TexturedPoint2D(), 4}; setGeometry(m_geometry); setRect(rect); - m_material = new ElevatedRectangleMaterial{}; + m_material = new ShadowedRectangleMaterial{}; setMaterial(m_material); setFlags(QSGNode::OwnsGeometry | QSGNode::OwnsMaterial); } -void ElevatedRectangleNode::setRect(const QRectF& rect) +void ShadowedRectangleNode::setRect(const QRectF& rect) { if (rect == m_rect) { return; } m_rect = rect; QVector2D newAspect{1.0, 1.0}; if (m_rect.width() >= m_rect.height()) { newAspect.setX(m_rect.width() / m_rect.height()); } else { newAspect.setY(m_rect.height() / m_rect.width()); } if (m_material->aspect != newAspect) { m_material->aspect = newAspect; markDirty(QSGNode::DirtyMaterial); m_aspect = newAspect; } } -void ElevatedRectangleNode::setSize(qreal size) +void ShadowedRectangleNode::setSize(qreal size) { auto minDimension = std::min(m_rect.width(), m_rect.height()); float uniformSize = (size / minDimension) * 2.0; if (!qFuzzyCompare(m_material->size, uniformSize)) { m_material->size = uniformSize; markDirty(QSGNode::DirtyMaterial); m_size = size; } } -void ElevatedRectangleNode::setRadius(qreal radius) +void ShadowedRectangleNode::setRadius(qreal radius) { auto minDimension = std::min(m_rect.width(), m_rect.height()); float uniformRadius = radius * 2.0 / minDimension; if (!qFuzzyCompare(m_material->radius, uniformRadius)) { m_material->radius = std::min(uniformRadius, 1.0f); markDirty(QSGNode::DirtyMaterial); m_radius = radius; } } -void ElevatedRectangleNode::setColor(const QColor &color) +void ShadowedRectangleNode::setColor(const QColor &color) { if (m_material->color != color) { m_material->color = premultiply(color); markDirty(QSGNode::DirtyMaterial); } } -void ElevatedRectangleNode::setShadowColor(const QColor& color) +void ShadowedRectangleNode::setShadowColor(const QColor& color) { if (m_material->shadowColor != color) { m_material->shadowColor = premultiply(color); markDirty(QSGNode::DirtyMaterial); } } -void ElevatedRectangleNode::setOffset(const QVector2D& offset) +void ShadowedRectangleNode::setOffset(const QVector2D& offset) { auto minDimension = std::min(m_rect.width(), m_rect.height()); auto uniformOffset = offset * 2.0 / minDimension; if (m_material->offset != uniformOffset) { m_material->offset = uniformOffset; markDirty(QSGNode::DirtyMaterial); m_offset = offset; } } -void ElevatedRectangleNode::updateGeometry() +void ShadowedRectangleNode::updateGeometry() { auto rect = m_rect.adjusted(-m_size * m_aspect.x(), -m_size * m_aspect.y(), m_size * m_aspect.x(), m_size * m_aspect.y()); auto offsetLength = m_offset.length(); rect = rect.adjusted(-offsetLength * m_aspect.x(), -offsetLength * m_aspect.y(), offsetLength * m_aspect.x(), offsetLength * m_aspect.y()); QSGGeometry::updateTexturedRectGeometry(m_geometry, rect, QRectF{0.0, 0.0, 1.0, 1.0}); markDirty(QSGNode::DirtyGeometry); } diff --git a/src/scenegraph/elevatedrectanglenode.h b/src/scenegraph/shadowedrectanglenode.h similarity index 83% rename from src/scenegraph/elevatedrectanglenode.h rename to src/scenegraph/shadowedrectanglenode.h index 1e880024..88ac680d 100644 --- a/src/scenegraph/elevatedrectanglenode.h +++ b/src/scenegraph/shadowedrectanglenode.h @@ -1,55 +1,52 @@ /* * Copyright 2020 Arjen Hiemstra * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see */ -#ifndef ELEVATEDRECTANGLENODE_H -#define ELEVATEDRECTANGLENODE_H +#pragma once #include #include #include -class ElevatedRectangleMaterial; +class ShadowedRectangleMaterial; -class ElevatedRectangleNode : public QSGGeometryNode +class ShadowedRectangleNode : public QSGGeometryNode { public: - ElevatedRectangleNode(const QRectF &rect = QRectF{}); + ShadowedRectangleNode(const QRectF &rect = QRectF{}); void setRect(const QRectF &rect); void setSize(qreal size); void setRadius(qreal radius); void setColor(const QColor &color); void setShadowColor(const QColor &color); void setOffset(const QVector2D &offset); void updateGeometry(); private: QSGGeometry *m_geometry; - ElevatedRectangleMaterial *m_material; + ShadowedRectangleMaterial *m_material; QRectF m_rect; qreal m_size = 0.0; qreal m_radius = 0.0; QVector2D m_offset; QVector2D m_aspect; }; - -#endif // ELEVATEDRECTANGLENODE_H diff --git a/src/elevatedrectangle.cpp b/src/shadowedrectangle.cpp similarity index 73% rename from src/elevatedrectangle.cpp rename to src/shadowedrectangle.cpp index 5da32145..de2baba2 100644 --- a/src/elevatedrectangle.cpp +++ b/src/shadowedrectangle.cpp @@ -1,160 +1,160 @@ /* * Copyright 2020 Arjen Hiemstra * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ -#include "elevatedrectangle.h" +#include "shadowedrectangle.h" -#include "scenegraph/elevatedrectanglenode.h" +#include "scenegraph/shadowedrectanglenode.h" -class ElevatedRectangle::Private +class ShadowedRectangle::Private { public: qreal size = 0.0; qreal radius = 0.0; qreal xOffset = 0.0; qreal yOffset = 0.0; QColor color = Qt::white; QColor shadowColor = Qt::black; }; -ElevatedRectangle::ElevatedRectangle(QQuickItem *parentItem) +ShadowedRectangle::ShadowedRectangle(QQuickItem *parentItem) : QQuickItem(parentItem), d(new Private) { setFlag(QQuickItem::ItemHasContents, true); } -ElevatedRectangle::~ElevatedRectangle() +ShadowedRectangle::~ShadowedRectangle() { } -qreal ElevatedRectangle::size() const +qreal ShadowedRectangle::size() const { return d->size; } -void ElevatedRectangle::setSize(qreal newSize) +void ShadowedRectangle::setSize(qreal newSize) { if (newSize == d->size) { return; } d->size = newSize; update(); Q_EMIT sizeChanged(); } -qreal ElevatedRectangle::radius() const +qreal ShadowedRectangle::radius() const { return d->radius; } -void ElevatedRectangle::setRadius(qreal newRadius) +void ShadowedRectangle::setRadius(qreal newRadius) { if (newRadius == d->radius) { return; } d->radius = newRadius; update(); Q_EMIT radiusChanged(); } -qreal ElevatedRectangle::xOffset() const +qreal ShadowedRectangle::xOffset() const { return d->xOffset; } -void ElevatedRectangle::setXOffset(qreal newXOffset) +void ShadowedRectangle::setXOffset(qreal newXOffset) { if (newXOffset == d->xOffset) { return; } d->xOffset = newXOffset; update(); Q_EMIT xOffsetChanged(); } -qreal ElevatedRectangle::yOffset() const +qreal ShadowedRectangle::yOffset() const { return d->yOffset; } -void ElevatedRectangle::setYOffset(qreal newYOffset) +void ShadowedRectangle::setYOffset(qreal newYOffset) { if (newYOffset == d->yOffset) { return; } d->yOffset = newYOffset; update(); Q_EMIT yOffsetChanged(); } -QColor ElevatedRectangle::color() const +QColor ShadowedRectangle::color() const { return d->color; } -void ElevatedRectangle::setColor(const QColor & newColor) +void ShadowedRectangle::setColor(const QColor & newColor) { if (newColor == d->color) { return; } d->color = newColor; update(); Q_EMIT colorChanged(); } -QColor ElevatedRectangle::shadowColor() const +QColor ShadowedRectangle::shadowColor() const { return d->shadowColor; } -void ElevatedRectangle::setShadowColor(const QColor &newShadowColor) +void ShadowedRectangle::setShadowColor(const QColor &newShadowColor) { if (newShadowColor == d->shadowColor) { return; } d->shadowColor = newShadowColor; update(); Q_EMIT shadowColorChanged(); } -QSGNode *ElevatedRectangle::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *data) +QSGNode *ShadowedRectangle::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *data) { Q_UNUSED(data); if (!node) { - node = new ElevatedRectangleNode; + node = new ShadowedRectangleNode; } - auto elevatedNode = static_cast(node); + auto elevatedNode = static_cast(node); elevatedNode->setRect(boundingRect()); elevatedNode->setSize(d->size); elevatedNode->setRadius(d->radius); elevatedNode->setOffset(QVector2D{float(d->xOffset), float(d->yOffset)}); elevatedNode->setColor(d->color); elevatedNode->setShadowColor(d->shadowColor); elevatedNode->updateGeometry(); return elevatedNode; } diff --git a/src/elevatedrectangle.h b/src/shadowedrectangle.h similarity index 91% rename from src/elevatedrectangle.h rename to src/shadowedrectangle.h index c437d3ef..49443041 100644 --- a/src/elevatedrectangle.h +++ b/src/shadowedrectangle.h @@ -1,73 +1,70 @@ /* * Copyright 2020 Arjen Hiemstra * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ -#ifndef ELEVATEDRECTANGLE_H -#define ELEVATEDRECTANGLE_H +#pragma once #include #include -class ElevatedRectangle : public QQuickItem +class ShadowedRectangle : public QQuickItem { Q_OBJECT Q_PROPERTY(qreal size READ size WRITE setSize NOTIFY sizeChanged) Q_PROPERTY(qreal radius READ radius WRITE setRadius NOTIFY radiusChanged) Q_PROPERTY(qreal xOffset READ xOffset WRITE setXOffset NOTIFY xOffsetChanged) Q_PROPERTY(qreal yOffset READ yOffset WRITE setYOffset NOTIFY yOffsetChanged) Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(QColor shadowColor READ shadowColor WRITE setShadowColor NOTIFY shadowColorChanged) public: - ElevatedRectangle(QQuickItem *parent = nullptr); - ~ElevatedRectangle() override; + ShadowedRectangle(QQuickItem *parent = nullptr); + ~ShadowedRectangle() override; qreal size() const; void setSize(qreal newSize); Q_SIGNAL void sizeChanged(); qreal radius() const; void setRadius(qreal newRadius); Q_SIGNAL void radiusChanged(); qreal xOffset() const; void setXOffset(qreal newXOffset); Q_SIGNAL void xOffsetChanged(); qreal yOffset() const; void setYOffset(qreal newYOffset); Q_SIGNAL void yOffsetChanged(); QColor color() const; void setColor(const QColor &newColor); Q_SIGNAL void colorChanged(); QColor shadowColor() const; void setShadowColor(const QColor &newShadowColor); Q_SIGNAL void shadowColorChanged(); protected: QSGNode *updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *data) override; private: class Private; const std::unique_ptr d; }; - -#endif // ELEVATEDRECTANGLE_H