Skip to content

Commit

Permalink
Merge pull request #32 from TytanRock/combobox-deviceselection
Browse files Browse the repository at this point in the history
Selected Device is now a combobox
  • Loading branch information
TytanRock authored Mar 14, 2021
2 parents 4bb9998 + 7f44773 commit 0cd14de
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"/usr/lib/x86_64-linux-gnu/dbus-1.0/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64"
Expand Down
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/PenguinTuner",
"program": "${workspaceFolder}/build/penguintuner",
"additionalSOLibSearchPath": "/usr/lib",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build",
Expand Down
9 changes: 6 additions & 3 deletions include/can_devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
#include "backend.h"

void add_slct_device_selection(GtkTreeSelection *selector);
void add_lbl_selected_device(GtkLabel *lbl);
void add_lbl_selected_device(GtkComboBox *cmbobx);
void add_txt_change_id(GtkSpinButton *idTxt);
void add_btn_firmware_file(GtkFileChooserButton *btn);
void add_txt_device_name(GtkEntry *entry);

void set_selected_device_treemodel(GtkTreeModel *treeModel);

can_device_t get_selected_device();
void set_selected_device(can_device_t newDevice);

void react_changed_id(GtkWidget *widget, gpointer data);
void react_changed_device(GtkWidget *widget, gpointer data);
void update_fields_with_tree_selection(int hardUpdate);
void react_changed_device_from_treeview(GtkWidget *widget, gpointer data);
void react_changed_device_from_combobox(GtkWidget *widget, gpointer data);
void react_update_firmware(GtkWidget *widget, gpointer data);
void react_changed_name(GtkWidget *widget, gpointer data);
void react_blink_device(GtkWidget *widget, gpointer data);
Expand Down
32 changes: 18 additions & 14 deletions src/app_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static struct

double firm_upgrade_status;
int firmware_upgrading;
int hardUpdateNextNPasses;

/* Gtk Widget references that matter */
GtkTreeModel *deviceTreeModel;
Expand Down Expand Up @@ -73,7 +74,8 @@ gint periodic_callback(gpointer data)
if(validIter) {
while(gtk_list_store_remove((GtkListStore *)_module.deviceTreeModel, &iter)) ;
}
react_changed_device(NULL, NULL); // Update selected device to fix some issues
update_fields_with_tree_selection(_module.hardUpdateNextNPasses); // Update device fields to match device list
if(_module.hardUpdateNextNPasses > 0) _module.hardUpdateNextNPasses--;
}
/* Update firmware status if we're upgrading */
gtk_level_bar_set_value(_module.firmUpgradeStatus, _module.firmware_upgrading ? _module.firm_upgrade_status : 0);
Expand Down Expand Up @@ -116,22 +118,20 @@ gboolean frontend_gui_callback(gpointer user_data)

switch(action->action)
{
case Set_ID:
{
/* Update selected device to use newest ID */
can_device_t dev = get_selected_device();
dev.id = action->intParam;
set_selected_device(dev);
break;
}
case Snapshot:
{
/* Get contents of string and copy it to module */
gtk_text_buffer_set_text(_module.snapshotTxt, *((char **)action->pointerParam), -1);
break;
}
case Set_ID:
case Set_Name:
{
/* Hard-Update for the next 500ms to ensure we pick up new name */
_module.hardUpdateNextNPasses = 5;
break;
}
case No_Action:
case Set_Name:
case Blink:
case FactoryDefault:
case Update_Firmware:
Expand Down Expand Up @@ -207,8 +207,9 @@ int connect_all_signals()
add_txt_controller_status_buffer((GtkTextBuffer *)obj);

/* Connect Selected Device label to can_devices file */
obj = gtk_builder_get_object(builder, "txt_selected_device");
add_lbl_selected_device((GtkLabel *)obj);
obj = gtk_builder_get_object(builder, "cmbobx_selected_device");
add_lbl_selected_device((GtkComboBox *)obj);
g_signal_connect(obj, "changed", G_CALLBACK(react_changed_device_from_combobox), NULL);

/* Connect the widgets used to switch between POST and SSH */
obj = gtk_builder_get_object(builder, "chckmnu_use_post");
Expand Down Expand Up @@ -254,14 +255,17 @@ int connect_all_signals()
/* Connect server address and port text change to react function */

/* Get reference to the list store */
_module.deviceTreeModel = (GtkTreeModel *)gtk_builder_get_object(builder, "lst_can_devices");
obj = gtk_builder_get_object(builder, "lst_can_devices");
_module.deviceTreeModel = (GtkTreeModel *)obj;
set_selected_device_treemodel((GtkTreeModel *)obj);


_module.serverVersion = (GtkLabel *)gtk_builder_get_object(builder, "txt_server_version");
_module.serverStatus = (GtkLabel *)gtk_builder_get_object(builder, "txt_server_status");

obj = gtk_builder_get_object(builder, "slct_device_selection");
add_slct_device_selection((GtkTreeSelection *)obj);
g_signal_connect(obj, "changed", G_CALLBACK(react_changed_device), NULL);
g_signal_connect(obj, "changed", G_CALLBACK(react_changed_device_from_treeview), NULL);

return 0;
}
102 changes: 59 additions & 43 deletions src/can_devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,23 @@
static struct
{
GtkTreeSelection *device_selector;
GtkLabel *selected_device_label;
GtkComboBox *selected_device_combobox;
GtkSpinButton *txt_dev_id;
GtkFileChooserButton *btn_firmware_file;
GtkEntry *txt_device_name;

can_device_t selected_device;
}_module;

void update_lbl();

void add_slct_device_selection(GtkTreeSelection *selector)
{
_module.device_selector = selector;
_module.selected_device.id = -1; // Initialize selected device here as well for the moment
}

void add_lbl_selected_device(GtkLabel *lbl)
void add_lbl_selected_device(GtkComboBox *cmbobx)
{
_module.selected_device_label = lbl;
_module.selected_device_combobox = cmbobx;
}

void add_txt_change_id(GtkSpinButton *idTxt)
Expand All @@ -45,59 +43,77 @@ void add_txt_device_name(GtkEntry *entry)
}


can_device_t get_selected_device()
void set_selected_device_treemodel(GtkTreeModel *listStore)
{
return _module.selected_device;
gtk_combo_box_set_model(_module.selected_device_combobox, listStore);
gtk_combo_box_set_id_column(_module.selected_device_combobox, 0);
}
void set_selected_device(can_device_t newDevice)

can_device_t get_selected_device()
{
_module.selected_device = newDevice;
update_lbl();
return _module.selected_device;
}

void update_lbl()
void update_selections(GtkTreeModel *model, GtkTreeIter *selection, int hardUpdate)
{
/* Update label with our selected device */
char labelString[100];
sprintf(labelString, "Selected Device:\n%s %d", _module.selected_device.model, _module.selected_device.id);
gtk_label_set_text(_module.selected_device_label, labelString);
{
GValue value = G_VALUE_INIT;
gtk_tree_model_get_value(model, selection, 0, &value); /* Device Name */
/* Only update name if we're doing a hardUpdate */
if(hardUpdate)
{
strcpy(_module.selected_device.name, g_value_get_string(&value));
gtk_entry_set_text(_module.txt_device_name, g_value_get_string(&value));
}
g_value_unset(&value);
}
{
GValue value = G_VALUE_INIT;
gtk_tree_model_get_value(model, selection, 2, &value); /* Hardware */
strcpy(_module.selected_device.model, g_value_get_string(&value));
g_value_unset(&value);
}
{
GValue value = G_VALUE_INIT;
gtk_tree_model_get_value(model, selection, 3, &value); /* Device ID */
_module.selected_device.id = g_value_get_int(&value);
g_value_unset(&value);
}
}

void react_changed_device(GtkWidget *widget, gpointer data)
void update_fields_with_tree_selection(int hardUpdate)
{
/* We need to find the row in the treeselector that the user selected */
GtkTreeModel *tree;
GtkTreeIter iter;
/* We need to find the row in the treeselector that the user selected */
if(gtk_tree_selection_get_selected(_module.device_selector, &tree, &iter))
{
update_selections(tree, &iter, hardUpdate);

/* If we're doing a hardUpdate, clear and re-set the combobox */
/* This updates the name in combobox, since there isn't a function to refresh it */
if(hardUpdate)
{
GValue value = G_VALUE_INIT;
gtk_tree_model_get_value(tree, &iter, 0, &value); /* Device Name */
if(widget == NULL)
{
/* Don't update name, this is from the periodic refresh */
}
else
{
/* Update name, device actually changed */
strcpy(_module.selected_device.name, g_value_get_string(&value));
gtk_entry_set_text(_module.txt_device_name, g_value_get_string(&value));
}
g_value_unset(&value);
}
{
GValue value = G_VALUE_INIT;
gtk_tree_model_get_value(tree, &iter, 2, &value); /* Hardware */
strcpy(_module.selected_device.model, g_value_get_string(&value));
g_value_unset(&value);
}
{
GValue value = G_VALUE_INIT;
gtk_tree_model_get_value(tree, &iter, 3, &value); /* Device ID */
_module.selected_device.id = g_value_get_int(&value);
g_value_unset(&value);
gtk_combo_box_set_active_iter(_module.selected_device_combobox, NULL);
gtk_combo_box_set_active_iter(_module.selected_device_combobox, &iter);
}
update_lbl();
} else {
_module.selected_device.id = -1;
}
}

void react_changed_device_from_treeview(GtkWidget *widget, gpointer data)
{
update_fields_with_tree_selection(TRUE);
}
void react_changed_device_from_combobox(GtkWidget *widget, gpointer data)
{
/* We need to find what the user selected */
GtkTreeIter iter;
if(gtk_combo_box_get_active_iter(_module.selected_device_combobox, &iter))
{
update_selections(gtk_combo_box_get_model(_module.selected_device_combobox), &iter, widget != NULL);
gtk_tree_selection_select_iter(_module.device_selector, &iter);
} else {
_module.selected_device.id = -1;
}
Expand Down
38 changes: 34 additions & 4 deletions src/ui/MainApp.ui
Original file line number Diff line number Diff line change
Expand Up @@ -770,12 +770,42 @@ Unknown</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="txt_selected_device">
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Selected Device:
Unknown</property>
<property name="justify">center</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="label" translatable="yes">Selected Device</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkComboBox" id="cmbobx_selected_device">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="has_entry">True</property>
<property name="entry_text_column">0</property>
<child internal-child="entry">
<object class="GtkEntry">
<property name="can_focus">False</property>
</object>
</child>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">1</property>
</packing>
</child>
</object>
<packing>
<property name="left_attach">0</property>
Expand Down

0 comments on commit 0cd14de

Please sign in to comment.