summaryrefslogtreecommitdiffstats
path: root/core/divelist.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-07-19 22:35:25 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-10-11 16:22:27 -0700
commit12df9faaa2037b5155ebb84a7f6f6102491a0091 (patch)
tree33dcf0e400f669d088f1b20652e9c0c6f69aae2b /core/divelist.c
parent61467ea0d59b04f141a68452ee16c70760421d72 (diff)
downloadsubsurface-12df9faaa2037b5155ebb84a7f6f6102491a0091.tar.gz
Undo: implement undo of manual dive-creation
Play manual addition of dives via an UndoCommand. Since this does in large parts the same thing as undo/redo of dive deletion (just the other way round and only a single instead of multiple dive), factor out the functions that add/delete dives and take care of trips. The UI-interaction is just mindless copy&paste and will have to be adapted. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/divelist.c')
-rw-r--r--core/divelist.c44
1 files changed, 33 insertions, 11 deletions
diff --git a/core/divelist.c b/core/divelist.c
index 07c9ea87d..a25927e90 100644
--- a/core/divelist.c
+++ b/core/divelist.c
@@ -987,25 +987,29 @@ struct dive **grow_dive_table(struct dive_table *table)
return dives;
}
+/* get the index where we want to insert the dive so that everything stays
+ * ordered reverse-chronologically */
+int dive_get_insertion_index(struct dive *dive)
+{
+ for (int i = 0; i < dive_table.nr; i++) {
+ if (dive->when <= dive_table.dives[i]->when)
+ return i;
+ }
+ return dive_table.nr;
+}
+
+/* add a dive at the given index. if the index is negative, the dive will
+ * be added according to reverse chronological order */
void add_single_dive(int idx, struct dive *dive)
{
int i;
+ if (idx < 0)
+ idx = dive_get_insertion_index(dive);
grow_dive_table(&dive_table);
dive_table.nr++;
if (dive->selected)
amount_selected++;
- if (idx < 0) {
- // convert an idx of -1 so we do insert-in-chronological-order
- idx = dive_table.nr - 1;
- for (int i = 0; i < dive_table.nr - 1; i++) {
- if (dive->when <= dive_table.dives[i]->when) {
- idx = i;
- break;
- }
- }
- }
-
for (i = idx; i < dive_table.nr; i++) {
struct dive *tmp = dive_table.dives[i];
dive_table.dives[i] = dive;
@@ -1453,6 +1457,24 @@ void process_imported_dives(struct dive_table *import_table, bool prefer_importe
mark_divelist_changed(true);
}
+/* return the number a dive gets when inserted at the given index.
+ * this function is supposed to be called *before* a dive was added.
+ * this returns:
+ * - 1 for an empty log
+ * - last_nr+1 for addition at end of log (if last dive had a number)
+ * - 0 for all other cases
+ */
+int get_dive_nr_at_idx(int idx)
+{
+ if (dive_table.nr == 0)
+ return 1;
+ if (idx >= dive_table.nr) {
+ struct dive *last_dive = get_dive(dive_table.nr - 1);
+ return last_dive->number ? last_dive->number + 1 : 0;
+ }
+ return 0;
+}
+
void set_dive_nr_for_current_dive()
{
if (dive_table.nr == 1)