-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVTabDocument.swift
95 lines (84 loc) · 3.23 KB
/
VTabDocument.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// VTabDocument.swift
// tablution
//
// Created by Ian McCowan on 10/11/15.
//
//
import Foundation
@objc public class VTabDocument: NSDocument {
var tablature: VTablature = VTablature(numStrings: 6)
@IBOutlet weak var controller: VTabController?
@IBOutlet weak var tabView: VTabView?
override public var windowNibName: String? {
get {
return "TabDocument"
}
}
override init() {
if tablature.countOfChords() == 0 {
tablature.addChordFromString("-1 -1 -1 -1 -1 -1")
}
super.init()
}
override public func windowControllerDidLoadNib(aController: NSWindowController) {
controller!.setupTablature(self.tablature)
}
override public func dataOfType(typeName: String) throws -> NSData {
var tabText: String = ""
NSLog("Saving doc of type: %@", typeName)
if typeName.isEqual("com.valrusware.tablature") {
tabText = tablature.toSerialString()
}
else if typeName.isEqual("public.plain-text") {
tabText = tablature.toHumanReadableString()
}
else {
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
guard let data = tabText.dataUsingEncoding(NSUTF8StringEncoding) else {
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
return data
}
override public func readFromData(data: NSData, ofType typeName: String) throws {
if let tabText: String = String(data: data, encoding: NSUTF8StringEncoding) {
self.tablature = VTablature.tablatureFromText(tabText)
}
else {
throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil)
}
}
@IBAction func copy(sender: AnyObject) {
guard let selectedChords = tabView!.selectedChords() as? [VChord] else {
return
}
guard selectedChords.count != 0 else {
return
}
let pasteboard: NSPasteboard = NSPasteboard.generalPasteboard()
pasteboard.clearContents()
let tabWithSelection: VTablature = VTablature.tablatureWithChords(selectedChords)
pasteboard.writeObjects([tabWithSelection])
}
@IBAction func paste(sender: AnyObject) {
let pasteboard: NSPasteboard = NSPasteboard.generalPasteboard()
if pasteboard.canReadObjectForClasses([VTablature.self], options: Dictionary()) {
guard let objectsToPaste = pasteboard.readObjectsForClasses([VTablature.self], options: Dictionary()) as? [VTablature] else {
return
}
let tabToPaste: VTablature = objectsToPaste[0]
if tabView!.hasSelection() {
controller!.replaceSelectedChordsWithChords(tabToPaste.chords)
}
else {
let indexRange: NSRange = NSMakeRange(Int(tabView!.currFocusChordIndex), tabToPaste.countOfChords())
controller!.insertChords(tabToPaste.chords, atIndexes: NSIndexSet(indexesInRange: indexRange), andSelectThem: true)
}
}
}
@IBAction func cut(sender: AnyObject) {
self.copy(sender)
controller!.deleteSelectedChords()
}
}