You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

181 lines
5.0 KiB

  1. /*
  2. Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
  3. This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
  4. which should be included with this package. The terms are also available at
  5. http://www.gnu.org/licenses/gpl-3.0.html
  6. */
  7. #import "ResultTable.h"
  8. #import "Dialogs.h"
  9. #import "Utils.h"
  10. #import "HSQuicklook.h"
  11. @interface HSTable (private)
  12. - (void)setPySelection;
  13. - (void)setViewSelection;
  14. @end
  15. @implementation ResultTable
  16. - (id)initWithPyRef:(PyObject *)aPyRef view:(NSTableView *)aTableView
  17. {
  18. self = [super initWithPyRef:aPyRef wrapperClass:[PyResultTable class] callbackClassName:@"ResultTableView" view:aTableView];
  19. return self;
  20. }
  21. - (PyResultTable *)model
  22. {
  23. return (PyResultTable *)model;
  24. }
  25. /* Private */
  26. - (void)updateQuicklookIfNeeded
  27. {
  28. if ([[QLPreviewPanel sharedPreviewPanel] dataSource] == self) {
  29. [[QLPreviewPanel sharedPreviewPanel] reloadData];
  30. }
  31. }
  32. - (void)setPySelection
  33. {
  34. [super setPySelection];
  35. [self updateQuicklookIfNeeded];
  36. }
  37. - (void)setViewSelection
  38. {
  39. [super setViewSelection];
  40. [self updateQuicklookIfNeeded];
  41. }
  42. /* Public */
  43. - (BOOL)powerMarkerMode
  44. {
  45. return [[self model] powerMarkerMode];
  46. }
  47. - (void)setPowerMarkerMode:(BOOL)aPowerMarkerMode
  48. {
  49. [[self model] setPowerMarkerMode:aPowerMarkerMode];
  50. }
  51. - (BOOL)deltaValuesMode
  52. {
  53. return [[self model] deltaValuesMode];
  54. }
  55. - (void)setDeltaValuesMode:(BOOL)aDeltaValuesMode
  56. {
  57. [[self model] setDeltaValuesMode:aDeltaValuesMode];
  58. }
  59. /* Datasource */
  60. - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger)row
  61. {
  62. NSString *identifier = [column identifier];
  63. if ([identifier isEqual:@"marked"]) {
  64. return [[self model] valueForColumn:@"marked" row:row];
  65. }
  66. return [[self model] valueForRow:row column:identifier];
  67. }
  68. - (void)tableView:(NSTableView *)aTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)column row:(NSInteger)row
  69. {
  70. NSString *identifier = [column identifier];
  71. if ([identifier isEqual:@"marked"]) {
  72. [[self model] setValue:object forColumn:identifier row:row];
  73. }
  74. else if ([identifier isEqual:@"name"]) {
  75. NSString *oldName = [[self model] valueForRow:row column:identifier];
  76. NSString *newName = object;
  77. if (![newName isEqual:oldName]) {
  78. BOOL renamed = [[self model] renameSelected:newName];
  79. if (!renamed) {
  80. [Dialogs showMessage:[NSString stringWithFormat:NSLocalizedString(@"The name '%@' already exists.", @""), newName]];
  81. }
  82. else {
  83. [[self view] setNeedsDisplay:YES];
  84. }
  85. }
  86. }
  87. }
  88. /* Delegate */
  89. - (void)tableView:(NSTableView *)aTableView didClickTableColumn:(NSTableColumn *)tableColumn
  90. {
  91. if ([[[self view] sortDescriptors] count] < 1)
  92. return;
  93. NSSortDescriptor *sd = [[[self view] sortDescriptors] objectAtIndex:0];
  94. [[self model] sortBy:[sd key] ascending:[sd ascending]];
  95. }
  96. - (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)column row:(NSInteger)row
  97. {
  98. BOOL isSelected = [[self view] isRowSelected:row];
  99. BOOL isMarkable = n2b([[self model] valueForColumn:@"markable" row:row]);
  100. if ([[column identifier] isEqual:@"marked"]) {
  101. [cell setEnabled:isMarkable];
  102. // Low-tech solution, for indentation, but it works...
  103. NSCellImagePosition pos = isMarkable ? NSImageRight : NSImageLeft;
  104. [cell setImagePosition:pos];
  105. }
  106. if ([cell isKindOfClass:[NSTextFieldCell class]]) {
  107. NSColor *color = [NSColor textColor];
  108. if (isSelected) {
  109. color = [NSColor selectedTextColor];
  110. }
  111. else if (isMarkable) {
  112. if ([[self model] isDeltaAtRow:row column:[column identifier]]) {
  113. color = [NSColor orangeColor];
  114. }
  115. }
  116. else {
  117. color = [NSColor blueColor];
  118. }
  119. [(NSTextFieldCell *)cell setTextColor:color];
  120. }
  121. }
  122. - (BOOL)tableViewHadDeletePressed:(NSTableView *)tableView
  123. {
  124. [[self model] removeSelected];
  125. return YES;
  126. }
  127. - (BOOL)tableViewHadSpacePressed:(NSTableView *)tableView
  128. {
  129. [[self model] markSelected];
  130. return YES;
  131. }
  132. /* Quicklook */
  133. - (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel
  134. {
  135. return [[[self model] selectedRows] count];
  136. }
  137. - (id <QLPreviewItem>)previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index
  138. {
  139. NSArray *selectedRows = [[self model] selectedRows];
  140. NSInteger absIndex = n2i([selectedRows objectAtIndex:index]);
  141. NSString *path = [[self model] pathAtIndex:absIndex];
  142. return [[HSQLPreviewItem alloc] initWithUrl:[NSURL fileURLWithPath:path] title:path];
  143. }
  144. - (BOOL)previewPanel:(QLPreviewPanel *)panel handleEvent:(NSEvent *)event
  145. {
  146. // redirect all key down events to the table view
  147. if ([event type] == NSEventTypeKeyDown) {
  148. [[self view] keyDown:event];
  149. return YES;
  150. }
  151. return NO;
  152. }
  153. /* Python --> Cocoa */
  154. - (void)invalidateMarkings
  155. {
  156. [[self view] setNeedsDisplay:YES];
  157. }
  158. @end