mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-09-28 21:34:12 +02:00
8334a9a27f
A script reaches the whole project and, through the export calls, the
filesystem. That is a capability most people installing an electrical CAD
program never asked for, and leaving it on by default hands it to them
anyway. So QET_HAS_SCRIPTING builds now ship with it switched off.
QetSettings::scriptingEnabled() is the single answer, read by all three
places that need it, with QET_ENABLE_SCRIPTING=1 overriding the stored
value. The override is not decoration: a CI job or a batch run has no
dialog to tick, and a machine whose HOME is created fresh for each run has
nowhere to keep the setting either. It beats a stored "false" on purpose,
so a box unticked once cannot lock a build server out of --run for good.
Only the exact value "1" counts.
--run refuses with exit 3 and a message naming both ways in.
Projet > Exécuter un script... asks once, and turns the setting on if
the answer is yes. Asking beats grey: a disabled menu
entry says something exists and nothing about how to have
it, and this is the pattern people already know from
macro security in office software.
Configurer QElectroTech > Général > Projets has the checkbox, for
turning it back off. While the environment forces
scripting on, the box is disabled and says why, and
applyConf() then leaves the stored value alone rather
than quietly overwriting it.
runOnProject() checks as well, after both callers have. It is the one
function that actually evaluates JavaScript, so it is the one place a
future caller cannot forget to ask; the callers check first only to give a
better answer than it can.
Verified on the built binary, all four states, with an isolated HOME:
stored env result
absent - refused, exit 3
true - script runs, exit 0
false - refused, exit 3
false 1 script runs, exit 0
tst_scriptingsetting covers the same matrix hermetically, in its own
QSettings scope, and was mutation-checked: flipping the default to true
turns defaultsToOff() red.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
156 lines
4.9 KiB
C++
156 lines
4.9 KiB
C++
/*
|
|
Copyright 2006-2026 The QElectroTech Team
|
|
This file is part of QElectroTech.
|
|
|
|
QElectroTech 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 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
QElectroTech 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 QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "qetsettings.h"
|
|
#include <QSettings>
|
|
#include <QVariant>
|
|
#include <QByteArray>
|
|
|
|
namespace QetSettings
|
|
{
|
|
/**
|
|
* @brief setHdpiScaleFactorRoundingPolicy
|
|
* Write the value of HdpiScaleFactorRoundingPolicy in
|
|
* QElectroTech settings
|
|
* the value is in form of a string.
|
|
* @a policy can be : Round, Ceil, Floor, RoundPreferFloor, PassThrough
|
|
* In case of wrong policy, PassThrough is use as default value.
|
|
* The value is stored with key : hdpi_scale_factor_rounding_policy
|
|
* @sa Qt::HighDpiScaleFactorRoundingPolicy
|
|
* @param policy
|
|
*/
|
|
void setHdpiScaleFactorRoundingPolicy(const QString &policy_str)
|
|
{
|
|
auto policy = QString("PassThrough");
|
|
if (policy_str == QLatin1String("Round")) {
|
|
policy = "Round";
|
|
} else if (policy_str == QLatin1String("Ceil")) {
|
|
policy = "Ceil";
|
|
} else if (policy_str == QLatin1String("Floor")) {
|
|
policy = "Floor";
|
|
} else if (policy_str == QLatin1String("RoundPreferFloor")) {
|
|
policy = "RoundPreferFloor";
|
|
}
|
|
QSettings settings;
|
|
settings.setValue("hdpi_scale_factor_rounding_policy", policy);
|
|
}
|
|
|
|
/**
|
|
* @brief setHdpiScaleFactorRoundingPolicy
|
|
* Overload function
|
|
* @sa void setHdpiScaleFactorRoundingPolicy(const QString &policy_str)
|
|
* @param policy
|
|
*/
|
|
void setHdpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy policy)
|
|
{
|
|
QString policy_str;
|
|
switch (policy) {
|
|
case Qt::HighDpiScaleFactorRoundingPolicy::Round:
|
|
policy_str = "Round";
|
|
break;
|
|
case Qt::HighDpiScaleFactorRoundingPolicy::Ceil:
|
|
policy_str = "Ceil";
|
|
break;
|
|
case Qt::HighDpiScaleFactorRoundingPolicy::Floor:
|
|
policy_str = "Floor";
|
|
break;
|
|
case Qt::HighDpiScaleFactorRoundingPolicy::RoundPreferFloor:
|
|
policy_str = "RoundPreferFloor";
|
|
break;
|
|
default:
|
|
policy_str = "PassThrough";
|
|
break;
|
|
}
|
|
|
|
QSettings settings;
|
|
settings.setValue("hdpi_scale_factor_rounding_policy", policy_str);
|
|
}
|
|
|
|
/**
|
|
* @brief hdpiScaleFactorRoundingPolicy
|
|
* @param default_policy
|
|
* @return the hdpiScaleFactorRoundingPolicy value stored in current settings.
|
|
* @sa setHdpiScaleFactorRoundingPolicy
|
|
*/
|
|
Qt::HighDpiScaleFactorRoundingPolicy hdpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy default_policy)
|
|
{
|
|
QSettings settings;
|
|
auto value_ = settings.value("hdpi_scale_factor_rounding_policy");
|
|
|
|
if (value_ == QLatin1String("Round")) {
|
|
return Qt::HighDpiScaleFactorRoundingPolicy::Round;
|
|
} else if (value_ == QLatin1String("Ceil")) {
|
|
return Qt::HighDpiScaleFactorRoundingPolicy::Ceil;
|
|
} else if (value_ == QLatin1String("Floor")) {
|
|
return Qt::HighDpiScaleFactorRoundingPolicy::Floor;
|
|
} else if (value_ == QLatin1String("RoundPreferFloor")) {
|
|
return Qt::HighDpiScaleFactorRoundingPolicy::RoundPreferFloor;
|
|
} else if (value_ == QLatin1String("PassThrough")) {
|
|
return Qt::HighDpiScaleFactorRoundingPolicy::PassThrough;
|
|
} else {
|
|
return default_policy;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief scriptingForcedByEnvironment
|
|
* @return true if QET_ENABLE_SCRIPTING is set to 1 in the environment.
|
|
*
|
|
* The way to turn scripting on where there is nobody to tick a box:
|
|
* a headless run, a CI job, a build server. Those have no settings
|
|
* file worth writing to -- and on a machine whose HOME is created
|
|
* fresh for the run, writing one would not survive anyway.
|
|
*/
|
|
bool scriptingForcedByEnvironment()
|
|
{
|
|
return qgetenv("QET_ENABLE_SCRIPTING") == QByteArray("1");
|
|
}
|
|
|
|
/**
|
|
* @brief scriptingEnabled
|
|
* @return whether QElectroTech may run a JavaScript script.
|
|
*
|
|
* Off unless the user turned it on. A script reaches the whole project
|
|
* and the filesystem through the export calls, so it is capability the
|
|
* great majority of users never asked for; leaving it on by default
|
|
* would hand it to them anyway. @sa setScriptingEnabled
|
|
*
|
|
* The environment override wins over the stored value, and is checked
|
|
* first so that a machine with no settings at all still answers.
|
|
*/
|
|
bool scriptingEnabled()
|
|
{
|
|
if (scriptingForcedByEnvironment()) {
|
|
return true;
|
|
}
|
|
QSettings settings;
|
|
return settings.value("scripting/enabled", false).toBool();
|
|
}
|
|
|
|
/**
|
|
* @brief setScriptingEnabled
|
|
* Store whether scripting is allowed. @sa scriptingEnabled
|
|
* @param enabled
|
|
*/
|
|
void setScriptingEnabled(bool enabled)
|
|
{
|
|
QSettings settings;
|
|
settings.setValue("scripting/enabled", enabled);
|
|
}
|
|
}
|