Group view has rudimentary drag'n'drop support

This commit is contained in:
michael starke
2013-06-09 02:52:22 +02:00
parent 53850d8d35
commit 1130f56567
6 changed files with 61 additions and 39 deletions

View File

@@ -17,10 +17,11 @@ APPKIT_EXTERN NSString *const MPGroupNameUndoableKey;
- (NSString *)nameUndoable;
- (void)setNameUndoable:(NSString *)newName;
- (void)addEntryUndoable:(KdbEntry *)entry;
- (void)addGroupUndoable:(KdbGroup *)group;
- (void)removeGroupUndoable:(KdbGroup *)group;
- (void)removeEntryUndoable:(KdbEntry *)entry;
- (void)moveToGroupUndoable:(KdbGroup *)group;
@end

View File

@@ -58,4 +58,17 @@ NSString *const MPGroupNameUndoableKey = @"nameUndoable";
[[self undoManager] setActionName:NSLocalizedString(@"UNDO_DELETE_GROUP", @"Create Group Undo")];
[group.parent removeObjectFromGroupsAtIndex:index];
}
- (void)moveToGroupUndoable:(KdbGroup *)group {
NSInteger index = [self.parent.groups indexOfObject:self];
if(NSNotFound == index) {
return; // No object found
}
[[self undoManager] registerUndoWithTarget:self selector:@selector(moveToGroupUndoable:) object:self.parent];
[[self undoManager] setActionName:NSLocalizedString(@"UNDO_MOVE_GROUP", @"Move Group Undo")];
[self.parent removeObjectFromGroupsAtIndex:index];
[group insertObject:self inGroupsAtIndex:[group.groups count]];
}
@end

View File

@@ -8,7 +8,11 @@
#import <Foundation/Foundation.h>
@interface MPOutlineDataSource : NSObject <NSOutlineViewDataSource>
APPKIT_EXTERN NSString *const MPPasteBoardType;
@class KdbGroup;
@interface MPOutlineDataSource : NSObject <NSOutlineViewDataSource> {
KdbGroup *_draggedItem;
}
@end

View File

@@ -9,43 +9,45 @@
#import "MPOutlineDataSource.h"
#import "MPDocument.h"
#import "KdbLib.h"
#import "KdbGroup+Undo.h"
NSString *const MPPasteBoardType = @"com.hicknhack.macpass.pasteboard";
@implementation MPOutlineDataSource
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
if(!item) {
return 1;
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard {
[_draggedItem release];
_draggedItem = nil;
[pasteboard setString:@"Weee" forType:MPPasteBoardType];
if([items count] == 1) {
_draggedItem = [[[items lastObject] representedObject] retain];
return (nil != _draggedItem.parent);
}
if( [item isKindOfClass:[KdbGroup class]]) {
KdbGroup *group = item;
return [[group groups] count];
}
return 0;
}
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
if(!item) {
MPDocument *document = [[[outlineView window] windowController] document];
return document.root;
}
if( [item isKindOfClass:[KdbGroup class]]) {
KdbGroup *group = item;
if( [[group groups] count] > index ) {
return [group groups][index];
}
}
return nil;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
if(!item) {
MPDocument *document = [[[outlineView window] windowController] document];
return ([[document.root groups] count] > 0);
}
if([item isKindOfClass:[KdbGroup class]])
{
KdbGroup *group = item;
return ([[group groups] count] > 0);
}
return NO;
return YES;
}
- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id<NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index {
if(_draggedItem) {
KdbGroup *target = [item representedObject];
if( target == nil) {
return NSDragOperationNone; // Draggin over root
}
BOOL validParent = ( _draggedItem.parent == target && index != NSOutlineViewDropOnItemIndex);
if(validParent || _draggedItem.parent != target) {
return NSDragOperationMove;
info.animatesToDestination = YES;
}
}
return NSDragOperationNone;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id<NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index {
NSLog(@"Drag %@ to: %@ index: %ld", _draggedItem, [item representedObject], index);
KdbGroup *target = [item representedObject];
BOOL accepted = (target != _draggedItem.parent);
info.animatesToDestination = YES;
[_draggedItem moveToGroupUndoable:target];
return accepted;
}
@end

View File

@@ -13,6 +13,7 @@
#import "MPAppDelegate.h"
#import "KdbLib.h"
@interface MPOutlineViewController () {
BOOL _bindingEstablished;
}
@@ -42,8 +43,8 @@
if (self) {
_treeController = [[NSTreeController alloc] init];
_bindingEstablished = NO;
self.outlineDelegate = [[[MPOutlineViewDelegate alloc] init] autorelease];
self.datasource = [[[MPOutlineDataSource alloc] init] autorelease];
_outlineDelegate = [[MPOutlineViewDelegate alloc] init];
_datasource = [[MPOutlineDataSource alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_didUpdateData:)
@@ -71,11 +72,11 @@
}
- (void)didLoadView {
//[self.outlineView setDataSource:self.datasource];
[self.outlineView setDelegate:self.outlineDelegate];
[self.outlineView setMenu:[self _contextMenu]];
[self.outlineView setAllowsEmptySelection:YES];
[self.outlineView setFloatsGroupRows:NO];
[_outlineView registerForDraggedTypes:@[ MPPasteBoardType ]];
[self.outlineView setDraggingSourceOperationMask:NSDragOperationEvery forLocal:YES];
}
@@ -85,6 +86,7 @@
[_treeController setChildrenKeyPath:@"groups"];
[_treeController bind:NSContentBinding toObject:document withKeyPath:@"root" options:nil];
[_outlineView bind:NSContentBinding toObject:_treeController withKeyPath:@"arrangedObjects" options:nil];
[_outlineView setDataSource:self.datasource];
_bindingEstablished = YES;
}
NSTreeNode *node = [_outlineView itemAtRow:0];

View File

@@ -48,7 +48,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>A2F</string>
<string>A6E</string>
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSHumanReadableCopyright</key>