123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- //
- // KMPDFInsertPageWindow.swift
- // Cisdem PDFMaster
- //
- // Created by wanjun on 2022/11/24.
- //
- import Cocoa
- enum KMPDFToolType : Int {
- case Insert = 0
- case Extract
- }
- class KMPDFInsertPageWindow: NSWindowController {
-
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var mainLeftBox: NSBox!
- @IBOutlet weak var mainRightBox: NSBox!
- @IBOutlet weak var okBox: KMBox!
- @IBOutlet weak var okLabel: NSTextField!
- @IBOutlet weak var cancelBox: KMBox!
- @IBOutlet weak var cancelLabel: NSTextField!
-
- @IBOutlet var insertPreviewViewController: KMPDFInsertPreviewViewController!
- @IBOutlet var insertActionViewController: KMHomeInsertActionViewController!
- @IBOutlet var extractActionViewController: KMHomeExtractActionViewController!
-
- var type: KMPDFToolType!
- var pdfDocument: CPDFDocument!
-
- var filePath: URL?
- var insertScopeType: KMInsertScopeType! = .firstPage
- var pageScopeType: KMPageScopeType! = .previous
- var password: String = ""
- var fromFilePassword: String = ""
- var insertPages: [CPDFPage] = []
- var insertPageIndex: Int = 0
- var currentPageCount: Int = 1
-
- var isOneDocumentPerPage: Bool = false
- var isDeletePage: Bool = false
- override func windowDidLoad() {
- super.windowDidLoad()
- initializeUI()
- initLocalization()
-
- cancelBox.downCallback = { [self](downEntered: Bool, mouseBox: KMBox, event) -> Void in
- if downEntered {
- cancelAction(mouseBox)
- }
- }
-
- okBox.downCallback = { [self] (downEntered: Bool, mouseBox: KMBox, event) -> Void in
- if downEntered {
- switch type {
- case .Insert:
- insertAction(mouseBox)
- case .Extract:
- extractAction(mouseBox)
- case .none:
- break
- }
- }
- }
-
- insertPreviewViewController.delete = self
- insertActionViewController.delete = self
- extractActionViewController.delete = self
- reloadData()
-
- if pdfDocument.isLocked {
-
- }
- }
-
- // MARK: Init
- convenience init(documentPath: URL, toolType: KMPDFToolType) {
- self.init(windowNibName: NSNib.Name("KMPDFInsertPageWindow"))
-
- type = toolType
- pdfDocument = CPDFDocument(url: documentPath)
- switch toolType {
- case .Insert:
- self.window?.setContentSize(NSSize(width: 480, height: 441))
- case .Extract:
- self.window?.setContentSize(NSSize(width: 480, height: 404))
- }
- }
-
- func initializeUI() -> Void {
- cancelBox.fillColor = .white
- cancelLabel.textColor = .black
- okBox.fillColor = .black
- okLabel.textColor = .white
- }
-
- func initLocalization() -> Void {
- switch type {
- case .Insert:
- titleLabel.stringValue = NSLocalizedString("插入", comment: "")
- okLabel.stringValue = NSLocalizedString("插入", comment: "")
- okBox.toolTip = NSLocalizedString("插入", comment: "")
- cancelLabel.stringValue = NSLocalizedString("取消", comment: "")
- cancelBox.toolTip = NSLocalizedString("取消", comment: "")
- case .some(.Extract):
- titleLabel.stringValue = NSLocalizedString("提取", comment: "")
- okLabel.stringValue = NSLocalizedString("提取", comment: "")
- okBox.toolTip = NSLocalizedString("提取", comment: "")
- cancelLabel.stringValue = NSLocalizedString("取消", comment: "")
- cancelBox.toolTip = NSLocalizedString("取消", comment: "")
- case .none:
- break
- }
- }
-
- // MARK: private
- func reloadData() -> Void {
- switch type {
- case .Insert:
- insertPreviewViewController.pdfDocument = pdfDocument
- insertActionViewController.pdfDocument = pdfDocument
- mainLeftBox.contentView = insertPreviewViewController.view
- mainRightBox.contentView = insertActionViewController.view
- case .Extract:
- insertPreviewViewController.pdfDocument = pdfDocument
- extractActionViewController.pdfDocument = pdfDocument
- mainLeftBox.contentView = insertPreviewViewController.view
- mainRightBox.contentView = extractActionViewController.view
- default:
- break
- }
- }
-
- func beginSheetModal(for window: NSWindow, completionHandler handler: ((_ pdfDocument: CPDFDocument, _ password: String, _ pages: [CPDFPage], _ indexPage: Int) -> Void)?) {
- window.beginSheet(self.window!) { [self] returnCode in
- if pdfDocument != nil {
- handler!(pdfDocument!, password, insertPages, insertPageIndex)
- }
- }
- }
- func beginSheetExtractModal(for window: NSWindow, completionHandler handler: ((_ pdfDocument: CPDFDocument, _ pages: [CPDFPage], _ oneDocumentPerPage: Bool, _ isDeletePage: Bool) -> Void)?) {
- window.beginSheet(self.window!) { [self] returnCode in
- if pdfDocument != nil {
- handler!(pdfDocument!, insertPages, isOneDocumentPerPage, isDeletePage)
- }
- }
- }
-
- // MARK: Action
- @IBAction func cancelAction(_ sender: Any) -> Void {
- pdfDocument = nil
- NSApp.mainWindow!.endSheet(self.window!)
- self.window?.orderOut(self)
- }
-
- @IBAction func insertAction(_ sender: Any) -> Void {
- if filePath?.path == "" {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("Select a File", comment: "")
- alert.runModal()
- return
- }
-
- if pdfDocument.isLocked {
- pdfDocument.unlock(withPassword: password)
- }
-
- let fromDocument = CPDFDocument(url: filePath)!
- if fromDocument.isLocked {
- fromDocument.unlock(withPassword: fromFilePassword)
- }
-
- var pages: [CPDFPage] = []
- let insertPDF = CPDFDocument(url: filePath)
- for count in 1...insertPDF!.pageCount {
- let page = insertPDF?.page(at: UInt(count - 1))
- pages.append(page!)
- }
-
- var index = 0
- switch insertScopeType {
- case .firstPage:
- index = 0
- case .lastPage:
- index = Int(pdfDocument.pageCount)
- case .customizePage:
- index = currentPageCount
- if 1 == pageScopeType.rawValue {
- index = index - 1
- }
- case .none:
- index = 0
- }
-
- insertPages = pages
- insertPageIndex = index
-
- NSApp.mainWindow!.endSheet(self.window!)
- self.window?.orderOut(self)
- }
-
- @IBAction func extractAction(_ sender: Any) -> Void {
- NSApp.mainWindow!.endSheet(self.window!)
- self.window?.orderOut(self)
- }
- }
- // MARK: - KMPDFInsertPreviewViewControllerDelegate
- extension KMPDFInsertPageWindow: KMPDFInsertPreviewViewControllerDelegate {
- func insertPreviewViewController(_ viewController: KMPDFInsertPreviewViewController, pageChanged: Int) {
- switch type {
- case .Insert:
- insertActionViewController.currentPageCount = pageChanged
- insertActionViewController.reloadData()
- case .Extract:
- extractActionViewController.currentPageCount = pageChanged
- extractActionViewController.reloadData()
- default:
- break
- }
- }
- }
- // MARK: - KMHomeInsertActionViewControllerDelegate
- extension KMPDFInsertPageWindow: KMHomeInsertActionViewControllerDelegate {
- func insertActionViewController(_ viewController: KMHomeInsertActionViewController, fromFilePath: URL) {
- self.filePath = fromFilePath
- }
-
- func insertActionViewController(_ viewController: KMHomeInsertActionViewController, insertScopeType: KMInsertScopeType, currentPage: Int, pageScope: KMPageScopeType) {
- self.insertScopeType = insertScopeType
- self.currentPageCount = currentPage
- self.pageScopeType = pageScope
- }
- }
- // MARK: KMHomeExtractActionViewControllerDelegate
- extension KMPDFInsertPageWindow: KMHomeExtractActionViewControllerDelegate {
- func extractActionViewController(_ viewController: KMHomeExtractActionViewController, pages: [CPDFPage]) {
- self.insertPages = pages
- }
-
- func extractActionViewController(_ viewController: KMHomeExtractActionViewController, isOneDocumentPerPage: Bool, isDeletePage: Bool) {
- self.isOneDocumentPerPage = isOneDocumentPerPage
- self.isDeletePage = isDeletePage
- }
- }
|