123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- //
- // KMRedactSelectPagesWindowController.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/12/19.
- //
- import Cocoa
- class KMRedactSelectPagesWindowController: NSWindowController {
-
- var pdfDocument: CPDFDocument?
- var selectedPages = NSMutableArray()
-
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var allPagesButton: NSButton!
- @IBOutlet weak var oddPagesButton: NSButton!
- @IBOutlet weak var evenPagesButton: NSButton!
- @IBOutlet weak var otherButton: NSButton!
- @IBOutlet weak var otherTextField: NSTextField!
- @IBOutlet weak var pagesLabel: NSTextField!
- @IBOutlet weak var okButton: NSButton!
- @IBOutlet weak var cancelButton: NSButton!
-
- var callback: ((NSMutableArray)->Void)?
-
- convenience init(document: CPDFDocument) {
- self.init(windowNibName: "KMRedactSelectPagesWindowController")
-
- self.pdfDocument = document
- }
- override func windowDidLoad() {
- super.windowDidLoad()
-
- self.titleLabel.stringValue = KMLocalizedString("Repeat Mark Across Pages", nil)
- self.allPagesButton.title = KMLocalizedString("All Pages", nil)
- self.oddPagesButton.title = KMLocalizedString("Odd Pages Only", nil)
- self.evenPagesButton.title = KMLocalizedString("Even Pages Only", nil)
- self.otherTextField.placeholderString = KMLocalizedString("e.g. 1,3-5,10",nil)
- self.cancelButton.title = KMLocalizedString("Cancel", nil)
- self.okButton.title = KMLocalizedString("OK", nil)
- if(self.pdfDocument != nil) {
- self.pagesLabel.stringValue = String(format: "/%ld", self.pdfDocument!.pageCount)
- }
- self.otherTextField.isEnabled = false
- self.allPagesButton.state = .on
- }
- @IBAction func allpages_button(_ sender: NSButton) {
- if(sender.state == .on) {
- self.oddPagesButton.state = .off
- self.evenPagesButton.state = .off
- self.otherButton.state = .off
- self.otherTextField.isEnabled = false
- } else {
- self.allPagesButton.state = .on
- }
- }
-
- @IBAction func oddpages_button(_ sender: NSButton) {
- if(sender.state == .on) {
- self.allPagesButton.state = .off
- self.evenPagesButton.state = .off
- self.otherButton.state = .off
- self.otherTextField.isEnabled = false
- } else {
- self.oddPagesButton.state = .on
- }
- }
-
- @IBAction func evenPages_button(_ sender: NSButton) {
- if(sender.state == .on) {
- self.oddPagesButton.state = .off
- self.allPagesButton.state = .off
- self.otherButton.state = .off
- self.otherTextField.isEnabled = false
- } else {
- self.evenPagesButton.state = .on
- }
- }
-
- @IBAction func other_button(_ sender: NSButton) {
- if(sender.state == .on) {
- self.oddPagesButton.state = .off
- self.evenPagesButton.state = .off
- self.allPagesButton.state = .off
- self.otherTextField.isEnabled = true
- } else {
- self.otherButton.state = .on
- }
- }
-
- @IBAction func sure_button(_ sender: NSButton) {
- self.selectedPages = NSMutableArray()
- var hasError = false
- if (self.otherButton.state == .on) {
- // hasError = [self isInvalidString:self.otherTextField.stringValue];
- hasError = KMPageRangeTools.isValidPagesString(pagesString: self.otherTextField.stringValue) == false
- let pages = KMPageRangeTools.findSelectPage(pageRangeString: self.otherTextField.stringValue, pageCount: Int(self.pdfDocument?.pageCount ?? 0))
- for i in pages {
- self.selectedPages.add(NSNumber(integerLiteral: i))
- }
- } else if (self.evenPagesButton.state == .on) {
- var tempPagesArray = NSMutableArray()
- for i in 0 ..< self.pdfDocument!.pageCount {
- if ((i+1)%2 == 0) {
- tempPagesArray.add(NSNumber(integerLiteral: Int(i) + 1))
- }
- }
- self.selectedPages = tempPagesArray
- } else if (self.oddPagesButton.state == .on) {
- var tempPagesArray = NSMutableArray()
- for i in 0 ..< self.pdfDocument!.pageCount {
- if ((i+1)%2 == 1) {
- tempPagesArray.add(NSNumber(integerLiteral: Int(i) + 1))
- }
- }
- self.selectedPages = tempPagesArray
- } else {
- var tempPagesArray = NSMutableArray()
- for i in 0 ..< self.pdfDocument!.pageCount {
- tempPagesArray.add(NSNumber(integerLiteral: Int(i) + 1))
- }
- self.selectedPages = tempPagesArray
- }
- if (hasError || self.selectedPages.count < 1) {
- let message = String(format: "%@ %@", self.pdfDocument?.documentURL.lastPathComponent ?? "", KMLocalizedString("Invalid page range or the page number is out of range. Please try again.", nil))
- KMAlertTool.runModelForMainThread(message: message)
- return;
- }
- if self.callback != nil{
- self.callback!(self.selectedPages)
- }
- self.km_quick_endSheet()
- }
-
- @IBAction func cancel_button(_ sender: AnyObject?) {
- if self.callback != nil {
- self.callback!(NSMutableArray())
- }
- self.km_quick_endSheet()
- }
-
- /*
- - (BOOL)isInvalidString:(NSString *)text {
- NSInteger pageNumber = self.pdfDocument.pageCount;
- NSMutableArray *pageNumbers = [[[NSMutableArray alloc] init] autorelease];
- BOOL isInvalid = NO;
- if (!isInvalid) {
- NSArray *array = [text componentsSeparatedByString:@","];
- for (NSString *s in array) {
- if ([s isEqualToString:@""]) {
- isInvalid = YES;
- break;
- } else {
- NSArray *pages = [s componentsSeparatedByString:@"-"];
- if ([pages count] > 2) {
- isInvalid = YES;
- break;
- } else if ([pages count] == 1) {
- NSString *p = [pages objectAtIndex:0];
- if ([p isEqualToString:@""] ||
- [p intValue] > pageNumber ||
- [p integerValue] == 0) {
- isInvalid = YES;
- break;
- } else {
- BOOL isEqual = NO;
- for (NSNumber *pageNumber in pageNumbers) {
- if ([pageNumber integerValue] == [p integerValue]) {
- isEqual = YES;
- isInvalid = YES;
- break;
- }
- }
- if (!isEqual) {
- [pageNumbers addObject:[NSNumber numberWithInteger:[p integerValue]]];
- }
- }
- } else if([pages count] == 2) {
- NSString *p1 = [pages objectAtIndex:0];
- NSString *p2 = [pages objectAtIndex:1];
- if ([p1 isEqualToString:@""] ||
- [p2 isEqualToString:@""] ||
- [p1 intValue] > [p2 intValue] ||
- [p2 intValue] > pageNumber ||
- [p1 integerValue] == 0) {
- isInvalid = YES;
- break;
- } else {
- BOOL isEqual = NO;
- for (int i=[p1 intValue]; i<=[p2 intValue]; i++) {
- for (NSNumber *pageNumber in pageNumbers) {
- if ([pageNumber integerValue] == i) {
- isEqual = YES;
- isInvalid = YES;
- break;
- }
- }
- }
- if (!isEqual) {
- for (int i=[p1 intValue]; i<=[p2 intValue]; i++) {
- [pageNumbers addObject:[NSNumber numberWithInteger:i]];
- }
- }
- }
- }
- }
- }
- }
- if ([text length] == 0) {
- isInvalid = YES;
- }
- if (!isInvalid) {
- self.selectedPages = pageNumbers;
- } else {
- self.selectedPages = nil;
- }
- return isInvalid;
- }
- */
- }
|