summaryrefslogtreecommitdiffstats
path: root/src/actions/actions-node-align.cpp
blob: 89e446af5e1c8bbc2ba8127b5d02a1d8450bd387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Gio::Actions for aligning and distributing objects without GUI.
 *
 * Copyright (C) 2020 Tavmjong Bah
 *
 * Some code and ideas from src/ui/dialogs/align-and-distribute.cpp
 *   Authors: Bryce Harrington
 *            Martin Owens
 *            John Smith
 *            Patrick Storz
 *            Jabier Arraiza
 *
 * The contents of this file may be used under the GNU General Public License Version 2 or later.
 *
 * To do: Remove GUI dependency!
 */

#include "actions-node-align.h"

#include <iostream>
#include <limits>

#include <giomm.h>  // Not <gtkmm.h>! To eventually allow a headless version!
#include <glibmm/i18n.h>

#include <2geom/coord.h>

#include "inkscape-application.h"
#include "inkscape-window.h"
#include "ui/tool/node-types.h"
#include "ui/tool/multi-path-manipulator.h" // Node align/distribute
#include "ui/tools/node-tool.h"             // Node align/distribute

using Inkscape::UI::AlignTargetNode;

void
node_align(const Glib::VariantBase& value, InkscapeWindow* win, Geom::Dim2 direction)
{
    auto tool = win->get_desktop()->getEventContext();
    auto node_tool = dynamic_cast<Inkscape::UI::Tools::NodeTool*>(tool);
    if (node_tool) {
    } else {
        std::cerr << "node_align: tool is not Node tool!" << std::endl;
        return;
    }

    Glib::Variant<Glib::ustring> s = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring> >(value);
    std::vector<Glib::ustring> tokens = Glib::Regex::split_simple(" ", s.get());
    if (tokens.size() > 1) {
        std::cerr << "node_align: too many arguments!" << std::endl;
        return;
    }

    // clang-format off
    auto target = AlignTargetNode::MID_NODE;
    if (tokens.size() == 1) {
        std::string token = tokens[0];
        if (token == "pref") {
            Inkscape::Preferences *prefs = Inkscape::Preferences::get();
            token = prefs->getString("/dialogs/align/nodes-align-to", "first");
        }
        if      (token == "last"   ) target = AlignTargetNode::LAST_NODE;
        else if (token == "first"  ) target = AlignTargetNode::FIRST_NODE;
        else if (token == "middle" ) target = AlignTargetNode::MID_NODE;
        else if (token == "min"    ) target = AlignTargetNode::MIN_NODE;
        else if (token == "max"    ) target = AlignTargetNode::MAX_NODE;
    }
    // clang-format on
    node_tool->_multipath->alignNodes(direction, target);
} 

void
node_distribute(InkscapeWindow* win, Geom::Dim2 direction)
{
    auto tool = win->get_desktop()->getEventContext();
    auto node_tool = dynamic_cast<Inkscape::UI::Tools::NodeTool*>(tool);
    if (node_tool) {
    } else {
        std::cerr << "node_distribute: tool is not Node tool!" << std::endl;
        return;
    }

    node_tool->_multipath->distributeNodes(direction);
} 

std::vector<std::vector<Glib::ustring>> raw_data_node_align =
{
    // clang-format off
    {"win.node-align-horizontal",       N_("Align nodes horizontally"),      "Node", N_("Align selected nodes horizontally; usage [last|first|middle|min|max|pref]" )},
    {"win.node-align-vertical",         N_("Align nodes vertically"),        "Node", N_("Align selected nodes vertically; usage [last|first|middle|min|max|pref]"   )},
    {"win.node-distribute-horizontal",  N_("Distribute nodes horizontally"), "Node", N_("Distribute selected nodes horizontally."                              )},
    {"win.node-distribute-vertical",    N_("Distribute nodes vertically"),   "Node", N_("Distribute selected nodes vertically."                                )}
    // clang-format on
};

std::vector<std::vector<Glib::ustring>> hint_data_node_align =
{
    // clang-format off
    {"app.node-align-horizontal",      N_("Enter string for alignment anchor, one of: first/last/middle/min/max")},
    {"app.node-align-vertical",        N_("Enter string for alignment anchor, one of: first/last/middle/min/max")},
    // clang-format on
};

// These are window actions as the require the node tool to be active and nodes to be selected.
void
add_actions_node_align(InkscapeWindow* win)
{
    Glib::VariantType String(Glib::VARIANT_TYPE_STRING);

    // clang-format off
    win->add_action_with_parameter( "node-align-horizontal",      String, sigc::bind<InkscapeWindow*, Geom::Dim2>(sigc::ptr_fun(&node_align),      win, Geom::X));
    win->add_action_with_parameter( "node-align-vertical",        String, sigc::bind<InkscapeWindow*, Geom::Dim2>(sigc::ptr_fun(&node_align),      win, Geom::Y));
    win->add_action(                "node-distribute-horizontal",         sigc::bind<InkscapeWindow*, Geom::Dim2>(sigc::ptr_fun(&node_distribute), win, Geom::X));
    win->add_action(                "node-distribute-vertical",           sigc::bind<InkscapeWindow*, Geom::Dim2>(sigc::ptr_fun(&node_distribute), win, Geom::Y));
    // clang-format on

    auto app = InkscapeApplication::instance();
    if (!app) {
        std::cerr << "add_actions_node_align: no app!" << std::endl;
        return;
    }
    app->get_action_extra_data().add_data(raw_data_node_align);
}


/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :