KMSignatureAnnotationViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // KMSignatureAnnotationViewController.m
  3. // PDF Master
  4. //
  5. // Created by lxy on 2023/1/10.
  6. //
  7. #import "KMSignatureAnnotationViewController.h"
  8. #import <PDF_Master-Swift.h>
  9. @interface KMSignatureAnnotationViewController ()<NSCollectionViewDelegate,
  10. NSCollectionViewDataSource,
  11. KMSigntureViewItemDelegate>
  12. @property (weak) IBOutlet NSBox *emptyBox;
  13. @property (weak) IBOutlet NSTextField *signatureLabel;
  14. @property (weak) IBOutlet NSTextField *emptyTipLabel;
  15. @property (weak) IBOutlet NSTextField *emptyMinTipLabel;
  16. @property (nonatomic,strong) NSMutableArray <KMSignature *>*signatures;
  17. @property (nonatomic,strong) NSMutableArray <KMSignature *>* selectSignatures;
  18. @property (strong) IBOutlet NSCollectionView *collectionView;
  19. @property (nonatomic,strong) KMSignature *clickSignature;
  20. @end
  21. @implementation KMSignatureAnnotationViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. self.signatures = [[NSMutableArray alloc] init];
  25. self.selectSignatures = [[NSMutableArray alloc] init];
  26. [self reloadDataSignatures];
  27. self.collectionView.delegate = self;
  28. self.collectionView.dataSource = self;
  29. [self.collectionView registerClass:[KMSigntureViewItem class] forItemWithIdentifier:@"kmSigntureViewItem"];
  30. self.collectionView.backgroundColors = @[[NSColor clearColor]];
  31. self.collectionView.selectable = YES;
  32. self.collectionView.allowsMultipleSelection = YES;
  33. [self setup];
  34. [self updateLanguage];
  35. }
  36. - (void)setup {
  37. self.signatureLabel.font = [NSFont SFProTextSemiboldFont: 14.0];
  38. self.signatureLabel.textColor = [NSColor km_initWithHex:@"#252629" alpha:1];
  39. self.emptyTipLabel.font = [NSFont SFProTextRegularFont: 14.0];
  40. self.emptyTipLabel.textColor = [NSColor km_initWithHex:@"#616469" alpha:1];
  41. }
  42. - (void)updateLanguage {
  43. self.signatureLabel.stringValue = NSLocalizedString(@"Signature", nil);
  44. self.emptyTipLabel.stringValue = NSLocalizedString(@"No Signatures", nil);
  45. self.emptyMinTipLabel.stringValue = NSLocalizedString(@"Click the Add button in the upper right corner to create a new signature.", nil);
  46. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  47. paragraphStyle.lineHeightMultiple = 1.32;
  48. paragraphStyle.alignment = NSTextAlignmentCenter;
  49. NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont SFProTextRegularFont: 12.0],NSFontAttributeName,
  50. [NSColor km_initWithHex:@"#94989C" alpha:1],
  51. NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil];
  52. NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.emptyMinTipLabel.stringValue];
  53. [string addAttributes:dic range:NSMakeRange(0, string.length)];
  54. NSRange range = [self.emptyMinTipLabel.stringValue rangeOfString:NSLocalizedString(@"Add button", comment: "")];
  55. [string setAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [NSFont SFProTextSemiboldFont: 12.0],NSFontAttributeName,
  56. [NSColor km_initWithHex:@"#94989C" alpha:1],
  57. NSForegroundColorAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil] range:range];
  58. self.emptyMinTipLabel.attributedStringValue = string;
  59. }
  60. - (void)tableViewMenu {
  61. NSMenu *menu = [[NSMenu alloc] init];
  62. // NSMenuItem *item = [menu addItemWithTitle:NSLocalizedString(@"Export stamp", nil) action:nil target:self];
  63. // NSMenu *tSubMenu = [[NSMenu alloc] init];
  64. // NSMenuItem *tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PNG", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:0];
  65. // tMenuItem.tag = 0;
  66. // tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"JPG", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:1];
  67. // tMenuItem.tag = 1;
  68. // tMenuItem = [tSubMenu insertItemWithTitle:NSLocalizedString(@"PDF", @"Menu item title") action:@selector(exportCustomizeStamp:) target:self atIndex:2];
  69. // tMenuItem.tag = 2;
  70. // item.submenu = tSubMenu;
  71. // [menu addItem:[NSMenuItem separatorItem]];
  72. // [menu addItemWithTitle:NSLocalizedString(@"Delete stamp", nil) action:@selector(deleteCustomizeStamp) target:self];
  73. [menu addItemWithTitle:NSLocalizedString(@"Delete All stamp", nil) action:@selector(deleteAllCustomizeStamp) target:self];
  74. self.collectionView.menu = menu;
  75. }
  76. - (void)tableCellCloseButton_Click:(NSButton *)button {
  77. NSInteger clickIndex = [self.signatures indexOfObject:self.clickSignature];
  78. if(button.tag >= 0 && button.tag < self.signatures.count) {
  79. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  80. [signatureManager loadAllSignatureList];
  81. [signatureManager removeObjectWithIndex:button.tag];
  82. [signatureManager saveSingaturesToFile];
  83. [self reloadDataSignatures];
  84. }
  85. if(clickIndex == button.tag) {
  86. [self.pdfView setAddStampObject:nil keepToolModel:NO];
  87. }
  88. }
  89. - (IBAction)insertSignatureButton_Click:(id)sender {
  90. KMSignatureWindowController *signatureWindowController = [[KMSignatureWindowController alloc] initWithWindowNibName:@"KMSignatureWindowController"];
  91. [signatureWindowController beginSheetModalFor:[NSApp mainWindow] completionHandler:^(KMSignature * _Nonnull signature) {
  92. if (signature) {
  93. [self reloadDataSignaturesIsSelectLast:YES];
  94. }
  95. }];
  96. }
  97. - (void)reloadDataSignatures {
  98. [self reloadDataSignaturesIsSelectLast:NO];
  99. }
  100. - (void)reloadDataSignaturesIsSelectLast:(BOOL)selectLast {
  101. [self.signatures removeAllObjects];
  102. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  103. [signatureManager loadAllSignatureList];
  104. [self.signatures addObjectsFromArray:signatureManager.signatureList];
  105. if (selectLast && self.signatures.count > 0) {
  106. [self.selectSignatures removeAllObjects];
  107. KMSignature *selectedSig = self.signatures.lastObject;
  108. selectedSig.isSelect = YES;
  109. [self.selectSignatures addObject:selectedSig];
  110. CStampSignatureObject *sigObject = [[CStampSignatureObject alloc] initWithSignatureImage:selectedSig.pathsImage];
  111. self.clickSignature = selectedSig;
  112. [self.pdfView setAddStampObject:sigObject keepToolModel:NO];
  113. }
  114. if (_signatures.count > 0) {
  115. [self reloadCustomEmptyState:NO];
  116. [self tableViewMenu];
  117. } else {
  118. [self reloadCustomEmptyState:YES];
  119. self.collectionView.menu = nil;
  120. }
  121. [self.collectionView reloadData];
  122. }
  123. - (void)reloadCustomEmptyState:(BOOL)isEmpty {
  124. if (isEmpty) {
  125. self.emptyBox.hidden = NO;
  126. self.collectionView.hidden = YES;
  127. } else {
  128. self.emptyBox.hidden = YES;
  129. self.collectionView.hidden = NO;
  130. }
  131. }
  132. //MARK: menu Action
  133. - (void)exportCustomizeStamp:(NSMenuItem *)sender {
  134. if (self.collectionView.selectionIndexes.count != 1) {
  135. return;
  136. }
  137. NSInteger index = 0;
  138. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  139. index = indexPath.item;
  140. }
  141. NSInteger type = sender.tag;
  142. NSImage *image = self.signatures[index].pathsImage;
  143. if (type == 0) {
  144. NSData *data = image.TIFFRepresentation;
  145. NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
  146. [imageRep setSize:image.size];
  147. NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
  148. NSSavePanel *savePanel = [NSSavePanel savePanel];
  149. savePanel.allowedFileTypes = @[@"png"];
  150. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  151. if (result) {
  152. if ([imageData writeToURL:savePanel.URL atomically:YES]) {
  153. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  154. inFileViewerRootedAtPath:@""];
  155. }
  156. }
  157. }];
  158. } else if (type == 1) {
  159. NSData *data = image.TIFFRepresentation;
  160. NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:data];
  161. [imageRep setSize:image.size];
  162. NSData *imageData = [imageRep representationUsingType:NSBitmapImageFileTypeJPEG properties:@{}];
  163. NSSavePanel *savePanel = [NSSavePanel savePanel];
  164. savePanel.allowedFileTypes = @[@"jpg"];
  165. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  166. if (result) {
  167. if ([imageData writeToURL:savePanel.URL atomically:YES]) {
  168. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  169. inFileViewerRootedAtPath:@""];
  170. }
  171. }
  172. }];
  173. } else {
  174. CPDFDocument *pdf = [[CPDFDocument alloc] init];
  175. NSString *signatureImagePath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"signatureImage.png"];
  176. NSData *data = image.TIFFRepresentation;
  177. [data writeToURL:[NSURL fileURLWithPath:signatureImagePath] atomically:YES];
  178. [pdf insertPage:image.size withImage:signatureImagePath atIndex:0];
  179. NSSavePanel *savePanel = [NSSavePanel savePanel];
  180. savePanel.allowedFileTypes = @[@"pdf"];
  181. [savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  182. if (result) {
  183. if ([pdf writeToURL:savePanel.URL]) {
  184. [[NSWorkspace sharedWorkspace] selectFile:savePanel.URL.path
  185. inFileViewerRootedAtPath:@""];
  186. }
  187. }
  188. }];
  189. }
  190. }
  191. - (void)deleteCustomizeStamp {
  192. if (self.collectionView.selectionIndexes.count != 1) {
  193. return;
  194. }
  195. NSInteger index = 0;
  196. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  197. index = indexPath.item;
  198. }
  199. NSInteger clickIndex = [self.signatures indexOfObject:self.clickSignature];
  200. if(index < self.signatures.count) {
  201. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  202. [signatureManager loadAllSignatureList];
  203. [signatureManager removeObjectWithIndex:index];
  204. [signatureManager saveSingaturesToFile];
  205. [self reloadDataSignatures];
  206. }
  207. if(clickIndex == index) {
  208. [self.pdfView setAddStampObject:nil keepToolModel:NO];
  209. }
  210. }
  211. - (void)deleteAllCustomizeStamp {
  212. NSAlert *alert = [[NSAlert alloc] init];
  213. alert.messageText = NSLocalizedString(@"All signatures will disappear from the list", nil);
  214. alert.informativeText = NSLocalizedString(@"All signatures will disappear from the list", nil);
  215. [alert addButtonWithTitle:NSLocalizedString(@"Delete", nil)];
  216. [alert addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
  217. if ([alert runModal] != NSAlertFirstButtonReturn) { /// 取消
  218. return;
  219. }
  220. KMSignatureManager *signatureManager = [[KMSignatureManager alloc] init];
  221. [signatureManager loadAllSignatureList];
  222. [signatureManager removeAllObject];
  223. [signatureManager saveSingaturesToFile];
  224. [self reloadDataSignatures];
  225. [self.pdfView setAddStampObject:nil keepToolModel:NO];
  226. }
  227. #pragma mark - NSCollectionViewDelegate
  228. - (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView {
  229. return 1;
  230. }
  231. - (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  232. return self.signatures.count;
  233. }
  234. - (nonnull NSCollectionViewItem *)collectionView:(nonnull NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(nonnull NSIndexPath *)indexPath {
  235. if (indexPath.item >= self.signatures.count) {
  236. return nil;
  237. }
  238. KMSigntureViewItem *item = [collectionView makeItemWithIdentifier:@"kmSigntureViewItem" forIndexPath:indexPath];
  239. KMSignature *signature = [self.signatures objectAtIndex:indexPath.item];
  240. item.iconImg.image = signature.pathsImage;
  241. item.deleteIconBtn.tag = indexPath.item;
  242. [item.deleteIconBtn setTarget:self];
  243. [item.deleteIconBtn setAction:@selector(tableCellCloseButton_Click:)];
  244. item.delegate = self;
  245. item.itemIndex = indexPath.item;
  246. item.isSelect = signature.isSelect;
  247. return item;
  248. }
  249. - (CGSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  250. return CGSizeMake(228, 64);
  251. }
  252. - (NSEdgeInsets)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  253. return NSEdgeInsetsMake(0, 16, 16, 16);
  254. }
  255. - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  256. return 16;
  257. }
  258. - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  259. return 16;
  260. }
  261. - (void)collectionView:(NSCollectionView *)collectionView didSelectItemsAtIndexPaths:(NSSet<NSIndexPath *> *)indexPaths {
  262. if (indexPaths.count == 1) {
  263. NSInteger index = 0;
  264. for (NSIndexPath *indexPath in self.collectionView.selectionIndexPaths) {
  265. index = indexPath.item;
  266. }
  267. for (KMSignature *signature in self.selectSignatures) {
  268. signature.isSelect = NO;
  269. }
  270. [self.collectionView reloadData];
  271. if (index < self.signatures.count) {
  272. [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"KMContinuousAdditionStamp"];
  273. KMSignature *signature = [self.signatures objectAtIndex:index];
  274. signature.isSelect = YES;
  275. CStampSignatureObject *sigObject = [[CStampSignatureObject alloc] initWithSignatureImage:signature.pathsImage];
  276. self.clickSignature = signature;
  277. [self.pdfView setAddStampObject:sigObject keepToolModel:NO];
  278. [self.collectionView reloadItemsAtIndexPaths:self.collectionView.selectionIndexPaths];
  279. [self.selectSignatures addObject:signature];
  280. }
  281. }
  282. }
  283. #pragma mark - KMSigntureViewItemDelegate
  284. - (void)kmSigntureViewItem:(KMSigntureViewItem *)item didClickMenuWithItem:(NSMenuItem *)menuItem {
  285. [self.collectionView deselectAll:nil];
  286. [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
  287. [self exportCustomizeStamp:menuItem];
  288. }
  289. - (void)kmSigntureViewItemDidClickDeleteMenuItem:(KMSigntureViewItem *)item {
  290. [self.collectionView deselectAll:nil];
  291. [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
  292. [self deleteCustomizeStamp];
  293. }
  294. - (void)kmSigntureViewItemDidClickAllDeleteMenuItem:(KMSigntureViewItem *)item {
  295. [self.collectionView deselectAll:nil];
  296. [self.collectionView selectItemsAtIndexPaths:[NSSet setWithObjects:[NSIndexPath indexPathForItem:item.itemIndex inSection:0],nil] scrollPosition:NSCollectionViewScrollPositionNone];
  297. [self deleteAllCustomizeStamp];
  298. }
  299. @end