From 94982d331f723fdac1358d6697ce1e5268b1dbbc Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Sat, 13 Jan 2024 16:15:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=AE=E7=82=B9=E6=95=B01.?= =?UTF-8?q?0=E5=9C=A8SANE=E5=B1=9E=E6=80=A7=E4=B8=AD=E8=A2=AB=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E4=B8=BA=E6=95=B4=E6=95=B0=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20-=20=E9=A6=96=E6=AC=A1=E6=B7=BB=E5=8A=A0=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=E4=BD=9C=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/json/gb_json.cpp | 33 ++++++++++++++++ sdk/json/gb_json.h | 5 +++ sdk/sane_opt_json/device_opt.cpp | 66 ++++++++++++++++++++++++++++++-- sdk/sane_opt_json/device_opt.h | 3 +- 4 files changed, 103 insertions(+), 4 deletions(-) diff --git a/sdk/json/gb_json.cpp b/sdk/json/gb_json.cpp index 24a73f0..b91e6d0 100644 --- a/sdk/json/gb_json.cpp +++ b/sdk/json/gb_json.cpp @@ -960,3 +960,36 @@ bool gb_json::operator!=(const gb_json& r) { return !(*this == r); } + +bool gb_json::revise_number_type(bool dbval) +{ + bool chg = false; + + if (dbval) + { + // int -> double + if (type_ == VAL_TYPE_INT) + { + type_ = VAL_TYPE_FLOAT; + simple_val_.dval = simple_val_.nval; + chg = true; + } + else + chg = type_ == VAL_TYPE_FLOAT; + } + else + { + // double -> int + if (type_ == VAL_TYPE_FLOAT) + { + type_ = VAL_TYPE_INT; + simple_val_.nval = simple_val_.dval + .5f; + chg = true; + } + else + chg = type_ == VAL_TYPE_INT; + } + + return chg; +} + diff --git a/sdk/json/gb_json.h b/sdk/json/gb_json.h index 628c8ce..f9c90ec 100644 --- a/sdk/json/gb_json.h +++ b/sdk/json/gb_json.h @@ -115,6 +115,11 @@ public: bool operator==(const gb_json& r); bool operator!=(const gb_json& r); + + // [2024-01-13] double value maybe consider as integer if the number just an integer, this method is to set numbre to right type + // dbval - true: make integer to double and simple_val_.dval = simple_val_.nval; + // false: make double to integer and simple_val_.nval = simple_val_.dval + bool revise_number_type(bool dbval); }; #ifdef DUMP_JSON_OBJECT_LIFE diff --git a/sdk/sane_opt_json/device_opt.cpp b/sdk/sane_opt_json/device_opt.cpp index dc7da46..d32542e 100644 --- a/sdk/sane_opt_json/device_opt.cpp +++ b/sdk/sane_opt_json/device_opt.cpp @@ -1239,6 +1239,12 @@ bool device_option::arrange_raw_json(sane_opt_provider* sop) child->get_value("type", str); if (str != JSON_SANE_TYPE_GROUP) // omit group { + if (str == JSON_SANE_TYPE_FIXED) + { + // revise cur and default + device_option::revise_number_type(child, true); + } + if(no_grp_) { insert_option(child, sop); @@ -1329,7 +1335,7 @@ void device_option::init_depends(gb_json* opt) delete v; } } -gb_json* device_option::copy_opt(gb_json* from) +gb_json* device_option::copy_opt(gb_json* from, bool* changed_cur) { std::string text(from->to_string()); gb_json* to = new gb_json(); @@ -1385,6 +1391,9 @@ gb_json* device_option::copy_opt(gb_json* from) if (!from->get_value("readonly", rdo)) rdo = false; to->get_value("type", type); + if (changed_cur) + *changed_cur = apply_cur; + if (type == JSON_SANE_TYPE_BOOL) { to->set_value("default", *(bool*)val.c_str()); @@ -1421,6 +1430,8 @@ gb_json* device_option::copy_opt(gb_json* from) update_provider_value(to->key().c_str(), &val[0], rdo); } } + else if (changed_cur) + *changed_cur = false; } // 4: range value ... @@ -1560,6 +1571,7 @@ bool device_option::to_now(bool init, bool* changed) while (from) { std::string name(from->key()); + bool cur_chged = false; if (init) init_depends(from); @@ -1577,12 +1589,12 @@ bool device_option::to_now(bool init, bool* changed) } } - to = copy_opt(from); + to = copy_opt(from, &cur_chged); from->release(); if (to) { // copy cur value ... - if (now_) + if (now_ && !cur_chged) { gb_json* now = nullptr; now_->get_value(to->key().c_str(), now); @@ -1718,6 +1730,54 @@ std::string device_option::option_value(gb_json* jsn, bool def_val) return std::move(type); } +void device_option::revise_number_type(gb_json* opt, bool to_double) +{ + gb_json* child = nullptr; + + opt->get_value("cur", child); + if (child) + { + if (child->is_leaf_node()) + child->revise_number_type(to_double); + //else + // no object now, fixed me if be object + + child->release(); + } + + opt->get_value("default", child); + if (child) + { + if (child->is_leaf_node()) + child->revise_number_type(to_double); + else + { + gb_json* v = child->first_child(); + while (v) + { + if (v->is_leaf_node()) + v->revise_number_type(to_double); + v->release(); + v = child->next_child(); + } + } + child->release(); + } + + opt->get_value("range", child); + if (child) + { + gb_json* v = child->first_child(); + while (v) + { + if (v->is_leaf_node()) + v->revise_number_type(to_double); + v->release(); + v = child->next_child(); + } + child->release(); + } +} std::string device_option::trans_group(const char* utf8, bool to_title) { diff --git a/sdk/sane_opt_json/device_opt.h b/sdk/sane_opt_json/device_opt.h index 54a018a..d38397a 100644 --- a/sdk/sane_opt_json/device_opt.h +++ b/sdk/sane_opt_json/device_opt.h @@ -181,13 +181,14 @@ class device_option : public refer void insert_option(gb_json* opt, sane_opt_provider* from, const char* group = nullptr); bool arrange_raw_json(sane_opt_provider* sop); // create origin_ and re-arrange groups void init_depends(gb_json* opt); - gb_json* copy_opt(gb_json* from); + gb_json* copy_opt(gb_json* from, bool *changed_cur = nullptr); int visibility(gb_json* jsn); bool to_now(bool init, bool* changed); void update_provider_value(const char* name, void* value, bool skip_first = false/*readonly value should skip first*/); protected: static std::string option_value(gb_json* jsn, bool def_val); + static void revise_number_type(gb_json* opt, bool to_double); template static condition_value* to_condition_value(gb_json* jsn, const char* key, const char* type, device_option* parent)