mirror of
				https://github.com/subsurface/subsurface.git
				synced 2025-02-19 22:16:15 +00:00 
			
		
		
		
	undo: add device related undo commands
Add commands for deleting devices and editing device nicknames to include the device-handling in the undo system. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
		
							parent
							
								
									572e2678a0
								
							
						
					
					
						commit
						faebb53909
					
				
					 9 changed files with 140 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -6,6 +6,8 @@ set(SUBSURFACE_GENERIC_COMMANDS_SRCS
 | 
			
		|||
	command_base.h
 | 
			
		||||
	command.cpp
 | 
			
		||||
	command.h
 | 
			
		||||
	command_device.cpp
 | 
			
		||||
	command_device.h
 | 
			
		||||
	command_divelist.cpp
 | 
			
		||||
	command_divelist.h
 | 
			
		||||
	command_divesite.cpp
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
// SPDX-License-Identifier: GPL-2.0
 | 
			
		||||
 | 
			
		||||
#include "command.h"
 | 
			
		||||
#include "command_device.h"
 | 
			
		||||
#include "command_divelist.h"
 | 
			
		||||
#include "command_divesite.h"
 | 
			
		||||
#include "command_edit.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -380,6 +381,16 @@ void addPictures(const std::vector<PictureListForAddition> &pictures)
 | 
			
		|||
	execute(new AddPictures(pictures));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void removeDevice(int idx)
 | 
			
		||||
{
 | 
			
		||||
	execute(new RemoveDevice(idx));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void editDeviceNickname(int idx, const QString &nickname)
 | 
			
		||||
{
 | 
			
		||||
	execute(new EditDeviceNickname(idx, nickname));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void createFilterPreset(const QString &name, const FilterData &data)
 | 
			
		||||
{
 | 
			
		||||
	execute(new CreateFilterPreset(name, data));
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -138,7 +138,12 @@ void setPictureOffset(dive *d, const QString &filename, offset_t offset);
 | 
			
		|||
void removePictures(const std::vector<PictureListForDeletion> &pictures);
 | 
			
		||||
void addPictures(const std::vector<PictureListForAddition> &pictures);
 | 
			
		||||
 | 
			
		||||
// 8) Filter commands
 | 
			
		||||
// 8) Device commands
 | 
			
		||||
 | 
			
		||||
void removeDevice(int idx);
 | 
			
		||||
void editDeviceNickname(int idx, const QString &nickname);
 | 
			
		||||
 | 
			
		||||
// 9) Filter commands
 | 
			
		||||
 | 
			
		||||
void createFilterPreset(const QString &name, const FilterData &data);
 | 
			
		||||
void removeFilterPreset(int index);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										66
									
								
								commands/command_device.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								commands/command_device.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,66 @@
 | 
			
		|||
// SPDX-License-Identifier: GPL-2.0
 | 
			
		||||
 | 
			
		||||
#include "command_device.h"
 | 
			
		||||
#include "core/subsurface-qt/divelistnotifier.h"
 | 
			
		||||
 | 
			
		||||
namespace Command {
 | 
			
		||||
 | 
			
		||||
RemoveDevice::RemoveDevice(int indexIn) : index(indexIn)
 | 
			
		||||
{
 | 
			
		||||
	const device *dev = get_device(&device_table, index);
 | 
			
		||||
	if (!dev)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	setText(Command::Base::tr("Delete device %1 (0x%2)").arg(QString::fromStdString(dev->model),
 | 
			
		||||
								 QString::number(dev->deviceId)));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool RemoveDevice::workToBeDone()
 | 
			
		||||
{
 | 
			
		||||
	return get_device(&device_table, index) != nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RemoveDevice::redo()
 | 
			
		||||
{
 | 
			
		||||
	dev = *get_device(&device_table, index);
 | 
			
		||||
	remove_from_device_table(&device_table, index);
 | 
			
		||||
	emit diveListNotifier.deviceDeleted(index);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RemoveDevice::undo()
 | 
			
		||||
{
 | 
			
		||||
	index = add_to_device_table(&device_table, &dev);
 | 
			
		||||
	emit diveListNotifier.deviceAdded(index);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
EditDeviceNickname::EditDeviceNickname(int indexIn, const QString &nicknameIn) :
 | 
			
		||||
	index(indexIn), nickname(nicknameIn.toStdString())
 | 
			
		||||
{
 | 
			
		||||
	const device *dev = get_device(&device_table, index);
 | 
			
		||||
	if (!dev)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	setText(Command::Base::tr("Set nickname of device %1 (0x%2) to %3").arg(QString::fromStdString(dev->model),
 | 
			
		||||
										QString::number(dev->deviceId,1 ,16), nicknameIn));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool EditDeviceNickname::workToBeDone()
 | 
			
		||||
{
 | 
			
		||||
	return get_device(&device_table, index) != nullptr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void EditDeviceNickname::redo()
 | 
			
		||||
{
 | 
			
		||||
	device *dev = get_device_mutable(&device_table, index);
 | 
			
		||||
	if (!dev)
 | 
			
		||||
		return;
 | 
			
		||||
	std::swap(dev->nickName, nickname);
 | 
			
		||||
	emit diveListNotifier.deviceEdited(index);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void EditDeviceNickname::undo()
 | 
			
		||||
{
 | 
			
		||||
	redo(); // undo() and redo() do the same thing
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
} // namespace Command
 | 
			
		||||
							
								
								
									
										44
									
								
								commands/command_device.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								commands/command_device.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,44 @@
 | 
			
		|||
// SPDX-License-Identifier: GPL-2.0
 | 
			
		||||
// Note: this header file is used by the undo-machinery and should not be included elsewhere.
 | 
			
		||||
 | 
			
		||||
#ifndef COMMAND_DEVICE_H
 | 
			
		||||
#define COMMAND_DEVICE_H
 | 
			
		||||
 | 
			
		||||
#include "command_base.h"
 | 
			
		||||
#include "core/device.h"
 | 
			
		||||
 | 
			
		||||
struct device;
 | 
			
		||||
 | 
			
		||||
// We put everything in a namespace, so that we can shorten names without polluting the global namespace
 | 
			
		||||
namespace Command {
 | 
			
		||||
 | 
			
		||||
class RemoveDevice final : public Base {
 | 
			
		||||
public:
 | 
			
		||||
	RemoveDevice(int index);
 | 
			
		||||
private:
 | 
			
		||||
	// for undo
 | 
			
		||||
	device dev;
 | 
			
		||||
 | 
			
		||||
	// for redo
 | 
			
		||||
	int index;
 | 
			
		||||
 | 
			
		||||
	void undo() override;
 | 
			
		||||
	void redo() override;
 | 
			
		||||
	bool workToBeDone() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class EditDeviceNickname final : public Base {
 | 
			
		||||
public:
 | 
			
		||||
	EditDeviceNickname(int index, const QString &nickname);
 | 
			
		||||
private:
 | 
			
		||||
	// for redo and undo
 | 
			
		||||
	int index;
 | 
			
		||||
	std::string nickname;
 | 
			
		||||
 | 
			
		||||
	void undo() override;
 | 
			
		||||
	void redo() override;
 | 
			
		||||
	bool workToBeDone() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
} // namespace Command
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -392,6 +392,13 @@ extern "C" const struct device *get_device(const struct device_table *table, int
 | 
			
		|||
	return &table->devices[i];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
extern "C" struct device *get_device_mutable(struct device_table *table, int i)
 | 
			
		||||
{
 | 
			
		||||
	if (i < 0 || i > nr_devices(table))
 | 
			
		||||
		return NULL;
 | 
			
		||||
	return &table->devices[i];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
extern "C" const char *device_get_model(const struct device *dev)
 | 
			
		||||
{
 | 
			
		||||
	return dev ? dev->model.c_str() : NULL;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,6 +23,7 @@ extern void add_devices_of_dive(const struct dive *dive, struct device_table *ta
 | 
			
		|||
extern void create_device_node(struct device_table *table, const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname);
 | 
			
		||||
extern int nr_devices(const struct device_table *table);
 | 
			
		||||
extern const struct device *get_device(const struct device_table *table, int i);
 | 
			
		||||
extern struct device *get_device_mutable(struct device_table *table, int i);
 | 
			
		||||
extern void clear_device_table(struct device_table *table);
 | 
			
		||||
const char *get_dc_nickname(const struct divecomputer *dc);
 | 
			
		||||
extern bool device_used_by_selected_dive(const struct device *dev);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -130,6 +130,7 @@ signals:
 | 
			
		|||
	// Devices related signals
 | 
			
		||||
	void deviceAdded(int index);
 | 
			
		||||
	void deviceDeleted(int index);
 | 
			
		||||
	void deviceEdited(int index);
 | 
			
		||||
 | 
			
		||||
	// Filter related signals
 | 
			
		||||
	void filterPresetAdded(int index);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,6 +17,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \
 | 
			
		|||
	../../map-widget/qmlmapwidgethelper.cpp \
 | 
			
		||||
	../../commands/command_base.cpp \
 | 
			
		||||
	../../commands/command.cpp \
 | 
			
		||||
	../../commands/command_device.cpp \
 | 
			
		||||
	../../commands/command_divelist.cpp \
 | 
			
		||||
	../../commands/command_divesite.cpp \
 | 
			
		||||
	../../commands/command_edit.cpp \
 | 
			
		||||
| 
						 | 
				
			
			@ -190,6 +191,7 @@ INCLUDEPATH += ../../../install-root/ios/include/ \
 | 
			
		|||
HEADERS += \
 | 
			
		||||
	../../commands/command_base.h \
 | 
			
		||||
	../../commands/command.h \
 | 
			
		||||
	../../commands/command_device.h \
 | 
			
		||||
	../../commands/command_divelist.h \
 | 
			
		||||
	../../commands/command_divesite.h \
 | 
			
		||||
	../../commands/command_edit.h \
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue