src/eric7/EricWidgets/EricMapWidget.py

branch
eric7
changeset 10423
299802979277
parent 9972
68ac01294544
child 10439
21c28b0f9e41
equal deleted inserted replaced
10422:e28b89693f37 10423:299802979277
19 19
20 def __init__(self, parent=None): 20 def __init__(self, parent=None):
21 """ 21 """
22 Constructor 22 Constructor
23 23
24 @param parent reference to the parent widget (QWidget) 24 @param parent reference to the parent widget
25 @type QWidget
25 """ 26 """
26 super().__init__(parent) 27 super().__init__(parent)
27 self.setAttribute(Qt.WidgetAttribute.WA_OpaquePaintEvent) 28 self.setAttribute(Qt.WidgetAttribute.WA_OpaquePaintEvent)
28 29
29 self.__width = 14 30 self.__width = 14
82 83
83 def setWidth(self, width): 84 def setWidth(self, width):
84 """ 85 """
85 Public method to set the widget width. 86 Public method to set the widget width.
86 87
87 @param width widget width (integer) 88 @param width widget width
89 @type int
88 """ 90 """
89 if width != self.__width: 91 if width != self.__width:
90 self.__width = max(6, width) # minimum width 6 pixels 92 self.__width = max(6, width) # minimum width 6 pixels
91 self.__updateControllerViewportWidth() 93 self.__updateControllerViewportWidth()
92 self.update() 94 self.update()
93 95
94 def width(self): 96 def width(self):
95 """ 97 """
96 Public method to get the widget's width. 98 Public method to get the widget's width.
97 99
98 @return widget width (integer) 100 @return widget width
101 @rtype int
99 """ 102 """
100 return self.__width 103 return self.__width
101 104
102 def setMapPosition(self, onRight): 105 def setMapPosition(self, onRight):
103 """ 106 """
126 129
127 def setLineDimensions(self, border, height): 130 def setLineDimensions(self, border, height):
128 """ 131 """
129 Public method to set the line (indicator) dimensions. 132 Public method to set the line (indicator) dimensions.
130 133
131 @param border border width on each side in x-direction (integer) 134 @param border border width on each side in x-direction
132 @param height height of the line in pixels (integer) 135 @type int
136 @param height height of the line in pixels
137 @type int
133 """ 138 """
134 if border != self.__lineBorder or height != self.__lineHeight: 139 if border != self.__lineBorder or height != self.__lineHeight:
135 self.__lineBorder = max(1, border) # min border 1 pixel 140 self.__lineBorder = max(1, border) # min border 1 pixel
136 self.__lineHeight = max(1, height) # min height 1 pixel 141 self.__lineHeight = max(1, height) # min height 1 pixel
137 self.update() 142 self.update()
138 143
139 def lineDimensions(self): 144 def lineDimensions(self):
140 """ 145 """
141 Public method to get the line (indicator) dimensions. 146 Public method to get the line (indicator) dimensions.
142 147
143 @return tuple with border width (integer) and line height (integer) 148 @return tuple with border width (integer) and line height
149 @rtype int
144 """ 150 """
145 return self.__lineBorder, self.__lineHeight 151 return self.__lineBorder, self.__lineHeight
146 152
147 def setEnabled(self, enable): 153 def setEnabled(self, enable):
148 """ 154 """
149 Public method to set the enabled state. 155 Public method to set the enabled state.
150 156
151 @param enable flag indicating the enabled state (boolean) 157 @param enable flag indicating the enabled state
158 @type bool
152 """ 159 """
153 if enable != self.__enabled: 160 if enable != self.__enabled:
154 self.__enabled = enable 161 self.__enabled = enable
155 self.setVisible(enable) 162 self.setVisible(enable)
156 self.__updateControllerViewportWidth() 163 self.__updateControllerViewportWidth()
157 164
158 def isEnabled(self): 165 def isEnabled(self):
159 """ 166 """
160 Public method to check the enabled state. 167 Public method to check the enabled state.
161 168
162 @return flag indicating the enabled state (boolean) 169 @return flag indicating the enabled state
170 @rtype bool
163 """ 171 """
164 return self.__enabled 172 return self.__enabled
165 173
166 def setBackgroundColor(self, color): 174 def setBackgroundColor(self, color):
167 """ 175 """
168 Public method to set the widget background color. 176 Public method to set the widget background color.
169 177
170 @param color color for the background (QColor) 178 @param color color for the background
179 @type QColor
171 """ 180 """
172 if color != self.__backgroundColor: 181 if color != self.__backgroundColor:
173 self.__backgroundColor = color 182 self.__backgroundColor = color
174 self.__setSliderColor() 183 self.__setSliderColor()
175 self.update() 184 self.update()
176 185
177 def backgroundColor(self): 186 def backgroundColor(self):
178 """ 187 """
179 Public method to get the background color. 188 Public method to get the background color.
180 189
181 @return background color (QColor) 190 @return background color
191 @rtype QColor
182 """ 192 """
183 return QColor(self.__backgroundColor) 193 return QColor(self.__backgroundColor)
184 194
185 def sizeHint(self): 195 def sizeHint(self):
186 """ 196 """
187 Public method to give an indication about the preferred size. 197 Public method to give an indication about the preferred size.
188 198
189 @return preferred size (QSize) 199 @return preferred size
200 @rtype QSize
190 """ 201 """
191 return QSize(self.__width, 0) 202 return QSize(self.__width, 0)
192 203
193 def paintEvent(self, event): 204 def paintEvent(self, event):
194 """ 205 """
195 Protected method to handle a paint event. 206 Protected method to handle a paint event.
196 207
197 @param event paint event (QPaintEvent) 208 @param event paint event
209 @type QPaintEvent
198 """ 210 """
199 # step 1: fill the whole painting area 211 # step 1: fill the whole painting area
200 painter = QPainter(self) 212 painter = QPainter(self)
201 painter.fillRect(event.rect(), self.__backgroundColor) 213 painter.fillRect(event.rect(), self.__backgroundColor)
202 214
217 """ 229 """
218 Protected method for painting the widget's indicators. 230 Protected method for painting the widget's indicators.
219 231
220 Note: This method should be implemented by subclasses. 232 Note: This method should be implemented by subclasses.
221 233
222 @param painter reference to the painter object (QPainter) 234 @param painter reference to the painter object
235 @type QPainter
223 """ 236 """
224 pass 237 pass
225 238
226 def mousePressEvent(self, event): 239 def mousePressEvent(self, event):
227 """ 240 """
228 Protected method to handle a mouse button press. 241 Protected method to handle a mouse button press.
229 242
230 @param event reference to the mouse event (QMouseEvent) 243 @param event reference to the mouse event
244 @type QMouseEvent
231 """ 245 """
232 if event.button() == Qt.MouseButton.LeftButton and self._controller: 246 if event.button() == Qt.MouseButton.LeftButton and self._controller:
233 vsb = self._controller.verticalScrollBar() 247 vsb = self._controller.verticalScrollBar()
234 value = self.position2Value(event.position().toPoint().y() - 1) 248 value = self.position2Value(event.position().toPoint().y() - 1)
235 vsb.setValue(int(value - 0.5 * vsb.pageStep())) # center on page 249 vsb.setValue(int(value - 0.5 * vsb.pageStep())) # center on page
237 251
238 def mouseMoveEvent(self, event): 252 def mouseMoveEvent(self, event):
239 """ 253 """
240 Protected method to handle a mouse moves. 254 Protected method to handle a mouse moves.
241 255
242 @param event reference to the mouse event (QMouseEvent) 256 @param event reference to the mouse event
257 @type QMouseEvent
243 """ 258 """
244 if event.buttons() & Qt.MouseButton.LeftButton and self._controller: 259 if event.buttons() & Qt.MouseButton.LeftButton and self._controller:
245 vsb = self._controller.verticalScrollBar() 260 vsb = self._controller.verticalScrollBar()
246 value = self.position2Value(event.position().toPoint().y() - 1) 261 value = self.position2Value(event.position().toPoint().y() - 1)
247 vsb.setValue(int(value - 0.5 * vsb.pageStep())) # center on page 262 vsb.setValue(int(value - 0.5 * vsb.pageStep())) # center on page
248 263
249 def wheelEvent(self, event): 264 def wheelEvent(self, event):
250 """ 265 """
251 Protected slot handling mouse wheel events. 266 Protected slot handling mouse wheel events.
252 267
253 @param event reference to the wheel event (QWheelEvent) 268 @param event reference to the wheel event
269 @type QWheelEvent
254 """ 270 """
255 isVertical = event.angleDelta().x() == 0 271 isVertical = event.angleDelta().x() == 0
256 if ( 272 if (
257 self._controller 273 self._controller
258 and event.modifiers() == Qt.KeyboardModifier.NoModifier 274 and event.modifiers() == Qt.KeyboardModifier.NoModifier
290 def scaleFactor(self, slider=False): 306 def scaleFactor(self, slider=False):
291 """ 307 """
292 Public method to determine the scrollbar's scale factor. 308 Public method to determine the scrollbar's scale factor.
293 309
294 @param slider flag indicating to calculate the result for the slider 310 @param slider flag indicating to calculate the result for the slider
295 (boolean) 311 @type bool
296 @return scale factor (float) 312 @return scale factor
313 @rtype float
297 """ 314 """
298 if self._controller: 315 if self._controller:
299 delta = 0 if slider else 2 316 delta = 0 if slider else 2
300 vsb = self._controller.verticalScrollBar() 317 vsb = self._controller.verticalScrollBar()
301 posHeight = vsb.height() - delta - 1 318 posHeight = vsb.height() - delta - 1
306 323
307 def value2Position(self, value, slider=False): 324 def value2Position(self, value, slider=False):
308 """ 325 """
309 Public method to convert a scrollbar value into a position. 326 Public method to convert a scrollbar value into a position.
310 327
311 @param value value to convert (integer) 328 @param value value to convert
329 @type int
312 @param slider flag indicating to calculate the result for the slider 330 @param slider flag indicating to calculate the result for the slider
313 (boolean) 331 @type bool
314 @return position (integer) 332 @return position
333 @rtype int
315 """ 334 """
316 if self._controller: 335 if self._controller:
317 offset = 0 if slider else 1 336 offset = 0 if slider else 1
318 vsb = self._controller.verticalScrollBar() 337 vsb = self._controller.verticalScrollBar()
319 return int((value - vsb.minimum()) * self.scaleFactor(slider) + offset) 338 return int((value - vsb.minimum()) * self.scaleFactor(slider) + offset)
322 341
323 def position2Value(self, position, slider=False): 342 def position2Value(self, position, slider=False):
324 """ 343 """
325 Public method to convert a position into a scrollbar value. 344 Public method to convert a position into a scrollbar value.
326 345
327 @param position scrollbar position to convert (integer) 346 @param position scrollbar position to convert
347 @type int
328 @param slider flag indicating to calculate the result for the slider 348 @param slider flag indicating to calculate the result for the slider
329 (boolean) 349
330 @return scrollbar value (integer) 350 @type bool
351 @return scrollbar value
352 @rtype int
331 """ 353 """
332 if self._controller: 354 if self._controller:
333 offset = 0 if slider else 1 355 offset = 0 if slider else 1
334 vsb = self._controller.verticalScrollBar() 356 vsb = self._controller.verticalScrollBar()
335 return vsb.minimum() + max( 357 return vsb.minimum() + max(
340 362
341 def generateIndicatorRect(self, position): 363 def generateIndicatorRect(self, position):
342 """ 364 """
343 Public method to generate an indicator rectangle. 365 Public method to generate an indicator rectangle.
344 366
345 @param position indicator position (integer) 367 @param position indicator position
346 @return indicator rectangle (QRect) 368 @type int
369 @return indicator rectangle
370 @rtype QRect
347 """ 371 """
348 return QRect( 372 return QRect(
349 self.__lineBorder, 373 self.__lineBorder,
350 position - self.__lineHeight // 2, 374 position - self.__lineHeight // 2,
351 self.__width - self.__lineBorder, 375 self.__width - self.__lineBorder,
354 378
355 def __generateSliderRange(self, scrollbar): 379 def __generateSliderRange(self, scrollbar):
356 """ 380 """
357 Private method to generate the slider rectangle. 381 Private method to generate the slider rectangle.
358 382
359 @param scrollbar reference to the vertical scrollbar (QScrollBar) 383 @param scrollbar reference to the vertical scrollbar
360 @return slider rectangle (QRect) 384 @type QScrollBar
385 @return slider rectangle
386 @rtype QRect
361 """ 387 """
362 pos1 = self.value2Position(scrollbar.value(), slider=True) 388 pos1 = self.value2Position(scrollbar.value(), slider=True)
363 pos2 = self.value2Position( 389 pos2 = self.value2Position(
364 scrollbar.value() + scrollbar.pageStep(), slider=True 390 scrollbar.value() + scrollbar.pageStep(), slider=True
365 ) 391 )

eric ide

mercurial