Skip to content

Commit

Permalink
Adding new option for thumbnails!
Browse files Browse the repository at this point in the history
  • Loading branch information
onekiloparsec committed Apr 18, 2015
1 parent b205652 commit 0bc743f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 25 deletions.
1 change: 1 addition & 0 deletions QLFits3/Common.h
Expand Up @@ -13,3 +13,4 @@
void DrawSpectrumCanvas(CGContextRef context, CGSize canvasSize);
void DrawSpectrum(CGContextRef context, CGSize canvasSize, FITSSpectrum *spectrum);
void DrawObjectName(CGContextRef context, CGSize canvasSize, NSString *objectName, BOOL forThumbnail, BOOL forSpectrum);
void DrawHDUSummary(CGContextRef context, CGSize canvasSize, NSString *summaryString, BOOL forThumbnail, BOOL forSpectrum);
19 changes: 19 additions & 0 deletions QLFits3/Common.m
Expand Up @@ -68,3 +68,22 @@ void DrawObjectName(CGContextRef context, CGSize canvasSize, NSString *objectNam
}
}

void DrawHDUSummary(CGContextRef context, CGSize canvasSize, NSString *summaryString, BOOL forThumbnail, BOOL forSpectrum)
{
if (summaryString != nil && [summaryString length] > 0) {
const char *summary = [summaryString UTF8String];
CGFloat fontPoint = (forSpectrum && forThumbnail) ? 46.0 : 20.0;
CGContextSelectFont(context, "Futura", fontPoint, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGContextSetRGBStrokeColor(context, 1., 1., 1., 0.6);
CGContextSetRGBFillColor(context, 0.0, 0.5, 1.0, 1.0);

CGFloat h = (forThumbnail) ? (canvasSize.height-1.5*pad)/2.0 : canvasSize.height-1.5*pad;
CGContextShowTextAtPoint(context,
(canvasSize.width - strlen(summary)/2*fontPoint)/2.0,
h - 70,
summary,
strlen(summary));
}
}

51 changes: 41 additions & 10 deletions QLFits3/GeneratePreviewForURL.m
Expand Up @@ -10,9 +10,22 @@
#define MAX_HDU_COUNT 10
#define ATTACH_IMAGES NO

static NSDictionary *defaultOptionsValues = nil;

OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options);
void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview);

NSString *optionStringForKey(NSString *key, NSString *title, NSString *subtitle, BOOL checked);
NSString *optionStringForKey(NSString *key, NSString *title, NSString *subtitle, BOOL checked)
{
NSMutableString *options = [NSMutableString string];
[options appendString:@"<div class=\"option\">"];
[options appendFormat:@"<div class=\"option_title\">%@ <span class=\"option_subtitle\">%@</span></div>", title, subtitle];
[options appendFormat:@"<input class=\"option_input\" type=\"checkbox\" id=\"%@Input\" %@ />", key, (checked) ? @"checked" : @""];
[options appendString:@"</div>"];
return [options copy];
}

/* -----------------------------------------------------------------------------
Generate a preview for file
Expand All @@ -31,10 +44,15 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
@autoreleasepool {
static NSString *suiteName = @"com.onekiloparsec.qlfitsconfig.user-defaults-suite";
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:suiteName];
BOOL alwaysShowHeaders = YES;

BOOL alwaysShowHeaders = YES; // One day, it should be retrieved from defaultOptions.plist file.
if ([defaults stringForKey:@"alwaysShowHeaders"]) {
alwaysShowHeaders = [[defaults stringForKey:@"alwaysShowHeaders"] isEqualToString:@"1"];
}
BOOL showSummaryInThumbnails = NO;
if ([defaults stringForKey:@"showSummaryInThumbnails"]) {
showSummaryInThumbnails = [[defaults stringForKey:@"showSummaryInThumbnails"] isEqualToString:@"1"];
}

NSMutableDictionary *previewProperties = [NSMutableDictionary dictionary];
[previewProperties setObject:@"UTF-8" forKey:(__bridge NSString *)kQLPreviewPropertyTextEncodingNameKey];
Expand All @@ -45,20 +63,16 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
NSMutableDictionary *attachements = [NSMutableDictionary dictionary];
[previewProperties setObject:attachements forKey:(__bridge NSString *)kQLPreviewPropertyAttachmentsKey];

DebugLog(@"[QLFits3] Open FITS file");
FITSFile *fits = [FITSFile FITSFileWithURL:(__bridge NSURL *)url];
CFITSIO_STATUS status = [fits open];

NSMutableDictionary *synthesizedInfo = [NSMutableDictionary dictionary];
[synthesizedInfo setObject:[[(__bridge NSURL *)url path] lastPathComponent] forKey:@"FileName"];

NSDictionary *shortSummary = [FITSFile FITSFileShortSummaryWithURL:(__bridge NSURL *)url];
NSDictionary *shortSummary = [fits shortSummary];
[synthesizedInfo setObject:(shortSummary) ? shortSummary[@"summary"] : @"" forKey:@"ContentSummary"];

NSMutableString *options = [NSMutableString string];
[options appendFormat:@"<input class=\"OptionInput\" type=\"checkbox\" id=\"alwaysShowHeadersInput\" %@ />", (alwaysShowHeaders) ? @"checked" : @""];
[options appendString:@"<div class=\"OptionTitle\">Always Show Headers <span class=\"OptionSubtitle\">(By default, HDUs with no data immediately show their header)</span></div>"];
[synthesizedInfo setObject:options forKey:@"QuickLookOptions"];

DebugLog(@"[QLFits3] Open FITS file");
FITSFile *fits = [FITSFile FITSFileWithURL:(__bridge NSURL *)url];
CFITSIO_STATUS status = [fits open];

// The above might have taken some time, so before proceeding make sure the user didn't cancel the request
if (QLPreviewRequestIsCancelled(preview)) return noErr;
Expand Down Expand Up @@ -219,6 +233,22 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
[synthesizedInfo setObject:HDULinesString forKey:@"HDUTableLines"];
}


NSMutableString *options = [NSMutableString string];

[options appendString:optionStringForKey(@"alwaysShowHeaders",
@"Always Show Headers",
@"By default, for HDUs with no data, the header is displayed in place. Uncheck to hide them behind a Toggle button.",
alwaysShowHeaders)];

[options appendString:optionStringForKey(@"showSummaryInThumbnails",
@"Show HDUs Summary in Thumbnails",
@"Check to see HDUs summary also in Thumbnails.",
showSummaryInThumbnails)];

[synthesizedInfo setObject:options forKey:@"QuickLookOptions"];


NSString *versionString = [[bundle infoDictionary] objectForKey:@"CFBundleVersion"];
[synthesizedInfo setObject:versionString forKey:@"BundleVersion"];

Expand All @@ -232,6 +262,7 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
}

DebugLog(@"[QLFits3] Ready to shoot.");
NSLog(@"%@", html);

QLPreviewRequestSetDataRepresentation(preview,
(__bridge CFDataRef)[html dataUsingEncoding:NSUTF8StringEncoding],
Expand Down
19 changes: 19 additions & 0 deletions QLFits3/GenerateThumbnailForURL.m
Expand Up @@ -30,6 +30,13 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
FITSHDU *hdu = [[fits HDUs] objectAtIndex:0];
NSString *objectName = [[hdu header] stringValueForKey:@"OBJECT"];

static NSString *suiteName = @"com.onekiloparsec.qlfitsconfig.user-defaults-suite";
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:suiteName];
BOOL showSummaryInThumbnails = NO; // Defaults
if ([defaults stringForKey:@"showSummaryInThumbnails"]) {
showSummaryInThumbnails = [[defaults stringForKey:@"showSummaryInThumbnails"] isEqualToString:@"1"];
}

if ([hdu type] == FITSHDUTypeImage) {
FITSImage *img = [hdu image];
if (FITSIsEmptySize(img.size) && [fits countOfHDUs] > 1) {
Expand All @@ -42,8 +49,14 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
if (cgImage != NULL) {
CGContextRef context = QLThumbnailRequestCreateContext(thumbnail, maxSize, true, NULL);
CGRect renderRect = CGRectMake(0., 0., maxSize.width, maxSize.height);

CGContextDrawImage(context, renderRect, cgImage);
DrawObjectName(context, renderRect.size, objectName, YES, NO);
if (showSummaryInThumbnails) {
NSDictionary *summary = [fits shortSummary];
DrawHDUSummary(context, renderRect.size, summary[@"summary"], YES, NO);
}

QLThumbnailRequestFlushContext(thumbnail, context);
CFRelease(context);
}
Expand All @@ -58,9 +71,15 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum

CGSize canvasSize = CGSizeMake(img.size.nx/imgScale, img.size.nx/imgScale);
CGContextRef context = QLThumbnailRequestCreateContext(thumbnail, canvasSize, true, NULL);

DrawSpectrumCanvas(context, canvasSize);
DrawSpectrum(context, canvasSize, spectrum);
DrawObjectName(context, canvasSize, objectName, YES, YES);
if (showSummaryInThumbnails) {
NSDictionary *summary = [fits shortSummary];
DrawHDUSummary(context, canvasSize, summary[@"summary"], YES, YES);
}

QLThumbnailRequestFlushContext(thumbnail, context);
CFRelease(context);
}
Expand Down
26 changes: 11 additions & 15 deletions QLFits3/template.html
Expand Up @@ -95,29 +95,24 @@
color: #6aa1d3;
}

div.options_header {
height: 1.1em;
div.option {
height: 2.1em;
padding: 0.5em;
border-radius: 3px 3px 0 0;
background-color: #eee;
}

div.OptionTitle {
width: auto;
text-align: left;
div.option_title {
color: #000;
width: 95%;
height: 2.2em;
float: left;
}
span.OptionSubtitle {
width: auto;
text-align: left;
span.option_subtitle {
color: #666;
font-size: 0.9em;
clear: right;
}
input.OptionInput {
float:right;
clear:both;
input.option_input {
float: right;
}

div#header_info > div.header {
Expand Down Expand Up @@ -177,6 +172,7 @@
function saveConfig (a) {
a.href = "qlfitsconfig://save";
a.href += "/alwaysShowHeaders=" + (document.getElementById("alwaysShowHeadersInput").checked ? "1" : "0");
a.href += "/showSummaryInThumbnails=" + (document.getElementById("showSummaryInThumbnailsInput").checked ? "1" : "0");
return true;
}
</script>
Expand All @@ -200,10 +196,10 @@
<div class="header">
<div class="label">
QuickLook Options &nbsp;
<a href="#" onClick="saveConfig(this);return true;"><input id="save" type="button" value="Save"></a>
<a href="#" onClick="saveConfig(this);return true;" style="float:right;"><input id="save" type="button" value="Save"></a>
</div>
</div>
<div class="options_header">
<div id="options">
__QuickLookOptions__
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions QLFitsConfig/defaultOptions.plist
Expand Up @@ -4,5 +4,7 @@
<dict>
<key>alwaysShowHeaders</key>
<string>1</string>
<key>showSummaryInThumbnails</key>
<string>0</string>
</dict>
</plist>

0 comments on commit 0bc743f

Please sign in to comment.