Permalink
Browse files

#14 Added cell populator and test

  • Loading branch information...
1 parent 41e81b9 commit 9f43514ea72b51123f29e7565d7b246b9499095d Tae Won Ha committed May 5, 2013

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -6,7 +6,6 @@
* See LICENSE
*/
-#import <Qkit/QLog.h>
#import "QMAppDelegate.h"
#import "TBCacao/TBCacao.h"
View
@@ -0,0 +1,19 @@
+/**
+ * Tae Won Ha
+ * http://qvacua.com
+ * https://github.com/qvacua
+ *
+ * See LICENSE
+ */
+
+#import <Cocoa/Cocoa.h>
+
+@protocol QMMindmapViewDataSource;
+@class QMCell;
+
+@interface QMCellPopulator : NSObject
+
+- (id)initWithDataSource:(id <QMMindmapViewDataSource>)dataSource;
+
+- (QMCell *)cellWithParent:(QMCell *)parentCell itemOfParent:(id)itemOfParent;
+@end
View
@@ -0,0 +1,94 @@
+/**
+ * Tae Won Ha
+ * http://qvacua.com
+ * https://github.com/qvacua
+ *
+ * See LICENSE
+ */
+
+#import "QMCellPopulator.h"
+#import "QMCell.h"
+#import "QMMindmapViewDataSource.h"
+#import "QMRootCell.h"
+
+@interface QMCellPopulator ()
+
+@property id <QMMindmapViewDataSource> dataSource;
+
+@end
+
+@implementation QMCellPopulator
+
+#pragma mark Public
+- (id)initWithDataSource:(id <QMMindmapViewDataSource>)dataSource {
+ self = [super init];
+ if (self) {
+ _dataSource = dataSource;
+ }
+
+ return self;
+}
+
+- (QMCell *)cellWithParent:(QMCell *)parentCell itemOfParent:(id)itemOfParent {
+ QMCell *cell;
+
+ if (itemOfParent == nil) {
+ QMRootCell *rootCell = [[QMRootCell alloc] initWithView:nil];
+ cell = rootCell;
+ } else {
+ cell = [[QMCell alloc] initWithView:nil];
+
+ if (parentCell.isRoot) {
+ BOOL isItemLeft = [_dataSource mindmapView:nil isItemLeft:itemOfParent];
+ if (isItemLeft) {
+ [(QMRootCell *) parentCell addObjectInLeftChildren:cell];
+ } else {
+ [parentCell addObjectInChildren:cell];
+ }
+ } else {
+ [parentCell addObjectInChildren:cell];
+ }
+ }
+
+ [self fillCellPropertiesWithIdentifier:itemOfParent cell:cell];
+ [self fillAllChildrenWithIdentifier:itemOfParent cell:cell];
+
+ return cell;
+}
+
+#pragma mark Private
+- (void)fillCellPropertiesWithIdentifier:(id)givenItem cell:(QMCell *)cell {
+ cell.identifier = [_dataSource mindmapView:nil identifierForItem:givenItem];
+ cell.stringValue = [_dataSource mindmapView:nil stringValueOfItem:givenItem];
+ cell.font = [_dataSource mindmapView:nil fontOfItem:givenItem];
+ cell.folded = [_dataSource mindmapView:nil isItemFolded:givenItem];
+
+ [self fillIconsOfCell:cell];
+}
+
+- (void)fillIconsOfCell:(QMCell *)cell {
+ NSArray *iconsOfItem = [_dataSource mindmapView:nil iconsOfItem:cell.identifier];
+ for (id icon in iconsOfItem) {
+ [cell insertObject:icon inIconsAtIndex:cell.icons.count];
+ }
+}
+
+- (void)fillAllChildrenWithIdentifier:(id)givenItem cell:(QMCell *)cell {
+ NSInteger childrenCount = [_dataSource mindmapView:nil numberOfChildrenOfItem:givenItem];
+ for (NSUInteger i = 0; i < childrenCount; i++) {
+ id childItem = [_dataSource mindmapView:nil child:i ofItem:givenItem];
+ [self cellWithParent:cell itemOfParent:childItem];
+ }
+
+ if (!cell.root) {
+ return;
+ }
+
+ NSInteger leftChildrenCount = [_dataSource mindmapView:nil numberOfLeftChildrenOfItem:givenItem];
+ for (NSUInteger i = 0; i < leftChildrenCount; i++) {
+ id childItem = [_dataSource mindmapView:nil leftChild:i ofItem:givenItem];
+ [self cellWithParent:cell itemOfParent:childItem];
+ }
+}
+
+@end
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="8.00">
<data>
- <int key="IBDocument.SystemTarget">1070</int>
+ <int key="IBDocument.SystemTarget">1080</int>
<string key="IBDocument.SystemVersion">12D78</string>
<string key="IBDocument.InterfaceBuilderVersion">3084</string>
<string key="IBDocument.AppKitVersion">1187.37</string>
@@ -1498,7 +1498,6 @@
<string key="NSFrame">{{18, 19}, {225, 18}}</string>
<reference key="NSSuperview" ref="542680204"/>
<reference key="NSWindow"/>
- <reference key="NSNextKeyView"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="NSEnabled">YES</bool>
<object class="NSButtonCell" key="NSCell" id="716246255">
@@ -1535,7 +1534,7 @@
<reference key="NSNextKeyView" ref="877139742"/>
<string key="NSReuseIdentifierKey">_NS:20</string>
</object>
- <string key="NSScreenRect">{{0, 0}, {1440, 878}}</string>
+ <string key="NSScreenRect">{{0, 0}, {1920, 1178}}</string>
<string key="NSMaxSize">{10000000000000, 10000000000000}</string>
<string key="NSFrameAutosaveName">PreferencesWindow</string>
<bool key="NSWindowIsRestorable">YES</bool>
@@ -2343,14 +2342,6 @@
</object>
<int key="connectionID">565</int>
</object>
- <object class="IBConnectionRecord">
- <object class="IBOutletConnection" key="connection">
- <string key="label">automaticUpdateCheckbox</string>
- <reference key="source" ref="1060956976"/>
- <reference key="destination" ref="877139742"/>
- </object>
- <int key="connectionID">622</int>
- </object>
<object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection">
<string key="label">preferencesWindow</string>
@@ -2367,14 +2358,6 @@
</object>
<int key="connectionID">626</int>
</object>
- <object class="IBConnectionRecord">
- <object class="IBActionConnection" key="connection">
- <string key="label">toggleAutomaticUpdateCheck:</string>
- <reference key="source" ref="1060956976"/>
- <reference key="destination" ref="877139742"/>
- </object>
- <int key="connectionID">627</int>
- </object>
<object class="IBConnectionRecord">
<object class="IBBindingConnection" key="connection">
<string key="label">value: automaticallyChecksForUpdates</string>
@@ -3993,10 +3976,6 @@
</object>
<int key="IBDocument.localizationMode">0</int>
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaFramework</string>
- <object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDependencyDefaults">
- <string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin.macosx</string>
- <real value="1070" key="NS.object.0"/>
- </object>
<bool key="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
<int key="IBDocument.defaultPropertyAccessControl">3</int>
<dictionary class="NSMutableDictionary" key="IBDocument.LastKnownImageSizes">
@@ -1,16 +0,0 @@
-#include <CoreFoundation/CoreFoundation.h>
-#include <CoreServices/CoreServices.h>
-#include <QuickLook/QuickLook.h>
-
-OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options);
-
-void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview);
-
-OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options) {
- // To complete your generator please implement the function GeneratePreviewForURL in GeneratePreviewForURL.c
- return noErr;
-}
-
-void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview) {
- // Implement only if supported
-}
@@ -0,0 +1,30 @@
+#include <QuickLook/QuickLook.h>
+#import <Qkit/QLog.h>
+#import <TBCacao/TBContext.h>
+#import "QMIconManager.h"
+#import "QMMindmapReader.h"
+#import "QMDocument.h"
+
+OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef cfUrl, CFStringRef contentTypeUTI, CFDictionaryRef options);
+
+void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview);
+
+OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef cfUrl, CFStringRef contentTypeUTI, CFDictionaryRef options) {
+ NSURL *url = (__bridge NSURL *) cfUrl;
+ TBContext *context = [TBContext sharedContext];
+
+ QMDocument *doc = [[QMDocument alloc] init];
+ doc.fileURL = url;
+
+ NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] initWithURL:url options:(NSFileWrapperReadingOptions) 0 error:NULL];
+ [doc readFromFileWrapper:fileWrapper ofType:nil error:NULL];
+
+
+
+
+ return noErr;
+}
+
+void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview) {
+ // Implement only if supported
+}
@@ -12,6 +12,7 @@
<key>LSItemContentTypes</key>
<array>
<string>com.qvacua.mindmap</string>
+ <string>public.objective-c-plus-plus-source</string>
</array>
</dict>
</array>
@@ -1,4 +1,3 @@
-//
-// Prefix header for all source files of the 'QmindLook' target in the 'QmindLook' project
-//
-
+#ifdef __OBJC__
+ #import <Cocoa/Cocoa.h>
+#endif
@@ -16,6 +16,7 @@
#include <CoreFoundation/CFPlugInCOM.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>
+#import <TBCacao/TBCacao.h>
// -----------------------------------------------------------------------------
// constants
@@ -41,7 +42,7 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail);
// The preview generation function to be implemented in GeneratePreviewForURL.c
-OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options);
+OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef cfUrl, CFStringRef contentTypeUTI, CFDictionaryRef options);
void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview);
// The layout for an instance of QuickLookGeneratorPlugIn
@@ -106,6 +107,12 @@ QuickLookGeneratorPluginType *AllocQuickLookGeneratorPluginType(CFUUIDRef inFact
/* This function returns the IUnknown interface so set the refCount to one. */
theNewInstance->refCount = 1;
+
+ /**
+ * Custom init code
+ */
+ [[TBContext sharedContext] initContext];
+
return theNewInstance;
}
@@ -0,0 +1,111 @@
+/**
+ * Tae Won Ha
+ * http://qvacua.com
+ * https://github.com/qvacua
+ *
+ * See LICENSE
+ */
+
+#import <objc/message.h>
+#import "QMBaseTestCase.h"
+#import "QMRootNode.h"
+#import "QMBaseTestCase+Util.h"
+#import "QMCellPopulator.h"
+#import "QMMindmapViewDataSourceImpl.h"
+#import "QMDocument.h"
+
+@interface QMCellPopulatorTest : QMBaseTestCase
+@end
+
+@implementation QMCellPopulatorTest {
+ QMRootNode *rootNode;
+
+ QMDocument *doc;
+ id <QMMindmapViewDataSource> dataSource;
+ QMCellPopulator *populator;
+
+ QMRootCell *rootCell;
+}
+
+- (void)setUp {
+ rootNode = [self rootNodeForTest];
+
+ doc = [[QMDocument alloc] init];
+ wireRootNodeOfDoc(doc, rootNode);
+ [doc setUndoManager:mock([NSUndoManager class])];
+
+ dataSource = [[QMMindmapViewDataSourceImpl alloc] initWithDoc:doc view:nil];
+
+ populator = [[QMCellPopulator alloc] initWithDataSource:dataSource];
+ rootCell = (QMRootCell *) [populator cellWithParent:nil itemOfParent:nil];
+}
+
+- (void)testInitAndPopulateCell {
+ BOOL (^checkCellAndNode)(QMCell *, QMNode *) = ^(QMCell *cell, QMNode *node) {
+ assertThat(cell.identifier, is(node));
+ assertThat(@(cell.isFolded), is(@(node.isFolded)));
+ assertThat(@(cell.isLeaf), is(@(node.isLeaf)));
+ assertThat(cell.stringValue, equalTo(node.stringValue));
+ assertThat(cell.children, hasSize(node.children.count));
+ assertThat(cell.icons, hasSize(node.icons.count));
+ if (cell.font != nil) {
+ assertThat(node.font, notNilValue());
+ }
+
+ return YES;
+ };
+
+ BOOL result = [self deepCompareStructureOfCell:rootCell
+ withNode:rootNode
+ ignoringFoldedChildren:NO
+ usingBlock:checkCellAndNode];
+
+ assertThat(@(result), isYes);
+}
+
+#pragma mark Private
+- (BOOL)deepCompareStructureOfCell:(id)cell
+ withNode:(id)node
+ ignoringFoldedChildren:(BOOL)ignoreFolded
+ usingBlock:(BOOL (^)(QMCell *, QMNode *))compare {
+
+ if (compare(cell, node) == NO) {
+ return NO;
+ }
+
+ if ([cell isFolded] && ignoreFolded) {
+ return YES;
+ }
+
+ NSArray *sourceChildCells;
+ NSArray *targetChildCells;
+
+ if ([cell isRoot]) {
+ sourceChildCells = [[cell children] arrayByAddingObjectsFromArray:[cell leftChildren]];
+ targetChildCells = [[node children] arrayByAddingObjectsFromArray:[node leftChildren]];
+ } else {
+ sourceChildCells = [cell children];
+ targetChildCells = [node children];
+ }
+
+ id childCell;
+ id childNode;
+
+ for (NSUInteger i = 0; i < [sourceChildCells count]; i++) {
+ childCell = [sourceChildCells objectAtIndex:i];
+ childNode = [targetChildCells objectAtIndex:i];
+
+ BOOL resultOfChildren = [self deepCompareStructureOfCell:childCell
+ withNode:childNode
+ ignoringFoldedChildren:ignoreFolded
+ usingBlock:compare];
+
+ if (resultOfChildren == NO) {
+ return NO;
+ }
+ }
+
+ return YES;
+}
+
+@end

0 comments on commit 9f43514

Please sign in to comment.