diff options
| author | Diederik van Lierop <mail@diedenrezi.nl> | 2011-08-22 18:27:53 +0000 |
|---|---|---|
| committer | Diederik van Lierop <mail@diedenrezi.nl> | 2011-08-22 18:27:53 +0000 |
| commit | babb7a67749cb691674bdd9758f0568d4b094b56 (patch) | |
| tree | d1e71acda1562bf3d394aa76f44b96be1175ca61 /src/snap-preferences.cpp | |
| parent | svgfont2layers extension: glyph layers (except the first one) are innitially ... (diff) | |
| download | inkscape-babb7a67749cb691674bdd9758f0568d4b094b56.tar.gz inkscape-babb7a67749cb691674bdd9758f0568d4b094b56.zip | |
Refactoring of the snapping preferences; mainly about storing all toggles in a single array, instead of each having its own member variable
(bzr r10569)
Diffstat (limited to 'src/snap-preferences.cpp')
| -rw-r--r-- | src/snap-preferences.cpp | 199 |
1 files changed, 172 insertions, 27 deletions
diff --git a/src/snap-preferences.cpp b/src/snap-preferences.cpp index b98726a86..a02e4baba 100644 --- a/src/snap-preferences.cpp +++ b/src/snap-preferences.cpp @@ -12,20 +12,24 @@ #include "inkscape.h" #include "snap-preferences.h" +#include <glib.h> // g_assert() Inkscape::SnapPreferences::SnapPreferences() : - _include_item_center(false), - _intersectionGG(true), - _snap_to_grids(true), - _snap_to_guides(true), _snap_enabled_globally(true), _snap_postponed_globally(false), - _snap_to_itemnode(true), _snap_to_itempath(true), - _snap_to_bboxnode(true), _snap_to_bboxpath(true), - _snap_to_page_border(false), _strict_snapping(true) { + // Check for enough space to hold all snap target toggles in the "others" group; see the comments in snap-enums.h + g_assert(SNAPTARGET_MAX_ENUM_VALUE - SNAPTARGET_OTHERS_CATEGORY < SNAPTARGET_BBOX_CATEGORY); + // Check for powers of two; see the comments in snap-enums.h + g_assert((SNAPTARGET_BBOX_CATEGORY != 0) && !(SNAPTARGET_BBOX_CATEGORY & (SNAPTARGET_BBOX_CATEGORY - 1))); + g_assert((SNAPTARGET_NODE_CATEGORY != 0) && !(SNAPTARGET_NODE_CATEGORY & (SNAPTARGET_NODE_CATEGORY - 1))); + g_assert((SNAPTARGET_OTHERS_CATEGORY != 0) && !(SNAPTARGET_OTHERS_CATEGORY & (SNAPTARGET_OTHERS_CATEGORY - 1))); + setSnapFrom(SnapSourceType(SNAPSOURCE_BBOX_CATEGORY | SNAPSOURCE_NODE_CATEGORY | SNAPSOURCE_OTHERS_CATEGORY), true); //Snap any point. In v0.45 and earlier, this was controlled in the preferences tab + for (int n = 0; n < Inkscape::SNAPTARGET_MAX_ENUM_VALUE; n++) { + _active_snap_targets[n] = -1; + } } /* @@ -46,6 +50,7 @@ void Inkscape::SnapPreferences::setSnapModeBBox(bool enabled) } else { _snap_from = SnapSourceType(_snap_from & ~Inkscape::SNAPSOURCE_BBOX_CATEGORY); } + setTargetSnappable(SNAPTARGET_BBOX_CATEGORY, enabled); } bool Inkscape::SnapPreferences::getSnapModeBBox() const @@ -60,6 +65,7 @@ void Inkscape::SnapPreferences::setSnapModeNode(bool enabled) } else { _snap_from = SnapSourceType(_snap_from & ~Inkscape::SNAPSOURCE_NODE_CATEGORY); } + setTargetSnappable(SNAPTARGET_NODE_CATEGORY, enabled); } bool Inkscape::SnapPreferences::getSnapModeNode() const @@ -74,6 +80,7 @@ void Inkscape::SnapPreferences::setSnapModeOthers(bool enabled) } else { _snap_from = SnapSourceType(_snap_from & ~Inkscape::SNAPSOURCE_OTHERS_CATEGORY); } + setTargetSnappable(SNAPTARGET_OTHERS_CATEGORY, enabled); } bool Inkscape::SnapPreferences::getSnapModeOthers() const @@ -81,31 +88,11 @@ bool Inkscape::SnapPreferences::getSnapModeOthers() const return (_snap_from & Inkscape::SNAPSOURCE_OTHERS_CATEGORY); } - -//bool Inkscape::SnapPreferences::getSnapModeBBoxOrNodes() const -//{ -// return (_snap_from & (Inkscape::SNAPSOURCE_BBOX_CATEGORY | Inkscape::SNAPSOURCE_NODE_CATEGORY) ); -//} - bool Inkscape::SnapPreferences::getSnapModeAny() const { return (_snap_from != 0); } -void Inkscape::SnapPreferences::setSnapModeGuide(bool enabled) -{ - if (enabled) { - _snap_from = SnapSourceType(_snap_from | Inkscape::SNAPSOURCE_OTHERS_CATEGORY); - } else { - _snap_from = SnapSourceType(_snap_from & ~Inkscape::SNAPSOURCE_OTHERS_CATEGORY); - } -} - -bool Inkscape::SnapPreferences::getSnapModeGuide() const -{ - return (_snap_from & Inkscape::SNAPSOURCE_OTHERS_CATEGORY); -} - /** * Turn on/off snapping of specific point types. * \param t Point type. @@ -129,6 +116,164 @@ bool Inkscape::SnapPreferences::getSnapFrom(Inkscape::SnapSourceType t) const return (_snap_from & t); } +void Inkscape::SnapPreferences::_mapTargetToArrayIndex(Inkscape::SnapTargetType &target, bool &always_on, bool &group_on) const +{ + if (target & SNAPTARGET_BBOX_CATEGORY) { + group_on = getSnapModeBBox(); // Only if the group with bbox sources/targets has been enabled, then we might snap to any of the bbox targets + } else if (target & SNAPTARGET_NODE_CATEGORY) { + group_on = getSnapModeNode(); // Only if the group with path/node sources/targets has been enabled, then we might snap to any of the nodes/paths + if (target == SNAPTARGET_RECT_CORNER || target == SNAPTARGET_ELLIPSE_QUADRANT_POINT) { // Don't have their own button; on when the group is on + target = SNAPTARGET_NODE_CATEGORY; + } + } else if (target & SNAPTARGET_OTHERS_CATEGORY) { + // Only if the group with "other" snap sources/targets has been enabled, then we might snap to any of those targets + // ... but this doesn't hold for the page border, grids, and guides + group_on = getSnapModeOthers(); + switch (target) { + // Some snap targets don't have their own toggle. These targets are called "secondary targets". We will re-map + // them to their cousin which does have a toggle, and which is called a "primary target" + case SNAPTARGET_GRID_INTERSECTION: + group_on = true; // cannot be disabled as part of a disabled group; + target = SNAPTARGET_GRID; + break; + case SNAPTARGET_GUIDE_INTERSECTION: + case SNAPTARGET_GUIDE_ORIGIN: + group_on = true; // cannot be disabled as part of a disabled group; + target = SNAPTARGET_GUIDE; + break; + case SNAPTARGET_PAGE_CORNER: + group_on = true; // cannot be disabled as part of a disabled group; + target = SNAPTARGET_PAGE_BORDER; + break; + case SNAPTARGET_TEXT_ANCHOR: + target = SNAPTARGET_TEXT_BASELINE; + break; + + case SNAPTARGET_IMG_CORNER: // Doesn't have its own button, on if the group is on + target = SNAPTARGET_OTHERS_CATEGORY; + break; + // Some snap targets cannot be toggled at all, and are therefore always enabled + case SNAPTARGET_GRID_GUIDE_INTERSECTION: + case SNAPTARGET_CONSTRAINED_ANGLE: + case SNAPTARGET_CONSTRAINT: + always_on = true; // Doesn't have it's own button + break; + case SNAPTARGET_GRID: + case SNAPTARGET_GUIDE: + case SNAPTARGET_PAGE_BORDER: + group_on = true; // cannot be disabled as part of a disabled group; + break; + + // These are only listed for completeness + case SNAPTARGET_OBJECT_MIDPOINT: + case SNAPTARGET_ROTATION_CENTER: + case SNAPTARGET_TEXT_BASELINE: + break; + + case SNAPTARGET_BBOX_CATEGORY: + case SNAPTARGET_NODE_CATEGORY: + case SNAPTARGET_OTHERS_CATEGORY: + break; + default: + g_warning("Snap-preferences warning: Undefined snap target (#%i)", target); + break; + } + } else if (target == SNAPTARGET_UNDEFINED ) { + g_warning("Snap-preferences warning: Undefined snaptarget (#%i)", target); + } +} + +void Inkscape::SnapPreferences::setTargetSnappable(Inkscape::SnapTargetType const target, bool enabled) +{ + bool always_on = false; + bool group_on = false; + Inkscape::SnapTargetType index = target; + + _mapTargetToArrayIndex(index, always_on, group_on); + + if (always_on) { + // Catch coding errors + g_warning("Snap-preferences warning: Trying to enable/disable a snap target (#%i) that's always on by definition", index); + } else { + if (index == target) { // I.e. if it has not been re-mapped, then we have a primary target at hand + _active_snap_targets[index] = enabled; + } else { // If it has been re-mapped though, then this target does not have its own toggle button and should therefore not be set + g_warning("Snap-preferences warning: Trying to enable/disable a secondary snap target (#%i); only primary targets can be set", index); + } + } +} + +bool Inkscape::SnapPreferences::isTargetSnappable(Inkscape::SnapTargetType const target) const +{ + bool always_on = false; + bool group_on = false; + Inkscape::SnapTargetType index = target; + + _mapTargetToArrayIndex(index, always_on, group_on); + + if (group_on) { + if (always_on) { + return true; + } else { + if (_active_snap_targets[index] == -1) { + // Catch coding errors + g_warning("Snap-preferences warning: Using an uninitialized snap target setting (#%i)", index); + } + return _active_snap_targets[index]; + } + } else { + return false; + } +} + +bool Inkscape::SnapPreferences::isTargetSnappable(Inkscape::SnapTargetType const target1, Inkscape::SnapTargetType const target2) const { + return isTargetSnappable(target1) || isTargetSnappable(target2); +} + +bool Inkscape::SnapPreferences::isTargetSnappable(Inkscape::SnapTargetType const target1, Inkscape::SnapTargetType const target2, Inkscape::SnapTargetType const target3) const { + return isTargetSnappable(target1) || isTargetSnappable(target2) || isTargetSnappable(target3); +} + +bool Inkscape::SnapPreferences::isTargetSnappable(Inkscape::SnapTargetType const target1, Inkscape::SnapTargetType const target2, Inkscape::SnapTargetType const target3, Inkscape::SnapTargetType const target4) const { + return isTargetSnappable(target1) || isTargetSnappable(target2) || isTargetSnappable(target3) || isTargetSnappable(target4); +} + + +//bool Inkscape::SnapPreferences::isAnyBBoxSnappable() const { +// return getSnapModeBBox() || getSnapModeOthers() || (getSnapModeNode() && !_strict_snapping); +//} + +//bool Inkscape::SnapPreferences::isAnyNodeOrPathSnappable() const { +// return getSnapModeNode() || getSnapModeOthers() || (getSnapModeBBox() && !_strict_snapping); +//} + +//bool Inkscape::SnapPreferences::isAnyOtherSnappable() const { +// return getSnapModeOthers(); +//} + +bool Inkscape::SnapPreferences::isSnapButtonEnabled(Inkscape::SnapTargetType const target) const +{ + bool always_on = false; + bool group_on = false; + Inkscape::SnapTargetType index = target; + + _mapTargetToArrayIndex(index, always_on, group_on); + + if (_active_snap_targets[index] == -1) { + // Catch coding errors + g_warning("Snap-preferences warning: Using an uninitialized snap target setting"); + } else { + if (index == target) { // I.e. if it has not been re-mapped, then we have a primary target at hand, which does have its own toggle button + return _active_snap_targets[index]; + } else { // If it has been re-mapped though, then this target does not have its own toggle button and therefore the button status cannot be read + g_warning("Snap-preferences warning: Trying to determine the button status of a secondary snap target; However, only primary targets have a button"); + } + } + + return false; +} + + /* Local Variables: mode:c++ |
