summaryrefslogtreecommitdiffstats
path: root/src/ui
diff options
context:
space:
mode:
authorJohan B. C. Engelen <jbc.engelen@swissonline.ch>2011-04-13 22:04:49 +0000
committerJohan Engelen <goejendaagh@zonnet.nl>2011-04-13 22:04:49 +0000
commitfe875852760883dd20b6a57b57be50de83e37fc2 (patch)
tree60832d184f5899591b918ff551bb561aca6846a1 /src/ui
parentBreton translation update (diff)
downloadinkscape-fe875852760883dd20b6a57b57be50de83e37fc2.tar.gz
inkscape-fe875852760883dd20b6a57b57be50de83e37fc2.zip
add expression evaluator for spinbox input boxes. also knows a little about units.
needs more work to fully integrate it in all of inkscape spinboxes also needs documentation rework (bzr r10162)
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/dialog/guides.cpp3
-rw-r--r--src/ui/widget/preferences-widget.cpp1
-rw-r--r--src/ui/widget/scalar-unit.cpp3
-rw-r--r--src/ui/widget/scalar.cpp3
-rw-r--r--src/ui/widget/spinbutton.cpp41
-rw-r--r--src/ui/widget/spinbutton.h24
6 files changed, 47 insertions, 28 deletions
diff --git a/src/ui/dialog/guides.cpp b/src/ui/dialog/guides.cpp
index 619b6fb16..fd64a713f 100644
--- a/src/ui/dialog/guides.cpp
+++ b/src/ui/dialog/guides.cpp
@@ -194,9 +194,7 @@ void GuidelinePropertiesDialog::_setup() {
sp_unit_selector_add_adjustment(SP_UNIT_SELECTOR(unit_selector), GTK_ADJUSTMENT(_adjustment_x.gobj()));
sp_unit_selector_add_adjustment(SP_UNIT_SELECTOR(unit_selector), GTK_ADJUSTMENT(_adjustment_y.gobj()));
_spin_button_x.configure(_adjustment_x, 1.0 , 3);
- _spin_button_x.set_numeric();
_spin_button_y.configure(_adjustment_y, 1.0 , 3);
- _spin_button_y.set_numeric();
_layout_table.attach(_label_X,
1, 2, 4, 5, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
_layout_table.attach(_spin_button_x,
@@ -213,7 +211,6 @@ void GuidelinePropertiesDialog::_setup() {
// angle spinbutton
_spin_angle.configure(_adj_angle, 5.0 , 3);
- _spin_angle.set_numeric();
_spin_angle.show();
_layout_table.attach(_label_degrees,
1, 2, 8, 9, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
diff --git a/src/ui/widget/preferences-widget.cpp b/src/ui/widget/preferences-widget.cpp
index afcaa338e..f92e2518d 100644
--- a/src/ui/widget/preferences-widget.cpp
+++ b/src/ui/widget/preferences-widget.cpp
@@ -227,7 +227,6 @@ void PrefSpinButton::init(Glib::ustring const &prefs_path,
this->set_range (lower, upper);
this->set_increments (step_increment, 0);
- this->set_numeric();
this->set_value (value);
this->set_width_chars(6);
if (is_int)
diff --git a/src/ui/widget/scalar-unit.cpp b/src/ui/widget/scalar-unit.cpp
index 6209d40e0..e00e82198 100644
--- a/src/ui/widget/scalar-unit.cpp
+++ b/src/ui/widget/scalar-unit.cpp
@@ -27,6 +27,7 @@
#endif
#include "scalar-unit.h"
+#include "spinbutton.h"
namespace Inkscape {
namespace UI {
@@ -65,6 +66,8 @@ ScalarUnit::ScalarUnit(Glib::ustring const &label, Glib::ustring const &tooltip,
}
_unit_menu->signal_changed()
.connect_notify(sigc::mem_fun(*this, &ScalarUnit::on_unit_changed));
+
+ static_cast<SpinButton*>(_widget)->setUnitMenu(_unit_menu);
}
/**
diff --git a/src/ui/widget/scalar.cpp b/src/ui/widget/scalar.cpp
index eda8cd2cc..6ada379fb 100644
--- a/src/ui/widget/scalar.cpp
+++ b/src/ui/widget/scalar.cpp
@@ -41,7 +41,6 @@ Scalar::Scalar(Glib::ustring const &label, Glib::ustring const &tooltip,
: Labelled(label, tooltip, new SpinButton(), suffix, icon, mnemonic),
setProgrammatically(false)
{
- static_cast<SpinButton*>(_widget)->set_numeric();
}
/**
@@ -63,7 +62,6 @@ Scalar::Scalar(Glib::ustring const &label, Glib::ustring const &tooltip,
: Labelled(label, tooltip, new SpinButton(0.0, digits), suffix, icon, mnemonic),
setProgrammatically(false)
{
- static_cast<SpinButton*>(_widget)->set_numeric();
}
/**
@@ -87,7 +85,6 @@ Scalar::Scalar(Glib::ustring const &label, Glib::ustring const &tooltip,
: Labelled(label, tooltip, new SpinButton(adjust, 0.0, digits), suffix, icon, mnemonic),
setProgrammatically(false)
{
- static_cast<SpinButton*>(_widget)->set_numeric();
}
/** Fetches the precision of the spin buton */
diff --git a/src/ui/widget/spinbutton.cpp b/src/ui/widget/spinbutton.cpp
index 55c2d877f..22bc30bb2 100644
--- a/src/ui/widget/spinbutton.cpp
+++ b/src/ui/widget/spinbutton.cpp
@@ -16,32 +16,39 @@
#include "spinbutton.h"
-#include <locale.h>
+#include "unit-menu.h"
+#include "util/expression-evaluator.h"
namespace Inkscape {
namespace UI {
namespace Widget {
-void
-SpinButton::on_insert_text(const Glib::ustring& text, int* position)
+/**
+ * This callback function should try to convert the entered text to a number and write it to newvalue.
+ * It calls a method to evaluate the (potential) mathematical expression.
+ *
+ * @retval false No conversion done, continue with default handler.
+ * @retval true Conversion successful, don't call default handler.
+ */
+int
+SpinButton::on_input(double* newvalue)
{
- Glib::ustring newtext = text;
-
- // if in numeric mode: replace '.' or ',' with the locale's decimal point
- if (get_numeric()) {
- size_t found = newtext.find('.');
- if (found != Glib::ustring::npos) {
- newtext.replace(found, 1, localeconv()->decimal_point);
- } else {
- found = newtext.find(',');
- if (found != Glib::ustring::npos) {
- newtext.replace(found, 1, localeconv()->decimal_point);
- }
+ try {
+ Inkscape::Util::GimpEevlQuantity result = Inkscape::Util::gimp_eevl_evaluate (get_text().c_str(), _unit_menu ? &_unit_menu->getUnit() : NULL);
+ // check if output dimension corresponds to input unit
+ if (_unit_menu && result.dimension != (_unit_menu->getUnit().isAbsolute() ? 1 : 0) ) {
+ throw Inkscape::Util::EvaluatorException("Input dimensions do not match with parameter dimensions.","");
}
+
+ *newvalue = result.value;
+ }
+ catch(Inkscape::Util::EvaluatorException &e) {
+ g_message ("%s", e.what());
+
+ return false;
}
- // call parent function with replaced text:
- Gtk::SpinButton::on_insert_text(newtext, position);
+ return true;
}
} // namespace Widget
diff --git a/src/ui/widget/spinbutton.h b/src/ui/widget/spinbutton.h
index 408310d09..0eb58bb9e 100644
--- a/src/ui/widget/spinbutton.h
+++ b/src/ui/widget/spinbutton.h
@@ -19,21 +19,37 @@ namespace Inkscape {
namespace UI {
namespace Widget {
+class UnitMenu;
+
/**
- * SpinButton widget, that allows entry of both '.' and ',' for the decimal, even when in numeric mode.
+ * SpinButton widget, that allows entry of simple math expressions (also units, when linked with UnitMenu).
+ *
+ * Calling "set_numeric()" effectively disables the expression parsing. If no unit menu is linked, all unitlike characters are ignored.
*/
class SpinButton : public Gtk::SpinButton
{
public:
SpinButton(double climb_rate = 0.0, guint digits = 0)
- : Gtk::SpinButton(climb_rate, digits) {};
+ : Gtk::SpinButton(climb_rate, digits),
+ _unit_menu(NULL)
+ {
+ signal_input().connect(sigc::mem_fun(*this, &SpinButton::on_input));
+ };
explicit SpinButton(Gtk::Adjustment& adjustment, double climb_rate = 0.0, guint digits = 0)
- : Gtk::SpinButton(adjustment, climb_rate, digits) {};
+ : Gtk::SpinButton(adjustment, climb_rate, digits),
+ _unit_menu(NULL)
+ {
+ signal_input().connect(sigc::mem_fun(*this, &SpinButton::on_input));
+ };
virtual ~SpinButton() {};
+ void setUnitMenu(UnitMenu* unit_menu) { _unit_menu = unit_menu; };
+
protected:
- virtual void on_insert_text(const Glib::ustring& text, int* position);
+ UnitMenu *_unit_menu; /// Linked unit menu for unit conversion in entered expressions.
+
+ int on_input(double* newvalue);
private:
// noncopyable