Minor code cleanup in toolbar code

upstream-master
Bryan Tan 5 years ago committed by Bryan Tan
parent 62c2af2214
commit 481960b039
  1. 15
      src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.cpp
  2. 20
      src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.h
  3. 25
      src/gui/toolbarMenubar/model/ToolbarEntry.cpp
  4. 4
      src/gui/toolbarMenubar/model/ToolbarEntry.h
  5. 9
      src/gui/toolbarMenubar/model/ToolbarItem.cpp
  6. 7
      src/gui/toolbarMenubar/model/ToolbarItem.h

@ -1,16 +1,14 @@
#include "ToolbarDragDropHelper.h"
GdkAtom ToolbarDragDropHelper::atomToolItem = gdk_atom_intern_static_string("application/xournal-ToolbarItem");
GtkTargetEntry ToolbarDragDropHelper::dropTargetEntry = {const_cast<char*>("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<char*>("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<GdkPixbuf*>(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

@ -13,18 +13,12 @@
#include <gtk/gtk.h>
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

@ -1,18 +1,28 @@
#include "ToolbarEntry.h"
#include <algorithm>
#include <utility>
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<ToolbarItem*> 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;
}
}

@ -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();

@ -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; }

@ -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);

Loading…
Cancel
Save