Commit graph

319 commits

Author SHA1 Message Date
Linus Torvalds
f4bf16d5db Fix up SAC calculations for ATM/bar confusion
We even documented that we did SAC in bar*l/min, but the "S" in SAC
stands for "Surface".  So we should normalize SAC rate to surface
pressure, not one bar.

It's a tiny 1% difference, and doesn't actually matter in practice, but
it's noticeable when you want to explicitly test for SAC-rate by
creating a test-dive that averages exactly 10m.  Suddenly you don't get
the round numbers you expect.

[ Side note: 10m is not _exactly_ one extra atmosphere according to our
  calculations, but it's darn close in sea water: the standard salinity
  of 1.03 kg/l together with the standard acceleration of 9.81m/s^2
  gives an additional pressure of 1.01 bar, which is within a fraction
  of a percent of one ATM.

  Of course, divers have likely chosen that value exactly for the math
  to come out that way, since the true average salinity of seawater is
  actually slightly lower ]

So here's a few test-dives, along with the SAC rate fixup to make them
look right.

(There's also a one-liner to dive.c that makes the duration come out
right if the last sample has a non-zero depth, and the previous sample
did not: one of my original test-dives did the "average 10m depth" by
starting at 0 and ending at 20m, and dive.c got a tiny bit confused
about that ;)

[ The rationale for me testing our SAC rate calculations in the first
  place was that on snorkkeli.net user "Poltsi" reported that our SAC rate
  calculations differ from the ones that Suunto DM4 reports. So I wanted
  to verify that we did things right.

  Note that Poltsi reported differences larger than the difference of
  BAR/ATM, so this is not the cause. I'll continue to look at this. ]

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-24 10:49:26 -08:00
Lubomir I. Ivanov
0b4dfd6cff Unref the two GtkTreeStore instances when destroying the divelist
Even if these exists for the heap lifespan, we can call:
g_object_unref(dive_list.treemodel);
g_object_unref(dive_list.listmodel);

in divelist.c:dive_list_destroy()

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-20 17:20:22 -08:00
Lubomir I. Ivanov
9a5818e92c Fixed a memory leak in the divelist when moving with the keyboard
There were some small leaks before here, related to gtk_tree_iter_copy(),
but there is another one in select_next_dive():
nextiter = gtk_tree_iter_copy(iter);

This now requires a SJ near the epilog where we do the memory cleanup.
Lets call this similar label consistently "free_iter" between
select_prev_dive and select_next_dive.

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-20 17:19:50 -08:00
Lubomir I. Ivanov
32761485d1 Indicate to the user a default divelist sorting order
Each time subsurface starts there is supposedly no sorting order
(or divelist column) specified by the UI, yet the actual column is '#'
(or dive number column), since its the *only* column which allows
trips to be visible. If the user selects a different sorting order
then he has no idea which column was the one who had the trips visible.

"Where did all those 'trip' things go?"
This can be a bit confusing...

Lets provide indication by calling gtk_tree_view_column_set_sort_indicator().
Also call gtk_tree_view_column_set_sort_order() to specify a descending
order.

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-20 07:24:59 -08:00
Lubomir I. Ivanov
57e8349b87 Preserve keyboard focus when changing sorting order in the divelist
When the user changes the dive list sorting order via clicking
on different column titles, using gtk_widget_grab_focus() gives
keyboard focus back to the list itself (not staying on the column titles),
which gives a hint that the list itself has focus index of 0 and is
reset each time the widget receives this type of "initial" focus.

Acked-by: Miika Turkia <miika.turkia@gmail.com>
Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-20 07:24:28 -08:00
Dirk Hohndel
b406dbe1c0 Fix tracking of selected dives across sort column changes
With  commit 800b0482f39e ("When switching sort order, scroll the dive
list to the current dive") we introduced an unwanted new behavior. When
changing sort columns we could lose all selections except for the main
"selected_dive". This was caused by the call to gtk_tree_view_set_cursor
on the selected_dive which unselected all other dives.

As a side-effect this also fixed another bug that was introduced by the
same change: shift-cursor-up and -down now works again to select multiple
dives with the keyboard.

But fixing that bug unearthed a different issue. Our code that restored
the selection state of the tree model oddly decided to mark a divetrip as
selected if its first child was selected. The bug above masked that by
immediately unselecting the trip again, but now that this was fixed the
problem was immediately obvious: we would start with both the first trip
and the first dive in that trip selected (well, since we are in reverse
order it's actually the chronologically last trip and last dive...).

Instead we now use the remembered state of the trip to determine whether
it should be expanded or selected.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-19 23:55:24 -08:00
Linus Torvalds
0fd6907965 Obviate the need for explicit 'remember_tree_state/restore_tree_state' calls
Instead, just keep track of the expanded state of trips as we get the
gtk callbacks for the state changes (which we need to track anyway for
the selection logic), and automatically restore the state whenever we
re-create the divelist.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-19 14:00:21 -08:00
Linus Torvalds
aa6b164a3e Remember non-expanded state in 'remember_tree_state()' too
The 'remember_tree_state()' thing is meant to remember if a dive trip is
expanded or not, but it missed the "or not" part.  IOW, it never cleared
the expanded flag, it only ever set it.

As a result, if you were doing multiple operations on the divelist tree
(testing all the recent gtk-model removal, for example) the dive trips
would end up expanding more and more, even if you collapsed things by
hand in between operations.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-19 13:05:48 -08:00
Linus Torvalds
7847de62b1 Rewrite "merge_dive_into_trip_above()" using our own data structures
This does the moving of dives into the trip above without the complexity
of the gtk data structures, and instead just recreates the whole
divelist afterwards.  As usual, this simplifies things a lot, and the
less gtk-specific code we have, the better.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-19 11:50:58 -08:00
Linus Torvalds
e453ea8695 Don't do "insert_trip_before" using the gtk data structures
.. use our own data structures instead, and regenerate the gtk ones
after having successfully created the new trip.

This simplifies the code enormously, and also makes it much more
generic.  You can now create a new trip from any arbitrary set of
selected dives (it used to be that the "multiple selected dives" case
worked, but only for some very specific special cases of selected dives).

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-19 11:24:42 -08:00
Linus Torvalds
a00bfc918a Fix find_trip_by_idx() over-zealous cleanup
Commit bcf1f8c4fe ("Don't do "remove_from_trip" by walking the gtk
data structures") made find_trip_by_idx() only work for negative indexes
(positive indexes are dives), but when it removed the unnecessary test
for negativity, the statement inside it should have been kept as
unconditional, rather than removed with the test.

Oops.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-19 11:24:04 -08:00
Linus Torvalds
bcf1f8c4fe Don't do "remove_from_trip" by walking the gtk data structures
They are complicated and confusing.  Just use our own data structures
and re-generate the gtk ones from them.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-19 10:38:20 -08:00
Linus Torvalds
97bd24246e Don't do "remove_trip()" by walking the gtk data structures
They are complicated and confusing.  Just use our own data structures
and re-generate the gtk ones from them.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-19 10:38:17 -08:00
Linus Torvalds
cb490b7abf Use the divetrip index to look up divelist trip entries
We used to look up dive trips by their date, but these days we always
create a dynamic index for a dive trip when we insert it into the
divelist model, so we can use that to unambiguously match up dive trips
with the dive model entries.

That means that we don't get confused if we have two trips with the
exact same time, which happens when you load all the test-dives, for
example.

Reported-by: Miika Turkia <miika.turkia@gmail.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-19 08:23:27 -08:00
Linus Torvalds
1e04e08064 When only a dive trip is selected, select all the dives in the trip
This does a final pass after all the selection logic, and notices if we
have dive trips that are selected, but that have no dives in them
selected.  In that case, we assume that the user wanted to select all
dives in that trip.

NOTE! This still allows a range selection that selects the dive trip
entry and a few dives under the trip.  If a trip has any dives selected
in it, we leave that manual selection alone.  So this new logic really
only triggers on the case where somebody selected *just* the trip.

Note: unselecting the trip still leaves the dives under it selected,
because having a dive trip that isn't selected have all the dives under
it be selected is normal, and we can't recognize that as some kind of
special event.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-18 16:18:26 -08:00
Dirk Hohndel
2f059c8b81 Replace deprecated gdk_pixbuf_unref with g_object_unref
This appears to be the better API call to do this (according to online
documentation and compiler warnings on Linux).

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-09 11:48:12 -08:00
Lubomir I. Ivanov
ea21126f62 Fixed a memory leak related to the satelite icon
divelist.c:get_gps_icon_for_dive()
In all callers of the function use gdk_pixbuf_unref() to
release the returned GdkPixbuf (but also check for NULL).

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-09 11:40:20 -08:00
Lubomir I. Ivanov
363c38f557 Fixed some memory leaks in divelist.c related to gtk_tree_iter_copy()
divelist.c:
get_iter_from_idx() goes trought the tree model and calls
iter_has_index(), until a match is found. when the match is found
we use gtk_tree_iter_copy() to make a copy of the iterator.

This means that the caller of get_iter_from_idx() has to take care
the de-allocation using gtk_tree_iter_free().

Also take care of the eventual:
parent = gtk_tree_iter_copy(...)
allocation in select_prev_dive(), select_next_dive()

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-09 11:40:03 -08:00
Dirk Hohndel
ebcbe5aedd Finish removing accesses to first divecomputer instead of dive
This adds watertemp and airtemp to the dive, populates them in fixup and
uses them elsewhere in the code.

WARNING: as a sideeffect we now edit the airtemp in the dive, but we never
display this in the DIve Info notebook (as that always displays the data
from the specific selected divecomputer). This is likely to cause
confusion. It's consistent behavior, but... odd. This brings back the
desire to have a view of "best data available" for a dive, in addition to
the "per divecomputer" view. This would also allow us to consolidate the
different pressure graphs we may be getting from different divecomputers
(consider the case where you dive with multiple air integrated computers
that are connected to different tanks - now we could have one profile with
all the correct tank pressure plots overlayed - and the best available (or
edited) data in the corresponding Dive Info notebook.

This commit also fixes a few remaining accesses to the first divecomputer
that fell through the cracks earlier and does a couple of other related
cleanups.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-09 07:41:15 -08:00
Dirk Hohndel
b9865d6bbc Move duration to dive structure and replace accessor function
When starting on this quest to stop using the first divecomputer instead
of data for the whole dive in commit eb73b5a528c8 ("Duration of a dive is
the maximum duration from all divecomputers") I introduced an accessor
function that calculates the dive duration on the fly as the maximum of
the durations in the divecomputers.

Since then Linus and I have added quite a few of the variables back to the
dive data structure and it makes perfect sense to do the same thing for
the duration as well and simply do the calculation once during fixup.

This commit also replaces accesses to the first divecomputer in
likely_same_dive to use the maxdepth and meandepth of the dive (those two
slipped through the cracks in the previous commits, it seems).

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-09 07:19:29 -08:00
Dirk Hohndel
1511271201 Add maxdepth back to the dive structure
Populate during dive fixup as the maximum depth shown by all the
divecomputers. Use this value (instead of the one in the first
divecomputer) in printing, statistics, etc.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-08 20:44:04 -08:00
Linus Torvalds
926fcef2a1 Do more dive fixup for each dive computer
In commit b6c9301e58 ("Move more dive computer filled data to the
divecomputer structure") we moved the fields that get filled in by the
dive computers to be per-divecomputer data structures.

This patch re-creates some of those fields back in the "struct dive",
but now the fields are initialized to be a reasonable average from the
dive computer data.  We already did some of this for the temperature
min/max fields for the statistics, so this just continues that trend.

The goal is to make it easy to look at "dive values" without having to
iterate over dive computers every time you do.  Just do it once in
"fixup_dive()" instead.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-08 18:22:30 -08:00
Dirk Hohndel
61861d2611 Clean up the handling of surface pressure
There are two ways to look at surface pressure. One is to say "what was
the surface pressure during that dive?" - in that case we now return an
average over the pressure reported by the different divecomputers (or the
standard 1013mbar if none reported any).

Or you want to do specific calculations for a specific divecomputer - in
which case we access only the pressure reported by THAT divecomputer, if
present (and fall back to the previous case, otherwise).

We still have lots of places in Subsurface that only act on the first
divecomputer. As a side effect of this change we now make this more
obvious as we in those cases pass a pointer to the first divecomputer
explicitly to the calculations.

Either way, this commit should prevent us from ever mistakenly basing our
calculations on a surface pressure of 0 (which is the initial bug in
deco.c that triggered all this).

Similar changes need to be made for other elements that we currently only
use from the first divecomputer, i.e., salinity.

Reported-by: Robert C. Helling <helling@atdotde.de>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-09 08:01:59 +11:00
Dirk Hohndel
9846ba9e94 Duration of a dive is the maximum duration from all divecomputers
So far we always used the duration of the first divecomputer. The same fix
needs to be done for some of the other calculations that always use the
first divecomputer.

This commit also removes some obsolete code from the webservice merging.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-08 17:48:07 +11:00
Linus Torvalds
b18c34373f Properly de-select dives in collapsed trips that are unselected
We had the logic for the "select" case, but not for the "deselect" case. Ugh.

Reported-by: Dirk Hohndel <dirk@hohndel.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-08 11:46:06 +11:00
Jan Schubert
99dbd667bf Fixing SP handling in planner, adding event
This moves some double/floating handling for po2 to plain integer. There
are still non int values around (also for phe and po2) in the plot area.

Signed-off-by: Jan Schubert <Jan.Schubert@GMX.li>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-03 07:07:12 +11:00
Pierre-Yves Chibon
20d3b5f714 Add 'Save As' entry to context menu shown when right clicking on a dive
Something which is nice especially when asked on the list to share an
interesting dive is the possibility to save just some dives into a file.

This commit adds to the context menu shown with right-click the 'Save As'
entry. This entry allows to save selected dives.

[Dirk Hohndel: clean up white space, commit message and remove unused
               variables]

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-02 00:10:14 +11:00
Dirk Hohndel
cf3c0266c2 Move flag icon to include file
Just like with the satellite icon we are creating a pixdata structure for
the flag.

The Makefile cleanup in commit df6a9ddd8a21 ("Auto-generate C file
dependencies, and make the build more quiet") removed the rules for
generating the .h file by mistake (I hope).

This adds a more generic rule back in and also makes sure that the data
structures get more useful names.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-01 17:25:41 +11:00
Linus Torvalds
7e7cbb0dce When switching sort order, scroll the dive list to the current dive
Now that we actually seem to understand the whole notion of setting the
active dive, let's take that code a bit further, and always scroll to it
when we're introducing a new sort ordering.

Sure, there may be other selected dives, but we have one primary
(current) dive that we show the profile and dive data for, and when we
switch sort order we probably want to see that dive in the dive list.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-02-01 10:51:45 +11:00
Dirk Hohndel
de7d5b2d5b Make sure that the planned dive is selected
With the changes to the selection logic the selected_dive variable didn't
get updated at the end of planning a dive. With an empty dive list that
could cause selected_dive to be -1 which would subsequently cause a
SIGSEGV when trying to edit the newly created dive.

With this commit we use the shared go_to_iter() function and also make
sure that selected_dive is set correctly.

Reported-by: Sergey Starosek <sergey.starosek@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-31 22:31:58 +11:00
Linus Torvalds
b93f2ec6c3 Make the dive selection logic also set the treeview cursor
This fixes "enter" after moving around with the cursor keys.

Hinted-at-by: Carl Worth <cworth@cworth.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-31 17:31:36 +11:00
Dirk Hohndel
635c28923d Better algorithm to merge gps locations & locations names from webservice
This no longer abuses the dive merging code (which would leave stray
"dives" behind if a gps fix couldn't be merged with any of the dives) and
instead parses the gps fixes into a second table and then walks that table
and tries to find matching dives.

The code tries to be reasonably smart about this. If we have
auto-generated GPS fixes at regular intervals, we look for a fix that is
during a dive (that's likely when the boat where the phone is staying dry
is more or less above the diver having fun). And if we have named entries
(so the user typed in a location name) we try to match them in order to
the dives that happened "that day" (where "that day" is about 6h before
and after the timestamp of the gps fix).

This commit also renames dive_has_location() to dive_has_gps_location() as
the difference between if(!dive->location) and if(dives_has_location) is a
bit too subtle...

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-31 14:29:02 +11:00
Henrik Brautaset Aronsen
0fb1fe8c6e Fix segfault pressing Menu key
Pierre wrote: "On my keyboard I have a key on the right side
of the space bar, between the alt+gr key and the right ctrl
which most of the time emulates the right mouse click.
If I press this button on subsurface, I end up with:

   Segmentation fault (core dumped)

This whatever the selection and nicely always reproducible."

This patch doesn't make the key work, but it fixes the segfault.

Reported-by: Pierre-Yves Chibon <pingou@pingoured.fr>
Debugged-and-acked-by: Sergey Starosek <sergey.starosek@gmail.com>
Signed-off-by: Henrik Brautaset Aronsen <subsurface@henrik.synth.no>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-30 12:07:13 +11:00
Dirk Hohndel
e3a8ed5183 Massive cleanup
Mostly coding style and whitespace changes plus making lots of functions
static that have no need to be extern. This also helped find a bit of code
that is actually no longer used.

This should have absolutely no functional impact - all changes should be
purely cosmetic. But it removes a bunch of lines of code and makes the
rest easier to read.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-30 08:31:53 +11:00
Dirk Hohndel
468d3f28f9 Make the selection logic more robust
In commit 304526850c91 ("Don't deselect all dives on all selection
"change" events") the handling of "selected_dive" is incorrect. We ended
up with non-sensical values for the selected dive, including dives that
Gtk didn't think were selected.

This commit tries to be smart about what to do when the dive that we
currently consider selected is unselected (we have this weird notion of
many dives being selected, but one of them is shown in the profile and
that is the "selected_dive"). As long as there are others selected, we
pick one of them (first walking to earlier dives and if there are none
that are selected, looking for a later dive) as the new selected dive.

This appears to give us a rather intuitive behavior when playing with
multiple selected dives.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-30 07:05:30 +11:00
Linus Torvalds
4b5b732f2c Don't deselect all dives on all selection "change" events
gtk sends the selection change events all the time, for pretty much any
"divelist changed - so selection changed".  The expansion of a trip, the
switch to a new model, yadda yadda.  But we actually want selections to
be sticky across these events, so we can't just forget all of our old
selection state and repopulate it.

So we re-introduce the "am I allowed to change this row" callback, which
we used to use to create a list of every actual selection that was
changed.  But instead of remembering the list (and having the stale
entries issue with that remembered list that caused problems), we now
just use that as a "that *particular* selection cleared" event.

So this callback works as the "which part of the visible, currently
selected state got cleared" notifier, and handles unselection.

Then, when the selection is over, we use the new model of "let's just
traverse the list of things gtk thinks are selected" and use that to
handle new selections in the visible state that gtk actually tracks
well.  So that logic handles the new selections.

This way, dives that aren't visible to gtk don't ever get modified: gtk
won't ask about them being selected or not, and gtk won't track them in
its selection logic, so with this model their state never changes for
us.

gtk selections are annoying.  They are simple for the case gtk knows
about (ie they are *visually* selected in the GUI), but since we very
much want to track selection across events that change the visual state,
we need to have this insane "impedance match".

Reported-by: Dirk Hohdnel <dirk@hohndel.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-30 06:47:48 +11:00
Linus Torvalds
14524d8e1d Fix cursor up/down logic
The dive selection rewrite didn't set the selected dive index, breaking
the cursor key logic.

Reported-by: Dirk Hohndel <dirk@hohndel.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-30 06:46:52 +11:00
Linus Torvalds
8d6dd5f87d Rewrite gtk dive selection tracking logic
We used to generate a list of possibly changed selections using the gtk
tree selection "selection function".

But that's actually meant to just tell gtk whether an entry can be
selected or not, and our list of possibly changed entries ended up being
stale if the selection change was due to a list entry removal, for
example.

So rip out the old model entirely, and instead just walk the whole
selection that gtk gives us on a selection "change" event.  We throw all
our old selections away when this happens, and just rebuild it all.

This should fix the occasional internal gtklib-quartz assertion that
Henrik is seeing.  And it actually simplifies the code too.

Reported-by: Henrik Brautaset Aronsen <subsurface@henrik.synth.no>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-28 22:36:28 -08:00
Dirk Hohndel
55b3fd12a8 Make dive planner more useful for closed circuit diving
Add a sample at time 0 to allow for a pO2 from the start of the dive.
Remember the last pO2 so it doesn't have to be repeated (and the right
thing happens for the planned part of the dive).

This still doesn't allow us to change the setpoint at a certain depth
(which would be analogous to being able to switch to a certain gas at a
certain depth in OC plans), but with this commit it's already usable.

This commit also fixes a couple of small bugs in commit b8ee3de870fa
("Dive planning for closed circuit rebreather") where a pO2 of 1.1 was
hardcoded in one place, throwing off all plan calculations and integer
math was used to calculate a floating point value (leading to most pO2
values actually used being 1.0).

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-28 21:39:25 -08:00
Jan Schubert
9df9fc088d Dive planning for closed circuit rebreather
This misses a single issue to be used as a base for further discussion:
The CC setpoint is used for the next segment, not the one specified for. I
also have in mind to modify the existing code to use setpoints specified
in mbar and plain integer instead of float values.

Signed-off-by: Jan Schubert <Jan.Schubert@GMX.li>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-28 18:31:30 -08:00
Dirk Hohndel
332d372b80 Pick GPS coordinates of dive location via map widget
I have some concerns about the way this is implemented - especially the
use of gtk_grab_add to make the map widget work has me worried. But it
seems to work and survived some test cases that I threw at it.

The GtkButton with the Pixmap looks a little off on my screen, but this
way it was easy to implement. Feel free to come up with a better design.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-28 13:07:08 -08:00
Dirk Hohndel
27d51ce1d7 Fix potential uninitialized variable access
There are paths through this function that reach the comparison at the end
of it without trip_a and/or trip_b being initialized.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-25 15:56:34 -08:00
Lubomir I. Ivanov
7bb6108c34 Small fix for a possible crash in divelist.c
divelist.c:copy_tree_node():
pass the pointer "icon" to gtk_tree_store_set()

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-25 07:29:19 -08:00
Linus Torvalds
4982389ca7 Fix setting of the dive_table.preexisting logic
The 'preexisting' value is used for downloading dives: we want to add
new dives but, but then compare those new dives against the
preexisting ones before we start sorting things and possibly merging
them.

However, the value was only updated sporadically, resulting in it
having stale information in it.  Which would cause problems
particularly if you deleted dives, so that the preexisting value would
point past the actual existing values!

So just update it unconditionally in dive_list_update_dives(), which
anything that changes the dive list is supposed to call in order to
display the changes anyway.

Also, just for safety, when removing a dive, put NULL in the last dive
table location.  Nobody should ever access past the end anyway (this
is enforced by 'get_dive()') but there are places that access the dive
list table directly, and the libdivecomputer download was one of
those.  No reason to leave stale dive pointers possibly around for
uses like that.

Reported-by: Henrik Brautaset Aronsen <subsurface@henrik.synth.no>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-23 13:37:02 -08:00
Linus Torvalds
b6c9301e58 Move more dive computer filled data to the divecomputer structure
This moves the fields 'duration', 'surfacetime', 'maxdepth',
'meandepth', 'airtemp', 'watertemp', 'salinity' and 'surface_pressure'
to the per-divecomputer data structure.  They are filled in by the dive
computer, and normally not edited.

NOTE! All actual *use* of this data was then changed from dive->field to
dive->dc.field programmatically with a shell-script and sed, and the
result then edited for details.  So while the XML save and restore code
has been updated, all the displaying etc will currently always just show
the first dive computer entry.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-23 12:55:33 -08:00
Lubomir I. Ivanov
b1db60ba47 divelist.c:icon_click_cb(): check if a GtkTreePath is found
In icon_click_cb() we need to check if a correct GtkTreePath is found
(using gtk_tree_view_get_path_at_pos()) before requesting a GtkTreeIter
for it.

Without this patch a bug is reproducible, where the user may click
outside of the GtkTreeView entries, but still in the GtkTreeView -
e.g. when only one entry is available.

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-23 08:49:10 -08:00
Linus Torvalds
e618a245b9 Use proper helper functions for dive location and for_each_dive
This makes the code use the "dive_has_location()" function rather than
check the longitude and latitude directly.

It also uses "for_each_dive()" rather than open-coding it.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-22 22:26:12 -08:00
Dirk Hohndel
714d5e2443 Fix crash when clicking on icon column in trip header entries
Silly oversight.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-22 20:44:27 -08:00
Dirk Hohndel
713c845e5a Add GPS icon to the location column for dive sites where we have GPS data
This replaces the really lame "italics text" from commit abe810ca1a29
("Mark locations that have GPS location data attached") with a marginally
less lame GPS icon.There's a reason why I am not making a living as
graphics artist. But I think this is a huge step forward from what we had
before...

The satellite.svg file is very loosely based on a different icon that I
found as public domain here http://www.clker.com/clipart-30400.html.
From that I created the PNG and then that was converted into the
GdkPixdata via gdk-pixbuf-csource; a rule for that was added to
the Makefile but commented out as I don't know if this tool will always be
available in the path. Having this icon included in the sources avoids
locating yet another icon file.

Better icons are certainly welcome!

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-22 20:11:13 -08:00
Dirk Hohndel
3d248682ee Mark locations that have GPS location data attached
This is rather lame - we simply turn the location text into italics for those
dives where we have GPS location data. Underlining might be more natural, but
Gtk plays games with the underline attribute if the mouse hovers over text.

Ideally I would have prefered a little GPS logo next to the location text - but
I couldn't figure out how to do that without writing my own cell renderer which
seemed total overkill.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-20 17:32:55 -08:00
Dirk Hohndel
d0d4bbece7 Hack Makefile, gtk-gui.c and divelist.c to allow building w/o osm-gps-map
While we are waiting for an autotools generated Makefile, this should allow
people to build that don't have osm-gps-map.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-17 11:17:48 -08:00
Linus Torvalds
30d090cee2 Only offer to show dive on map if we have location information
This adds the "Show in map" menu entry to the divelist only if we
actually have a location to show.

Of course, having some way to visually see whether we have a GPS
location even before we show the menu would probably be good. Maybe a
marker in the "location" string or something. But in the meanwhile, at
least we don't have that menu entry if we have nothing to show.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-16 13:04:31 -08:00
Henrik Brautaset Aronsen
e9953fa5e6 Show single dives in map.
This adds a "Show in map" entry in the dive list context menu.  It will
zoom to the dive location if it exists, otherwise the full map will be
displayed.

I've also switched map tiles from OpenStreetMap to Google Maps just to
show off that we can.

Signed-off-by: Henrik Brautaset Aronsen <subsurface@henrik.synth.no>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-15 17:41:57 -08:00
Jan Schubert
90d3c5614a Centralising and redefining values as integers
This patch centralizes the definition for surface pressure, oxygen in
air, (re)defines all such values as plain integers and adapts calculations.

It eliminates 11 (!) occurrences of definitions for surface pressure and
also a few for oxygen in air.

It also rewrites the calculation for EAD, END and EADD using the new
definitons, harmonizing it for OC and CC and fixes a bug for EADD OC
calculation.

And finally it removes the unneeded variable entry_ead in gtk-gui.c.

Jan

Signed-off-by: Jan Schubert <Jan.Schubert@GMX.li>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-14 20:12:40 -08:00
Dirk Hohndel
e0b53a5d31 Scroll the divelist to show the planned dive
Just put the code to do so in a function and reuse that function elsewhere
as well.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-13 21:36:50 -08:00
Dirk Hohndel
1ea445102e Correctly show the planned dive in the divelist
Previously we would simply show the first dive in the divelist - which
worked fine in the default sort by trip setting and assuming that there
are no dives from the future in the divelist.

With this commit we actually find the correct dive in the divelist and
select it instead. If you sort by depth you will see the dive move around
in the divelist, but it will stay selected and visible in the profile.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-13 21:01:52 -08:00
Dirk Hohndel
c8a9986451 Make sure init_decompression works for dive 0 as well
The loop would actually get entered for dive 0 and try to compare things
with dive -1. Which of course fails.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-13 21:00:49 -08:00
Dirk Hohndel
b94a93d061 During multi-dive deco calculations don't look at dives from other trips
Previously the code in init_decompression() would mindlessly walk back the
dive_table until it found a gap of at least 48h and take all those dives
into consideration when calculating tissue saturation. This goes horribly
wrong if you load dives from two divers into the same data file.

With this commit things will still turn out correctly, as long as the
dives are in separate trips in for each of the divers. So with this I can
load both Linus' and my divelog and things stay sane even on our shared
dive trips.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-13 19:37:41 -08:00
Linus Torvalds
6a10700ca5 Add default filename and divelist font to prefs structure
.. and add the usual logic to not save the default values.

This also simplifies the initial system-specific setup of both of these:
since we have defaults for all the preferences that get set up at
startup, we can just initialize those defaults to the system-specific
fonts then and there.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-11 17:46:00 -08:00
Linus Torvalds
868a2cc090 Split up preference data structure definition into 'pref.h'
.. and rename the badly named 'output_units/input_units' variables.

We used to have this confusing thing where we had two different units
(input vs output) that *look* like they are mirror images, but in fact
"output_units" was the user units, and "input_units" are the XML parsing
units.

So this renames them to be clearer.  "output_units" is now just "units"
(it's the units a user would ever see), and "input_units" is now
"xml_parsing_units" and set by the XML file parsers to reflect the units
of the parsed file.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-10 20:30:32 -08:00
Linus Torvalds
88313f1077 Clean up duplicated depth interpolation
We have several places where we interpolate the depth based on two
samples and the time between them. Some of them use floating point, some
of them don't, some of them meant to do it but didn't.

Just use a common helper function for it. I seriously doubt the floating
point here really matters, since doing it in integers is not going to
overflow unless we're interpolating between two samples that are hours
apart at hundreds of meters of depth, but hey, it gives that rounding to
the nearest millimeter. Which I'm sure matters.

Anyway, we can probably just get rid of the rounding and the floating
point math, but it won't really hurt either, so at least do it
consistently.

The interpolation could be for other things than just depth, but we
probably don't have anything else we'd want to interpolate. But make the
function naming generic just in case.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-09 16:17:57 -08:00
Robert C. Helling
67d59ff018 Rewrite of the deco code
o) Instead of using gradient factors as means of comparison, I now use
   pressure (as in: maximal ambient pressure).

o) tissue_tolerance_calc() now computes the maximal ambient pressure now
   respecting gradient factors. For this, it needs to know about the
   surface pressure (as refernce for GF_high), thus gets *dive as an
   argument. It is called from add_segment() which this also needs *dive
   as an additional argument.

o) This implies deco_allowed_depth is now mainly a ambient-pressure to
   depth conversion with decorations to avoid negative depth (i.e. no deco
   obliation), implementation of quantization (!smooth => multiples of 3m)
   and explicit setting of last deco depth (e.g. 6m for O2 deco).

o) gf_low_pressure_this_dive (slight change of name), the max depth in
   pressure units is updated in add_segment. I set the minimal value in
   buehlmann_config to the equivalent of 20m as otherwise good values of
   GF_low add a lot of deco to shallow dives which do not need deep stops
   in the first place.

o) The bogus loop is gone as well as actual_gradient_limit() and
   gradient_factor_calculation() and large parts of deco_allowed_depth()
   although I did not delete the code but put it in comments.

o) The meat is in the formula in lines 147-154 of deco.c. Here is the
   rationale:

   Without gradient factors, the M-value (i.e the maximal tissue pressure)
   at a given depth is given by ambient_pressure / buehlmann_b + a.

   According to "Clearing Up The Confusion About "Deep Stops" by Erik C.
   Baker (as found via google) the effect of the gradient factors is no
   replace this by a reduced affine relation (i.e. another line) such that
   at the surface the difference between M-value and ambient pressure is
   reduced by a factor GF_high and at the maximal depth by a factor
   GF_low.

   That is, we are looking for parameters alpha and beta such that

   alpha surface + beta = surface + gf_high * (surface/b + a - surface)

   and

   alpha max_p + beta = max_p + gf_low * (max_p/b + a - max_p)

   This can be solved for alpha and beta and then inverted to obtain the
   max ambient pressure given tissue loadings. The result is the above
   mentioned formula.

Signed-off-by: Robert C. Helling <helling@atdotde.de>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-08 08:59:20 -08:00
Linus Torvalds
c2bc6633ec Improve dive trip sort comparison function
We do want to compare "loose" dives too, but we need to be a bit
careful, and always use the trip date as the primary sort key for any
dives that are not in the same trip.

Reported-and-tested-by: Miika Turkia <miika.turkia@gmail.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-07 14:55:42 -08:00
Dirk Hohndel
be9be189f7 Display dive profile of the dive we plan, as we plan it
As the user enters data into the entry fields, that data is validated and
as soon as there is enough data we start constructing a dive profile,
including the final ascent to the surface, including required deco stops,
etc.

This commit still has some serious issues.

- when data is input that doesn't validate, we just print a warning to
  stdout - instead we need to change the backgroundcolor of the input
  field or something.
- when we switch to the last dive in order to show the profile we don't
  actually search for the last dive - we just show the first one in the
  tree. This works for the default sort order but is of course wrong
  otherwise

I'm sure there are many other bugs, but I want to push it out where it is
right now for others to be able to take a look.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-06 22:15:03 -08:00
Dirk Hohndel
cca847791a First stab at simplistic dive planning
This comes with absolutely no gui - so the plan literally needs to be
compiled into Subsurface. Not exactly a feature, but this allowed me to
focus on the planning part instead of spending time on tedious UI work.

A new menu "Planner" with entry "Test Planner" calls into the hard-coded
function in planner.c. There a simple dive plan can be constructed with
calls to plan_add_segment(&diveplan, duration, depth at the end, fO2, pO2)

Calling plan(&diveplan) does the deco calculations and creates deco stops
that keep us below the ceiling (with the GFlow/high values currently
configured). The stop levels used are defined at the top of planner.c in
the stoplevels array - there is no need to do the traditional multiples of
3m or anything like that.

The dive including the ascents and deco stops all the way to the surface
is completed and then added as simulated dive to the end of the divelist
(I guess we could automatically select it later) and can be viewed.

This is crude but shows the direction we can go with this. Envision a nice
UI that allows you to simply enter the segments and pick the desired
stops.

What is missing is the ability to give the algorithm additional gases that
it can use during the deco phase - right now it simply keeps using the
last gas used in the diveplan.

All that said, there are clear bugs here - and sadly they seem to be in
the deco calculations, as with the example given the ceiling that is
calculated makes no sense. When displayed in smooth mode it has very
strange jumps up and down that I wouldn't expect. For example with GF
35/75 (the default) the deco ceiling when looking at the simulated dive
jumps from 16m back up to 13m around 14:10 into the dive. That seems very
odd.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-04 23:56:55 -08:00
Dirk Hohndel
1b22ac16f2 Clean up DEBUG code
It's still a mess (and the symbols aren't used consistently), but it's a
tiny bit more logical...

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-04 11:56:43 -08:00
Dirk Hohndel
6dc247ff78 Fix deco calculations to correctly use GF values and add CC support
The old implementation was broken in several ways.

For one thing the GF values are percentages, so they should normally be
0 < GF < 1 (well, some crazy people like to go above that).

With this most of the Bühlmann config constants were wrong.

Furthermore, after we adjust the pressure tolerance based on the gradient
factors, we need to convert this back into a depth (instead of passing
back the unmodified depth - oops).

Finally, this commit adds closed circuit support to the deco calculations.

Major progress and much more useful at this stage.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-04 11:13:14 -08:00
Dirk Hohndel
f93f578eca Merge branch 'deco2'
Bringing in the first attempts to do our own deco calculations
2013-01-03 21:58:46 -08:00
Lubomir I. Ivanov
fc882f574f Improvements to select_prev_dive() and select_next_dive()
There were some minor problems when moving the selection
cursor around:

1) If the selection was larger than 1, it was possible
for the selection to get "stuck" in the middle of the list.

This patch approaches this by always calling
gtk_tree_selection_unselect_all() before
gtk_tree_selection_select_iter(), or simply always making
sure we have one selected iterator when navigating with the keys.

2) When there was a single top level dive before the first trip
it wasn't possible to navigate trough the child dives of said
trip in both directions.

The patch attempts to fix this by having the hunks/checks:
if (idx < 0) {

(idx is of a trip) performed regardless of other conditions.

*** Note: testing was done by importing all test*.xml
dives with auto-group on.

[Dirk Hohndel: adjusted the patch to also fix on_key_press to only grab
               the key if no modifier key is pressed; otherwise this
	       breaks shift-cursor-keys for selecting multiple dives.]

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-03 21:52:30 -08:00
Dirk Hohndel
2c33603256 Consider previous dives when calculating deco
This also initializes the N2 tissue saturations to correct numbers
(setting them to zero was clearly silly).

With this commit we walk back in the dive_table until we find a surface
intervall that's longer than 48h. Or a dive that comes after the last one
we looked at; that would indicate that this is a divelist that contains
dives from multiple divers or dives that for other reasons are not
ordered. In a sane environment one would assume that the dives that need
to be taken into account when doing deco calculations are organized as one
trip in the XML file and so this logic should work.

One major downside of the current implementation is that we recalculate
everything whenever the plot_info is recreated - which happens quite
frequently, for example when resizing the window or even when we go into
loup mode. While this isn't all that compute intensive, this is an utter
waste and we should at least cache the saturation inherited from previous
dives (and clear that number when the selected dive changes). We don't
want to cache all of it as the recreation of the plot_info may be
triggered by the user changing equipment (and most importantly, gasmix)
information. In that case the deco data for this dive does indeed have to
be recreated. But without changing the current dive the saturation after
the last surface intervall should stay the same.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-03 20:45:20 -08:00
Salvador Cuñat
8dbe3528b4 Limit to 4 the number of cylinders shown in the data frame
For dives with more than 4 cylinders, the frame got very crowded and we
needed a magnifier to see the numbers.
If we used more than four tanks, let's put the info in another frame, if not, print
the OTUs, the maxcns and the weight sytem in the new frame.
There is still room for two more short data.

Changed naming of nitrox and trimix mixes.

Changed cylinder description.

There are issues with the size of some translations.

Signed-off-by: Salvador Cuñat <salvador.cunat@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-02 13:03:45 -08:00
Linus Torvalds
b900a21165 Fix mixed dive/trip tree model grouping
In commit 96db56f89c ("Allow overlapping (and disjoint) dive trips")
I allowed dives to be part of arbitrary dive trips regardless of date,
which meant that the divelist tree model code needed to find the right
parent for a dive as it was inserted.

That code stupidly assumed that the top level of the dive list tree
containted *only* trips, which is not at all the case. It happens to
be true if you group all your dives into divetrips (the common case
for autogroup=1, which real users do tend to have), but now that Dirk
made the autogrouping be a per-xml-file setting, it became much easier
to trigger the "mixed trips and non-trip dives" case, and that showed
the stupid bug with the test dives.

So instead of just blindly iterating to the 'n'th entry, search for
the actual entry that is the dive trip we want to associate a dive
with.

Reported-by: Lubomir Ivanov <neolit123@gmail.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-02 10:26:30 -08:00
Linus Torvalds
bc16f06536 Keep the selected dive visible in the divelist
This patch makes the divelist behave more as you would expect it as you
scroll up and down through its entries.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-01 17:33:11 -08:00
Dirk Hohndel
91ca0d2cf6 First step towards grabbing keys and handling them ourselves
This commit steals the cursor up and down keys away from gtk so regardless
where gtk thinks the focus may be, we can still use the keys to change
between dives.

In the current UI design where all editing happens in separate windows
this works as expected, as we only grab the keys for the main window. If
we manage to re-enable in-place editing then we need to make sure that
this doesn't cause problems (as gtk uses up/down for the ability to change
drop down selections in combo boxes or values in spin buttons. So we must
make sure that we stop stealing these keys once we start editing something
(in which case simply switching to the next/prev dive wouldn't be a good
thing, anyway).

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-01-01 08:24:42 -08:00
Linus Torvalds
d720e133d8 First step in cleaning up cylinder pressure sensor logic
This clarifies/changes the meaning of our "cylinderindex" entry in our
samples. It has been rather confused, because different dive computers
have done things differently, and the naming really hasn't helped.

There are two totally different - and independent - cylinder "indexes":

 - the pressure sensor index, which indicates which cylinder the sensor
   data is from.

 - the "active cylinder" index, which indicates which cylinder we actually
   breathe from.

These two values really are totally independent, and have nothing
what-so-ever to do with each other. The sensor index may well be fixed:
many dive computers only support a single pressure sensor (whether
wireless or wired), and the sensor index is thus always zero.

Other dive computers may support multiple pressure sensors, and the gas
switch event may - or may not - indicate that the sensor changed too. A
dive computer might give the sensor data for *all* cylinders it can read,
regardless of which one is the one we're actively breathing. In fact, some
dive computers might give sensor data for not just *your* cylinder, but
your buddies.

This patch renames "cylinderindex" in the samples as "sensor", making it
quite clear that it's about which sensor index the pressure data in the
sample is about.

The way we figure out which is the currently active gas is with an
explicit has change event. If a computer (like the Uemis Zurich) joins the
two concepts together, then a sensor change should also create a gas
switch event. This patch also changes the Uemis importer to do that.

Finally, it should be noted that the plot info works totally separately
from the sample data, and is about what we actually *display*, not about
the sample pressures etc. In the plot info, the "cylinderindex" does in
fact mean the currently active cylinder, and while it is initially set to
match the sensor information from the samples, we then walk the gas change
events and fix it up - and if the active cylinder differs from the sensor
cylinder, we clear the sensor data.

[Dirk Hohndel:  this conflicted with some of my recent changes - I think
		I merged things correctly...]

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-30 20:44:47 -08:00
Linus Torvalds
96db56f89c Allow overlapping (and disjoint) dive trips
We used to have the rule that a dive trip has to have all dives in it in
sequential order, even though our XML file really is much more flexible,
and allows arbitrary nesting of dives within a dive trip.

Put another way, the old model had fairly inflexible rules:

 - the dive array is sorted by time

 - a dive trip is always a contiguous slice of this sorted array

which makes perfect sense when you think of the dive and trip list as a
physical activity by one person, but leads to various very subtle issues
in the general case when there are no guarantees that the user then uses
subsurface that way.

In particular, if you load the XML files of two divers that have
overlapping dive trips, the end result is incredibly messy, and does not
conform to the above model at all.

There's two ways to enforce such conformance:

 - disallow that kind of behavior entirely.

   This is actually hard.  Our XML files aren't date-based, they are
   based on XML nesting rules, and even a single XML file can have
   nesting that violates the date ordering.  With multiple XML files,
   it's trivial to do in practice, and while we could just fail at
   loading, the failure would have to be a hard failure that leaves the
   user no way to use the data at all.

 - try to "fix it up" by sorting, splitting, and combining dive trips
   automatically.

   Dirk had a patch to do this, but it really does destroy the actual
   dive data: if you load both mine and Dirk's dive trips, you ended up
   with a result that followed the above two technical rules, but that
   didn't actually make any *sense*.

So this patch doesn't try to enforce the rules, and instead just changes
them to be more generic:

 - the dive array is still sorted by dive time

 - a dive trip is just an arbitrary collection of dives.

The relaxed rules means that mixing dives and dive trips for two people
is trivial, and we can easily handle any XML file.  The dive trip is
defined by the XML nesting level, and is totally independent of any
date-based sorting.

It does require a few things:

 - when we save our dive data, we have to do it hierarchically by dive
   trip, not just by walking the dive array linearly.

 - similarly, when we create the dive tree model, we can't just blindly
   walk the array of dives one by one, we have to look up the correct
   trip (parent)

 - when we try to merge two dives that are adjacent (by date sorting),
   we can't do it if they are in different trips.

but apart from that, nothing else really changes.

NOTE! Despite the new relaxed model, creating totally disjoing dive
trips is not all that easy (nor is there any *reason* for it to be
easty).  Our GUI interfaces still are "add dive to trip above" etc, and
the automatic adding of dives to dive trips is obviously still based on
date.

So this does not really change the expected normal usage, the relaxed
data structure rules just mean that we don't need to worry about the odd
cases as much, because we can just let them be.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-30 19:40:15 -08:00
Linus Torvalds
e862f7c343 Allow sorting dives by date - without the dive trips in the dive list
We used to enable dive trips whenever we sorted by date, which can be
a bit annoying. Sometimes you really just want to sort all your dives
by date, without necessarily seeing the trip data.

So this changes the default sort to be the "dive number" table, and
then does *not* actually sort by the dive number, but instead enables
the trips, and then sorts the result by date. So the "dive number"
column - which used to be non-sortable - becomes semantically
equivalent to the old date column sorting.

And now sorting by date makes it act like sorting by depth or any
other attribute - we hide the dive trip tree, and just show the plain
list of dives (sorted by date, obviously).

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-28 22:35:55 -08:00
Lubomir I. Ivanov
596b13e310 Free trip notes memory when deleting a trip
Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-28 08:59:42 -08:00
Lubomir I. Ivanov
906fd400e7 Small changes in the memory management when dive-merging
This patch makes a couple of modifications:
1) divelist.c:delete_single_dive() now tries to free all memory associated
with a dive, such as the string values for divemaster, location, notes &
etc.

2) dive.c:merge_text(), now always makes a copy in memory for the returned
string - either combined or one of the two which are passed
to the function.

The reason for the above two changes is that when (say) importing the same
data over and over, technically a merge will occur for the contained dives,
but mapped pointers can go out of scope.

main.c:report_dives() calls try_to_merge() and if succeeds the two dives
that were merged are deleted from the table. when we delete a dive,
we now make sure all string data is cleared with it, but also in the actual merge
itself, which precedes, copies of the merged texts are made (with merge_text()),
so that the new, resulted dive has his own text allocations.

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-27 08:08:59 -08:00
Dirk Hohndel
720d4c65e8 Fix crash when trip info changes across remember/restore tree state
Interesting crash. Importing a file gets us to a stage where we have a
trip tree note with a date that doesn't exist as trip date. That's clearly
bogus. And in import_files() we assume that all is still fine and try to
restore the old expanded / selected state for the various trips.

There is clearly a bigger issue here, this patch at least prevents the
actual crash from happening by making sure the pointer is non-NULL before
dereferencing it.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-26 19:44:01 -08:00
Lubomir I. Ivanov
b10ebbda61 Fix some small memory leaks related to gtk_tree_model_get_path()
From the GTK docs on gtk_tree_model_get_path():
"Returns a newly-created GtkTreePath referenced by iter.
This path should be freed with gtk_tree_path_free()."

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-22 20:15:00 -08:00
Dirk Hohndel
eb3e879030 Correctly calculate OTUs when pO2 is recorded in the samples
This will normally happen for CCR dives with a set pO2. The old
calculations was only valid for OC dives.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-20 17:42:10 -08:00
Dirk Hohndel
2f9960f20b Deal with some trip issues when editing dive timestamps
The idea behind editing timestamps had of course been the typical "oops, I
forgot to set my time correctly" which shifts a dive (or a few of them) by
a few hours but keeps the overall order of dives the same).

But reasonable people might argue that they can envision a scenario where
more dramatic changes are being made. And we need to deal with the impact
this has on dive trips.

Here we handle a couple of simple cases:
- this is the only dive in a trip; just update the trip
  (this can still cause problems if the new time is in the middle of an
  existing trip).
- this dives moves before the start of the trip it is in; let's remove it
  from that trip (this response is a bit simplistic - but as I tried to
  say, I don't expect this to be a common use case; and removing it at
  least doesn't lead to entirely unexpected behavior).
- this dive moves past the end of this trip into the range of a different
  trip (in this case we remove the dive from the current trip and allow it
  to interrupt the trip it is moving into).

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-15 21:01:29 -10:00
Dirk Hohndel
91577f11b5 Merge branch 'cns' into cns-merge
I foolishly changed visible_columns in both the (ill-named) cns branch and
master...

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>

Conflicts:
	divelist.c
	gtk-gui.c
	profile.c
2012-12-11 13:09:48 -08:00
Dirk Hohndel
39f80a1e0f Display maximum CNS in the divelist
We either pick the CNS reported by the dive computer at the end of the
dive, or the maximum of that and the CNS values in the samples, if any.

As usual, this column in the dive list defaults to off and it is
controlled by a setting in the tec page of the preferences.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-10 21:18:48 -08:00
Dirk Hohndel
febcbd6325 Add the ability to edit the date/time of a dive
This can cause some fun unintended side effects - especially when the dive
is part of a trip and the new date/time moves this into a different trip.
Instead, trips get split and the overall result is consistent, but a bit
unexpected.

But since this is designed to help people right after a dive import in
case the clock on the dive computer was wrong, my guess is this won't ever
be a problem for actual users.

Fixes ticket 18

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-10 13:23:55 -08:00
Dirk Hohndel
92c0d8c516 Move global variables covered by Preferences into one structure
Now we can simply remember the state of all the preferences at the
beginning of preferences_dialog() and restore them if the user presses
'Cancel'.

Fixes #21

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-10 10:26:06 -08:00
Dirk Hohndel
936d289cb7 Fix insert_trip to correctly deal with trips that start at the same time
The existing code did not move the dives that are part of the second trip
to the first trip (and forgot to keep the 'better' notes as well).

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-06 13:04:37 -08:00
Lubomir I. Ivanov
b435166dc3 Merge trips without modifying the GTK tree
This patch does 4 small divelist.c changes in the following
order of importance:

1) In find_trip_by_time() now there is a check if a trip is actually found
before looking at the "when" flag.

2) Make remember_tree_state() slighly safer. If for example we have recently
deleted a trip from the linked list, it may still exist in the GTK tree model,
thus we want to check when calling find_trip_by_time() if there is an actual
match before setting the "expanded" flag for a trip.

3) When merging two trips in merge_trips_cb(), only use the tree model
to retrieve the timestamps (DIVE_DATE) and then find matching trips with
find_matching_trip(). Once we have pointers to the two trips to be merged,
move dives from one to another iterating with add_dive_to_trip().

4) In merge_trips_cb() - remember the tree state, repopulate the tree and
restore tree state, since now we are not adding/removing rows directly.
tesdsad

Reported-by: Miika Turkia <miika.turkia@gmail.com>
Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-12-04 21:21:28 -08:00
Linus Torvalds
6fe52ee19e Simplify tripflags: remove tripflag_names[]
This removes the tripflag name array, since it's not actually useful.
The only information we ever save in the XML file is whether a dive is
explicitly not supposed to ever be grouped with a trip ("NOTRIP"), and
everything else is implicit.

I'm going to simplify the trip flags further (possibly removing it
entirely - like I did for dive trips already), and don't like having to
maintain the tripflag_names[] array logic.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-26 21:44:51 -07:00
Linus Torvalds
3f197336aa Remove 'tripflag' from dive trips
Both dives and dive trips have the same 'tripflag' thing, but they are
used very differently.  In particular, for dive trips, the only case
that has any meaning is the TF_AUTOGEN case, so instead of having that
trip flag, replace it with a bitfield that says whether the trip was
auto-generated or not.

And make the one-bit bitfields explicitly unsigned.  Signed bitfields
are almost always a mistake, and can be confusing.

Also remove a few now stale macros that are no longer needed now that we
don't do the GList thing for dive list handling, and our autogen logic
has been simplified.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-26 21:44:04 -07:00
Linus Torvalds
ffa3fd551c Clarify (and fix) dive trip auto-generation
This makes the dive trip auto-generation a separate pass from the
showing of the dive trips, which makes things much more understandable.
It simplifies the code a lot too, because it's much more natural to
generate the automatic trip data by walking the dives from oldest to
newest (while the tree model wants to walk the other way).

It gets rid of the most annoying part of using the gtk tree model for
dive trip management, but some still remains.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-26 09:30:27 -08:00
Linus Torvalds
85f0749c85 Remove 'when_from_file' field from dive trip
It had become a write-only field (apart from some now useless debugging)
when simplifying the remove_autogen_trips() function.

So remove it.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-26 09:28:34 -08:00
Linus Torvalds
544a8b9d77 Rewrite 'remove_autogen_trips()' without the gtk tree model walking
I'm trying to remove (or at least simplify) the gtk tree model usage for
our trip handling, but I'm doing it in small chunks.  The goal is to
just do all our trip handling logic explicitly using our own data
structures, and use the gtk tree model purely for showing the end
result.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-26 09:27:54 -08:00
Linus Torvalds
ba3e6bcc51 Associate each dive trip with the dives in that trip
We already kept a count of dives per trip in order to figure out when
there are no more dives left and the trip needs to be freed.  Now we
explicitly keep track of the list of dives associated with the trip too,
which simplifies the "find the time of the trip" logic.

We may want to sort it in time, but for now this is mainly about trying
to keep track of the divetrip relationships explicitly.  I want to move
away from the whole "use the gtk tree model to keep track of things"
approach.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-26 09:27:24 -08:00
Linus Torvalds
25b4fee655 Move events and samples into a 'struct divecomputer'
For now we only have one fixed divecomputer associated with each dive,
so this doesn't really change any current semantics.  But it will make
it easier for us to associate a dive with multiple dive computers.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-23 19:36:27 -08:00
Linus Torvalds
a9786564c2 Allocate dive samples separately from 'struct dive'
We used to avoid some extra allocations by just allocating the dive
samples as part of the 'struct dive' allocation itself, but that ends up
complicating things, and will make it impossible to have multiple
different sets of samples (for multiple dive computers).

So stop doing it. Just allocate the dive samples array separately.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-23 19:33:39 -08:00
Dirk Hohndel
d872a5c8aa Make sure there are no invisible selected dives after import / download
Prior to this commit, gtk often decided to collapse the trip with the
selected dive after the user imported or downloaded additional dives.
Since Subsurface tracks dives as being selected even after gtk collapses a
trip (which clears all selection state as far as gtk is concerned) this
could lead to the strange situation that the user could click on a new
dive to select it without unselecting the already selected dive - and
suddenly edit or delete did things that were entirely unwanted.

With this change we explicitly save and then restore the tree state around
import and download operations. This ensures that the same dive(s) stay
selected and trips stay expanded and therefore avoids the issues described
here.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-21 13:53:36 -08:00
Linus Torvalds
d166e34fed Fix silly thinko in recent changes to "Delete dive" code
Commit 38c79d149d ("Simplify and clean up dive trip management")
simplified the code a bit *too* much, and removed the check for
"dive->selected".

As a result, trying to delete a dive resulted in *all* dives being
deleted.

Oops.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-19 12:02:09 -08:00
Linus Torvalds
b304562fa8 Be stricter about when we allow merging of dives
If the surface interval between two dives is more than half an hour,
don't try to call it a single dive.  Just the dive profile will be
looking ridiculous.

Things like tank refills etc could also be a good thing to check (again,
the dive profile would look ridiculous), but the cylinder pressure going
up a small amount is actually normal (ie cylinder warming up in warmer
water on the surface).  So I don't know what the proper limit for that
would be.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2012-11-11 15:09:00 +01:00