1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
"""
This module contains the panels controller, responsible of drawing panel
inside CodeEdit's margins
"""
import logging
from pyqode.core.api.utils import TextHelper
from pyqode.core.api.manager import Manager
from pyqode.core.api.panel import Panel
def _logger():
return logging.getLogger(__name__)
class PanelsManager(Manager):
"""
Manages the list of panels and draws them inised the margin of the code
edit widget.
"""
def __init__(self, editor):
super(PanelsManager, self).__init__(editor)
self._cached_cursor_pos = (-1, -1)
self._margin_sizes = (0, 0, 0, 0)
self._top = self._left = self._right = self._bottom = -1
self._panels = {
Panel.Position.TOP: {},
Panel.Position.LEFT: {},
Panel.Position.RIGHT: {},
Panel.Position.BOTTOM: {}
}
try:
editor.blockCountChanged.connect(self._update_viewport_margins)
editor.updateRequest.connect(self._update)
except AttributeError:
# QTextEdit
editor.document().blockCountChanged.connect(
self._update_viewport_margins)
def append(self, panel, position=Panel.Position.LEFT):
"""
Installs a panel on the editor.
:param panel: Panel to install
:param position: Position where the panel must be installed.
:return: The installed panel
"""
assert panel is not None
pos_to_string = {
Panel.Position.BOTTOM: 'bottom',
Panel.Position.LEFT: 'left',
Panel.Position.RIGHT: 'right',
Panel.Position.TOP: 'top'
}
_logger().log(5, 'adding panel %s at %r', panel.name,
pos_to_string[position])
panel.order_in_zone = len(self._panels[position])
self._panels[position][panel.name] = panel
panel.position = position
panel.on_install(self.editor)
_logger().log(5, 'panel %s installed', panel.name)
return panel
def remove(self, name_or_klass):
"""
Removes the specified panel.
:param name_or_klass: Name or class of the panel to remove.
:return: The removed panel
"""
_logger().log(5, 'removing panel %r', name_or_klass)
panel = self.get(name_or_klass)
panel.on_uninstall()
panel.hide()
panel.setParent(None)
return self._panels[panel.position].pop(panel.name, None)
def clear(self):
"""
Removes all panel from the editor.
"""
for i in range(4):
while len(self._panels[i]):
key = sorted(list(self._panels[i].keys()))[0]
panel = self.remove(key)
panel.setParent(None)
panel.deleteLater()
def get(self, name_or_klass):
"""
Gets a specific panel instance.
:param name_or_klass: Name or class of the panel to retrieve.
:return: The specified panel instance.
"""
if not isinstance(name_or_klass, str):
name_or_klass = name_or_klass.__name__
for zone in range(4):
try:
panel = self._panels[zone][name_or_klass]
except KeyError:
pass
else:
return panel
raise KeyError(name_or_klass)
def keys(self):
"""
Returns the list of installed panel names.
"""
return self._modes.keys()
def values(self):
"""
Returns the list of installed panels.
"""
return self._modes.values()
def __iter__(self):
lst = []
for zone, zone_dict in self._panels.items():
for name, panel in zone_dict.items():
lst.append(panel)
return iter(lst)
def __len__(self):
lst = []
for zone, zone_dict in self._panels.items():
for name, panel in zone_dict.items():
lst.append(panel)
return len(lst)
def panels_for_zone(self, zone):
"""
Gets the list of panels attached to the specified zone.
:param zone: Panel position.
:return: List of panels instances.
"""
return list(self._panels[zone].values())
def refresh(self):
""" Refreshes the editor panels (resize and update margins) """
_logger().log(5, 'refresh_panels')
self.resize()
self._update(self.editor.contentsRect(), 0,
force_update_margins=True)
def resize(self):
""" Resizes panels """
crect = self.editor.contentsRect()
view_crect = self.editor.viewport().contentsRect()
s_bottom, s_left, s_right, s_top = self._compute_zones_sizes()
tw = s_left + s_right
th = s_bottom + s_top
w_offset = crect.width() - (view_crect.width() + tw)
h_offset = crect.height() - (view_crect.height() + th)
left = 0
panels = self.panels_for_zone(Panel.Position.LEFT)
panels.sort(key=lambda panel: panel.order_in_zone, reverse=True)
for panel in panels:
if not panel.isVisible():
continue
panel.adjustSize()
size_hint = panel.sizeHint()
panel.setGeometry(crect.left() + left,
crect.top() + s_top,
size_hint.width(),
crect.height() - s_bottom - s_top - h_offset)
left += size_hint.width()
right = 0
panels = self.panels_for_zone(Panel.Position.RIGHT)
panels.sort(key=lambda panel: panel.order_in_zone, reverse=True)
for panel in panels:
if not panel.isVisible():
continue
size_hint = panel.sizeHint()
panel.setGeometry(
crect.right() - right - size_hint.width() - w_offset,
crect.top() + s_top,
size_hint.width(),
crect.height() - s_bottom - s_top - h_offset)
right += size_hint.width()
top = 0
panels = self.panels_for_zone(Panel.Position.TOP)
panels.sort(key=lambda panel: panel.order_in_zone)
for panel in panels:
if not panel.isVisible():
continue
size_hint = panel.sizeHint()
panel.setGeometry(crect.left(),
crect.top() + top,
crect.width() - w_offset,
size_hint.height())
top += size_hint.height()
bottom = 0
panels = self.panels_for_zone(Panel.Position.BOTTOM)
panels.sort(key=lambda panel: panel.order_in_zone)
for panel in panels:
if not panel.isVisible():
continue
size_hint = panel.sizeHint()
panel.setGeometry(
crect.left(),
crect.bottom() - bottom - size_hint.height() - h_offset,
crect.width() - w_offset,
size_hint.height())
bottom += size_hint.height()
def _update(self, rect, delta_y, force_update_margins=False):
""" Updates panels """
helper = TextHelper(self.editor)
if not self:
return
for zones_id, zone in self._panels.items():
if zones_id == Panel.Position.TOP or \
zones_id == Panel.Position.BOTTOM:
continue
panels = list(zone.values())
for panel in panels:
if panel.scrollable and delta_y:
panel.scroll(0, delta_y)
line, col = helper.cursor_position()
oline, ocol = self._cached_cursor_pos
if line != oline or col != ocol or panel.scrollable:
panel.update(0, rect.y(), panel.width(), rect.height())
self._cached_cursor_pos = helper.cursor_position()
if (rect.contains(self.editor.viewport().rect()) or
force_update_margins):
self._update_viewport_margins()
def _update_viewport_margins(self):
""" Update viewport margins """
top = 0
left = 0
right = 0
bottom = 0
for panel in self.panels_for_zone(Panel.Position.LEFT):
if panel.isVisible():
width = panel.sizeHint().width()
left += width
for panel in self.panels_for_zone(Panel.Position.RIGHT):
if panel.isVisible():
width = panel.sizeHint().width()
right += width
for panel in self.panels_for_zone(Panel.Position.TOP):
if panel.isVisible():
height = panel.sizeHint().height()
top += height
for panel in self.panels_for_zone(Panel.Position.BOTTOM):
if panel.isVisible():
height = panel.sizeHint().height()
bottom += height
self._margin_sizes = (top, left, right, bottom)
self.editor.setViewportMargins(left, top, right, bottom)
def margin_size(self, position=Panel.Position.LEFT):
"""
Gets the size of a specific margin.
:param position: Margin position. See
:class:`pyqode.core.api.Panel.Position`
:return: The size of the specified margin
:rtype: float
"""
return self._margin_sizes[position]
def _compute_zones_sizes(self):
""" Compute panel zone sizes """
# Left panels
left = 0
for panel in self.panels_for_zone(Panel.Position.LEFT):
if not panel.isVisible():
continue
size_hint = panel.sizeHint()
left += size_hint.width()
# Right panels
right = 0
for panel in self.panels_for_zone(Panel.Position.RIGHT):
if not panel.isVisible():
continue
size_hint = panel.sizeHint()
right += size_hint.width()
# Top panels
top = 0
for panel in self.panels_for_zone(Panel.Position.TOP):
if not panel.isVisible():
continue
size_hint = panel.sizeHint()
top += size_hint.height()
# Bottom panels
bottom = 0
for panel in self.panels_for_zone(Panel.Position.BOTTOM):
if not panel.isVisible():
continue
size_hint = panel.sizeHint()
bottom += size_hint.height()
self._top, self._left, self._right, self._bottom = (
top, left, right, bottom)
return bottom, left, right, top
|