mirror of
				https://github.com/subsurface/subsurface.git
				synced 2025-02-19 22:16:15 +00:00 
			
		
		
		
	Refactoring: move undo commands to top level
In the future we might want to use undo-commands for mobile as well (even if not implementing undo). Therefore, move the undo-command source from desktop-widgets to their own commands top-level folder. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
		
							parent
							
								
									685b92b9c2
								
							
						
					
					
						commit
						5e29245e68
					
				
					 34 changed files with 45 additions and 31 deletions
				
			
		
							
								
								
									
										26
									
								
								commands/CMakeLists.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								commands/CMakeLists.txt
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,26 @@ | |||
| # the undo-commands that perform undoable actions. | ||||
| 
 | ||||
| # models used both mobile and desktop builds | ||||
| set(SUBSURFACE_GENERIC_COMMANDS_SRCS | ||||
| 	command_base.cpp | ||||
| 	command_base.h | ||||
| 	command.cpp | ||||
| 	command.h | ||||
| 	command_divelist.cpp | ||||
| 	command_divelist.h | ||||
| 	command_divesite.cpp | ||||
| 	command_divesite.h | ||||
| 	command_edit.cpp | ||||
| 	command_edit.h | ||||
| 	command_edit_trip.cpp | ||||
| 	command_edit_trip.h | ||||
| 	command_private.cpp | ||||
| 	command_private.h | ||||
| ) | ||||
| if (SUBSURFACE_TARGET_EXECUTABLE MATCHES "DesktopExecutable") | ||||
| 	add_library(subsurface_commands_desktop STATIC ${SUBSURFACE_GENERIC_COMMANDS_SRCS}) | ||||
| 	target_link_libraries(subsurface_commands_desktop ${QT_LIBRARIES}) | ||||
| #elseif (SUBSURFACE_TARGET_EXECUTABLE MATCHES "MobileExecutable") | ||||
| #	add_library(subsurface_commands_mobile STATIC ${SUBSURFACE_GENERIC_COMMANDS_SRCS}) | ||||
| #	target_link_libraries(subsurface_commands_mobile ${QT_LIBRARIES}) | ||||
| endif() | ||||
							
								
								
									
										257
									
								
								commands/command.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										257
									
								
								commands/command.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,257 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| 
 | ||||
| #include "command.h" | ||||
| #include "command_divelist.h" | ||||
| #include "command_divesite.h" | ||||
| #include "command_edit.h" | ||||
| #include "command_edit_trip.h" | ||||
| 
 | ||||
| namespace Command { | ||||
| 
 | ||||
| // Dive-list related commands
 | ||||
| void addDive(dive *d, bool autogroup, bool newNumber) | ||||
| { | ||||
| 	execute(new AddDive(d, autogroup, newNumber)); | ||||
| } | ||||
| 
 | ||||
| void importDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, int flags, const QString &source) | ||||
| { | ||||
| 	execute(new ImportDives(dives, trips, sites, flags, source)); | ||||
| } | ||||
| 
 | ||||
| void deleteDive(const QVector<struct dive*> &divesToDelete) | ||||
| { | ||||
| 	execute(new DeleteDive(divesToDelete)); | ||||
| } | ||||
| 
 | ||||
| void shiftTime(const QVector<dive *> &changedDives, int amount) | ||||
| { | ||||
| 	execute(new ShiftTime(changedDives, amount)); | ||||
| } | ||||
| 
 | ||||
| void renumberDives(const QVector<QPair<dive *, int>> &divesToRenumber) | ||||
| { | ||||
| 	execute(new RenumberDives(divesToRenumber)); | ||||
| } | ||||
| 
 | ||||
| void removeDivesFromTrip(const QVector<dive *> &divesToRemove) | ||||
| { | ||||
| 	execute(new RemoveDivesFromTrip(divesToRemove)); | ||||
| } | ||||
| 
 | ||||
| void removeAutogenTrips() | ||||
| { | ||||
| 	execute(new RemoveAutogenTrips); | ||||
| } | ||||
| 
 | ||||
| void addDivesToTrip(const QVector<dive *> &divesToAddIn, dive_trip *trip) | ||||
| { | ||||
| 	execute(new AddDivesToTrip(divesToAddIn, trip)); | ||||
| } | ||||
| 
 | ||||
| void createTrip(const QVector<dive *> &divesToAddIn) | ||||
| { | ||||
| 	execute(new CreateTrip(divesToAddIn)); | ||||
| } | ||||
| 
 | ||||
| void autogroupDives() | ||||
| { | ||||
| 	execute(new AutogroupDives); | ||||
| } | ||||
| 
 | ||||
| void mergeTrips(dive_trip *trip1, dive_trip *trip2) | ||||
| { | ||||
| 	execute(new MergeTrips(trip1, trip2)); | ||||
| } | ||||
| 
 | ||||
| void splitDives(dive *d, duration_t time) | ||||
| { | ||||
| 	execute(new SplitDives(d, time)); | ||||
| } | ||||
| 
 | ||||
| void splitDiveComputer(dive *d, int dc_num) | ||||
| { | ||||
| 	execute(new SplitDiveComputer(d, dc_num)); | ||||
| } | ||||
| 
 | ||||
| void moveDiveComputerToFront(dive *d, int dc_num) | ||||
| { | ||||
| 	execute(new MoveDiveComputerToFront(d, dc_num)); | ||||
| } | ||||
| 
 | ||||
| void deleteDiveComputer(dive *d, int dc_num) | ||||
| { | ||||
| 	execute(new DeleteDiveComputer(d, dc_num)); | ||||
| } | ||||
| 
 | ||||
| void mergeDives(const QVector <dive *> &dives) | ||||
| { | ||||
| 	execute(new MergeDives(dives)); | ||||
| } | ||||
| 
 | ||||
| // Dive site related commands
 | ||||
| void deleteDiveSites(const QVector <dive_site *> &sites) | ||||
| { | ||||
| 	execute(new DeleteDiveSites(sites)); | ||||
| } | ||||
| 
 | ||||
| void editDiveSiteName(dive_site *ds, const QString &value) | ||||
| { | ||||
| 	execute(new EditDiveSiteName(ds, value)); | ||||
| } | ||||
| 
 | ||||
| void editDiveSiteDescription(dive_site *ds, const QString &value) | ||||
| { | ||||
| 	execute(new EditDiveSiteDescription(ds, value)); | ||||
| } | ||||
| 
 | ||||
| void editDiveSiteNotes(dive_site *ds, const QString &value) | ||||
| { | ||||
| 	execute(new EditDiveSiteNotes(ds, value)); | ||||
| } | ||||
| 
 | ||||
| void editDiveSiteCountry(dive_site *ds, const QString &value) | ||||
| { | ||||
| 	execute(new EditDiveSiteCountry(ds, value)); | ||||
| } | ||||
| 
 | ||||
| void editDiveSiteLocation(dive_site *ds, location_t value) | ||||
| { | ||||
| 	execute(new EditDiveSiteLocation(ds, value)); | ||||
| } | ||||
| 
 | ||||
| void editDiveSiteTaxonomy(dive_site *ds, taxonomy_data &value) | ||||
| { | ||||
| 	execute(new EditDiveSiteTaxonomy(ds, value)); | ||||
| } | ||||
| 
 | ||||
| void addDiveSite(const QString &name) | ||||
| { | ||||
| 	execute(new AddDiveSite(name)); | ||||
| } | ||||
| 
 | ||||
| void importDiveSites(struct dive_site_table *sites, const QString &source) | ||||
| { | ||||
| 	execute(new ImportDiveSites(sites, source)); | ||||
| } | ||||
| 
 | ||||
| void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites) | ||||
| { | ||||
| 	execute(new MergeDiveSites(ds, sites)); | ||||
| } | ||||
| 
 | ||||
| void purgeUnusedDiveSites() | ||||
| { | ||||
| 	execute(new PurgeUnusedDiveSites); | ||||
| } | ||||
| 
 | ||||
| // Execute an edit-command and return number of edited dives
 | ||||
| static int execute_edit(EditDivesBase *cmd) | ||||
| { | ||||
| 	int count = cmd->numDives(); | ||||
| 	return execute(cmd) ? count : 0; | ||||
| } | ||||
| 
 | ||||
| // Dive editing related commands
 | ||||
| int editNotes(const QString &newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditNotes(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editMode(int index, int newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditMode(index, newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editNumber(int newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditNumber(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editSuit(const QString &newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditSuit(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editRating(int newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditRating(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editVisibility(int newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditVisibility(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editAirTemp(int newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditAirTemp(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editWaterTemp(int newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditWaterTemp(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editAtmPress(int newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditAtmPress(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editDepth(int newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditDepth(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editDuration(int newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditDuration(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editDiveSite(struct dive_site *newValue, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditDiveSite(newValue, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editDiveSiteNew(const QString &newName, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditDiveSiteNew(newName, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editTags(const QStringList &newList, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditTags(newList, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editBuddies(const QStringList &newList, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditBuddies(newList, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| int editDiveMaster(const QStringList &newList, bool currentDiveOnly) | ||||
| { | ||||
| 	return execute_edit(new EditDiveMaster(newList, currentDiveOnly)); | ||||
| } | ||||
| 
 | ||||
| void pasteDives(const dive *d, dive_components what) | ||||
| { | ||||
| 	execute(new PasteDives(d, what)); | ||||
| } | ||||
| 
 | ||||
| void replanDive(dive *d) | ||||
| { | ||||
| 	execute(new ReplanDive(d)); | ||||
| } | ||||
| 
 | ||||
| // Trip editing related commands
 | ||||
| void editTripLocation(dive_trip *trip, const QString &s) | ||||
| { | ||||
| 	execute(new EditTripLocation(trip, s)); | ||||
| } | ||||
| 
 | ||||
| void editTripNotes(dive_trip *trip, const QString &s) | ||||
| { | ||||
| 	execute(new EditTripNotes(trip, s)); | ||||
| } | ||||
| 
 | ||||
| } // namespace Command
 | ||||
							
								
								
									
										87
									
								
								commands/command.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								commands/command.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,87 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| #ifndef COMMAND_H | ||||
| #define COMMAND_H | ||||
| 
 | ||||
| #include "core/dive.h" | ||||
| #include <QVector> | ||||
| #include <QAction> | ||||
| 
 | ||||
| // We put everything in a namespace, so that we can shorten names without polluting the global namespace
 | ||||
| namespace Command { | ||||
| 
 | ||||
| // 1) General commands
 | ||||
| 
 | ||||
| void init();				// Setup signals to inform frontend of clean status.
 | ||||
| void clear();				// Reset the undo stack. Delete all commands.
 | ||||
| void setClean();			// Call after save - this marks a state where no changes need to be saved.
 | ||||
| bool isClean();				// Any changes need to be saved?
 | ||||
| QAction *undoAction(QObject *parent);	// Create an undo action.
 | ||||
| QAction *redoAction(QObject *parent);	// Create an redo action.
 | ||||
| 
 | ||||
| // 2) Dive-list related commands
 | ||||
| 
 | ||||
| // If d->dive_trip is null and autogroup is true, dives within the auto-group
 | ||||
| // distance are added to a trip. dive d is consumed (the structure is reset)!
 | ||||
| // If newNumber is true, the dive is assigned a new number, depending on the
 | ||||
| // insertion position.
 | ||||
| void addDive(dive *d, const bool autogroup, bool newNumber); | ||||
| void importDives(struct dive_table *dives, struct trip_table *trips, | ||||
| 		 struct dive_site_table *sites, int flags, const QString &source); // The tables are consumed!
 | ||||
| void deleteDive(const QVector<struct dive*> &divesToDelete); | ||||
| void shiftTime(const QVector<dive *> &changedDives, int amount); | ||||
| void renumberDives(const QVector<QPair<dive *, int>> &divesToRenumber); | ||||
| void removeDivesFromTrip(const QVector<dive *> &divesToRemove); | ||||
| void removeAutogenTrips(); | ||||
| void addDivesToTrip(const QVector<dive *> &divesToAddIn, dive_trip *trip); | ||||
| void createTrip(const QVector<dive *> &divesToAddIn); | ||||
| void autogroupDives(); | ||||
| void mergeTrips(dive_trip *trip1, dive_trip *trip2); | ||||
| void splitDives(dive *d, duration_t time); | ||||
| void splitDiveComputer(dive *d, int dc_num); | ||||
| void moveDiveComputerToFront(dive *d, int dc_num); | ||||
| void deleteDiveComputer(dive *d, int dc_num); | ||||
| void mergeDives(const QVector <dive *> &dives); | ||||
| 
 | ||||
| // 3) Dive-site related commands
 | ||||
| 
 | ||||
| void deleteDiveSites(const QVector <dive_site *> &sites); | ||||
| void editDiveSiteName(dive_site *ds, const QString &value); | ||||
| void editDiveSiteDescription(dive_site *ds, const QString &value); | ||||
| void editDiveSiteNotes(dive_site *ds, const QString &value); | ||||
| void editDiveSiteCountry(dive_site *ds, const QString &value); | ||||
| void editDiveSiteLocation(dive_site *ds, location_t value); | ||||
| void editDiveSiteTaxonomy(dive_site *ds, taxonomy_data &value); // value is consumed (i.e. will be erased after call)!
 | ||||
| void addDiveSite(const QString &name); | ||||
| void importDiveSites(struct dive_site_table *sites, const QString &source); | ||||
| void mergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites); | ||||
| void purgeUnusedDiveSites(); | ||||
| 
 | ||||
| // 4) Dive editing related commands
 | ||||
| 
 | ||||
| int editNotes(const QString &newValue, bool currentDiveOnly); | ||||
| int editSuit(const QString &newValue, bool currentDiveOnly); | ||||
| int editMode(int index, int newValue, bool currentDiveOnly); | ||||
| int editNumber(int newValue, bool currentDiveOnly); | ||||
| int editRating(int newValue, bool currentDiveOnly); | ||||
| int editVisibility(int newValue, bool currentDiveOnly); | ||||
| int editAirTemp(int newValue, bool currentDiveOnly); | ||||
| int editWaterTemp(int newValue, bool currentDiveOnly); | ||||
| int editAtmPress(int newValue, bool currentDiveOnly); | ||||
| int editDepth(int newValue, bool currentDiveOnly); | ||||
| int editDuration(int newValue, bool currentDiveOnly); | ||||
| int editDiveSite(struct dive_site *newValue, bool currentDiveOnly); | ||||
| int editDiveSiteNew(const QString &newName, bool currentDiveOnly); | ||||
| int editTags(const QStringList &newList, bool currentDiveOnly); | ||||
| int editBuddies(const QStringList &newList, bool currentDiveOnly); | ||||
| int editDiveMaster(const QStringList &newList, bool currentDiveOnly); | ||||
| void pasteDives(const dive *d, dive_components what); | ||||
| void replanDive(dive *d); // dive computer(s) and cylinder(s) will be reset!
 | ||||
| 
 | ||||
| // 5) Trip editing commands
 | ||||
| 
 | ||||
| void editTripLocation(dive_trip *trip, const QString &s); | ||||
| void editTripNotes(dive_trip *trip, const QString &s); | ||||
| 
 | ||||
| } // namespace Command
 | ||||
| 
 | ||||
| #endif // COMMAND_H
 | ||||
							
								
								
									
										56
									
								
								commands/command_base.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								commands/command_base.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,56 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| 
 | ||||
| #include "command_base.h" | ||||
| #include "core/qthelper.h" // for updateWindowTitle()
 | ||||
| #include "core/subsurface-qt/DiveListNotifier.h" | ||||
| 
 | ||||
| namespace Command { | ||||
| 
 | ||||
| static QUndoStack undoStack; | ||||
| 
 | ||||
| // General commands
 | ||||
| void init() | ||||
| { | ||||
| 	QObject::connect(&undoStack, &QUndoStack::cleanChanged, &updateWindowTitle); | ||||
| } | ||||
| 
 | ||||
| void clear() | ||||
| { | ||||
| 	undoStack.clear(); | ||||
| } | ||||
| 
 | ||||
| void setClean() | ||||
| { | ||||
| 	undoStack.setClean(); | ||||
| } | ||||
| 
 | ||||
| bool isClean() | ||||
| { | ||||
| 	return undoStack.isClean(); | ||||
| } | ||||
| 
 | ||||
| QAction *undoAction(QObject *parent) | ||||
| { | ||||
| 	return undoStack.createUndoAction(parent, QCoreApplication::translate("Command", "&Undo")); | ||||
| } | ||||
| 
 | ||||
| QAction *redoAction(QObject *parent) | ||||
| { | ||||
| 	return undoStack.createRedoAction(parent, QCoreApplication::translate("Command", "&Redo")); | ||||
| } | ||||
| 
 | ||||
| bool execute(Base *cmd) | ||||
| { | ||||
| 	if (cmd->workToBeDone()) { | ||||
| 		undoStack.push(cmd); | ||||
| 		emit diveListNotifier.commandExecuted(); | ||||
| 		return true; | ||||
| 	} else { | ||||
| 		delete cmd; | ||||
| 		return false; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| } // namespace Command
 | ||||
| 
 | ||||
| 
 | ||||
							
								
								
									
										178
									
								
								commands/command_base.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										178
									
								
								commands/command_base.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,178 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| // Note: this header file is used by the undo-machinery and should not be included elsewhere.
 | ||||
| 
 | ||||
| #ifndef COMMAND_BASE_H | ||||
| #define COMMAND_BASE_H | ||||
| 
 | ||||
| #include "core/divesite.h" | ||||
| #include "core/trip.h" | ||||
| 
 | ||||
| #include <QUndoCommand> | ||||
| #include <QCoreApplication>	// For Q_DECLARE_TR_FUNCTIONS | ||||
| #include <memory> | ||||
| 
 | ||||
| // The classes derived from Command::Base represent units-of-work, which can be exectuted / undone
 | ||||
| // repeatedly. The command objects are collected in a linear list implemented in the QUndoStack class.
 | ||||
| // They contain the information that is necessary to either perform or undo the unit-of-work.
 | ||||
| // The usage is:
 | ||||
| //	constructor: generate information that is needed for executing the unit-of-work
 | ||||
| //	redo(): performs the unit-of-work and generates the information that is needed for undo()
 | ||||
| //	undo(): undos the unit-of-work and regenerates the initial information needed in redo()
 | ||||
| // The needed information is mostly kept in pointers to dives and/or trips, which have to be added
 | ||||
| // or removed.
 | ||||
| // For this to work it is crucial that
 | ||||
| // 1) Pointers to dives and trips remain valid as long as referencing command-objects exist.
 | ||||
| // 2) The dive-table is not resorted, because dives are inserted at given indices.
 | ||||
| //
 | ||||
| // Thus, if a command deletes a dive or a trip, the actual object must not be deleted. Instead,
 | ||||
| // the command object removes pointers to the dive/trip object from the backend and takes ownership.
 | ||||
| // To reverse such a deletion, the object is re-injected into the backend and ownership is given up.
 | ||||
| // Once ownership of a dive is taken, any reference to it was removed from the backend. Thus,
 | ||||
| // subsequent redo()/undo() actions cannot access this object and integrity of the data is ensured.
 | ||||
| //
 | ||||
| // As an example, consider the following course of events: Dive 1 is renumbered and deleted, dive 2
 | ||||
| // is added and renumbered. The undo list looks like this (---> non-owning, ***> owning pointers,
 | ||||
| // ===> next item in list)
 | ||||
| //
 | ||||
| //                                        Undo-List
 | ||||
| // +-----------------+     +---------------+     +------------+     +-----------------+
 | ||||
| // | Renumber dive 1 |====>| Delete dive 1 |====>| Add dive 2 |====>| Renumber dive 2 |
 | ||||
| // +------------------     +---------------+     +------------+     +-----------------+
 | ||||
| //          |                      *                   |                     |
 | ||||
| //          |      +--------+      *                   |    +--------+       |
 | ||||
| //          +----->| Dive 1 |<******                   +--->| Dive 2 |<------+
 | ||||
| //                 +--------+                               +--------+
 | ||||
| //                                                              ^
 | ||||
| //                                    +---------+               *
 | ||||
| //                                    | Backend |****************
 | ||||
| //                                    +---------+
 | ||||
| // Two points of note:
 | ||||
| // 1) Every dive is owned by either the backend or exactly one command object.
 | ||||
| // 2) All references to dive 1 are *before* the owner "delete dive 2", thus the pointer is always valid.
 | ||||
| // 3) References by the backend are *always* owning.
 | ||||
| //
 | ||||
| // The user undos the last two commands. The situation now looks like this:
 | ||||
| //
 | ||||
| //
 | ||||
| //                  Undo-List                                Redo-List
 | ||||
| // +-----------------+     +---------------+     +------------+     +-----------------+
 | ||||
| // | Renumber dive 1 |====>| Delete dive 1 |     | Add dive 2 |<====| Renumber dive 2 |
 | ||||
| // +------------------     +---------------+     +------------+     +-----------------+
 | ||||
| //          |                      *                   *                     |
 | ||||
| //          |      +--------+      *                   *    +--------+       |
 | ||||
| //          +----->| Dive 1 |<******                   ****>| Dive 2 |<------+
 | ||||
| //                 +--------+                               +--------+
 | ||||
| //
 | ||||
| //                                    +---------+
 | ||||
| //                                    | Backend |
 | ||||
| //                                    +---------+
 | ||||
| // Again:
 | ||||
| // 1) Every dive is owned by either the backend (here none) or exactly one command object.
 | ||||
| // 2) All references to dive 1 are *before* the owner "delete dive 1", thus the pointer is always valid.
 | ||||
| // 3) All references to dive 2 are *after* the owner "add dive 2", thus the pointer is always valid.
 | ||||
| //
 | ||||
| // The user undos one more command:
 | ||||
| //
 | ||||
| //       Undo-List                                 Redo-List
 | ||||
| // +-----------------+     +---------------+     +------------+     +-----------------+
 | ||||
| // | Renumber dive 1 |     | Delete dive 1 |<====| Add dive 2 |<====| Renumber dive 2 |
 | ||||
| // +------------------     +---------------+     +------------+     +-----------------+
 | ||||
| //          |                      |                   *                     |
 | ||||
| //          |      +--------+      |                   *    +--------+       |
 | ||||
| //          +----->| Dive 1 |<-----+                   ****>| Dive 2 |<------+
 | ||||
| //                 +--------+                               +--------+
 | ||||
| //                     ^
 | ||||
| //                     *              +---------+
 | ||||
| //                     ***************| Backend |
 | ||||
| //                                    +---------+
 | ||||
| // Same points as above.
 | ||||
| // The user now adds a dive 3. The redo list will be deleted:
 | ||||
| //
 | ||||
| //                                      Undo-List
 | ||||
| // +-----------------+                                              +------------+
 | ||||
| // | Renumber dive 1 |=============================================>| Add dive 3 |
 | ||||
| // +------------------                                              +------------+
 | ||||
| //          |                                                             |
 | ||||
| //          |      +--------+                               +--------+    |
 | ||||
| //          +----->| Dive 1 |                               | Dive 3 |<---+
 | ||||
| //                 +--------+                               +--------+
 | ||||
| //                     ^                                        ^
 | ||||
| //                     *              +---------+               *
 | ||||
| //                     ***************| Backend |****************
 | ||||
| //                                    +---------+
 | ||||
| // Note:
 | ||||
| // 1) Dive 2 was deleted with the "add dive 2" command, because that was the owner.
 | ||||
| // 2) Dive 1 was not deleted, because it is owned by the backend.
 | ||||
| //
 | ||||
| // To take ownership of dives/trips, the OnwingDivePtr and OwningTripPtr types are used. These
 | ||||
| // are simply derived from std::unique_ptr and therefore use well-established semantics.
 | ||||
| // Expressed in C-terms: std::unique_ptr<T> is exactly the same as T* with the following
 | ||||
| // twists:
 | ||||
| // 1) default-initialized to NULL.
 | ||||
| // 2) if it goes out of scope (local scope or containing object destroyed), it does:
 | ||||
| //	if (ptr) free_function(ptr);
 | ||||
| //    whereby free_function can be configured (defaults to delete ptr).
 | ||||
| // 3) assignment between two std::unique_ptr<T> compiles only if the source is reset (to NULL).
 | ||||
| //    (hence the name - there's a *unique* owner).
 | ||||
| // While this sounds trivial, experience shows that this distinctly simplifies memory-management
 | ||||
| // (it's not necessary to manually delete all vector items in the destructur, etc).
 | ||||
| // Note that Qt's own implementation (QScoperPointer) is not up to the job, because it doesn't implement
 | ||||
| // move-semantics and Qt's containers are incompatible, owing to COW semantics.
 | ||||
| //
 | ||||
| // Usage:
 | ||||
| //	OwningDivePtr dPtr;			// Initialize to null-state: not owning any dive.
 | ||||
| //	OwningDivePtr dPtr(dive);		// Take ownership of dive (which is of type struct dive *).
 | ||||
| //						// If dPtr goes out of scope, the dive will be freed with free_dive().
 | ||||
| //	struct dive *d = dPtr.release();	// Give up ownership of dive. dPtr is reset to null.
 | ||||
| //	struct dive *d = d.get();		// Get pointer dive, but don't release ownership.
 | ||||
| //	dPtr.reset(dive2);			// Delete currently owned dive with free_dive() and get ownership of dive2.
 | ||||
| //	dPtr.reset();				// Delete currently owned dive and reset to null.
 | ||||
| //	dPtr2 = dPtr1;				// Fails to compile.
 | ||||
| //	dPtr2 = std::move(dPtr1);		// dPtr2 takes ownership, dPtr1 is reset to null.
 | ||||
| //	OwningDivePtr fun();
 | ||||
| //	dPtr1 = fun();				// Compiles. Simply put: the compiler knows that the result of fun() will
 | ||||
| //						// be trashed and therefore can be moved-from.
 | ||||
| //	std::vector<OwningDivePtr> v:		// Define an empty vector of owning pointers.
 | ||||
| //	v.emplace_back(dive);			// Take ownership of dive and add at end of vector
 | ||||
| //						// If the vector goes out of scope, all dives will be freed with free_dive().
 | ||||
| //	v.clear(v);				// Reset the vector to zero length. If the elements weren't release()d,
 | ||||
| //						// the pointed-to dives are freed with free_dive()
 | ||||
| 
 | ||||
| // We put everything in a namespace, so that we can shorten names without polluting the global namespace
 | ||||
| namespace Command { | ||||
| 
 | ||||
| // Classes used to automatically call free_dive()/free_trip for owning pointers that go out of scope.
 | ||||
| struct DiveDeleter { | ||||
| 	void operator()(dive *d) { free_dive(d); } | ||||
| }; | ||||
| struct TripDeleter { | ||||
| 	void operator()(dive_trip *t) { free_trip(t); } | ||||
| }; | ||||
| struct DiveSiteDeleter { | ||||
| 	void operator()(dive_site *ds) { free_dive_site(ds); } | ||||
| }; | ||||
| 
 | ||||
| // Owning pointers to dive and dive_trip objects.
 | ||||
| typedef std::unique_ptr<dive, DiveDeleter> OwningDivePtr; | ||||
| typedef std::unique_ptr<dive_trip, TripDeleter> OwningTripPtr; | ||||
| typedef std::unique_ptr<dive_site, DiveSiteDeleter> OwningDiveSitePtr; | ||||
| 
 | ||||
| // This is the base class of all commands.
 | ||||
| // It defines the Qt-translation functions
 | ||||
| class Base : public QUndoCommand { | ||||
| 	Q_DECLARE_TR_FUNCTIONS(Command) | ||||
| public: | ||||
| 	// Check whether work is to be done.
 | ||||
| 	// TODO: replace by setObsolete (>Qt5.9)
 | ||||
| 	virtual bool workToBeDone() = 0; | ||||
| }; | ||||
| 
 | ||||
| // Put a command on the undoStack (and take ownership), but test whether there
 | ||||
| // is something to be done beforehand by calling the workToBeDone() function.
 | ||||
| // If nothing is to be done, the command will be deleted and false is returned.
 | ||||
| bool execute(Base *cmd); | ||||
| 
 | ||||
| } // namespace Command
 | ||||
| 
 | ||||
| #endif // COMMAND_BASE_H
 | ||||
| 
 | ||||
							
								
								
									
										999
									
								
								commands/command_divelist.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										999
									
								
								commands/command_divelist.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,999 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| 
 | ||||
| #include "command_divelist.h" | ||||
| #include "command_private.h" | ||||
| #include "core/divelist.h" | ||||
| #include "core/display.h" // for amount_selected
 | ||||
| #include "core/qthelper.h" | ||||
| #include "core/subsurface-qt/DiveListNotifier.h" | ||||
| #include "qt-models/filtermodels.h" | ||||
| #include "../profile-widget/profilewidget2.h" | ||||
| 
 | ||||
| #include <array> | ||||
| 
 | ||||
| namespace Command { | ||||
| 
 | ||||
| // This helper function removes a dive, takes ownership of the dive and adds it to a DiveToAdd structure.
 | ||||
| // If the trip the dive belongs to becomes empty, it is removed and added to the tripsToAdd vector.
 | ||||
| // It is crucial that dives are added in reverse order of deletion, so that the indices are correctly
 | ||||
| // set and that the trips are added before they are used!
 | ||||
| DiveToAdd DiveListBase::removeDive(struct dive *d, std::vector<OwningTripPtr> &tripsToAdd) | ||||
| { | ||||
| 	// If the dive was the current dive, reset the current dive. The calling
 | ||||
| 	// command is responsible of finding a new dive.
 | ||||
| 	if (d == current_dive) | ||||
| 		current_dive = nullptr; | ||||
| 
 | ||||
| 	// remove dive from trip and site - if this is the last dive in the trip
 | ||||
| 	// remove the whole trip.
 | ||||
| 	DiveToAdd res; | ||||
| 	res.trip = unregister_dive_from_trip(d); | ||||
| 	if (d->dive_site) | ||||
| 		diveSiteCountChanged(d->dive_site); | ||||
| 	res.site = unregister_dive_from_dive_site(d); | ||||
| 	if (res.trip && res.trip->dives.nr == 0) { | ||||
| 		remove_trip(res.trip, &trip_table);	// Remove trip from backend
 | ||||
| 		tripsToAdd.emplace_back(res.trip);	// Take ownership of trip
 | ||||
| 	} | ||||
| 
 | ||||
| 	int idx = get_divenr(d); | ||||
| 	if (idx < 0) | ||||
| 		qWarning("Deletion of unknown dive!"); | ||||
| 
 | ||||
| 	res.dive.reset(unregister_dive(idx));		// Remove dive from backend
 | ||||
| 
 | ||||
| 	return res; | ||||
| } | ||||
| 
 | ||||
| void DiveListBase::diveSiteCountChanged(struct dive_site *ds) | ||||
| { | ||||
| 	if (std::find(sitesCountChanged.begin(), sitesCountChanged.end(), ds) == sitesCountChanged.end()) | ||||
| 		sitesCountChanged.push_back(ds); | ||||
| } | ||||
| 
 | ||||
| // This helper function adds a dive and returns ownership to the backend. It may also add a dive trip.
 | ||||
| // It is crucial that dives are added in reverse order of deletion (see comment above)!
 | ||||
| // Returns pointer to added dive (which is owned by the backend!)
 | ||||
| dive *DiveListBase::addDive(DiveToAdd &d) | ||||
| { | ||||
| 	if (d.trip) | ||||
| 		add_dive_to_trip(d.dive.get(), d.trip); | ||||
| 	if (d.site) { | ||||
| 		add_dive_to_dive_site(d.dive.get(), d.site); | ||||
| 		diveSiteCountChanged(d.site); | ||||
| 	} | ||||
| 	dive *res = d.dive.release();		// Give up ownership of dive
 | ||||
| 
 | ||||
| 	// Set the filter flag according to current filter settings
 | ||||
| 	bool show = MultiFilterSortModel::instance()->showDive(res); | ||||
| 	res->hidden_by_filter = !show; | ||||
| 
 | ||||
| 	int idx = dive_table_get_insertion_index(&dive_table, res); | ||||
| 	add_to_dive_table(&dive_table, idx, res);	// Return ownership to backend
 | ||||
| 	invalidate_dive_cache(res);		// Ensure that dive is written in git_save()
 | ||||
| 
 | ||||
| 	return res; | ||||
| } | ||||
| 
 | ||||
| // Some signals are sent in batches per trip. To avoid writing the same loop
 | ||||
| // twice, this template takes a vector of trip / dive pairs, sorts it
 | ||||
| // by trip and then calls a function-object with trip and a QVector of dives in that trip.
 | ||||
| // The dives are sorted by the dive_less_than() function defined in the core.
 | ||||
| // Input parameters:
 | ||||
| //	- dives: a vector of trip,dive pairs, which will be sorted and processed in batches by trip.
 | ||||
| //	- action: a function object, taking a trip-pointer and a QVector of dives, which will be called for each batch.
 | ||||
| template<typename Function> | ||||
| void processByTrip(std::vector<std::pair<dive_trip *, dive *>> &dives, Function action) | ||||
| { | ||||
| 	// Sort lexicographically by trip then according to the dive_less_than() function.
 | ||||
| 	std::sort(dives.begin(), dives.end(), | ||||
| 		  [](const std::pair<dive_trip *, dive *> &e1, const std::pair<dive_trip *, dive *> &e2) | ||||
| 		  { return e1.first == e2.first ? dive_less_than(e1.second, e2.second) : e1.first < e2.first; }); | ||||
| 
 | ||||
| 	// Then, process the dives in batches by trip
 | ||||
| 	size_t i, j; // Begin and end of batch
 | ||||
| 	for (i = 0; i < dives.size(); i = j) { | ||||
| 		dive_trip *trip = dives[i].first; | ||||
| 		for (j = i + 1; j < dives.size() && dives[j].first == trip; ++j) | ||||
| 			; // pass
 | ||||
| 		// Copy dives into a QVector. Some sort of "range_view" would be ideal, but Qt doesn't work this way.
 | ||||
| 		QVector<dive *> divesInTrip(j - i); | ||||
| 		for (size_t k = i; k < j; ++k) | ||||
| 			divesInTrip[k - i] = dives[k].second; | ||||
| 
 | ||||
| 		// Finally, emit the signal
 | ||||
| 		action(trip, divesInTrip); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // This helper function calls removeDive() on a list of dives to be removed and
 | ||||
| // returns a vector of corresponding DiveToAdd objects, which can later be readded.
 | ||||
| // Moreover, a vector of deleted trips is returned, if trips became empty.
 | ||||
| // The passed in vector is cleared.
 | ||||
| DivesAndTripsToAdd DiveListBase::removeDives(DivesAndSitesToRemove &divesAndSitesToDelete) | ||||
| { | ||||
| 	std::vector<DiveToAdd> divesToAdd; | ||||
| 	std::vector<OwningTripPtr> tripsToAdd; | ||||
| 	std::vector<OwningDiveSitePtr> sitesToAdd; | ||||
| 	divesToAdd.reserve(divesAndSitesToDelete.dives.size()); | ||||
| 	sitesToAdd.reserve(divesAndSitesToDelete.sites.size()); | ||||
| 
 | ||||
| 	// Make sure that the dive list is sorted. The added dives will be sent in a signal
 | ||||
| 	// and the recipients assume that the dives are sorted the same way as they are
 | ||||
| 	// in the core list.
 | ||||
| 	std::sort(divesAndSitesToDelete.dives.begin(), divesAndSitesToDelete.dives.end(), dive_less_than); | ||||
| 
 | ||||
| 	for (dive *d: divesAndSitesToDelete.dives) | ||||
| 		divesToAdd.push_back(removeDive(d, tripsToAdd)); | ||||
| 	divesAndSitesToDelete.dives.clear(); | ||||
| 
 | ||||
| 	for (dive_site *ds: divesAndSitesToDelete.sites) { | ||||
| 		int idx = unregister_dive_site(ds); | ||||
| 		sitesToAdd.emplace_back(ds); | ||||
| 		emit diveListNotifier.diveSiteDeleted(ds, idx); | ||||
| 	} | ||||
| 	divesAndSitesToDelete.sites.clear(); | ||||
| 
 | ||||
| 	// We send one dives-deleted signal per trip (see comments in DiveListNotifier.h).
 | ||||
| 	// Therefore, collect all dives in an array and sort by trip.
 | ||||
| 	std::vector<std::pair<dive_trip *, dive *>> dives; | ||||
| 	dives.reserve(divesToAdd.size()); | ||||
| 	for (const DiveToAdd &entry: divesToAdd) | ||||
| 		dives.push_back({ entry.trip, entry.dive.get() }); | ||||
| 
 | ||||
| 	// Send signals.
 | ||||
| 	processByTrip(dives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { | ||||
| 		// Check if this trip is supposed to be deleted, by checking if it was marked as "add it".
 | ||||
| 		bool deleteTrip = trip && | ||||
| 				  std::find_if(tripsToAdd.begin(), tripsToAdd.end(), [trip](const OwningTripPtr &ptr) | ||||
| 					       { return ptr.get() == trip; }) != tripsToAdd.end(); | ||||
| 		emit diveListNotifier.divesDeleted(trip, deleteTrip, divesInTrip); | ||||
| 	}); | ||||
| 	return { std::move(divesToAdd), std::move(tripsToAdd), std::move(sitesToAdd) }; | ||||
| } | ||||
| 
 | ||||
| // This helper function is the counterpart fo removeDives(): it calls addDive() on a list
 | ||||
| // of dives to be (re)added and returns a vector of the added dives. It does this in reverse
 | ||||
| // order, so that trips are created appropriately and indexing is correct.
 | ||||
| // The passed in vector is cleared.
 | ||||
| DivesAndSitesToRemove DiveListBase::addDives(DivesAndTripsToAdd &toAdd) | ||||
| { | ||||
| 	std::vector<dive *> res; | ||||
| 	std::vector<dive_site *> sites; | ||||
| 	std::vector<std::pair<dive_trip *, dive *>> dives; | ||||
| 	res.resize(toAdd.dives.size()); | ||||
| 	sites.reserve(toAdd.sites.size()); | ||||
| 	dives.reserve(toAdd.sites.size()); | ||||
| 
 | ||||
| 	// Make sure that the dive list is sorted. The added dives will be sent in a signal
 | ||||
| 	// and the recipients assume that the dives are sorted the same way as they are
 | ||||
| 	// in the core list.
 | ||||
| 	std::sort(toAdd.dives.begin(), toAdd.dives.end(), | ||||
| 		  [](const DiveToAdd &d, const DiveToAdd &d2) | ||||
| 		  { return dive_less_than(d.dive.get(), d2.dive.get()); }); | ||||
| 
 | ||||
| 	// Now, add the dives
 | ||||
| 	// Note: the idiomatic STL-way would be std::transform, but let's use a loop since
 | ||||
| 	// that is closer to classical C-style.
 | ||||
| 	auto it2 = res.rbegin(); | ||||
| 	for (auto it = toAdd.dives.rbegin(); it != toAdd.dives.rend(); ++it, ++it2) { | ||||
| 		*it2 = addDive(*it); | ||||
| 		dives.push_back({ (*it2)->divetrip, *it2 }); | ||||
| 	} | ||||
| 	toAdd.dives.clear(); | ||||
| 
 | ||||
| 	// If the dives belong to new trips, add these as well.
 | ||||
| 	// Remember the pointers so that we can later check if a trip was newly added
 | ||||
| 	std::vector<dive_trip *> addedTrips; | ||||
| 	addedTrips.reserve(toAdd.trips.size()); | ||||
| 	for (OwningTripPtr &trip: toAdd.trips) { | ||||
| 		addedTrips.push_back(trip.get()); | ||||
| 		insert_trip(trip.release(), &trip_table); // Return ownership to backend
 | ||||
| 	} | ||||
| 	toAdd.trips.clear(); | ||||
| 
 | ||||
| 	// Finally, add any necessary dive sites
 | ||||
| 	for (OwningDiveSitePtr &ds: toAdd.sites) { | ||||
| 		sites.push_back(ds.get()); | ||||
| 		int idx = register_dive_site(ds.release()); // Return ownership to backend
 | ||||
| 		emit diveListNotifier.diveSiteAdded(sites.back(), idx); | ||||
| 	} | ||||
| 	toAdd.sites.clear(); | ||||
| 
 | ||||
| 	// Send signals by trip.
 | ||||
| 	processByTrip(dives, [&](dive_trip *trip, const QVector<dive *> &divesInTrip) { | ||||
| 		// Now, let's check if this trip is supposed to be created, by checking if it was marked as "add it".
 | ||||
| 		bool createTrip = trip && std::find(addedTrips.begin(), addedTrips.end(), trip) != addedTrips.end(); | ||||
| 		// Finally, emit the signal
 | ||||
| 		emit diveListNotifier.divesAdded(trip, createTrip, divesInTrip); | ||||
| 	}); | ||||
| 	return { res, sites }; | ||||
| } | ||||
| 
 | ||||
| // This helper function renumbers dives according to an array of id/number pairs.
 | ||||
| // The old numbers are stored in the array, thus calling this function twice has no effect.
 | ||||
| static void renumberDives(QVector<QPair<dive *, int>> &divesToRenumber) | ||||
| { | ||||
| 	QVector<dive *> dives; | ||||
| 	dives.reserve(divesToRenumber.size()); | ||||
| 	for (auto &pair: divesToRenumber) { | ||||
| 		dive *d = pair.first; | ||||
| 		if (!d) | ||||
| 			continue; | ||||
| 		std::swap(d->number, pair.second); | ||||
| 		dives.push_back(d); | ||||
| 		invalidate_dive_cache(d); | ||||
| 	} | ||||
| 
 | ||||
| 	// Send signals.
 | ||||
| 	emit diveListNotifier.divesChanged(dives, DiveField::NR); | ||||
| } | ||||
| 
 | ||||
| // This helper function moves a dive to a trip. The old trip is recorded in the
 | ||||
| // passed-in structure. This means that calling the function twice on the same
 | ||||
| // object is a no-op concerning the dive. If the old trip was deleted from the
 | ||||
| // core, an owning pointer to the removed trip is returned, otherwise a null pointer.
 | ||||
| static OwningTripPtr moveDiveToTrip(DiveToTrip &diveToTrip) | ||||
| { | ||||
| 	// Firstly, check if we move to the same trip and bail if this is a no-op.
 | ||||
| 	if (diveToTrip.trip == diveToTrip.dive->divetrip) | ||||
| 		return {}; | ||||
| 
 | ||||
| 	// Remove from old trip
 | ||||
| 	OwningTripPtr res; | ||||
| 
 | ||||
| 	// Remove dive from trip - if this is the last dive in the trip, remove the whole trip.
 | ||||
| 	dive_trip *trip = unregister_dive_from_trip(diveToTrip.dive); | ||||
| 	if (trip && trip->dives.nr == 0) { | ||||
| 		remove_trip(trip, &trip_table);	// Remove trip from backend
 | ||||
| 		res.reset(trip); | ||||
| 	} | ||||
| 
 | ||||
| 	// Store old trip and get new trip we should associate this dive with
 | ||||
| 	std::swap(trip, diveToTrip.trip); | ||||
| 	add_dive_to_trip(diveToTrip.dive, trip); | ||||
| 	invalidate_dive_cache(diveToTrip.dive);		// Ensure that dive is written in git_save()
 | ||||
| 	return res; | ||||
| } | ||||
| 
 | ||||
| // This helper function moves a set of dives between trips using the
 | ||||
| // moveDiveToTrip function. Before doing so, it adds the necessary trips to
 | ||||
| // the core. Trips that are removed from the core because they are empty
 | ||||
| // are recorded in the passed in struct. The vectors of trips and dives
 | ||||
| // are reversed. Thus, calling the function twice on the same object is
 | ||||
| // a no-op.
 | ||||
| static void moveDivesBetweenTrips(DivesToTrip &dives) | ||||
| { | ||||
| 	// We collect an array of created trips so that we can instruct
 | ||||
| 	// the model to create a new entry
 | ||||
| 	std::vector<dive_trip *> createdTrips; | ||||
| 	createdTrips.reserve(dives.tripsToAdd.size()); | ||||
| 
 | ||||
| 	// First, bring back the trip(s)
 | ||||
| 	for (OwningTripPtr &trip: dives.tripsToAdd) { | ||||
| 		dive_trip *t = trip.release();	// Give up ownership
 | ||||
| 		createdTrips.push_back(t); | ||||
| 		insert_trip(t, &trip_table);	// Return ownership to backend
 | ||||
| 	} | ||||
| 	dives.tripsToAdd.clear(); | ||||
| 
 | ||||
| 	for (DiveToTrip &dive: dives.divesToMove) { | ||||
| 		OwningTripPtr tripToAdd = moveDiveToTrip(dive); | ||||
| 		// register trips that we'll have to readd
 | ||||
| 		if (tripToAdd) | ||||
| 			dives.tripsToAdd.push_back(std::move(tripToAdd)); | ||||
| 	} | ||||
| 
 | ||||
| 	// We send one signal per from-trip/to-trip pair.
 | ||||
| 	// First, collect all dives in a struct and sort by from-trip/to-trip.
 | ||||
| 	struct DiveMoved { | ||||
| 		dive_trip *from; | ||||
| 		dive_trip *to; | ||||
| 		dive *d; | ||||
| 	}; | ||||
| 	std::vector<DiveMoved> divesMoved; | ||||
| 	divesMoved.reserve(dives.divesToMove.size()); | ||||
| 	for (const DiveToTrip &entry: dives.divesToMove) | ||||
| 		divesMoved.push_back({ entry.trip, entry.dive->divetrip, entry.dive }); | ||||
| 
 | ||||
| 	// Sort lexicographically by from-trip, to-trip and by start-time.
 | ||||
| 	// Use std::tie() for lexicographical sorting.
 | ||||
| 	std::sort(divesMoved.begin(), divesMoved.end(), [] ( const DiveMoved &d1, const DiveMoved &d2) | ||||
| 		  { return std::tie(d1.from, d1.to, d1.d->when) < std::tie(d2.from, d2.to, d2.d->when); }); | ||||
| 
 | ||||
| 	// Now, process the dives in batches by trip
 | ||||
| 	// TODO: this is a bit different from the cases above, so we don't use the processByTrip template,
 | ||||
| 	// but repeat the loop here. We might think about generalizing the template, if more of such
 | ||||
| 	// "special cases" appear.
 | ||||
| 	size_t i, j; // Begin and end of batch
 | ||||
| 	for (i = 0; i < divesMoved.size(); i = j) { | ||||
| 		dive_trip *from = divesMoved[i].from; | ||||
| 		dive_trip *to = divesMoved[i].to; | ||||
| 		for (j = i + 1; j < divesMoved.size() && divesMoved[j].from == from && divesMoved[j].to == to; ++j) | ||||
| 			; // pass
 | ||||
| 		// Copy dives into a QVector. Some sort of "range_view" would be ideal, but Qt doesn't work this way.
 | ||||
| 		QVector<dive *> divesInTrip(j - i); | ||||
| 		for (size_t k = i; k < j; ++k) | ||||
| 			divesInTrip[k - i] = divesMoved[k].d; | ||||
| 
 | ||||
| 		// Check if the from-trip was deleted: If yes, it was recorded in the tripsToAdd structure.
 | ||||
| 		// Only set the flag if this is that last time this trip is featured.
 | ||||
| 		bool deleteFrom = from && | ||||
| 				  std::find_if(divesMoved.begin() + j, divesMoved.end(), // Is this the last occurence of "from"?
 | ||||
| 					       [from](const DiveMoved &entry) { return entry.from == from; }) == divesMoved.end() && | ||||
| 				  std::find_if(dives.tripsToAdd.begin(), dives.tripsToAdd.end(), // Is "from" in tripsToAdd?
 | ||||
| 					       [from](const OwningTripPtr &trip) { return trip.get() == from; }) != dives.tripsToAdd.end(); | ||||
| 		// Check if the to-trip has to be created. For this purpose, we saved an array of trips to be created.
 | ||||
| 		bool createTo = false; | ||||
| 		if (to) { | ||||
| 			// Check if the element is there...
 | ||||
| 			auto it = std::find(createdTrips.begin(), createdTrips.end(), to); | ||||
| 
 | ||||
| 			// ...if it is - remove it as we don't want the model to create the trip twice!
 | ||||
| 			if (it != createdTrips.end()) { | ||||
| 				createTo = true; | ||||
| 				// erase/remove would be more performant, but this is irrelevant in the big scheme of things.
 | ||||
| 				createdTrips.erase(it); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		// Finally, emit the signal
 | ||||
| 		emit diveListNotifier.divesMovedBetweenTrips(from, to, deleteFrom, createTo, divesInTrip); | ||||
| 	} | ||||
| 
 | ||||
| 	// Reverse the tripsToAdd and the divesToAdd, so that on undo/redo the operations
 | ||||
| 	// will be performed in reverse order.
 | ||||
| 	std::reverse(dives.tripsToAdd.begin(), dives.tripsToAdd.end()); | ||||
| 	std::reverse(dives.divesToMove.begin(), dives.divesToMove.end()); | ||||
| } | ||||
| 
 | ||||
| void DiveListBase::initWork() | ||||
| { | ||||
| } | ||||
| 
 | ||||
| void DiveListBase::finishWork() | ||||
| { | ||||
| 	for (dive_site *ds: sitesCountChanged) | ||||
| 		emit diveListNotifier.diveSiteDiveCountChanged(ds); | ||||
| } | ||||
| 
 | ||||
| void DiveListBase::undo() | ||||
| { | ||||
| 	auto marker = diveListNotifier.enterCommand(); | ||||
| 	initWork(); | ||||
| 	undoit(); | ||||
| 	finishWork(); | ||||
| } | ||||
| 
 | ||||
| void DiveListBase::redo() | ||||
| { | ||||
| 	auto marker = diveListNotifier.enterCommand(); | ||||
| 	initWork(); | ||||
| 	redoit(); | ||||
| 	finishWork(); | ||||
| } | ||||
| 
 | ||||
| AddDive::AddDive(dive *d, bool autogroup, bool newNumber) | ||||
| { | ||||
| 	setText(tr("add dive")); | ||||
| 	// By convention, d is a pointer to "displayed dive" or a temporary variable and can be overwritten.
 | ||||
| 	d->maxdepth.mm = 0; | ||||
| 	d->dc.maxdepth.mm = 0; | ||||
| 	fixup_dive(d); | ||||
| 
 | ||||
| 	// this only matters if undoit were called before redoit
 | ||||
| 	currentDive = nullptr; | ||||
| 
 | ||||
| 	// Get an owning pointer to a moved dive.
 | ||||
| 	OwningDivePtr divePtr(move_dive(d)); | ||||
| 	divePtr->selected = false; // If we clone a planned dive, it might have been selected.
 | ||||
| 				   // We have to clear the flag, as selections will be managed
 | ||||
| 				   // on dive-addition.
 | ||||
| 
 | ||||
| 	// If we alloc a new-trip for autogrouping, get an owning pointer to it.
 | ||||
| 	OwningTripPtr allocTrip; | ||||
| 	dive_trip *trip = divePtr->divetrip; | ||||
| 	dive_site *site = divePtr->dive_site; | ||||
| 	// We have to delete the pointers to trip and site, because this would prevent the core from adding to the
 | ||||
| 	// trip or site and we would get the count-of-dives in the trip or site wrong. Yes, that's all horribly subtle!
 | ||||
| 	divePtr->divetrip = nullptr; | ||||
| 	divePtr->dive_site = nullptr; | ||||
| 	if (!trip && autogroup) { | ||||
| 		bool alloc; | ||||
| 		trip = get_trip_for_new_dive(divePtr.get(), &alloc); | ||||
| 		if (alloc) | ||||
| 			allocTrip.reset(trip); | ||||
| 	} | ||||
| 
 | ||||
| 	int idx = dive_table_get_insertion_index(&dive_table, divePtr.get()); | ||||
| 	if (newNumber) | ||||
| 		divePtr->number = get_dive_nr_at_idx(idx); | ||||
| 
 | ||||
| 	divesToAdd.dives.push_back({ std::move(divePtr), trip, site }); | ||||
| 	if (allocTrip) | ||||
| 		divesToAdd.trips.push_back(std::move(allocTrip)); | ||||
| } | ||||
| 
 | ||||
| bool AddDive::workToBeDone() | ||||
| { | ||||
| 	return true; | ||||
| } | ||||
| 
 | ||||
| void AddDive::redoit() | ||||
| { | ||||
| 	// Remember selection so that we can undo it
 | ||||
| 	selection = getDiveSelection(); | ||||
| 	currentDive = current_dive; | ||||
| 
 | ||||
| 	divesAndSitesToRemove = addDives(divesToAdd); | ||||
| 	sort_trip_table(&trip_table); // Though unlikely, adding a dive may reorder trips
 | ||||
| 
 | ||||
| 	// Select the newly added dive
 | ||||
| 	setSelection(divesAndSitesToRemove.dives, divesAndSitesToRemove.dives[0]); | ||||
| } | ||||
| 
 | ||||
| void AddDive::undoit() | ||||
| { | ||||
| 	// Simply remove the dive that was previously added...
 | ||||
| 	divesToAdd = removeDives(divesAndSitesToRemove); | ||||
| 	sort_trip_table(&trip_table); // Though unlikely, removing a dive may reorder trips
 | ||||
| 
 | ||||
| 	// ...and restore the selection
 | ||||
| 	setSelection(selection, currentDive); | ||||
| } | ||||
| 
 | ||||
| ImportDives::ImportDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, int flags, const QString &source) | ||||
| { | ||||
| 	setText(tr("import %n dive(s) from %1", "", dives->nr).arg(source)); | ||||
| 
 | ||||
| 	// this only matters if undoit were called before redoit
 | ||||
| 	currentDive = nullptr; | ||||
| 
 | ||||
| 	struct dive_table dives_to_add = { 0 }; | ||||
| 	struct dive_table dives_to_remove = { 0 }; | ||||
| 	struct trip_table trips_to_add = { 0 }; | ||||
| 	struct dive_site_table sites_to_add = { 0 }; | ||||
| 	process_imported_dives(dives, trips, sites, flags, &dives_to_add, &dives_to_remove, &trips_to_add, &sites_to_add); | ||||
| 
 | ||||
| 	// Add trips to the divesToAdd.trips structure
 | ||||
| 	divesToAdd.trips.reserve(trips_to_add.nr); | ||||
| 	for (int i = 0; i < trips_to_add.nr; ++i) | ||||
| 		divesToAdd.trips.emplace_back(trips_to_add.trips[i]); | ||||
| 
 | ||||
| 	// Add sites to the divesToAdd.sites structure
 | ||||
| 	divesToAdd.sites.reserve(sites_to_add.nr); | ||||
| 	for (int i = 0; i < sites_to_add.nr; ++i) | ||||
| 		divesToAdd.sites.emplace_back(sites_to_add.dive_sites[i]); | ||||
| 
 | ||||
| 	// Add dives to the divesToAdd.dives structure
 | ||||
| 	divesToAdd.dives.reserve(dives_to_add.nr); | ||||
| 	for (int i = 0; i < dives_to_add.nr; ++i) { | ||||
| 		OwningDivePtr divePtr(dives_to_add.dives[i]); | ||||
| 		divePtr->selected = false; // See above in AddDive::AddDive()
 | ||||
| 		dive_trip *trip = divePtr->divetrip; | ||||
| 		divePtr->divetrip = nullptr; // See above in AddDive::AddDive()
 | ||||
| 		dive_site *site = divePtr->dive_site; | ||||
| 		divePtr->dive_site = nullptr; // See above in AddDive::AddDive()
 | ||||
| 
 | ||||
| 		divesToAdd.dives.push_back({ std::move(divePtr), trip, site }); | ||||
| 	} | ||||
| 
 | ||||
| 	// Add dive to be deleted to the divesToRemove structure
 | ||||
| 	divesAndSitesToRemove.dives.reserve(dives_to_remove.nr); | ||||
| 	for (int i = 0; i < dives_to_remove.nr; ++i) | ||||
| 		divesAndSitesToRemove.dives.push_back(dives_to_remove.dives[i]); | ||||
| } | ||||
| 
 | ||||
| bool ImportDives::workToBeDone() | ||||
| { | ||||
| 	return !divesToAdd.dives.empty(); | ||||
| } | ||||
| 
 | ||||
| void ImportDives::redoit() | ||||
| { | ||||
| 	// Remember selection so that we can undo it
 | ||||
| 	currentDive = current_dive; | ||||
| 
 | ||||
| 	// Add new dives and sites
 | ||||
| 	DivesAndSitesToRemove divesAndSitesToRemoveNew = addDives(divesToAdd); | ||||
| 
 | ||||
| 	// Remove old dives and sites
 | ||||
| 	divesToAdd = removeDives(divesAndSitesToRemove); | ||||
| 
 | ||||
| 	// Select the newly added dives
 | ||||
| 	setSelection(divesAndSitesToRemoveNew.dives, divesAndSitesToRemoveNew.dives.back()); | ||||
| 
 | ||||
| 	// Remember dives and sites to remove
 | ||||
| 	divesAndSitesToRemove = std::move(divesAndSitesToRemoveNew); | ||||
| } | ||||
| 
 | ||||
| void ImportDives::undoit() | ||||
| { | ||||
| 	// Add new dives and sites
 | ||||
| 	DivesAndSitesToRemove divesAndSitesToRemoveNew = addDives(divesToAdd); | ||||
| 
 | ||||
| 	// Remove old dives and sites
 | ||||
| 	divesToAdd = removeDives(divesAndSitesToRemove); | ||||
| 
 | ||||
| 	// Remember dives and sites to remove
 | ||||
| 	divesAndSitesToRemove = std::move(divesAndSitesToRemoveNew); | ||||
| 
 | ||||
| 	// ...and restore the selection
 | ||||
| 	setSelection(selection, currentDive); | ||||
| } | ||||
| 
 | ||||
| DeleteDive::DeleteDive(const QVector<struct dive*> &divesToDeleteIn) | ||||
| { | ||||
| 	divesToDelete.dives = divesToDeleteIn.toStdVector(); | ||||
| 	setText(tr("delete %n dive(s)", "", divesToDelete.dives.size())); | ||||
| } | ||||
| 
 | ||||
| bool DeleteDive::workToBeDone() | ||||
| { | ||||
| 	return !divesToDelete.dives.empty(); | ||||
| } | ||||
| 
 | ||||
| void DeleteDive::undoit() | ||||
| { | ||||
| 	divesToDelete = addDives(divesToAdd); | ||||
| 	sort_trip_table(&trip_table); // Though unlikely, removing a dive may reorder trips
 | ||||
| 
 | ||||
| 	// Select all re-added dives and make the first one current
 | ||||
| 	dive *currentDive = !divesToDelete.dives.empty() ? divesToDelete.dives[0] : nullptr; | ||||
| 	setSelection(divesToDelete.dives, currentDive); | ||||
| } | ||||
| 
 | ||||
| void DeleteDive::redoit() | ||||
| { | ||||
| 	divesToAdd = removeDives(divesToDelete); | ||||
| 	sort_trip_table(&trip_table); // Though unlikely, adding a dive may reorder trips
 | ||||
| 
 | ||||
| 	// Deselect all dives and select dive that was close to the first deleted dive
 | ||||
| 	dive *newCurrent = nullptr; | ||||
| 	if (!divesToAdd.dives.empty()) { | ||||
| 		timestamp_t when = divesToAdd.dives[0].dive->when; | ||||
| 		newCurrent = find_next_visible_dive(when); | ||||
| 	} | ||||
| 	if (newCurrent) | ||||
| 		setSelection(std::vector<dive *>{ newCurrent }, newCurrent); | ||||
| 	else | ||||
| 		setSelection(std::vector<dive *>(), nullptr); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| ShiftTime::ShiftTime(const QVector<dive *> &changedDives, int amount) | ||||
| 	: diveList(changedDives), timeChanged(amount) | ||||
| { | ||||
| 	setText(tr("shift time of %n dives", "", changedDives.count())); | ||||
| } | ||||
| 
 | ||||
| void ShiftTime::redoit() | ||||
| { | ||||
| 	std::vector<dive_trip *> trips; | ||||
| 	for (dive *d: diveList) { | ||||
| 		d->when += timeChanged; | ||||
| 		if (d->divetrip && std::find(trips.begin(), trips.end(), d->divetrip) == trips.end()) | ||||
| 			trips.push_back(d->divetrip); | ||||
| 	} | ||||
| 
 | ||||
| 	// Changing times may have unsorted the dive and trip tables
 | ||||
| 	sort_dive_table(&dive_table); | ||||
| 	sort_trip_table(&trip_table); | ||||
| 	for (dive_trip *trip: trips) | ||||
| 		sort_dive_table(&trip->dives); // Keep the trip-table in order
 | ||||
| 
 | ||||
| 	// Send signals
 | ||||
| 	emit diveListNotifier.divesTimeChanged(timeChanged, diveList); | ||||
| 	emit diveListNotifier.divesChanged(diveList, DiveField::DATETIME); | ||||
| 
 | ||||
| 	// Select the changed dives
 | ||||
| 	setSelection(diveList.toStdVector(), diveList[0]); | ||||
| 
 | ||||
| 	// Negate the time-shift so that the next call does the reverse
 | ||||
| 	timeChanged = -timeChanged; | ||||
| } | ||||
| 
 | ||||
| bool ShiftTime::workToBeDone() | ||||
| { | ||||
| 	return !diveList.isEmpty(); | ||||
| } | ||||
| 
 | ||||
| void ShiftTime::undoit() | ||||
| { | ||||
| 	// Same as redoit(), since after redoit() we reversed the timeOffset
 | ||||
| 	redoit(); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| RenumberDives::RenumberDives(const QVector<QPair<dive *, int>> &divesToRenumberIn) : divesToRenumber(divesToRenumberIn) | ||||
| { | ||||
| 	setText(tr("renumber %n dive(s)", "", divesToRenumber.count())); | ||||
| } | ||||
| 
 | ||||
| void RenumberDives::undoit() | ||||
| { | ||||
| 	renumberDives(divesToRenumber); | ||||
| 
 | ||||
| 	// Select the changed dives
 | ||||
| 	std::vector<dive *> dives; | ||||
| 	dives.reserve(divesToRenumber.size()); | ||||
| 	for (const QPair<dive *, int> &item: divesToRenumber) | ||||
| 		dives.push_back(item.first); | ||||
| 	setSelection(dives, dives[0]); | ||||
| } | ||||
| 
 | ||||
| bool RenumberDives::workToBeDone() | ||||
| { | ||||
| 	return !divesToRenumber.isEmpty(); | ||||
| } | ||||
| 
 | ||||
| void RenumberDives::redoit() | ||||
| { | ||||
| 	// Redo and undo do the same thing!
 | ||||
| 	undoit(); | ||||
| } | ||||
| 
 | ||||
| bool TripBase::workToBeDone() | ||||
| { | ||||
| 	return !divesToMove.divesToMove.empty(); | ||||
| } | ||||
| 
 | ||||
| void TripBase::redoit() | ||||
| { | ||||
| 	moveDivesBetweenTrips(divesToMove); | ||||
| 	sort_trip_table(&trip_table); // Though unlikely, moving dives may reorder trips
 | ||||
| 
 | ||||
| 	// Select the moved dives
 | ||||
| 	std::vector<dive *> dives; | ||||
| 	dives.reserve(divesToMove.divesToMove.size()); | ||||
| 	for (const DiveToTrip &item: divesToMove.divesToMove) | ||||
| 		dives.push_back(item.dive); | ||||
| 	setSelection(dives, dives[0]); | ||||
| } | ||||
| 
 | ||||
| void TripBase::undoit() | ||||
| { | ||||
| 	// Redo and undo do the same thing!
 | ||||
| 	redoit(); | ||||
| } | ||||
| 
 | ||||
| RemoveDivesFromTrip::RemoveDivesFromTrip(const QVector<dive *> &divesToRemove) | ||||
| { | ||||
| 	setText(tr("remove %n dive(s) from trip", "", divesToRemove.size())); | ||||
| 	divesToMove.divesToMove.reserve(divesToRemove.size()); | ||||
| 	for (dive *d: divesToRemove) { | ||||
| 		// If a user manually removes a dive from a trip, don't autogroup this dive.
 | ||||
| 		// The flag will not be reset on undo, but that should be acceptable.
 | ||||
| 		d->notrip = true; | ||||
| 		divesToMove.divesToMove.push_back( {d, nullptr} ); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| RemoveAutogenTrips::RemoveAutogenTrips() | ||||
| { | ||||
| 	setText(tr("remove autogenerated trips")); | ||||
| 	// TODO: don't touch core-innards directly
 | ||||
| 	int i; | ||||
| 	struct dive *dive; | ||||
| 	for_each_dive(i, dive) { | ||||
| 		if (dive->divetrip && dive->divetrip->autogen) | ||||
| 			divesToMove.divesToMove.push_back( {dive, nullptr} ); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| AddDivesToTrip::AddDivesToTrip(const QVector<dive *> &divesToAddIn, dive_trip *trip) | ||||
| { | ||||
| 	setText(tr("add %n dives to trip", "", divesToAddIn.size())); | ||||
| 	for (dive *d: divesToAddIn) | ||||
| 		divesToMove.divesToMove.push_back( {d, trip} ); | ||||
| } | ||||
| 
 | ||||
| CreateTrip::CreateTrip(const QVector<dive *> &divesToAddIn) | ||||
| { | ||||
| 	setText(tr("create trip")); | ||||
| 
 | ||||
| 	if (divesToAddIn.isEmpty()) | ||||
| 		return; | ||||
| 
 | ||||
| 	dive_trip *trip = create_trip_from_dive(divesToAddIn[0]); | ||||
| 	divesToMove.tripsToAdd.emplace_back(trip); | ||||
| 	for (dive *d: divesToAddIn) | ||||
| 		divesToMove.divesToMove.push_back( {d, trip} ); | ||||
| } | ||||
| 
 | ||||
| AutogroupDives::AutogroupDives() | ||||
| { | ||||
| 	setText(tr("autogroup dives")); | ||||
| 
 | ||||
| 	dive_trip *trip; | ||||
| 	bool alloc; | ||||
| 	int from, to; | ||||
| 	for(int i = 0; (trip = get_dives_to_autogroup(&dive_table, i, &from, &to, &alloc)) != NULL; i = to) { | ||||
| 		// If this is an allocated trip, take ownership
 | ||||
| 		if (alloc) | ||||
| 			divesToMove.tripsToAdd.emplace_back(trip); | ||||
| 		for (int j = from; j < to; ++j) | ||||
| 			divesToMove.divesToMove.push_back( { get_dive(j), trip } ); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| MergeTrips::MergeTrips(dive_trip *trip1, dive_trip *trip2) | ||||
| { | ||||
| 	if (trip1 == trip2) | ||||
| 		return; | ||||
| 	dive_trip *newTrip = combine_trips(trip1, trip2); | ||||
| 	divesToMove.tripsToAdd.emplace_back(newTrip); | ||||
| 	for (int i = 0; i < trip1->dives.nr; ++i) | ||||
| 		divesToMove.divesToMove.push_back( { trip1->dives.dives[i], newTrip } ); | ||||
| 	for (int i = 0; i < trip2->dives.nr; ++i) | ||||
| 		divesToMove.divesToMove.push_back( { trip2->dives.dives[i], newTrip } ); | ||||
| } | ||||
| 
 | ||||
| // std::array<dive *, 2> is the same as struct *dive[2], with the fundamental
 | ||||
| // difference that it can be returned from functions. Thus, this constructor
 | ||||
| // can be chained with the result of a function.
 | ||||
| SplitDivesBase::SplitDivesBase(dive *d, std::array<dive *, 2> newDives) | ||||
| { | ||||
| 	// If either of the new dives is null, simply return. Empty arrays indicate that nothing is to be done.
 | ||||
| 	if (!newDives[0] || !newDives[1]) | ||||
| 		return; | ||||
| 
 | ||||
| 	// Currently, the core code selects the dive -> this is not what we want, as
 | ||||
| 	// we manually manage the selection post-command.
 | ||||
| 	// TODO: Reset selection in core.
 | ||||
| 	newDives[0]->selected = false; | ||||
| 	newDives[1]->selected = false; | ||||
| 
 | ||||
| 	// The new dives will be registered to the dive site using the site member
 | ||||
| 	// of the DiveToAdd structure. For this to work, we must set the dive's
 | ||||
| 	// dive_site member to null. Yes, that's subtle!
 | ||||
| 	newDives[0]->dive_site = nullptr; | ||||
| 	newDives[1]->dive_site = nullptr; | ||||
| 
 | ||||
| 	diveToSplit.dives.push_back(d); | ||||
| 	splitDives.dives.resize(2); | ||||
| 	splitDives.dives[0].dive.reset(newDives[0]); | ||||
| 	splitDives.dives[0].trip = d->divetrip; | ||||
| 	splitDives.dives[0].site = d->dive_site; | ||||
| 	splitDives.dives[1].dive.reset(newDives[1]); | ||||
| 	splitDives.dives[1].trip = d->divetrip; | ||||
| 	splitDives.dives[1].site = d->dive_site; | ||||
| } | ||||
| 
 | ||||
| bool SplitDivesBase::workToBeDone() | ||||
| { | ||||
| 	return !diveToSplit.dives.empty(); | ||||
| } | ||||
| 
 | ||||
| void SplitDivesBase::redoit() | ||||
| { | ||||
| 	divesToUnsplit = addDives(splitDives); | ||||
| 	unsplitDive = removeDives(diveToSplit); | ||||
| 
 | ||||
| 	// Select split dives and make first dive current
 | ||||
| 	setSelection(divesToUnsplit.dives, divesToUnsplit.dives[0]); | ||||
| } | ||||
| 
 | ||||
| void SplitDivesBase::undoit() | ||||
| { | ||||
| 	// Note: reverse order with respect to redoit()
 | ||||
| 	diveToSplit = addDives(unsplitDive); | ||||
| 	splitDives = removeDives(divesToUnsplit); | ||||
| 
 | ||||
| 	// Select unsplit dive and make it current
 | ||||
| 	setSelection(diveToSplit.dives, diveToSplit.dives[0] ); | ||||
| } | ||||
| 
 | ||||
| static std::array<dive *, 2> doSplitDives(const dive *d, duration_t time) | ||||
| { | ||||
| 	// Split the dive
 | ||||
| 	dive *new1, *new2; | ||||
| 	if (time.seconds < 0) | ||||
| 		split_dive(d, &new1, &new2); | ||||
| 	else | ||||
| 		split_dive_at_time(d, time, &new1, &new2); | ||||
| 
 | ||||
| 	return { new1, new2 }; | ||||
| } | ||||
| 
 | ||||
| SplitDives::SplitDives(dive *d, duration_t time) : SplitDivesBase(d, doSplitDives(d, time)) | ||||
| { | ||||
| 	setText(tr("split dive")); | ||||
| } | ||||
| 
 | ||||
| static std::array<dive *, 2> splitDiveComputer(const dive *d, int dc_num) | ||||
| { | ||||
| 	// Refuse to do anything if the dive has only one dive computer.
 | ||||
| 	// Yes, this should have been checked by the UI, but let's just make sure.
 | ||||
| 	if (!d->dc.next) | ||||
| 		return { nullptr, nullptr}; | ||||
| 
 | ||||
| 	dive *new1, *new2; | ||||
| 	split_divecomputer(d, dc_num, &new1, &new2); | ||||
| 
 | ||||
| 	return { new1, new2 }; | ||||
| } | ||||
| 
 | ||||
| SplitDiveComputer::SplitDiveComputer(dive *d, int dc_num) : SplitDivesBase(d, splitDiveComputer(d, dc_num)) | ||||
| { | ||||
| 	setText(tr("split dive computer")); | ||||
| } | ||||
| 
 | ||||
| DiveComputerBase::DiveComputerBase(dive *old_dive, dive *new_dive, int dc_nr_after_in) : dc_nr_before(dc_number), | ||||
| 	dc_nr_after(dc_nr_after_in) | ||||
| { | ||||
| 	if (!new_dive) | ||||
| 		return; | ||||
| 
 | ||||
| 	diveToRemove.dives.push_back(old_dive); | ||||
| 
 | ||||
| 	// Currently, the core code selects the dive -> this is not what we want, as
 | ||||
| 	// we manually manage the selection post-command.
 | ||||
| 	// TODO: Reset selection in core.
 | ||||
| 	new_dive->selected = false; | ||||
| 
 | ||||
| 	// Reset references to trip and site in the new dive, as the undo command
 | ||||
| 	// will add the dive to the trip and site.
 | ||||
| 	new_dive->divetrip = nullptr; | ||||
| 	new_dive->dive_site = nullptr; | ||||
| 
 | ||||
| 	diveToAdd.dives.resize(1); | ||||
| 	diveToAdd.dives[0].dive.reset(new_dive); | ||||
| 	diveToAdd.dives[0].trip = old_dive->divetrip; | ||||
| 	diveToAdd.dives[0].site = old_dive->dive_site; | ||||
| } | ||||
| 
 | ||||
| bool DiveComputerBase::workToBeDone() | ||||
| { | ||||
| 	return !diveToRemove.dives.empty() || !diveToAdd.dives.empty(); | ||||
| } | ||||
| 
 | ||||
| void DiveComputerBase::redoit() | ||||
| { | ||||
| 	DivesAndSitesToRemove addedDive = addDives(diveToAdd); | ||||
| 	diveToAdd = removeDives(diveToRemove); | ||||
| 	diveToRemove = std::move(addedDive); | ||||
| 
 | ||||
| 	dc_number = dc_nr_after; | ||||
| 
 | ||||
| 	// Select added dive and make it current.
 | ||||
| 	// This automatically replots the profile.
 | ||||
| 	setSelection(diveToRemove.dives, diveToRemove.dives[0]); | ||||
| 
 | ||||
| 	std::swap(dc_nr_before, dc_nr_after); | ||||
| } | ||||
| 
 | ||||
| void DiveComputerBase::undoit() | ||||
| { | ||||
| 	// Undo and redo do the same
 | ||||
| 	redoit(); | ||||
| } | ||||
| 
 | ||||
| MoveDiveComputerToFront::MoveDiveComputerToFront(dive *d, int dc_num) | ||||
| 	: DiveComputerBase(d, make_first_dc(d, dc_num), 0) | ||||
| { | ||||
| 	setText(tr("move dive computer to front")); | ||||
| } | ||||
| 
 | ||||
| DeleteDiveComputer::DeleteDiveComputer(dive *d, int dc_num) | ||||
| 	: DiveComputerBase(d, clone_delete_divecomputer(d, dc_num), std::min(count_divecomputers(d) - 1, dc_num)) | ||||
| { | ||||
| 	setText(tr("delete dive computer")); | ||||
| } | ||||
| 
 | ||||
| MergeDives::MergeDives(const QVector <dive *> &dives) | ||||
| { | ||||
| 	setText(tr("merge dive")); | ||||
| 
 | ||||
| 	// Just a safety check - if there's not two or more dives - do nothing
 | ||||
| 	// The caller should have made sure that this doesn't happen.
 | ||||
| 	if (dives.count() < 2) { | ||||
| 		qWarning("Merging less than two dives"); | ||||
| 		return; | ||||
| 	} | ||||
| 
 | ||||
| 	dive_trip *preferred_trip; | ||||
| 	dive_site *preferred_site; | ||||
| 	OwningDivePtr d(merge_dives(dives[0], dives[1], dives[1]->when - dives[0]->when, false, &preferred_trip, &preferred_site)); | ||||
| 
 | ||||
| 	// Currently, the core code selects the dive -> this is not what we want, as
 | ||||
| 	// we manually manage the selection post-command.
 | ||||
| 	// TODO: Remove selection code from core.
 | ||||
| 	d->selected = false; | ||||
| 
 | ||||
| 	// Set the preferred dive trip, so that for subsequent merges the better trip can be selected
 | ||||
| 	d->divetrip = preferred_trip; | ||||
| 	for (int i = 2; i < dives.count(); ++i) { | ||||
| 		d.reset(merge_dives(d.get(), dives[i], dives[i]->when - d->when, false, &preferred_trip, &preferred_site)); | ||||
| 		// Set the preferred dive trip and site, so that for subsequent merges the better trip and site can be selected
 | ||||
| 		d->divetrip = preferred_trip; | ||||
| 		d->dive_site = preferred_site; | ||||
| 	} | ||||
| 
 | ||||
| 	// We got our preferred trip and site, so now the references can be deleted from the newly generated dive
 | ||||
| 	d->divetrip = nullptr; | ||||
| 	d->dive_site = nullptr; | ||||
| 
 | ||||
| 	// The merged dive gets the number of the first dive with a non-zero number
 | ||||
| 	for (const dive *dive: dives) { | ||||
| 		if (dive->number) { | ||||
| 			d->number = dive->number; | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	// We will only renumber the remaining dives if the joined dives are consecutive.
 | ||||
| 	// Otherwise all bets are off concerning what the user wanted and doing nothing seems
 | ||||
| 	// like the best option.
 | ||||
| 	int idx = get_divenr(dives[0]); | ||||
| 	int num = dives.count(); | ||||
| 	if (idx < 0 || idx + num > dive_table.nr) { | ||||
| 		// It was the callers responsibility to pass only known dives.
 | ||||
| 		// Something is seriously wrong - give up.
 | ||||
| 		qWarning("Merging unknown dives"); | ||||
| 		return; | ||||
| 	} | ||||
| 	// std::equal compares two ranges. The parameters are (begin_range1, end_range1, begin_range2).
 | ||||
| 	// Here, we can compare C-arrays, because QVector guarantees contiguous storage.
 | ||||
| 	if (std::equal(&dives[0], &dives[0] + num, &dive_table.dives[idx]) && | ||||
| 	    dives[0]->number && dives.last()->number && dives[0]->number < dives.last()->number) { | ||||
| 		// We have a consecutive set of dives. Rename all following dives according to the
 | ||||
| 		// number of erased dives. This considers that there might be missing numbers.
 | ||||
| 		// Comment copied from core/divelist.c:
 | ||||
| 		// So if you had a dive list  1 3 6 7 8, and you
 | ||||
| 		// merge 1 and 3, the resulting numbered list will
 | ||||
| 		// be 1 4 5 6, because we assume that there were
 | ||||
| 		// some missing dives (originally dives 4 and 5),
 | ||||
| 		// that now will still be missing (dives 2 and 3
 | ||||
| 		// in the renumbered world).
 | ||||
| 		//
 | ||||
| 		// Obviously the normal case is that everything is
 | ||||
| 		// consecutive, and the difference will be 1, so the
 | ||||
| 		// above example is not supposed to be normal.
 | ||||
| 		int diff = dives.last()->number - dives[0]->number; | ||||
| 		divesToRenumber.reserve(dive_table.nr - idx - num); | ||||
| 		int previousnr = dives[0]->number; | ||||
| 		for (int i = idx + num; i < dive_table.nr; ++i) { | ||||
| 			int newnr = dive_table.dives[i]->number - diff; | ||||
| 
 | ||||
| 			// Stop renumbering if stuff isn't in order (see also core/divelist.c)
 | ||||
| 			if (newnr <= previousnr) | ||||
| 				break; | ||||
| 			divesToRenumber.append(QPair<dive *,int>(dive_table.dives[i], newnr)); | ||||
| 			previousnr = newnr; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	mergedDive.dives.resize(1); | ||||
| 	mergedDive.dives[0].dive = std::move(d); | ||||
| 	mergedDive.dives[0].trip = preferred_trip; | ||||
| 	mergedDive.dives[0].site = preferred_site; | ||||
| 	divesToMerge.dives = dives.toStdVector(); | ||||
| } | ||||
| 
 | ||||
| bool MergeDives::workToBeDone() | ||||
| { | ||||
| 	return !mergedDive.dives.empty(); | ||||
| } | ||||
| 
 | ||||
| void MergeDives::redoit() | ||||
| { | ||||
| 	renumberDives(divesToRenumber); | ||||
| 	diveToUnmerge = addDives(mergedDive); | ||||
| 	unmergedDives = removeDives(divesToMerge); | ||||
| 
 | ||||
| 	// Select merged dive and make it current
 | ||||
| 	setSelection(diveToUnmerge.dives, diveToUnmerge.dives[0]); | ||||
| } | ||||
| 
 | ||||
| void MergeDives::undoit() | ||||
| { | ||||
| 	divesToMerge = addDives(unmergedDives); | ||||
| 	mergedDive = removeDives(diveToUnmerge); | ||||
| 	renumberDives(divesToRenumber); | ||||
| 
 | ||||
| 	// Select unmerged dives and make first one current
 | ||||
| 	setSelection(divesToMerge.dives, divesToMerge.dives[0]); | ||||
| } | ||||
| 
 | ||||
| } // namespace Command
 | ||||
							
								
								
									
										285
									
								
								commands/command_divelist.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										285
									
								
								commands/command_divelist.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,285 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| // Note: this header file is used by the undo-machinery and should not be included elsewhere.
 | ||||
| 
 | ||||
| #ifndef COMMAND_DIVELIST_H | ||||
| #define COMMAND_DIVELIST_H | ||||
| 
 | ||||
| #include "command_base.h" | ||||
| 
 | ||||
| #include <QVector> | ||||
| 
 | ||||
| // We put everything in a namespace, so that we can shorten names without polluting the global namespace
 | ||||
| namespace Command { | ||||
| 
 | ||||
| // This helper structure describes a dive that we want to add.
 | ||||
| struct DiveToAdd { | ||||
| 	OwningDivePtr	 dive;		// Dive to add
 | ||||
| 	dive_trip	*trip;		// Trip the dive belongs to, may be null
 | ||||
| 	dive_site	*site;		// Site the dive is associated with, may be null
 | ||||
| }; | ||||
| 
 | ||||
| // Multiple trips, dives and dive sites that have to be added for a command
 | ||||
| struct DivesAndTripsToAdd { | ||||
| 	std::vector<DiveToAdd> dives; | ||||
| 	std::vector<OwningTripPtr> trips; | ||||
| 	std::vector<OwningDiveSitePtr> sites; | ||||
| }; | ||||
| 
 | ||||
| // Dives and sites that have to be removed for a command
 | ||||
| struct DivesAndSitesToRemove { | ||||
| 	std::vector<dive *> dives; | ||||
| 	std::vector<dive_site *> sites; | ||||
| }; | ||||
| 
 | ||||
| // This helper structure describes a dive that should be moved to / removed from
 | ||||
| // a trip. If the "trip" member is null, the dive is removed from its trip (if
 | ||||
| // it is in a trip, that is)
 | ||||
| struct DiveToTrip | ||||
| { | ||||
| 	struct dive	*dive; | ||||
| 	dive_trip	*trip; | ||||
| }; | ||||
| 
 | ||||
| // This helper structure describes a number of dives to add to / remove from /
 | ||||
| // move between trips.
 | ||||
| // It has ownership of the trips (if any) that have to be added before hand.
 | ||||
| struct DivesToTrip | ||||
| { | ||||
| 	std::vector<DiveToTrip> divesToMove;		// If dive_trip is null, remove from trip
 | ||||
| 	std::vector<OwningTripPtr> tripsToAdd; | ||||
| }; | ||||
| 
 | ||||
| // All divelist commands derive from a common base class. It keeps track
 | ||||
| // of dive site counts that may have changed.
 | ||||
| // Derived classes implement the virtual methods redoit() and undoit()
 | ||||
| // [Yes, the names could be more expressive].
 | ||||
| class DiveListBase : public Base { | ||||
| protected: | ||||
| 	// These are helper functions to add / remove dive from the C-core structures.
 | ||||
| 	DiveToAdd removeDive(struct dive *d, std::vector<OwningTripPtr> &tripsToAdd); | ||||
| 	dive *addDive(DiveToAdd &d); | ||||
| 	DivesAndTripsToAdd removeDives(DivesAndSitesToRemove &divesAndSitesToDelete); | ||||
| 	DivesAndSitesToRemove addDives(DivesAndTripsToAdd &toAdd); | ||||
| 
 | ||||
| 	// Register dive sites where counts changed so that we can signal the frontend later.
 | ||||
| 	void diveSiteCountChanged(struct dive_site *ds); | ||||
| 
 | ||||
| private: | ||||
| 	// Keep track of dive sites where the number of dives changed
 | ||||
| 	std::vector<dive_site *> sitesCountChanged; | ||||
| 	void initWork(); | ||||
| 	void finishWork(); // update dive site counts
 | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 	virtual void redoit() = 0; | ||||
| 	virtual void undoit() = 0; | ||||
| }; | ||||
| 
 | ||||
| class AddDive : public DiveListBase { | ||||
| public: | ||||
| 	AddDive(dive *dive, bool autogroup, bool newNumber); | ||||
| private: | ||||
| 	void undoit() override; | ||||
| 	void redoit() override; | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| 	// For redo
 | ||||
| 	// Note: we a multi-dive structure even though we add only a single dive, so
 | ||||
| 	// that we can reuse the multi-dive functions of the other commands.
 | ||||
| 	DivesAndTripsToAdd	divesToAdd; | ||||
| 
 | ||||
| 	// For undo
 | ||||
| 	DivesAndSitesToRemove	divesAndSitesToRemove; | ||||
| 	std::vector<dive *>	selection; | ||||
| 	dive *			currentDive; | ||||
| }; | ||||
| 
 | ||||
| class ImportDives : public DiveListBase { | ||||
| public: | ||||
| 	// Note: dives and trips are consumed - after the call they will be empty.
 | ||||
| 	ImportDives(struct dive_table *dives, struct trip_table *trips, struct dive_site_table *sites, int flags, const QString &source); | ||||
| private: | ||||
| 	void undoit() override; | ||||
| 	void redoit() override; | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| 	// For redo and undo
 | ||||
| 	DivesAndTripsToAdd	divesToAdd; | ||||
| 	DivesAndSitesToRemove	divesAndSitesToRemove; | ||||
| 
 | ||||
| 	// For redo
 | ||||
| 	std::vector<OwningDiveSitePtr>	sitesToAdd; | ||||
| 
 | ||||
| 	// For undo
 | ||||
| 	std::vector<dive_site *>	sitesToRemove; | ||||
| 	std::vector<dive *>	selection; | ||||
| 	dive *			currentDive; | ||||
| }; | ||||
| 
 | ||||
| class DeleteDive : public DiveListBase { | ||||
| public: | ||||
| 	DeleteDive(const QVector<dive *> &divesToDelete); | ||||
| private: | ||||
| 	void undoit() override; | ||||
| 	void redoit() override; | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| 	// For redo
 | ||||
| 	DivesAndSitesToRemove divesToDelete; | ||||
| 
 | ||||
| 	std::vector<OwningTripPtr> tripsToAdd; | ||||
| 	DivesAndTripsToAdd divesToAdd; | ||||
| }; | ||||
| 
 | ||||
| class ShiftTime : public DiveListBase { | ||||
| public: | ||||
| 	ShiftTime(const QVector<dive *> &changedDives, int amount); | ||||
| private: | ||||
| 	void undoit() override; | ||||
| 	void redoit() override; | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| 	// For redo and undo
 | ||||
| 	QVector<dive *> diveList; | ||||
| 	int timeChanged; | ||||
| }; | ||||
| 
 | ||||
| class RenumberDives : public DiveListBase { | ||||
| public: | ||||
| 	RenumberDives(const QVector<QPair<dive *, int>> &divesToRenumber); | ||||
| private: | ||||
| 	void undoit() override; | ||||
| 	void redoit() override; | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| 	// For redo and undo: pairs of dive-id / new number
 | ||||
| 	QVector<QPair<dive *, int>> divesToRenumber; | ||||
| }; | ||||
| 
 | ||||
| // The classes RemoveDivesFromTrip, RemoveAutogenTrips, CreateTrip, AutogroupDives
 | ||||
| // and MergeTrips all do the same thing, just the intialization differs.
 | ||||
| // Therefore, define a base class with the proper data-structures, redo()
 | ||||
| // and undo() functions and derive to specialize the initialization.
 | ||||
| class TripBase : public DiveListBase { | ||||
| protected: | ||||
| 	void undoit() override; | ||||
| 	void redoit() override; | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| 	// For redo and undo
 | ||||
| 	DivesToTrip divesToMove; | ||||
| }; | ||||
| struct RemoveDivesFromTrip : public TripBase { | ||||
| 	RemoveDivesFromTrip(const QVector<dive *> &divesToRemove); | ||||
| }; | ||||
| struct RemoveAutogenTrips : public TripBase { | ||||
| 	RemoveAutogenTrips(); | ||||
| }; | ||||
| struct AddDivesToTrip : public TripBase { | ||||
| 	AddDivesToTrip(const QVector<dive *> &divesToAdd, dive_trip *trip); | ||||
| }; | ||||
| struct CreateTrip : public TripBase { | ||||
| 	CreateTrip(const QVector<dive *> &divesToAdd); | ||||
| }; | ||||
| struct AutogroupDives : public TripBase { | ||||
| 	AutogroupDives(); | ||||
| }; | ||||
| struct MergeTrips : public TripBase { | ||||
| 	MergeTrips(dive_trip *trip1, dive_trip *trip2); | ||||
| }; | ||||
| 
 | ||||
| class SplitDivesBase : public DiveListBase { | ||||
| protected: | ||||
| 	SplitDivesBase(dive *old, std::array<dive *, 2> newDives); | ||||
| private: | ||||
| 	void undoit() override; | ||||
| 	void redoit() override; | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| 	// For redo
 | ||||
| 	// For each dive to split, we remove one from and put two dives into the backend
 | ||||
| 	// Note: we use a vector even though we split only a single dive, so
 | ||||
| 	// that we can reuse the multi-dive functions of the other commands.
 | ||||
| 	DivesAndSitesToRemove	diveToSplit; | ||||
| 	DivesAndTripsToAdd	splitDives; | ||||
| 
 | ||||
| 	// For undo
 | ||||
| 	// For each dive to unsplit, we remove two dives from and add one into the backend
 | ||||
| 	// Note: we use a multi-dive structure even though we unsplit only a single dive, so
 | ||||
| 	// that we can reuse the multi-dive functions of the other commands.
 | ||||
| 	DivesAndTripsToAdd	unsplitDive; | ||||
| 	DivesAndSitesToRemove	divesToUnsplit; | ||||
| }; | ||||
| 
 | ||||
| class SplitDives : public SplitDivesBase { | ||||
| public: | ||||
| 	// If time is < 0, split at first surface interval
 | ||||
| 	SplitDives(dive *d, duration_t time); | ||||
| }; | ||||
| 
 | ||||
| class SplitDiveComputer : public SplitDivesBase { | ||||
| public: | ||||
| 	// If time is < 0, split at first surface interval
 | ||||
| 	SplitDiveComputer(dive *d, int dc_num); | ||||
| }; | ||||
| 
 | ||||
| // When manipulating dive computers (moving, deleting) we go the ineffective,
 | ||||
| // but simple and robust way: We keep two full copies of the dive (before and after).
 | ||||
| // Removing and readding assures that the dive stays at the correct
 | ||||
| // position in the list (the dive computer list is used for sorting dives).
 | ||||
| class DiveComputerBase : public DiveListBase { | ||||
| protected: | ||||
| 	// old_dive must be a dive known to the core.
 | ||||
| 	// new_dive must be new dive whose ownership is taken.
 | ||||
| 	DiveComputerBase(dive *old_dive, dive *new_dive, int dc_nr_after); | ||||
| private: | ||||
| 	void undoit() override; | ||||
| 	void redoit() override; | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| protected: | ||||
| 	// For redo and undo
 | ||||
| 	DivesAndTripsToAdd	diveToAdd; | ||||
| 	DivesAndSitesToRemove	diveToRemove; | ||||
| 	int			dc_nr_before, dc_nr_after; | ||||
| }; | ||||
| 
 | ||||
| class MoveDiveComputerToFront : public DiveComputerBase { | ||||
| public: | ||||
| 	MoveDiveComputerToFront(dive *d, int dc_num); | ||||
| }; | ||||
| 
 | ||||
| class DeleteDiveComputer : public DiveComputerBase { | ||||
| public: | ||||
| 	DeleteDiveComputer(dive *d, int dc_num); | ||||
| }; | ||||
| 
 | ||||
| class MergeDives : public DiveListBase { | ||||
| public: | ||||
| 	MergeDives(const QVector<dive *> &dives); | ||||
| private: | ||||
| 	void undoit() override; | ||||
| 	void redoit() override; | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| 	// For redo
 | ||||
| 	// Add one and remove a batch of dives
 | ||||
| 	// Note: we use a multi-dives structure even though we add only a single dive, so
 | ||||
| 	// that we can reuse the multi-dive functions of the other commands.
 | ||||
| 	DivesAndTripsToAdd	mergedDive; | ||||
| 	DivesAndSitesToRemove	divesToMerge; | ||||
| 
 | ||||
| 	// For undo
 | ||||
| 	// Remove one and add a batch of dives
 | ||||
| 	// Note: we use a vector even though we remove only a single dive, so
 | ||||
| 	// that we can reuse the multi-dive functions of the other commands.
 | ||||
| 	DivesAndSitesToRemove	diveToUnmerge; | ||||
| 	DivesAndTripsToAdd	unmergedDives; | ||||
| 
 | ||||
| 	// For undo and redo
 | ||||
| 	QVector<QPair<dive *, int>> divesToRenumber; | ||||
| }; | ||||
| 
 | ||||
| } // namespace Command
 | ||||
| 
 | ||||
| #endif // COMMAND_DIVELIST_H
 | ||||
							
								
								
									
										395
									
								
								commands/command_divesite.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										395
									
								
								commands/command_divesite.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,395 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| 
 | ||||
| #include "command_divesite.h" | ||||
| #include "command_private.h" | ||||
| #include "core/divesite.h" | ||||
| #include "core/subsurface-qt/DiveListNotifier.h" | ||||
| #include "core/qthelper.h" | ||||
| #include "core/subsurface-string.h" | ||||
| #include "qt-models/divelocationmodel.h" | ||||
| #include "qt-models/filtermodels.h" | ||||
| 
 | ||||
| namespace Command { | ||||
| 
 | ||||
| // Helper functions to add / remove a set of dive sites
 | ||||
| 
 | ||||
| // Add a set of dive sites to the core. The dives that were associated with
 | ||||
| // that dive site will be restored to that dive site.
 | ||||
| static std::vector<dive_site *> addDiveSites(std::vector<OwningDiveSitePtr> &sites) | ||||
| { | ||||
| 	std::vector<dive_site *> res; | ||||
| 	QVector<dive *> changedDives; | ||||
| 	res.reserve(sites.size()); | ||||
| 
 | ||||
| 	for (OwningDiveSitePtr &ds: sites) { | ||||
| 		// Readd the dives that belonged to this site
 | ||||
| 		for (int i = 0; i < ds->dives.nr; ++i) { | ||||
| 			// TODO: send dive site changed signal
 | ||||
| 			struct dive *d = ds->dives.dives[i]; | ||||
| 			d->dive_site = ds.get(); | ||||
| 			changedDives.push_back(d); | ||||
| 		} | ||||
| 
 | ||||
| 		// Add dive site to core, but remember a non-owning pointer first.
 | ||||
| 		res.push_back(ds.get()); | ||||
| 		int idx = register_dive_site(ds.release()); // Return ownership to backend.
 | ||||
| 		emit diveListNotifier.diveSiteAdded(res.back(), idx); // Inform frontend of new dive site.
 | ||||
| 	} | ||||
| 
 | ||||
| 	emit diveListNotifier.divesChanged(changedDives, DiveField::DIVESITE); | ||||
| 
 | ||||
| 	// Clear vector of unused owning pointers
 | ||||
| 	sites.clear(); | ||||
| 
 | ||||
| 	return res; | ||||
| } | ||||
| 
 | ||||
| // Remove a set of dive sites. Get owning pointers to them. The dives are set to
 | ||||
| // being at no dive site, but the dive site will retain a list of dives, so
 | ||||
| // that the dives can be readded to the site on undo.
 | ||||
| static std::vector<OwningDiveSitePtr> removeDiveSites(std::vector<dive_site *> &sites) | ||||
| { | ||||
| 	std::vector<OwningDiveSitePtr> res; | ||||
| 	QVector<dive *> changedDives; | ||||
| 	res.reserve(sites.size()); | ||||
| 
 | ||||
| 	for (dive_site *ds: sites) { | ||||
| 		// Reset the dive_site field of the affected dives
 | ||||
| 		for (int i = 0; i < ds->dives.nr; ++i) { | ||||
| 			struct dive *d = ds->dives.dives[i]; | ||||
| 			d->dive_site = nullptr; | ||||
| 			changedDives.push_back(d); | ||||
| 		} | ||||
| 
 | ||||
| 		// Remove dive site from core and take ownership.
 | ||||
| 		int idx = unregister_dive_site(ds); | ||||
| 		res.emplace_back(ds); | ||||
| 		emit diveListNotifier.diveSiteDeleted(ds, idx); // Inform frontend of removed dive site.
 | ||||
| 	} | ||||
| 
 | ||||
| 	emit diveListNotifier.divesChanged(changedDives, DiveField::DIVESITE); | ||||
| 
 | ||||
| 	sites.clear(); | ||||
| 
 | ||||
| 	return res; | ||||
| } | ||||
| 
 | ||||
| AddDiveSite::AddDiveSite(const QString &name) | ||||
| { | ||||
| 	setText(tr("add dive site")); | ||||
| 	sitesToAdd.emplace_back(alloc_dive_site()); | ||||
| 	sitesToAdd.back()->name = copy_qstring(name); | ||||
| } | ||||
| 
 | ||||
| bool AddDiveSite::workToBeDone() | ||||
| { | ||||
| 	return true; | ||||
| } | ||||
| 
 | ||||
| void AddDiveSite::redo() | ||||
| { | ||||
| 	sitesToRemove = addDiveSites(sitesToAdd); | ||||
| } | ||||
| 
 | ||||
| void AddDiveSite::undo() | ||||
| { | ||||
| 	sitesToAdd = removeDiveSites(sitesToRemove); | ||||
| } | ||||
| 
 | ||||
| ImportDiveSites::ImportDiveSites(struct dive_site_table *sites, const QString &source) | ||||
| { | ||||
| 	setText(tr("import dive sites from %1").arg(source)); | ||||
| 
 | ||||
| 	for (int i = 0; i < sites->nr; ++i) { | ||||
| 		struct dive_site *new_ds = sites->dive_sites[i]; | ||||
| 
 | ||||
| 		// Don't import dive sites that already exist. Currently we only check for
 | ||||
| 		// the same name. We might want to be smarter here and merge dive site data, etc.
 | ||||
| 		struct dive_site *old_ds = get_same_dive_site(new_ds); | ||||
| 		if (old_ds) { | ||||
| 			free_dive_site(new_ds); | ||||
| 			continue; | ||||
| 		} | ||||
| 		sitesToAdd.emplace_back(new_ds); | ||||
| 	} | ||||
| 
 | ||||
| 	// All site have been consumed
 | ||||
| 	sites->nr = 0; | ||||
| } | ||||
| 
 | ||||
| bool ImportDiveSites::workToBeDone() | ||||
| { | ||||
| 	return !sitesToAdd.empty(); | ||||
| } | ||||
| 
 | ||||
| void ImportDiveSites::redo() | ||||
| { | ||||
| 	sitesToRemove = addDiveSites(sitesToAdd); | ||||
| } | ||||
| 
 | ||||
| void ImportDiveSites::undo() | ||||
| { | ||||
| 	sitesToAdd = removeDiveSites(sitesToRemove); | ||||
| } | ||||
| 
 | ||||
| DeleteDiveSites::DeleteDiveSites(const QVector<dive_site *> &sites) : sitesToRemove(sites.toStdVector()) | ||||
| { | ||||
| 	setText(tr("delete %n dive site(s)", "", sites.size())); | ||||
| } | ||||
| 
 | ||||
| bool DeleteDiveSites::workToBeDone() | ||||
| { | ||||
| 	return !sitesToRemove.empty(); | ||||
| } | ||||
| 
 | ||||
| void DeleteDiveSites::redo() | ||||
| { | ||||
| 	sitesToAdd = removeDiveSites(sitesToRemove); | ||||
| } | ||||
| 
 | ||||
| void DeleteDiveSites::undo() | ||||
| { | ||||
| 	sitesToRemove = addDiveSites(sitesToAdd); | ||||
| } | ||||
| 
 | ||||
| PurgeUnusedDiveSites::PurgeUnusedDiveSites() | ||||
| { | ||||
| 	setText(tr("purge unused dive sites")); | ||||
| 	for (int i = 0; i < dive_site_table.nr; ++i) { | ||||
| 		dive_site *ds = dive_site_table.dive_sites[i]; | ||||
| 		if (ds->dives.nr == 0) | ||||
| 			sitesToRemove.push_back(ds); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| bool PurgeUnusedDiveSites::workToBeDone() | ||||
| { | ||||
| 	return !sitesToRemove.empty(); | ||||
| } | ||||
| 
 | ||||
| void PurgeUnusedDiveSites::redo() | ||||
| { | ||||
| 	sitesToAdd = removeDiveSites(sitesToRemove); | ||||
| } | ||||
| 
 | ||||
| void PurgeUnusedDiveSites::undo() | ||||
| { | ||||
| 	sitesToRemove = addDiveSites(sitesToAdd); | ||||
| } | ||||
| 
 | ||||
| // Helper function: swap C and Qt string
 | ||||
| static void swap(char *&c, QString &q) | ||||
| { | ||||
| 	QString s = c; | ||||
| 	free(c); | ||||
| 	c = copy_qstring(q); | ||||
| 	q = s; | ||||
| } | ||||
| 
 | ||||
| EditDiveSiteName::EditDiveSiteName(dive_site *dsIn, const QString &name) : ds(dsIn), | ||||
| 	value(name) | ||||
| { | ||||
| 	setText(tr("Edit dive site name")); | ||||
| } | ||||
| 
 | ||||
| bool EditDiveSiteName::workToBeDone() | ||||
| { | ||||
| 	return value != QString(ds->name); | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteName::redo() | ||||
| { | ||||
| 	swap(ds->name, value); | ||||
| 	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::NAME); // Inform frontend of changed dive site.
 | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteName::undo() | ||||
| { | ||||
| 	// Undo and redo do the same
 | ||||
| 	redo(); | ||||
| } | ||||
| 
 | ||||
| EditDiveSiteDescription::EditDiveSiteDescription(dive_site *dsIn, const QString &description) : ds(dsIn), | ||||
| 	value(description) | ||||
| { | ||||
| 	setText(tr("Edit dive site description")); | ||||
| } | ||||
| 
 | ||||
| bool EditDiveSiteDescription::workToBeDone() | ||||
| { | ||||
| 	return value != QString(ds->description); | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteDescription::redo() | ||||
| { | ||||
| 	swap(ds->description, value); | ||||
| 	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::DESCRIPTION); // Inform frontend of changed dive site.
 | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteDescription::undo() | ||||
| { | ||||
| 	// Undo and redo do the same
 | ||||
| 	redo(); | ||||
| } | ||||
| 
 | ||||
| EditDiveSiteNotes::EditDiveSiteNotes(dive_site *dsIn, const QString ¬es) : ds(dsIn), | ||||
| 	value(notes) | ||||
| { | ||||
| 	setText(tr("Edit dive site notes")); | ||||
| } | ||||
| 
 | ||||
| bool EditDiveSiteNotes::workToBeDone() | ||||
| { | ||||
| 	return value != QString(ds->notes); | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteNotes::redo() | ||||
| { | ||||
| 	swap(ds->notes, value); | ||||
| 	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::NOTES); // Inform frontend of changed dive site.
 | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteNotes::undo() | ||||
| { | ||||
| 	// Undo and redo do the same
 | ||||
| 	redo(); | ||||
| } | ||||
| 
 | ||||
| EditDiveSiteCountry::EditDiveSiteCountry(dive_site *dsIn, const QString &country) : ds(dsIn), | ||||
| 	value(country) | ||||
| { | ||||
| 	setText(tr("Edit dive site country")); | ||||
| } | ||||
| 
 | ||||
| bool EditDiveSiteCountry::workToBeDone() | ||||
| { | ||||
| 	return !same_string(qPrintable(value), taxonomy_get_country(&ds->taxonomy)); | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteCountry::redo() | ||||
| { | ||||
| 	QString old = taxonomy_get_country(&ds->taxonomy); | ||||
| 	taxonomy_set_country(&ds->taxonomy, copy_qstring(value), taxonomy_origin::GEOMANUAL); | ||||
| 	value = old; | ||||
| 	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::TAXONOMY); // Inform frontend of changed dive site.
 | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteCountry::undo() | ||||
| { | ||||
| 	// Undo and redo do the same
 | ||||
| 	redo(); | ||||
| } | ||||
| 
 | ||||
| EditDiveSiteLocation::EditDiveSiteLocation(dive_site *dsIn, const location_t location) : ds(dsIn), | ||||
| 	value(location) | ||||
| { | ||||
| 	setText(tr("Edit dive site location")); | ||||
| } | ||||
| 
 | ||||
| bool EditDiveSiteLocation::workToBeDone() | ||||
| { | ||||
| 	bool ok = has_location(&value); | ||||
| 	bool old_ok = has_location(&ds->location); | ||||
| 	if (ok != old_ok) | ||||
| 		return true; | ||||
| 	return ok && !same_location(&value, &ds->location); | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteLocation::redo() | ||||
| { | ||||
| 	std::swap(value, ds->location); | ||||
| 	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::LOCATION); // Inform frontend of changed dive site.
 | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteLocation::undo() | ||||
| { | ||||
| 	// Undo and redo do the same
 | ||||
| 	redo(); | ||||
| } | ||||
| 
 | ||||
| EditDiveSiteTaxonomy::EditDiveSiteTaxonomy(dive_site *dsIn, taxonomy_data &taxonomy) : ds(dsIn), | ||||
| 	value(taxonomy) | ||||
| { | ||||
| 	// We did a dumb copy. Erase the source to remove double references to strings.
 | ||||
| 	memset(&taxonomy, 0, sizeof(taxonomy)); | ||||
| 	setText(tr("Edit dive site taxonomy")); | ||||
| } | ||||
| 
 | ||||
| EditDiveSiteTaxonomy::~EditDiveSiteTaxonomy() | ||||
| { | ||||
| 	free_taxonomy(&value); | ||||
| } | ||||
| 
 | ||||
| bool EditDiveSiteTaxonomy::workToBeDone() | ||||
| { | ||||
| 	// TODO: Apparently we have no way of comparing taxonomies?
 | ||||
| 	return true; | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteTaxonomy::redo() | ||||
| { | ||||
| 	std::swap(value, ds->taxonomy); | ||||
| 	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::TAXONOMY); // Inform frontend of changed dive site.
 | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteTaxonomy::undo() | ||||
| { | ||||
| 	// Undo and redo do the same
 | ||||
| 	redo(); | ||||
| } | ||||
| 
 | ||||
| MergeDiveSites::MergeDiveSites(dive_site *dsIn, const QVector<dive_site *> &sites) : ds(dsIn) | ||||
| { | ||||
| 	setText(tr("merge dive sites")); | ||||
| 	sitesToRemove.reserve(sites.size()); | ||||
| 	for (dive_site *site: sites) { | ||||
| 		if (site != ds) | ||||
| 			sitesToRemove.push_back(site); | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| bool MergeDiveSites::workToBeDone() | ||||
| { | ||||
| 	return !sitesToRemove.empty(); | ||||
| } | ||||
| 
 | ||||
| void MergeDiveSites::redo() | ||||
| { | ||||
| 	// First, remove all dive sites
 | ||||
| 	sitesToAdd = removeDiveSites(sitesToRemove); | ||||
| 
 | ||||
| 	// Remember which dives changed so that we can send a single dives-edited signal
 | ||||
| 	QVector<dive *> divesChanged; | ||||
| 
 | ||||
| 	// The dives of the above dive sites were reset to no dive sites.
 | ||||
| 	// Add them to the merged-into dive site. Thankfully, we remember
 | ||||
| 	// the dives in the sitesToAdd vector.
 | ||||
| 	for (const OwningDiveSitePtr &site: sitesToAdd) { | ||||
| 		for (int i = 0; i < site->dives.nr; ++i) { | ||||
| 			add_dive_to_dive_site(site->dives.dives[i], ds); | ||||
| 			divesChanged.push_back(site->dives.dives[i]); | ||||
| 		} | ||||
| 	} | ||||
| 	emit diveListNotifier.divesChanged(divesChanged, DiveField::DIVESITE); | ||||
| } | ||||
| 
 | ||||
| void MergeDiveSites::undo() | ||||
| { | ||||
| 	// Remember which dives changed so that we can send a single dives-edited signal
 | ||||
| 	QVector<dive *> divesChanged; | ||||
| 
 | ||||
| 	// Before readding the dive sites, unregister the corresponding dives so that they can be
 | ||||
| 	// readded to their old dive sites.
 | ||||
| 	for (const OwningDiveSitePtr &site: sitesToAdd) { | ||||
| 		for (int i = 0; i < site->dives.nr; ++i) { | ||||
| 			unregister_dive_from_dive_site(site->dives.dives[i]); | ||||
| 			divesChanged.push_back(site->dives.dives[i]); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	sitesToRemove = addDiveSites(sitesToAdd); | ||||
| 
 | ||||
| 	emit diveListNotifier.divesChanged(divesChanged, DiveField::DIVESITE); | ||||
| } | ||||
| 
 | ||||
| } // namespace Command
 | ||||
							
								
								
									
										170
									
								
								commands/command_divesite.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										170
									
								
								commands/command_divesite.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,170 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| // Note: this header file is used by the undo-machinery and should not be included elsewhere.
 | ||||
| 
 | ||||
| #ifndef COMMAND_DIVESITE_H | ||||
| #define COMMAND_DIVESITE_H | ||||
| 
 | ||||
| #include "command_base.h" | ||||
| 
 | ||||
| #include <QVector> | ||||
| 
 | ||||
| // We put everything in a namespace, so that we can shorten names without polluting the global namespace
 | ||||
| namespace Command { | ||||
| 
 | ||||
| class AddDiveSite : public Base { | ||||
| public: | ||||
| 	AddDiveSite(const QString &name); | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	// Note: we only add one dive site. Nevertheless, we use vectors so that we
 | ||||
| 	// can reuse the dive site deletion code.
 | ||||
| 	// For undo
 | ||||
| 	std::vector<dive_site *> sitesToRemove; | ||||
| 
 | ||||
| 	// For redo
 | ||||
| 	std::vector<OwningDiveSitePtr> sitesToAdd; | ||||
| }; | ||||
| 
 | ||||
| class ImportDiveSites : public Base { | ||||
| public: | ||||
| 	// Note: the dive site table is consumed after the call it will be empty.
 | ||||
| 	ImportDiveSites(struct dive_site_table *sites, const QString &source); | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	// For undo
 | ||||
| 	std::vector<dive_site *> sitesToRemove; | ||||
| 
 | ||||
| 	// For redo
 | ||||
| 	std::vector<OwningDiveSitePtr> sitesToAdd; | ||||
| }; | ||||
| 
 | ||||
| class DeleteDiveSites : public Base { | ||||
| public: | ||||
| 	DeleteDiveSites(const QVector<dive_site *> &sites); | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	// For redo
 | ||||
| 	std::vector<dive_site *> sitesToRemove; | ||||
| 
 | ||||
| 	// For undo
 | ||||
| 	std::vector<OwningDiveSitePtr> sitesToAdd; | ||||
| }; | ||||
| 
 | ||||
| class PurgeUnusedDiveSites : public Base { | ||||
| public: | ||||
| 	PurgeUnusedDiveSites(); | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	// For redo
 | ||||
| 	std::vector<dive_site *> sitesToRemove; | ||||
| 
 | ||||
| 	// For undo
 | ||||
| 	std::vector<OwningDiveSitePtr> sitesToAdd; | ||||
| }; | ||||
| 
 | ||||
| class EditDiveSiteName : public Base { | ||||
| public: | ||||
| 	EditDiveSiteName(dive_site *ds, const QString &name); | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	dive_site *ds; | ||||
| 	QString value; // Value to be set
 | ||||
| }; | ||||
| 
 | ||||
| class EditDiveSiteDescription : public Base { | ||||
| public: | ||||
| 	EditDiveSiteDescription(dive_site *ds, const QString &description); | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	dive_site *ds; | ||||
| 	QString value; // Value to be set
 | ||||
| }; | ||||
| 
 | ||||
| class EditDiveSiteNotes : public Base { | ||||
| public: | ||||
| 	EditDiveSiteNotes(dive_site *ds, const QString ¬es); | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	dive_site *ds; | ||||
| 	QString value; // Value to be set
 | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| class EditDiveSiteCountry : public Base { | ||||
| public: | ||||
| 	EditDiveSiteCountry(dive_site *ds, const QString &country); | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	dive_site *ds; | ||||
| 	QString value; // Value to be set
 | ||||
| }; | ||||
| 
 | ||||
| class EditDiveSiteLocation : public Base { | ||||
| public: | ||||
| 	EditDiveSiteLocation(dive_site *ds, location_t location); | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	dive_site *ds; | ||||
| 	location_t value; // Value to be set
 | ||||
| }; | ||||
| 
 | ||||
| class EditDiveSiteTaxonomy : public Base { | ||||
| public: | ||||
| 	EditDiveSiteTaxonomy(dive_site *ds, taxonomy_data &taxonomy); | ||||
| 	~EditDiveSiteTaxonomy(); // free taxonomy
 | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	dive_site *ds; | ||||
| 	taxonomy_data value; // Value to be set
 | ||||
| }; | ||||
| 
 | ||||
| class MergeDiveSites : public Base { | ||||
| public: | ||||
| 	MergeDiveSites(dive_site *ds, const QVector<dive_site *> &sites); | ||||
| private: | ||||
| 	bool workToBeDone() override; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	dive_site *ds; | ||||
| 
 | ||||
| 	// For redo
 | ||||
| 	std::vector<dive_site *> sitesToRemove; | ||||
| 
 | ||||
| 	// For undo
 | ||||
| 	std::vector<OwningDiveSitePtr> sitesToAdd; | ||||
| }; | ||||
| 
 | ||||
| } // namespace Command
 | ||||
| 
 | ||||
| #endif // COMMAND_DIVESITE_H
 | ||||
							
								
								
									
										848
									
								
								commands/command_edit.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										848
									
								
								commands/command_edit.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,848 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| 
 | ||||
| #include "command_edit.h" | ||||
| #include "command_private.h" | ||||
| #include "core/divelist.h" | ||||
| #include "core/qthelper.h" // for copy_qstring
 | ||||
| #include "core/subsurface-string.h" | ||||
| #include "core/tag.h" | ||||
| 
 | ||||
| namespace Command { | ||||
| 
 | ||||
| static std::vector<dive *> getDives(bool currentDiveOnly) | ||||
| { | ||||
| 	if (currentDiveOnly) | ||||
| 		return current_dive ? std::vector<dive *> { current_dive } | ||||
| 				    : std::vector<dive *> { }; | ||||
| 
 | ||||
| 	std::vector<dive *> res; | ||||
| 	struct dive *d; | ||||
| 	int i; | ||||
| 	for_each_dive (i, d) { | ||||
| 		if (d->selected) | ||||
| 			res.push_back(d); | ||||
| 	} | ||||
| 	return res; | ||||
| } | ||||
| 
 | ||||
| EditDivesBase::EditDivesBase(bool currentDiveOnly) : | ||||
| 	dives(getDives(currentDiveOnly)), | ||||
| 	selectedDives(getDiveSelection()), | ||||
| 	current(current_dive) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| EditDivesBase::EditDivesBase(dive *d) : | ||||
| 	dives({ d }), | ||||
| 	selectedDives(getDiveSelection()), | ||||
| 	current(current_dive) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| int EditDivesBase::numDives() const | ||||
| { | ||||
| 	return dives.size(); | ||||
| } | ||||
| 
 | ||||
| template<typename T> | ||||
| EditBase<T>::EditBase(T newValue, bool currentDiveOnly) : | ||||
| 	EditDivesBase(currentDiveOnly), | ||||
| 	value(std::move(newValue)) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| template<typename T> | ||||
| EditBase<T>::EditBase(T newValue, dive *d) : | ||||
| 	EditDivesBase(d), | ||||
| 	value(std::move(newValue)) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| // This is quite hackish: we can't use virtual functions in the constructor and
 | ||||
| // therefore can't initialize the list of dives [the values of the dives are
 | ||||
| // accessed by virtual functions]. Therefore, we (mis)use the fact that workToBeDone()
 | ||||
| // is called exactly once before adding the Command to the system and perform this here.
 | ||||
| // To be more explicit about this, we might think about renaming workToBeDone() to init().
 | ||||
| template<typename T> | ||||
| bool EditBase<T>::workToBeDone() | ||||
| { | ||||
| 	// First, let's fetch the old value, i.e. the value of the current dive.
 | ||||
| 	// If no current dive exists, bail.
 | ||||
| 	if (!current) | ||||
| 		return false; | ||||
| 	old = data(current); | ||||
| 
 | ||||
| 	// If there is no change - do nothing.
 | ||||
| 	if (old == value) | ||||
| 		return false; | ||||
| 
 | ||||
| 	std::vector<dive *> divesNew; | ||||
| 	divesNew.reserve(dives.size()); | ||||
| 	for (dive *d: dives) { | ||||
| 		if (data(d) == old) | ||||
| 			divesNew.push_back(d); | ||||
| 	} | ||||
| 	dives = std::move(divesNew); | ||||
| 
 | ||||
| 	// Create a text for the menu entry. In the case of multiple dives add the number
 | ||||
| 	size_t num_dives = dives.size(); | ||||
| 	if (num_dives > 0) | ||||
| 		//: remove the part in parentheses for %n = 1
 | ||||
| 		setText(tr("Edit %1 (%n dive(s))", "", num_dives).arg(fieldName())); | ||||
| 
 | ||||
| 	return num_dives > 0; | ||||
| } | ||||
| 
 | ||||
| template<typename T> | ||||
| void EditBase<T>::undo() | ||||
| { | ||||
| 	if (dives.empty()) { | ||||
| 		qWarning("Edit command called with empty dives list (shouldn't happen)"); | ||||
| 		return; | ||||
| 	} | ||||
| 
 | ||||
| 	for (dive *d: dives) { | ||||
| 		set(d, value); | ||||
| 		invalidate_dive_cache(d); // Ensure that dive is written in git_save()
 | ||||
| 	} | ||||
| 
 | ||||
| 	std::swap(old, value); | ||||
| 
 | ||||
| 	// Send signals.
 | ||||
| 	DiveField id = fieldId(); | ||||
| 	emit diveListNotifier.divesChanged(QVector<dive *>::fromStdVector(dives), id); | ||||
| 
 | ||||
| 	setSelection(selectedDives, current); | ||||
| } | ||||
| 
 | ||||
| // We have to manually instantiate the constructors of the EditBase class,
 | ||||
| // because the base class is never constructed and the derived classes
 | ||||
| // don't have their own constructor. They simply delegate to the base
 | ||||
| // class by virtue of a "using" declaration.
 | ||||
| template | ||||
| EditBase<QString>::EditBase(QString newValue, bool currentDiveOnly); | ||||
| template | ||||
| EditBase<int>::EditBase(int newValue, bool currentDiveOnly); | ||||
| template | ||||
| EditBase<struct dive_site *>::EditBase(struct dive_site *newValue, bool currentDiveOnly); | ||||
| 
 | ||||
| // Undo and redo do the same as just the stored value is exchanged
 | ||||
| template<typename T> | ||||
| void EditBase<T>::redo() | ||||
| { | ||||
| 	// Note: here, we explicitly call the undo function of EditBase<T> and don't do
 | ||||
| 	// virtual dispatch. Thus, derived classes can call this redo function without
 | ||||
| 	// having their own undo() function called.
 | ||||
| 	EditBase<T>::undo(); | ||||
| } | ||||
| 
 | ||||
| // Implementation of virtual functions
 | ||||
| 
 | ||||
| // ***** Notes *****
 | ||||
| void EditNotes::set(struct dive *d, QString s) const | ||||
| { | ||||
| 	free(d->notes); | ||||
| 	d->notes = strdup(qPrintable(s)); | ||||
| } | ||||
| 
 | ||||
| QString EditNotes::data(struct dive *d) const | ||||
| { | ||||
| 	return QString(d->notes); | ||||
| } | ||||
| 
 | ||||
| QString EditNotes::fieldName() const | ||||
| { | ||||
| 	return tr("notes"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditNotes::fieldId() const | ||||
| { | ||||
| 	return DiveField::NOTES; | ||||
| } | ||||
| 
 | ||||
| // ***** Suit *****
 | ||||
| void EditSuit::set(struct dive *d, QString s) const | ||||
| { | ||||
| 	free(d->suit); | ||||
| 	d->suit = strdup(qPrintable(s)); | ||||
| } | ||||
| 
 | ||||
| QString EditSuit::data(struct dive *d) const | ||||
| { | ||||
| 	return QString(d->suit); | ||||
| } | ||||
| 
 | ||||
| QString EditSuit::fieldName() const | ||||
| { | ||||
| 	return tr("suit"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditSuit::fieldId() const | ||||
| { | ||||
| 	return DiveField::SUIT; | ||||
| } | ||||
| 
 | ||||
| // ***** Rating *****
 | ||||
| void EditRating::set(struct dive *d, int value) const | ||||
| { | ||||
| 	d->rating = value; | ||||
| } | ||||
| 
 | ||||
| int EditRating::data(struct dive *d) const | ||||
| { | ||||
| 	return d->rating; | ||||
| } | ||||
| 
 | ||||
| QString EditRating::fieldName() const | ||||
| { | ||||
| 	return tr("rating"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditRating::fieldId() const | ||||
| { | ||||
| 	return DiveField::RATING; | ||||
| } | ||||
| 
 | ||||
| // ***** Visibility *****
 | ||||
| void EditVisibility::set(struct dive *d, int value) const | ||||
| { | ||||
| 	d->visibility = value; | ||||
| } | ||||
| 
 | ||||
| int EditVisibility::data(struct dive *d) const | ||||
| { | ||||
| 	return d->visibility; | ||||
| } | ||||
| 
 | ||||
| QString EditVisibility::fieldName() const | ||||
| { | ||||
| 	return tr("visibility"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditVisibility::fieldId() const | ||||
| { | ||||
| 	return DiveField::VISIBILITY; | ||||
| } | ||||
| 
 | ||||
| // ***** Air Temperature *****
 | ||||
| void EditAirTemp::set(struct dive *d, int value) const | ||||
| { | ||||
| 	d->airtemp.mkelvin = value > 0 ? (uint32_t)value : 0u; | ||||
| } | ||||
| 
 | ||||
| int EditAirTemp::data(struct dive *d) const | ||||
| { | ||||
| 	return (int)d->airtemp.mkelvin; | ||||
| } | ||||
| 
 | ||||
| QString EditAirTemp::fieldName() const | ||||
| { | ||||
| 	return tr("air temperature"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditAirTemp::fieldId() const | ||||
| { | ||||
| 	return DiveField::AIR_TEMP; | ||||
| } | ||||
| 
 | ||||
| // ***** Water Temperature *****
 | ||||
| void EditWaterTemp::set(struct dive *d, int value) const | ||||
| { | ||||
| 	d->watertemp.mkelvin = value > 0 ? (uint32_t)value : 0u; | ||||
| 
 | ||||
| 	// re-populate the temperatures - easiest way to do this is by calling fixup_dive
 | ||||
| 	fixup_dive(d); | ||||
| } | ||||
| 
 | ||||
| int EditWaterTemp::data(struct dive *d) const | ||||
| { | ||||
| 	return (int)d->watertemp.mkelvin; | ||||
| } | ||||
| 
 | ||||
| QString EditWaterTemp::fieldName() const | ||||
| { | ||||
| 	return tr("water temperature"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditWaterTemp::fieldId() const | ||||
| { | ||||
| 	return DiveField::WATER_TEMP; | ||||
| } | ||||
| 
 | ||||
| // ***** Atmospheric pressure *****
 | ||||
| void EditAtmPress::set(struct dive *d, int value) const | ||||
| { | ||||
| 	d->surface_pressure.mbar = value > 0 ? (uint32_t)value : 0u; | ||||
| } | ||||
| 
 | ||||
| int EditAtmPress::data(struct dive *d) const | ||||
| { | ||||
| 	return (int)d->surface_pressure.mbar; | ||||
| } | ||||
| 
 | ||||
| QString EditAtmPress::fieldName() const | ||||
| { | ||||
| 	return tr("Atm. pressure"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditAtmPress::fieldId() const | ||||
| { | ||||
| 	return DiveField::ATM_PRESS; | ||||
| } | ||||
| 
 | ||||
| // ***** Duration *****
 | ||||
| void EditDuration::set(struct dive *d, int value) const | ||||
| { | ||||
| 	d->dc.duration.seconds = value; | ||||
| 	d->duration = d->dc.duration; | ||||
| 	d->dc.meandepth.mm = 0; | ||||
| 	d->dc.samples = 0; | ||||
| } | ||||
| 
 | ||||
| int EditDuration::data(struct dive *d) const | ||||
| { | ||||
| 	return d->duration.seconds; | ||||
| } | ||||
| 
 | ||||
| QString EditDuration::fieldName() const | ||||
| { | ||||
| 	return tr("duration"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditDuration::fieldId() const | ||||
| { | ||||
| 	return DiveField::DURATION; | ||||
| } | ||||
| 
 | ||||
| // ***** Depth *****
 | ||||
| void EditDepth::set(struct dive *d, int value) const | ||||
| { | ||||
| 	d->dc.maxdepth.mm = value; | ||||
| 	d->maxdepth = d->dc.maxdepth; | ||||
| 	d->dc.meandepth.mm = 0; | ||||
| 	d->dc.samples = 0; | ||||
| } | ||||
| 
 | ||||
| int EditDepth::data(struct dive *d) const | ||||
| { | ||||
| 	return d->maxdepth.mm; | ||||
| } | ||||
| 
 | ||||
| QString EditDepth::fieldName() const | ||||
| { | ||||
| 	return tr("depth"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditDepth::fieldId() const | ||||
| { | ||||
| 	return DiveField::DEPTH; | ||||
| } | ||||
| 
 | ||||
| // ***** DiveSite *****
 | ||||
| void EditDiveSite::set(struct dive *d, struct dive_site *dive_site) const | ||||
| { | ||||
| 	unregister_dive_from_dive_site(d); | ||||
| 	add_dive_to_dive_site(d, dive_site); | ||||
| } | ||||
| 
 | ||||
| struct dive_site *EditDiveSite::data(struct dive *d) const | ||||
| { | ||||
| 	return d->dive_site; | ||||
| } | ||||
| 
 | ||||
| QString EditDiveSite::fieldName() const | ||||
| { | ||||
| 	return tr("dive site"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditDiveSite::fieldId() const | ||||
| { | ||||
| 	return DiveField::DIVESITE; | ||||
| } | ||||
| 
 | ||||
| void EditDiveSite::undo() | ||||
| { | ||||
| 	// Do the normal undo thing, then send dive site changed signals
 | ||||
| 	EditBase<dive_site *>::undo(); | ||||
| 	if (value) | ||||
| 		emit diveListNotifier.diveSiteDivesChanged(value); | ||||
| 	if (old) | ||||
| 		emit diveListNotifier.diveSiteDivesChanged(old); | ||||
| } | ||||
| 
 | ||||
| void EditDiveSite::redo() | ||||
| { | ||||
| 	EditDiveSite::undo(); // Undo and redo do the same
 | ||||
| } | ||||
| 
 | ||||
| static struct dive_site *createDiveSite(const QString &name) | ||||
| { | ||||
| 	struct dive_site *ds = alloc_dive_site(); | ||||
| 	struct dive_site *old = current_dive ? current_dive->dive_site : nullptr; | ||||
| 	if (old) { | ||||
| 		copy_dive_site(old, ds); | ||||
| 		free(ds->name); // Free name, as we will overwrite it with our own version
 | ||||
| 	} | ||||
| 
 | ||||
| 	// If the current dive has a location, use that as location for the new dive site
 | ||||
| 	if (current_dive) { | ||||
| 		location_t loc = dive_get_gps_location(current_dive); | ||||
| 		if (has_location(&loc)) | ||||
| 			ds->location = loc; | ||||
| 	} | ||||
| 
 | ||||
| 	ds->name = copy_qstring(name); | ||||
| 	return ds; | ||||
| } | ||||
| 
 | ||||
| EditDiveSiteNew::EditDiveSiteNew(const QString &newName, bool currentDiveOnly) : | ||||
| 	EditDiveSite(createDiveSite(newName), currentDiveOnly), | ||||
| 	diveSiteToAdd(value), | ||||
| 	diveSiteToRemove(nullptr) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteNew::undo() | ||||
| { | ||||
| 	EditDiveSite::undo(); | ||||
| 	int idx = unregister_dive_site(diveSiteToRemove); | ||||
| 	diveSiteToAdd.reset(diveSiteToRemove); | ||||
| 	emit diveListNotifier.diveSiteDeleted(diveSiteToRemove, idx); // Inform frontend of removed dive site.
 | ||||
| 	diveSiteToRemove = nullptr; | ||||
| } | ||||
| 
 | ||||
| void EditDiveSiteNew::redo() | ||||
| { | ||||
| 	diveSiteToRemove = diveSiteToAdd.get(); | ||||
| 	int idx = register_dive_site(diveSiteToAdd.release()); // Return ownership to backend.
 | ||||
| 	emit diveListNotifier.diveSiteAdded(diveSiteToRemove, idx); // Inform frontend of new dive site.
 | ||||
| 	EditDiveSite::redo(); | ||||
| } | ||||
| 
 | ||||
| // ***** Mode *****
 | ||||
| // Editing the dive mode has very peculiar semantics for historic reasons:
 | ||||
| // Since the dive-mode depends on the dive computer, the i-th dive computer
 | ||||
| // of each dive will be edited. If the dive has less than i dive computers,
 | ||||
| // the default dive computer will be edited.
 | ||||
| // The index "i" will be stored as an additional payload with the command.
 | ||||
| // Thus, we need an explicit constructor. Since the actual handling is done
 | ||||
| // by the base class, which knows nothing about this index, it will not be
 | ||||
| // sent via signals. Currently this is not needed. Should it turn out to
 | ||||
| // become necessary, then we might either
 | ||||
| //	- Not derive EditMode from EditBase.
 | ||||
| //	- Change the semantics of the mode-editing.
 | ||||
| // The future will tell.
 | ||||
| EditMode::EditMode(int indexIn, int newValue, bool currentDiveOnly) | ||||
| 	: EditBase(newValue, currentDiveOnly), index(indexIn) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| void EditMode::set(struct dive *d, int i) const | ||||
| { | ||||
| 	get_dive_dc(d, index)->divemode = (enum divemode_t)i; | ||||
| 	update_setpoint_events(d, get_dive_dc(d, index)); | ||||
| } | ||||
| 
 | ||||
| int EditMode::data(struct dive *d) const | ||||
| { | ||||
| 	return get_dive_dc(d, index)->divemode; | ||||
| } | ||||
| 
 | ||||
| QString EditMode::fieldName() const | ||||
| { | ||||
| 	return tr("dive mode"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditMode::fieldId() const | ||||
| { | ||||
| 	return DiveField::MODE; | ||||
| } | ||||
| 
 | ||||
| // ***** Number *****
 | ||||
| void EditNumber::set(struct dive *d, int number) const | ||||
| { | ||||
| 	d->number = number; | ||||
| } | ||||
| 
 | ||||
| int EditNumber::data(struct dive *d) const | ||||
| { | ||||
| 	return d->number; | ||||
| } | ||||
| 
 | ||||
| QString EditNumber::fieldName() const | ||||
| { | ||||
| 	return tr("number"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditNumber::fieldId() const | ||||
| { | ||||
| 	return DiveField::NR; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| // ***** Tag based commands *****
 | ||||
| EditTagsBase::EditTagsBase(const QStringList &newListIn, bool currentDiveOnly) : | ||||
| 	EditDivesBase(currentDiveOnly), | ||||
| 	newList(newListIn) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| // Two helper functions: returns true if first list contains any tag or
 | ||||
| // misses any tag of second list.
 | ||||
| static bool containsAny(const QStringList &tags1, const QStringList &tags2) | ||||
| { | ||||
| 	return std::any_of(tags2.begin(), tags2.end(), [&tags1](const QString &tag) | ||||
| 			   { return tags1.contains(tag); }); | ||||
| } | ||||
| 
 | ||||
| static bool missesAny(const QStringList &tags1, const QStringList &tags2) | ||||
| { | ||||
| 	return std::any_of(tags2.begin(), tags2.end(), [&tags1](const QString &tag) | ||||
| 			   { return !tags1.contains(tag); }); | ||||
| } | ||||
| 
 | ||||
| // This is quite hackish: we can't use virtual functions in the constructor and
 | ||||
| // therefore can't initialize the list of dives [the values of the dives are
 | ||||
| // accessed by virtual functions]. Therefore, we (mis)use the fact that workToBeDone()
 | ||||
| // is called exactly once before adding the Command to the system and perform this here.
 | ||||
| // To be more explicit about this, we might think about renaming workToBeDone() to init().
 | ||||
| bool EditTagsBase::workToBeDone() | ||||
| { | ||||
| 	// changing the tags on multiple dives is semantically strange - what's the right thing to do?
 | ||||
| 	// here's what I think... add the tags that were added to the displayed dive and remove the tags
 | ||||
| 	// that were removed from it
 | ||||
| 
 | ||||
| 	// If there is no current dive, bail.
 | ||||
| 	if (!current) | ||||
| 		return false; | ||||
| 
 | ||||
| 	// Calculate tags to add and tags to remove
 | ||||
| 	QStringList oldList = data(current); | ||||
| 	for (const QString &s: newList) { | ||||
| 		if (!oldList.contains(s)) | ||||
| 			tagsToAdd.push_back(s); | ||||
| 	} | ||||
| 	for (const QString &s: oldList) { | ||||
| 		if (!newList.contains(s)) | ||||
| 			tagsToRemove.push_back(s); | ||||
| 	} | ||||
| 
 | ||||
| 	// Now search for all dives that either
 | ||||
| 	//	- miss a tag to be added
 | ||||
| 	//	- have a tag to be removed
 | ||||
| 	std::vector<dive *> divesNew; | ||||
| 	divesNew.reserve(dives.size()); | ||||
| 	for (dive *d: dives) { | ||||
| 		QStringList tags = data(d); | ||||
| 		if (missesAny(tags, tagsToAdd) || containsAny(tags, tagsToRemove)) | ||||
| 			divesNew.push_back(d); | ||||
| 	} | ||||
| 	dives = std::move(divesNew); | ||||
| 
 | ||||
| 	// Create a text for the menu entry. In the case of multiple dives add the number
 | ||||
| 	size_t num_dives = dives.size(); | ||||
| 	if (num_dives > 0) | ||||
| 		//: remove the part in parentheses for %n = 1
 | ||||
| 		setText(tr("Edit %1 (%n dive(s))", "", num_dives).arg(fieldName())); | ||||
| 
 | ||||
| 	return num_dives != 0; | ||||
| } | ||||
| 
 | ||||
| void EditTagsBase::undo() | ||||
| { | ||||
| 	if (dives.empty()) { | ||||
| 		qWarning("Edit command called with empty dives list (shouldn't happen)"); | ||||
| 		return; | ||||
| 	} | ||||
| 
 | ||||
| 	for (dive *d: dives) { | ||||
| 		QStringList tags = data(d); | ||||
| 		for (const QString &tag: tagsToRemove) | ||||
| 			tags.removeAll(tag); | ||||
| 		for (const QString &tag: tagsToAdd) { | ||||
| 			if (!tags.contains(tag)) | ||||
| 				tags.push_back(tag); | ||||
| 		} | ||||
| 		invalidate_dive_cache(d); // Ensure that dive is written in git_save()
 | ||||
| 		set(d, tags); | ||||
| 	} | ||||
| 
 | ||||
| 	std::swap(tagsToAdd, tagsToRemove); | ||||
| 
 | ||||
| 	// Send signals.
 | ||||
| 	DiveField id = fieldId(); | ||||
| 	emit diveListNotifier.divesChanged(QVector<dive *>::fromStdVector(dives), id); | ||||
| 
 | ||||
| 	setSelection(selectedDives, current); | ||||
| } | ||||
| 
 | ||||
| // Undo and redo do the same as just the stored value is exchanged
 | ||||
| void EditTagsBase::redo() | ||||
| { | ||||
| 	undo(); | ||||
| } | ||||
| 
 | ||||
| // ***** Tags *****
 | ||||
| QStringList EditTags::data(struct dive *d) const | ||||
| { | ||||
| 	QStringList res; | ||||
| 	for (const struct tag_entry *tag = d->tag_list; tag; tag = tag->next) | ||||
| 		res.push_back(tag->tag->name); | ||||
| 	return res; | ||||
| } | ||||
| 
 | ||||
| void EditTags::set(struct dive *d, const QStringList &v) const | ||||
| { | ||||
| 	taglist_free(d->tag_list); | ||||
| 	d->tag_list = NULL; | ||||
| 	for (const QString &tag: v) | ||||
| 		taglist_add_tag(&d->tag_list, qPrintable(tag)); | ||||
| 	taglist_cleanup(&d->tag_list); | ||||
| } | ||||
| 
 | ||||
| QString EditTags::fieldName() const | ||||
| { | ||||
| 	return tr("tags"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditTags::fieldId() const | ||||
| { | ||||
| 	return DiveField::TAGS; | ||||
| } | ||||
| 
 | ||||
| // String list helper
 | ||||
| static QStringList stringToList(const QString &s) | ||||
| { | ||||
| 	QStringList res = s.split(",", QString::SkipEmptyParts); | ||||
| 	for (QString &str: res) | ||||
| 		str = str.trimmed(); | ||||
| 	return res; | ||||
| } | ||||
| 
 | ||||
| // ***** Buddies *****
 | ||||
| QStringList EditBuddies::data(struct dive *d) const | ||||
| { | ||||
| 	return stringToList(d->buddy); | ||||
| } | ||||
| 
 | ||||
| void EditBuddies::set(struct dive *d, const QStringList &v) const | ||||
| { | ||||
| 	QString text = v.join(", "); | ||||
| 	free(d->buddy); | ||||
| 	d->buddy = copy_qstring(text); | ||||
| } | ||||
| 
 | ||||
| QString EditBuddies::fieldName() const | ||||
| { | ||||
| 	return tr("buddies"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditBuddies::fieldId() const | ||||
| { | ||||
| 	return DiveField::BUDDY; | ||||
| } | ||||
| 
 | ||||
| // ***** DiveMaster *****
 | ||||
| QStringList EditDiveMaster::data(struct dive *d) const | ||||
| { | ||||
| 	return stringToList(d->divemaster); | ||||
| } | ||||
| 
 | ||||
| void EditDiveMaster::set(struct dive *d, const QStringList &v) const | ||||
| { | ||||
| 	QString text = v.join(", "); | ||||
| 	free(d->divemaster); | ||||
| 	d->divemaster = copy_qstring(text); | ||||
| } | ||||
| 
 | ||||
| QString EditDiveMaster::fieldName() const | ||||
| { | ||||
| 	return tr("dive master"); | ||||
| } | ||||
| 
 | ||||
| DiveField EditDiveMaster::fieldId() const | ||||
| { | ||||
| 	return DiveField::DIVEMASTER; | ||||
| } | ||||
| 
 | ||||
| static void swapCandQString(QString &q, char *&c) | ||||
| { | ||||
| 	QString tmp(c); | ||||
| 	free(c); | ||||
| 	c = copy_qstring(q); | ||||
| 	q = std::move(tmp); | ||||
| } | ||||
| 
 | ||||
| PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dIn), | ||||
| 	tags(nullptr) | ||||
| { | ||||
| 	memset(&cylinders, 0, sizeof(cylinders)); | ||||
| 	memset(&weightsystems, 0, sizeof(weightsystems)); | ||||
| 	if (what.notes) | ||||
| 		notes = data->notes; | ||||
| 	if (what.divemaster) | ||||
| 		divemaster = data->divemaster; | ||||
| 	if (what.buddy) | ||||
| 		buddy = data->buddy; | ||||
| 	if (what.suit) | ||||
| 		suit = data->suit; | ||||
| 	if (what.rating) | ||||
| 		rating = data->rating; | ||||
| 	if (what.visibility) | ||||
| 		visibility = data->visibility; | ||||
| 	if (what.divesite) | ||||
| 		divesite = data->dive_site; | ||||
| 	if (what.tags) | ||||
| 		tags = taglist_copy(data->tag_list); | ||||
| 	if (what.cylinders) | ||||
| 		copy_cylinders(&data->cylinders, &cylinders); | ||||
| 	if (what.weights) | ||||
| 		copy_weights(&data->weightsystems, &weightsystems); | ||||
| } | ||||
| 
 | ||||
| PasteState::~PasteState() | ||||
| { | ||||
| 	taglist_free(tags); | ||||
| 	clear_cylinder_table(&cylinders); | ||||
| 	clear_weightsystem_table(&weightsystems); | ||||
| 	free(weightsystems.weightsystems); | ||||
| } | ||||
| 
 | ||||
| void PasteState::swap(dive_components what) | ||||
| { | ||||
| 	if (what.notes) | ||||
| 		swapCandQString(notes, d->notes); | ||||
| 	if (what.divemaster) | ||||
| 		swapCandQString(divemaster, d->divemaster); | ||||
| 	if (what.buddy) | ||||
| 		swapCandQString(buddy, d->buddy); | ||||
| 	if (what.suit) | ||||
| 		swapCandQString(suit, d->suit); | ||||
| 	if (what.rating) | ||||
| 		std::swap(rating, d->rating); | ||||
| 	if (what.visibility) | ||||
| 		std::swap(visibility, d->visibility); | ||||
| 	if (what.divesite) | ||||
| 		std::swap(divesite, d->dive_site); | ||||
| 	if (what.tags) | ||||
| 		std::swap(tags, d->tag_list); | ||||
| 	if (what.cylinders) | ||||
| 		std::swap(cylinders, d->cylinders); | ||||
| 	if (what.weights) | ||||
| 		std::swap(weightsystems, d->weightsystems); | ||||
| } | ||||
| 
 | ||||
| // ***** Paste *****
 | ||||
| PasteDives::PasteDives(const dive *data, dive_components whatIn) : what(whatIn), | ||||
| 	current(current_dive) | ||||
| { | ||||
| 	std::vector<dive *> selection = getDiveSelection(); | ||||
| 	dives.reserve(selection.size()); | ||||
| 	for (dive *d: selection) | ||||
| 		dives.emplace_back(d, data, what); | ||||
| 
 | ||||
| 	setText(tr("Paste onto %n dive(s)", "", dives.size())); | ||||
| } | ||||
| 
 | ||||
| bool PasteDives::workToBeDone() | ||||
| { | ||||
| 	return !dives.empty(); | ||||
| } | ||||
| 
 | ||||
| void PasteDives::undo() | ||||
| { | ||||
| 	QVector<dive *> divesToNotify; // Remember dives so that we can send signals later
 | ||||
| 	divesToNotify.reserve(dives.size()); | ||||
| 	for (PasteState &state: dives) { | ||||
| 		divesToNotify.push_back(state.d); | ||||
| 		state.swap(what); | ||||
| 		invalidate_dive_cache(state.d); // Ensure that dive is written in git_save()
 | ||||
| 	} | ||||
| 
 | ||||
| 	// Send signals.
 | ||||
| 	DiveField fields(DiveField::NONE); | ||||
| 	fields.notes = what.notes; | ||||
| 	fields.divemaster = what.divemaster; | ||||
| 	fields.buddy = what.buddy; | ||||
| 	fields.suit = what.suit; | ||||
| 	fields.rating = what.rating; | ||||
| 	fields.visibility = what.visibility; | ||||
| 	fields.divesite = what.divesite; | ||||
| 	fields.tags = what.tags; | ||||
| 	emit diveListNotifier.divesChanged(divesToNotify, fields); | ||||
| 	if (what.cylinders) | ||||
| 		emit diveListNotifier.cylindersReset(divesToNotify); | ||||
| 	if (what.weights) | ||||
| 		emit diveListNotifier.weightsystemsReset(divesToNotify); | ||||
| } | ||||
| 
 | ||||
| // Redo and undo do the same
 | ||||
| void PasteDives::redo() | ||||
| { | ||||
| 	undo(); | ||||
| } | ||||
| 
 | ||||
| // ***** Paste *****
 | ||||
| ReplanDive::ReplanDive(dive *source) : d(current_dive), | ||||
| 	dc({ 0 }), | ||||
| 	notes(nullptr) | ||||
| { | ||||
| 	memset(&cylinders, 0, sizeof(cylinders)); | ||||
| 	if (!d) | ||||
| 		return; | ||||
| 
 | ||||
| 	when = source->when; | ||||
| 	maxdepth = source->maxdepth; | ||||
| 	meandepth = source->meandepth; | ||||
| 	notes = copy_string(source->notes); | ||||
| 	duration = source->duration; | ||||
| 	salinity = source->salinity; | ||||
| 	surface_pressure = source->surface_pressure; | ||||
| 
 | ||||
| 	// This resets the dive computers and cylinders of the source dive, avoiding deep copies.
 | ||||
| 	std::swap(source->cylinders, cylinders); | ||||
| 	std::swap(source->dc, dc); | ||||
| 
 | ||||
| 	setText(tr("Replan dive")); | ||||
| } | ||||
| 
 | ||||
| ReplanDive::~ReplanDive() | ||||
| { | ||||
| 	clear_cylinder_table(&cylinders); | ||||
| 	free_dive_dcs(&dc); | ||||
| 	free(notes); | ||||
| } | ||||
| 
 | ||||
| bool ReplanDive::workToBeDone() | ||||
| { | ||||
| 	return !!d; | ||||
| } | ||||
| 
 | ||||
| void ReplanDive::undo() | ||||
| { | ||||
| 	std::swap(d->when, when); | ||||
| 	std::swap(d->maxdepth, maxdepth); | ||||
| 	std::swap(d->meandepth, meandepth); | ||||
| 	std::swap(d->cylinders, cylinders); | ||||
| 	std::swap(d->dc, dc); | ||||
| 	std::swap(d->notes, notes); | ||||
| 	std::swap(d->surface_pressure, surface_pressure); | ||||
| 	std::swap(d->duration, duration); | ||||
| 	std::swap(d->salinity, salinity); | ||||
| 	fixup_dive(d); | ||||
| 
 | ||||
| 	QVector<dive *> divesToNotify = { d }; | ||||
| 	// Note that we have to emit cylindersReset before divesChanged, because the divesChanged
 | ||||
| 	// updates the DivePlotDataModel, which is out-of-sync and gets confused.
 | ||||
| 	emit diveListNotifier.cylindersReset(divesToNotify); | ||||
| 	emit diveListNotifier.divesChanged(divesToNotify, DiveField::DATETIME | DiveField::DURATION | DiveField::DEPTH | DiveField::MODE | | ||||
| 							  DiveField::NOTES | DiveField::SALINITY | DiveField::ATM_PRESS); | ||||
| } | ||||
| 
 | ||||
| // Redo and undo do the same
 | ||||
| void ReplanDive::redo() | ||||
| { | ||||
| 	undo(); | ||||
| } | ||||
| 
 | ||||
| } // namespace Command
 | ||||
							
								
								
									
										292
									
								
								commands/command_edit.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										292
									
								
								commands/command_edit.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,292 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| // Note: this header file is used by the undo-machinery and should not be included elsewhere.
 | ||||
| 
 | ||||
| #ifndef COMMAND_EDIT_H | ||||
| #define COMMAND_EDIT_H | ||||
| 
 | ||||
| #include "command_base.h" | ||||
| #include "core/subsurface-qt/DiveListNotifier.h" | ||||
| 
 | ||||
| #include <QVector> | ||||
| 
 | ||||
| // These are commands that edit individual fields of a set of dives.
 | ||||
| // The implementation is very OO-style. Out-of-fashion and certainly
 | ||||
| // not elegant, but in line with Qt's OO-based design.
 | ||||
| // The actual code is in a common base class "Command::EditBase". To
 | ||||
| // read and set the fields, the base class calls virtual functions of
 | ||||
| // the derived classes.
 | ||||
| //
 | ||||
| // To deal with different data types, the base class is implemented
 | ||||
| // as a template. The template parameter is the type to be read or
 | ||||
| // set. Thus, switch-cascades and union trickery can be avoided.
 | ||||
| 
 | ||||
| // We put everything in a namespace, so that we can shorten names without polluting the global namespace
 | ||||
| namespace Command { | ||||
| 
 | ||||
| // Base class for commands that have a list of dives.
 | ||||
| // This is used for extracting the number of dives and show a
 | ||||
| // warning message when multiple dives are edited.
 | ||||
| class EditDivesBase : public Base { | ||||
| protected: | ||||
| 	EditDivesBase(bool currentDiveOnly); | ||||
| 	EditDivesBase(dive *d); | ||||
| 	std::vector<dive *> dives; // Dives to be edited.
 | ||||
| 
 | ||||
| 	// On undo, we set the selection and current dive at the time of the operation.
 | ||||
| 	std::vector<dive *> selectedDives; | ||||
| 	struct dive *current; | ||||
| public: | ||||
| 	int numDives() const; | ||||
| }; | ||||
| 
 | ||||
| template <typename T> | ||||
| class EditBase : public EditDivesBase { | ||||
| protected: | ||||
| 	T value; // Value to be set
 | ||||
| 	T old; // Previous value
 | ||||
| 
 | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| public: | ||||
| 	EditBase(T newValue, bool currentDiveOnly); | ||||
| 	EditBase(T newValue, dive *d); | ||||
| 
 | ||||
| protected: | ||||
| 	// Get and set functions to be overriden by sub-classes.
 | ||||
| 	virtual void set(struct dive *d, T) const = 0; | ||||
| 	virtual T data(struct dive *d) const = 0; | ||||
| 	virtual QString fieldName() const = 0;	// Name of the field, used to create the undo menu-entry
 | ||||
| 	virtual DiveField fieldId() const = 0; | ||||
| }; | ||||
| 
 | ||||
| class EditNotes : public EditBase<QString> { | ||||
| public: | ||||
| 	using EditBase<QString>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, QString s) const override; | ||||
| 	QString data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditSuit : public EditBase<QString> { | ||||
| public: | ||||
| 	using EditBase<QString>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, QString s) const override; | ||||
| 	QString data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditRating : public EditBase<int> { | ||||
| public: | ||||
| 	using EditBase<int>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, int value) const override; | ||||
| 	int data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditVisibility : public EditBase<int> { | ||||
| public: | ||||
| 	using EditBase<int>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, int value) const override; | ||||
| 	int data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditAirTemp : public EditBase<int> { | ||||
| public: | ||||
| 	using EditBase<int>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, int value) const override; | ||||
| 	int data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditWaterTemp : public EditBase<int> { | ||||
| public: | ||||
| 	using EditBase<int>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, int value) const override; | ||||
| 	int data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditAtmPress : public EditBase<int> { | ||||
| public: | ||||
| 	using EditBase<int>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, int value) const override; | ||||
| 	int data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditDuration : public EditBase<int> { | ||||
| public: | ||||
| 	using EditBase<int>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, int value) const override; | ||||
| 	int data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditDepth : public EditBase<int> { | ||||
| public: | ||||
| 	using EditBase<int>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, int value) const override; | ||||
| 	int data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditDiveSite : public EditBase<struct dive_site *> { | ||||
| public: | ||||
| 	using EditBase<struct dive_site *>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, struct dive_site *value) const override; | ||||
| 	struct dive_site *data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| 
 | ||||
| 	// We specialize these so that we can send dive-site changed signals.
 | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| }; | ||||
| 
 | ||||
| // Edit dive site, but add a new dive site first. Reuses the code of EditDiveSite by
 | ||||
| // deriving from it and hooks into undo() and redo() to add / remove the dive site.
 | ||||
| class EditDiveSiteNew : public EditDiveSite { | ||||
| public: | ||||
| 	OwningDiveSitePtr diveSiteToAdd; | ||||
| 	struct dive_site *diveSiteToRemove; | ||||
| 	EditDiveSiteNew(const QString &newName, bool currentDiveOnly); | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| }; | ||||
| 
 | ||||
| class EditMode : public EditBase<int> { | ||||
| 	int index; | ||||
| public: | ||||
| 	EditMode(int indexIn, int newValue, bool currentDiveOnly); | ||||
| 	void set(struct dive *d, int i) const override; | ||||
| 	int data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditNumber : public EditBase<int> { | ||||
| public: | ||||
| 	using EditBase<int>::EditBase;	// Use constructor of base class.
 | ||||
| 	void set(struct dive *d, int number) const override; | ||||
| 	int data(struct dive *d) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| // Fields that work with tag-lists (tags, buddies, divemasters) work differently and therefore
 | ||||
| // have their own base class. In this case, it's not a template, as all these lists are base
 | ||||
| // on strings.
 | ||||
| class EditTagsBase : public EditDivesBase { | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| 	QStringList newList;	// Temporary until initialized
 | ||||
| public: | ||||
| 	EditTagsBase(const QStringList &newList, bool currentDiveOnly); | ||||
| 
 | ||||
| protected: | ||||
| 	QStringList tagsToAdd; | ||||
| 	QStringList tagsToRemove; | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	// Getters, setters and parsers to be overriden by sub-classes.
 | ||||
| 	virtual QStringList data(struct dive *d) const = 0; | ||||
| 	virtual void set(struct dive *d, const QStringList &v) const = 0; | ||||
| 	virtual QString fieldName() const = 0;	// Name of the field, used to create the undo menu-entry
 | ||||
| 	virtual DiveField fieldId() const = 0; | ||||
| }; | ||||
| 
 | ||||
| class EditTags : public EditTagsBase { | ||||
| public: | ||||
| 	using EditTagsBase::EditTagsBase;	// Use constructor of base class.
 | ||||
| 	QStringList data(struct dive *d) const override; | ||||
| 	void set(struct dive *d, const QStringList &v) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditBuddies : public EditTagsBase { | ||||
| public: | ||||
| 	using EditTagsBase::EditTagsBase;	// Use constructor of base class.
 | ||||
| 	QStringList data(struct dive *d) const override; | ||||
| 	void set(struct dive *d, const QStringList &v) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditDiveMaster : public EditTagsBase { | ||||
| public: | ||||
| 	using EditTagsBase::EditTagsBase;	// Use constructor of base class.
 | ||||
| 	QStringList data(struct dive *d) const override; | ||||
| 	void set(struct dive *d, const QStringList &v) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	DiveField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| // Fields we have to remember to undo paste
 | ||||
| struct PasteState { | ||||
| 	dive *d; | ||||
| 	dive_site *divesite; | ||||
| 	QString notes; | ||||
| 	QString divemaster; | ||||
| 	QString buddy; | ||||
| 	QString suit; | ||||
| 	int rating; | ||||
| 	int visibility; | ||||
| 	tag_entry *tags; | ||||
| 	struct cylinder_table cylinders; | ||||
| 	struct weightsystem_table weightsystems; | ||||
| 
 | ||||
| 	PasteState(dive *d, const dive *data, dive_components what); // Read data from dive data for dive d
 | ||||
| 	~PasteState(); | ||||
| 	void swap(dive_components what); // Exchange values here and in dive
 | ||||
| }; | ||||
| 
 | ||||
| class PasteDives : public Base { | ||||
| 	dive_components what; | ||||
| 	std::vector<PasteState> dives; | ||||
| 	dive *current; | ||||
| public: | ||||
| 	PasteDives(const dive *d, dive_components what); | ||||
| private: | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 	bool workToBeDone() override; | ||||
| }; | ||||
| 
 | ||||
| class ReplanDive : public Base { | ||||
| 	dive *d; | ||||
| 
 | ||||
| 	// Exchange these data with current dive
 | ||||
| 	timestamp_t when; | ||||
| 	depth_t maxdepth, meandepth; | ||||
| 	struct cylinder_table cylinders; | ||||
| 	struct divecomputer dc; | ||||
| 	char *notes; | ||||
| 	pressure_t surface_pressure; | ||||
| 	duration_t duration; | ||||
| 	int salinity; | ||||
| public: | ||||
| 	ReplanDive(dive *source); // Dive computer(s) and cylinders(s) of the source dive will be reset!
 | ||||
| 	~ReplanDive(); | ||||
| private: | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 	bool workToBeDone() override; | ||||
| }; | ||||
| 
 | ||||
| } // namespace Command
 | ||||
| 
 | ||||
| #endif | ||||
							
								
								
									
										83
									
								
								commands/command_edit_trip.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								commands/command_edit_trip.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,83 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| 
 | ||||
| #include "command_edit_trip.h" | ||||
| #include "command_private.h" | ||||
| #include "core/qthelper.h" | ||||
| 
 | ||||
| namespace Command { | ||||
| 
 | ||||
| EditTripBase::EditTripBase(dive_trip *tripIn, const QString &newValue) : trip(tripIn), | ||||
| 	value(newValue) | ||||
| { | ||||
| } | ||||
| 
 | ||||
| // Note: Virtual functions cannot be called in the constructor.
 | ||||
| // Therefore, setting of the title is done here.
 | ||||
| bool EditTripBase::workToBeDone() | ||||
| { | ||||
| 	setText(tr("Edit %1").arg(fieldName())); | ||||
| 	return data(trip) != value; | ||||
| } | ||||
| 
 | ||||
| void EditTripBase::undo() | ||||
| { | ||||
| 	QString old = data(trip); | ||||
| 	set(trip, value); | ||||
| 	value = old; | ||||
| 
 | ||||
| 	emit diveListNotifier.tripChanged(trip, fieldId()); | ||||
| } | ||||
| 
 | ||||
| // Undo and redo do the same as just the stored value is exchanged
 | ||||
| void EditTripBase::redo() | ||||
| { | ||||
| 	undo(); | ||||
| } | ||||
| 
 | ||||
| // Implementation of virtual functions
 | ||||
| 
 | ||||
| // ***** Location *****
 | ||||
| void EditTripLocation::set(dive_trip *t, const QString &s) const | ||||
| { | ||||
| 	free(t->location); | ||||
| 	t->location = copy_qstring(s); | ||||
| } | ||||
| 
 | ||||
| QString EditTripLocation::data(dive_trip *t) const | ||||
| { | ||||
| 	return QString(t->location); | ||||
| } | ||||
| 
 | ||||
| QString EditTripLocation::fieldName() const | ||||
| { | ||||
| 	return tr("trip location"); | ||||
| } | ||||
| 
 | ||||
| TripField EditTripLocation::fieldId() const | ||||
| { | ||||
| 	return TripField::LOCATION; | ||||
| } | ||||
| 
 | ||||
| // ***** Notes *****
 | ||||
| void EditTripNotes::set(dive_trip *t, const QString &s) const | ||||
| { | ||||
| 	free(t->notes); | ||||
| 	t->notes = copy_qstring(s); | ||||
| } | ||||
| 
 | ||||
| QString EditTripNotes::data(dive_trip *t) const | ||||
| { | ||||
| 	return QString(t->notes); | ||||
| } | ||||
| 
 | ||||
| QString EditTripNotes::fieldName() const | ||||
| { | ||||
| 	return tr("trip notes"); | ||||
| } | ||||
| 
 | ||||
| TripField EditTripNotes::fieldId() const | ||||
| { | ||||
| 	return TripField::NOTES; | ||||
| } | ||||
| 
 | ||||
| } // namespace Command
 | ||||
							
								
								
									
										58
									
								
								commands/command_edit_trip.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								commands/command_edit_trip.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,58 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| // Note: this header file is used by the undo-machinery and should not be included elsewhere.
 | ||||
| 
 | ||||
| #ifndef COMMAND_EDIT_TRIP_H | ||||
| #define COMMAND_EDIT_TRIP_H | ||||
| 
 | ||||
| #include "command_base.h" | ||||
| #include "core/subsurface-qt/DiveListNotifier.h" | ||||
| 
 | ||||
| #include <QVector> | ||||
| 
 | ||||
| // These are commands that edit individual fields of a a trip.
 | ||||
| // The implementation follows the (rather verbose) OO-style of the dive-edit commands.
 | ||||
| // But here, no template is used as the two fields are both of string type.
 | ||||
| 
 | ||||
| // We put everything in a namespace, so that we can shorten names without polluting the global namespace
 | ||||
| namespace Command { | ||||
| 
 | ||||
| class EditTripBase : public Base { | ||||
| 	bool workToBeDone() override; | ||||
| 
 | ||||
| 	dive_trip *trip; // Trip to be edited.
 | ||||
| public: | ||||
| 	EditTripBase(dive_trip *trip, const QString &newValue); | ||||
| 
 | ||||
| protected: | ||||
| 	QString value; // Value to be set
 | ||||
| 	void undo() override; | ||||
| 	void redo() override; | ||||
| 
 | ||||
| 	// Get and set functions to be overriden by sub-classes.
 | ||||
| 	virtual void set(struct dive_trip *t, const QString &) const = 0; | ||||
| 	virtual QString data(struct dive_trip *t) const = 0; | ||||
| 	virtual QString fieldName() const = 0;	// Name of the field, used to create the undo menu-entry
 | ||||
| 	virtual TripField fieldId() const = 0; | ||||
| }; | ||||
| 
 | ||||
| class EditTripLocation : public EditTripBase { | ||||
| public: | ||||
| 	using EditTripBase::EditTripBase;	// Use constructor of base class.
 | ||||
| 	void set(dive_trip *t, const QString &s) const override; | ||||
| 	QString data(dive_trip *t) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	TripField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| class EditTripNotes : public EditTripBase { | ||||
| public: | ||||
| 	using EditTripBase::EditTripBase;	// Use constructor of base class.
 | ||||
| 	void set(dive_trip *t, const QString &s) const override; | ||||
| 	QString data(dive_trip *t) const override; | ||||
| 	QString fieldName() const override; | ||||
| 	TripField fieldId() const override; | ||||
| }; | ||||
| 
 | ||||
| } // namespace Command
 | ||||
| 
 | ||||
| #endif | ||||
							
								
								
									
										108
									
								
								commands/command_private.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								commands/command_private.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,108 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| // Helper functions for the undo-commands
 | ||||
| 
 | ||||
| #include "command_private.h" | ||||
| #include "core/divelist.h" | ||||
| #include "core/display.h" // for amount_selected
 | ||||
| #include "core/subsurface-qt/DiveListNotifier.h" | ||||
| 
 | ||||
| namespace Command { | ||||
| 
 | ||||
| // Set the current dive either from a list of selected dives,
 | ||||
| // or a newly selected dive. In both cases, try to select the
 | ||||
| // dive that is newer that is newer than the given date.
 | ||||
| // This mimics the old behavior when the current dive changed.
 | ||||
| static void setClosestCurrentDive(timestamp_t when, const std::vector<dive *> &selection) | ||||
| { | ||||
| 	// Start from back until we get the first dive that is before
 | ||||
| 	// the supposed-to-be selected dive. (Note: this mimics the
 | ||||
| 	// old behavior when the current dive changed).
 | ||||
| 	for (auto it = selection.rbegin(); it < selection.rend(); ++it) { | ||||
| 		if ((*it)->when > when && !(*it)->hidden_by_filter) { | ||||
| 			current_dive = *it; | ||||
| 			return; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	// We didn't find a more recent selected dive -> try to
 | ||||
| 	// find *any* visible selected dive.
 | ||||
| 	for (dive *d: selection) { | ||||
| 		if (!d->hidden_by_filter) { | ||||
| 			current_dive = d; | ||||
| 			return; | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	// No selected dive is visible! Take the closest dive. Note, this might
 | ||||
| 	// return null, but that just means unsetting the current dive (as no
 | ||||
| 	// dive is visible anyway).
 | ||||
| 	current_dive = find_next_visible_dive(when); | ||||
| } | ||||
| 
 | ||||
| // Reset the selection to the dives of the "selection" vector and send the appropriate signals.
 | ||||
| // Set the current dive to "currentDive". "currentDive" must be an element of "selection" (or
 | ||||
| // null if "seletion" is empty). Return true if the selection or current dive changed.
 | ||||
| void setSelection(const std::vector<dive *> &selection, dive *currentDive) | ||||
| { | ||||
| 	// To do so, generate vectors of dives to be selected and deselected.
 | ||||
| 	// We send signals batched by trip, so keep track of trip/dive pairs.
 | ||||
| 	QVector<dive *> divesToSelect; | ||||
| 	divesToSelect.reserve(selection.size()); | ||||
| 
 | ||||
| 	// TODO: We might want to keep track of selected dives in a more efficient way!
 | ||||
| 	int i; | ||||
| 	dive *d; | ||||
| 	amount_selected = 0; // We recalculate amount_selected
 | ||||
| 	for_each_dive(i, d) { | ||||
| 		// We only modify dives that are currently visible.
 | ||||
| 		if (d->hidden_by_filter) { | ||||
| 			d->selected = false; // Note, not necessary, just to be sure
 | ||||
| 					     // that we get amount_selected right
 | ||||
| 			continue; | ||||
| 		} | ||||
| 
 | ||||
| 		// Search the dive in the list of selected dives.
 | ||||
| 		// TODO: By sorting the list in the same way as the backend, this could be made more efficient.
 | ||||
| 		bool newState = std::find(selection.begin(), selection.end(), d) != selection.end(); | ||||
| 
 | ||||
| 		if (newState) { | ||||
| 			++amount_selected; | ||||
| 			divesToSelect.push_back(d); | ||||
| 		} | ||||
| 		// TODO: Instead of using select_dive() and deselect_dive(), we set selected directly.
 | ||||
| 		// The reason is that deselect() automatically sets a new current dive, which we
 | ||||
| 		// don't want, as we set it later anyway.
 | ||||
| 		// There is other parts of the C++ code that touches the innards directly, but
 | ||||
| 		// ultimately this should be pushed down to C.
 | ||||
| 		d->selected = newState; | ||||
| 	} | ||||
| 
 | ||||
| 	// We cannot simply change the current dive to the given dive.
 | ||||
| 	// It might be hidden by a filter and thus not be selected.
 | ||||
| 	current_dive = currentDive; | ||||
| 	if (current_dive && !currentDive->selected) { | ||||
| 		// Current not visible -> find a different dive.
 | ||||
| 		setClosestCurrentDive(currentDive->when, selection); | ||||
| 	} | ||||
| 
 | ||||
| 	// Send the new selection
 | ||||
| 	emit diveListNotifier.divesSelected(divesToSelect, current_dive); | ||||
| } | ||||
| 
 | ||||
| // Turn current selection into a vector.
 | ||||
| // TODO: This could be made much more efficient if we kept a sorted list of selected dives!
 | ||||
| std::vector<dive *> getDiveSelection() | ||||
| { | ||||
| 	std::vector<dive *> res; | ||||
| 	res.reserve(amount_selected); | ||||
| 
 | ||||
| 	int i; | ||||
| 	dive *d; | ||||
| 	for_each_dive(i, d) { | ||||
| 		if (d->selected) | ||||
| 			res.push_back(d); | ||||
| 	} | ||||
| 	return res; | ||||
| } | ||||
| 
 | ||||
| } // namespace Command
 | ||||
							
								
								
									
										25
									
								
								commands/command_private.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								commands/command_private.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,25 @@ | |||
| // SPDX-License-Identifier: GPL-2.0
 | ||||
| // Private definitions for the command-objects
 | ||||
| 
 | ||||
| #ifndef COMMAND_PRIVATE_H | ||||
| #define COMMAND_PRIVATE_H | ||||
| 
 | ||||
| #include "core/dive.h" | ||||
| 
 | ||||
| #include <vector> | ||||
| #include <utility> | ||||
| #include <QVector> | ||||
| 
 | ||||
| namespace Command { | ||||
| 
 | ||||
| // Reset the selection to the dives of the "selection" vector and send the appropriate signals.
 | ||||
| // Set the current dive to "currentDive". "currentDive" must be an element of "selection" (or
 | ||||
| // null if "seletion" is empty). Return true if the selection or current dive changed.
 | ||||
| void setSelection(const std::vector<dive *> &selection, dive *currentDive); | ||||
| 
 | ||||
| // Get currently selectd dives
 | ||||
| std::vector<dive *> getDiveSelection(); | ||||
| 
 | ||||
| } // namespace Command
 | ||||
| 
 | ||||
| #endif // COMMAND_PRIVATE_H
 | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue