summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/tolerance-slider.cpp
blob: 817e783125b0f86db0165eada6de84dd9f99fd5f (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *   Ralf Stephan <ralf@ark.in-berlin.de>
 *   Abhishek Sharma
 *
 * Copyright (C) 2006 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "tolerance-slider.h"

#include "registry.h"

#include <gtkmm/adjustment.h>
#include <gtkmm/box.h>
#include <gtkmm/label.h>
#include <gtkmm/radiobutton.h>
#include <gtkmm/scale.h>

#include "inkscape.h"
#include "document.h"
#include "document-undo.h"
#include "desktop.h"

#include "object/sp-namedview.h"

#include "svg/stringstream.h"

#include "xml/repr.h"

namespace Inkscape {
namespace UI {
namespace Widget {

//===================================================

//---------------------------------------------------



//====================================================

ToleranceSlider::ToleranceSlider(const Glib::ustring& label1, const Glib::ustring& label2, const Glib::ustring& label3, const Glib::ustring& tip1, const Glib::ustring& tip2, const Glib::ustring& tip3, const Glib::ustring& key, Registry& wr)
: _vbox(nullptr)
{
    init(label1, label2, label3, tip1, tip2, tip3, key, wr);
}

ToleranceSlider::~ToleranceSlider()
{
    if (_vbox) delete _vbox;
    _scale_changed_connection.disconnect();
}

void ToleranceSlider::init (const Glib::ustring& label1, const Glib::ustring& label2, const Glib::ustring& label3, const Glib::ustring& tip1, const Glib::ustring& tip2, const Glib::ustring& tip3, const Glib::ustring& key, Registry& wr)
{
    // hbox = label + slider
    //
    // e.g. 
    //
    // snap distance |-------X---| 37
    
    // vbox = checkbutton
    //             +
    //           hbox
    
    _vbox = new Gtk::Box(Gtk::ORIENTATION_VERTICAL);
    _hbox = Gtk::manage(new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL));
    
    Gtk::Label *theLabel1 = Gtk::manage(new Gtk::Label(label1));
    theLabel1->set_use_underline();
    theLabel1->set_halign(Gtk::ALIGN_START);
    theLabel1->set_valign(Gtk::ALIGN_CENTER);
    // align the label with the checkbox text above by indenting 22 px.
    _hbox->pack_start(*theLabel1, Gtk::PACK_EXPAND_WIDGET, 22);

    _hscale = Gtk::manage(new Gtk::Scale(Gtk::ORIENTATION_HORIZONTAL));
    _hscale->set_range(1.0, 51.0);

    theLabel1->set_mnemonic_widget (*_hscale);
    _hscale->set_draw_value (true);
    _hscale->set_value_pos (Gtk::POS_RIGHT);
    _hscale->set_size_request (100, -1);
    _old_val = 10;
    _hscale->set_value (_old_val);
    _hscale->set_tooltip_text (tip1);
    _hbox->add (*_hscale);    
    
    
    Gtk::Label *theLabel2 = Gtk::manage(new Gtk::Label(label2));
    theLabel2->set_use_underline();
    Gtk::Label *theLabel3 = Gtk::manage(new Gtk::Label(label3));
    theLabel3->set_use_underline();    
    _button1 = Gtk::manage(new Gtk::RadioButton);
    _radio_button_group = _button1->get_group();
    _button2 = Gtk::manage(new Gtk::RadioButton);
    _button2->set_group(_radio_button_group);    
    _button1->set_tooltip_text (tip2);
    _button2->set_tooltip_text (tip3);    
    _button1->add (*theLabel3);
    _button1->set_halign(Gtk::ALIGN_START);
    _button1->set_valign(Gtk::ALIGN_CENTER);
    _button2->add (*theLabel2);
    _button2->set_halign(Gtk::ALIGN_START);
    _button2->set_valign(Gtk::ALIGN_CENTER);
    
    _vbox->add (*_button1);
    _vbox->add (*_button2);    
    // Here we need some extra pixels to get the vertical spacing right. Why? 
    _vbox->pack_end(*_hbox, true, true, 3); // add 3 px.  
    _key = key;
    _scale_changed_connection = _hscale->signal_value_changed().connect (sigc::mem_fun (*this, &ToleranceSlider::on_scale_changed));
    _btn_toggled_connection = _button2->signal_toggled().connect (sigc::mem_fun (*this, &ToleranceSlider::on_toggled));
    _wr = &wr;
    _vbox->show_all_children();
}

void ToleranceSlider::setValue (double val)
{
    auto adj = _hscale->get_adjustment();

    adj->set_lower (1.0);
    adj->set_upper (51.0);
    adj->set_step_increment (1.0);

    if (val > 9999.9) // magic value 10000.0
    {
        _button1->set_active (true);
        _button2->set_active (false);        
        _hbox->set_sensitive (false);
        val = 50.0;
    }
    else
    {
        _button1->set_active (false);
        _button2->set_active (true);        
        _hbox->set_sensitive (true);
    }
    _hscale->set_value (val);
    _hbox->show_all();
}

void ToleranceSlider::setLimits (double theMin, double theMax)
{
    _hscale->set_range (theMin, theMax);
    _hscale->get_adjustment()->set_step_increment (1);
}

void ToleranceSlider::on_scale_changed()
{
    update (_hscale->get_value());
}

void ToleranceSlider::on_toggled()
{
    if (!_button2->get_active())
    {
        _old_val = _hscale->get_value();
        _hbox->set_sensitive (false);
        _hbox->show_all();
        setValue (10000.0);
        update (10000.0);
    }
    else
    {
        _hbox->set_sensitive (true);
        _hbox->show_all();
        setValue (_old_val);
        update (_old_val);
    }
}

void ToleranceSlider::update (double val)
{
    if (_wr->isUpdating())
        return;

    SPDesktop *dt = _wr->desktop();
    if (!dt) 
        return;

    Inkscape::SVGOStringStream os;
    os << val;

    _wr->setUpdating (true);

    SPDocument *doc = dt->getDocument();
    bool saved = DocumentUndo::getUndoSensitive(doc);
    DocumentUndo::setUndoSensitive(doc, false);
    Inkscape::XML::Node *repr = dt->getNamedView()->getRepr();
    repr->setAttribute(_key, os.str());
    DocumentUndo::setUndoSensitive(doc, saved);

    doc->setModifiedSinceSave();
    
    _wr->setUpdating (false);
}


} // namespace Dialog
} // namespace UI
} // namespace Inkscape

/*
  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:fileencoding=utf-8:textwidth=99 :