Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 130 lines (109 sloc) 3.245 kb
a10421a2 » Watson1978
2012-08-04 moved the files from MacRuby's repository
1 PackModelChangedNotification = 'PackModelChangedNotification'
2
3 BLOCK_COUNT = 8
4
5 class PackModel
6 def initialize
7 @pageInfos = Array.new(BLOCK_COUNT)
8 end
9
10 def preparedImageRepForPage(pageNum)
11 obj = @pageInfos[pageNum]
12 if obj == nil
13 nil
14 else
15 obj.preparedImageRep
16 end
17 end
18
19 def _replacePageInfoAt(i, withPageInfo:pi)
20 oldInfo = @pageInfos[i]
21 if pi != oldInfo
22 obj = @undoManager.prepareWithInvocationTarget self
23 obj._replacePageInfoAt i, withPageInfo:oldInfo
24 @pageInfos[i] = pi
25 NSNotificationCenter.defaultCenter.postNotificationName PackModelChangedNotification,
26 object:self, userInfo:nil
27 end
28 end
29
30 def setImageRep(r, pageOfRep:repPage, forPage:viewPage)
31 pi = PageInfo.new
32 pi.setImageRep r
33 pi.setPageOfRep repPage
34 _replacePageInfoAt viewPage, withPageInfo:pi
35 end
36
37 def initWithCoder(c)
38 @pageInfos = c.decodeObjectForKey('pageInfos')
39 self
40 end
41
42 def encodeWithCoder(c)
43 c.encodeObject @pageInfos, forKey:'pageInfos'
44 end
45
46 def setUndoManager(undo)
47 @undoManager = undo
48 end
49
50 attr_reader :undoManager
51
52 def removeAllImageReps
53 BLOCK_COUNT.times { |i| _replacePageInfoAt i, withPageInfo:nil }
54 end
55
56 def removeImageRepAtPage(i)
57 _replacePageInfoAt i, withPageInfo:nil
58 end
59
60 def swapImageRepAt(i, withRepAt:j)
61 pii = @pageInfos[i]
62 pij = @pageInfos[j]
63
64 _replacePageInfoAt(i, withPageInfo:pij)
65 _replacePageInfoAt(j, withPageInfo:pii)
66 end
67
68 def copyImageRepAt(i, toRepAt:j)
69 pii = @pageInfos[i]
70 pij = PageInfo.new
71 pij.setImageRep pii.imageRep
72 pij.setPageOfRep pii.pageOfRep
73 _replacePageInfoAt j, withPageInfo:pij
74 end
75
76 def pageIsFilled(i)
77 @pageInfos[i] != nil
78 end
79
80 def textAttributes
81 { NSFontAttributeName => PreferenceController.sharedPreferenceController.textFont }
82 end
83
84 def putAttributedString(attString, startingOnPage:i)
85 pdf = pdfFromAttributedStringOfSize attString, NSMakeSize(200, 300)
86 putPDFData pdf, startingOnPage:i
87 end
88
89 def putPDF(pdf, startingOnPage:i)
90 endPage = 0
91 pdf.pageCount.times do |j|
92 endPage = j
93 break if j + i >= BLOCK_COUNT
94 setImageRep pdf, pageOfRep:j, forPage:j+i
95 end
96 i + endPage
97 end
98
99 def putFile(currentPath, startingOnPage:i)
100 imageRep = NSImageRep.imageRepWithContentsOfFile currentPath
101 if imageRep == nil
102 str = NSString.stringWithContentsOfFile currentPath, encoding:NSUTF8StringEncoding, error:nil
103 if str == nil
104 i
105 else
106 attString = NSAttributedString.alloc.initWithString str, attributes:textAttributes
107 putAttributedString attString, startingOnPage:i
108 end
109 elsif imageRep.kind_of?(NSPDFImageRep)
110 putPDF imageRep, startingOnPage:i
111 else
112 setImageRep imageRep, pageOfRep:-1, forPage:i
113 i + 1
114 end
115 end
116
117 def putFiles(filenames, startingOnPage:i)
118 currentStart = i
119 filenames.each { |x| currentStart = putFile(x, startingOnPage:currentStart) }
120 currentStart
121 end
122
123 def putPDFData(d, startingOnPage:i)
124 ir = NSPDFImageRep.alloc.initWithData(d)
125 i - 1 + ir.pageCount.times do |j|
126 break if j + i >= BLOCK_COUNT
127 setImageRep ir, pageOfRep:j, forPage:j + i
128 end
129 end
130 end
Something went wrong with that request. Please try again.