From 161f7168d2c8a458a3431639bf7262e58bfc32ee Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Mon, 31 Oct 2022 15:38:37 +0800 Subject: [PATCH] The decdoer info surports mutil-language --- DSView/pv/data/decoderstack.cpp | 9 ++++ DSView/pv/data/decoderstack.h | 2 + DSView/pv/dialogs/decoderoptionsdlg.cpp | 46 +++++++++++++++---- DSView/pv/dialogs/decoderoptionsdlg.h | 4 +- DSView/pv/mainwindow.cpp | 6 ++- DSView/pv/prop/binding/decoderoptions.cpp | 11 ++++- DSView/pv/ui/langresource.cpp | 4 +- DSView/pv/ui/langresource.h | 2 +- DSView/pv/view/decodetrace.cpp | 2 +- libsigrokdecode4DSL/decoder.c | 30 ++++++++++-- .../libsigrokdecode-internal.h | 2 + libsigrokdecode4DSL/libsigrokdecode.h | 3 ++ 12 files changed, 100 insertions(+), 21 deletions(-) diff --git a/DSView/pv/data/decoderstack.cpp b/DSView/pv/data/decoderstack.cpp index 2807b7e7..76a90d7d 100644 --- a/DSView/pv/data/decoderstack.cpp +++ b/DSView/pv/data/decoderstack.cpp @@ -773,5 +773,14 @@ int64_t DecoderStack::get_mark_index() return _mark_index; } +const char* DecoderStack::get_root_decoder_id() +{ + if (_stack.size() > 0){ + decode::Decoder *dec = _stack.front(); + return dec->decoder()->id; + } + return NULL; +} + } // namespace data } // namespace pv diff --git a/DSView/pv/data/decoderstack.h b/DSView/pv/data/decoderstack.h index 8276c59c..3ce5c6d9 100644 --- a/DSView/pv/data/decoderstack.h +++ b/DSView/pv/data/decoderstack.h @@ -96,6 +96,8 @@ public: return _stack; } + const char* get_root_decoder_id(); + void add_sub_decoder(decode::Decoder *decoder); void remove_sub_decoder(decode::Decoder *decoder); void remove_decoder_by_handel(const srd_decoder *dec); diff --git a/DSView/pv/dialogs/decoderoptionsdlg.cpp b/DSView/pv/dialogs/decoderoptionsdlg.cpp index 4fed2d54..a44be16e 100644 --- a/DSView/pv/dialogs/decoderoptionsdlg.cpp +++ b/DSView/pv/dialogs/decoderoptionsdlg.cpp @@ -68,11 +68,23 @@ DecoderOptionsDlg::~DecoderOptionsDlg() _bindings.clear(); } -void DecoderOptionsDlg::load_options(view::DecodeTrace *trace, bool isNew) -{ +void DecoderOptionsDlg::load_options(view::DecodeTrace *trace) +{ assert(trace); - _trace = trace; + + const char *dec_id = trace->decoder()->get_root_decoder_id(); + + if (LangResource::Instance()->is_new_decoder(dec_id)) + LangResource::Instance()->reload_dynamic(); + + load_options_view(); + + LangResource::Instance()->release_dynamic(); +} + +void DecoderOptionsDlg::load_options_view() +{ DSDialog *dlg = this; QFormLayout *form = new QFormLayout(); @@ -349,31 +361,45 @@ void DecoderOptionsDlg::create_decoder_form( // Add the mandatory channels for(l = decoder->channels; l; l = l->next) { - const struct srd_channel *const pdch = - (struct srd_channel *)l->data; + const struct srd_channel *const pdch = (struct srd_channel *)l->data; DsComboBox *const combo = create_probe_selector(parent, dec, pdch); + const char *desc_str = NULL; + if (pdch->idn != NULL){ + desc_str = LangResource::Instance()->get_lang_text(STR_PAGE_DECODER, pdch->idn, pdch->desc); + } + else{ + desc_str = pdch->desc; + } + //tr decoder_form->addRow(QString("%1 (%2) *") .arg(QString::fromUtf8(pdch->name)) - .arg(QString::fromUtf8(pdch->desc)), combo); + .arg(QString::fromUtf8(desc_str)), combo); const ProbeSelector s = {combo, dec, pdch}; _probe_selectors.push_back(s); - connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_probe_selected(int))); + connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_probe_selected(int))); } // Add the optional channels for(l = decoder->opt_channels; l; l = l->next) { - const struct srd_channel *const pdch = - (struct srd_channel *)l->data; + const struct srd_channel *const pdch = (struct srd_channel *)l->data; DsComboBox *const combo = create_probe_selector(parent, dec, pdch); + const char *desc_str = NULL; + if (pdch->idn != NULL){ + desc_str = LangResource::Instance()->get_lang_text(STR_PAGE_DECODER, pdch->idn, pdch->desc); + } + else{ + desc_str = pdch->desc; + } + //tr decoder_form->addRow(QString("%1 (%2)") .arg(QString::fromUtf8(pdch->name)) - .arg(QString::fromUtf8(pdch->desc)), combo); + .arg(QString::fromUtf8(desc_str)), combo); const ProbeSelector s = {combo, dec, pdch}; _probe_selectors.push_back(s); diff --git a/DSView/pv/dialogs/decoderoptionsdlg.h b/DSView/pv/dialogs/decoderoptionsdlg.h index be4386ce..ff0a77a4 100644 --- a/DSView/pv/dialogs/decoderoptionsdlg.h +++ b/DSView/pv/dialogs/decoderoptionsdlg.h @@ -84,9 +84,11 @@ public: cursor2 = _cursor2; } - void load_options(view::DecodeTrace *trace, bool isNew); + void load_options(view::DecodeTrace *trace); private: + void load_options_view(); + void load_decoder_forms(QWidget *container); DsComboBox* create_probe_selector(QWidget *parent, const data::decode::Decoder *dec, diff --git a/DSView/pv/mainwindow.cpp b/DSView/pv/mainwindow.cpp index 383df7c1..11933c01 100644 --- a/DSView/pv/mainwindow.cpp +++ b/DSView/pv/mainwindow.cpp @@ -1501,7 +1501,11 @@ namespace pv { QString ses_name = dir.absolutePath() + "/" + _device_agent->driver_name() + QString::number(mode) + ".dsc"; - on_load_session(ses_name); + + QFile sf(ses_name); + if (sf.exists()){ + on_load_session(ses_name); + } } } } diff --git a/DSView/pv/prop/binding/decoderoptions.cpp b/DSView/pv/prop/binding/decoderoptions.cpp index 6b5d2446..ccbff9ee 100644 --- a/DSView/pv/prop/binding/decoderoptions.cpp +++ b/DSView/pv/prop/binding/decoderoptions.cpp @@ -31,6 +31,7 @@ #include "../enum.h" #include "../int.h" #include "../string.h" +#include "../../ui/langresource.h" using namespace boost; using namespace std; @@ -54,7 +55,15 @@ DecoderOptions::DecoderOptions(pv::data::DecoderStack* decoder_stack, data::deco const srd_decoder_option *const opt = (srd_decoder_option*)l->data; - const QString name = QString::fromUtf8(opt->desc); + const char *desc_str = NULL; + if (opt->idn != NULL){ + desc_str = LangResource::Instance()->get_lang_text(STR_PAGE_DECODER, opt->idn, opt->desc); + } + else{ + desc_str = opt->desc; + } + + const QString name = QString::fromUtf8(desc_str); const Property::Getter getter = bind( &DecoderOptions::getter, this, opt->id); diff --git a/DSView/pv/ui/langresource.cpp b/DSView/pv/ui/langresource.cpp index 2dea930b..274549ae 100644 --- a/DSView/pv/ui/langresource.cpp +++ b/DSView/pv/ui/langresource.cpp @@ -161,6 +161,8 @@ void LangResource::load_page(Lang_resource_page &p, QString file) if (raw_bytes.length() == 0) return; + dsv_info("Load lang resouce file: %s", file.toLocal8Bit().data()); + QJsonParseError error; QString jsonStr(raw_bytes.data()); QByteArray qbs = jsonStr.toUtf8(); @@ -264,7 +266,7 @@ void LangResource::reload_dynamic() } } -void LangResource::relase_dynamic() +void LangResource::release_dynamic() { for (Lang_resource_page *p : _pages) { diff --git a/DSView/pv/ui/langresource.h b/DSView/pv/ui/langresource.h index f9e9e989..dde228a5 100644 --- a/DSView/pv/ui/langresource.h +++ b/DSView/pv/ui/langresource.h @@ -85,7 +85,7 @@ public: const char* get_lang_text(int page_id, const char *str_id, const char *default_str); bool is_new_decoder(const char *decoder_id); void reload_dynamic(); - void relase_dynamic(); + void release_dynamic(); private: const char *get_lang_key(int lang); diff --git a/DSView/pv/view/decodetrace.cpp b/DSView/pv/view/decodetrace.cpp index db78c1c0..fd951928 100644 --- a/DSView/pv/view/decodetrace.cpp +++ b/DSView/pv/view/decodetrace.cpp @@ -681,7 +681,7 @@ bool DecodeTrace::create_popup(bool isnew) QWidget *top = AppControl::Instance()->GetTopWindow(); dialogs::DecoderOptionsDlg dlg(top); dlg.set_cursor_range(_decode_cursor1, _decode_cursor2); - dlg.load_options(this, isnew); + dlg.load_options(this); if (QDialog::Accepted == dlg.exec()) { diff --git a/libsigrokdecode4DSL/decoder.c b/libsigrokdecode4DSL/decoder.c index 7d15723c..e8d58ab5 100644 --- a/libsigrokdecode4DSL/decoder.c +++ b/libsigrokdecode4DSL/decoder.c @@ -112,9 +112,10 @@ static void channel_free(void *data) if (!ch) return; - g_free(ch->desc); - g_free(ch->name); - g_free(ch->id); + g_safe_free(ch->desc); + g_safe_free(ch->name); + g_safe_free(ch->id); + g_safe_free(ch->idn); g_free(ch); } @@ -150,8 +151,9 @@ static void decoder_option_free(void *data) g_slist_free_full(opt->values, &variant_free); variant_free(opt->def); - g_free(opt->desc); - g_free(opt->id); + g_safe_free(opt->desc); + g_safe_free(opt->id); + g_safe_free(opt->idn); g_free(opt); } @@ -226,6 +228,11 @@ static int get_channels(const struct srd_decoder *d, const char *attr, goto err_out; } pdch = g_malloc(sizeof(struct srd_channel)); + pdch->id = NULL; + pdch->name = NULL; + pdch->desc = NULL; + pdch->idn = NULL; + /* Add to list right away so it doesn't get lost. */ pdchl = g_slist_prepend(pdchl, pdch); @@ -236,6 +243,8 @@ static int get_channels(const struct srd_decoder *d, const char *attr, if (py_dictitem_as_str(py_entry, "desc", &pdch->desc) != SRD_OK) goto err_out; + py_dictitem_as_str(py_entry, "idn", &pdch->idn); + pdch->type = py_dictitem_to_int(py_entry, "type"); if (pdch->type < 0) pdch->type = SRD_CHANNEL_COMMON; @@ -303,6 +312,12 @@ static int get_options(struct srd_decoder *d) } o = g_malloc0(sizeof(struct srd_decoder_option)); + o->id = NULL; + o->idn = NULL; + o->desc = NULL; + o->def = NULL; + o->values = NULL; + /* Add to list right away so it doesn't get lost. */ options = g_slist_prepend(options, o); @@ -321,6 +336,11 @@ static int get_options(struct srd_decoder *d) goto err_out; } + py_str = PyDict_GetItemString(py_opt, "idn"); + if (py_str){ + py_str_as_str(py_str, &o->idn); + } + py_default = PyDict_GetItemString(py_opt, "default"); if (py_default) { gvar = py_obj_to_variant(py_default); diff --git a/libsigrokdecode4DSL/libsigrokdecode-internal.h b/libsigrokdecode4DSL/libsigrokdecode-internal.h index f023360c..a529ae72 100644 --- a/libsigrokdecode4DSL/libsigrokdecode-internal.h +++ b/libsigrokdecode4DSL/libsigrokdecode-internal.h @@ -29,6 +29,8 @@ #include "libsigrokdecode.h" #include +#define g_safe_free(p) if((p)){g_free((p)); (p) = NULL;} + enum { SRD_TERM_HIGH, SRD_TERM_LOW, diff --git a/libsigrokdecode4DSL/libsigrokdecode.h b/libsigrokdecode4DSL/libsigrokdecode.h index 49dfc5cd..cc0ad5d8 100644 --- a/libsigrokdecode4DSL/libsigrokdecode.h +++ b/libsigrokdecode4DSL/libsigrokdecode.h @@ -234,10 +234,13 @@ struct srd_channel { int order; /** The type of the channel, such us: sclk/sdata/.../others */ int type; + /** The language text soruce id. */ + char *idn; }; struct srd_decoder_option { char *id; + char *idn; char *desc; GVariant *def; GSList *values;