Package mcbase :: Package libs :: Package designeditor :: Module ruler
[hide private]
[frames] | no frames]

Source Code for Module mcbase.libs.designeditor.ruler

  1  # -*- coding: utf-8 -*- 
  2   
  3  ########################################################################## 
  4  # ruler.py 
  5  # 
  6  # Üst ve Yan Cetveller 
  7  # 
  8  # (C) 2007 Likya Yazılım ve Bilişim Hizmetleri Ltd. Şti. 
  9  ########################################################################## 
 10   
 11  from PyQt4.QtCore import * 
 12  from PyQt4.QtGui import * 
 13   
 14  from tools import * 
 15   
16 -class Ruler(QWidget):
17 18 HORIZONTAL = 0 19 VERTICAL = 1 20
21 - def __init__(self, parent, plugin):
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 # Cetvelin nereden başlayacağını belirtir. 39 self._frameWidth = 0 40 self._frameLeftOffset = 0 41 self._frameHeights = [] 42 self._maxFrameHeight = 0 43 44 # Cetvelin çizgili ve rakamlı kısmı 45 self._rulerPixmap = None 46 47 # Minimum genişlikleri ayarla. 48 self.adjustMinimumSizes()
49
50 - def refresh(self):
51 ''' 52 Cetvelin kendisini bir daha çizmesini sağlar. 53 ''' 54 self._rulerPixmap = None 55 self.update()
56
57 - def adjustMinimumSizes(self):
58 ''' 59 Cetvelin yatay ve dikey konumuna göre, minimum genişliklerini ayarlar. 60 ''' 61 fontHeight = self._fontMetrics.height() 62 63 # Alttan ve üstten fontun yarısı kadar boşluk bırakıyoruz. 64 minHeight = fontHeight + fontHeight / 2 65 66 self.setSizePolicy(QSizePolicy( 67 QSizePolicy.Minimum, 68 QSizePolicy.Minimum)) 69 self.setMinimumSize(minHeight, minHeight)
70
71 - def setOrientation(self, newOrientation):
72 ''' 73 Cetvelin yatay veya dikey oluşunu ayarlar. 74 ''' 75 if newOrientation != Ruler.VERTICAL: 76 self._orientation = Ruler.HORIZONTAL 77 78 else: 79 self._orientation = Ruler.VERTICAL
80
81 - def setEditorSize(self, width, height):
82 ''' 83 Editörün ölçülerini belirtir. 84 ''' 85 self._editorWidth = width 86 self._editorHeight = height
87
88 - def setFrameOffsetAndWidth(self, globalLeft, frameWidth):
89 ''' 90 Çerçevelerin genişliklerini ve başladıkları yerleri belirtir. 91 ''' 92 # Eğer çerçeve genişliği değişmiş ise, cetveli yeniden çiziyoruz. 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
101 - def setFrameHeights(self, frameHeights):
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 # En yüksek çerçevenin yüksekliğini buluyoruz. 112 newHeights = map(lambda x: x[1], frameHeights) 113 if newHeights: 114 newMaxHeight = max(newHeights) 115 116 else: 117 newMaxHeight = 0 118 119 # Max çerçeve yüksekliği değişmiş ise cetveli güncelliyoruz. 120 if newMaxHeight > self._maxFrameHeight: 121 self._rulerPixmap = None 122 123 self._maxFrameHeight = newMaxHeight 124 125 self.repaint()
126
127 - def getRulerPixmap(self):
128 ''' 129 Cetvelin çizgili ve rakamlı kısmını çizer. 130 ''' 131 # Eğer çerçeveler daha hazır değilse, çizim yapmıyoruz. 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 # Eğer önceden çizilmiş ise bir daha çizmiyoruz. 139 if self._rulerPixmap is not None: 140 return self._rulerPixmap 141 142 # Zemin renginde ve cetvel boyunda bir pixmap üretiyoruz. 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 # Pixmap üzerinde çizime başlıyoruz. 156 p = QPainter(pixmap) 157 p.setFont(self._font) 158 159 # Yatay ve dikey konumuna göre ekseni ve uzunluğu ayarlıyoruz. 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 # Ekseni döndürüyoruz 188 worldMatrix = QMatrix() 189 worldMatrix.translate(self.width(), 0) 190 worldMatrix.rotate(90) 191 p.setWorldMatrix(worldMatrix, True) 192 193 # Çizgileri gri ile çiziyoruz. 194 p.setPen(Qt.gray) 195 196 # Yarım santim aralıklarla çizgiler çiziyoruz. 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 # Yazıları siyah renkle yazıyoruz. 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 # Cetvelin çerçevesini çiziyoruz. 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
234 - def paintEvent(self, e):
235 ''' 236 Cetveli istenilen yere çizer. 237 ''' 238 # Pixmap üzerinde çizime başlıyoruz. 239 p = QPainter(self) 240 241 # Cetvelin çizgili kısmını çizdiriyoruz. 242 rulerPixmap = self.getRulerPixmap() 243 244 # Daha cetvel çizimi hazır değilse. 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