KMRedactSelectPagesWindowController.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // KMRedactSelectPagesWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/19.
  6. //
  7. import Cocoa
  8. class KMRedactSelectPagesWindowController: NSWindowController {
  9. var pdfDocument: CPDFDocument?
  10. var selectedPages = NSMutableArray()
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var allPagesButton: NSButton!
  13. @IBOutlet weak var oddPagesButton: NSButton!
  14. @IBOutlet weak var evenPagesButton: NSButton!
  15. @IBOutlet weak var otherButton: NSButton!
  16. @IBOutlet weak var otherTextField: NSTextField!
  17. @IBOutlet weak var pagesLabel: NSTextField!
  18. @IBOutlet weak var okButton: NSButton!
  19. @IBOutlet weak var cancelButton: NSButton!
  20. var callback: ((NSMutableArray)->Void)?
  21. convenience init(document: CPDFDocument) {
  22. self.init(windowNibName: "KMRedactSelectPagesWindowController")
  23. self.pdfDocument = document
  24. }
  25. override func windowDidLoad() {
  26. super.windowDidLoad()
  27. self.titleLabel.stringValue = KMLocalizedString("Repeat Mark Across Pages")
  28. self.allPagesButton.title = KMLocalizedString("All Pages")
  29. self.oddPagesButton.title = KMLocalizedString("Odd Pages Only")
  30. self.evenPagesButton.title = KMLocalizedString("Even Pages Only")
  31. self.otherTextField.placeholderString = KMLocalizedString("e.g. 1,3-5,10")
  32. self.cancelButton.title = KMLocalizedString("Cancel")
  33. self.okButton.title = KMLocalizedString("OK")
  34. if(self.pdfDocument != nil) {
  35. self.pagesLabel.stringValue = String(format: "/%ld", self.pdfDocument!.pageCount)
  36. }
  37. self.otherTextField.isEnabled = false
  38. self.allPagesButton.state = .on
  39. }
  40. @IBAction func allpages_button(_ sender: NSButton) {
  41. if(sender.state == .on) {
  42. self.oddPagesButton.state = .off
  43. self.evenPagesButton.state = .off
  44. self.otherButton.state = .off
  45. self.otherTextField.isEnabled = false
  46. } else {
  47. self.allPagesButton.state = .on
  48. }
  49. }
  50. @IBAction func oddpages_button(_ sender: NSButton) {
  51. if(sender.state == .on) {
  52. self.allPagesButton.state = .off
  53. self.evenPagesButton.state = .off
  54. self.otherButton.state = .off
  55. self.otherTextField.isEnabled = false
  56. } else {
  57. self.oddPagesButton.state = .on
  58. }
  59. }
  60. @IBAction func evenPages_button(_ sender: NSButton) {
  61. if(sender.state == .on) {
  62. self.oddPagesButton.state = .off
  63. self.allPagesButton.state = .off
  64. self.otherButton.state = .off
  65. self.otherTextField.isEnabled = false
  66. } else {
  67. self.evenPagesButton.state = .on
  68. }
  69. }
  70. @IBAction func other_button(_ sender: NSButton) {
  71. if(sender.state == .on) {
  72. self.oddPagesButton.state = .off
  73. self.evenPagesButton.state = .off
  74. self.allPagesButton.state = .off
  75. self.otherTextField.isEnabled = true
  76. } else {
  77. self.otherButton.state = .on
  78. }
  79. }
  80. @IBAction func sure_button(_ sender: NSButton) {
  81. self.selectedPages = NSMutableArray()
  82. var hasError = false
  83. if (self.otherButton.state == .on) {
  84. // hasError = [self isInvalidString:self.otherTextField.stringValue];
  85. hasError = KMPageRangeTools.isValidPagesString(pagesString: self.otherTextField.stringValue) == false
  86. let pages = KMPageRangeTools.findSelectPage(pageRangeString: self.otherTextField.stringValue, pageCount: Int(self.pdfDocument?.pageCount ?? 0))
  87. for i in pages {
  88. self.selectedPages.add(NSNumber(integerLiteral: i))
  89. }
  90. } else if (self.evenPagesButton.state == .on) {
  91. var tempPagesArray = NSMutableArray()
  92. for i in 0 ..< self.pdfDocument!.pageCount {
  93. if ((i+1)%2 == 0) {
  94. tempPagesArray.add(NSNumber(integerLiteral: Int(i) + 1))
  95. }
  96. }
  97. self.selectedPages = tempPagesArray
  98. } else if (self.oddPagesButton.state == .on) {
  99. var tempPagesArray = NSMutableArray()
  100. for i in 0 ..< self.pdfDocument!.pageCount {
  101. if ((i+1)%2 == 1) {
  102. tempPagesArray.add(NSNumber(integerLiteral: Int(i) + 1))
  103. }
  104. }
  105. self.selectedPages = tempPagesArray
  106. } else {
  107. var tempPagesArray = NSMutableArray()
  108. for i in 0 ..< self.pdfDocument!.pageCount {
  109. tempPagesArray.add(NSNumber(integerLiteral: Int(i) + 1))
  110. }
  111. self.selectedPages = tempPagesArray
  112. }
  113. if (hasError || self.selectedPages.count < 1) {
  114. let message = String(format: "%@ %@", self.pdfDocument?.documentURL.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again."))
  115. KMAlertTool.runModelForMainThread(message: message)
  116. return;
  117. }
  118. if self.callback != nil{
  119. self.callback!(self.selectedPages)
  120. }
  121. self.km_quick_endSheet()
  122. }
  123. @IBAction func cancel_button(_ sender: AnyObject?) {
  124. if self.callback != nil {
  125. self.callback!(NSMutableArray())
  126. }
  127. self.km_quick_endSheet()
  128. }
  129. /*
  130. - (BOOL)isInvalidString:(NSString *)text {
  131. NSInteger pageNumber = self.pdfDocument.pageCount;
  132. NSMutableArray *pageNumbers = [[[NSMutableArray alloc] init] autorelease];
  133. BOOL isInvalid = NO;
  134. if (!isInvalid) {
  135. NSArray *array = [text componentsSeparatedByString:@","];
  136. for (NSString *s in array) {
  137. if ([s isEqualToString:@""]) {
  138. isInvalid = YES;
  139. break;
  140. } else {
  141. NSArray *pages = [s componentsSeparatedByString:@"-"];
  142. if ([pages count] > 2) {
  143. isInvalid = YES;
  144. break;
  145. } else if ([pages count] == 1) {
  146. NSString *p = [pages objectAtIndex:0];
  147. if ([p isEqualToString:@""] ||
  148. [p intValue] > pageNumber ||
  149. [p integerValue] == 0) {
  150. isInvalid = YES;
  151. break;
  152. } else {
  153. BOOL isEqual = NO;
  154. for (NSNumber *pageNumber in pageNumbers) {
  155. if ([pageNumber integerValue] == [p integerValue]) {
  156. isEqual = YES;
  157. isInvalid = YES;
  158. break;
  159. }
  160. }
  161. if (!isEqual) {
  162. [pageNumbers addObject:[NSNumber numberWithInteger:[p integerValue]]];
  163. }
  164. }
  165. } else if([pages count] == 2) {
  166. NSString *p1 = [pages objectAtIndex:0];
  167. NSString *p2 = [pages objectAtIndex:1];
  168. if ([p1 isEqualToString:@""] ||
  169. [p2 isEqualToString:@""] ||
  170. [p1 intValue] > [p2 intValue] ||
  171. [p2 intValue] > pageNumber ||
  172. [p1 integerValue] == 0) {
  173. isInvalid = YES;
  174. break;
  175. } else {
  176. BOOL isEqual = NO;
  177. for (int i=[p1 intValue]; i<=[p2 intValue]; i++) {
  178. for (NSNumber *pageNumber in pageNumbers) {
  179. if ([pageNumber integerValue] == i) {
  180. isEqual = YES;
  181. isInvalid = YES;
  182. break;
  183. }
  184. }
  185. }
  186. if (!isEqual) {
  187. for (int i=[p1 intValue]; i<=[p2 intValue]; i++) {
  188. [pageNumbers addObject:[NSNumber numberWithInteger:i]];
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. if ([text length] == 0) {
  197. isInvalid = YES;
  198. }
  199. if (!isInvalid) {
  200. self.selectedPages = pageNumbers;
  201. } else {
  202. self.selectedPages = nil;
  203. }
  204. return isInvalid;
  205. }
  206. */
  207. }