Permalink
Browse files

#14 correct rendering and thumbnail generation

  • Loading branch information...
1 parent 7ebf467 commit da40aeada5c956cfe4c9a4e94a24508f17b3c96e Tae Won Ha committed May 5, 2013
View
2 Qmind/Qmind-Info.plist
@@ -17,8 +17,6 @@
<string>Mindmap</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
- <key>LSHandlerRank</key>
- <string>Owner</string>
<key>LSItemContentTypes</key>
<array>
<string>com.qvacua.mindmap</string>
View
14 QmindLook/GeneratePreviewForURL.m
@@ -1,5 +1,4 @@
#include <QuickLook/QuickLook.h>
-#import "QMDocument.h"
#import "QMRootCell.h"
#import "QMLookUtil.h"
@@ -17,6 +16,17 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
return noErr;
}
+ /**
+ * When I use
+ * [NSGraphicsContext graphicsContextWithGraphicsPort:(void *) cgContext flipped:YES]
+ * and draw the mindmap, the whole mindmap is drawn upside down.
+ * When I use flipped:NO, then the NSLayoutManager is correctly flipped, but drawings of other primitive objects
+ * are not flipped => wrong rendering.
+ * Thus, we flip the whole CGContext which is the base of the NSGraphicsContext. Not very elegant, but it works.
+ */
+ CGAffineTransform verticalFlipTrafo = CGAffineTransformMake(1, 0, 0, -1, 0, canvasSize.height);
+ CGContextConcatCTM(cgContext, verticalFlipTrafo);
+
NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *) cgContext flipped:YES];
if (!context) {
return noErr;
@@ -25,7 +35,7 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:context];
- [rootCell drawRect:NSMakeRect(0, 0, canvasSize.width, canvasSize.height)];
+ [rootCell drawRect:NSMakeRect(qMindmapOrigin.x, qMindmapOrigin.y, canvasSize.width, canvasSize.height)];
[NSGraphicsContext restoreGraphicsState];
View
44 QmindLook/GenerateThumbnailForURL.m
@@ -1,13 +1,49 @@
-#include <CoreFoundation/CoreFoundation.h>
-#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>
+#import "QMLookUtil.h"
+#import "QMRootCell.h"
OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize);
void CancelThumbnailGeneration(void *thisInterface, QLThumbnailRequestRef thumbnail);
-OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize) {
- // To complete your generator please implement the function GenerateThumbnailForURL in GenerateThumbnailForURL.c
+OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef cfUrl, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize) {
+ @autoreleasepool {
+ QMRootCell *rootCell = [QMLookUtil rootCellForUrl:(__bridge NSURL *) cfUrl];
+ CGSize canvasSize = rootCell.familySize;
+
+ CGContextRef cgContext = QLThumbnailRequestCreateContext(thumbnail, canvasSize, false, NULL);
+ if (!cgContext) {
+ return noErr;
+ }
+
+ /**
+ * When I use
+ * [NSGraphicsContext graphicsContextWithGraphicsPort:(void *) cgContext flipped:YES]
+ * and draw the mindmap, the whole mindmap is drawn upside down.
+ * When I use flipped:NO, then the NSLayoutManager is correctly flipped, but drawings of other primitive objects
+ * are not flipped => wrong rendering.
+ * Thus, we flip the whole CGContext which is the base of the NSGraphicsContext. Not very elegant, but it works.
+ */
+ CGAffineTransform verticalFlipTrafo = CGAffineTransformMake(1, 0, 0, -1, 0, canvasSize.height);
+ CGContextConcatCTM(cgContext, verticalFlipTrafo);
+
+ NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *) cgContext flipped:YES];
+ if (!context) {
+ return noErr;
+ }
+
+ [NSGraphicsContext saveGraphicsState];
+ [NSGraphicsContext setCurrentContext:context];
+
+ // TODO: more intelligent thumbnail generation
+ [rootCell drawRect:NSMakeRect(qMindmapOrigin.x, qMindmapOrigin.y, canvasSize.width, canvasSize.height)];
+
+ [NSGraphicsContext restoreGraphicsState];
+
+ QLThumbnailRequestFlushContext(thumbnail, cgContext);
+ CFRelease(cgContext);
+ }
+
return noErr;
}
View
2 QmindLook/QMLookUtil.h
@@ -8,6 +8,8 @@
#import <Cocoa/Cocoa.h>
+static NSPoint qMindmapOrigin = {10, 10};
+
@class QMRootCell;
@interface QMLookUtil : NSObject
View
6 QmindLook/QMLookUtil.m
@@ -29,12 +29,12 @@ + (QMRootCell *)rootCellForUrl:(NSURL *)url {
QMCellPropertiesManager *cellPropertiesManager = [[QMCellPropertiesManager alloc] initWithDataSource:dataSource];
QMRootCell *rootCell = (QMRootCell *) [cellPropertiesManager cellWithParent:nil itemOfParent:nil];
- rootCell.familyOrigin = NSMakePoint(0, 0);
+ rootCell.familyOrigin = NSZeroPoint;
[rootCell computeGeometry];
NSSize canvasSize = rootCell.familySize;
- canvasSize.width += 25;
- canvasSize.height += 25;
+ canvasSize.width += qMindmapOrigin.x;
+ canvasSize.height += qMindmapOrigin.y;
return rootCell;
}

0 comments on commit da40aea

Please sign in to comment.