1
2
3
4
5
6
7
8
9
10
11 from PyQt4.QtCore import *
12 from PyQt4.QtGui import *
13
14 from tools import *
15
17
18 HORIZONTAL = 0
19 VERTICAL = 1
20
22 '''
23 Cetveli temel olarak oluşturur.
24 '''
25 QWidget.__init__(self, parent)
26
27 self._plugin = plugin
28
29 self._editorWidth = 10
30 self._editorHeight = 10
31
32 self._font = QFont()
33 self._font.setPointSize(8)
34 self._fontMetrics = QFontMetrics(self._font)
35
36 self._orientation = Ruler.HORIZONTAL
37
38
39 self._frameWidth = 0
40 self._frameLeftOffset = 0
41 self._frameHeights = []
42 self._maxFrameHeight = 0
43
44
45 self._rulerPixmap = None
46
47
48 self.adjustMinimumSizes()
49
51 '''
52 Cetvelin kendisini bir daha çizmesini sağlar.
53 '''
54 self._rulerPixmap = None
55 self.update()
56
58 '''
59 Cetvelin yatay ve dikey konumuna göre, minimum genişliklerini ayarlar.
60 '''
61 fontHeight = self._fontMetrics.height()
62
63
64 minHeight = fontHeight + fontHeight / 2
65
66 self.setSizePolicy(QSizePolicy(
67 QSizePolicy.Minimum,
68 QSizePolicy.Minimum))
69 self.setMinimumSize(minHeight, minHeight)
70
80
82 '''
83 Editörün ölçülerini belirtir.
84 '''
85 self._editorWidth = width
86 self._editorHeight = height
87
89 '''
90 Çerçevelerin genişliklerini ve başladıkları yerleri belirtir.
91 '''
92
93 if self._frameWidth != frameWidth:
94 self._rulerPixmap = None
95
96 self._frameWidth = frameWidth
97 self._frameLeftOffset = self.mapFromGlobal(globalLeft).x()
98
99 self.repaint()
100
102 '''
103 Çervelerin body kısmının başladığı yerleri ve yükseklikleri belirtir.
104 '''
105 self._frameHeights = []
106
107 for globalPoint, frameHeight in frameHeights:
108 localPoint = self.mapFromGlobal(globalPoint)
109 self._frameHeights.append((localPoint.y(), frameHeight))
110
111
112 newHeights = map(lambda x: x[1], frameHeights)
113 if newHeights:
114 newMaxHeight = max(newHeights)
115
116 else:
117 newMaxHeight = 0
118
119
120 if newMaxHeight > self._maxFrameHeight:
121 self._rulerPixmap = None
122
123 self._maxFrameHeight = newMaxHeight
124
125 self.repaint()
126
128 '''
129 Cetvelin çizgili ve rakamlı kısmını çizer.
130 '''
131
132 if self._orientation == Ruler.HORIZONTAL and not self._frameWidth:
133 return
134
135 elif self._orientation == Ruler.VERTICAL and not self._frameHeights:
136 return
137
138
139 if self._rulerPixmap is not None:
140 return self._rulerPixmap
141
142
143 width = self._frameWidth
144 height = self._maxFrameHeight + 5
145
146 if self._orientation == Ruler.HORIZONTAL:
147 height = self.minimumHeight()
148
149 elif self._orientation == Ruler.VERTICAL:
150 width = self.minimumWidth()
151
152 pixmap = QPixmap(width, height)
153 pixmap.fill(Qt.white)
154
155
156 p = QPainter(pixmap)
157 p.setFont(self._font)
158
159
160 length = 0
161 height = 0
162 shortLineRatio1 = 1
163 shortLineRatio2 = 1
164 longLineRatio1 = 1
165 longLineRatio2 = 1
166 textHeightRatio1 = 1
167 textHeightRatio2 = 1
168
169 if self._orientation == Ruler.HORIZONTAL:
170 length = width
171 height = self.height()
172 shortLineRatio1 = 0.3
173 longLineRatio1 = 0.7
174 textHeightRatio1 = 0
175 textHeightRatio2 = 0.65
176
177 else:
178 length = self._maxFrameHeight + 5
179 height = self.width()
180 shortLineRatio1 = 0
181 shortLineRatio2 = 0.7
182 longLineRatio1 = 0
183 longLineRatio2 = 0.3
184 textHeightRatio1 = 0.35
185 textHeightRatio2 = 0.65
186
187
188 worldMatrix = QMatrix()
189 worldMatrix.translate(self.width(), 0)
190 worldMatrix.rotate(90)
191 p.setWorldMatrix(worldMatrix, True)
192
193
194 p.setPen(Qt.gray)
195
196
197 dx = 0
198 if self._orientation == Ruler.HORIZONTAL:
199 dx = self._plugin.gridDx()
200
201 elif self._orientation == Ruler.VERTICAL:
202 dx = self._plugin.gridDy()
203
204 x = 0
205
206 while x < length:
207 p.drawLine(x, height * longLineRatio1, x, height * longLineRatio2)
208 p.drawLine(x + dx, height * shortLineRatio1, x + dx, height * shortLineRatio2)
209 x += 2 * dx
210
211
212 p.setPen(Qt.black)
213
214 x = 0
215 i = 1
216 while x < length:
217 textRect = QRect(x + dx, height * textHeightRatio1, 2 * dx, height * textHeightRatio2)
218 number = i * 2 * (self._plugin.gridStep()) % self._plugin.rulerModulo()
219 p.drawText(textRect, Qt.AlignCenter, str(number))
220 x += 2 * dx
221 i += 1
222
223
224 p.setWorldMatrix(QMatrix())
225 p.setPen(Qt.gray)
226 p.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1)
227
228 p.end()
229
230 self._rulerPixmap = pixmap
231
232 return pixmap
233
235 '''
236 Cetveli istenilen yere çizer.
237 '''
238
239 p = QPainter(self)
240
241
242 rulerPixmap = self.getRulerPixmap()
243
244
245 if not rulerPixmap:
246 return
247
248 if self._orientation == Ruler.HORIZONTAL:
249 p.drawPixmap(self._frameLeftOffset, 0, rulerPixmap)
250
251 else:
252 for frameStart, frameHeight in self._frameHeights:
253 p.drawPixmap(0, frameStart, rulerPixmap, 0, 0, rulerPixmap.width(), frameHeight)
254 p.setPen(Qt.gray)
255 p.drawRect(0, frameStart, rulerPixmap.width(), frameHeight)
256