From 69fb2a17e45fa10383cc4b28df2e1e9a9605d59e Mon Sep 17 00:00:00 2001 From: Developer Alexander Date: Sun, 27 Jun 2021 13:05:52 +0200 Subject: [PATCH] lua: dialog with prefilled values new_dialog() lua api gets extended to enable that dialog fields can be prefilled by lua scripts instead of always starting empty. --- epan/funnel.h | 9 ++--- epan/wslua/wslua_gui.c | 65 +++++++++++++++++++++++++++------- ui/qt/funnel_string_dialog.cpp | 31 +++++++++------- ui/qt/funnel_string_dialog.h | 8 ++--- 4 files changed, 80 insertions(+), 33 deletions(-) diff --git a/epan/funnel.h b/epan/funnel.h index 99a57cb88c..24ae0b3ead 100644 --- a/epan/funnel.h +++ b/epan/funnel.h @@ -60,10 +60,11 @@ typedef struct _funnel_ops_t { void (*add_button)(funnel_text_window_t* win, funnel_bt_t* cb, const char* label); void (*new_dialog)(const gchar* title, - const gchar** fieldnames, - funnel_dlg_cb_t dlg_cb, - void* data, - funnel_dlg_cb_data_free_t dlg_cb_data_free); + const gchar** field_names, + const gchar** field_values, + funnel_dlg_cb_t dlg_cb, + void* data, + funnel_dlg_cb_data_free_t dlg_cb_data_free); void (*close_dialogs)(void); diff --git a/epan/wslua/wslua_gui.c b/epan/wslua/wslua_gui.c index 0e3cffbf4e..ec0c4c426b 100644 --- a/epan/wslua/wslua_gui.c +++ b/epan/wslua/wslua_gui.c @@ -246,12 +246,14 @@ WSLUA_FUNCTION wslua_new_dialog(lua_State* L) { /* */ #define WSLUA_ARG_new_dialog_TITLE 1 /* The title of the dialog. */ #define WSLUA_ARG_new_dialog_ACTION 2 /* Action to be performed when the user presses btn:[OK]. */ -/* WSLUA_MOREARGS new_dialog Strings to be used a labels of the dialog's fields. Each string creates a new labeled field. The first field is required. */ +/* WSLUA_MOREARGS new_dialog Strings to be used a labels of the dialog's fields. Each string creates a new labeled field. The first field is required. +Instead of a strings it is possible to provide tables with fields 'name' and 'value' of type string. Then the created dialog's field will labeld with the content of name and prefilled with the content of value.*/ const gchar* title; int top = lua_gettop(L); int i; - GPtrArray* labels; + GPtrArray* field_names; + GPtrArray* field_values; struct _dlg_cb_data* dcbd; if (! ops) { @@ -286,26 +288,63 @@ WSLUA_FUNCTION wslua_new_dialog(lua_State* L) { /* dcbd->func_ref = luaL_ref(L, LUA_REGISTRYINDEX); lua_remove(L,1); - labels = g_ptr_array_new_with_free_func(g_free); + field_names = g_ptr_array_new_with_free_func(g_free); + field_values = g_ptr_array_new_with_free_func(g_free); top -= 2; - for (i = 1; i <= top; i++) { - if (! lua_isstring(L,i)) { - g_ptr_array_free(labels,TRUE); - g_free (dcbd); - WSLUA_ERROR(new_dialog,"All fields must be strings"); + for (i = 1; i <= top; i++) + { + if (lua_isstring(L, i)) + { + gchar* field_name = g_strdup(luaL_checkstring(L, i)); + gchar* field_value = g_strdup(""); + g_ptr_array_add(field_names, (gpointer)field_name); + g_ptr_array_add(field_values, (gpointer)field_value); + } + else if (lua_istable(L, i)) + { + lua_getfield(L, i, "name"); + lua_getfield(L, i, "value"); + + if (!lua_isstring(L, -2)) + { + lua_pop(L, 2); + + g_ptr_array_free(field_names, TRUE); + g_ptr_array_free(field_values, TRUE); + g_free(dcbd); + WSLUA_ERROR(new_dialog, "All fields must be strings or a table with a string field 'name'."); + return 0; + } + + gchar* field_name = g_strdup(luaL_checkstring(L, -2)); + gchar* field_value = lua_isstring(L, -1) ? + g_strdup(luaL_checkstring(L, -1)) : + g_strdup(""); + + g_ptr_array_add(field_names, (gpointer)field_name); + g_ptr_array_add(field_values, (gpointer)field_value); + + lua_pop(L, 2); + } + else + { + g_ptr_array_free(field_names, TRUE); + g_ptr_array_free(field_values, TRUE); + g_free(dcbd); + WSLUA_ERROR(new_dialog, "All fields must be strings or a table with a string field 'name'."); return 0; } - - g_ptr_array_add(labels,(gpointer)g_strdup(luaL_checkstring(L,i))); } - g_ptr_array_add(labels,NULL); + g_ptr_array_add(field_names, NULL); + g_ptr_array_add(field_values, NULL); - ops->new_dialog(title, (const gchar**)(labels->pdata), lua_dialog_cb, dcbd, g_free); + ops->new_dialog(title, (const gchar**)(field_names->pdata), (const gchar**)(field_values->pdata), lua_dialog_cb, dcbd, g_free); - g_ptr_array_free(labels,TRUE); + g_ptr_array_free(field_names, TRUE); + g_ptr_array_free(field_values, TRUE); WSLUA_RETURN(0); } diff --git a/ui/qt/funnel_string_dialog.cpp b/ui/qt/funnel_string_dialog.cpp index 0e6abcf7d0..8eec725ec9 100644 --- a/ui/qt/funnel_string_dialog.cpp +++ b/ui/qt/funnel_string_dialog.cpp @@ -20,8 +20,7 @@ static FunnelStringDialogHelper dialogHelper; const int min_edit_width_ = 20; // em widths -FunnelStringDialog::FunnelStringDialog(const QString title, const QStringList field_name_list, funnel_dlg_cb_t dialog_cb, void *dialog_cb_data, - funnel_dlg_cb_data_free_t dialog_data_free_cb) : +FunnelStringDialog::FunnelStringDialog(const QString title, const QList> field_list, funnel_dlg_cb_t dialog_cb, void* dialog_cb_data, funnel_dlg_cb_data_free_t dialog_data_free_cb) : QDialog(NULL), ui(new Ui::FunnelStringDialog), dialog_cb_(dialog_cb), @@ -33,10 +32,12 @@ FunnelStringDialog::FunnelStringDialog(const QString title, const QStringList fi int one_em = fontMetrics().height(); int row = 0; - foreach (QString field_name, field_name_list) { - QLabel *field_label = new QLabel(field_name, this); + QPair field; + foreach(field, field_list) { + QLabel* field_label = new QLabel(field.first, this); ui->stringGridLayout->addWidget(field_label, row, 0); - QLineEdit *field_edit = new QLineEdit(this); + QLineEdit* field_edit = new QLineEdit(this); + field_edit->setText(field.second); field_edit->setMinimumWidth(one_em * min_edit_width_); field_edits_ << field_edit; ui->stringGridLayout->addWidget(field_edit, row, 1); @@ -84,9 +85,9 @@ void FunnelStringDialog::on_buttonBox_accepted() dialog_cb_(user_input, dialog_cb_data_); } -void FunnelStringDialog::stringDialogNew(const QString title, const QStringList field_name_list, funnel_dlg_cb_t dialog_cb, void *dialog_cb_data, funnel_dlg_cb_data_free_t dialog_cb_data_free) +void FunnelStringDialog::stringDialogNew(const QString title, QList> field_list, funnel_dlg_cb_t dialog_cb, void* dialog_cb_data, funnel_dlg_cb_data_free_t dialog_cb_data_free) { - FunnelStringDialog *fsd = new FunnelStringDialog(title, field_name_list, dialog_cb, dialog_cb_data, dialog_cb_data_free); + FunnelStringDialog* fsd = new FunnelStringDialog(title, field_list, dialog_cb, dialog_cb_data, dialog_cb_data_free); connect(&dialogHelper, SIGNAL(closeDialogs()), fsd, SLOT(close())); fsd->show(); } @@ -96,13 +97,19 @@ void FunnelStringDialogHelper::emitCloseDialogs() emit closeDialogs(); } -void string_dialog_new(const gchar *title, const gchar **fieldnames, funnel_dlg_cb_t dialog_cb, void *dialog_cb_data, funnel_dlg_cb_data_free_t dialog_cb_data_free) +void string_dialog_new(const gchar* title, const gchar** field_names, const gchar** field_values, funnel_dlg_cb_t dialog_cb, void* dialog_cb_data, funnel_dlg_cb_data_free_t dialog_cb_data_free) { - QStringList field_name_list; - for (int i = 0; fieldnames[i]; i++) { - field_name_list << fieldnames[i]; + QList> field_list; + for (int i = 0; field_names[i]; i++) { + QPair field = QPair(QString(field_names[i]), QString("")); + if (field_values != NULL && field_values[i]) + { + field.second = QString(field_values[i]); + } + + field_list << field; } - FunnelStringDialog::stringDialogNew(title, field_name_list, dialog_cb, dialog_cb_data, dialog_cb_data_free); + FunnelStringDialog::stringDialogNew(title, field_list, dialog_cb, dialog_cb_data, dialog_cb_data_free); } void string_dialogs_close(void) diff --git a/ui/qt/funnel_string_dialog.h b/ui/qt/funnel_string_dialog.h index 0f867682a7..c59a3ada67 100644 --- a/ui/qt/funnel_string_dialog.h +++ b/ui/qt/funnel_string_dialog.h @@ -28,11 +28,11 @@ class FunnelStringDialog : public QDialog Q_OBJECT public: - explicit FunnelStringDialog(const QString title, const QStringList field_name_list, funnel_dlg_cb_t dialog_cb, void *dialog_cb_data, funnel_dlg_cb_data_free_t dialog_cb_data_free); + explicit FunnelStringDialog(const QString title, const QList> field_list, funnel_dlg_cb_t dialog_cb, void* dialog_cb_data, funnel_dlg_cb_data_free_t dialog_data_free_cb); ~FunnelStringDialog(); // Funnel ops - static void stringDialogNew(const QString title, const QStringList field_name_list, funnel_dlg_cb_t dialog_cb, void* dialog_cb_data, funnel_dlg_cb_data_free_t dialog_cb_data_free); + static void stringDialogNew(const QString title, const QList> field_list, funnel_dlg_cb_t dialog_cb, void* dialog_cb_data, funnel_dlg_cb_data_free_t dialog_cb_data_free); void accept(); void reject(); @@ -60,8 +60,8 @@ signals: }; extern "C" { -void string_dialog_new(const gchar* title, const gchar** fieldnames, funnel_dlg_cb_t dialog_cb, void* dialog_cb_data, funnel_dlg_cb_data_free_t dialog_cb_data_free); -void string_dialogs_close(void); + void string_dialog_new(const gchar* title, const gchar** field_names, const gchar** field_values, funnel_dlg_cb_t dialog_cb, void* dialog_cb_data, funnel_dlg_cb_data_free_t dialog_cb_data_free); + void string_dialogs_close(void); } #endif // FUNNEL_STRING_DIALOG_H