[chronojump] comboSelectJump class, big refactoring of combo_select_jumps



commit 970ce36381fab7e5618b1e57b42efa7e57a20866
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Oct 7 22:01:46 2016 +0200

    comboSelectJump class, big refactoring of combo_select_jumps

 po/POTFILES.in              |    1 +
 src/Makefile.am             |    2 +
 src/gui/chronojump.cs       |   65 ++++++-------------
 src/gui/cjCombo.cs          |  153 +++++++++++++++++++++++++++++++++++++++++++
 src/gui/jump.cs             |    9 +--
 src/sqlite/jumpType.cs      |   67 ++++++++++++++++++-
 src/sqlite/usefulObjects.cs |   49 ++++++++++++++
 7 files changed, 293 insertions(+), 53 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 4a2bc35..2e737f0 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -71,6 +71,7 @@ src/sqlite/session.cs
 src/sqlite/speciallity.cs
 src/sqlite/sport.cs
 src/sqlite/stat.cs
+src/sqlite/usefulObjects.cs
 src/stats/chronojumpProfile.cs
 src/stats/djIndex.cs
 src/stats/djPower.cs
diff --git a/src/Makefile.am b/src/Makefile.am
index cd2a53e..a10102c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -10,6 +10,7 @@ SOURCES = \
        gui/chronopicRegister.cs\
        gui/chronopicWizard.cs\
        gui/confirm.cs\
+       gui/cjCombo.cs\
        gui/encoder.cs\
        gui/encoderTreeviews.cs\
        gui/encoderConfiguration.cs\
@@ -112,6 +113,7 @@ SOURCES = \
        sqlite/event.cs\
        sqlite/sport.cs\
        sqlite/speciallity.cs\
+       sqlite/usefulObjects.cs\
        sqlite/country.cs\
        sqlite/oldConvert.cs\
        sqlite/server.cs\
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index fca6f88..a56c753 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -127,7 +127,7 @@ public partial class ChronoJumpWindow
        [Widget] Gtk.TreeView treeview_pulses;
        [Widget] Gtk.TreeView treeview_multi_chronopic;
        
-       [Widget] Gtk.Box hbox_combo_select_jumps;
+       [Widget] Gtk.HBox hbox_combo_select_jumps;
        [Widget] Gtk.Box hbox_combo_select_jumps_rj;
        [Widget] Gtk.Box hbox_combo_select_runs;
        [Widget] Gtk.Box hbox_combo_select_runs_interval;
@@ -165,6 +165,8 @@ public partial class ChronoJumpWindow
        [Widget] Gtk.ComboBox combo_select_jumps_rj;
        [Widget] Gtk.ComboBox combo_select_runs;
        [Widget] Gtk.ComboBox combo_select_runs_interval;
+
+       CjComboSelectJumps comboSelectJumps; //new since 1.6.3. Using gui/cjCombo.cs
        
        [Widget] Gtk.ComboBox combo_result_jumps;
        [Widget] Gtk.ComboBox combo_result_jumps_rj;
@@ -1938,39 +1940,22 @@ public partial class ChronoJumpWindow
         *  --------------------------------------------------------
         */
        
-       string [] selectJumpsString;
        string [] selectJumpsRjString;
        string [] selectRunsString;
        string [] selectRunsIntervalString;
 
        // ---------------- combo_select ----------------------
 
-       private void createComboSelectJumps(bool create) {
+       private void createComboSelectJumps(bool create) 
+       {
                if(create)
-                       combo_select_jumps = ComboBox.NewText ();
-
-               string [] jumpTypes = SqliteJumpType.SelectJumpTypes(false, "", "", false); //without 
alljumpsname, without filter, not only name
-               selectJumpsString = new String [jumpTypes.Length];
-               string [] jumpNamesToCombo = new String [jumpTypes.Length];
-               int i =0;
-               foreach(string jumpType in jumpTypes) {
-                       string [] j = jumpType.Split(new char[] {':'});
-                       string nameTranslated = Catalog.GetString(j[1]);
-                       selectJumpsString[i] = 
-                               j[0] + ":" + j[1] + ":" + nameTranslated + ":" +        //uniqueID, name, 
nameTranslated
-                               j[2] + ":" + j[3] + ":" + j[4];                         //startIn, weight, 
description
-                       jumpNamesToCombo[i] = nameTranslated;
-                       i++;
-               }
-
-               UtilGtk.ComboUpdate(combo_select_jumps, jumpNamesToCombo, "");
-               combo_select_jumps.Active = 0;
-               combo_select_jumps.Changed += new EventHandler (on_combo_select_jumps_changed);
-
-               if(create) {
-                       hbox_combo_select_jumps.PackStart(combo_select_jumps, true, true, 0);
-                       hbox_combo_select_jumps.ShowAll();
-                       combo_select_jumps.Sensitive = false;
+               {
+                       comboSelectJumps = new CjComboSelectJumps(combo_select_jumps, 
hbox_combo_select_jumps);
+                       combo_select_jumps = comboSelectJumps.Combo;
+                       combo_select_jumps.Changed += new EventHandler (on_combo_select_jumps_changed);
+               } else {
+                       comboSelectJumps.Fill();
+                       combo_select_jumps = comboSelectJumps.Combo;
                }
        }
        
@@ -2428,7 +2413,7 @@ public partial class ChronoJumpWindow
 
                if (result.success) {
                        //if a simple jump type has been added
-                       createComboSelectJumps(false); //this will update also the selectJumpsString
+                       createComboSelectJumps(false);
                        UtilGtk.ComboUpdate(combo_result_jumps,
                                        SqliteJumpType.SelectJumpTypes(false, Constants.AllJumpsName, "", 
true), ""); //without filter, only select name
                        combo_select_jumps.Active = 0;
@@ -3980,10 +3965,6 @@ public partial class ChronoJumpWindow
                if(execute_auto_doing)
                        sensitiveGuiAutoExecuteOrWait (true);
                
-               //currentJumpType is already defined in selecting name from combo or from jumpsMoreWin
-               //string jumpEnglishName = Util.FindOnArray(':',2,1, 
UtilGtk.ComboGetActive(combo_select_jumps), selectJumpsString);
-               //currentJumpType = new JumpType(jumpEnglishName);
-
                double jumpWeight = 0;
                if(currentJumpType.HasWeight) {
                        if(extra_window_jumps_option == "%") 
@@ -5949,7 +5930,7 @@ LogB.Debug("X");
        private void on_jump_type_add_accepted (object o, EventArgs args) {
                LogB.Information("ACCEPTED Add new jump type");
                if(jumpTypeAddWin.InsertedSimple) {
-                       createComboSelectJumps(false); //this will update also the selectJumpsString
+                       createComboSelectJumps(false);
 
                        UtilGtk.ComboUpdate(combo_result_jumps, 
                                        SqliteJumpType.SelectJumpTypes(false, Constants.AllJumpsName, "", 
true), ""); //without filter, only select name
@@ -6041,17 +6022,12 @@ LogB.Debug("X");
        
        //----
        
-       private void on_deleted_jump_type (object o, EventArgs args) {
-               //first delete if from combos
-               string translatedName = Util.FindOnArray(':', 2, 1, jumpsMoreWin.SelectedEventName, 
selectJumpsString);
-               UtilGtk.ComboDelThisValue(combo_select_jumps, translatedName);
-               UtilGtk.ComboDelThisValue(combo_result_jumps, translatedName);
-               
-               //2nd delete if from global string. -1 selects all row
-               string row = Util.FindOnArray(':',1, -1, jumpsMoreWin.SelectedEventName, selectJumpsString);
-               selectJumpsString = Util.DeleteString(selectJumpsString, row);
+       private void on_deleted_jump_type (object o, EventArgs args)
+       {
+               string translatedName = comboSelectJumps.GetNameTranslated(jumpsMoreWin.SelectedEventName);
+               combo_select_jumps = comboSelectJumps.DeleteValue(translatedName);
 
-               combo_select_jumps.Active = 0;
+               UtilGtk.ComboDelThisValue(combo_result_jumps, translatedName);
                combo_result_jumps.Active = 0;
 
                extra_window_jumps_initialize(new JumpType("Free"));
@@ -6225,8 +6201,7 @@ LogB.Debug("X");
                        label_jump_auto_current_person.Text = currentPerson.Name;
 
                        //select the test
-                       int rowTest = Convert.ToInt32(Util.FindOnArray(':', 0, -2, 
ea.testUniqueID.ToString(), selectJumpsString));
-                       combo_select_jumps.Active = rowTest;
+                       combo_select_jumps = comboSelectJumps.SelectById(ea.testUniqueID);
                        label_jump_auto_current_test.Text = "(" + ea.testTrName + ")";
                        
                        //put GUI on auto_waiting
diff --git a/src/gui/cjCombo.cs b/src/gui/cjCombo.cs
new file mode 100644
index 0000000..6368427
--- /dev/null
+++ b/src/gui/cjCombo.cs
@@ -0,0 +1,153 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * Chronojump is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or   
+ *    (at your option) any later version.
+ *    
+ * Chronojump is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ *    GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Copyright (C) 2016   Xavier de Blas <xaviblas gmail com> 
+ */
+
+using System;
+using System.Collections.Generic; //List<T>
+using Gtk;
+
+public class CjCombo
+{
+       protected Gtk.ComboBox combo;
+       protected Gtk.HBox hbox;
+
+       protected void create() 
+       {
+               combo = ComboBox.NewText ();
+       }
+       
+       public virtual void Fill()
+       {
+       }
+       
+       protected void package() 
+       {
+               hbox.PackStart(combo, true, true, 0);
+               hbox.ShowAll();
+               combo.Sensitive = false;
+       }
+       
+       public virtual string GetSelectedNameEnglish()
+       {
+               return "";
+       }
+       
+       public virtual Gtk.ComboBox SelectById(int id) 
+       {
+               return combo;
+       }
+       
+       public virtual string GetNameTranslated(string nameEnglish)
+       {
+               return "";
+       }
+       
+       public virtual void MakeActive(string nameEnglish)
+       {
+       }
+
+       public Gtk.ComboBox DeleteValue(string nameTranslated)
+       {
+               UtilGtk.ComboDelThisValue(combo, nameTranslated);
+               combo.Active = 0;
+               return combo;
+       }
+
+       public Gtk.ComboBox Combo {
+               get { return combo; }
+       }
+}
+
+public class CjComboSelectJumps : CjCombo
+{
+       List<SelectJumpTypes> jumpTypes;
+
+       public CjComboSelectJumps(Gtk.ComboBox combo_select_jumps, Gtk.HBox hbox_combo_select_jumps) 
+       {
+               this.combo = combo_select_jumps;
+               this.hbox = hbox_combo_select_jumps;
+
+               create();
+               
+               Fill();
+       
+               package();      
+       }
+
+       //if we just need to update values, call only this method
+       public override void Fill()
+       {
+               jumpTypes = SqliteJumpType.SelectJumpTypesNew(false, "", "", false); //without alljumpsname, 
without filter, not only name
+
+               string [] jumpNamesToCombo = new String [jumpTypes.Count];
+               int i =0;
+               foreach(SelectJumpTypes jumpType in jumpTypes)
+                       jumpNamesToCombo[i++] = jumpType.NameTranslated;
+               
+               UtilGtk.ComboUpdate(combo, jumpNamesToCombo, "");
+               combo.Active = 0;
+       }
+
+       //TODO: refactor this. Move it to parent class  
+       public override string GetSelectedNameEnglish()
+       {
+               string nameTranslatedSelected = UtilGtk.ComboGetActive(combo);
+               foreach(SelectJumpTypes jumpType in jumpTypes)
+                       if(jumpType.NameTranslated == nameTranslatedSelected)
+                               return jumpType.NameEnglish;
+
+               return "";
+       }
+
+       //TODO: refactor this. Move it to parent class  
+       public Gtk.ComboBox SelectById(int id)
+       {
+               int pos = 0;
+               foreach(SelectJumpTypes jumpType in jumpTypes) 
+               {
+                       if(jumpType.Id == id) 
+                       {
+                               combo.Active = pos;
+                               break;
+                       }
+
+                       pos ++;
+               }
+
+               return combo;
+       }
+
+       //TODO: refactor this. Move it to parent class  
+       public override string GetNameTranslated(string nameEnglish)
+       {
+               foreach(SelectJumpTypes jumpType in jumpTypes)
+                       if(jumpType.NameEnglish == nameEnglish)
+                               return jumpType.NameTranslated;
+
+               return "";
+       }
+
+       //TODO: refactor this. Move it to parent class  
+       public override void MakeActive(string nameEnglish)
+       {
+               foreach(SelectJumpTypes jumpType in jumpTypes)
+                       if(jumpType.NameTranslated == nameEnglish)
+                               combo.Active = UtilGtk.ComboMakeActive(combo, jumpType.NameTranslated);
+       }
+}
diff --git a/src/gui/jump.cs b/src/gui/jump.cs
index c605366..5613af0 100644
--- a/src/gui/jump.cs
+++ b/src/gui/jump.cs
@@ -1082,7 +1082,7 @@ partial class ChronoJumpWindow
        
        private void on_extra_window_jumps_test_changed(object o, EventArgs args)
        {
-               string jumpEnglishName = Util.FindOnArray(':',2,1, 
UtilGtk.ComboGetActive(combo_select_jumps), selectJumpsString);
+               string jumpEnglishName = comboSelectJumps.GetSelectedNameEnglish();
                currentJumpType = createJumpType(jumpEnglishName, true);
        
                extra_window_jumps_initialize(currentJumpType);
@@ -1267,11 +1267,10 @@ partial class ChronoJumpWindow
                SqlitePreferences.Update("allowFinishRjAfterTime", 
checkbutton_allow_finish_rj_after_time.Active.ToString(), false);
        }
 
-       private void on_more_jumps_update_test (object o, EventArgs args) {
+       private void on_more_jumps_update_test (object o, EventArgs args) 
+       {
                currentEventType = new JumpType(jumpsMoreWin.SelectedEventName);
-               string jumpTranslatedName = Util.FindOnArray(':',1,2, jumpsMoreWin.SelectedEventName, 
selectJumpsString);
-               
-               combo_select_jumps.Active = UtilGtk.ComboMakeActive(combo_select_jumps, jumpTranslatedName);  
  
+               comboSelectJumps.MakeActive(jumpsMoreWin.SelectedEventName);
        }
        
        private void on_more_jumps_rj_update_test (object o, EventArgs args) {
diff --git a/src/sqlite/jumpType.cs b/src/sqlite/jumpType.cs
index d2fee6a..96bfb8a 100644
--- a/src/sqlite/jumpType.cs
+++ b/src/sqlite/jumpType.cs
@@ -22,6 +22,7 @@ using System;
 using System.Data;
 using System.IO;
 using System.Collections; //ArrayList
+using System.Collections.Generic; //List<T>
 using Mono.Data.Sqlite;
 
 
@@ -237,6 +238,66 @@ class SqliteJumpType : Sqlite
                        Sqlite.Close();
                }
        }
+       
+       //use SelectJumpTypes object. Since 1.6.3
+       public static List<SelectJumpTypes> SelectJumpTypesNew(bool dbconOpened, string allJumpsName, string 
filter, bool onlyName) 
+       {
+               //allJumpsName: add and "allJumpsName" value
+               //filter:
+               //      "" all jumps,
+               //      "TC" only with previous fall,
+               //      "nonTC" only not with previous fall
+               //      used in gui/stats.cs
+               //onlyName: return only type name
+
+               string whereString = "";
+               if(filter == "TC") { whereString = " WHERE startIn == 0 "; }
+               else if(filter == "nonTC") { whereString = " WHERE startIn == 1 "; }
+
+               openIfNeeded(dbconOpened);
+
+               dbcmd.CommandText = "SELECT * " +
+                       " FROM " + Constants.JumpTypeTable + " " +
+                       whereString +
+                       " ORDER BY uniqueID";
+
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+
+               SqliteDataReader reader;
+               reader = dbcmd.ExecuteReader();
+
+               List<SelectJumpTypes> jumpTypes = new List<SelectJumpTypes>();
+
+               int count = new int();
+               count = 0;
+
+               SelectJumpTypes jumpType;
+               if(allJumpsName != "") {
+                       jumpType = new SelectJumpTypes(allJumpsName);
+                       jumpTypes.Add(jumpType);
+               }
+
+               while(reader.Read()) {
+                       if(onlyName) {
+                               jumpType = new SelectJumpTypes(reader[1].ToString());
+                       } else {
+                               jumpType = new SelectJumpTypes(
+                                               Convert.ToInt32(reader[0]),     //uniqueID
+                                               reader[1].ToString(),           //nameEnglish
+                                               Util.IntToBool(Convert.ToInt32(reader[2].ToString())),  
//startIn
+                                               Util.IntToBool(Convert.ToInt32(reader[3].ToString())),  
//hasWeight
+                                               reader[4].ToString());          //description
+                       }
+                       jumpTypes.Add(jumpType);
+               }
+
+               reader.Close();
+               closeIfNeeded(dbconOpened);
+
+               return jumpTypes;
+       }
+
 
        public static string[] SelectJumpTypes(bool dbconOpened, string allJumpsName, string filter, bool 
onlyName) 
        {
@@ -247,11 +308,11 @@ class SqliteJumpType : Sqlite
                //      "nonTC" only not with previous fall
                //      used in gui/stats.cs
                //onlyName: return only type name
-       
+
                string whereString = "";
                if(filter == "TC") { whereString = " WHERE startIn == 0 "; }
                else if(filter == "nonTC") { whereString = " WHERE startIn == 1 "; }
-       
+
                if(! dbconOpened)       
                        Sqlite.Open();
 
@@ -259,7 +320,7 @@ class SqliteJumpType : Sqlite
                        " FROM " + Constants.JumpTypeTable + " " +
                        whereString +
                        " ORDER BY uniqueID";
-               
+
                LogB.SQL(dbcmd.CommandText.ToString());
                dbcmd.ExecuteNonQuery();
 
diff --git a/src/sqlite/usefulObjects.cs b/src/sqlite/usefulObjects.cs
new file mode 100644
index 0000000..393f30d
--- /dev/null
+++ b/src/sqlite/usefulObjects.cs
@@ -0,0 +1,49 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * Chronojump is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or   
+ *    (at your option) any later version.
+ *    
+ * Chronojump is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ *    GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Copyright (C) 2016   Xavier de Blas <xaviblas gmail com> 
+ */
+
+using System;
+using Mono.Unix;
+
+public class SelectJumpTypes
+{
+       public int Id;
+       public string NameEnglish;
+       public string NameTranslated;
+       public bool StartIn;
+       public bool HasWeight;
+       public string Description;
+
+       public SelectJumpTypes(string nameEnglish)
+       {
+               this.NameEnglish = nameEnglish;
+       }
+
+       public SelectJumpTypes(int id, string nameEnglish, bool startIn, bool hasWeight, string description)
+       {
+               this.Id = id;
+               this.NameEnglish = nameEnglish;
+               this.NameTranslated = Catalog.GetString(nameEnglish);
+               this.StartIn = startIn;
+               this.HasWeight = hasWeight;
+               this.Description = description;
+       }
+
+}
+


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