30 self.__lineBorder = 1 |
30 self.__lineBorder = 1 |
31 self.__lineHeight = 2 |
31 self.__lineHeight = 2 |
32 self.__backgroundColor = QColor("#e7e7e7") |
32 self.__backgroundColor = QColor("#e7e7e7") |
33 self.__setSliderColor() |
33 self.__setSliderColor() |
34 |
34 |
35 self._master = None |
35 self._controller = None |
36 self.__enabled = False |
36 self.__enabled = False |
37 self.__rightSide = True |
37 self.__rightSide = True |
38 |
38 |
39 if parent is not None and isinstance(parent, QAbstractScrollArea): |
39 if parent is not None and isinstance(parent, QAbstractScrollArea): |
40 self.setMaster(parent) |
40 self.setController(parent) |
41 |
41 |
42 def __setSliderColor(self): |
42 def __setSliderColor(self): |
43 """ |
43 """ |
44 Private method to set the slider color depending upon the background |
44 Private method to set the slider color depending upon the background |
45 color. |
45 color. |
49 self.__sliderColor = Qt.GlobalColor.white |
49 self.__sliderColor = Qt.GlobalColor.white |
50 else: |
50 else: |
51 # light background, use black slider |
51 # light background, use black slider |
52 self.__sliderColor = Qt.GlobalColor.black |
52 self.__sliderColor = Qt.GlobalColor.black |
53 |
53 |
54 def __updateMasterViewportWidth(self): |
54 def __updateControllerViewportWidth(self): |
55 """ |
55 """ |
56 Private method to update the master's viewport width. |
56 Private method to update the controller's viewport width. |
57 """ |
57 """ |
58 if self._master: |
58 if self._controller: |
59 if self.__enabled: |
59 if self.__enabled: |
60 width = self.__width |
60 width = self.__width |
61 else: |
61 else: |
62 width = 0 |
62 width = 0 |
63 if self.__rightSide: |
63 if self.__rightSide: |
64 self._master.setViewportMargins(0, 0, width, 0) |
64 self._controller.setViewportMargins(0, 0, width, 0) |
65 else: |
65 else: |
66 self._master.setViewportMargins(width, 0, 0, 0) |
66 self._controller.setViewportMargins(width, 0, 0, 0) |
67 |
67 |
68 def setMaster(self, master): |
68 def setController(self, controller): |
69 """ |
69 """ |
70 Public method to set the map master widget. |
70 Public method to set the map controller widget. |
71 |
71 |
72 @param master map master widget (QAbstractScrollArea) |
72 @param controller map controller widget |
73 """ |
73 @type QAbstractScrollArea |
74 self._master = master |
74 """ |
75 self._master.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn) |
75 self._controller = controller |
76 self._master.verticalScrollBar().valueChanged.connect(self.update) |
76 self._controller.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn) |
77 self._master.verticalScrollBar().rangeChanged.connect(self.update) |
77 self._controller.verticalScrollBar().valueChanged.connect(self.update) |
78 self.__updateMasterViewportWidth() |
78 self._controller.verticalScrollBar().rangeChanged.connect(self.update) |
|
79 self.__updateControllerViewportWidth() |
79 |
80 |
80 def setWidth(self, width): |
81 def setWidth(self, width): |
81 """ |
82 """ |
82 Public method to set the widget width. |
83 Public method to set the widget width. |
83 |
84 |
84 @param width widget width (integer) |
85 @param width widget width (integer) |
85 """ |
86 """ |
86 if width != self.__width: |
87 if width != self.__width: |
87 self.__width = max(6, width) # minimum width 6 pixels |
88 self.__width = max(6, width) # minimum width 6 pixels |
88 self.__updateMasterViewportWidth() |
89 self.__updateControllerViewportWidth() |
89 self.update() |
90 self.update() |
90 |
91 |
91 def width(self): |
92 def width(self): |
92 """ |
93 """ |
93 Public method to get the widget's width. |
94 Public method to get the widget's width. |
97 return self.__width |
98 return self.__width |
98 |
99 |
99 def setMapPosition(self, onRight): |
100 def setMapPosition(self, onRight): |
100 """ |
101 """ |
101 Public method to set, whether the map should be shown to the right or |
102 Public method to set, whether the map should be shown to the right or |
102 left of the master widget. |
103 left of the controller widget. |
103 |
104 |
104 @param onRight flag indicating to show the map on the right side of |
105 @param onRight flag indicating to show the map on the right side of |
105 the master widget |
106 the controller widget |
106 @type bool |
107 @type bool |
107 """ |
108 """ |
108 if onRight != self.__rightSide: |
109 if onRight != self.__rightSide: |
109 self.__rightSide = onRight |
110 self.__rightSide = onRight |
110 self.__updateMasterViewportWidth() |
111 self.__updateControllerViewportWidth() |
111 self.update() |
112 self.update() |
112 |
113 |
113 def isOnRightSide(self): |
114 def isOnRightSide(self): |
114 """ |
115 """ |
115 Public method to test, if the map is shown on the right side of the |
116 Public method to test, if the map is shown on the right side of the |
116 master widget. |
117 controller widget. |
117 |
118 |
118 @return flag indicating that the map is to the right of the master |
119 @return flag indicating that the map is to the right of the controller |
119 widget |
120 widget |
120 @rtype bool |
121 @rtype bool |
121 """ |
122 """ |
122 return self.__rightSide |
123 return self.__rightSide |
123 |
124 |
148 @param enable flag indicating the enabled state (boolean) |
149 @param enable flag indicating the enabled state (boolean) |
149 """ |
150 """ |
150 if enable != self.__enabled: |
151 if enable != self.__enabled: |
151 self.__enabled = enable |
152 self.__enabled = enable |
152 self.setVisible(enable) |
153 self.setVisible(enable) |
153 self.__updateMasterViewportWidth() |
154 self.__updateControllerViewportWidth() |
154 |
155 |
155 def isEnabled(self): |
156 def isEnabled(self): |
156 """ |
157 """ |
157 Public method to check the enabled state. |
158 Public method to check the enabled state. |
158 |
159 |
199 |
200 |
200 # step 2: paint the indicators |
201 # step 2: paint the indicators |
201 self._paintIt(painter) |
202 self._paintIt(painter) |
202 |
203 |
203 # step 3: paint the slider |
204 # step 3: paint the slider |
204 if self._master: |
205 if self._controller: |
205 penColor = self.__sliderColor |
206 penColor = self.__sliderColor |
206 painter.setPen(penColor) |
207 painter.setPen(penColor) |
207 brushColor = Qt.GlobalColor.transparent |
208 brushColor = Qt.GlobalColor.transparent |
208 painter.setBrush(QBrush(brushColor)) |
209 painter.setBrush(QBrush(brushColor)) |
209 painter.drawRect( |
210 painter.drawRect( |
210 self.__generateSliderRange(self._master.verticalScrollBar()) |
211 self.__generateSliderRange(self._controller.verticalScrollBar()) |
211 ) |
212 ) |
212 |
213 |
213 def _paintIt(self, painter): |
214 def _paintIt(self, painter): |
214 """ |
215 """ |
215 Protected method for painting the widget's indicators. |
216 Protected method for painting the widget's indicators. |
224 """ |
225 """ |
225 Protected method to handle a mouse button press. |
226 Protected method to handle a mouse button press. |
226 |
227 |
227 @param event reference to the mouse event (QMouseEvent) |
228 @param event reference to the mouse event (QMouseEvent) |
228 """ |
229 """ |
229 if event.button() == Qt.MouseButton.LeftButton and self._master: |
230 if event.button() == Qt.MouseButton.LeftButton and self._controller: |
230 vsb = self._master.verticalScrollBar() |
231 vsb = self._controller.verticalScrollBar() |
231 value = self.position2Value(event.position().toPoint().y() - 1) |
232 value = self.position2Value(event.position().toPoint().y() - 1) |
232 vsb.setValue(int(value - 0.5 * vsb.pageStep())) # center on page |
233 vsb.setValue(int(value - 0.5 * vsb.pageStep())) # center on page |
233 self.__mousePressPos = None |
234 self.__mousePressPos = None |
234 |
235 |
235 def mouseMoveEvent(self, event): |
236 def mouseMoveEvent(self, event): |
236 """ |
237 """ |
237 Protected method to handle a mouse moves. |
238 Protected method to handle a mouse moves. |
238 |
239 |
239 @param event reference to the mouse event (QMouseEvent) |
240 @param event reference to the mouse event (QMouseEvent) |
240 """ |
241 """ |
241 if event.buttons() & Qt.MouseButton.LeftButton and self._master: |
242 if event.buttons() & Qt.MouseButton.LeftButton and self._controller: |
242 vsb = self._master.verticalScrollBar() |
243 vsb = self._controller.verticalScrollBar() |
243 value = self.position2Value(event.position().toPoint().y() - 1) |
244 value = self.position2Value(event.position().toPoint().y() - 1) |
244 vsb.setValue(int(value - 0.5 * vsb.pageStep())) # center on page |
245 vsb.setValue(int(value - 0.5 * vsb.pageStep())) # center on page |
245 |
246 |
246 def wheelEvent(self, event): |
247 def wheelEvent(self, event): |
247 """ |
248 """ |
249 |
250 |
250 @param event reference to the wheel event (QWheelEvent) |
251 @param event reference to the wheel event (QWheelEvent) |
251 """ |
252 """ |
252 isVertical = event.angleDelta().x() == 0 |
253 isVertical = event.angleDelta().x() == 0 |
253 if ( |
254 if ( |
254 self._master |
255 self._controller |
255 and event.modifiers() == Qt.KeyboardModifier.NoModifier |
256 and event.modifiers() == Qt.KeyboardModifier.NoModifier |
256 and isVertical |
257 and isVertical |
257 ): |
258 ): |
258 QCoreApplication.sendEvent(self._master.verticalScrollBar(), event) |
259 QCoreApplication.sendEvent(self._controller.verticalScrollBar(), event) |
259 |
260 |
260 def calculateGeometry(self): |
261 def calculateGeometry(self): |
261 """ |
262 """ |
262 Public method to recalculate the map widget's geometry. |
263 Public method to recalculate the map widget's geometry. |
263 """ |
264 """ |
264 if self._master: |
265 if self._controller: |
265 cr = self._master.contentsRect() |
266 cr = self._controller.contentsRect() |
266 vsb = self._master.verticalScrollBar() |
267 vsb = self._controller.verticalScrollBar() |
267 if vsb.isVisible(): |
268 if vsb.isVisible(): |
268 vsbw = vsb.contentsRect().width() |
269 vsbw = vsb.contentsRect().width() |
269 else: |
270 else: |
270 vsbw = 0 |
271 vsbw = 0 |
271 margins = self._master.contentsMargins() |
272 margins = self._controller.contentsMargins() |
272 if margins.right() > vsbw: |
273 if margins.right() > vsbw: |
273 vsbw = 0 |
274 vsbw = 0 |
274 if self.__rightSide: |
275 if self.__rightSide: |
275 self.setGeometry( |
276 self.setGeometry( |
276 QRect( |
277 QRect( |
290 |
291 |
291 @param slider flag indicating to calculate the result for the slider |
292 @param slider flag indicating to calculate the result for the slider |
292 (boolean) |
293 (boolean) |
293 @return scale factor (float) |
294 @return scale factor (float) |
294 """ |
295 """ |
295 if self._master: |
296 if self._controller: |
296 delta = 0 if slider else 2 |
297 delta = 0 if slider else 2 |
297 vsb = self._master.verticalScrollBar() |
298 vsb = self._controller.verticalScrollBar() |
298 posHeight = vsb.height() - delta - 1 |
299 posHeight = vsb.height() - delta - 1 |
299 valHeight = vsb.maximum() - vsb.minimum() + vsb.pageStep() |
300 valHeight = vsb.maximum() - vsb.minimum() + vsb.pageStep() |
300 return float(posHeight) / valHeight |
301 return float(posHeight) / valHeight |
301 else: |
302 else: |
302 return 1.0 |
303 return 1.0 |
308 @param value value to convert (integer) |
309 @param value value to convert (integer) |
309 @param slider flag indicating to calculate the result for the slider |
310 @param slider flag indicating to calculate the result for the slider |
310 (boolean) |
311 (boolean) |
311 @return position (integer) |
312 @return position (integer) |
312 """ |
313 """ |
313 if self._master: |
314 if self._controller: |
314 offset = 0 if slider else 1 |
315 offset = 0 if slider else 1 |
315 vsb = self._master.verticalScrollBar() |
316 vsb = self._controller.verticalScrollBar() |
316 return int((value - vsb.minimum()) * self.scaleFactor(slider) + offset) |
317 return int((value - vsb.minimum()) * self.scaleFactor(slider) + offset) |
317 else: |
318 else: |
318 return value |
319 return value |
319 |
320 |
320 def position2Value(self, position, slider=False): |
321 def position2Value(self, position, slider=False): |
324 @param position scrollbar position to convert (integer) |
325 @param position scrollbar position to convert (integer) |
325 @param slider flag indicating to calculate the result for the slider |
326 @param slider flag indicating to calculate the result for the slider |
326 (boolean) |
327 (boolean) |
327 @return scrollbar value (integer) |
328 @return scrollbar value (integer) |
328 """ |
329 """ |
329 if self._master: |
330 if self._controller: |
330 offset = 0 if slider else 1 |
331 offset = 0 if slider else 1 |
331 vsb = self._master.verticalScrollBar() |
332 vsb = self._controller.verticalScrollBar() |
332 return vsb.minimum() + max( |
333 return vsb.minimum() + max( |
333 0, (position - offset) / self.scaleFactor(slider) |
334 0, (position - offset) / self.scaleFactor(slider) |
334 ) |
335 ) |
335 else: |
336 else: |
336 return position |
337 return position |