|
@@ -44,12 +44,15 @@ class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
|
|
|
@IBOutlet var pageRadioWidthButton:NSLayoutConstraint!
|
|
|
|
|
|
private var orgDocument:CPDFDocument?
|
|
|
- private var orgPageIndex:Int = 0
|
|
|
-
|
|
|
- convenience init(_ document: CPDFDocument?, currentPageIndex: Int) {
|
|
|
+ private var selectionIndexPaths:Set<IndexPath>?
|
|
|
+ private var lastUnitString: String = "mm"
|
|
|
+
|
|
|
+ var callback: ((CGSize, Int) -> Void)?
|
|
|
+
|
|
|
+ convenience init(_ document: CPDFDocument?, selectionIndexPaths: Set<IndexPath>?) {
|
|
|
self.init(windowNibName: "KMNPDFInsertBlankWindowController")
|
|
|
orgDocument = document
|
|
|
- orgPageIndex = currentPageIndex
|
|
|
+ self.selectionIndexPaths = selectionIndexPaths
|
|
|
}
|
|
|
|
|
|
convenience init(_ filePath: String,password:String?) {
|
|
@@ -78,7 +81,7 @@ class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
|
|
|
|
|
|
private func setUpUnitSelctProperty() {
|
|
|
var menuItemArr: [ComponentMenuitemProperty] = []
|
|
|
- for language in [KMLocalizedString("mm"),KMLocalizedString("cn"),KMLocalizedString("in")] {
|
|
|
+ for language in ["mm","cm","in"] {
|
|
|
let itemProperty: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
|
|
|
itemSelected: false,
|
|
|
isDisabled: false,
|
|
@@ -126,11 +129,23 @@ class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
|
|
|
positionLabel.stringValue = KMLocalizedString("Where to insert?")
|
|
|
positionLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-medium")
|
|
|
positionLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
|
|
|
+ var currentPageDex = 0
|
|
|
|
|
|
- let page = self.orgDocument?.page(at: UInt(orgPageIndex)) as? CPDFPage
|
|
|
+ if(selectionIndexPaths?.count ?? 0 > 0) {
|
|
|
+ pageRadioButton.properties.checkboxType = .selected
|
|
|
+
|
|
|
+ let maxmumIndexPath = selectionIndexPaths?.max(by: { $0 < $1 })
|
|
|
+ currentPageDex = maxmumIndexPath?.item ?? 0
|
|
|
+ }
|
|
|
+ let page = self.orgDocument?.page(at: UInt(currentPageDex)) as? CPDFPage
|
|
|
let rect = page?.bounds ?? CGRectZero
|
|
|
- let width = KMNPageSizeTool.conversion(withUnit: "mm", value: (NSWidth(rect)/595 * 210))
|
|
|
- let height = KMNPageSizeTool.conversion(withUnit: "mm", value: (NSHeight(rect)/842 * 297))
|
|
|
+ var width = KMNPageSizeTool.conversion(withUnit: "mm", value: (NSWidth(rect)/595 * 210))
|
|
|
+ var height = KMNPageSizeTool.conversion(withUnit: "mm", value: (NSHeight(rect)/842 * 297))
|
|
|
+
|
|
|
+ if((page?.rotation ?? 0) % 180 != 0) {
|
|
|
+ width = KMNPageSizeTool.conversion(withUnit: "mm", value: (NSHeight(rect)/595 * 210))
|
|
|
+ height = KMNPageSizeTool.conversion(withUnit: "mm", value: (NSWidth(rect)/842 * 297))
|
|
|
+ }
|
|
|
|
|
|
currentRadioButton.properties = ComponentCheckBoxProperty(size: .s,
|
|
|
state: .normal,
|
|
@@ -177,17 +192,17 @@ class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
|
|
|
isDisabled: false,
|
|
|
showhelp: false,
|
|
|
text: KMLocalizedString("Landscape pages"),
|
|
|
- checkboxType: .selected)
|
|
|
+ checkboxType: .normal)
|
|
|
horizontalRadioButton.properties = ComponentCheckBoxProperty(size: .s,
|
|
|
state: .normal,
|
|
|
isDisabled: false,
|
|
|
showhelp: false,
|
|
|
text: KMLocalizedString("Portrait pages"),
|
|
|
- checkboxType: .normal)
|
|
|
+ checkboxType: .selected)
|
|
|
|
|
|
unitSelect.properties = ComponentSelectProperties(size: .s,
|
|
|
state: .normal,
|
|
|
- isDisabled: false,
|
|
|
+ isDisabled: true,
|
|
|
isError: false,
|
|
|
leftIcon: false,
|
|
|
placeholder: nil,
|
|
@@ -198,7 +213,7 @@ class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
|
|
|
|
|
|
standardSelect.properties = ComponentSelectProperties(size: .s,
|
|
|
state: .normal,
|
|
|
- isDisabled: false,
|
|
|
+ isDisabled: true,
|
|
|
isError: false,
|
|
|
leftIcon: false,
|
|
|
placeholder: nil,
|
|
@@ -263,10 +278,11 @@ class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
|
|
|
showPrefix: false,
|
|
|
showSuffix: false,
|
|
|
showClear: false,
|
|
|
- isDisabled: false,
|
|
|
+ isDisabled: true,
|
|
|
placeholder: "210",
|
|
|
text: "210")
|
|
|
widthInput.properties = inputWidthProperty
|
|
|
+ widthInput.delegate = self
|
|
|
|
|
|
let inputHeightProperty: ComponentInputProperty = ComponentInputProperty(size: .s,
|
|
|
state:.pressed ,
|
|
@@ -274,11 +290,12 @@ class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
|
|
|
showPrefix: false,
|
|
|
showSuffix: false,
|
|
|
showClear: false,
|
|
|
- isDisabled: false,
|
|
|
+ isDisabled: true,
|
|
|
placeholder: "297",
|
|
|
text: "297")
|
|
|
heightInput.properties = inputHeightProperty
|
|
|
-
|
|
|
+ heightInput.delegate = self
|
|
|
+
|
|
|
pageCountLabel.stringValue = "/" + String(format: "%d", orgDocument?.pageCount ?? 0)
|
|
|
|
|
|
setUpPositionSelctProperty()
|
|
@@ -301,14 +318,30 @@ class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
|
|
|
positionSelect.selectItemAtIndex(0)
|
|
|
unitSelect.selectItemAtIndex(0)
|
|
|
standardSelect.selectItemAtIndex(1)
|
|
|
-
|
|
|
- pageRadioButton.properties.checkboxType = .selected
|
|
|
- pageRadioButton.reloadData()
|
|
|
-
|
|
|
- pageNumInput.properties.text = String((orgPageIndex+1))
|
|
|
- pageNumInput.reloadData()
|
|
|
+
|
|
|
+ pageCountLabel.stringValue = "/" + String(format: "%d", orgDocument?.pageCount ?? 0)
|
|
|
|
|
|
- pageCountLabel.stringValue = "/" + String(format: "%d", orgDocument?.pageCount ?? 0)
|
|
|
+ if(selectionIndexPaths?.count ?? 0 > 0) {
|
|
|
+ pageRadioButton.properties.checkboxType = .selected
|
|
|
+
|
|
|
+ let maxmumIndexPath = selectionIndexPaths?.max(by: { $0 < $1 })
|
|
|
+ pageNumInput.properties.text = String((maxmumIndexPath?.item ?? 0)+1)
|
|
|
+
|
|
|
+ pageRadioButton.reloadData()
|
|
|
+ pageNumInput.reloadData()
|
|
|
+ } else {
|
|
|
+ firstRadioButton.properties.checkboxType = .selected
|
|
|
+ pageRadioButton.properties.checkboxType = .normal
|
|
|
+
|
|
|
+ positionSelect.properties.isDisabled = true
|
|
|
+ pageNumInput.properties.isDisabled = true
|
|
|
+
|
|
|
+ firstRadioButton.reloadData()
|
|
|
+ pageRadioButton.reloadData()
|
|
|
+ pageNumInput.reloadData()
|
|
|
+ positionSelect.reloadData()
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
//MARK: - Action
|
|
@@ -317,6 +350,85 @@ class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
|
|
|
}
|
|
|
|
|
|
@objc func insertButtonClicked(_ sender: NSView) {
|
|
|
+ self.window?.makeFirstResponder(nil)
|
|
|
+ var currentPageDex = 0
|
|
|
+ if(selectionIndexPaths?.count ?? 0 > 0) {
|
|
|
+ pageRadioButton.properties.checkboxType = .selected
|
|
|
+
|
|
|
+ let maxmumIndexPath = selectionIndexPaths?.max(by: { $0 < $1 })
|
|
|
+ currentPageDex = maxmumIndexPath?.item ?? 0
|
|
|
+ }
|
|
|
+
|
|
|
+ var height: CGFloat = 0
|
|
|
+ var width: CGFloat = 0
|
|
|
+ if currentRadioButton.properties.checkboxType == .selected {
|
|
|
+ let page = self.orgDocument?.page(at: UInt(currentPageDex)) as? CPDFPage
|
|
|
+ let rect = page?.bounds ?? .zero
|
|
|
+ width = rect.size.width
|
|
|
+ height = rect.size.height
|
|
|
+
|
|
|
+ let w = KMPageSizeTool.conversion(withUnit: "mm", value: (NSWidth(rect)/595 * 210))
|
|
|
+ let h = KMPageSizeTool.conversion(withUnit: "mm", value: (NSHeight(rect)/842 * 297))
|
|
|
+
|
|
|
+ width = w.stringToCGFloat()
|
|
|
+ height = h.stringToCGFloat()
|
|
|
+
|
|
|
+ if((page?.rotation ?? 0) % 180 != 0) {
|
|
|
+ width = h.stringToCGFloat()
|
|
|
+ height = w.stringToCGFloat()
|
|
|
+ }
|
|
|
+ } else if standardRadioButton.properties.checkboxType == .selected {
|
|
|
+ let sizeString = standardSelect.properties.text ?? KMNPaperSizeNameA4
|
|
|
+ let tSize = KMPageSizeTool.getPaperSize(paperName: sizeString)
|
|
|
+ width = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.width).stringToCGFloat()
|
|
|
+ height = KMPageSizeTool.conversion(withUnit: "mm", value: tSize.height).stringToCGFloat()
|
|
|
+
|
|
|
+ } else if customizationRadioButton.properties.checkboxType == .selected {
|
|
|
+ let unitString = unitSelect.properties.text ?? "mm"
|
|
|
+ height = widthInput.properties.text.stringToCGFloat()
|
|
|
+ width = heightInput.properties.text.stringToCGFloat()
|
|
|
+
|
|
|
+ width = KMPageSizeTool.conversion(with: "mm", from: unitString, value: width).stringToCGFloat()
|
|
|
+ height = KMPageSizeTool.conversion(with: "mm", from: unitString, value: height).stringToCGFloat()
|
|
|
+
|
|
|
+ if(!(width > 0 && height > 0)) {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = KMLocalizedString("Please enter the correct size.")
|
|
|
+ alert.runModal()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ width *= 595 / 210
|
|
|
+ height *= 842 / 297
|
|
|
+
|
|
|
+ var insertSize = CGSize(width: width, height: height)
|
|
|
+ if(verticalRadioButton.properties.checkboxType == .selected) {
|
|
|
+ if(width < height) {
|
|
|
+ insertSize = CGSize(width: height, height: width)
|
|
|
+ }
|
|
|
+ } else if(horizontalRadioButton.properties.checkboxType == .selected){
|
|
|
+ if(width > height) {
|
|
|
+ insertSize = CGSize(width: height, height: width)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var insetPageDex:Int = 0
|
|
|
+ if firstRadioButton.properties.checkboxType == .selected {
|
|
|
+ insetPageDex = 0
|
|
|
+ } else if lastRadioButton.properties.checkboxType == .selected {
|
|
|
+ insetPageDex = Int(orgDocument?.pageCount ?? 0)
|
|
|
+ } else if pageRadioButton.properties.checkboxType == .selected {
|
|
|
+ insetPageDex = Int(pageNumInput.properties.text ?? "0") ?? 0
|
|
|
+ let positionSelectIndex = positionSelect.indexOfSelect()
|
|
|
+ if (1 == positionSelectIndex) {
|
|
|
+ insetPageDex -= 1
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ callback?(insertSize, insetPageDex)
|
|
|
+
|
|
|
own_closeEndSheet()
|
|
|
}
|
|
|
|
|
@@ -379,6 +491,29 @@ class KMNPDFInsertBlankWindowController: KMNBaseWindowController {
|
|
|
oldSelectRadioBtn?.properties.checkboxType = .normal
|
|
|
oldSelectRadioBtn?.reloadData()
|
|
|
}
|
|
|
+
|
|
|
+ let widthString = widthInput.properties.text
|
|
|
+ let heightString = heightInput.properties.text
|
|
|
+
|
|
|
+ var width = widthString.stringToCGFloat()
|
|
|
+ var height = heightString.stringToCGFloat()
|
|
|
+
|
|
|
+ if(verticalRadioButton.properties.checkboxType == .selected) {
|
|
|
+ if(width < height) {
|
|
|
+ widthInput.properties.text = heightString
|
|
|
+ heightInput.properties.text = widthString
|
|
|
+ widthInput.reloadData()
|
|
|
+ heightInput.reloadData()
|
|
|
+ }
|
|
|
+ } else if(horizontalRadioButton.properties.checkboxType == .selected){
|
|
|
+ if(width > height) {
|
|
|
+ widthInput.properties.text = heightString
|
|
|
+ heightInput.properties.text = widthString
|
|
|
+ widthInput.reloadData()
|
|
|
+ heightInput.reloadData()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@objc func insertPageSizeAction(_ sender: NSView) {
|
|
@@ -450,7 +585,14 @@ extension KMNPDFInsertBlankWindowController: ComponentInputNumberDelegate {
|
|
|
extension KMNPDFInsertBlankWindowController: ComponentSelectDelegate {
|
|
|
func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
|
|
|
if(view == unitSelect) {
|
|
|
+ let width = widthInput.properties.text.stringToCGFloat()
|
|
|
+ let height = heightInput.properties.text.stringToCGFloat()
|
|
|
+ widthInput.properties.text = KMNPageSizeTool.conversion(with: unitSelect.properties.text ?? "mm", from: lastUnitString, value: width)
|
|
|
+ heightInput.properties.text = KMNPageSizeTool.conversion(with: unitSelect.properties.text ?? "mm", from: lastUnitString, value: height)
|
|
|
|
|
|
+ lastUnitString = unitSelect.properties.text ?? "mm"
|
|
|
+ widthInput.reloadData()
|
|
|
+ heightInput.reloadData()
|
|
|
} else if (view == standardSelect) {
|
|
|
|
|
|
} else if (view == positionSelect) {
|
|
@@ -458,4 +600,36 @@ extension KMNPDFInsertBlankWindowController: ComponentSelectDelegate {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+extension KMNPDFInsertBlankWindowController: ComponentInputDelegate {
|
|
|
+
|
|
|
+ //文字变化
|
|
|
+ func componentInputDidChanged(inputView: ComponentInput) {
|
|
|
+ if (inputView == widthInput) {
|
|
|
+ let fValue = widthInput.properties.text.stringToCGFloat()
|
|
|
+ if fValue > heightInput.properties.text.stringToCGFloat() {
|
|
|
+ verticalRadioButton.properties.checkboxType = .selected
|
|
|
+ horizontalRadioButton.properties.checkboxType = .normal
|
|
|
+ } else {
|
|
|
+ verticalRadioButton.properties.checkboxType = .normal
|
|
|
+ horizontalRadioButton.properties.checkboxType = .selected
|
|
|
+ }
|
|
|
+ verticalRadioButton.reloadData()
|
|
|
+ horizontalRadioButton.reloadData()
|
|
|
+ } else if (inputView == heightInput) {
|
|
|
+ let fValue = heightInput.properties.text.stringToCGFloat()
|
|
|
+ if fValue > widthInput.properties.text.stringToCGFloat() {
|
|
|
+ verticalRadioButton.properties.checkboxType = .normal
|
|
|
+ horizontalRadioButton.properties.checkboxType = .selected
|
|
|
+ } else {
|
|
|
+ verticalRadioButton.properties.checkboxType = .selected
|
|
|
+ horizontalRadioButton.properties.checkboxType = .normal
|
|
|
+ }
|
|
|
+ verticalRadioButton.reloadData()
|
|
|
+ horizontalRadioButton.reloadData()
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|