PartDesign: Use Style Parameters for theming previews

This commit is contained in:
Kacper Donat
2025-08-10 23:13:58 +02:00
parent da03225f3b
commit 2cb3e87d01
4 changed files with 69 additions and 12 deletions

View File

@@ -217,6 +217,7 @@ SET(PartDesignGuiModule_SRCS
PreCompiled.h
SketchWorkflow.cpp
SketchWorkflow.h
StyleParameters.h
Utils.cpp
Utils.h
Workbench.cpp

View File

@@ -0,0 +1,43 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* *
* Copyright (c) 2025 Kacper Donat <kacper@kadet.net> *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD 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) any later version. *
* *
* FreeCAD 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 FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#ifndef STYLEPARAMETERS_H
#define STYLEPARAMETERS_H
#include <Gui/StyleParameters/ParameterManager.h>
namespace PartDesignGui::StyleParameters {
DEFINE_STYLE_PARAMETER(PreviewAdditiveColor, Base::Color(0.0F, 1.0F, 0.6F));
DEFINE_STYLE_PARAMETER(PreviewSubtractiveColor, Base::Color(1.0F, 0.0F, 0.0F));
DEFINE_STYLE_PARAMETER(PreviewDressUpColor, Base::Color(1.0F, 0.0F, 1.0F));
DEFINE_STYLE_PARAMETER(PreviewErrorColor, Base::Color(1.0F, 0.0F, 0.0F));
DEFINE_STYLE_PARAMETER(PreviewErrorTransparency, Gui::StyleParameters::Numeric(0.95));
DEFINE_STYLE_PARAMETER(PreviewToolTransparency, Gui::StyleParameters::Numeric(0.95));
DEFINE_STYLE_PARAMETER(PreviewShapeTransparency, Gui::StyleParameters::Numeric(0.8));
DEFINE_STYLE_PARAMETER(PreviewLineWidth, Gui::StyleParameters::Numeric(2));
}
#endif //STYLEPARAMETERS_H

View File

@@ -33,6 +33,7 @@
#endif
#include <Base/Exception.h>
#include <Base/ServiceProvider.h>
#include <App/Document.h>
#include <Gui/Application.h>
#include <Gui/BitmapFactory.h>
@@ -51,6 +52,7 @@
#include <Mod/Part/Gui/ViewProviderPreviewExtension.h>
#include "TaskFeatureParameters.h"
#include "StyleParameters.h"
#include "ViewProvider.h"
#include "ViewProviderPy.h"
@@ -78,13 +80,14 @@ void ViewProvider::attach(App::DocumentObject* pcObject)
{
ViewProviderPart::attach(pcObject);
if (auto addSubFeature = getObject<PartDesign::FeatureAddSub>()) {
const Base::Color green(0.0F, 1.0F, 0.6F);
const Base::Color red(1.0F, 0.0F, 0.0F);
auto* styleParameterManager = Base::provideService<Gui::StyleParameters::ParameterManager>();
if (auto addSubFeature = getObject<PartDesign::FeatureAddSub>()) {
bool isAdditive = addSubFeature->getAddSubType() == PartDesign::FeatureAddSub::Additive;
PreviewColor.setValue(isAdditive ? green : red);
PreviewColor.setValue(
isAdditive ? styleParameterManager->resolve(StyleParameters::PreviewAdditiveColor)
: styleParameterManager->resolve(StyleParameters::PreviewSubtractiveColor));
}
}
@@ -217,8 +220,12 @@ void ViewProvider::attachPreview()
{
ViewProviderPreviewExtension::attachPreview();
auto* styleParameterManager = Base::provideService<Gui::StyleParameters::ParameterManager>();
pcPreviewShape->lineWidth = styleParameterManager->resolve(StyleParameters::PreviewLineWidth).value;
pcToolPreview = new PartGui::SoPreviewShape;
pcToolPreview->transparency = 0.95F;
pcToolPreview->transparency = styleParameterManager->resolve(StyleParameters::PreviewToolTransparency).value;
pcToolPreview->color.connectFrom(&pcPreviewShape->color);
pcPreviewRoot->addChild(pcToolPreview);

View File

@@ -37,8 +37,11 @@
#include <Mod/PartDesign/App/FeatureDressUp.h>
#include "ViewProviderDressUp.h"
#include "StyleParameters.h"
#include "TaskDressUpParameters.h"
#include <Base/ServiceProvider.h>
#include <Gui/Utilities.h>
using namespace PartDesignGui;
@@ -50,8 +53,8 @@ void ViewProviderDressUp::attach(App::DocumentObject* pcObject)
{
ViewProvider::attach(pcObject);
const Base::Color magenta(1.0F, 0.0F, 1.0F);
PreviewColor.setValue(magenta);
auto* styleParameterManager = Base::provideService<Gui::StyleParameters::ParameterManager>();
PreviewColor.setValue(styleParameterManager->resolve(StyleParameters::PreviewDressUpColor));
setErrorState(false);
}
@@ -135,11 +138,14 @@ void ViewProviderDressUp::highlightReferences(const bool on)
void ViewProviderDressUp::setErrorState(bool error)
{
const Base::Color red(1.0, 0.0, 0.0);
auto* styleParameterManager = Base::provideService<Gui::StyleParameters::ParameterManager>();
constexpr float errorTransparency = 0.95F;
pcPreviewShape->transparency = error ? errorTransparency : PartGui::SoPreviewShape::defaultTransparency;
pcPreviewShape->color = Base::convertTo<SbColor>(error ? red : PreviewColor.getValue());
pcPreviewShape->transparency = styleParameterManager
->resolve(error ? StyleParameters::PreviewErrorTransparency
: StyleParameters::PreviewShapeTransparency)
.value;
pcPreviewShape->color = error
? styleParameterManager->resolve(StyleParameters::PreviewErrorColor).asValue<SbColor>()
: PreviewColor.getValue().asValue<SbColor>();
}