Revision: 13638
          http://sourceforge.net/p/skim-app/code/13638
Author:   hofman
Date:     2023-09-04 14:26:19 +0000 (Mon, 04 Sep 2023)
Log Message:
-----------
Separate method to add notes from dictionaries for open/revert and for 
read/convert. Include pdf document setter code in method for open/revert.

Modified Paths:
--------------
    trunk/SKMainDocument.m
    trunk/SKMainWindowController.h
    trunk/SKMainWindowController.m

Modified: trunk/SKMainDocument.m
===================================================================
--- trunk/SKMainDocument.m      2023-09-03 22:24:30 UTC (rev 13637)
+++ trunk/SKMainDocument.m      2023-09-04 14:26:19 UTC (rev 13638)
@@ -198,7 +198,7 @@
     
     [self tryToUnlockDocument:pdfDoc];
     
-    [[self mainWindowController] addAnnotationsFromDictionaries:[tmpData 
noteDicts] removeAnnotations:[self notes] setDocument:pdfDoc];
+    [[self mainWindowController] setPdfDocument:pdfDoc 
addAnnotationsFromDictionaries:[tmpData noteDicts]];
     
     if ([tmpData presentationOptions])
         [[self mainWindowController] setPresentationOptions:[tmpData 
presentationOptions]];
@@ -1079,7 +1079,7 @@
     }
     
     if (array) {
-        [[self mainWindowController] addAnnotationsFromDictionaries:array 
removeAnnotations:replace ? [self notes] : nil setDocument:nil];
+        [[self mainWindowController] addAnnotationsFromDictionaries:array 
removeAnnotations:replace ? [self notes] : nil];
         [[self undoManager] setActionName:replace ? 
NSLocalizedString(@"Replace Notes", @"Undo action name") : 
NSLocalizedString(@"Add Notes", @"Undo action name")];
     } else
         NSBeep();
@@ -1197,7 +1197,7 @@
 
             dispatch_async(dispatch_get_main_queue(), ^{
                 
-                [[self mainWindowController] 
addAnnotationsFromDictionaries:noteDicts removeAnnotations:annotations 
setDocument:nil];
+                [[self mainWindowController] 
addAnnotationsFromDictionaries:noteDicts removeAnnotations:annotations];
                 
                 [self setPDFData:data pageOffsets:offsets];
                 

Modified: trunk/SKMainWindowController.h
===================================================================
--- trunk/SKMainWindowController.h      2023-09-03 22:24:30 UTC (rev 13637)
+++ trunk/SKMainWindowController.h      2023-09-04 14:26:19 UTC (rev 13638)
@@ -228,7 +228,7 @@
 - (NSWindowController *)windowControllerForNote:(PDFAnnotation *)annotation;
 
 @property (nonatomic, readonly) SKPDFView *pdfView;
-@property (nonatomic, retain) PDFDocument *pdfDocument;
+@property (nonatomic, readonly) PDFDocument *pdfDocument;
 @property (nonatomic, readonly) PDFView *secondaryPdfView;
 
 @property (nonatomic, readonly) PDFDocument *placeholderPdfDocument;
@@ -335,7 +335,8 @@
 - (void)updateSnapshotsIfNeeded;
 - (void)updateSnapshot:(NSTimer *)timer;
 
-- (void)addAnnotationsFromDictionaries:(NSArray *)noteDicts 
removeAnnotations:(NSArray *)notesToRemove setDocument:(PDFDocument 
*)pdfDocument;
+- (void)setPdfDocument:(PDFDocument *)pdfDocument 
addAnnotationsFromDictionaries:(NSArray *)noteDicts;
+- (void)addAnnotationsFromDictionaries:(NSArray *)noteDicts 
removeAnnotations:(NSArray *)notesToRemove;
 
 - (void)applySetup:(NSDictionary *)setup;
 - (NSDictionary *)currentSetup;

Modified: trunk/SKMainWindowController.m
===================================================================
--- trunk/SKMainWindowController.m      2023-09-03 22:24:30 UTC (rev 13637)
+++ trunk/SKMainWindowController.m      2023-09-04 14:26:19 UTC (rev 13638)
@@ -1025,71 +1025,20 @@
     return properties;
 }
 
-/*
- open: notesToRemove == @[], pdfDocument != nil
- revert: notesToRemove == self.notes, pdfDocument != nil
- convert: notesToRemove == @[not isSkimNote], pdfDocument == nil
- read: notesToRemove == nil or self.notes, pdfDocument == nil
- */
-- (void)addAnnotationsFromDictionaries:(NSArray *)noteDicts 
removeAnnotations:(NSArray *)notesToRemove setDocument:(PDFDocument 
*)pdfDocument {
-    PDFAnnotation *annotation;
-    PDFDocument *pdfDoc = pdfDocument ?: [pdfView document];
+- (void)addAnnotationsFromDictionaries:(NSArray *)noteDicts 
toDocument:(PDFDocument *)pdfDoc pageIndexes:(NSMutableIndexSet *)pageIndexes 
autoUpdate:(BOOL)autoUpdate {
     NSMutableArray *notesToAdd = [NSMutableArray array];
-    NSMutableArray *widgetProperties = [NSMutableArray array];
-    NSMutableIndexSet *pageIndexes = pdfDocument ? nil : [NSMutableIndexSet 
indexSet];
-    BOOL isConvert = [notesToRemove count] > 0 && [[notesToRemove firstObject] 
isSkimNote] == NO;
     
-    SKDESTROY(placeholderPdfDocument);
-    if ([pdfDoc allowsNotes] == NO && [noteDicts count] > 0) {
-        // there should not be any notesToRemove at this point
-        NSUInteger i, pageCount = MIN([pdfDoc pageCount], [[noteDicts 
valueForKeyPath:@"@max.pageIndex"] unsignedIntegerValue] + 1);
-        pdfDoc = placeholderPdfDocument = [[SKPDFDocument alloc] init];
-        [placeholderPdfDocument setContainingDocument:[self document]];
-        for (i = 0; i < pageCount; i++) {
-            PDFPage *page = [[SKPDFPage alloc] init];
-            [placeholderPdfDocument insertPage:page atIndex:i];
-            [page release];
-        }
-    }
-    
     // disable automatic add/remove from the notification handlers
     // we want to do this in bulk as binding can be very slow and there are 
potentially many notes
     mwcFlags.addOrRemoveNotesInBulk = 1;
     
-    if ([notesToRemove count]) {
-        // notesToRemove is either all notes, no notes, or non Skim notes
-        BOOL removeAllNotes = [[notesToRemove firstObject] isSkimNote];
-        if (removeAllNotes) {
-            [pdfView removePDFToolTipRects];
-            // remove the current annotations
-            [pdfView setCurrentAnnotation:nil];
-        }
-        if (pdfDocument == nil) {
-            for (annotation in [[notesToRemove copy] autorelease]) {
-                [pageIndexes addIndex:[annotation pageIndex]];
-                PDFAnnotation *popup = [annotation popup];
-                if (popup)
-                    [pdfDoc removeAnnotation:popup];
-                [pdfDoc removeAnnotation:annotation];
-            }
-        }
-        if (removeAllNotes)
-            [self removeAllObjectsFromNotes];
-    }
-    if (notesToRemove && isConvert == NO && pdfDocument == nil) {
-        for (PDFAnnotation *widget in widgets) {
-            id origValue = [widgetValues objectForKey:widget];
-            if ([([widget objectValue] ?: @"") isEqual:(origValue ?: @"")] == 
NO)
-                [widget setObjectValue:origValue];
-        }
-    }
-    
     // create new annotations from the dictionary and add them to their page 
and to the document
     for (NSDictionary *dict in noteDicts) {
+        if ([[dict objectForKey:SKNPDFAnnotationTypeKey] 
isEqualToString:SKNWidgetString])
+            continue;
         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-        if ([[dict objectForKey:SKNPDFAnnotationTypeKey] 
isEqualToString:SKNWidgetString]) {
-            [widgetProperties addObject:dict];
-        } else if ((annotation = [PDFAnnotation 
newSkimNoteWithProperties:dict])) {
+        PDFAnnotation *annotation = [PDFAnnotation 
newSkimNoteWithProperties:dict];
+        if (annotation) {
             // this is only to make sure markup annotations generate the 
lineRects, for thread safety
             [annotation boundsOrder];
             NSUInteger pageIndex = [[dict 
objectForKey:SKNPDFAnnotationPageIndexKey] unsignedIntegerValue];
@@ -1099,7 +1048,7 @@
                 pageIndex = [pdfDoc pageCount] - 1;
             [pageIndexes addIndex:pageIndex];
             [pdfDoc addAnnotation:annotation toPage:[pdfDoc 
pageAtIndex:pageIndex]];
-            if (isConvert && [[annotation contents] length] == 0)
+            if (autoUpdate && [[annotation contents] length] == 0)
                 [annotation autoUpdateString];
             [notesToAdd addObject:annotation];
             [annotation release];
@@ -1109,159 +1058,212 @@
     
     mwcFlags.addOrRemoveNotesInBulk = 0;
     
-    if (pdfDocument)
-        [self setPdfDocument:pdfDocument];
-    
     if ([notesToAdd count] > 0)
         [self insertNotes:notesToAdd atIndexes:[NSIndexSet 
indexSetWithIndexesInRange:NSMakeRange([notes count], [notesToAdd count])]];
+}
+
+- (void)addAnnotationsFromDictionaries:(NSArray *)noteDicts 
removeAnnotations:(NSArray *)notesToRemove {
+    BOOL isConvert = [notesToRemove count] > 0 && [[notesToRemove firstObject] 
isSkimNote] == NO;
+    PDFDocument *pdfDoc = [pdfView document];
+    NSMutableIndexSet *pageIndexes = [NSMutableIndexSet indexSet];
     
-    if ([[pdfView document] isLocked]) {
-        [placeholderWidgetProperties release];
-        placeholderWidgetProperties = [widgetProperties count] ? 
[widgetProperties copy] : nil;
+    if ([notesToRemove count]) {
+        // notesToRemove is either all notes, no notes, or non Skim notes
+        if (isConvert == NO) {
+            [pdfView removePDFToolTipRects];
+            // remove the current annotations
+            [pdfView setCurrentAnnotation:nil];
+        }
+        PDFAnnotation *annotation;
+        mwcFlags.addOrRemoveNotesInBulk = 1;
+        for (annotation in [[notesToRemove copy] autorelease]) {
+            [pageIndexes addIndex:[annotation pageIndex]];
+            PDFAnnotation *popup = [annotation popup];
+            if (popup)
+                [pdfDoc removeAnnotation:popup];
+            [pdfDoc removeAnnotation:annotation];
+        }
+        mwcFlags.addOrRemoveNotesInBulk = 0;
+        if (isConvert == NO)
+            [self removeAllObjectsFromNotes];
+    }
+    if (notesToRemove && isConvert == NO) {
+        for (PDFAnnotation *widget in widgets) {
+            id origValue = [widgetValues objectForKey:widget];
+            if ([([widget objectValue] ?: @"") isEqual:(origValue ?: @"")] == 
NO)
+                [widget setObjectValue:origValue];
+        }
+    }
+    
+    [self addAnnotationsFromDictionaries:noteDicts toDocument:pdfDoc 
pageIndexes:pageIndexes autoUpdate:isConvert];
+    
+    if (isConvert) {
+        NSMapTable *values = [NSMapTable strongToStrongObjectsMapTable];
+        for (PDFAnnotation *widget in widgets) {
+            id value = [widget objectValue];
+            if (value)
+                [values setObject:value forKey:widget];
+        }
+        if ([values count])
+            [self setWidgetValues:values];
     } else {
-        if (widgets == nil)
-            [self makeWidgets];
+        NSArray *widgetProperties = [noteDicts 
filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"type = 
\"Widget\""]];
         if ([widgetProperties count])
             [self changeWidgetsFromDictionaries:widgetProperties];
-        if (isConvert) {
-            NSMapTable *values = [NSMapTable strongToStrongObjectsMapTable];
-            for (PDFAnnotation *widget in widgets) {
-                id value = [widget objectValue];
-                if (value)
-                    [values setObject:value forKey:widget];
-            }
-            if ([values count])
-                [self setWidgetValues:values];
-        }
     }
     
     // make sure we clear the undo handling
     [self observeUndoManagerCheckpoint:nil];
     [rightSideController.noteOutlineView reloadData];
-    if ([pageIndexes count])
-        [self updateThumbnailsAtPageIndexes:pageIndexes];
+    [self updateThumbnailsAtPageIndexes:pageIndexes];
     [pdfView resetPDFToolTipRects];
 }
 
-#pragma mark Accessors
-
-- (PDFDocument *)pdfDocument{
-    return [pdfView document];
-}
-
-- (void)setPdfDocument:(PDFDocument *)document{
-
-    if ([pdfView document] != document) {
+- (void)setPdfDocument:(PDFDocument *)pdfDocument 
addAnnotationsFromDictionaries:(NSArray *)noteDicts {
+    PDFDocument *pdfDoc = pdfDocument;
+    NSArray *widgetProperties = [noteDicts 
filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"type = 
\"Widget\""]];
+    PDFDocument *oldPdfDoc = [pdfView document];
+    NSUInteger pageIndex = NSNotFound, secondaryPageIndex = NSNotFound;
+    NSPoint point = NSZeroPoint, secondaryPoint = NSZeroPoint;
+    BOOL rotated = NO, secondaryRotated = NO;
+    NSArray *snapshotDicts = nil;
+    NSDictionary *openState = nil;
+    
+    SKDESTROY(placeholderPdfDocument);
+    if ([pdfDoc allowsNotes] == NO && [noteDicts count] > 0) {
+        // there should not be any notesToRemove at this point
+        NSUInteger i, pageCount = MIN([pdfDoc pageCount], [[noteDicts 
valueForKeyPath:@"@max.pageIndex"] unsignedIntegerValue] + 1);
+        pdfDoc = placeholderPdfDocument = [[SKPDFDocument alloc] init];
+        [placeholderPdfDocument setContainingDocument:[self document]];
+        for (i = 0; i < pageCount; i++) {
+            PDFPage *page = [[SKPDFPage alloc] init];
+            [placeholderPdfDocument insertPage:page atIndex:i];
+            [page release];
+        }
+    }
+    
+    if (oldPdfDoc) {
+        // this is a revert
+        // need to clean up data and actions, and remember settings to restore
+        pageIndex = [pdfView currentPageIndexAndPoint:&point rotated:&rotated];
+        if (secondaryPdfView)
+            secondaryPageIndex = [secondaryPdfView 
currentPageIndexAndPoint:&secondaryPoint rotated:&secondaryRotated];
+        openState = [self expansionStateForOutline:[[pdfView document] 
outlineRoot]];
         
-        NSUInteger pageIndex = NSNotFound, secondaryPageIndex = NSNotFound;
-        NSPoint point = NSZeroPoint, secondaryPoint = NSZeroPoint;
-        BOOL rotated = NO, secondaryRotated = NO;
-        NSArray *snapshotDicts = nil;
-        NSDictionary *openState = nil;
-        PDFDocument *oldPdfDoc = [pdfView document];
+        [oldPdfDoc cancelFindString];
         
-        if (oldPdfDoc) {
-            pageIndex = [pdfView currentPageIndexAndPoint:&point 
rotated:&rotated];
-            if (secondaryPdfView)
-                secondaryPageIndex = [secondaryPdfView 
currentPageIndexAndPoint:&secondaryPoint rotated:&secondaryRotated];
-            openState = [self expansionStateForOutline:[[pdfView document] 
outlineRoot]];
-            
-            [oldPdfDoc cancelFindString];
-            
-            // make sure these will not be activated, or they can lead to a 
crash
-            [pdfView removePDFToolTipRects];
-            [pdfView setCurrentAnnotation:nil];
-            
-            // these will be invalid. If needed, the document will restore them
-            [self setSearchResults:nil];
-            [self setGroupedSearchResults:nil];
-            [self removeAllObjectsFromNotes];
-            [self setThumbnails:nil];
-            [self clearWidgets];
-            
-            // remmeber snapshots and close them, without animation
-            snapshotDicts = [snapshots valueForKey:SKSnapshotCurrentSetupKey];
-            [snapshots setValue:nil forKey:@"delegate"];
-            [snapshots makeObjectsPerformSelector:@selector(close)];
-            [self removeAllObjectsFromSnapshots];
-            [rightSideController.snapshotTableView reloadData];
-            
-            [lastViewedPages setCount:0];
-            
-            [self unregisterForDocumentNotifications];
-            
-            [oldPdfDoc setDelegate:nil];
-            
-            [[oldPdfDoc outlineRoot] clearDocument];
-            
-            [oldPdfDoc setContainingDocument:nil];
-        }
+        // make sure these will not be activated, or they can lead to a crash
+        [pdfView removePDFToolTipRects];
+        [pdfView setCurrentAnnotation:nil];
         
-        if ([document isLocked] == NO) {
-            NSArray *cropBoxes = [savedNormalSetup objectForKey:CROPBOXES_KEY];
-            if ([cropBoxes count])
-                [self applyChangedCropBoxes:cropBoxes inDocument:document];
-        }
+        // these will be invalid. If needed, they will be restored for the new 
document
+        [self setSearchResults:nil];
+        [self setGroupedSearchResults:nil];
+        [self removeAllObjectsFromNotes];
+        [self setThumbnails:nil];
+        [self clearWidgets];
         
-        [pdfView setDocument:document];
-        [[pdfView document] setDelegate:self];
+        // remember snapshots and close them, without animation
+        snapshotDicts = [snapshots valueForKey:SKSnapshotCurrentSetupKey];
+        [snapshots setValue:nil forKey:@"delegate"];
+        [snapshots makeObjectsPerformSelector:@selector(close)];
+        [self removeAllObjectsFromSnapshots];
+        [rightSideController.snapshotTableView reloadData];
         
-        [secondaryPdfView setDocument:document];
+        [lastViewedPages setCount:0];
         
-        [document setContainingDocument:[self document]];
-
-        [self registerForDocumentNotifications];
+        [self unregisterForDocumentNotifications];
         
-        [self updatePageLabelsAndOutlineForExpansionState:openState];
-        [self updateNoteSelection];
+        [oldPdfDoc setDelegate:nil];
         
-        if ([snapshotDicts count]) {
-            if ([document isLocked] && ([self interactionMode] == SKNormalMode 
|| [self interactionMode] == SKFullScreenMode))
-                [savedNormalSetup setObject:snapshotDicts 
forKey:SNAPSHOTS_KEY];
-            else
-                [self showSnapshotsWithSetups:snapshotDicts];
-        }
+        [[oldPdfDoc outlineRoot] clearDocument];
         
-        if ([document pageCount] && (pageIndex != NSNotFound || 
secondaryPageIndex != NSNotFound)) {
-            if (pageIndex != NSNotFound) {
-                if (pageIndex >= [document pageCount])
-                    pageIndex = [document pageCount] - 1;
-                if ([document isLocked] && ([self interactionMode] == 
SKNormalMode || [self interactionMode] == SKFullScreenMode)) {
-                    [savedNormalSetup setObject:[NSNumber 
numberWithUnsignedInteger:pageIndex] forKey:PAGEINDEX_KEY];
-                } else {
-                    if (rotated)
-                        [pdfView goToCurrentPage:[document 
pageAtIndex:pageIndex]];
-                    else
-                        [pdfView goToPageAtIndex:pageIndex point:point];
-                }
-            }
-            if (secondaryPageIndex != NSNotFound) {
-                if (secondaryPageIndex >= [document pageCount])
-                    secondaryPageIndex = [document pageCount] - 1;
-                if (secondaryRotated)
-                    [secondaryPdfView goToCurrentPage:[document 
pageAtIndex:secondaryPageIndex]];
+        [oldPdfDoc setContainingDocument:nil];
+    }
+    
+    if ([pdfDocument isLocked] == NO) {
+        NSArray *cropBoxes = [savedNormalSetup objectForKey:CROPBOXES_KEY];
+        if ([cropBoxes count])
+            [self applyChangedCropBoxes:cropBoxes inDocument:pdfDocument];
+    }
+    
+    [self addAnnotationsFromDictionaries:noteDicts toDocument:pdfDoc 
pageIndexes:nil autoUpdate:NO];
+    
+    [pdfView setDocument:pdfDocument];
+    [pdfDocument setDelegate:self];
+    
+    [secondaryPdfView setDocument:pdfDocument];
+    
+    [pdfDocument setContainingDocument:[self document]];
+
+    [self registerForDocumentNotifications];
+    
+    if ([[pdfView document] isLocked]) {
+        placeholderWidgetProperties = [widgetProperties count] ? 
[widgetProperties copy] : nil;
+    } else {
+        [self makeWidgets];
+        if ([widgetProperties count])
+            [self changeWidgetsFromDictionaries:widgetProperties];
+    }
+    
+    [self updatePageLabelsAndOutlineForExpansionState:openState];
+    [self updateNoteSelection];
+    
+    if ([snapshotDicts count]) {
+        if ([pdfDocument isLocked] && ([self interactionMode] == SKNormalMode 
|| [self interactionMode] == SKFullScreenMode))
+            [savedNormalSetup setObject:snapshotDicts forKey:SNAPSHOTS_KEY];
+        else
+            [self showSnapshotsWithSetups:snapshotDicts];
+    }
+    
+    if ([pdfDocument pageCount] && (pageIndex != NSNotFound || 
secondaryPageIndex != NSNotFound)) {
+        if (pageIndex != NSNotFound) {
+            if (pageIndex >= [pdfDocument pageCount])
+                pageIndex = [pdfDocument pageCount] - 1;
+            if ([pdfDocument isLocked] && ([self interactionMode] == 
SKNormalMode || [self interactionMode] == SKFullScreenMode)) {
+                [savedNormalSetup setObject:[NSNumber 
numberWithUnsignedInteger:pageIndex] forKey:PAGEINDEX_KEY];
+            } else {
+                if (rotated)
+                    [pdfView goToCurrentPage:[pdfDocument 
pageAtIndex:pageIndex]];
                 else
-                    [secondaryPdfView goToPageAtIndex:secondaryPageIndex 
point:secondaryPoint];
+                    [pdfView goToPageAtIndex:pageIndex point:point];
             }
-            [pdfView resetHistory];
         }
-        
-        if (markedPageIndex >= [document pageCount]) {
-            markedPageIndex = NSNotFound;
-            beforeMarkedPageIndex = NSNotFound;
-        } else if (beforeMarkedPageIndex >= [document pageCount]) {
-            beforeMarkedPageIndex = NSNotFound;
+        if (secondaryPageIndex != NSNotFound) {
+            if (secondaryPageIndex >= [pdfDocument pageCount])
+                secondaryPageIndex = [pdfDocument pageCount] - 1;
+            if (secondaryRotated)
+                [secondaryPdfView goToCurrentPage:[pdfDocument 
pageAtIndex:secondaryPageIndex]];
+            else
+                [secondaryPdfView goToPageAtIndex:secondaryPageIndex 
point:secondaryPoint];
         }
-        
-        // the number of pages may have changed
-        [toolbarController handleChangedHistoryNotification:nil];
-        [toolbarController handlePageChangedNotification:nil];
-        [self handlePageChangedNotification:nil];
-        [self updateLeftStatus];
-        [self updateRightStatus];
+        [pdfView resetHistory];
     }
+    
+    if (markedPageIndex >= [pdfDocument pageCount])
+        markedPageIndex = beforeMarkedPageIndex = NSNotFound;
+    else if (beforeMarkedPageIndex >= [pdfDocument pageCount])
+        beforeMarkedPageIndex = NSNotFound;
+    
+    // the number of pages may have changed
+    [toolbarController handleChangedHistoryNotification:nil];
+    [toolbarController handlePageChangedNotification:nil];
+    [self handlePageChangedNotification:nil];
+    [self updateLeftStatus];
+    [self updateRightStatus];
+    
+    // make sure we clear the undo handling
+    [self observeUndoManagerCheckpoint:nil];
+    [rightSideController.noteOutlineView reloadData];
+    [pdfView resetPDFToolTipRects];
 }
 
+#pragma mark Accessors
+
+- (PDFDocument *)pdfDocument{
+    return [pdfView document];
+}
+
 - (void)updatePageLabel {
     NSString *label = [[pdfView currentPage] displayLabel];
     if ([label isEqualToString:pageLabel] == NO) {

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to