Merge pull request #25674 from WandererFan/DimensionColorSizeOldDocuments2

TechDraw: fix handling of alpha channel in old documents
This commit is contained in:
Chris Hennes
2025-11-28 18:04:30 -06:00
committed by GitHub
7 changed files with 121 additions and 12 deletions

View File

@@ -719,3 +719,10 @@ bool Preferences::printCenterMarks()
{
return getPreferenceGroup("Decorations")->GetBool("PrintCenterMarks", false);
}
//! true if old style transparency values should be converted to new style alpha values for color properties.
bool Preferences::fixColorAlphaOnLoad()
{
return getPreferenceGroup("General")->GetBool("FixColorAlphaOnLoad", true);
}

View File

@@ -169,6 +169,8 @@ public:
static bool showCenterMarks();
static bool printCenterMarks();
static bool fixColorAlphaOnLoad();
};

View File

@@ -283,7 +283,7 @@ void QGIViewDimension::updateDim()
QFont font = datumLabel->getFont();
font.setFamily(QString::fromUtf8(vp->Font.getValue()));
int fontSize = QGIView::exactFontSize(vp->Font.getValue(), vp->Fontsize.getValue());
int fontSize = QGIView::exactFontSize(vp->Font.getValue(), std::max(1.0, vp->Fontsize.getValue()));
font.setPixelSize(fontSize);
datumLabel->setFont(font);

View File

@@ -349,3 +349,41 @@ std::vector<App::DocumentObject*> ViewProviderDimension::claimChildren() const
}
void ViewProviderDimension::finishRestoring()
{
fixTextSize();
fixArrowSize();
ViewProviderDrawingView::finishRestoring();
}
void ViewProviderDimension::fixTextSize()
{
App::Document* ourDoc = getDocument()->getDocument();
if (checkMiniumumDocumentVersion(ourDoc, 1, 1)) {
return;
}
double size = Fontsize.getValue();
if (size == 0.0) {
size = Preferences::dimFontSizeMM();
Fontsize.setValue(size);
}
}
void ViewProviderDimension::fixArrowSize()
{
App::Document* ourDoc = getDocument()->getDocument();
if (checkMiniumumDocumentVersion(ourDoc, 1, 1)) {
return;
}
double size = Arrowsize.getValue();
if (size == 0.0) {
size = Preferences::dimFontSizeMM();
Arrowsize.setValue(size);
}
}

View File

@@ -78,6 +78,7 @@ public:
bool setEdit(int ModNum) override;
bool doubleClicked() override;
bool onDelete(const std::vector<std::string> & parms) override;
void finishRestoring() override;
TechDraw::DrawViewDimension* getViewObject() const override;
@@ -93,6 +94,8 @@ public:
std::vector<App::DocumentObject*> claimChildren() const override;
void fixTextSize();
void fixArrowSize();
protected:
void handleChangedPropertyType(Base::XMLReader &reader, const char *TypeName, App::Property * prop) override;

View File

@@ -25,7 +25,11 @@
#include <boost/signals2.hpp>
#include <boost/signals2/connection.hpp>
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <App/Metadata.h>
#include <Base/Color.h>
#include <Base/Console.h>
#include <Gui/Application.h>
#include <Gui/Control.h>
@@ -75,7 +79,6 @@ ViewProviderDrawingView::~ViewProviderDrawingView()
void ViewProviderDrawingView::attach(App::DocumentObject *pcFeat)
{
// Base::Console().message("VPDV::attach(%s)\n", pcFeat->getNameInDocument());
ViewProviderDocumentObject::attach(pcFeat);
//NOLINTBEGIN
@@ -209,11 +212,8 @@ void ViewProviderDrawingView::startRestoring()
void ViewProviderDrawingView::finishRestoring()
{
if (Visibility.getValue()) {
show();
} else {
hide();
}
fixColorAlphaValues();
Gui::ViewProviderDocumentObject::finishRestoring();
}
@@ -290,10 +290,10 @@ Gui::MDIView *ViewProviderDrawingView::getMDIView() const
void ViewProviderDrawingView::onGuiRepaint(const TechDraw::DrawView* dv)
{
// Base::Console().message("VPDV::onGuiRepaint(%s) - this: %x\n", dv->getNameInDocument(), this);
Gui::Document* guiDoc = Gui::Application::Instance->getDocument(getViewObject()->getDocument());
if (!guiDoc)
if (!guiDoc) {
return;
}
std::vector<TechDraw::DrawPage*> pages = getViewObject()->findAllParentPages();
if (pages.size() > 1) {
@@ -351,9 +351,7 @@ void ViewProviderDrawingView::onProgressMessage(const TechDraw::DrawView* dv,
const std::string featureName,
const std::string text)
{
// Q_UNUSED(featureName)
Q_UNUSED(dv)
// Q_UNUSED(text)
showProgressMessage(featureName, text);
}
@@ -481,7 +479,6 @@ TechDraw::DrawView* ViewProviderDrawingView::getViewObject() const
//! handled by the undo mechanism, but the QGraphicsScene parentage is not reset.
void ViewProviderDrawingView::fixSceneDependencies()
{
Base::Console().message("VPDV::fixSceneDependencies()\n");
auto page = getViewProviderPage();
if (!page) {
return;
@@ -526,3 +523,58 @@ std::vector<App::DocumentObject*> ViewProviderDrawingView::claimChildren() const
return temp;
}
//! convert old style transparency values in PropertyColor to new style alpha channel values
void ViewProviderDrawingView::fixColorAlphaValues()
{
if (!Preferences::fixColorAlphaOnLoad() ||
checkMiniumumDocumentVersion(1, 1)) {
return;
}
// check every PropertyColor for transparency vs alpha
std::vector<App::Property*> allProperties;
getPropertyList(allProperties);
constexpr double alphaNone{0.0};
constexpr double alphaFull{1.0};
for (auto& prop : allProperties) {
auto* colorProp = freecad_cast<App::PropertyColor*>(prop);
if (colorProp) {
// Here we are assuming that transparent colors are not used/need not be converted.
// To invert more generally, colorOut.a = 1 - colorIn.a;, but we would need a different
// mechanism to determine when to do the conversion.
Base::Color colorTemp = colorProp->getValue();
if (colorTemp.a == alphaNone) {
colorTemp.a = alphaFull;
colorProp->setValue(colorTemp);
}
}
}
}
//! true if document toBeChecked was written by a program with version >= minMajor.minMinor.
//! note that we can not check point releases as only the major and minor are recorded in the Document.xml
//! file.
//! (ex <Document SchemaVersion="4" ProgramVersion="1.2R44322 +1 (Git)" FileVersion="1" StringHasher="1">)
bool ViewProviderDrawingView::checkMiniumumDocumentVersion(App::Document* toBeChecked,
int minMajor,
int minMinor)
{
const char* docVersionText = toBeChecked->getProgramVersion();
int docMajor{0};
int docMinor{0};
// stole this bit from App::AttachExtension.
// NOLINTNEXTLINE
if (sscanf(docVersionText, "%d.%d", &docMajor, &docMinor) != 2) {
Base::Console().warning("Failed to retrieve document version number for %s\n",
toBeChecked ? toBeChecked->getName() : "noname");
return false; // ?? should we fail here? the file appears broken.
}
return std::tie(docMajor, docMinor) >= std::tie(minMajor, minMinor);
}

View File

@@ -28,11 +28,13 @@
#include <boost/signals2.hpp>
#include <Gui/Document.h>
#include <Gui/ViewProviderDocumentObject.h>
#include <Mod/TechDraw/App/DrawView.h>
#include "ViewProviderDrawingViewExtension.h"
namespace TechDraw {
class DrawView;
}
@@ -102,6 +104,11 @@ public:
virtual void fixSceneDependencies();
std::vector<App::DocumentObject*> claimChildren() const override;
void fixColorAlphaValues();
bool checkMiniumumDocumentVersion(int minMajor, int minMinor) const
{ return checkMiniumumDocumentVersion(this->getDocument()->getDocument(), minMajor, minMinor); }
static bool checkMiniumumDocumentVersion(App::Document* toBeChecked, int minMajor, int minMinor);
private:
void multiParentPaint(std::vector<TechDraw::DrawPage*>& pages);