[glom] Field Formatting: Allow an alternative color for negative values.



commit 726f847ac2c90784ff897ae0711a2810dbc89e4d
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Jan 15 16:47:49 2010 +0100

    Field Formatting: Allow an alternative color for negative values.
    
    * glom/libglom/data_structure/numeric_format.[h|cc]:
    Added m_foreground_color_for_negatives.
    * glom/libglom/data_structure/layout/fieldformatting.[h|cc]:
    Added get_text_format_color_foreground_to_use().
    * glom/libglom/document/document.cc:
    load_after_layout_item_formatting(), save_before_layout_item_formatting():
    load/save the new setting.
    
    * glom/glom_developer.glade: window_formatting: Added negatives color
    and checkbox to the numeric formatting tab.
    * glom/mode_design/layout/layout_item_dialogs/box_formatting.[h|cc]:
    Show/Save the color.
    * glom/utility_widgets/db_adddel/db_adddel.cc:
    treeviewcolumn_on_cell_data()_
    * glom/utility_widgets/entryglom.cc: set_value():
    Use the color for negatives if appropriate.
    * glom/print_layout/canvas_layout_item.cc: Add a TODO to use it here too.

 ChangeLog                                          |   22 +++++++++++
 glom/glom_developer.glade                          |   38 ++++++++++++++++++++
 .../data_structure/layout/fieldformatting.cc       |   12 ++++++
 .../data_structure/layout/fieldformatting.h        |   12 ++++++
 glom/libglom/data_structure/numeric_format.cc      |    4 ++-
 glom/libglom/data_structure/numeric_format.h       |    3 ++
 glom/libglom/document/document.cc                  |    5 +++
 .../layout/layout_item_dialogs/box_formatting.cc   |   19 ++++++++++
 .../layout/layout_item_dialogs/box_formatting.h    |    2 +
 glom/print_layout/canvas_layout_item.cc            |    1 +
 glom/utility_widgets/db_adddel/db_adddel.cc        |   14 +++++++-
 glom/utility_widgets/entryglom.cc                  |   11 ++++++
 12 files changed, 141 insertions(+), 2 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 528392e..bd2262a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,25 @@
+2010-01-15  Murray Cumming  <murrayc murrayc com>
+
+	Field Formatting: Allow an alternative color for negative values.
+
+	* glom/libglom/data_structure/numeric_format.[h|cc]: 
+	Added m_foreground_color_for_negatives.
+	* glom/libglom/data_structure/layout/fieldformatting.[h|cc]:
+	Added get_text_format_color_foreground_to_use().
+	* glom/libglom/document/document.cc: 
+	load_after_layout_item_formatting(), save_before_layout_item_formatting():
+	load/save the new setting.
+
+	* glom/glom_developer.glade: window_formatting: Added negatives color 
+	and checkbox to the numeric formatting tab.
+	* glom/mode_design/layout/layout_item_dialogs/box_formatting.[h|cc]:
+	Show/Save the color.
+	* glom/utility_widgets/db_adddel/db_adddel.cc: 
+	treeviewcolumn_on_cell_data()_
+	* glom/utility_widgets/entryglom.cc: set_value():
+	Use the color for negatives if appropriate. 
+	* glom/print_layout/canvas_layout_item.cc: Add a TODO to use it here too.
+
 2010-01-14  Murray Cumming  <murrayc murrayc com>
 
 	libglom: Some API documentation.
diff --git a/glom/glom_developer.glade b/glom/glom_developer.glade
index d13c7ba..31bd473 100644
--- a/glom/glom_developer.glade
+++ b/glom/glom_developer.glade
@@ -6393,6 +6393,44 @@ Which user should be added to this group?</property>
                     <property name="position">2</property>
                   </packing>
                 </child>
+                <child>
+                  <object class="GtkHBox" id="hbox_color_negatives">
+                    <property name="visible">True</property>
+                    <property name="spacing">6</property>
+                    <child>
+                      <object class="GtkCheckButton" id="checkbutton_foreground_negatives">
+                        <property name="label" translatable="yes">Color for Negative Values:</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="receives_default">False</property>
+                        <property name="tooltip_text" translatable="yes">Click this check box to use a different foreground color to display negative values.</property>
+                        <property name="use_underline">True</property>
+                        <property name="draw_indicator">True</property>
+                      </object>
+                      <packing>
+                        <property name="expand">False</property>
+                        <property name="fill">False</property>
+                        <property name="position">0</property>
+                      </packing>
+                    </child>
+                    <child>
+                      <object class="GtkColorButton" id="colorbutton_foreground_negatives">
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="receives_default">False</property>
+                      </object>
+                      <packing>
+                        <property name="pack_type">end</property>
+                        <property name="position">1</property>
+                      </packing>
+                    </child>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">False</property>
+                    <property name="position">3</property>
+                  </packing>
+                </child>
               </object>
             </child>
             <child type="tab">
diff --git a/glom/libglom/data_structure/layout/fieldformatting.cc b/glom/libglom/data_structure/layout/fieldformatting.cc
index ae82cf7..ae4ef11 100644
--- a/glom/libglom/data_structure/layout/fieldformatting.cc
+++ b/glom/libglom/data_structure/layout/fieldformatting.cc
@@ -19,6 +19,7 @@
  */
 
 #include "fieldformatting.h"
+#include <glom/libglom/data_structure/glomconversions.h>
 #include <glibmm/i18n.h>
 
 const guint MULTILINE_TEXT_DEFAULT_HEIGHT_LINES = 6;
@@ -141,6 +142,17 @@ Glib::ustring FieldFormatting::get_text_format_color_foreground() const
   return m_text_color_foreground;
 }
 
+Glib::ustring FieldFormatting::get_text_format_color_foreground_to_use(const Gnome::Gda::Value& value) const
+{
+  if(!m_numeric_format.m_foreground_color_for_negatives.empty())
+  {
+    if(Conversions::get_double_for_gda_value_numeric(value) < 0)
+      return m_numeric_format.m_foreground_color_for_negatives;
+  }
+
+  return m_text_color_foreground;
+}
+
 void FieldFormatting::set_text_format_color_background(const Glib::ustring& color)
 {
   m_text_color_background = color;
diff --git a/glom/libglom/data_structure/layout/fieldformatting.h b/glom/libglom/data_structure/layout/fieldformatting.h
index c7100a7..373e507 100644
--- a/glom/libglom/data_structure/layout/fieldformatting.h
+++ b/glom/libglom/data_structure/layout/fieldformatting.h
@@ -76,7 +76,19 @@ public:
    */
   Glib::ustring get_text_format_font() const;
 
+  /** Set the foreground color to use for text when displaying a field value.
+   */
   void set_text_format_color_foreground(const Glib::ustring& color);
+
+  /** Get the foreground color to use for text for the specified value,
+   * taking the negative-color into account, if specified.
+   */
+  Glib::ustring get_text_format_color_foreground_to_use(const Gnome::Gda::Value& value) const;
+
+  /** Get the foreground color to use for text when displaying a field value.
+   * This should be overriden by by m_numeric_formatting.m_foreground_color_for_negatives
+   * if that is active.
+   */
   Glib::ustring get_text_format_color_foreground() const;
 
   void set_text_format_color_background(const Glib::ustring& color);
diff --git a/glom/libglom/data_structure/numeric_format.cc b/glom/libglom/data_structure/numeric_format.cc
index 09f246f..5f40f3f 100644
--- a/glom/libglom/data_structure/numeric_format.cc
+++ b/glom/libglom/data_structure/numeric_format.cc
@@ -45,6 +45,7 @@ NumericFormat& NumericFormat::operator=(const NumericFormat& src)
   m_use_thousands_separator = src.m_use_thousands_separator;
   m_decimal_places_restricted = src.m_decimal_places_restricted;
   m_decimal_places = src.m_decimal_places;
+  m_foreground_color_for_negatives = src.m_foreground_color_for_negatives;
 
   return *this;
 }
@@ -54,7 +55,8 @@ bool NumericFormat::operator==(const NumericFormat& src) const
   return (m_currency_symbol == src.m_currency_symbol) && 
          (m_use_thousands_separator == src.m_use_thousands_separator) &&
          (m_decimal_places_restricted == src.m_decimal_places_restricted) &&
-         (m_decimal_places == src.m_decimal_places);
+         (m_decimal_places == src.m_decimal_places) && 
+         (m_foreground_color_for_negatives == src.m_foreground_color_for_negatives);
 }
 
 bool NumericFormat::operator!=(const NumericFormat& src) const
diff --git a/glom/libglom/data_structure/numeric_format.h b/glom/libglom/data_structure/numeric_format.h
index f11b7f9..cc5e7ab 100644
--- a/glom/libglom/data_structure/numeric_format.h
+++ b/glom/libglom/data_structure/numeric_format.h
@@ -50,6 +50,9 @@ public:
   bool m_use_thousands_separator; //Setting this to false would override the locale, if it used a 1000s separator.
   bool m_decimal_places_restricted;
   guint m_decimal_places;
+  
+  /// The foreground color to use for negative values.
+  Glib::ustring m_foreground_color_for_negatives;
 };
 
 } //namespace Glom
diff --git a/glom/libglom/document/document.cc b/glom/libglom/document/document.cc
index c9f340f..a450e12 100644
--- a/glom/libglom/document/document.cc
+++ b/glom/libglom/document/document.cc
@@ -175,6 +175,7 @@ namespace Glom
 #define GLOM_ATTRIBUTE_FORMAT_DECIMAL_PLACES_RESTRICTED "format_decimal_places_restricted"
 #define GLOM_ATTRIBUTE_FORMAT_DECIMAL_PLACES "format_decimal_places"
 #define GLOM_ATTRIBUTE_FORMAT_CURRENCY_SYMBOL "format_currency_symbol"
+#define GLOM_ATTRIBUTE_FORMAT_NEGATIVE_COLOR "color_fg_negative"
 
 #define GLOM_ATTRIBUTE_FORMAT_TEXT_MULTILINE "format_text_multiline"
 #define GLOM_ATTRIBUTE_FORMAT_TEXT_MULTILINE_HEIGHT_LINES "format_text_multiline_height_lines"
@@ -1884,6 +1885,8 @@ void Document::load_after_layout_item_formatting(const xmlpp::Element* element,
     format.m_numeric_format.m_decimal_places_restricted = get_node_attribute_value_as_bool(element, GLOM_ATTRIBUTE_FORMAT_DECIMAL_PLACES_RESTRICTED);
     format.m_numeric_format.m_decimal_places = get_node_attribute_value_as_decimal(element, GLOM_ATTRIBUTE_FORMAT_DECIMAL_PLACES);
     format.m_numeric_format.m_currency_symbol = get_node_attribute_value(element, GLOM_ATTRIBUTE_FORMAT_CURRENCY_SYMBOL);
+    format.m_numeric_format.m_foreground_color_for_negatives = 
+      get_node_attribute_value(element, GLOM_ATTRIBUTE_FORMAT_NEGATIVE_COLOR);
   }
 
   //Text formatting:
@@ -2874,6 +2877,8 @@ void Document::save_before_layout_item_formatting(xmlpp::Element* nodeItem, cons
     set_node_attribute_value_as_bool(nodeItem, GLOM_ATTRIBUTE_FORMAT_DECIMAL_PLACES_RESTRICTED, format.m_numeric_format.m_decimal_places_restricted);
     set_node_attribute_value_as_decimal(nodeItem, GLOM_ATTRIBUTE_FORMAT_DECIMAL_PLACES, format.m_numeric_format.m_decimal_places);
     set_node_attribute_value(nodeItem, GLOM_ATTRIBUTE_FORMAT_CURRENCY_SYMBOL, format.m_numeric_format.m_currency_symbol);
+    set_node_attribute_value(nodeItem, GLOM_ATTRIBUTE_FORMAT_NEGATIVE_COLOR,
+      format.m_numeric_format.m_foreground_color_for_negatives);
 
     set_node_attribute_value_as_bool(nodeItem, GLOM_ATTRIBUTE_FORMAT_CHOICES_RESTRICTED, format.get_choices_restricted());
     set_node_attribute_value_as_bool(nodeItem, GLOM_ATTRIBUTE_FORMAT_CHOICES_CUSTOM, format.get_has_custom_choices());
diff --git a/glom/mode_design/layout/layout_item_dialogs/box_formatting.cc b/glom/mode_design/layout/layout_item_dialogs/box_formatting.cc
index 120889c..93877b3 100644
--- a/glom/mode_design/layout/layout_item_dialogs/box_formatting.cc
+++ b/glom/mode_design/layout/layout_item_dialogs/box_formatting.cc
@@ -28,6 +28,12 @@ namespace Glom
 Box_Formatting::Box_Formatting(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
 : Gtk::VBox(cobject),
   m_vbox_numeric_format(0),
+  m_checkbox_format_use_thousands(0),
+  m_checkbox_format_use_decimal_places(0),
+  m_entry_format_decimal_places(0),
+  m_entry_currency_symbol(0),
+  m_checkbox_format_color_negatives(0),
+  m_colorbutton_negatives(0),
   m_vbox_text_format(0),
   m_checkbox_format_text_multiline(0),
   m_label_format_text_multiline_height(0),
@@ -55,6 +61,8 @@ Box_Formatting::Box_Formatting(BaseObjectType* cobject, const Glib::RefPtr<Gtk::
   builder->get_widget("checkbutton_format_use_decimal_places", m_checkbox_format_use_decimal_places);
   builder->get_widget("entry_format_decimal_places", m_entry_format_decimal_places);
   builder->get_widget_derived("entry_currency_symbol", m_entry_currency_symbol);
+  builder->get_widget("colorbutton_foreground_negatives", m_colorbutton_negatives);
+  builder->get_widget("checkbutton_foreground_negatives", m_checkbox_format_color_negatives);
 
   //Text formatting:
   builder->get_widget("vbox_text_format", m_vbox_text_format);
@@ -95,6 +103,7 @@ Box_Formatting::Box_Formatting(BaseObjectType* cobject, const Glib::RefPtr<Gtk::
   m_checkbox_format_text_font->signal_toggled().connect( sigc::mem_fun(*this, &Box_Formatting::on_checkbox) );
   m_checkbox_format_text_color_foreground->signal_toggled().connect( sigc::mem_fun(*this, &Box_Formatting::on_checkbox) );
   m_checkbox_format_text_color_background->signal_toggled().connect( sigc::mem_fun(*this, &Box_Formatting::on_checkbox) );
+  m_checkbox_format_color_negatives->signal_toggled().connect( sigc::mem_fun(*this, &Box_Formatting::on_checkbox) );
 
   show_all_children();
 }
@@ -143,6 +152,11 @@ void Box_Formatting::set_formatting(const FieldFormatting& format)
 
   m_entry_currency_symbol->get_entry()->set_text(format.m_numeric_format.m_currency_symbol);
 
+  const Glib::ustring color_negatives = format.m_numeric_format.m_foreground_color_for_negatives;
+  m_checkbox_format_color_negatives->set_active(!color_negatives.empty());
+  m_colorbutton_negatives->set_color( Gdk::Color(color_negatives) );
+
+
   m_checkbox_format_text_multiline->set_active(format.get_text_format_multiline());
 
   m_spinbutton_format_text_multiline_height->set_value(format.get_text_format_multiline_height_lines());
@@ -209,6 +223,11 @@ bool Box_Formatting::get_formatting(FieldFormatting& format) const
 
   m_format.m_numeric_format.m_currency_symbol = m_entry_currency_symbol->get_entry()->get_text();
 
+  Glib::ustring color_negatives;
+  if(m_checkbox_format_color_negatives->get_active())
+    color_negatives = m_colorbutton_negatives->get_color().to_string();
+  m_format.m_numeric_format.m_foreground_color_for_negatives = color_negatives;
+
   //Text formatting:
   m_format.set_text_format_multiline(m_checkbox_format_text_multiline->get_active());
   m_format.set_text_format_multiline_height_lines( m_spinbutton_format_text_multiline_height->get_value_as_int() );
diff --git a/glom/mode_design/layout/layout_item_dialogs/box_formatting.h b/glom/mode_design/layout/layout_item_dialogs/box_formatting.h
index c4d8ea5..f2a392e 100644
--- a/glom/mode_design/layout/layout_item_dialogs/box_formatting.h
+++ b/glom/mode_design/layout/layout_item_dialogs/box_formatting.h
@@ -69,6 +69,8 @@ private:
   Gtk::CheckButton* m_checkbox_format_use_decimal_places;
   Gtk::Entry* m_entry_format_decimal_places;
   ComboEntry_Currency* m_entry_currency_symbol;
+  Gtk::CheckButton* m_checkbox_format_color_negatives;
+  Gtk::ColorButton* m_colorbutton_negatives;
 
   Gtk::VBox* m_vbox_text_format;
   Gtk::CheckButton* m_checkbox_format_text_multiline;
diff --git a/glom/print_layout/canvas_layout_item.cc b/glom/print_layout/canvas_layout_item.cc
index b2ed8ae..e8d35f8 100644
--- a/glom/print_layout/canvas_layout_item.cc
+++ b/glom/print_layout/canvas_layout_item.cc
@@ -92,6 +92,7 @@ void CanvasLayoutItem::check_and_apply_formatting(const Glib::RefPtr<CanvasTextM
   canvas_item->set_font_points(font);
 
   //TODO: Are these sensible properties? Maybe we need to use markup:
+  //TODO: Use the negative color.
   const Glib::ustring fg = formatting.get_text_format_color_foreground();
   if(!fg.empty())
   {
diff --git a/glom/utility_widgets/db_adddel/db_adddel.cc b/glom/utility_widgets/db_adddel/db_adddel.cc
index 77bd7b5..e25eda2 100644
--- a/glom/utility_widgets/db_adddel/db_adddel.cc
+++ b/glom/utility_widgets/db_adddel/db_adddel.cc
@@ -2234,7 +2234,8 @@ void DbAddDel::treeviewcolumn_on_cell_data(Gtk::CellRenderer* renderer, const Gt
          std::cout << "    GType name=\"" << g_type_name(debug_type) << "\"" << std::endl; 
       */
 
-      switch(field->get_glom_type())
+      const Field::glom_field_type type = field->get_glom_type();
+      switch(type)
       {
         case(Field::TYPE_BOOLEAN):
         {
@@ -2274,6 +2275,17 @@ void DbAddDel::treeviewcolumn_on_cell_data(Gtk::CellRenderer* renderer, const Gt
             g_object_set(pDerived->gobj(), "text", text.c_str(), (gpointer)0);
           }
 
+          //Show a different color if the value is numeric, if that's specified:
+          if(type == Field::TYPE_NUMERIC)
+          {
+             const Glib::ustring fg_color = 
+               field->get_formatting_used().get_text_format_color_foreground_to_use(value);
+             if(!fg_color.empty())
+                 g_object_set(pDerived->gobj(), "foreground", fg_color.c_str(), (gpointer)0);
+             else
+                 g_object_set(pDerived->gobj(), "foreground", (const char*)0, (gpointer)0);
+          }
+
           break;
         } 
       }
diff --git a/glom/utility_widgets/entryglom.cc b/glom/utility_widgets/entryglom.cc
index 0f4c671..ad4ab85 100644
--- a/glom/utility_widgets/entryglom.cc
+++ b/glom/utility_widgets/entryglom.cc
@@ -169,6 +169,17 @@ void EntryGlom::set_value(const Gnome::Gda::Value& value)
     const Glib::ustring text = Conversions::get_text_for_gda_value(m_glom_type, value, layout_item->get_formatting_used().m_numeric_format);
     set_text(text);
 
+    //Show a different color if the value is numeric, if that's specified:
+    if(layout_item->get_glom_type() == Field::TYPE_NUMERIC)
+    {
+      const Glib::ustring fg_color = 
+      layout_item->get_formatting_used().get_text_format_color_foreground_to_use(value);
+      if(!fg_color.empty())
+        modify_text(Gtk::STATE_NORMAL, Gdk::Color(fg_color));
+      else
+        modify_text(Gtk::STATE_NORMAL, Gdk::Color());
+    }
+
     //std::cout << "debug: EntryGlom::set_value(): name=" << layout_item->get_name() << ", text=" << text << std::endl;
   }
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]