From df388cf7f25b90536971122f427ed16da6c64952 Mon Sep 17 00:00:00 2001
From: edgrif <edgrif>
Date: Fri, 16 Jun 2006 17:03:41 +0000
Subject: [PATCH] Add code to cache feature styles in the feature set plus code
 to copy styles.

---
 src/include/ZMap/zmapFeature.h     | 15 ++++--
 src/zmapFeature/zmapFeature.c      | 70 +++++++++++++++++++++++--
 src/zmapFeature/zmapFeatureTypes.c | 82 ++++++++++++++++++++++--------
 3 files changed, 138 insertions(+), 29 deletions(-)

diff --git a/src/include/ZMap/zmapFeature.h b/src/include/ZMap/zmapFeature.h
index a67c51cf6..ce62dbaec 100755
--- a/src/include/ZMap/zmapFeature.h
+++ b/src/include/ZMap/zmapFeature.h
@@ -25,9 +25,9 @@
  * Description: Data structures describing a sequence feature.
  *              
  * HISTORY:
- * Last edited: Jun 14 14:01 2006 (edgrif)
+ * Last edited: Jun 16 16:45 2006 (edgrif)
  * Created: Fri Jun 11 08:37:19 2004 (edgrif)
- * CVS info:   $Id: zmapFeature.h,v 1.72 2006-06-14 14:49:25 edgrif Exp $
+ * CVS info:   $Id: zmapFeature.h,v 1.73 2006-06-16 17:03:41 edgrif Exp $
  *-------------------------------------------------------------------
  */
 #ifndef ZMAP_FEATURE_H
@@ -300,6 +300,9 @@ typedef struct ZMapFeatureSetStruct_
 							       drawn, this applies only to the set
 							       * itself, _not_ the features within
 							       * the set. */
+  GHashTable *feature_styles ;				    /* Cache of styles for features within
+							     * the feature set. */
+
   GData *features ;					    /* A set of ZMapFeatureStruct. */
 } ZMapFeatureSetStruct, *ZMapFeatureSet ;
 
@@ -618,6 +621,9 @@ void zmapFeatureDestroy(ZMapFeature feature) ;
 ZMapFeatureSet zMapFeatureSetCreate(char *source, GData *features) ;
 ZMapFeatureSet zMapFeatureSetIDCreate(GQuark original_id, GQuark unique_id,
 				      ZMapFeatureTypeStyle style, GData *features) ;
+void zMapFeatureSetStyle(ZMapFeatureSet feature_set, ZMapFeatureTypeStyle style) ;
+gboolean zMapFeatureSetAddStyle(ZMapFeatureSet feature_set, ZMapFeatureTypeStyle new_style) ;
+ZMapFeatureTypeStyle zMapFeatureSetFindStyle(ZMapFeatureSet feature_set, GQuark style_id) ;
 gboolean zMapFeatureSetAddFeature(ZMapFeatureSet feature_set, ZMapFeature feature) ;
 gboolean zMapFeatureSetRemoveFeature(ZMapFeatureSet feature_set, ZMapFeature feature) ;
 char *zMapFeatureSetGetName(ZMapFeatureSet feature_set) ;
@@ -631,12 +637,13 @@ gboolean zMapSetListEqualStyles(GList **feature_set_names, GList **styles) ;
 
 
 
-/* Style functions. */
+/* Style functions, name should all be rationalised to just use "style", not "type". */
 
 GList *zMapStylesGetNames(GList *styles) ;
 ZMapFeatureTypeStyle zMapFeatureTypeCreate(char *name, char *description,
 					   char *outline, char *foreground, char *background,
 					   double width) ;
+ZMapFeatureTypeStyle zMapFeatureStyleCopy(ZMapFeatureTypeStyle style) ;
 void zMapStyleSetColours(ZMapFeatureTypeStyle style, 
                          char *outline, 
                          char *foreground, 
@@ -657,7 +664,7 @@ GQuark zMapStyleCreateID(char *style_name) ;
 char *zMapStyleGetName(ZMapFeatureTypeStyle style) ;
 ZMapFeatureTypeStyle zMapFindStyle(GList *styles, GQuark style_id) ;
 gboolean zMapStyleNameExists(GList *style_name_list, char *style_name) ;
-ZMapFeatureTypeStyle zMapFeatureTypeCopy(ZMapFeatureTypeStyle type) ;
+ZMapFeatureTypeStyle zMapFeatureStyleCopy(ZMapFeatureTypeStyle type) ;
 void zMapFeatureTypeDestroy(ZMapFeatureTypeStyle type) ;
 GList *zMapFeatureTypeGetFromFile(char *types_file) ;
 gboolean zMapFeatureTypeSetAugment(GData **current, GData **new) ;
diff --git a/src/zmapFeature/zmapFeature.c b/src/zmapFeature/zmapFeature.c
index bb6248f55..d931b8193 100755
--- a/src/zmapFeature/zmapFeature.c
+++ b/src/zmapFeature/zmapFeature.c
@@ -27,9 +27,9 @@
  *              
  * Exported functions: See zmapView_P.h
  * HISTORY:
- * Last edited: Jun 14 14:08 2006 (edgrif)
+ * Last edited: Jun 16 17:00 2006 (edgrif)
  * Created: Fri Jul 16 13:05:58 2004 (edgrif)
- * CVS info:   $Id: zmapFeature.c,v 1.35 2006-06-14 14:49:24 edgrif Exp $
+ * CVS info:   $Id: zmapFeature.c,v 1.36 2006-06-16 17:03:41 edgrif Exp $
  *-------------------------------------------------------------------
  */
 
@@ -93,6 +93,8 @@ static void removeNotFreeFeature(GQuark key_id, gpointer data, gpointer user_dat
 static void destroyFeatureSet(gpointer data) ;
 static void removeNotFreeFeatureSet(GQuark key_id, gpointer data, gpointer user_data) ;
 
+static void styleDestroyCB(gpointer data) ;
+
 /* !
  * A set of functions for allocating, populating and destroying features.
  * The feature create and add data are in two steps because currently the required
@@ -512,6 +514,19 @@ ZMapFeatureSet zMapFeatureSetCreate(char *source, GData *features)
 }
 
 
+
+void zMapFeatureSetStyle(ZMapFeatureSet feature_set, ZMapFeatureTypeStyle style)
+{
+  zMapAssert(feature_set && style) ;
+
+  feature_set->style = style ;
+
+  return ;
+}
+
+
+
+
 /* Features can be NULL if there are no features yet.....
  * original_id  the original name of the feature set
  * unique_id    some derivation of the original name or otherwise unique id to identify this
@@ -526,17 +541,55 @@ ZMapFeatureSet zMapFeatureSetIDCreate(GQuark original_id, GQuark unique_id,
   feature_set->unique_id = unique_id ;
   feature_set->original_id = original_id ;
   feature_set->style = style ;
+  feature_set->feature_styles = g_hash_table_new_full(NULL, NULL, NULL, styleDestroyCB) ;
 
   if (!features)
     g_datalist_init(&(feature_set->features)) ;
   else
     feature_set->features = features ;
 
-
   return feature_set ;
 }
 
 
+/* Adds a style to the list of feature styles using the styles unique_id as the key.
+ * NOTE that if the style is already there it is not added as this would overwrite
+ * the existing style. */
+gboolean zMapFeatureSetAddStyle(ZMapFeatureSet feature_set, ZMapFeatureTypeStyle new_style)
+{
+  gboolean result = FALSE ;
+  ZMapFeatureTypeStyle style ;
+
+  zMapAssert(feature_set && new_style) ;
+
+  if (!(style = g_hash_table_lookup(feature_set->feature_styles,
+				    GINT_TO_POINTER(new_style->unique_id))))
+    {
+      g_hash_table_insert(feature_set->feature_styles,
+			  GINT_TO_POINTER(new_style->unique_id), new_style) ;
+
+      result = TRUE ;
+    }
+
+  return result ;
+}
+
+
+/* Finds a feature style using the styles unique_id. */
+ZMapFeatureTypeStyle zMapFeatureSetFindStyle(ZMapFeatureSet feature_set, GQuark style_id)
+{
+  ZMapFeatureTypeStyle style ;
+
+  zMapAssert(feature_set && style_id) ;
+
+  style = g_hash_table_lookup(feature_set->feature_styles,
+			      GINT_TO_POINTER(style_id)) ;
+
+  return style ;
+}
+
+
+
 /* Feature must not be null to be added we need at least the feature id and probably should.
  * check for more.
  * 
@@ -609,6 +662,8 @@ void zMapFeatureSetDestroy(ZMapFeatureSet feature_set, gboolean free_data)
     }
   g_datalist_clear(&(feature_set->features)) ;
 
+  g_hash_table_destroy(feature_set->feature_styles) ;
+
   g_free(feature_set) ;
 
   return ;
@@ -1333,10 +1388,15 @@ static void doNewFeatures(GQuark key_id, gpointer data, gpointer user_data)
 }
 
 
+/* Called when a style is destroyed and the styles hash of feature_styles is destroyed. */
+static void styleDestroyCB(gpointer data)
+{
+  ZMapFeatureTypeStyle style = (ZMapFeatureTypeStyle)data ;
 
+  zMapFeatureTypeDestroy(style) ;
 
-
-
+  return ;
+}
 
 
 
diff --git a/src/zmapFeature/zmapFeatureTypes.c b/src/zmapFeature/zmapFeatureTypes.c
index dd3f3731f..1f5ba9f2e 100755
--- a/src/zmapFeature/zmapFeatureTypes.c
+++ b/src/zmapFeature/zmapFeatureTypes.c
@@ -27,9 +27,9 @@
  *              
  * Exported functions: See ZMap/zmapFeature.h
  * HISTORY:
- * Last edited: Jun 14 16:47 2006 (edgrif)
+ * Last edited: Jun 16 16:45 2006 (edgrif)
  * Created: Tue Dec 14 13:15:11 2004 (edgrif)
- * CVS info:   $Id: zmapFeatureTypes.c,v 1.23 2006-06-14 15:47:28 edgrif Exp $
+ * CVS info:   $Id: zmapFeatureTypes.c,v 1.24 2006-06-16 17:03:41 edgrif Exp $
  *-------------------------------------------------------------------
  */
 
@@ -58,6 +58,24 @@ static void checkListName(gpointer data, gpointer user_data) ;
 static gint compareNameToStyle(gconstpointer glist_data, gconstpointer user_data) ;
 
 
+
+/*! @defgroup zmapstyles   zMapStyle: Feature Style handling for ZMap
+ * @{
+ * 
+ * \brief  Feature Style handling for ZMap.
+ * 
+ * zMapStyle routines provide functions to create/modify/destroy individual
+ * styles, the styles control how features are processed and displayed. They
+ * control aspects such as foreground colour, column bumping mode etc.
+ *
+ *  */
+
+
+
+
+
+
+
 /* Create a new type for displaying features. */
 ZMapFeatureTypeStyle zMapFeatureTypeCreate(char *name, char *description,
 					   char *outline, char *foreground, char *background,
@@ -97,6 +115,42 @@ ZMapFeatureTypeStyle zMapFeatureTypeCreate(char *name, char *description,
   return new_type ;
 }
 
+
+/*!
+ * Copy an existing style. The copy will copy all dynamically allocated memory within
+ * the struct as well.
+ * 
+ * Returns the new style or NULL if there is an error.
+ * 
+ * @param   style          The style to be copied.
+ * @return  ZMapFeatureTypeStyle   the style copy or NULL
+ *  */
+ZMapFeatureTypeStyle zMapFeatureStyleCopy(ZMapFeatureTypeStyle style)
+{
+  ZMapFeatureTypeStyle new_style = NULL ;
+
+  zMapAssert(style) ;
+
+  new_style = g_new0(ZMapFeatureTypeStyleStruct, 1) ;
+
+
+#ifdef ED_G_NEVER_INCLUDE_THIS_CODE
+  new_style = g_memdup((gpointer)style, sizeof(ZMapFeatureTypeStyleStruct)) ;
+#endif /* ED_G_NEVER_INCLUDE_THIS_CODE */
+
+  *new_style = *style ;					    /* better ?? */
+  
+  if (new_style->description)
+    new_style->description = g_strdup(new_style->description) ;
+
+
+
+  return new_style ;
+}
+
+
+
+
 void zMapStyleSetColours(ZMapFeatureTypeStyle style, 
                          char *outline, 
                          char *foreground, 
@@ -371,23 +425,6 @@ GQuark zMapStyleCreateID(char *style)
 }
 
 
-/* Copy an existing type. */
-ZMapFeatureTypeStyle zMapFeatureTypeCopy(ZMapFeatureTypeStyle type)
-{
-  ZMapFeatureTypeStyle new_type = NULL ;
-
-  zMapAssert(type) ;
-
-  new_type = g_new0(ZMapFeatureTypeStyleStruct, 1) ;
-  *new_type = *type ;					    /* n.b. struct copy. */
-
-  if (type->description)
-    new_type->description = g_strdup(type->description) ;
-
-  return new_type ;
-}
-
-
 char *zMapStyleGetName(ZMapFeatureTypeStyle style)
 {
   char *style_name ;
@@ -625,6 +662,11 @@ void zMapFeatureTypePrintAll(GData *type_set, char *user_string)
 
 
 
+/*! @} end of zmapstyles docs. */
+
+
+
+
 
 
 /* 
@@ -644,7 +686,7 @@ static void doTypeSets(GQuark key_id, gpointer data, gpointer user_data)
       /* copy the struct and then add it to the current set. */
       ZMapFeatureTypeStyle type ;
 
-      type = zMapFeatureTypeCopy(new_type) ;
+      type = zMapFeatureStyleCopy(new_type) ;
 
       g_datalist_id_set_data(&current, key_id, type) ;
     }
-- 
GitLab