src/hg/hgTracks/imageV2.c 1.12

1.12 2009/11/11 20:41:29 tdreszer
Cleanup before checkin of imageV2 framework without features. Also made track toggle work better for composites, but only in imageV2
Index: src/hg/hgTracks/imageV2.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/hgTracks/imageV2.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -b -B -U 4 -r1.11 -r1.12
--- src/hg/hgTracks/imageV2.c	24 Sep 2009 22:15:27 -0000	1.11
+++ src/hg/hgTracks/imageV2.c	11 Nov 2009 20:41:29 -0000	1.12
@@ -12,9 +12,9 @@
 struct imgBox   *theImgBox   = NULL; // Make this global for now to avoid huge rewrite
 //struct image    *theOneImg   = NULL; // Make this global for now to avoid huge rewrite
 struct imgTrack *curImgTrack = NULL; // Make this global for now to avoid huge rewrite
 //struct imgSlice *curSlice    = NULL; // Make this global for now to avoid huge rewrite
-struct mapSet   *curMap      = NULL; // Make this global for now to avoid huge rewrite
+//struct mapSet   *curMap      = NULL; // Make this global for now to avoid huge rewrite
 //struct mapItem  *curMapItem  = NULL; // Make this global for now to avoid huge rewrite
 
 //#define IMAGEv2_UI
 #ifdef IMAGEv2_UI
@@ -55,10 +55,44 @@
     }
 return map;
 }
 
+struct mapItem *mapSetItemFind(struct mapSet *map,int topLeftX,int topLeftY,int bottomRightX,int bottomRightY)
+/* Find a single mapItem based upon coordinates (within a pixel) */
+{
+struct mapItem *item;
+for(item=map->items;item!=NULL;item=item->next)
+    {
+    if((abs(item->topLeftX     - topLeftX)     < 2)
+    && (abs(item->topLeftY     - topLeftY)     < 2)
+    && (abs(item->bottomRightX - bottomRightX) < 2)
+    && (abs(item->bottomRightY - bottomRightY) < 2)) // coordinates within a pixel is okay
+        return item;
+    }
+return NULL;
+}
+
+struct mapItem *mapSetItemUpdate(struct mapSet *map,struct mapItem *item,char *link,char *title,int topLeftX,int topLeftY,int bottomRightX,int bottomRightY)
+/* Update a single mapItem */
+{
+if(title != NULL)
+    item->title = cloneString(title);
+if(link != NULL)
+    {
+    if(map->linkRoot != NULL && startsWith(map->linkRoot,link))
+        item->linkVar = cloneString(link + strlen(map->linkRoot));
+    else
+        item->linkVar = cloneString(link);
+    }
+item->topLeftX     = topLeftX;
+item->topLeftY     = topLeftY;
+item->bottomRightX = bottomRightX;
+item->bottomRightY = bottomRightY;
+return item;
+}
+
 struct mapItem *mapSetItemAdd(struct mapSet *map,char *link,char *title,int topLeftX,int topLeftY,int bottomRightX,int bottomRightY)
-/* Added a single mapItem to a growing mapSet */
+/* Add a single mapItem to a growing mapSet */
 {
 struct mapItem *item;
 AllocVar(item);
 if(title != NULL)
@@ -78,8 +112,28 @@
 //warn("Added map(%s) item '%s' count:%d",map->name,title,slCount(map->items));
 return map->items;
 }
 
+struct mapItem *mapSetItemUpdateOrAdd(struct mapSet *map,char *link,char *title,int topLeftX,int topLeftY,int bottomRightX,int bottomRightY)
+/* Update or add a single mapItem */
+{
+struct mapItem *item = mapSetItemFind(map,topLeftX,topLeftY,bottomRightX,bottomRightY);
+if(item != NULL)
+    return mapSetItemUpdate(map,item,link,title,topLeftX,topLeftY,bottomRightX,bottomRightY);
+else
+    return mapSetItemAdd(map,link,title,topLeftX,topLeftY,bottomRightX,bottomRightY);
+}
+
+struct mapItem *mapSetItemFindOrAdd(struct mapSet *map,char *link,char *title,int topLeftX,int topLeftY,int bottomRightX,int bottomRightY)
+/* Finds or adds the map item */
+{
+struct mapItem *item = mapSetItemFind(map,topLeftX,topLeftY,bottomRightX,bottomRightY);
+if(item != NULL)
+    return item;
+else
+    return mapSetItemAdd(map,link,title,topLeftX,topLeftY,bottomRightX,bottomRightY);
+}
+
 void mapItemFree(struct mapItem **pItem)
 /* frees all memory assocated with a single mapItem */
 {
 if(pItem != NULL && *pItem != NULL)
@@ -515,9 +569,10 @@
 
 int imgTrackAddMapItem(struct imgTrack *imgTrack,char *link,char *title,int topLeftX,int topLeftY,int bottomRightX,int bottomRightY)
 /* Will add a map item it an imgTrack's appropriate slice's map
    Since a map item may span slices, the imgTrack is in the best position to determine where to put the map item
-   returns count of map items added, which could be 0, 1 or more than one if item spans slices */
+   returns count of map items added, which could be 0, 1 or more than one if item spans slices
+   NOTE: Precedence is given to first map item when adding items with same coordinates! */
 {
 struct imgSlice *slice;
 char *imgFile = NULL;               // name of file that hold the image
 
@@ -537,10 +592,10 @@
     && bottomRightY >= slice->offsetY ) // some overlap
         {
         struct mapSet *map = sliceGetMap(slice,FALSE);
         if(map!=NULL)
-            {
-            mapSetItemAdd(map,link,title,max(topLeftX,slice->offsetX),max(topLeftY,slice->offsetY),min(bottomRightX,slice->offsetX + slice->width),min(bottomRightY,slice->offsetY + slice->height));
+        {          // NOTE: using find or add gives precedence to first of same coordinate map items added
+            mapSetItemFindOrAdd(map,link,title,max(topLeftX,slice->offsetX),max(topLeftY,slice->offsetY),min(bottomRightX,slice->offsetX + slice->width),min(bottomRightY,slice->offsetY + slice->height));
             count++;
             }
         }
     }