[glom] Print Layout: Avoid some code duplication.



commit 2773962fa93af7ff515dc68cb1e51c596b4721f4
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Oct 12 23:42:35 2011 +0200

    Print Layout: Avoid some code duplication.
    
    * glom/print_layout/print_layout_utils.[h|cc]: moved get_page_height()
    from CanvasPrintLayout to here. And actually get the margins.
    * glom/print_layout/canvas_print_layout.cc: get_page_y_start_and_end():
    Use it here.
    get_page_height(): Forward to the PrintLayoutUtils versions, passing
    the extra parameters.

 ChangeLog                                |   11 +++++++
 glom/print_layout/canvas_print_layout.cc |   28 +++++-------------
 glom/print_layout/print_layout_utils.cc  |   45 +++++++++++++++++++++++++----
 glom/print_layout/print_layout_utils.h   |    3 ++
 4 files changed, 60 insertions(+), 27 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index d6ad645..15efe3e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2011-10-12  Murray Cumming  <murrayc murrayc com>
 
+	Print Layout: Avoid some code duplication.
+
+	* glom/print_layout/print_layout_utils.[h|cc]: moved get_page_height()
+	from CanvasPrintLayout to here. And actually get the margins.
+	* glom/print_layout/canvas_print_layout.cc: get_page_y_start_and_end():
+	Use it here.
+	get_page_height(): Forward to the PrintLayoutUtils versions, passing 
+	the extra parameters.
+
+2011-10-12  Murray Cumming  <murrayc murrayc com>
+
 	Print Layout: Move items to next page when expanding portals.
 
 	* glom/print_layout/canvas_print_layout.[h|cc]: move_items_below_item():
diff --git a/glom/print_layout/canvas_print_layout.cc b/glom/print_layout/canvas_print_layout.cc
index 28979e4..6e13492 100644
--- a/glom/print_layout/canvas_print_layout.cc
+++ b/glom/print_layout/canvas_print_layout.cc
@@ -30,6 +30,7 @@
 #include <glom/utility_widgets/canvas/canvas_table_movable.h>
 #include <glom/utility_widgets/canvas/canvas_image_movable.h>
 #include <glom/utility_widgets/canvas/canvas_text_movable.h>
+#include <glom/print_layout/print_layout_utils.h>
 #include <glom/application.h>
 #include <libglom/data_structure/glomconversions.h>
 #include <libglom/db_utils.h>
@@ -1255,15 +1256,10 @@ guint Canvas_PrintLayout::get_page_for_y(double y) const
     return 0; //Avoid a division by zero.
      
   const double pages = y / (double)page_height;
-  std::cout << "pages = " << pages << ", for y=" << y << ", page_height=" << page_height << std::endl;
-  
   double pages_integral = 0;
   const double pages_fractional = modf(pages, &pages_integral);
-  std::cout << "pages_integral =" <<  pages_integral << std::endl;
-  
+
   const guint pages_full = (guint)pages_integral + (pages_fractional ? 1 : 0);
- std::cout << "pages_full =" <<  pages_full << std::endl;
-  
   return pages_full;
 }
 
@@ -1304,30 +1300,22 @@ double Canvas_PrintLayout::move_fully_to_page(const Glib::RefPtr<CanvasLayoutIte
     moved = false;
   }
 
-  item->set_xy(x, y);
+  if(moved)
+    item->set_xy(x, y);
+
   return y;
 }
 
 double Canvas_PrintLayout::get_page_height() const
 {
-  double margin_top = 0;
-  double margin_bottom = 0;
-  return get_page_height(margin_top, margin_bottom);
+  const Glib::RefPtr<const Gtk::PageSetup> page_setup = get_page_setup(); 
+  return PrintLayoutUtils::get_page_height(page_setup, property_units());
 }
 
 double Canvas_PrintLayout::get_page_height(double& margin_top, double& margin_bottom) const
 {
   const Glib::RefPtr<const Gtk::PageSetup> page_setup = get_page_setup();
-  const Gtk::PaperSize paper_size = page_setup->get_paper_size();
-  const Gtk::Unit units = property_units();
-  
-  double page_height = 0;
-  if(page_setup->get_orientation() == Gtk::PAGE_ORIENTATION_PORTRAIT) //TODO: Handle the reverse orientations too?
-    page_height = paper_size.get_height(units);
-  else
-    page_height = paper_size.get_width(units);
-
-  return page_height;
+  return PrintLayoutUtils::get_page_height(page_setup, property_units(), margin_top, margin_bottom);
 }
 
 } //namespace Glom
diff --git a/glom/print_layout/print_layout_utils.cc b/glom/print_layout/print_layout_utils.cc
index 991bd42..70f1772 100644
--- a/glom/print_layout/print_layout_utils.cc
+++ b/glom/print_layout/print_layout_utils.cc
@@ -36,6 +36,38 @@ static Gtk::Unit get_units()
   return Gtk::UNIT_MM;
 }
 
+double get_page_height(const Glib::RefPtr<const Gtk::PageSetup>& page_setup, Gtk::Unit units)
+{
+  double margin_top = 0;
+  double margin_bottom = 0;
+  return get_page_height(page_setup, units, margin_top, margin_bottom);
+}
+
+double get_page_height(const Glib::RefPtr<const Gtk::PageSetup>& page_setup, Gtk::Unit units, double& margin_top, double& margin_bottom)
+{
+  //Initialize output parameters:
+  margin_top = 0;
+  margin_bottom = 0;
+
+  const Gtk::PaperSize paper_size = page_setup->get_paper_size();
+  
+  double page_height = 0;
+  if(page_setup->get_orientation() == Gtk::PAGE_ORIENTATION_PORTRAIT) //TODO: Handle the reverse orientations too?
+  {
+    page_height = paper_size.get_height(units);
+    margin_top = page_setup->get_top_margin(units);
+    margin_bottom = page_setup->get_bottom_margin(units);
+  }
+  else
+  {
+    page_height = paper_size.get_width(units);
+    margin_top = page_setup->get_left_margin(units);
+    margin_bottom = page_setup->get_right_margin(units);
+  }
+
+  return page_height;
+}
+
 /* Get the start and end of the page, inside the margins.
  */
 static void get_page_y_start_and_end(const Glib::RefPtr<const Gtk::PageSetup>& page_setup, guint page_number, double& y1, double& y2)
@@ -46,21 +78,20 @@ static void get_page_y_start_and_end(const Glib::RefPtr<const Gtk::PageSetup>& p
   const Gtk::PaperSize paper_size = page_setup->get_paper_size();
   const Gtk::Unit units = get_units();
   
-  double page_height = 0;
-  if(page_setup->get_orientation() == Gtk::PAGE_ORIENTATION_PORTRAIT) //TODO: Handle the reverse orientations too?
-    page_height = paper_size.get_height(units);
-  else
-    page_height = paper_size.get_width(units);
+  double margin_top = 0;
+  double margin_bottom = 0;
+  const double page_height = get_page_height(page_setup, units, 
+    margin_top, margin_bottom);
     
   //y1:
   y1 = page_height * (page_number);  
-  double y_border = page_setup->get_top_margin(units);
+  double y_border = margin_top;
   while(y1 <= y_border)
     y1 += GRID_GAP;
   
   //y2:
   y2 = page_height * (page_number + 1);  
-  y2 -= page_setup->get_bottom_margin(units); //TODO: Handle orientation here and wherever else we use the margin?
+  y2 -= margin_bottom;
 
   //std::cout << G_STRFUNC << "page_number=" << page_number << ", y1=" << y1 << "y2=" << y2 << std::endl;
 }
diff --git a/glom/print_layout/print_layout_utils.h b/glom/print_layout/print_layout_utils.h
index 1ecc4d5..a9efc3b 100644
--- a/glom/print_layout/print_layout_utils.h
+++ b/glom/print_layout/print_layout_utils.h
@@ -44,6 +44,9 @@ sharedptr<PrintLayout> create_standard(const Glib::RefPtr<const Gtk::PageSetup>&
 
 void do_print_layout(const sharedptr<const PrintLayout>& print_layout, const FoundSet& found_set, bool preview, const Document* document, Gtk::Window* transient_for);
 
+double get_page_height(const Glib::RefPtr<const Gtk::PageSetup>& page_setup, Gtk::Unit units);
+double get_page_height(const Glib::RefPtr<const Gtk::PageSetup>& page_setup, Gtk::Unit units, double& margin_top, double& margin_bottom);
+
 } //namespace PrintLayoutUtils
 
 } //namespace Glom



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