From a1f668bffbea12453ecc330ec243f9f656582c57 Mon Sep 17 00:00:00 2001 From: randomguy3 Date: Wed, 14 Mar 2012 16:30:17 +0000 Subject: Use KVO for listening to style changes in the selectors git-svn-id: https://tikzit.svn.sourceforge.net/svnroot/tikzit/trunk@426 7c02a99a-9b00-45e3-bf44-6f3dd7fddb64 --- tikzit/src/common/ColorRGB.h | 4 +- tikzit/src/common/ColorRGB.m | 24 ++++++----- tikzit/src/common/EdgeStyle.m | 11 +++++ tikzit/src/common/NodeStyle.m | 23 +++++++++++ tikzit/src/gtk/EdgeStyleSelector.m | 64 ++++++++++++++++++++++++----- tikzit/src/gtk/NodeStyleSelector.m | 84 +++++++++++++++++++++++++++++++++----- 6 files changed, 178 insertions(+), 32 deletions(-) (limited to 'tikzit/src') diff --git a/tikzit/src/common/ColorRGB.h b/tikzit/src/common/ColorRGB.h index 412a1f6..607ba64 100644 --- a/tikzit/src/common/ColorRGB.h +++ b/tikzit/src/common/ColorRGB.h @@ -36,6 +36,8 @@ @property (assign) float greenFloat; @property (assign) float blueFloat; +@property (readonly) NSString *name; + - (RColor)rColor; - (RColor)rColorWithAlpha:(CGFloat)alpha; @@ -48,8 +50,6 @@ - (id)initWithFloatRed:(float)r green:(float)g blue:(float)b; - (id)initWithRColor:(RColor)color; -- (NSString*)name; - - (void)setToClosestHashed; + (ColorRGB*)colorWithRed:(unsigned short)r green:(unsigned short)g blue:(unsigned short)b; diff --git a/tikzit/src/common/ColorRGB.m b/tikzit/src/common/ColorRGB.m index 673a88d..74d1c55 100644 --- a/tikzit/src/common/ColorRGB.m +++ b/tikzit/src/common/ColorRGB.m @@ -183,19 +183,22 @@ static NSMapTable *colorHash = nil; @implementation ColorRGB -- (unsigned short)red { return red; } -- (void)setRed:(unsigned short)r { red = r; } -- (unsigned short)green { return green; } -- (void)setGreen:(unsigned short)g { green = g; } -- (unsigned short)blue { return blue; } -- (void)setBlue:(unsigned short)b { blue = b; } ++ (void)initialize { + [self setKeys:[NSArray arrayWithObject:@"red"] triggerChangeNotificationsForDependentKey:@"redFloat"]; + [self setKeys:[NSArray arrayWithObject:@"green"] triggerChangeNotificationsForDependentKey:@"greenFloat"]; + [self setKeys:[NSArray arrayWithObject:@"blue"] triggerChangeNotificationsForDependentKey:@"blueFloat"]; + [self setKeys:[NSArray arrayWithObjects:@"red", @"green", @"blue", nil] + triggerChangeNotificationsForDependentKey:@"name"]; +} + +@synthesize red, green, blue; - (float)redFloat { return ((float)red)/255.0f; } -- (void)setRedFloat:(float)r { red = round(r*255.0f); } +- (void)setRedFloat:(float)r { [self setRed:round(r*255.0f)]; } - (float)greenFloat { return ((float)green)/255.0f; } -- (void)setGreenFloat:(float)g { green = round(g*255.0f); } +- (void)setGreenFloat:(float)g { [self setGreen:round(g*255.0f)]; } - (float)blueFloat { return ((float)blue)/255.0f; } -- (void)setBlueFloat:(float)b { blue = round(b*255.0f); } +- (void)setBlueFloat:(float)b { [self setBlue:round(b*255.0f)]; } - (int)hash { return (red<<4) + (green<<2) + blue; @@ -230,7 +233,8 @@ static NSMapTable *colorHash = nil; } - (NSString*)name { - if (colorHash == nil) [ColorRGB makeColorHash]; + if (colorHash == nil) + [ColorRGB makeColorHash]; return [colorHash objectForKey:self]; } diff --git a/tikzit/src/common/EdgeStyle.m b/tikzit/src/common/EdgeStyle.m index a9dbc9f..da669e3 100644 --- a/tikzit/src/common/EdgeStyle.m +++ b/tikzit/src/common/EdgeStyle.m @@ -25,6 +25,17 @@ @implementation EdgeStyle ++ (void)initialize { + [self setKeys:[NSArray arrayWithObjects: + @"tailStyle", + @"headStyle", + @"decorationStyle", + @"thickness", + @"name", + nil] + triggerChangeNotificationsForDependentKey:@"tikz"]; +} + - (id)initWithName:(NSString*)nm { self = [super initWithNotificationName:@"EdgeStylePropertyChanged"]; diff --git a/tikzit/src/common/NodeStyle.m b/tikzit/src/common/NodeStyle.m index 8d8b83f..0794b6a 100644 --- a/tikzit/src/common/NodeStyle.m +++ b/tikzit/src/common/NodeStyle.m @@ -26,6 +26,29 @@ @implementation NodeStyle ++ (void)initialize { + [self setKeys:[NSArray arrayWithObjects: + @"fillColorRGB.red", + @"fillColorRGB.blue", + @"fillColorRGB.green", + @"strokeColorRGB.red", + @"strokeColorRGB.blue", + @"strokeColorRGB.green", + @"strokeThickness", + @"shapeName", + @"name", + nil] + triggerChangeNotificationsForDependentKey:@"tikz"]; + [self setKeys:[NSArray arrayWithObjects: + @"fillColorRGB.name", + nil] + triggerChangeNotificationsForDependentKey:@"fillColorIsKnown"]; + [self setKeys:[NSArray arrayWithObjects: + @"strokeColorRGB.name", + nil] + triggerChangeNotificationsForDependentKey:@"strokeColorIsKnown"]; +} + + (int) defaultStrokeThickness { return 1; } - (id)initWithName:(NSString*)nm { diff --git a/tikzit/src/gtk/EdgeStyleSelector.m b/tikzit/src/gtk/EdgeStyleSelector.m index 4e7736f..b6139b8 100644 --- a/tikzit/src/gtk/EdgeStyleSelector.m +++ b/tikzit/src/gtk/EdgeStyleSelector.m @@ -45,9 +45,12 @@ enum { - (void) styleAdded:(NSNotification*)notification; - (void) styleRemoved:(NSNotification*)notification; - (void) activeStyleChanged:(NSNotification*)notification; -- (void) stylePropertyChanged:(NSNotification*)notification; - (void) shapeDictionaryReplaced:(NSNotification*)n; - (void) selectionChanged; +- (void) observeValueForKeyPath:(NSString*)keyPath + ofObject:(id)object + change:(NSDictionary*)change + context:(void*)context; @end @interface EdgeStyleSelector (Private) @@ -58,6 +61,8 @@ enum { - (GdkPixbuf*) pixbufOfEdgeInStyle:(EdgeStyle*)style usingSurface:(cairo_surface_t*)surface; - (void) addStyle:(EdgeStyle*)style; - (void) postSelectedStyleChanged; +- (void) observeStyle:(EdgeStyle*)style; +- (void) stopObservingStyle:(EdgeStyle*)style; - (void) reloadStyles; @end @@ -108,10 +113,6 @@ enum { [self setStyleManager:m]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(stylePropertyChanged:) - name:@"EdgeStylePropertyChanged" - object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shapeDictionaryReplaced:) name:@"ShapeDictionaryReplaced" @@ -249,6 +250,7 @@ enum { gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1); if (style == rowStyle) { gtk_list_store_remove (store, &row); + [self stopObservingStyle:rowStyle]; [rowStyle release]; return; } @@ -265,8 +267,15 @@ enum { } } -- (void) stylePropertyChanged:(NSNotification*)notification { - EdgeStyle *style = [notification object]; +- (void) observeValueForKeyPath:(NSString*)keyPath + ofObject:(id)object + change:(NSDictionary*)change + context:(void*)context +{ + if ([object class] != [EdgeStyle class]) + return; + + EdgeStyle *style = object; GtkTreeModel *model = GTK_TREE_MODEL (store); GtkTreeIter row; @@ -275,9 +284,9 @@ enum { EdgeStyle *rowStyle; gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1); if (style == rowStyle) { - if ([@"name" isEqual:[[notification userInfo] objectForKey:@"propertyName"]]) { + if ([@"name" isEqual:keyPath]) { gtk_list_store_set (store, &row, STYLES_NAME_COL, [[style name] UTF8String], -1); - } else if (![@"scale" isEqual:[[notification userInfo] objectForKey:@"propertyName"]]) { + } else { GdkPixbuf *pixbuf = [self pixbufOfEdgeInStyle:style]; gtk_list_store_set (store, &row, STYLES_ICON_COL, pixbuf, -1); gdk_pixbuf_unref (pixbuf); @@ -314,6 +323,7 @@ enum { do { EdgeStyle *rowStyle; gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1); + [self stopObservingStyle:rowStyle]; [rowStyle release]; } while (gtk_tree_model_iter_next (model, &row)); } @@ -402,6 +412,7 @@ enum { STYLES_PTR_COL, (gpointer)[style retain], -1); gdk_pixbuf_unref (pixbuf); + [self observeStyle:style]; } - (void) addStyle:(EdgeStyle*)style { @@ -414,11 +425,44 @@ enum { [[NSNotificationCenter defaultCenter] postNotificationName:@"SelectedStyleChanged" object:self]; } +- (void) observeStyle:(EdgeStyle*)style { + [style addObserver:self + forKeyPath:@"name" + options:NSKeyValueObservingOptionNew + context:NULL]; + [style addObserver:self + forKeyPath:@"thickness" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"headStyle" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"tailStyle" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"decorationStyle" + options:0 + context:NULL]; +} + +- (void) stopObservingStyle:(EdgeStyle*)style { + [style removeObserver:self forKeyPath:@"name"]; + [style removeObserver:self forKeyPath:@"thickness"]; + [style removeObserver:self forKeyPath:@"headStyle"]; + [style removeObserver:self forKeyPath:@"tailStyle"]; + [style removeObserver:self forKeyPath:@"decorationStyle"]; +} + - (void) reloadStyles { [self clearModel]; + cairo_surface_t *surface = [self createEdgeIconSurface]; for (EdgeStyle *style in [styleManager edgeStyles]) { - [self addStyle:style]; + [self addStyle:style usingSurface:surface]; } + cairo_surface_destroy (surface); } @end diff --git a/tikzit/src/gtk/NodeStyleSelector.m b/tikzit/src/gtk/NodeStyleSelector.m index 7e9757a..af02338 100644 --- a/tikzit/src/gtk/NodeStyleSelector.m +++ b/tikzit/src/gtk/NodeStyleSelector.m @@ -42,9 +42,12 @@ enum { - (void) styleAdded:(NSNotification*)notification; - (void) styleRemoved:(NSNotification*)notification; - (void) activeStyleChanged:(NSNotification*)notification; -- (void) stylePropertyChanged:(NSNotification*)notification; - (void) shapeDictionaryReplaced:(NSNotification*)n; - (void) selectionChanged; +- (void) observeValueForKeyPath:(NSString*)keyPath + ofObject:(id)object + change:(NSDictionary*)change + context:(void*)context; @end @interface NodeStyleSelector (Private) @@ -54,6 +57,8 @@ enum { - (GdkPixbuf*) pixbufOfNodeInStyle:(NodeStyle*)style usingSurface:(cairo_surface_t*)surface; - (void) addStyle:(NodeStyle*)style; - (void) postSelectedStyleChanged; +- (void) observeStyle:(NodeStyle*)style; +- (void) stopObservingStyle:(NodeStyle*)style; - (void) clearModel; - (void) reloadStyles; @end @@ -96,10 +101,6 @@ enum { [self setStyleManager:m]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(stylePropertyChanged:) - name:@"NodeStylePropertyChanged" - object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shapeDictionaryReplaced:) name:@"ShapeDictionaryReplaced" @@ -242,6 +243,7 @@ enum { gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1); if (style == rowStyle) { gtk_list_store_remove (store, &row); + [self stopObservingStyle:rowStyle]; [rowStyle release]; return; } @@ -258,8 +260,15 @@ enum { } } -- (void) stylePropertyChanged:(NSNotification*)notification { - NodeStyle *style = [notification object]; +- (void) observeValueForKeyPath:(NSString*)keyPath + ofObject:(id)object + change:(NSDictionary*)change + context:(void*)context +{ + if ([object class] != [NodeStyle class]) + return; + + NodeStyle *style = object; GtkTreeModel *model = GTK_TREE_MODEL (store); GtkTreeIter row; @@ -268,9 +277,9 @@ enum { NodeStyle *rowStyle; gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1); if (style == rowStyle) { - if ([@"name" isEqual:[[notification userInfo] objectForKey:@"propertyName"]]) { + if ([@"name" isEqual:keyPath]) { gtk_list_store_set (store, &row, STYLES_NAME_COL, [[style name] UTF8String], -1); - } else if (![@"scale" isEqual:[[notification userInfo] objectForKey:@"propertyName"]]) { + } else { GdkPixbuf *pixbuf = [self pixbufOfNodeInStyle:style]; gtk_list_store_set (store, &row, STYLES_ICON_COL, pixbuf, -1); gdk_pixbuf_unref (pixbuf); @@ -379,6 +388,7 @@ enum { STYLES_PTR_COL, (gpointer)[style retain], -1); gdk_pixbuf_unref (pixbuf); + [self observeStyle:style]; } - (void) addStyle:(NodeStyle*)style { @@ -391,6 +401,57 @@ enum { [[NSNotificationCenter defaultCenter] postNotificationName:@"SelectedStyleChanged" object:self]; } +- (void) observeStyle:(NodeStyle*)style { + [style addObserver:self + forKeyPath:@"name" + options:NSKeyValueObservingOptionNew + context:NULL]; + [style addObserver:self + forKeyPath:@"strokeThickness" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"strokeColorRGB.red" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"strokeColorRGB.green" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"strokeColorRGB.blue" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"fillColorRGB.red" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"fillColorRGB.green" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"fillColorRGB.blue" + options:0 + context:NULL]; + [style addObserver:self + forKeyPath:@"shapeName" + options:0 + context:NULL]; +} + +- (void) stopObservingStyle:(NodeStyle*)style { + [style removeObserver:self forKeyPath:@"name"]; + [style removeObserver:self forKeyPath:@"strokeThickness"]; + [style removeObserver:self forKeyPath:@"strokeColorRGB.red"]; + [style removeObserver:self forKeyPath:@"strokeColorRGB.green"]; + [style removeObserver:self forKeyPath:@"strokeColorRGB.blue"]; + [style removeObserver:self forKeyPath:@"fillColorRGB.red"]; + [style removeObserver:self forKeyPath:@"fillColorRGB.green"]; + [style removeObserver:self forKeyPath:@"fillColorRGB.blue"]; + [style removeObserver:self forKeyPath:@"shapeName"]; +} + - (void) clearModel { [self setSelectedStyle:nil]; GtkTreeModel *model = GTK_TREE_MODEL (store); @@ -399,6 +460,7 @@ enum { do { NodeStyle *rowStyle; gtk_tree_model_get (model, &row, STYLES_PTR_COL, &rowStyle, -1); + [self stopObservingStyle:rowStyle]; [rowStyle release]; } while (gtk_tree_model_iter_next (model, &row)); } @@ -407,9 +469,11 @@ enum { - (void) reloadStyles { [self clearModel]; + cairo_surface_t *surface = [self createNodeIconSurface]; for (NodeStyle *style in [styleManager nodeStyles]) { - [self addStyle:style]; + [self addStyle:style usingSurface:surface]; } + cairo_surface_destroy (surface); } @end -- cgit v1.2.3