[moserial] Allow custom baud rate input.



commit c6f0fc8a9f8b0a5f32ff52e6b5a08c758e80af16
Author: Mictronics <github mictronics de>
Date:   Mon Apr 27 17:54:20 2020 +0200

    Allow custom baud rate input.

 data/ui/settings_dialog.ui | 32 ++++++++++++++++++++++----------
 src/MoUtils.vala           | 12 ++++++++----
 src/SerialConnection.vala  |  3 +++
 src/SettingsDialog.vala    | 29 ++++++++++++++++++-----------
 4 files changed, 51 insertions(+), 25 deletions(-)
---
diff --git a/data/ui/settings_dialog.ui b/data/ui/settings_dialog.ui
index 5d40ab7..f527c04 100644
--- a/data/ui/settings_dialog.ui
+++ b/data/ui/settings_dialog.ui
@@ -170,16 +170,6 @@
                     <property name="top_attach">2</property>
                   </packing>
                 </child>
-                <child>
-                  <object class="GtkComboBox" id="settings_baud_rate">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                  </object>
-                  <packing>
-                    <property name="left_attach">1</property>
-                    <property name="top_attach">1</property>
-                  </packing>
-                </child>
                 <child>
                   <object class="GtkCheckButton" id="settings_local_echo">
                     <property name="label" translatable="yes">Echo received data</property>
@@ -311,6 +301,28 @@
                     <property name="top_attach">0</property>
                   </packing>
                 </child>
+                <child>
+                  <object class="GtkComboBox" id="settings_baud_rate">
+                    <property name="visible">True</property>
+                    <property name="can_focus">False</property>
+                    <property name="has_entry">True</property>
+                    <property name="entry_text_column">0</property>
+                    <property name="id_column">0</property>
+                    <child internal-child="entry">
+                      <object class="GtkEntry" id="settings_baudrate_input">
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="primary_icon_activatable">False</property>
+                        <property name="secondary_icon_activatable">False</property>
+                        <property name="input_purpose">number</property>
+                      </object>
+                    </child>
+                  </object>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="top_attach">1</property>
+                  </packing>
+                </child>
               </object>
               <packing>
                 <property name="expand">True</property>
diff --git a/src/MoUtils.vala b/src/MoUtils.vala
index 0f35d58..6776f80 100644
--- a/src/MoUtils.vala
+++ b/src/MoUtils.vala
@@ -95,7 +95,7 @@ public class MoUtils : GLib.Object {
         return message;
     }
 
-    public static void populateComboBox (ComboBox Combo, string[] val_array) {
+    public static void populateComboBox (ComboBox Combo, string[] val_array, bool render_cell = true) {
         Gtk.ListStore Model = new Gtk.ListStore (1, typeof (string));
         foreach (string val_item in val_array) {
             TreeIter iter;
@@ -103,9 +103,13 @@ public class MoUtils : GLib.Object {
             Model.set (iter, 0, _(val_item));
         }
         Combo.set_model (Model);
-        CellRenderer Cell = new CellRendererText ();
-        Combo.pack_start (Cell, true);
-        Combo.set_attributes (Cell, "text", 0);
+        // Make cell rendering optional
+        // Required for standard ComboBox, but not for ComboBox/Entry combination.
+        if (render_cell) {
+            CellRenderer Cell = new CellRendererText ();
+            Combo.pack_start (Cell, true);
+            Combo.set_attributes (Cell, "text", 0);
+        }
     }
 }
 
diff --git a/src/SerialConnection.vala b/src/SerialConnection.vala
index 038d718..04e5af1 100644
--- a/src/SerialConnection.vala
+++ b/src/SerialConnection.vala
@@ -202,6 +202,9 @@ public class moserial.SerialConnection : GLib.Object {
             case 3000000:
                 baudRate = Linux.Termios.B3000000;
                 break;
+            default:
+                baudRate = settings.baudRate;
+                break;
         }
 
         Posix.cfsetospeed (ref newtio, baudRate);
diff --git a/src/SettingsDialog.vala b/src/SettingsDialog.vala
index e4f1273..9209040 100644
--- a/src/SettingsDialog.vala
+++ b/src/SettingsDialog.vala
@@ -24,6 +24,7 @@ public class moserial.SettingsDialog : GLib.Object {
     // Does anyone have more than 32 serial ports?
     const int max_devices = 32;
 
+    private Window parent;
     private Settings currentSettings;
     private Dialog dialog;
     private Button cancelButton;
@@ -40,9 +41,11 @@ public class moserial.SettingsDialog : GLib.Object {
     private CheckButton localEcho;
     private Gtk.ListStore deviceModel;
     private Gtk.Entry deviceInput;
+    private Gtk.Entry baudRateInput;
     public signal void updateSettings (Settings settings);
 
     public SettingsDialog (Window parent) {
+        this.parent = parent;
         var builder = new Gtk.Builder.from_resource (Config.UIROOT + "settings_dialog.ui");
 
         dialog = (Dialog) builder.get_object ("settings_dialog");
@@ -50,9 +53,10 @@ public class moserial.SettingsDialog : GLib.Object {
         cancelButton = (Button) builder.get_object ("settings_cancel_button");
         okButton = (Button) builder.get_object ("settings_ok_button");
         deviceInput = (Gtk.Entry)builder.get_object ("settings_device_input");
+        baudRateInput = (Gtk.Entry)builder.get_object ("settings_baudrate_input");
 
         baudRateCombo = (ComboBox) builder.get_object ("settings_baud_rate");
-        MoUtils.populateComboBox (baudRateCombo, Settings.BaudRateItems);
+        MoUtils.populateComboBox (baudRateCombo, Settings.BaudRateItems, false);
 
         dataBitsCombo = (ComboBox) builder.get_object ("settings_data_bits");
         MoUtils.populateComboBox (dataBitsCombo, Settings.DataBitItems);
@@ -134,15 +138,7 @@ public class moserial.SettingsDialog : GLib.Object {
         }
 
         // Baud Rate
-        t = baudRateCombo.get_model ();
-        success = t.get_iter_first (out ti);
-        while (success) {
-            Value str_data;
-            t.get_value (ti, 0, out str_data);
-            if (str_data.get_string () == "%i".printf (currentSettings.baudRate))
-                baudRateCombo.set_active_iter (ti);
-            success = t.iter_next (ref ti);
-        }
+        baudRateInput.set_text ("%i".printf (currentSettings.baudRate));
 
         // Data Bits
         t = dataBitsCombo.get_model ();
@@ -208,7 +204,18 @@ public class moserial.SettingsDialog : GLib.Object {
             device = deviceInput.get_text ();
         }
 
-        baudRate = int.parse (Settings.BaudRateItems[baudRateCombo.get_active ()]);
+        string unparsed = null;
+        if (!int.try_parse (baudRateInput.get_text (), out baudRate, out unparsed, 10)) {
+            var dialog = new MessageDialog (
+                this.parent,
+                DialogFlags.DESTROY_WITH_PARENT,
+                MessageType.ERROR,
+                ButtonsType.CLOSE, "Please enter valid baud rate!");
+            dialog.run ();
+            dialog.destroy ();
+            return;
+        }
+
         dataBits = int.parse (Settings.DataBitItems[dataBitsCombo.get_active ()]);
         stopBits = int.parse (Settings.StopBitItems[stopBitsCombo.get_active ()]);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]