Improve the cover art part of SBMovieViewController.

This commit is contained in:
Damiano Galassi committed 2016-10-12 13:18:12 +02:00
1 parent 9996ffd8cf
commit 1529a5c367
2 files changed
+222 -170

No files matched your search

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11535.1" systemVersion="16B2338c" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES"> <document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11535.1" systemVersion="16B2548a" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies> <dependencies>
<deployment identifier="macosx"/> <deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11535.1"/> <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11535.1"/>
@@ -31,7 +31,7 @@
<tabViewItems> <tabViewItems>
<tabViewItem label="Metadata" identifier="1" id="11"> <tabViewItem label="Metadata" identifier="1" id="11">
<view key="view" id="12"> <view key="view" id="12">
<rect key="frame" x="10" y="25" width="482" height="241"/> <rect key="frame" x="10" y="29" width="482" height="237"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews> <subviews>
<popUpButton verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="30"> <popUpButton verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="30">
@@ -85,13 +85,13 @@
</popUpButtonCell> </popUpButtonCell>
</popUpButton> </popUpButton>
<scrollView autohidesScrollers="YES" horizontalLineScroll="16" horizontalPageScroll="10" verticalLineScroll="16" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="r4i-wT-qDC"> <scrollView autohidesScrollers="YES" horizontalLineScroll="16" horizontalPageScroll="10" verticalLineScroll="16" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="r4i-wT-qDC">
<rect key="frame" x="17" y="36" width="448" height="199"/> <rect key="frame" x="17" y="36" width="448" height="195"/>
<clipView key="contentView" id="cQT-bX-0YB"> <clipView key="contentView" id="cQT-bX-0YB">
<rect key="frame" x="1" y="0.0" width="446" height="198"/> <rect key="frame" x="1" y="0.0" width="446" height="194"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews> <subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" alternatingRowBackgroundColors="YES" autosaveColumns="NO" rowHeight="14" rowSizeStyle="automatic" headerView="qXU-xB-xRF" viewBased="YES" floatsGroupRows="NO" id="qg5-r0-E4V" customClass="SBTableView"> <tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" alternatingRowBackgroundColors="YES" autosaveColumns="NO" rowHeight="14" rowSizeStyle="automatic" headerView="qXU-xB-xRF" viewBased="YES" floatsGroupRows="NO" id="qg5-r0-E4V" customClass="SBTableView">
<rect key="frame" x="0.0" y="0.0" width="446" height="175"/> <rect key="frame" x="0.0" y="0.0" width="446" height="171"/>
<autoresizingMask key="autoresizingMask"/> <autoresizingMask key="autoresizingMask"/>
<size key="intercellSpacing" width="3" height="2"/> <size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/> <color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
+217 -165
View File
@@ -7,6 +7,7 @@
// //
NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2"; NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
NSString *SublerCoverArtPBoardType = @"SublerCoverArtPBoardType";
#import "SBMovieViewController.h" #import "SBMovieViewController.h"
#import "SBTableView.h" #import "SBTableView.h"
@@ -27,9 +28,6 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
@property (nonatomic) NSMutableArray<MP42MetadataItem *> *artworks; @property (nonatomic) NSMutableArray<MP42MetadataItem *> *artworks;
@property (nonatomic) NSArray<NSString *> *ratings; @property (nonatomic) NSArray<NSString *> *ratings;
@property (nonatomic) NSArray<NSArray *> *mediaKinds;
@property (nonatomic) NSArray<NSArray *> *hdVideo;
@property (nonatomic) NSArray<NSArray *> *contentRatings;
@property (nonatomic) NSTableCellView *dummyCell; @property (nonatomic) NSTableCellView *dummyCell;
@property (nonatomic) NSLayoutConstraint *dummyCellWidth; @property (nonatomic) NSLayoutConstraint *dummyCellWidth;
@@ -56,17 +54,27 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
@implementation SBMovieViewController @implementation SBMovieViewController
static NSArray<NSArray *> *_contentRatings;
static NSArray<NSArray *> *_hdVideo;
static NSArray<NSArray *> *_mediaKinds;
+ (void)initialize
{
if (self == [SBMovieViewController class]) {
_contentRatings = @[@[NSLocalizedString(@"None", nil), @0], @[NSLocalizedString(@"Clean", nil), @2], @[NSLocalizedString(@"Explicit", nil), @4]];
_mediaKinds = @[@[NSLocalizedString(@"Home Video", nil), @0], @[NSLocalizedString(@"Music", nil), @1], @[NSLocalizedString(@"Audiobook", nil), @2],
@[NSLocalizedString(@"Music Video", nil), @6], @[NSLocalizedString(@"Movie", nil), @9], @[NSLocalizedString(@"TV Show", nil), @10],
@[NSLocalizedString(@"Booklet", nil), @11], @[NSLocalizedString(@"Ringtone", nil), @14], @[NSLocalizedString(@"Podcast", nil), @21],
@[NSLocalizedString(@"iTunes U", nil), @23], @[NSLocalizedString(@"Alert Tone", nil), @27]];
_hdVideo = @[@[NSLocalizedString(@"No", nil), @0], @[NSLocalizedString(@"720p", nil), @1], @[NSLocalizedString(@"1080p", nil), @2]];
}
}
- (void)loadView - (void)loadView
{ {
[super loadView]; [super loadView];
_ratings = [MP42Ratings defaultManager].ratings; _ratings = [MP42Ratings defaultManager].ratings;
_contentRatings = @[@[@"None", @0], @[@"Clean", @2], @[@"Explicit", @4]];
_mediaKinds = @[@[@"Home Video", @0], @[@"Music", @1], @[@"Audiobook", @2],
@[@"Music Video", @6], @[@"Movie", @9], @[@"TV Show", @10],
@[@"Booklet", @11], @[@"Ringtone", @14], @[@"Podcast", @21],
@[@"iTunes U", @23], @[@"Alert Tone", @27]];
_hdVideo = @[@[@"No", @0], @[@"720p", @1], @[@"1080p", @2]];
[self updateSetsMenu:self]; [self updateSetsMenu:self];
@@ -90,20 +98,30 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
self.metadataTableView.pasteboardTypes = @[SublerMetadataPBoardType]; self.metadataTableView.pasteboardTypes = @[SublerMetadataPBoardType];
[self.metadataTableView scrollRowToVisible:0]; [self.metadataTableView scrollRowToVisible:0];
self.artworksView.pasteboardTypes = @[NSPasteboardTypeTIFF, NSPasteboardTypePNG]; self.artworksView.pasteboardTypes = @[SublerCoverArtPBoardType, NSPasteboardTypeTIFF, NSPasteboardTypePNG];
[self.artworksView setZoomValue:1.0]; [self.artworksView setZoomValue:1.0];
[self.artworksView reloadData]; [self.artworksView reloadData];
} }
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_artworksView setDelegate:nil];
[_artworksView setDataSource:nil];
[_metadataTableView setDelegate:nil];
[_metadataTableView setDataSource:nil];
}
- (void)setMetadata:(MP42Metadata *)metadata - (void)setMetadata:(MP42Metadata *)metadata
{ {
_metadata = metadata; _metadata = metadata;
[self updateMetadataArray]; [self updateMetadataArray];
self.artworks = [[metadata metadataItemsFilteredByIdentifier:MP42MetadataKeyCoverArt] mutableCopy]; [self updateCoverArtArray];
} }
#pragma mark - UI #pragma mark - Metadata
- (void)updateMetadataArray - (void)updateMetadataArray
{ {
@@ -120,6 +138,7 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
return (right < left) ? NSOrderedDescending : NSOrderedAscending; return (right < left) ? NSOrderedDescending : NSOrderedAscending;
}]; }];
} }
- (void)addMetadataItems:(NSArray<MP42MetadataItem *> *)items - (void)addMetadataItems:(NSArray<MP42MetadataItem *> *)items
{ {
for (MP42MetadataItem *item in items) { for (MP42MetadataItem *item in items) {
@@ -199,18 +218,9 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
} }
} }
- (IBAction)removeTag:(id)sender { - (IBAction)removeTag:(id)sender
NSIndexSet *rowIndexes = self.metadataTableView.selectedRowIndexes; {
NSUInteger current_index = rowIndexes.lastIndex; NSArray<MP42MetadataItem *> *items = [self.tags objectsAtIndexes:self.metadataTableView.selectedRowIndexes];
NSMutableArray *items = [[NSMutableArray alloc] init];
while (current_index != NSNotFound) {
if (self.metadataTableView.editedRow == -1) {
MP42MetadataItem *item = self.tags[current_index];
[items addObject:item];
}
current_index = [rowIndexes indexLessThanIndex: current_index];
}
[self removeMetadataItems:items]; [self removeMetadataItems:items];
} }
@@ -280,15 +290,28 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
- (void)applySet:(id)sender - (void)applySet:(id)sender
{ {
NSInteger tag = [sender tag]; NSInteger tag = [sender tag];
SBPresetManager *presetManager = [SBPresetManager sharedManager]; MP42Metadata *preset = [SBPresetManager sharedManager].presets[tag];
MP42Metadata *preset = presetManager.presets[tag];
// FIX add back undo support. MP42MetadataItemDataType dataTypes = MP42MetadataItemDataTypeString | MP42MetadataItemDataTypeStringArray |
[self.metadata mergeMetadata:preset]; MP42MetadataItemDataTypeBool | MP42MetadataItemDataTypeInteger |
[self updateMetadataArray]; MP42MetadataItemDataTypeIntegerArray | MP42MetadataItemDataTypeDate;
NSArray<MP42MetadataItem *> *items = [preset metadataItemsFilteredByDataType:dataTypes];
[self.artworksView reloadData]; if (items) {
[self.metadataTableView reloadData]; NSMutableArray<NSString *> *identifiers = [NSMutableArray array];
for (MP42MetadataItem *item in items) {
[identifiers addObject:item.identifier];
}
[self removeMetadataItems:[self.metadata metadataItemsFilteredByIdentifiers:identifiers]];
[self addMetadataItems:items];
}
items = [preset metadataItemsFilteredByIdentifier:MP42MetadataKeyCoverArt];
if (items) {
[self removeMetadataCoverArtItems:[self.metadata metadataItemsFilteredByIdentifier:MP42MetadataKeyCoverArt]];
[self addMetadataCoverArtItems:items];
}
} }
- (void)updateSetsMenu:(id)sender - (void)updateSetsMenu:(id)sender
@@ -380,7 +403,7 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
- (NSTableCellView *)textCellWithString:(NSString *)text tableView:(NSTableView *)tableView - (NSTableCellView *)textCellWithString:(NSString *)text tableView:(NSTableView *)tableView
{ {
NSTableCellView *cell = [tableView makeViewWithIdentifier:@"TextCell" owner:self]; NSTableCellView *cell = [tableView makeViewWithIdentifier:@"TextCell" owner:self];
cell.textField.stringValue = text; cell.textField.stringValue = text ? text : @"";
return cell; return cell;
} }
@@ -438,15 +461,15 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
case MP42MetadataItemDataTypeInteger: case MP42MetadataItemDataTypeInteger:
{ {
if ([item.identifier isEqualToString:MP42MetadataKeyContentRating]) { if ([item.identifier isEqualToString:MP42MetadataKeyContentRating]) {
cell = [self popUpCellWithArrayContents:self.contentRatings value:item.numberValue tableView:tableView]; cell = [self popUpCellWithArrayContents:_contentRatings value:item.numberValue tableView:tableView];
break; break;
} }
else if ([item.identifier isEqualToString:MP42MetadataKeyMediaKind]) { else if ([item.identifier isEqualToString:MP42MetadataKeyMediaKind]) {
cell = [self popUpCellWithArrayContents:self.mediaKinds value:item.numberValue tableView:tableView]; cell = [self popUpCellWithArrayContents:_mediaKinds value:item.numberValue tableView:tableView];
break; break;
} }
else if ([item.identifier isEqualToString:MP42MetadataKeyHDVideo]) { else if ([item.identifier isEqualToString:MP42MetadataKeyHDVideo]) {
cell = [self popUpCellWithArrayContents:self.hdVideo value:item.numberValue tableView:tableView]; cell = [self popUpCellWithArrayContents:_hdVideo value:item.numberValue tableView:tableView];
break; break;
} }
} }
@@ -560,13 +583,13 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
break; break;
case MP42MetadataItemDataTypeInteger: case MP42MetadataItemDataTypeInteger:
if ([item.identifier isEqualToString:MP42MetadataKeyContentRating]) { if ([item.identifier isEqualToString:MP42MetadataKeyContentRating]) {
value = self.contentRatings[index][1]; value = _contentRatings[index][1];
} }
else if ([item.identifier isEqualToString:MP42MetadataKeyMediaKind]) { else if ([item.identifier isEqualToString:MP42MetadataKeyMediaKind]) {
value = self.mediaKinds[index][1]; value = _mediaKinds[index][1];
} }
else if ([item.identifier isEqualToString:MP42MetadataKeyHDVideo]) { else if ([item.identifier isEqualToString:MP42MetadataKeyHDVideo]) {
value = self.hdVideo[index][1]; value = _hdVideo[index][1];
} }
break; break;
default: default:
@@ -602,23 +625,18 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
- (void)_copySelectionFromTableView:(NSTableView *)tableView; - (void)_copySelectionFromTableView:(NSTableView *)tableView;
{ {
NSIndexSet *rowIndexes = tableView.selectedRowIndexes; NSIndexSet *rowIndexes = tableView.selectedRowIndexes;
NSUInteger current_index = rowIndexes.lastIndex; NSArray<MP42MetadataItem *> *items = [self.tags objectsAtIndexes:rowIndexes];
NSMutableArray *data = [NSMutableArray array];
NSMutableString *string = [NSMutableString string]; NSMutableString *string = [NSMutableString string];
while (current_index != NSNotFound) { for (MP42MetadataItem *item in items) {
MP42MetadataItem *item = self.tags[current_index];
[string appendFormat:@"%@: %@\n", item.identifier, item.stringValue]; [string appendFormat:@"%@: %@\n", item.identifier, item.stringValue];
[data addObject:item];
current_index = [rowIndexes indexLessThanIndex: current_index];
} }
NSArray<NSString *> *types = @[SublerMetadataPBoardType, NSStringPboardType];
NSPasteboard *pb = [NSPasteboard generalPasteboard]; NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes:types owner:nil]; [pb declareTypes:@[SublerMetadataPBoardType, NSStringPboardType] owner:nil];
[pb setString:string forType: NSStringPboardType]; [pb setString:string forType: NSStringPboardType];
[pb setData:[NSKeyedArchiver archivedDataWithRootObject:data] forType:SublerMetadataPBoardType]; [pb setData:[NSKeyedArchiver archivedDataWithRootObject:items] forType:SublerMetadataPBoardType];
} }
- (void)_cutSelectionFromTableView:(NSTableView *)tableView; - (void)_cutSelectionFromTableView:(NSTableView *)tableView;
@@ -636,24 +654,6 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
[self addMetadataItems:data]; [self addMetadataItems:data];
} }
- (void)_pasteToImageBrowserView:(IKImageBrowserView *)ImageBrowserView
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classes = @[[NSURL class], [NSImage class]];
NSDictionary *options = @{NSPasteboardURLReadingContentsConformToTypesKey: [NSImage imageTypes]};
NSArray *copiedItems = [pasteboard readObjectsForClasses:classes options:options];
if (copiedItems != nil) {
for (id item in copiedItems) {
[self addArtwork:item];
}
[self.view.window.windowController.document updateChangeCount:NSChangeDone];
[self.artworksView reloadData];
}
}
- (NSString *)tableView:(NSTableView *)tableView toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row mouseLocation:(NSPoint)mouseLocation - (NSString *)tableView:(NSTableView *)tableView toolTipForCell:(NSCell *)cell rect:(NSRectPointer)rect tableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row mouseLocation:(NSPoint)mouseLocation
{ {
return @""; return @"";
@@ -680,8 +680,8 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row; - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row;
{ {
MP42MetadataItem *item = self.tags[row]; MP42MetadataItem *item = self.tags[row];
CGFloat height = 0;
BOOL calculateHeight = NO; BOOL calculateHeight = NO;
CGFloat height = 0;
// Height calculation is slow, so calculate only if stricly necessary. // Height calculation is slow, so calculate only if stricly necessary.
switch (item.dataType) { switch (item.dataType) {
@@ -699,7 +699,10 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
// Set the width in the dummy cell, and let autolayout calculate the height. // Set the width in the dummy cell, and let autolayout calculate the height.
self.dummyCellWidth.constant = self.columnWidth; self.dummyCellWidth.constant = self.columnWidth;
self.dummyCell.textField.preferredMaxLayoutWidth = self.columnWidth; self.dummyCell.textField.preferredMaxLayoutWidth = self.columnWidth;
self.dummyCell.textField.stringValue = item.stringValue; NSString *stringValue = item.stringValue;
if (stringValue) {
self.dummyCell.textField.stringValue = item.stringValue;
}
height = self.dummyCell.fittingSize.height; height = self.dummyCell.fittingSize.height;
self.rowHeights[item.identifier] = @(height); self.rowHeights[item.identifier] = @(height);
@@ -738,6 +741,109 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
self.removeTagButton.enabled = enabled; self.removeTagButton.enabled = enabled;
} }
#pragma mark - Cover Art
- (void)updateCoverArtArray
{
self.artworks = [[self.metadata metadataItemsFilteredByIdentifier:MP42MetadataKeyCoverArt] mutableCopy];
}
- (void)addMetadataCoverArtItems:(NSArray<MP42MetadataItem *> *)items
{
for (MP42MetadataItem *item in items) {
[self.metadata addMetadataItem:item];
}
NSUndoManager *undo = self.view.undoManager;
[[undo prepareWithInvocationTarget:self] removeMetadataCoverArtItems:items];
if (!undo.undoing) {
[undo setActionName:NSLocalizedString(@"Insert", @"Undo cover art insert.")];
}
[self updateCoverArtArray];
[self.artworksView reloadData];
}
- (void)removeMetadataCoverArtItems:(NSArray<MP42MetadataItem *> *)items
{
for (MP42MetadataItem *item in items) {
[self.metadata removeMetadataItem:item];
}
NSUndoManager *undo = self.view.undoManager;
[[undo prepareWithInvocationTarget:self] addMetadataCoverArtItems:items];
if (!undo.undoing) {
[undo setActionName:NSLocalizedString(@"Delete", @"Undo cover art delete")];
}
[self updateCoverArtArray];
[self.artworksView reloadData];
}
- (IBAction)removeArtwork:(id)sender
{
[self imageBrowser:self.artworksView removeItemsAtIndexes:[self.artworksView selectionIndexes]];
[self.artworksView reloadData];
}
- (BOOL)addArtworks:(NSArray *)items
{
NSMutableArray<MP42MetadataItem *> *itemsToAdd = [NSMutableArray array];
for (id item in items) {
MP42Image *artwork = nil;
if ([item isKindOfClass:[NSURL class]]) {
NSString *type;
if ([item getResourceValue:&type forKey:NSURLTypeIdentifierKey error:NULL]) {
if (UTTypeConformsTo((__bridge CFStringRef)type, (__bridge CFStringRef)@"public.jpeg")) {
artwork = [[MP42Image alloc] initWithData:[NSData dataWithContentsOfURL:item] type:MP42_ART_JPEG];
} else {
NSImage *artworkImage = [[NSImage alloc] initWithContentsOfURL:item];
artwork = [[MP42Image alloc] initWithImage:artworkImage];
}
}
}
else if ([item isKindOfClass:[NSImage class]]) {
artwork = [[MP42Image alloc] initWithImage:item];
}
else if ([item isKindOfClass:[MP42Image class]]) {
artwork = item;
}
if (artwork) {
MP42MetadataItem *metadataItem = [MP42MetadataItem metadataItemWithIdentifier:MP42MetadataKeyCoverArt
value:artwork
dataType:MP42MetadataItemDataTypeImage
extendedLanguageTag:nil];
[itemsToAdd addObject:metadataItem];
}
}
if (itemsToAdd.count) {
[self addMetadataCoverArtItems:itemsToAdd];
}
return itemsToAdd.count;
}
- (IBAction)selectArtwork:(id)sender
{
NSOpenPanel *panel = [NSOpenPanel openPanel];
panel.allowsMultipleSelection = YES;
panel.canChooseFiles = YES;
panel.canChooseDirectories = NO;
panel.allowedFileTypes = @[@"public.image"];
[panel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
[self addArtworks:panel.URLs];
}
}];
}
#pragma mark - IKImageBrowserDataSource #pragma mark - IKImageBrowserDataSource
- (NSUInteger)numberOfItemsInImageBrowser:(IKImageBrowserView *) aBrowser - (NSUInteger)numberOfItemsInImageBrowser:(IKImageBrowserView *) aBrowser
@@ -761,6 +867,13 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
[self.artworks insertObject:object atIndex:destinationIndex]; [self.artworks insertObject:object atIndex:destinationIndex];
} }
for (MP42MetadataItem *item in self.artworks) {
[self.metadata removeMetadataItem:item];
}
for (MP42MetadataItem *item in self.artworks) {
[self.metadata addMetadataItem:item];
}
[self.view.window.windowController.document updateChangeCount:NSChangeDone]; [self.view.window.windowController.document updateChangeCount:NSChangeDone];
return YES; return YES;
@@ -768,25 +881,48 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
- (NSUInteger)imageBrowser:(IKImageBrowserView *)aBrowser writeItemsAtIndexes:(NSIndexSet *)itemIndexes toPasteboard:(NSPasteboard *)pasteboard - (NSUInteger)imageBrowser:(IKImageBrowserView *)aBrowser writeItemsAtIndexes:(NSIndexSet *)itemIndexes toPasteboard:(NSPasteboard *)pasteboard
{ {
NSInteger index; [pasteboard declareTypes:@[SublerCoverArtPBoardType, NSPasteboardTypePNG] owner:nil];
[pasteboard declareTypes:@[NSTIFFPboardType] owner:nil];
for (index = itemIndexes.lastIndex; index != NSNotFound; index = [itemIndexes indexLessThanIndex:index]) { for (MP42MetadataItem *item in [self.artworks objectsAtIndexes:itemIndexes]) {
NSArray *representations = self.artworks[index].imageValue.image.representations; MP42Image *image = item.imageValue;
if (representations) { if (image) {
NSData *bitmapData = [NSBitmapImageRep representationOfImageRepsInArray:representations NSArray *representations = image.image.representations;
usingType:NSTIFFFileType properties:@{}]; if (representations) {
[pasteboard setData:bitmapData forType:NSTIFFPboardType]; NSData *bitmapData = [NSBitmapImageRep representationOfImageRepsInArray:representations usingType:NSBitmapImageFileTypePNG properties:@{}];
[pasteboard setData:bitmapData forType:NSPasteboardTypePNG];
}
[pasteboard setData:[NSKeyedArchiver archivedDataWithRootObject:image] forType:SublerCoverArtPBoardType];
} }
} }
return itemIndexes.count; return itemIndexes.count;
} }
- (void)_pasteToImageBrowserView:(IKImageBrowserView *)ImageBrowserView
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *items = nil;
NSData *archivedImageData = [pasteboard dataForType:SublerCoverArtPBoardType];
if (archivedImageData) {
MP42Image *image = [NSKeyedUnarchiver unarchiveObjectWithData:archivedImageData];
if (image) {
items = @[image];
}
}
else {
NSArray *classes = @[[NSURL class], [NSImage class]];
NSDictionary *options = @{NSPasteboardURLReadingContentsConformToTypesKey: [NSImage imageTypes]};
items = [pasteboard readObjectsForClasses:classes options:options];
}
[self addArtworks:items];
}
- (void)imageBrowser:(IKImageBrowserView *)aBrowser removeItemsAtIndexes:(NSIndexSet *)indexes - (void)imageBrowser:(IKImageBrowserView *)aBrowser removeItemsAtIndexes:(NSIndexSet *)indexes
{ {
[self.artworks removeObjectsAtIndexes:indexes]; NSArray<MP42MetadataItem *> *items = [self.artworks objectsAtIndexes:indexes];
[self.view.window.windowController.document updateChangeCount:NSChangeDone]; [self removeMetadataCoverArtItems:items];
} }
#pragma mark - IKImageBrowserDelegate #pragma mark - IKImageBrowserDelegate
@@ -808,64 +944,7 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
[self.artworksView setZoomValue:[sender floatValue]]; [self.artworksView setZoomValue:[sender floatValue]];
} }
- (IBAction)removeArtwork:(id)sender #pragma mark - Cover Art drag & drop
{
[self imageBrowser:self.artworksView removeItemsAtIndexes:[self.artworksView selectionIndexes]];
[self.artworksView reloadData];
}
- (BOOL)addArtwork:(id)item
{
MP42Image *artwork = nil;
if ([item isKindOfClass:[NSURL class]]) {
NSString *type;
if ([item getResourceValue:&type forKey:NSURLTypeIdentifierKey error:NULL]) {
if (UTTypeConformsTo((__bridge CFStringRef)type, (__bridge CFStringRef)@"public.jpeg")) {
artwork = [[MP42Image alloc] initWithData:[NSData dataWithContentsOfURL:item] type:MP42_ART_JPEG];
} else {
NSImage *artworkImage = [[NSImage alloc] initWithContentsOfURL:item];
artwork = [[MP42Image alloc] initWithImage:artworkImage];
}
}
} else if ([item isKindOfClass:[NSImage class]]) {
artwork = [[MP42Image alloc] initWithImage:item];
} else if ([item isKindOfClass:[MP42Image class]]) {
artwork = item;
}
if (artwork) {
MP42MetadataItem *metadataItem = [MP42MetadataItem metadataItemWithIdentifier:MP42MetadataKeyCoverArt
value:artwork
dataType:MP42MetadataItemDataTypeImage
extendedLanguageTag:nil];
[self.metadata addMetadataItem:metadataItem];
return YES;
}
return NO;
}
- (IBAction)selectArtwork:(id)sender
{
NSOpenPanel *panel = [NSOpenPanel openPanel];
panel.allowsMultipleSelection = YES;
panel.canChooseFiles = YES;
panel.canChooseDirectories = NO;
panel.allowedFileTypes = @[@"public.image"];
[panel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) {
for (NSURL *url in panel.URLs) {
[self addArtwork:url];
}
[self.view.window.windowController.document updateChangeCount:NSChangeDone];
[self.artworksView reloadData];
}
}];
}
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{ {
@@ -879,42 +958,15 @@ NSString *SublerMetadataPBoardType = @"SublerMetadataPBoardTypeV2";
- (BOOL)performDragOperation:(id)sender - (BOOL)performDragOperation:(id)sender
{ {
BOOL edited = NO;
NSPasteboard *pasteboard = [sender draggingPasteboard]; NSPasteboard *pasteboard = [sender draggingPasteboard];
NSArray *classes = @[[NSURL class], [NSImage class]]; NSArray *classes = @[[NSURL class], [NSImage class]];
NSDictionary *options = @{NSPasteboardURLReadingContentsConformToTypesKey: [NSImage imageTypes]}; NSDictionary *options = @{NSPasteboardURLReadingContentsConformToTypesKey: [NSImage imageTypes]};
NSArray *draggedItems = [pasteboard readObjectsForClasses:classes options:options]; NSArray *draggedItems = [pasteboard readObjectsForClasses:classes options:options];
if (draggedItems) { return [self addArtworks:draggedItems];
for (id dragItem in draggedItems) {
edited = [self addArtwork:dragItem];
}
if (edited) {
[self.view.window.windowController.document updateChangeCount:NSChangeDone];
[self.artworksView reloadData];
return YES;
}
}
return NO;
} }
- (void)concludeDragOperation:(id < NSDraggingInfo >)sender - (void)concludeDragOperation:(id <NSDraggingInfo>)sender {}
{
[self.artworksView reloadData];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_artworksView setDelegate:nil];
[_artworksView setDataSource:nil];
[_metadataTableView setDelegate:nil];
[_metadataTableView setDataSource:nil];
}
@end @end