Code

1) Make snapping of object's center optional 2) Optimize snapping preferences dialog
[inkscape.git] / src / ui / widget / tolerance-slider.cpp
1 /** \file
2  *
3  Implementation of tolerance slider widget.
4  *
5  * Authors:
6  *   Ralf Stephan <ralf@ark.in-berlin.de> 
7  *
8  * Copyright (C) 2006 Authors
9  *
10  * Released under GNU GPL.  Read the file 'COPYING' for more information
11  */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #include <gtkmm/adjustment.h>
18 #include <gtkmm/box.h>
19 #include <gtkmm/label.h>
20 #include <gtkmm/scale.h>
22 #include "xml/repr.h"
23 #include "svg/stringstream.h"
25 #include "inkscape.h"
26 #include "document.h"
27 #include "desktop-handles.h"
28 #include "sp-namedview.h"
30 #include "registry.h"
31 #include "tolerance-slider.h"
33 namespace Inkscape {
34 namespace UI {
35 namespace Widget {
37 //===================================================
39 //---------------------------------------------------
43 //====================================================
45 ToleranceSlider::ToleranceSlider()
46 : _vbox(0)
47 {
48 }
50 ToleranceSlider::~ToleranceSlider()
51 {
52     if (_vbox) delete _vbox;
53     _scale_changed_connection.disconnect();
54 }
56 void
57 ToleranceSlider::init (const Glib::ustring& label1, const Glib::ustring& label2, const Glib::ustring& tip1, const Glib::ustring& tip2, const Glib::ustring& key, Registry& wr)
58 {
59     // hbox = label + slider
60     //
61     // e.g. 
62     //
63     // snap distance |-------X---| 37
64     
65     // vbox = checkbutton
66     //             +
67     //           hbox
68     
69     _vbox = new Gtk::VBox;
70     _hbox = manage (new Gtk::HBox);
71     
72     Gtk::Label *theLabel1 = manage (new Gtk::Label (label1));
73     theLabel1->set_use_underline();
74     theLabel1->set_alignment(0, 0.5);
75     // align the label with the checkbox text above by indenting 22 px.
76     _hbox->pack_start(*theLabel1, Gtk::PACK_EXPAND_WIDGET, 22); 
77     _hscale = manage (new Gtk::HScale (1.0, 51, 1.0));
78     theLabel1->set_mnemonic_widget (*_hscale);
79     _hscale->set_draw_value (true);
80     _hscale->set_value_pos (Gtk::POS_RIGHT);
81     _hscale->set_size_request (100, -1);
82     _old_val = 10;
83     _hscale->set_value (_old_val);
84     _tt.set_tip (*_hscale, tip1);
85     _hbox->add (*_hscale);    
86     
87     
88     Gtk::Label *theLabel2 = manage (new Gtk::Label (label2));
89     theLabel2->set_use_underline();
90     _button = manage (new Gtk::CheckButton);
91     _tt.set_tip (*_button, tip2);
92     _button->add (*theLabel2);
93     _button->set_alignment (0.0, 0.5);
94     
95     _vbox->add (*_button);
96     // Here we need some extra pixels to get the vertical spacing right. Why? 
97     _vbox->pack_end(*_hbox, true, true, 3); // add 3 px.  
98     _key = key;
99     _scale_changed_connection = _hscale->signal_value_changed().connect (sigc::mem_fun (*this, &ToleranceSlider::on_scale_changed));
100     _btn_toggled_connection = _button->signal_toggled().connect (sigc::mem_fun (*this, &ToleranceSlider::on_toggled));
101     _wr = &wr;
102     _vbox->show_all_children();
105 void 
106 ToleranceSlider::setValue (double val)
108     Gtk::Adjustment *adj = _hscale->get_adjustment();
110     adj->set_lower (1.0);
111     adj->set_upper (51.0);
112     adj->set_step_increment (1.0);
114     if (val > 9999.9) // magic value 10000.0
115     {
116         _button->set_active (true);
117         _hbox->set_sensitive (false);
118         val = 50.0;
119     }
120     else
121     {
122         _button->set_active (false);
123         _hbox->set_sensitive (true);
124     }
125     _hscale->set_value (val);
126     _hbox->show_all();
129 void
130 ToleranceSlider::setLimits (double theMin, double theMax)
132     _hscale->set_range (theMin, theMax);
133     _hscale->get_adjustment()->set_step_increment (1);
136 void
137 ToleranceSlider::on_scale_changed()
139     update (_hscale->get_value());
142 void
143 ToleranceSlider::on_toggled()
145     if (_button->get_active())
146     {
147         _old_val = _hscale->get_value();
148         _hbox->set_sensitive (false);
149         _hbox->show_all();
150         setValue (10000.0);
151         update (10000.0);
152     }
153     else
154     {
155         _hbox->set_sensitive (true);
156         _hbox->show_all();
157         setValue (_old_val);
158         update (_old_val);
159     }
162 void
163 ToleranceSlider::update (double val)
165     if (_wr->isUpdating())
166         return;
168     SPDesktop *dt = SP_ACTIVE_DESKTOP;
169     if (!dt) 
170         return;
172     Inkscape::SVGOStringStream os;
173     os << val;
175     _wr->setUpdating (true);
177     SPDocument *doc = sp_desktop_document(dt);
178     bool saved = sp_document_get_undo_sensitive (doc);
179     sp_document_set_undo_sensitive (doc, false);
180     Inkscape::XML::Node *repr = SP_OBJECT_REPR (sp_desktop_namedview(dt));
181     repr->setAttribute(_key.c_str(), os.str().c_str());
182     doc->rroot->setAttribute("sodipodi:modified", "true");
183     sp_document_set_undo_sensitive (doc, saved);
184     
185     _wr->setUpdating (false);
189 } // namespace Dialog
190 } // namespace UI
191 } // namespace Inkscape
193 /*
194   Local Variables:
195   mode:c++
196   c-file-style:"stroustrup"
197   c-file-offsets:((innamespace . 0)(inline-open . 0))
198   indent-tabs-mode:nil
199   fill-column:99
200   End:
201 */
202 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :