[glabels/vala] Initial merge support in print model.



commit 79be75ba6b11dd34a5beae236266abfcfaadce23
Author: Jim Evins <evins snaught com>
Date:   Sun Oct 21 19:44:58 2012 -0400

    Initial merge support in print model.

 glabels/label.vala                 |    5 +-
 glabels/merge.vala                 |   55 ++++++++++++++++++++++-
 glabels/merge_property_editor.vala |   79 ++++++++++++++++++---------------
 glabels/mini_preview.vala          |    3 +-
 glabels/model_print.vala           |   85 +++++++++++++++++++++++++++++++++++-
 glabels/object_editor.vala         |    8 +++
 glabels/print_op.vala              |   11 +++--
 libglabels/template_coord.vala     |    6 +-
 libglabels/template_frame.vala     |    2 +-
 9 files changed, 201 insertions(+), 53 deletions(-)
---
diff --git a/glabels/label.vala b/glabels/label.vala
index f6206d3..f089934 100644
--- a/glabels/label.vala
+++ b/glabels/label.vala
@@ -167,7 +167,7 @@ namespace glabels
 				if ( _merge != value )
 				{
 					_merge = value;
-					_merge.changed.connect( on_merge_changed );
+					_merge.source_changed.connect( on_merge_source_changed );
 					changed();
 					merge_changed();
 					modified = true;
@@ -319,10 +319,9 @@ namespace glabels
 		}
 
 
-		private void on_merge_changed()
+		private void on_merge_source_changed()
 		{
 			changed();
-			merge_changed();
 		}
 
 
diff --git a/glabels/merge.vala b/glabels/merge.vala
index b80f962..9ea58ae 100644
--- a/glabels/merge.vala
+++ b/glabels/merge.vala
@@ -27,7 +27,8 @@ namespace glabels
 	public abstract class Merge : Object
 	{
 
-		public signal void changed();
+		public signal void source_changed();
+		public signal void selection_changed();
 
 
 		public MergeInfo info { get; construct; }
@@ -66,7 +67,7 @@ namespace glabels
 					}
 					this.close();
 
-					changed();
+					source_changed();
 				}
 
 			}
@@ -75,6 +76,56 @@ namespace glabels
 		private string? _src;
 
 
+		public void select( MergeRecord record )
+		{
+			record.selected = true;
+			selection_changed();
+		}
+
+
+		public void unselect( MergeRecord record )
+		{
+			record.selected = false;
+			selection_changed();
+		}
+
+
+		public void select_all()
+		{
+			foreach ( MergeRecord record in _record_list )
+			{
+				record.selected = true;
+			}
+			selection_changed();
+		}
+
+
+		public void unselect_all()
+		{
+			foreach ( MergeRecord record in _record_list )
+			{
+				record.selected = false;
+			}
+			selection_changed();
+		}
+
+
+		public Gee.ArrayList<MergeRecord> get_selected_records()
+		{
+			Gee.ArrayList<MergeRecord> list = new Gee.ArrayList<MergeRecord>();
+
+			foreach ( MergeRecord record in _record_list )
+			{
+				if ( record.selected )
+				{
+					list.add( record );
+				}
+			}
+
+			return list;
+		}
+
+
 		public    abstract List<string>  get_key_list();
 		public    abstract string        get_primary_key();
 		protected abstract void          open();
diff --git a/glabels/merge_property_editor.vala b/glabels/merge_property_editor.vala
index 2503354..e674235 100644
--- a/glabels/merge_property_editor.vala
+++ b/glabels/merge_property_editor.vala
@@ -148,6 +148,22 @@ namespace glabels
 
 			load_new_column_set( model.label.merge );
 			load_tree( model.label.merge );
+
+			model.label.merge.source_changed.connect( on_merge_source_changed );
+			model.label.merge.selection_changed.connect( on_merge_selection_changed );
+		}
+
+
+		private void on_merge_source_changed()
+		{
+			load_new_column_set( model.label.merge );
+			load_tree( model.label.merge );
+		}
+
+
+		private void on_merge_selection_changed()
+		{
+			load_selected_column();
 		}
 
 
@@ -248,6 +264,23 @@ namespace glabels
 		}
 
 
+		private void load_selected_column()
+		{
+			Gtk.TreeIter iter;
+
+			for ( bool good = list_store.get_iter_first( out iter );
+			      good;
+			      good = list_store.iter_next( ref iter ) )
+			{
+				/* get record */
+				unowned MergeRecord record;
+				list_store.get( iter, DATA_COLUMN, out record );
+
+				/* set selected value in store accordingly */
+				list_store.set( iter, SELECT_COLUMN, record.selected );
+			}
+		}
+
 
 		private void on_type_combo_changed()
 		{
@@ -275,52 +308,26 @@ namespace glabels
 			list_store.get( iter, DATA_COLUMN, out record );
 
 			/* toggle the select flag within the record */
-			record.selected = !record.selected;
-
-			/* set new value in store */
-			list_store.set( iter, SELECT_COLUMN, record.selected );
+			if ( record.selected )
+			{
+				model.label.merge.unselect( record );
+			}
+			else
+			{
+				model.label.merge.select( record );
+			}
 		}
 
 
 		private void on_select_all_button_clicked()
 		{
-			Gtk.TreeIter iter;
-
-			for ( bool good = list_store.get_iter_first( out iter );
-			      good;
-			      good = list_store.iter_next( ref iter ) )
-			{
-				/* get current data */
-				unowned MergeRecord record;
-				list_store.get( iter, DATA_COLUMN, out record );
-
-				/* set select flag within the record */
-				record.selected = true;
-
-				/* set new value in store */
-				list_store.set( iter, SELECT_COLUMN, record.selected );
-			}
+			model.label.merge.select_all();
 		}
 
 
 		private void on_unselect_all_button_clicked()
 		{
-			Gtk.TreeIter iter;
-
-			for ( bool good = list_store.get_iter_first( out iter );
-			      good;
-			      good = list_store.iter_next( ref iter ) )
-			{
-				/* get current data */
-				unowned MergeRecord record;
-				list_store.get( iter, DATA_COLUMN, out record );
-
-				/* clear select flag within the record */
-				record.selected = false;
-
-				/* set new value in store */
-				list_store.set( iter, SELECT_COLUMN, record.selected );
-			}
+			model.label.merge.unselect_all();
 		}
 
 
diff --git a/glabels/mini_preview.vala b/glabels/mini_preview.vala
index d5d51dd..9ed6cf0 100644
--- a/glabels/mini_preview.vala
+++ b/glabels/mini_preview.vala
@@ -408,8 +408,7 @@ namespace glabels
 
 		private void draw_rich_preview( Cairo.Context cr )
 		{
-			/* TODO: test for merge. */
-			model.print.print_simple_sheet( cr, 0 );
+			model.print.print_sheet( cr, 0 );
 		}
 
 		
diff --git a/glabels/model_print.vala b/glabels/model_print.vala
index 917e9d3..93e3691 100644
--- a/glabels/model_print.vala
+++ b/glabels/model_print.vala
@@ -27,6 +27,7 @@ namespace glabels
 
 	public class ModelPrint
 	{
+
 		private Label label;
 
 		private const double OUTLINE_WIDTH =  0.25;
@@ -37,16 +38,70 @@ namespace glabels
 		public bool  reverse_flag    { get; set; }
 		public bool  crop_marks_flag { get; set; }
 
-		public int   n_pages         { get; set; default = 1; }
+		public int   n_pages         { get; set; }
+
+
+		private Gee.ArrayList<MergeRecord> record_list;
 
 
 		public ModelPrint( Label label )
 		{
 			this.label = label;
+
+			on_merge_changed();
+
+			label.merge_changed.connect( on_merge_changed );
+		}
+
+
+		private void on_merge_changed()
+		{
+			on_merge_selection_changed();
+
+			label.merge.source_changed.connect( on_merge_selection_changed );
+			label.merge.selection_changed.connect( on_merge_selection_changed );
+		}
+
+
+		private void on_merge_selection_changed()
+		{
+			record_list = label.merge.get_selected_records();
+
+			if ( label.merge is MergeNone )
+			{
+				n_pages = 1;
+			}
+			else
+			{
+				TemplateFrame frame = label.template.frames.first().data;
+				n_pages = record_list.size / frame.get_n_labels();
+				if ( (record_list.size % frame.get_n_labels()) != 0 )
+				{
+					n_pages++;
+				}
+
+				if ( n_pages == 0 )
+				{
+					n_pages = 1;
+				}
+			}
+		}
+
+
+		public void print_sheet( Cairo.Context cr, int i_page )
+		{
+			if ( label.merge is MergeNone )
+			{
+				print_simple_sheet( cr, i_page );
+			}
+			else
+			{
+				print_merge_sheet( cr, i_page );
+			}
 		}
 
 
-		public void print_simple_sheet( Cairo.Context cr, int i_page )
+		private void print_simple_sheet( Cairo.Context cr, int i_page )
 		{
 			if ( crop_marks_flag )
 			{
@@ -63,6 +118,32 @@ namespace glabels
 		}
 
 
+		private void print_merge_sheet( Cairo.Context cr, int i_page )
+		{
+			if ( crop_marks_flag )
+			{
+				print_crop_marks( cr );
+			}
+
+			TemplateFrame frame = label.template.frames.first().data;
+			Gee.ArrayList<TemplateCoord?> origins = frame.get_origins();
+
+			int i = i_page * frame.get_n_labels();
+
+			foreach ( TemplateCoord origin in origins )
+			{
+				if ( i >= record_list.size )
+				{
+					break;
+				}
+
+				print_label( cr, origin.x, origin.y, record_list.get(i) );
+
+				i++;
+			}
+		}
+
+
 		private void print_crop_marks( Cairo.Context cr )
 		{
 			TemplateFrame frame = label.template.frames.first().data;
diff --git a/glabels/object_editor.vala b/glabels/object_editor.vala
index 3928db4..d4ac9cf 100644
--- a/glabels/object_editor.vala
+++ b/glabels/object_editor.vala
@@ -669,6 +669,14 @@ namespace glabels
 
 		private void on_merge_changed()
 		{
+			on_merge_source_changed();
+
+			model.label.merge.source_changed.connect( on_merge_source_changed );
+		}
+
+
+		private void on_merge_source_changed()
+		{
 			if ( model.label.merge is MergeNone )
 			{
 				text_color_button.clear_keys();
diff --git a/glabels/print_op.vala b/glabels/print_op.vala
index fef06a0..9197738 100644
--- a/glabels/print_op.vala
+++ b/glabels/print_op.vala
@@ -38,6 +38,7 @@ namespace glabels
 
 			begin_print.connect( on_begin_print );
 			draw_page.connect( on_draw_page );
+			end_print.connect( on_end_print );
 		}
 
 
@@ -78,10 +79,12 @@ namespace glabels
 		{
 			Cairo.Context cr = context.get_cairo_context();
 
-			if ( model.label.merge is MergeNone )
-			{
-				model.print.print_simple_sheet( cr, i_page );
-			}
+			model.print.print_sheet( cr, i_page );
+		}
+
+
+		private void on_end_print( Gtk.PrintContext context )
+		{
 		}
 
 
diff --git a/libglabels/template_coord.vala b/libglabels/template_coord.vala
index 902167c..a3ed5ab 100644
--- a/libglabels/template_coord.vala
+++ b/libglabels/template_coord.vala
@@ -36,7 +36,8 @@ namespace libglabels
 			this.y = y;
 		}
 
-		public int compare( TemplateCoord b )
+
+		public int compare_to( TemplateCoord b )
 		{
 			if ( this.y < b.y )
 			{
@@ -58,12 +59,11 @@ namespace libglabels
 				}
 				else
 				{
-					return 0; /* hopefull 2 label frames won't have the same origin. */
+					return 0; /* hopefully 2 label frames won't have the same origin. */
 				}
 			}
 		}
 
-
 	}
 
 }
diff --git a/libglabels/template_frame.vala b/libglabels/template_frame.vala
index c87274b..afd711b 100644
--- a/libglabels/template_frame.vala
+++ b/libglabels/template_frame.vala
@@ -99,7 +99,7 @@ namespace libglabels
 				}
 			}
 
-			origins.sort();
+			origins.sort( (CompareFunc<TemplateCoord>)TemplateCoord.compare_to );
 
 			return origins;
 		}



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