Import Annotations/Tags from Text file

This commit is contained in:
Matthias Neeracher 2011-08-18 17:57:51 +02:00
parent 1ffab4eced
commit 7dd5af9737
5 changed files with 116 additions and 2 deletions

View File

@ -22,6 +22,7 @@
@property BOOL exportTags;
- (IBAction)addMediaFiles:(id)sender;
- (IBAction)importText:(id)sender;
- (IBAction)exportMedia:(id)sender;
- (IBAction)exportText:(id)sender;
- (void)addMedia:(NSArray *)urls;

View File

@ -203,7 +203,10 @@
- (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)item
{
if ([item action] == @selector(addMedia:))
if ([item action] == @selector(addMediaFiles:)
|| [item action] == @selector(exportText:)
|| [item action] == @selector(importText:)
)
return YES;
if ([item action] == @selector(toggleMediaPlay:)) {
@ -362,6 +365,58 @@ static NSTimeInterval sLastSkip = 0.0;
[self exportTextToURL:[savePanel URL]];
}];
}
- (void)importTextFromURL:(NSURL *)url
{
NSString * text = [NSString stringWithContentsOfURL:url usedEncoding:nil error:nil];
NSArray * lines = [text componentsSeparatedByString:@"\n"];
if ([lines count] > 0) {
MADocument * doc = [self document];
MAMedia * media =
[mediaController selectionIndex] != NSNotFound ? [[mediaController selectedObjects] objectAtIndex:0] : nil;
//
// Detect whether we start with a tag
//
BOOL nextIsTag = NO;
NSString * firstLine = [lines objectAtIndex:0];
NSRegularExpression * timeCode = [NSRegularExpression regularExpressionWithPattern:@"^\\d:\\d\\d:\\d\\d:\\d\\d" options:0 error:nil];
if ([timeCode numberOfMatchesInString:firstLine options:0 range:NSMakeRange(0, [firstLine length])] > 0)
nextIsTag = YES;
for (NSString * line in lines) {
NSArray * components = [line componentsSeparatedByString:@"\t"];
NSUInteger numComp = [components count];
if ([line isEqual:@""]) {
nextIsTag = YES;
} else if (nextIsTag) {
if (!media)
break;
QTTime location= QTTimeFromString([components objectAtIndex:0]);
MAAnno * anno = [doc addAnnotationForMedia:media location:location];
[anno setNotes:[components objectAtIndex:1]];
NSMutableArray * tagDescs = [NSMutableArray array];
for (NSUInteger i = 2; i<numComp; ++i)
[tagDescs addObject:[doc tagDescriptionForName:[components objectAtIndex:i]]];
[anno setTagDescriptions:tagDescs];
} else {
NSString * name = [components objectAtIndex:0];
NSString * notes = numComp > 1 ? [components objectAtIndex:1] : nil;
[doc tagDescriptionForName:name notes:notes];
}
}
}
}
- (IBAction)importText:(id)sender
{
NSOpenPanel * openPanel = [NSOpenPanel openPanel];
[openPanel setAllowedFileTypes:[NSArray arrayWithObject:@"txt"]];
[openPanel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) {
[openPanel orderOut:self];
if (result == NSFileHandlingPanelOKButton)
[self importTextFromURL:[openPanel URL]];
}];
}
@end
@implementation MADocumentWindow

View File

@ -20,6 +20,7 @@
- (void)addMediaURL:(NSURL *)url name:(NSString *)name date:(NSDate *)date copying:(BOOL)copying;
- (MAAnno *)addAnnotationForMedia:(MAMedia *)media location:(QTTime)location;
- (MATagDescription *)tagDescriptionForName:(NSString *)name;
- (MATagDescription *)tagDescriptionForName:(NSString *)name notes:(NSString *)notes;
- (NSArray *)tagNamesMatchingPrefix:(NSString *)prefix;
- (NSSet *)allTags;

View File

@ -87,6 +87,16 @@
}
}
- (MATagDescription *)tagDescriptionForName:(NSString *)name notes:(NSString *)notes
{
MATagDescription * tag = [self tagDescriptionForName:name];
if (notes && [notes length] && ![tag.notes length])
[tag setNotes:notes];
return tag;
}
- (NSArray *)tagNamesMatchingPrefix:(NSString *)prefix
{
NSManagedObjectContext *moc = [self managedObjectContext];
@ -100,4 +110,12 @@
return names;
}
- (NSSet *)allTags
{
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest * fetch = [NSFetchRequest fetchRequestWithEntityName:@"MATagDescription"];
return [NSSet setWithArray:[moc executeFetchRequest:fetch error:nil]];
}
@end

View File

@ -293,6 +293,15 @@
<reference key="NSOnImage" ref="1033313550"/>
<reference key="NSMixedImage" ref="310636482"/>
</object>
<object class="NSMenuItem" id="627623477">
<reference key="NSMenu" ref="720053764"/>
<string key="NSTitle">Import Tags/Annotations…</string>
<string key="NSKeyEquiv">a</string>
<int key="NSKeyEquivModMask">1572864</int>
<int key="NSMnemonicLoc">2147483647</int>
<reference key="NSOnImage" ref="1033313550"/>
<reference key="NSMixedImage" ref="310636482"/>
</object>
<object class="NSMenuItem" id="678484874">
<reference key="NSMenu" ref="720053764"/>
<string key="NSTitle">Export…</string>
@ -1379,6 +1388,14 @@
</object>
<int key="connectionID">595</int>
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">importText:</string>
<reference key="source" ref="1014"/>
<reference key="destination" ref="627623477"/>
</object>
<int key="connectionID">598</int>
</object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@ -1477,6 +1494,7 @@
<reference ref="7029338"/>
<reference ref="678484874"/>
<reference ref="995690267"/>
<reference ref="627623477"/>
</object>
<reference key="parent" ref="379814623"/>
</object>
@ -2089,6 +2107,11 @@
<reference key="object" ref="995690267"/>
<reference key="parent" ref="720053764"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">596</int>
<reference key="object" ref="627623477"/>
<reference key="parent" ref="720053764"/>
</object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
@ -2187,6 +2210,7 @@
<string>583.IBPluginDependency</string>
<string>590.IBPluginDependency</string>
<string>593.IBPluginDependency</string>
<string>596.IBPluginDependency</string>
<string>72.IBPluginDependency</string>
<string>73.IBPluginDependency</string>
<string>74.IBPluginDependency</string>
@ -2304,6 +2328,7 @@
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
</object>
</object>
<object class="NSMutableDictionary" key="unlocalizedProperties">
@ -2318,7 +2343,7 @@
<reference key="dict.values" ref="0"/>
</object>
<nil key="sourceID"/>
<int key="maxID">595</int>
<int key="maxID">598</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@ -2360,6 +2385,7 @@
<string>addMediaFiles:</string>
<string>exportMedia:</string>
<string>exportText:</string>
<string>importText:</string>
<string>mediaSkipBackward:</string>
<string>mediaSkipForward:</string>
<string>toggleMediaPlay:</string>
@ -2373,6 +2399,7 @@
<string>id</string>
<string>id</string>
<string>id</string>
<string>id</string>
</object>
</object>
<object class="NSMutableDictionary" key="actionInfosByName">
@ -2383,6 +2410,7 @@
<string>addMediaFiles:</string>
<string>exportMedia:</string>
<string>exportText:</string>
<string>importText:</string>
<string>mediaSkipBackward:</string>
<string>mediaSkipForward:</string>
<string>toggleMediaPlay:</string>
@ -2405,6 +2433,10 @@
<string key="name">exportText:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo">
<string key="name">importText:</string>
<string key="candidateClassName">id</string>
</object>
<object class="IBActionInfo">
<string key="name">mediaSkipBackward:</string>
<string key="candidateClassName">id</string>
@ -2428,6 +2460,7 @@
<string>mediaController</string>
<string>mediaTable</string>
<string>movieView</string>
<string>textExportAccessoryView</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
@ -2436,6 +2469,7 @@
<string>NSArrayController</string>
<string>NSTableView</string>
<string>QTMovieView</string>
<string>NSView</string>
</object>
</object>
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
@ -2447,6 +2481,7 @@
<string>mediaController</string>
<string>mediaTable</string>
<string>movieView</string>
<string>textExportAccessoryView</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
@ -2470,6 +2505,10 @@
<string key="name">movieView</string>
<string key="candidateClassName">QTMovieView</string>
</object>
<object class="IBToOneOutletInfo">
<string key="name">textExportAccessoryView</string>
<string key="candidateClassName">NSView</string>
</object>
</object>
</object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">