add is_default script function

This commit is contained in:
GenevensiS
2026-04-19 19:06:52 +02:00
parent 17dd3cf407
commit d782e4851c
22 changed files with 93 additions and 13 deletions
+1
View File
@@ -78,6 +78,7 @@ These functions are built into the program, other [[type:function]]s can be defi
| [[fun:exclusive_choice]] Require that at most one of the given choices is selected.
| [[fun:require_exclusive_choice]] Require that exactly one of the given choices is selected.
| [[fun:remove_choice]] Remove the given choices from a multiple choice value.
| [[fun:is_default]] Check if a field is in its default state.
! Images <<<
| [[fun:linear_blend]] Blend two images together using a linear gradient.
+10
View File
@@ -0,0 +1,10 @@
Function: is_default
--Usage--
> is_default(some_field)
Returns true if the field is in its default state, that is, if the user hasn't modified the value yet, false otherwise.
--Parameters--
! Parameter Type Description
| @input@ [[type:field]] Field to test.
+4
View File
@@ -305,6 +305,10 @@ bool Value::equals(const Value* that) {
return this == that;
}
bool Value::isDefault() {
return false;
}
bool Value::update(Context& ctx) {
updateAge();
updateSortValue(ctx);
+2
View File
@@ -256,6 +256,8 @@ public:
/// Convert this value to a string for use in tables
virtual String toString() const = 0;
/// Check if this value is in the default state
virtual bool isDefault();
/// Apply scripts to this value, return true if the value has changed
virtual bool update(Context& ctx);
/// This value has been updated by an action
+5
View File
@@ -318,6 +318,11 @@ ChoiceValue::ChoiceValue(const ChoiceFieldP& field, bool initial_first_choice)
String ChoiceValue::toString() const {
return value();
}
bool ChoiceValue::isDefault() {
return value.isDefault();
}
bool ChoiceValue::update(Context& ctx) {
bool change = field().default_script.invokeOnDefault(ctx, value)
| field(). script.invokeOn(ctx, value);
+2
View File
@@ -200,6 +200,8 @@ public:
ValueType value; /// The name of the selected choice
bool isDefault() override;
bool update(Context&) override;
};
+5
View File
@@ -111,6 +111,11 @@ String ColorValue::toString() const {
}
return _("<color>");
}
bool ColorValue::isDefault() {
return value.isDefault();
}
bool ColorValue::update(Context& ctx) {
bool change = field().default_script.invokeOnDefault(ctx, value)
| field(). script.invokeOn(ctx, value);
+2
View File
@@ -78,6 +78,8 @@ public:
ValueType value; ///< The value
bool isDefault() override;
bool update(Context&) override;
};
+4
View File
@@ -39,6 +39,10 @@ String ImageValue::toString() const {
return filename.empty() ? _("") : _("<image>");
}
bool ImageValue::isDefault() {
return filename.empty();
}
// custom reflection: convert to ScriptImageP for scripting
void ImageValue::reflect(Reader& handler) {
+2
View File
@@ -48,6 +48,8 @@ public:
ValueType filename; ///< Filename of the image (in the current package), or ""
Age last_update; ///< When was the image last changed?
bool isDefault() override;
};
// ----------------------------------------------------------------------------- : ImageStyle
+5
View File
@@ -63,6 +63,11 @@ IMPLEMENT_REFLECTION(InfoStyle) {
String InfoValue::toString() const {
return value;
}
bool InfoValue::isDefault() {
return true;
}
bool InfoValue::update(Context& ctx) {
if (value.empty()) value = field().caption.get();
bool change = field().script.invokeOn(ctx, value);
+2
View File
@@ -61,6 +61,8 @@ public:
ValueType value;
bool isDefault() override;
bool update(Context&) override;
};
+4
View File
@@ -52,6 +52,10 @@ IMPLEMENT_REFLECTION_NAMELESS(MultipleChoiceValue) {
REFLECT_BASE(ChoiceValue);
}
bool MultipleChoiceValue::isDefault() {
return value.isDefault();
}
bool MultipleChoiceValue::update(Context& ctx) {
String old_value = value();
ctx.setVariable(_("last_change"), to_script(last_change));
+2
View File
@@ -64,6 +64,8 @@ public:
/// Splits the value, stores the selected choices in the out parameter
void get(vector<String>& out) const;
bool isDefault() override;
bool update(Context&) override;
private:
+6
View File
@@ -62,6 +62,12 @@ PackagedP PackageChoiceValue::getPackage() const {
else return package_manager.openAny(package_name, true);
}
bool PackageChoiceValue::isDefault() {
PackageChoiceFieldP packageFieldP = boost::dynamic_pointer_cast<PackageChoiceField>(fieldP);
if (packageFieldP) return package_name == packageFieldP->initial;
return false;
}
bool PackageChoiceValue::update(Context& ctx) {
bool change = field().script.invokeOn(ctx, package_name);
Value::update(ctx);
+2
View File
@@ -62,6 +62,8 @@ public:
/// Get the package (if it is set), otherwise return nullptr
PackagedP getPackage() const;
bool isDefault() override;
bool update(Context&) override;
};
+4
View File
@@ -51,6 +51,10 @@ String SymbolValue::toString() const {
return filename.empty() ? _("") : _("<symbol>");
}
bool SymbolValue::isDefault() {
return filename.empty();
}
IMPLEMENT_REFLECTION_NO_GET_MEMBER(SymbolValue) {
if (fieldP->save_value || !handler.isWriting) REFLECT_NAMELESS(filename);
}
+2
View File
@@ -70,5 +70,7 @@ public:
ValueType filename; ///< Filename of the symbol (in the current package)
Age last_update; ///< When was the symbol last changed?
bool isDefault() override;
};
+5
View File
@@ -177,6 +177,11 @@ IMPLEMENT_REFLECTION(TextStyle) {
String TextValue::toString() const {
return untag_hide_sep(value());
}
bool TextValue::isDefault() {
return value.isDefault();
}
bool TextValue::update(Context& ctx) {
updateAge();
WITH_DYNAMIC_ARG(last_update_age, last_update.get());
+2
View File
@@ -113,6 +113,8 @@ public:
ValueType value; ///< The text of this value
Age last_update; ///< When was the text last changed?
bool isDefault() override;
bool update(Context&) override;
};
+3 -2
View File
@@ -117,13 +117,14 @@ Bitmap ImageValueViewer::imagePlaceholder(const Rotation& rot, UInt w, UInt h, c
// Draw text
if (editing) {
// only when in editor mode
String label = _LABEL_("load image");
for (UInt size = 12 ; size > 2 ; --size) {
dc.SetFont(wxFont(size, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL));
RealSize rs = dc.GetTextExtent(_LABEL_("load image"));
RealSize rs = dc.GetTextExtent(label);
if (rs.width <= w - 10 && rs.height < h - 10) {
// text fits
RealPoint pos = align_in_rect(ALIGN_MIDDLE_CENTER, rs, rect);
dc.DrawText(_LABEL_("load image"), pos, Color(255,255,255), 2, Color(0,0,0), 3); // stroked
dc.DrawText(label, pos, Color(255,255,255), 2, Color(0,0,0), 3); // stroked
break;
}
}
+8
View File
@@ -189,6 +189,13 @@ SCRIPT_FUNCTION_DEPENDENCIES(combined_editor) {
return dependency_dummy;
}
// ----------------------------------------------------------------------------- : Values
SCRIPT_FUNCTION(is_default) {
SCRIPT_PARAM_C(ValueP,input);
SCRIPT_RETURN(input->isDefault());
}
// ----------------------------------------------------------------------------- : Choice values
// convert a full choice name into the name of the top level group it is in
@@ -389,6 +396,7 @@ SCRIPT_FUNCTION(count_chosen) {
void init_script_editor_functions(Context& ctx) {
ctx.setVariable(_("forward_editor"), script_combined_editor); // compatability
ctx.setVariable(_("combined_editor"), script_combined_editor);
ctx.setVariable(_("is_default"), script_is_default);
ctx.setVariable(_("primary_choice"), script_primary_choice);
ctx.setVariable(_("chosen"), script_chosen);
ctx.setVariable(_("count_chosen"), script_count_chosen);