// // KMSignatureAnnotationViewController.m // PDF Master // // Created by lxy on 2023/1/10. // #import "KMSignatureAnnotationViewController.h" #import @interface KMSignatureAnnotationViewController () @property (weak) IBOutlet NSBox *emptyBox; @property (weak) IBOutlet NSTextField *signatureLabel; @property (weak) IBOutlet NSTextField *emptyTipLabel; @property (weak) IBOutlet NSTextField *emptyMinTipLabel; @property (nonatomic,strong) NSMutableArray *signatures; @property (nonatomic,strong) NSMutableArray * selectSignatures; @property (strong) IBOutlet NSCollectionView *collectionView; @property (nonatomic,strong) KMSignature *clickSignature; @end @implementation KMSignatureAnnotationViewController - (void)viewDidLoad { [super viewDidLoad]; self.signatures = [[NSMutableArray alloc] init]; self.selectSignatures = [[NSMutableArray alloc] init]; [self reloadDataSignatures]; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self.collectionView registerClass:[KMSigntureViewItem class] forItemWithIdentifier:@"kmSigntureViewItem"]; self.collectionView.backgroundColors = @[[NSColor clearColor]]; self.collectionView.selectable = YES; self.collectionView.allowsMultipleSelection = YES; [self setup]; [self updateLanguage]; } - (void)setup { self.signatureLabel.font = [NSFont SFProTextSemiboldFont: 14.0]; self.signatureLabel.textColor = [NSColor km_initWithHex:@"#252629" alpha:1]; self.emptyTipLabel.font = [NSFont SFProTextRegularFont: 14.0]; self.emptyTipLabel.textColor = [NSColor km_initWithHex:@"#616469" alpha:1]; } - (void)updateLanguage { self.signatureLabel.stringValue = NSLocalizedString(@"Signature", nil); self.emptyTipLabel.stringValue = NSLocalizedString(@"No Signatures", nil); self.emptyMinTipLabel.stringValue = NSLocalizedString(@"Click the Add button in the upper right corner to create a new signature.", nil); NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineHeightMultiple = 1.32; paragraphStyle.alignment = NSTextAlignmentCenter; NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont SFProTextRegularFont: 12.0],NSFontAttributeName, [NSColor km_initWithHex:@"#94989C" alpha:1], NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil]; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.emptyMinTipLabel.stringValue]; [string addAttributes:dic range:NSMakeRange(0, string.length)]; NSRange range = [self.emptyMinTipLabel.stringValue rangeOfString:NSLocalizedString(@"Add button", comment: "")]; [string setAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [NSFont SFProTextSemiboldFont: 12.0],NSFontAttributeName, [NSColor km_initWithHex:@"#94989C" alpha:1], NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil] range:range]; self.emptyMinTipLabel.attributedStringValue = string; } - (void)tableViewMenu { NSMenu *menu = [[NSMenu alloc] init]; // NSMenuItem *item = [menu addItemWithTitle:NSLocalizedString(@"Export stamp", nil) action:nil target:self]; // NSMenu *tSubMenu = [[NSMenu alloc] init]; // NSMenuItem *tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PNG", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:0]; // tMenuItem.tag = 0; // tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"JPG", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:1]; // tMenuItem.tag = 1; // tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PDF", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:2]; // tMenuItem.tag = 2; // item.submenu = tSubMenu; // [menu addItem:[NSMenuItem separatorItem]]; // [menu addItemWithTitle:NSLocalizedString(@"Delete stamp", nil) action:@selector(deleteCustomizeStamp) target:self]; [menu addItemWithTitle:NSLocalizedString(@"Delete All stamp", nil) action:@selector(deleteAllCustomizeStamp) target:self]; self.collectionView.menu = menu; } - (void)tableCellCloseButton_Click:(NSButton *)button { NSInteger clickIndex = [self.signatures indexOfObject:self.clickSignature]; if(button.tag >= 0 && button.tag < self.signatures.count) { KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init]; [signatureManager loadAllSignatureList]; [signatureManager removeObjectWithIndex:button.tag]; [signatureManager saveSingaturesToFile]; [self reloadDataSignatures]; } if(clickIndex == button.tag) { [self.pdfView setAddStampObject:nil keepToolModel:NO]; } } - (IBAction)insertSignatureButton_Click:(id)sender { KMSignatureWindowController *signatureWindowController = [[KMSignatureWindowController alloc] initWithWindowNibName:@"KMSignatureWindowController"]; [signatureWindowController beginSheetModalFor:[NSApp mainWindow] completionHandler:^(KMSignature * _Nonnull signature) { if (signature) { [self reloadDataSignaturesIsSelectLast:YES]; } }]; } - (void)reloadDataSignatures { [self reloadDataSignaturesIsSelectLast:NO]; } - (void)reloadDataSignaturesIsSelectLast:(BOOL)selectLast { [self.signatures removeAllObjects]; KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init]; [signatureManager loadAllSignatureList]; [self.signatures addObjectsFromArray:signatureManager.signatureList]; if (selectLast && self.signatures.count > 0) { [self.selectSignatures removeAllObjects]; KMSignature *selectedSig = self.signatures.lastObject; selectedSig.isSelect = YES; [self.selectSignatures addObject:selectedSig]; CStampSignatureObject *sigObject = [[CStampSignatureObject alloc] initWithSignatureImage:selectedSig.pathsImage]; self.clickSignature = selectedSig; [self.pdfView setAddStampObject:sigObject keepToolModel:NO]; } if (_signatures.count > 0) { [self reloadCustomEmptyState:NO]; [self tableViewMenu]; } else { [self reloadCustomEmptyState:YES]; self.collectionView.menu = nil; } [self.collectionView reloadData]; } - (void)reloadCustomEmptyState:(BOOL)isEmpty { if (isEmpty) { self.emptyBox.hidden = NO; self.collectionView.hidden = YES; } else { self.emptyBox.hidden = YES; self.collectionView.hidden = NO; } } //MARK: menu Action - (void)exportCustomizeStamp:(NSMenuItem *)sender { if (self.collectionView.selectionIndexes.count != 1) { return; } NSInteger index = 0; for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) { index = indexPath.item; } NSInteger type = sender.tag; NSImage *image = self.signatures[index].pathsImage; if (type == 0) { NSData *data = image.TIFFRepresentation; NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data]; [imageRep setSize:image.size]; NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}]; NSSavePanel *savePanel = [NSSavePanel savePanel]; savePanel.allowedFileTypes = @[@"png"]; [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) { if (result) { if ([imageData writeToURL:savePanel.URL atomically:YES]) { [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path inFileViewerRootedAtPath:@""]; } } }]; } else if (type == 1) { NSData *data = image.TIFFRepresentation; NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data]; [imageRep setSize:image.size]; NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypeJPEG properties:@{}]; NSSavePanel *savePanel = [NSSavePanel savePanel]; savePanel.allowedFileTypes = @[@"jpg"]; [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) { if (result) { if ([imageData writeToURL:savePanel.URL atomically:YES]) { [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path inFileViewerRootedAtPath:@""]; } } }]; } else { CPDFDocument *pdf = [[CPDFDocument alloc] init]; NSString *signatureImagePath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"signatureImage.png"]; NSData *data = image.TIFFRepresentation; [data writeToURL:[NSURL fileURLWithPath:signatureImagePath] atomically:YES]; [pdf insertPage:image.size withImage:signatureImagePath atIndex:0]; NSSavePanel *savePanel = [NSSavePanel savePanel]; savePanel.allowedFileTypes = @[@"pdf"]; [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) { if (result) { if ([pdf writeToURL:savePanel.URL]) { [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path inFileViewerRootedAtPath:@""]; } } }]; } } - (void)deleteCustomizeStamp { if (self.collectionView.selectionIndexes.count != 1) { return; } NSInteger index = 0; for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) { index = indexPath.item; } NSInteger clickIndex = [self.signatures indexOfObject:self.clickSignature]; if(index < self.signatures.count) { KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init]; [signatureManager loadAllSignatureList]; [signatureManager removeObjectWithIndex:index]; [signatureManager saveSingaturesToFile]; [self reloadDataSignatures]; } if(clickIndex == index) { [self.pdfView setAddStampObject:nil keepToolModel:NO]; } } - (void)deleteAllCustomizeStamp { NSAlert *alert = [[NSAlert alloc] init]; alert.messageText = NSLocalizedString(@"All signatures will disappear from the list", nil); alert.informativeText = NSLocalizedString(@"All signatures will disappear from the list", nil); [alert addButtonWithTitle:NSLocalizedString(@"Delete", nil)]; [alert addButtonWithTitle:NSLocalizedString(@"Cancel", nil)]; if ([alert runModal] != NSAlertFirstButtonReturn) { /// 取消 return; } KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init]; [signatureManager loadAllSignatureList]; [signatureManager removeAllObject]; [signatureManager saveSingaturesToFile]; [self reloadDataSignatures]; [self.pdfView setAddStampObject:nil keepToolModel:NO]; } #pragma mark - NSCollectionViewDelegate - (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.signatures.count; } - (nonnull NSCollectionViewItem *)collectionView:(nonnull NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath { if (indexPath.item >= self.signatures.count) { return nil; } KMSigntureViewItem *item = [collectionView makeItemWithIdentifier:@"kmSigntureViewItem" forIndexPath:indexPath]; KMSignature *signature = [self.signatures objectAtIndex:indexPath.item]; item.iconImg.image = signature.pathsImage; item.deleteIconBtn.tag = indexPath.item; [item.deleteIconBtn setTarget:self]; [item.deleteIconBtn setAction:@selector(tableCellCloseButton_Click:)]; item.delegate = self; item.itemIndex = indexPath.item; item.isSelect = signature.isSelect; return item; } - (CGSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(228, 64); } - (NSEdgeInsets)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return NSEdgeInsetsMake(0, 16, 16, 16); } - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 16; } - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 16; } - (void)collectionView:(NSCollectionView *)collectionView didSelectItemsAtIndexPaths:(NSSet *)indexPaths { if (indexPaths.count == 1) { NSInteger index = 0; for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) { index = indexPath.item; } for (KMSignature *signature in self.selectSignatures) { signature.isSelect = NO; } [self.collectionView reloadData]; if (index < self.signatures.count) { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"KMContinuousAdditionStamp"]; KMSignature *signature = [self.signatures objectAtIndex:index]; signature.isSelect = YES; CStampSignatureObject *sigObject = [[CStampSignatureObject alloc] initWithSignatureImage:signature.pathsImage]; self.clickSignature = signature; [self.pdfView setAddStampObject:sigObject keepToolModel:NO]; [self.collectionView reloadItemsAtIndexPaths:self.collectionView.selectionIndexPaths]; [self.selectSignatures addObject:signature]; } } } #pragma mark - KMSigntureViewItemDelegate - (void)kmSigntureViewItem:(KMSigntureViewItem *)item didClickMenuWithItem:(NSMenuItem *)menuItem { [self.collectionView deselectAll:nil]; [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone]; [self exportCustomizeStamp:menuItem]; } - (void)kmSigntureViewItemDidClickDeleteMenuItem:(KMSigntureViewItem *)item { [self.collectionView deselectAll:nil]; [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone]; [self deleteCustomizeStamp]; } - (void)kmSigntureViewItemDidClickAllDeleteMenuItem:(KMSigntureViewItem *)item { [self.collectionView deselectAll:nil]; [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone]; [self deleteAllCustomizeStamp]; } @end