From 481960b0393c52df45344b3476fd68bb809f14de Mon Sep 17 00:00:00 2001 From: Bryan Tan Date: Tue, 23 Feb 2021 15:13:35 -0800 Subject: [PATCH] Minor code cleanup in toolbar code --- .../ToolbarDragDropHelper.cpp | 15 ++++++----- .../toolbarCustomize/ToolbarDragDropHelper.h | 20 ++++++--------- src/gui/toolbarMenubar/model/ToolbarEntry.cpp | 25 +++++++++++++------ src/gui/toolbarMenubar/model/ToolbarEntry.h | 4 ++- src/gui/toolbarMenubar/model/ToolbarItem.cpp | 9 +------ src/gui/toolbarMenubar/model/ToolbarItem.h | 7 +----- 6 files changed, 36 insertions(+), 44 deletions(-) diff --git a/src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.cpp b/src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.cpp index 6403e4fa..c36ba3f6 100644 --- a/src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.cpp +++ b/src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.cpp @@ -1,16 +1,14 @@ #include "ToolbarDragDropHelper.h" -GdkAtom ToolbarDragDropHelper::atomToolItem = gdk_atom_intern_static_string("application/xournal-ToolbarItem"); -GtkTargetEntry ToolbarDragDropHelper::dropTargetEntry = {const_cast("move-buffer"), GTK_TARGET_SAME_APP, 1}; +namespace ToolbarDragDropHelper { -ToolbarDragDropHelper::ToolbarDragDropHelper() = default; - -ToolbarDragDropHelper::~ToolbarDragDropHelper() = default; +const GdkAtom atomToolItem = gdk_atom_intern_static_string("application/xournal-ToolbarItem"); +const GtkTargetEntry dropTargetEntry = {const_cast("move-buffer"), GTK_TARGET_SAME_APP, 1}; /** * Get a GDK Pixbuf from GTK widget image */ -auto ToolbarDragDropHelper::getImagePixbuf(GtkImage* image) -> GdkPixbuf* { +auto getImagePixbuf(GtkImage* image) -> GdkPixbuf* { switch (gtk_image_get_storage_type(image)) { case GTK_IMAGE_PIXBUF: return static_cast(g_object_ref(gtk_image_get_pixbuf(image))); @@ -28,7 +26,7 @@ auto ToolbarDragDropHelper::getImagePixbuf(GtkImage* image) -> GdkPixbuf* { } } -void ToolbarDragDropHelper::dragDestAddToolbar(GtkWidget* target) { +void dragDestAddToolbar(GtkWidget* target) { GtkTargetList* targetList = gtk_drag_dest_get_target_list(target); if (targetList) { gtk_target_list_ref(targetList); @@ -45,7 +43,7 @@ void ToolbarDragDropHelper::dragDestAddToolbar(GtkWidget* target) { gtk_target_list_unref(targetList); } -void ToolbarDragDropHelper::dragSourceAddToolbar(GtkWidget* widget) { +void dragSourceAddToolbar(GtkWidget* widget) { GtkTargetList* targetList = gtk_drag_source_get_target_list(widget); if (targetList) { // List contains already this type @@ -61,3 +59,4 @@ void ToolbarDragDropHelper::dragSourceAddToolbar(GtkWidget* widget) { gtk_drag_source_set_target_list(widget, targetList); gtk_target_list_unref(targetList); } +} // namespace ToolbarDragDropHelper diff --git a/src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.h b/src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.h index 46235b5f..45c15929 100644 --- a/src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.h +++ b/src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.h @@ -13,18 +13,12 @@ #include -class ToolbarDragDropHelper { -private: - ToolbarDragDropHelper(); - virtual ~ToolbarDragDropHelper(); +namespace ToolbarDragDropHelper { +void dragDestAddToolbar(GtkWidget* target); +void dragSourceAddToolbar(GtkWidget* widget); -public: - static void dragDestAddToolbar(GtkWidget* target); - static void dragSourceAddToolbar(GtkWidget* widget); +GdkPixbuf* getImagePixbuf(GtkImage* image); - static GdkPixbuf* getImagePixbuf(GtkImage* image); - -public: - static GdkAtom atomToolItem; - static GtkTargetEntry dropTargetEntry; -}; +extern const GdkAtom atomToolItem; +extern const GtkTargetEntry dropTargetEntry; +}; // namespace ToolbarDragDropHelper diff --git a/src/gui/toolbarMenubar/model/ToolbarEntry.cpp b/src/gui/toolbarMenubar/model/ToolbarEntry.cpp index a10f869d..d26ed7de 100644 --- a/src/gui/toolbarMenubar/model/ToolbarEntry.cpp +++ b/src/gui/toolbarMenubar/model/ToolbarEntry.cpp @@ -1,18 +1,28 @@ #include "ToolbarEntry.h" +#include #include ToolbarEntry::ToolbarEntry() = default; ToolbarEntry::ToolbarEntry(const ToolbarEntry& e) { *this = e; } +ToolbarEntry::ToolbarEntry(ToolbarEntry&& e) { *this = std::move(e); } -void ToolbarEntry::operator=(const ToolbarEntry& e) { +ToolbarEntry& ToolbarEntry::operator=(const ToolbarEntry& e) { this->name = e.name; - clearList(); - + std::vector entries; for (ToolbarItem* item: e.entries) { entries.push_back(new ToolbarItem(*item)); } + clearList(); + this->entries = std::move(entries); + return *this; +} + +ToolbarEntry& ToolbarEntry::operator=(ToolbarEntry&& e) { + this->name = std::move(e.name); + std::swap(this->entries, e.entries); + return *this; } ToolbarEntry::~ToolbarEntry() { clearList(); } @@ -36,11 +46,10 @@ auto ToolbarEntry::addItem(string item) -> int { } auto ToolbarEntry::removeItemById(int id) -> bool { - for (unsigned int i = 0; i < this->entries.size(); i++) { - if (this->entries[i]->getId() == id) { - delete this->entries[i]; - entries[i] = nullptr; - entries.erase(entries.begin() + i); + for (auto it = this->entries.begin(); it != this->entries.end(); it++) { + if ((*it)->getId() == id) { + delete *it; + this->entries.erase(it); return true; } } diff --git a/src/gui/toolbarMenubar/model/ToolbarEntry.h b/src/gui/toolbarMenubar/model/ToolbarEntry.h index e4866226..474c1bea 100644 --- a/src/gui/toolbarMenubar/model/ToolbarEntry.h +++ b/src/gui/toolbarMenubar/model/ToolbarEntry.h @@ -19,9 +19,11 @@ class ToolbarEntry { public: ToolbarEntry(); ToolbarEntry(const ToolbarEntry& e); + ToolbarEntry(ToolbarEntry&& e); ~ToolbarEntry(); - void operator=(const ToolbarEntry& e); + ToolbarEntry& operator=(const ToolbarEntry& e); + ToolbarEntry& operator=(ToolbarEntry&& e); public: void clearList(); diff --git a/src/gui/toolbarMenubar/model/ToolbarItem.cpp b/src/gui/toolbarMenubar/model/ToolbarItem.cpp index 36992b4b..03d41a2e 100644 --- a/src/gui/toolbarMenubar/model/ToolbarItem.cpp +++ b/src/gui/toolbarMenubar/model/ToolbarItem.cpp @@ -13,19 +13,12 @@ ToolbarItem::ToolbarItem(string name) { } } -ToolbarItem::ToolbarItem(const ToolbarItem& item) { - this->id = item.id; - this->name = item.name; -} - ToolbarItem::ToolbarItem() { this->name = ""; this->id = -100; } -ToolbarItem::~ToolbarItem() = default; - -auto ToolbarItem::getName() -> string { return this->name; } +auto ToolbarItem::getName() const -> const std::string& { return this->name; } auto ToolbarItem::operator==(ToolbarItem& other) -> bool { return this->name == other.name; } diff --git a/src/gui/toolbarMenubar/model/ToolbarItem.h b/src/gui/toolbarMenubar/model/ToolbarItem.h index 392ea895..e844cde5 100644 --- a/src/gui/toolbarMenubar/model/ToolbarItem.h +++ b/src/gui/toolbarMenubar/model/ToolbarItem.h @@ -19,15 +19,10 @@ class ToolbarItem { public: ToolbarItem(string name); - ToolbarItem(const ToolbarItem& item); ToolbarItem(); - virtual ~ToolbarItem(); - -private: - void operator=(const ToolbarItem& other); public: - string getName(); + const std::string& getName() const; bool operator==(ToolbarItem& other);