From 3174f1abec451e06ff36bf8f4d9a1e5992ee0382 Mon Sep 17 00:00:00 2001 From: Matthew Petroff Date: Tue, 16 Jul 2013 20:10:36 -0400 Subject: Added new UnitTracker class. (bzr r12380.1.9) --- src/ui/widget/unit-tracker.cpp | 255 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 src/ui/widget/unit-tracker.cpp (limited to 'src/ui/widget/unit-tracker.cpp') diff --git a/src/ui/widget/unit-tracker.cpp b/src/ui/widget/unit-tracker.cpp new file mode 100644 index 000000000..b701c785e --- /dev/null +++ b/src/ui/widget/unit-tracker.cpp @@ -0,0 +1,255 @@ +/* + * Inkscape::UI::Widget::UnitTracker + * Simple mediator to synchronize changes to unit menus + * + * Authors: + * Jon A. Cruz + * Matthew Petroff + * + * Copyright (C) 2007 Jon A. Cruz + * Copyright (C) 2013 Matthew Petroff + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "unit-tracker.h" +#include "ege-select-one-action.h" + +#define COLUMN_STRING 0 + +namespace Inkscape { +namespace UI { +namespace Widget { + +UnitTracker::UnitTracker(UnitType unit_type) : + _active(0), + _isUpdating(false), + _activeUnitInitialized(false), + _store(0), + _unitList(0), + _actionList(0), + _adjList(0), + _priorValues() +{ + _store = gtk_list_store_new(1, G_TYPE_STRING); + static Inkscape::Util::UnitTable unit_table; + + GtkTreeIter iter; + UnitTable::UnitMap m = _unit_table.units(unit_type); + UnitTable::UnitMap::iterator m_iter = m.begin(); + while(m_iter != m.end()) { + Glib::ustring text = (*m_iter).first; + m_iter++; + gtk_list_store_append(_store, &iter); + gtk_list_store_set(_store, &iter, COLUMN_STRING, text.c_str(), -1); + } + gint count = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(_store), 0); + if ((count > 0) && (_active > count)) { + _setActive(--count); + } else { + _setActive(_active); + } +} + +UnitTracker::~UnitTracker() +{ + // Unhook weak references to GtkActions + while (_actionList) { + g_signal_handlers_disconnect_by_func(G_OBJECT(_actionList->data), (gpointer) _unitChangedCB, this); + g_object_weak_unref(G_OBJECT(_actionList->data), _actionFinalizedCB, this); + _actionList = g_slist_delete_link(_actionList, _actionList); + } + + // Unhook weak references to GtkAdjustments + while (_adjList) { + g_object_weak_unref(G_OBJECT(_adjList->data), _adjustmentFinalizedCB, this); + _adjList = g_slist_delete_link(_adjList, _adjList); + } +} + +bool UnitTracker::isUpdating() const +{ + return _isUpdating; +} + +Inkscape::Util::Unit UnitTracker::getActiveUnit() const +{ + return _activeUnit; +} + +void UnitTracker::setActiveUnit(Inkscape::Util::Unit const *unit) +{ + if (unit) { + GtkTreeIter iter; + int index = 0; + gboolean found = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(_store), &iter); + while (found) { + gchar *storedUnit = 0; + gtk_tree_model_get(GTK_TREE_MODEL(_store), &iter, COLUMN_STRING, &storedUnit, -1); + if (storedUnit && (!unit->abbr.compare(storedUnit))) { + _setActive(index); + break; + } + + found = gtk_tree_model_iter_next(GTK_TREE_MODEL(_store), &iter); + index++; + } + } +} + +void UnitTracker::setActiveUnitByAbbr(gchar const *abbr) +{ + Inkscape::Util::Unit u = _unit_table.getUnit(abbr); + setActiveUnit(&u); +} + +void UnitTracker::addAdjustment(GtkAdjustment *adj) +{ + if (!g_slist_find(_adjList, adj)) { + g_object_weak_ref(G_OBJECT(adj), _adjustmentFinalizedCB, this); + _adjList = g_slist_append(_adjList, adj); + } +} + +void UnitTracker::setFullVal(GtkAdjustment *adj, gdouble val) +{ + _priorValues[adj] = val; +} + +GtkAction *UnitTracker::createAction(gchar const *name, gchar const *label, gchar const *tooltip) +{ + EgeSelectOneAction *act1 = ege_select_one_action_new(name, label, tooltip, NULL, GTK_TREE_MODEL(_store)); + ege_select_one_action_set_label_column(act1, COLUMN_STRING); + if (_active) { + ege_select_one_action_set_active(act1, _active); + } + + ege_select_one_action_set_appearance(act1, "minimal"); + g_object_weak_ref(G_OBJECT(act1), _actionFinalizedCB, this); + g_signal_connect(G_OBJECT(act1), "changed", G_CALLBACK(_unitChangedCB), this); + _actionList = g_slist_append(_actionList, act1); + + return GTK_ACTION(act1); +} + +void UnitTracker::_unitChangedCB(GtkAction *action, gpointer data) +{ + if (action && data) { + EgeSelectOneAction *act = EGE_SELECT_ONE_ACTION(action); + gint active = ege_select_one_action_get_active(act); + UnitTracker *self = reinterpret_cast(data); + self->_setActive(active); + } +} + +void UnitTracker::_actionFinalizedCB(gpointer data, GObject *where_the_object_was) +{ + if (data && where_the_object_was) { + UnitTracker *self = reinterpret_cast(data); + self->_actionFinalized(where_the_object_was); + } +} + +void UnitTracker::_adjustmentFinalizedCB(gpointer data, GObject *where_the_object_was) +{ + if (data && where_the_object_was) { + UnitTracker *self = reinterpret_cast(data); + self->_adjustmentFinalized(where_the_object_was); + } +} + +void UnitTracker::_actionFinalized(GObject *where_the_object_was) +{ + GSList *target = g_slist_find(_actionList, where_the_object_was); + if (target) { + _actionList = g_slist_remove(_actionList, where_the_object_was); + } else { + g_warning("Received a finalization callback for unknown object %p", where_the_object_was); + } +} + +void UnitTracker::_adjustmentFinalized(GObject *where_the_object_was) +{ + GSList *target = g_slist_find(_adjList, where_the_object_was); + if (target) { + _adjList = g_slist_remove(_adjList, where_the_object_was); + } else { + g_warning("Received a finalization callback for unknown object %p", where_the_object_was); + } +} + +void UnitTracker::_setActive(gint active) +{ + if ( active != _active || !_activeUnitInitialized ) { + gint oldActive = _active; + + GtkTreeIter iter; + gboolean found = gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(_store), &iter, NULL, oldActive); + if (found) { + gchar *abbr; + gtk_tree_model_get(GTK_TREE_MODEL(_store), &iter, COLUMN_STRING, &abbr, -1); + Inkscape::Util::Unit unit = _unit_table.getUnit(abbr); + + found = gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(_store), &iter, NULL, active); + if (found) { + gchar *newAbbr; + gtk_tree_model_get(GTK_TREE_MODEL(_store), &iter, COLUMN_STRING, &newAbbr, -1); + Inkscape::Util::Unit newUnit = _unit_table.getUnit(newAbbr); + _activeUnit = newUnit; + + if (_adjList) { + _fixupAdjustments(unit, newUnit); + } + + } else { + g_warning("Did not find new unit"); + } + } else { + g_warning("Did not find old unit"); + } + + _active = active; + + for ( GSList *cur = _actionList ; cur ; cur = g_slist_next(cur) ) { + if (IS_EGE_SELECT_ONE_ACTION(cur->data)) { + EgeSelectOneAction *act = EGE_SELECT_ONE_ACTION(cur->data); + ege_select_one_action_set_active(act, active); + } + } + + _activeUnitInitialized = true; + } +} + +void UnitTracker::_fixupAdjustments(Inkscape::Util::Unit const oldUnit, Inkscape::Util::Unit const newUnit) +{ + _isUpdating = true; + Inkscape::Util::Unit px = _unit_table.getUnit("px"); + for ( GSList *cur = _adjList ; cur ; cur = g_slist_next(cur) ) { + GtkAdjustment *adj = GTK_ADJUSTMENT(cur->data); + gdouble oldVal = gtk_adjustment_get_value(adj); + gdouble val = oldVal; + + if ( (oldUnit.type != Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) + && (newUnit.type == Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) ) + { + val = 1.0 / newUnit.factor; + _priorValues[adj] = Inkscape::Util::Quantity::convert(oldVal, &oldUnit, &px); + } else if ( (oldUnit.type == Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) + && (newUnit.type != Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) ) + { + if (_priorValues.find(adj) != _priorValues.end()) { + val = Inkscape::Util::Quantity::convert(_priorValues[adj], &newUnit, &px); + } + } else { + val = Inkscape::Util::Quantity::convert(oldVal, &oldUnit, &newUnit); + } + + gtk_adjustment_set_value(adj, val); + } + _isUpdating = false; +} + +} // namespace Widget +} // namespace UI +} // namespace Inkscape -- cgit v1.2.3 From c28e6d2bdeb2983698a4e4789de15570e0f3d161 Mon Sep 17 00:00:00 2001 From: Matthew Petroff Date: Wed, 17 Jul 2013 00:25:40 -0400 Subject: Fixed conversion factors and missed unit include removal. (bzr r12380.1.12) --- src/ui/widget/unit-tracker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ui/widget/unit-tracker.cpp') diff --git a/src/ui/widget/unit-tracker.cpp b/src/ui/widget/unit-tracker.cpp index b701c785e..df78e21dd 100644 --- a/src/ui/widget/unit-tracker.cpp +++ b/src/ui/widget/unit-tracker.cpp @@ -233,7 +233,7 @@ void UnitTracker::_fixupAdjustments(Inkscape::Util::Unit const oldUnit, Inkscape if ( (oldUnit.type != Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) && (newUnit.type == Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) ) { - val = 1.0 / newUnit.factor; + val = newUnit.factor; _priorValues[adj] = Inkscape::Util::Quantity::convert(oldVal, &oldUnit, &px); } else if ( (oldUnit.type == Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) && (newUnit.type != Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) ) -- cgit v1.2.3 From 3772fc428950b2b946a1bd7c7c97e06219c3165f Mon Sep 17 00:00:00 2001 From: Matthew Petroff Date: Thu, 18 Jul 2013 17:21:24 -0400 Subject: Switch unit functions from using pointer arguements to reference arguements. (bzr r12380.1.28) --- src/ui/widget/unit-tracker.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/ui/widget/unit-tracker.cpp') diff --git a/src/ui/widget/unit-tracker.cpp b/src/ui/widget/unit-tracker.cpp index df78e21dd..372419c3b 100644 --- a/src/ui/widget/unit-tracker.cpp +++ b/src/ui/widget/unit-tracker.cpp @@ -224,7 +224,6 @@ void UnitTracker::_setActive(gint active) void UnitTracker::_fixupAdjustments(Inkscape::Util::Unit const oldUnit, Inkscape::Util::Unit const newUnit) { _isUpdating = true; - Inkscape::Util::Unit px = _unit_table.getUnit("px"); for ( GSList *cur = _adjList ; cur ; cur = g_slist_next(cur) ) { GtkAdjustment *adj = GTK_ADJUSTMENT(cur->data); gdouble oldVal = gtk_adjustment_get_value(adj); @@ -234,15 +233,15 @@ void UnitTracker::_fixupAdjustments(Inkscape::Util::Unit const oldUnit, Inkscape && (newUnit.type == Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) ) { val = newUnit.factor; - _priorValues[adj] = Inkscape::Util::Quantity::convert(oldVal, &oldUnit, &px); + _priorValues[adj] = Inkscape::Util::Quantity::convert(oldVal, oldUnit, "px"); } else if ( (oldUnit.type == Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) && (newUnit.type != Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) ) { if (_priorValues.find(adj) != _priorValues.end()) { - val = Inkscape::Util::Quantity::convert(_priorValues[adj], &newUnit, &px); + val = Inkscape::Util::Quantity::convert(_priorValues[adj], newUnit, "px"); } } else { - val = Inkscape::Util::Quantity::convert(oldVal, &oldUnit, &newUnit); + val = Inkscape::Util::Quantity::convert(oldVal, oldUnit, newUnit); } gtk_adjustment_set_value(adj, val); -- cgit v1.2.3 From c135cb8c39a4004e9eb8adb227ba4c54848a8c45 Mon Sep 17 00:00:00 2001 From: Matthew Petroff Date: Wed, 31 Jul 2013 16:45:07 -0400 Subject: Added percent support back to select toolbar. (bzr r12380.1.53) --- src/ui/widget/unit-tracker.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/ui/widget/unit-tracker.cpp') diff --git a/src/ui/widget/unit-tracker.cpp b/src/ui/widget/unit-tracker.cpp index 372419c3b..c0d3eec9b 100644 --- a/src/ui/widget/unit-tracker.cpp +++ b/src/ui/widget/unit-tracker.cpp @@ -111,6 +111,13 @@ void UnitTracker::addAdjustment(GtkAdjustment *adj) } } +void UnitTracker::addUnit(Inkscape::Util::Unit const &u) +{ + GtkTreeIter iter; + gtk_list_store_append(_store, &iter); + gtk_list_store_set(_store, &iter, COLUMN_STRING, u.abbr.c_str(), -1); +} + void UnitTracker::setFullVal(GtkAdjustment *adj, gdouble val) { _priorValues[adj] = val; @@ -232,7 +239,7 @@ void UnitTracker::_fixupAdjustments(Inkscape::Util::Unit const oldUnit, Inkscape if ( (oldUnit.type != Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) && (newUnit.type == Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) ) { - val = newUnit.factor; + val = newUnit.factor * 100; _priorValues[adj] = Inkscape::Util::Quantity::convert(oldVal, oldUnit, "px"); } else if ( (oldUnit.type == Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) && (newUnit.type != Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) ) -- cgit v1.2.3 From f69ba9fbb46f827a2cb76f2d1f87acbf53edc416 Mon Sep 17 00:00:00 2001 From: Matthew Petroff Date: Thu, 1 Aug 2013 19:42:12 -0400 Subject: Fix UnitTracker percentage bug. (bzr r12380.1.59) --- src/ui/widget/unit-tracker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ui/widget/unit-tracker.cpp') diff --git a/src/ui/widget/unit-tracker.cpp b/src/ui/widget/unit-tracker.cpp index c0d3eec9b..99074be40 100644 --- a/src/ui/widget/unit-tracker.cpp +++ b/src/ui/widget/unit-tracker.cpp @@ -245,7 +245,7 @@ void UnitTracker::_fixupAdjustments(Inkscape::Util::Unit const oldUnit, Inkscape && (newUnit.type != Inkscape::Util::UNIT_TYPE_DIMENSIONLESS) ) { if (_priorValues.find(adj) != _priorValues.end()) { - val = Inkscape::Util::Quantity::convert(_priorValues[adj], newUnit, "px"); + val = Inkscape::Util::Quantity::convert(_priorValues[adj], "px", newUnit); } } else { val = Inkscape::Util::Quantity::convert(oldVal, oldUnit, newUnit); -- cgit v1.2.3 From 6ae6c0bea96eef09907091279e0678aa5f83102d Mon Sep 17 00:00:00 2001 From: Matthew Petroff Date: Sun, 4 Aug 2013 18:01:18 -0400 Subject: Switched to global UnitTable. (bzr r12380.1.62) --- src/ui/widget/unit-tracker.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src/ui/widget/unit-tracker.cpp') diff --git a/src/ui/widget/unit-tracker.cpp b/src/ui/widget/unit-tracker.cpp index 99074be40..5b2dc031b 100644 --- a/src/ui/widget/unit-tracker.cpp +++ b/src/ui/widget/unit-tracker.cpp @@ -17,6 +17,9 @@ #define COLUMN_STRING 0 +using Inkscape::Util::UnitTable; +using Inkscape::Util::unit_table; + namespace Inkscape { namespace UI { namespace Widget { @@ -32,10 +35,9 @@ UnitTracker::UnitTracker(UnitType unit_type) : _priorValues() { _store = gtk_list_store_new(1, G_TYPE_STRING); - static Inkscape::Util::UnitTable unit_table; GtkTreeIter iter; - UnitTable::UnitMap m = _unit_table.units(unit_type); + UnitTable::UnitMap m = unit_table.units(unit_type); UnitTable::UnitMap::iterator m_iter = m.begin(); while(m_iter != m.end()) { Glib::ustring text = (*m_iter).first; @@ -99,7 +101,7 @@ void UnitTracker::setActiveUnit(Inkscape::Util::Unit const *unit) void UnitTracker::setActiveUnitByAbbr(gchar const *abbr) { - Inkscape::Util::Unit u = _unit_table.getUnit(abbr); + Inkscape::Util::Unit u = unit_table.getUnit(abbr); setActiveUnit(&u); } @@ -195,13 +197,13 @@ void UnitTracker::_setActive(gint active) if (found) { gchar *abbr; gtk_tree_model_get(GTK_TREE_MODEL(_store), &iter, COLUMN_STRING, &abbr, -1); - Inkscape::Util::Unit unit = _unit_table.getUnit(abbr); + Inkscape::Util::Unit unit = unit_table.getUnit(abbr); found = gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(_store), &iter, NULL, active); if (found) { gchar *newAbbr; gtk_tree_model_get(GTK_TREE_MODEL(_store), &iter, COLUMN_STRING, &newAbbr, -1); - Inkscape::Util::Unit newUnit = _unit_table.getUnit(newAbbr); + Inkscape::Util::Unit newUnit = unit_table.getUnit(newAbbr); _activeUnit = newUnit; if (_adjList) { -- cgit v1.2.3