[chronojump] Moving to DB 1.06:"connect signal with curves" 80%



commit 2b280168c8422162c2dd19e7780845cfb3826f98
Author: Xavier de Blas <xaviblas gmail com>
Date:   Sun May 18 19:39:24 2014 +0200

    Moving to DB 1.06:"connect signal with curves" 80%

 src/encoder.cs              |   23 +++++++++++++
 src/gui/encoder.cs          |   34 ++++++++++++++++++-
 src/gui/encoderTreeviews.cs |   26 ++++++++++++++-
 src/sqlite/encoder.cs       |   77 ++++++++++++++++++++++++++++++++++++++++---
 src/sqlite/main.cs          |   40 ++++++++++++++++++----
 5 files changed, 184 insertions(+), 16 deletions(-)
---
diff --git a/src/encoder.cs b/src/encoder.cs
index 4259fc3..7e391d7 100644
--- a/src/encoder.cs
+++ b/src/encoder.cs
@@ -327,6 +327,29 @@ public class EncoderCurve
        }
 }
 
+//related to encoderSignalCurve table
+public class EncoderSignalCurve {
+       public int uniqueID;
+       public int signalID;
+       public int curveID;
+       public int msCentral;
+       
+       public EncoderSignalCurve(int uniqueID, int signalID, int curveID, int msCentral) {
+               this.uniqueID = uniqueID;
+               this.signalID = signalID;
+               this.curveID = curveID;
+               this.msCentral = msCentral;
+       }
+       
+       public string ToString() {
+               return uniqueID.ToString() + ":" + signalID.ToString() + ":" + 
+                       curveID.ToString() + ":" + msCentral.ToString();
+       }
+       
+       ~EncoderSignalCurve() {}
+}
+
+
 //used on TreeView
 public class EncoderNeuromuscularData
 {
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index ee87491..92f9181 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -216,7 +216,7 @@ public partial class ChronoJumpWindow
         * CAPTURE_EXTERNAL is deprecated (from Python)
         *
         * difference between:
-        * CURVES: calcule and recalculate, autosaves the curve at end
+        * CURVES: calcule and recalculate, autosaves the signal at end
         * LOAD curves does snot
         *
         * CAPTURE_IM records to get the inertia moment but does not calculate curves in R and not updates 
the treeview
@@ -935,6 +935,16 @@ public partial class ChronoJumpWindow
                bool deletedOk = Util.FileDelete(eSQL.GetFullURL(false));       //don't convertPathToR
                if(deletedOk)  {
                        Sqlite.Delete(false, Constants.EncoderTable, Convert.ToInt32(uniqueID));
+                       
+                       ArrayList escArray = SqliteEncoder.SelectSignalCurve(false, 
+                                               -1, Convert.ToInt32(uniqueID)); //signal, curve
+                       SqliteEncoder.DeleteSignalCurveWithCurveID(false, 
+                                       Convert.ToInt32(eSQL.uniqueID)); //delete by curveID on SignalCurve 
table
+                       //if deleted curve is from current signal, uncheck it in encoderCaptureCurves
+                       EncoderSignalCurve esc = (EncoderSignalCurve) escArray[0];
+                       if(esc.signalID == Convert.ToInt32(encoderSignalUniqueID))
+                               encoderCaptureSelectBySavedCurves(esc.msCentral, false);
+
                        updateUserCurvesLabelsAndCombo();
                }
                genericWin.Delete_row_accepted();
@@ -2861,7 +2871,7 @@ Log.WriteLine(str);
        }
        
        void on_combo_encoder_capture_save_curve_changed (object o, EventArgs args) {
-               encoderCaptureSelect(UtilGtk.ComboGetActive(combo_encoder_capture_save_curve));
+               encoderCaptureSelectByCombo(UtilGtk.ComboGetActive(combo_encoder_capture_save_curve));
        }
 
        void on_combo_encoder_analyze_data_compare_changed (object o, EventArgs args)
@@ -4465,6 +4475,26 @@ Log.WriteLine(str);
                                                                Convert.ToDouble(curve.PeakPower)
                                                                ));
                                }
+
+                               //find the saved curves
+                               ArrayList linkedCurves = SqliteEncoder.SelectSignalCurve(false, 
+                                               Convert.ToInt32(encoderSignalUniqueID), -1); //signal, curve
+                               Log.WriteLine("SAVED CURVES FOUND");
+                               foreach(EncoderSignalCurve esc in linkedCurves)
+                                       Log.WriteLine(esc.ToString());
+
+                               foreach (EncoderCurve curve in encoderCaptureCurves) {
+                                       foreach(EncoderSignalCurve esc in linkedCurves) {
+                                               if(Convert.ToDouble(curve.Start) <= esc.msCentral && 
+                                                               Convert.ToDouble(curve.Start) + 
Convert.ToDouble(curve.Duration) >= esc.msCentral)
+                                               {
+                                                       Log.WriteLine(curve.Start + " is saved");
+                                                       encoderCaptureSelectBySavedCurves(esc.msCentral, 
true);
+                                                       break;
+                                               }
+                                       }
+                               }
+
                                plotCurvesGraphDoPlot(mainVariable, mainVariableHigher, mainVariableLower, 
captureCurvesBarsData,
                                                false); //not capturing
                
diff --git a/src/gui/encoderTreeviews.cs b/src/gui/encoderTreeviews.cs
index a3269b0..b5489c0 100644
--- a/src/gui/encoderTreeviews.cs
+++ b/src/gui/encoderTreeviews.cs
@@ -210,7 +210,7 @@ public partial class ChronoJumpWindow
                }
        }
 
-       void encoderCaptureSelect(string toSelect) {
+       void encoderCaptureSelectByCombo(string toSelect) {
                if(toSelect == Catalog.GetString(Constants.Selected))
                        return;
 
@@ -239,6 +239,30 @@ public partial class ChronoJumpWindow
                callPlotCurvesGraphDoPlot();
        }
        
+       //saved curves (when load), or recently deleted curves should modify the encoderCapture treeview
+       void encoderCaptureSelectBySavedCurves(int msCentral, bool selectIt) {
+               TreeIter iter;
+               bool iterOk = encoderCaptureListStore.GetIterFirst(out iter);
+               while(iterOk) {
+                       TreePath path = encoderCaptureListStore.GetPath(iter);
+                       EncoderCurve curve = (EncoderCurve) encoderCaptureListStore.GetValue (iter, 0);
+                       
+                       if(Convert.ToDouble(curve.Start) <= msCentral && 
+                                       Convert.ToDouble(curve.Start) + Convert.ToDouble(curve.Duration) >= 
msCentral) 
+                       {
+                               ((EncoderCurve) encoderCaptureListStore.GetValue (iter, 0)).Record = selectIt;
+                       
+                               //this makes RenderRecord work on changed row without having to put mouse 
there
+                               encoderCaptureListStore.EmitRowChanged(path,iter);
+                       }
+                       
+                       iterOk = encoderCaptureListStore.IterNext (ref iter);
+               }
+               combo_encoder_capture_show_save_curve_button();
+                       
+               callPlotCurvesGraphDoPlot();
+       }
+       
        void combo_encoder_capture_show_save_curve_button () {
                label_encoder_save_curve.Text = "";
 
diff --git a/src/sqlite/encoder.cs b/src/sqlite/encoder.cs
index 6fa7f0c..f6c41e6 100644
--- a/src/sqlite/encoder.cs
+++ b/src/sqlite/encoder.cs
@@ -184,7 +184,7 @@ class SqliteEncoder : Sqlite
                        andString + Constants.EncoderTable + ".exerciseID = " + 
                                Constants.EncoderExerciseTable + ".uniqueID " +
                                onlyActiveString +
-                       " ORDER BY substr(filename,-23,19)"; //this contains the date of capture signal
+                       " ORDER BY substr(filename,-23,19), uniqueID DESC "; //'filename,-23,19' contains the 
date of capture signal
 
                Log.WriteLine(dbcmd.CommandText.ToString());
                
@@ -294,6 +294,9 @@ class SqliteEncoder : Sqlite
                return array;
        }
 
+       /*
+        * EncoderSignalCurve
+        */
        
        protected internal static void createTableEncoderSignalCurve()
        {
@@ -302,20 +305,19 @@ class SqliteEncoder : Sqlite
                        "uniqueID INTEGER PRIMARY KEY, " +
                        "signalID INT, " +
                        "curveID INT, " +
-                       "eccon TEXT, " +        //"c", "ecS", "ceS"
                        "msCentral INT, " +
                        "future1 TEXT )";
                dbcmd.ExecuteNonQuery();
        }
        
-       public static void SignalCurveInsert(bool dbconOpened, int signalID, int curveID, string eccon, int 
msCentral)
+       public static void SignalCurveInsert(bool dbconOpened, int signalID, int curveID, int msCentral)
        {
                if(! dbconOpened)
                        dbcon.Open();
 
                dbcmd.CommandText = "INSERT INTO " + Constants.EncoderSignalCurveTable +  
-                       " (uniqueID, signalID, curveID, eccon, msCentral, future1) " + 
-                       "VALUES (NULL, " + signalID + ", " + curveID + ", '" + eccon + "', " + msCentral + ", 
'')";
+                       " (uniqueID, signalID, curveID, msCentral, future1) " + 
+                       "VALUES (NULL, " + signalID + ", " + curveID + ", " + msCentral + ", '')";
                //Log.WriteLine(dbcmd.CommandText.ToString());
                dbcmd.ExecuteNonQuery();
                
@@ -323,6 +325,71 @@ class SqliteEncoder : Sqlite
                        dbcon.Close();
        }
        
+
+       //signalID == -1 (any signal)
+       //curveID == -1 (any curve)
+       public static ArrayList SelectSignalCurve (bool dbconOpened, int signalID, int curveID)
+       {
+               if(! dbconOpened)
+                       dbcon.Open();
+
+               string signalIDstr = "";
+               if(signalID != -1)
+                       signalIDstr = " signalID == " + signalID;
+               
+               string curveIDstr = "";
+               if(curveID != -1)
+                       curveIDstr = " curveID == " + curveID;
+
+               string whereStr = "";
+               if(signalID != -1 || curveID != -1)
+                       whereStr = " WHERE ";
+               
+               string andStr = "";
+               if(signalID != -1 && curveID != -1)
+                       andStr = " AND ";
+
+               dbcmd.CommandText = 
+                       "SELECT uniqueID, signalID, curveID, msCentral " +
+                       " FROM " + Constants.EncoderSignalCurveTable + 
+                       whereStr + signalIDstr + andStr + curveIDstr;
+               
+               Log.WriteLine(dbcmd.CommandText.ToString());
+               
+               SqliteDataReader reader;
+               reader = dbcmd.ExecuteReader();
+
+               ArrayList array = new ArrayList();
+               while(reader.Read()) {
+                       EncoderSignalCurve esc = new EncoderSignalCurve(
+                                       Convert.ToInt32(reader[0].ToString()),
+                                       Convert.ToInt32(reader[1].ToString()),
+                                       Convert.ToInt32(reader[2].ToString()),
+                                       Convert.ToInt32(reader[3].ToString()));
+                       
+                       array.Add(esc);
+               }
+               reader.Close();
+               if(! dbconOpened)
+                       dbcon.Close();
+
+               return array;
+       }
+
+       public static void DeleteSignalCurveWithCurveID(bool dbconOpened, int curveID)
+       {
+               if( ! dbconOpened)
+                       dbcon.Open();
+
+               dbcmd.CommandText = "Delete FROM " + Constants.EncoderSignalCurveTable +
+                       " WHERE curveID == " + curveID.ToString();
+               Log.WriteLine(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+               
+               if( ! dbconOpened)
+                       dbcon.Close();
+       }
+
        
 
        /*
diff --git a/src/sqlite/main.cs b/src/sqlite/main.cs
index ee50061..66125da 100644
--- a/src/sqlite/main.cs
+++ b/src/sqlite/main.cs
@@ -1507,6 +1507,10 @@ class Sqlite
                                        conversionRate ++;
                                        conversionSubRateTotal = curves.Count;
                                        conversionSubRate = 1;
+                                                       
+                                       //needed to know if there are duplicates
+                                       ArrayList curvesStored = new ArrayList();
+
                                        foreach(EncoderSQL c in curves) {
                                                conversionSubRate ++;
                                                if(s.GetDate(false) == c.GetDate(false) && s.eccon == 
c.eccon) {
@@ -1517,14 +1521,34 @@ class Sqlite
                                                        if(msCentral == -1)
                                                                signalID = -1; //mark as an orphaned curve 
(without signal)
 
-                                                       SqliteEncoder.SignalCurveInsert(true, 
-                                                                       signalID, 
Convert.ToInt32(c.uniqueID), c.eccon, msCentral);
-
-                                                       //duplicates will be found on signal load, 
-                                                       //because there we can see and delete if two curves 
of the same eccon overlap
-                                                       //if they overlap is because:
-                                                       //- they are saved two times (same msCentral), or
-                                                       //- they are saved two times with different smoothing 
(different msCentral)
+                                                       /*
+                                                        * about duplicated curves from 1.05 and before:
+                                                        * There are two kind of duplicates, in both, eccon 
is the same, and they overlap.
+                                                        *
+                                                        * Overlapings situations:
+                                                        * - they are saved two times (same msCentral), or
+                                                        * - they are saved two times with different 
smoothing (different msCentral)
+                                                        *
+                                                        * from now on (1.06) there will be no more duplicates
+                                                        * about the old duplicated curves, is the user 
problem,
+                                                        * except the curves of the first kind, that we know 
exactly that they are duplicated
+                                                        */
+                                                       
+                                                       //curves come sorted by UniqueID DESC
+                                                       //if does not exist: insert in encoderSignalCurve
+                                                       bool exists = false;
+                                                       foreach(int ms in curvesStored)
+                                                               if(ms == msCentral)
+                                                                       exists = true;
+                                                       if(exists) {
+                                                               //delete this (newer will not be deleted)
+                                                               Sqlite.Delete(true, 
+                                                                               Constants.EncoderTable, 
Convert.ToInt32(c.uniqueID));
+                                                       } else {
+                                                               curvesStored.Add(msCentral);
+                                                               SqliteEncoder.SignalCurveInsert(true, 
+                                                                               signalID, 
Convert.ToInt32(c.uniqueID), msCentral);
+                                                       }
                                                }
                                        }
                                }


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