/usr/share/pyshared/ninja_ide/gui/explorer/tree_symbols_widget.py is in ninja-ide 2.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- coding: utf-8 -*-
#
# This file is part of NINJA-IDE (http://ninja-ide.org).
#
# NINJA-IDE is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# NINJA-IDE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NINJA-IDE; If not, see <http://www.gnu.org/licenses/>.
from __future__ import absolute_import
from PyQt4.QtGui import QTreeWidget
from PyQt4.QtGui import QTreeWidgetItem
from PyQt4.QtGui import QIcon
from PyQt4.QtGui import QAbstractItemView
from PyQt4.QtGui import QHeaderView
from PyQt4.QtGui import QCursor
from PyQt4.QtGui import QMenu
from PyQt4.QtCore import Qt
from PyQt4.QtCore import SIGNAL
from ninja_ide import resources
class TreeSymbolsWidget(QTreeWidget):
###############################################################################
# TreeSymbolsWidget SIGNALS
###############################################################################
"""
goToDefinition(int)
"""
###############################################################################
def __init__(self):
QTreeWidget.__init__(self)
self.header().setHidden(True)
self.setSelectionMode(self.SingleSelection)
self.setAnimated(True)
self.header().setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
self.header().setResizeMode(0, QHeaderView.ResizeToContents)
self.header().setStretchLastSection(False)
self.actualSymbols = ('', {})
self.docstrings = {}
self.collapsedItems = {}
self.connect(self, SIGNAL("itemClicked(QTreeWidgetItem *, int)"),
self._go_to_definition)
self.connect(self, SIGNAL("itemActivated(QTreeWidgetItem *, int)"),
self._go_to_definition)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.connect(self,
SIGNAL("customContextMenuRequested(const QPoint &)"),
self._menu_context_tree)
self.connect(self, SIGNAL("itemCollapsed(QTreeWidgetItem *)"),
self._item_collapsed)
self.connect(self, SIGNAL("itemExpanded(QTreeWidgetItem *)"),
self._item_expanded)
def select_current_item(self, line, col):
#TODO
#print line, col
pass
def _menu_context_tree(self, point):
index = self.indexAt(point)
if not index.isValid():
return
menu = QMenu(self)
f_all = menu.addAction(self.tr("Fold all"))
u_all = menu.addAction(self.tr("Unfold all"))
menu.addSeparator()
u_class = menu.addAction(self.tr("Unfold classes"))
u_class_method = menu.addAction(self.tr("Unfold classes and methods"))
u_class_attr = menu.addAction(self.tr("Unfold classes and attributes"))
menu.addSeparator()
#save_state = menu.addAction(self.tr("Save State"))
self.connect(f_all, SIGNAL("triggered()"),
lambda: self.collapseAll())
self.connect(u_all, SIGNAL("triggered()"),
lambda: self.expandAll())
self.connect(u_class, SIGNAL("triggered()"), self._unfold_class)
self.connect(u_class_method, SIGNAL("triggered()"),
self._unfold_class_method)
self.connect(u_class_attr, SIGNAL("triggered()"),
self._unfold_class_attribute)
#self.connect(save_state, SIGNAL("triggered()"),
#self._save_symbols_state)
menu.exec_(QCursor.pos())
def _get_classes_root(self):
class_root = None
for i in range(self.topLevelItemCount()):
item = self.topLevelItem(i)
if item.isClass and not item.isClickable:
class_root = item
break
return class_root
def _unfold_class(self):
self.collapseAll()
classes_root = self._get_classes_root()
if not classes_root:
return
classes_root.setExpanded(True)
def _unfold_class_method(self):
self.expandAll()
classes_root = self._get_classes_root()
if not classes_root:
return
#for each class!
for i in range(classes_root.childCount()):
class_item = classes_root.child(i)
#for each attribute or functions
for j in range(class_item.childCount()):
item = class_item.child(j)
#METHODS ROOT!!
if not item.isMethod and not item.isClickable:
item.setExpanded(False)
break
def _unfold_class_attribute(self):
self.expandAll()
classes_root = self._get_classes_root()
if not classes_root:
return
#for each class!
for i in range(classes_root.childCount()):
class_item = classes_root.child(i)
#for each attribute or functions
for j in range(class_item.childCount()):
item = class_item.child(j)
#ATTRIBUTES ROOT!!
if not item.isAttribute and not item.isClickable:
item.setExpanded(False)
break
def _save_symbols_state(self):
#filename = self.actualSymbols[0]
#TODO: persist self.collapsedItems[filename] in QSettings
pass
def _get_expand(self, item):
"""
Returns True or False to be used as setExpanded() with the items
It method is based on the click that the user made in the tree
"""
name = self._get_unique_name(item)
filename = self.actualSymbols[0]
collapsed_items = self.collapsedItems.get(filename, [])
can_check = (not item.isClickable) or item.isClass or item.isMethod
if can_check and name in collapsed_items:
return False
return True
@staticmethod
def _get_unique_name(item):
"""
Returns a string used as unique name
"""
# className_Attributes/className_Functions
parent = item.parent()
if parent:
return "%s_%s" % (parent.text(0), item.text(0))
return "_%s" % item.text(0)
def update_symbols_tree(self, symbols, filename='', parent=None):
if not parent:
if filename == self.actualSymbols[0] and \
self.actualSymbols[1] and not symbols:
return
if symbols == self.actualSymbols[1]:
# Nothing new then return
return
# we have new symbols refresh it
self.clear()
self.actualSymbols = (filename, symbols)
self.docstrings = symbols.get('docstrings', {})
parent = self
if 'attributes' in symbols:
globalAttribute = ItemTree(parent, [self.tr("Attributes")])
globalAttribute.isClickable = False
globalAttribute.isAttribute = True
globalAttribute.setExpanded(self._get_expand(globalAttribute))
for glob in sorted(symbols['attributes']):
globItem = ItemTree(globalAttribute, [glob],
lineno=symbols['attributes'][glob])
globItem.isAttribute = True
globItem.setIcon(0, QIcon(resources.IMAGES['attribute']))
globItem.setExpanded(self._get_expand(globItem))
if 'functions' in symbols and symbols['functions']:
functionsItem = ItemTree(parent, [self.tr("Functions")])
functionsItem.isClickable = False
functionsItem.isMethod = True
functionsItem.setExpanded(self._get_expand(functionsItem))
for func in sorted(symbols['functions']):
item = ItemTree(functionsItem, [func],
lineno=symbols['functions'][func]['lineno'])
tooltip = self.create_tooltip(
func, symbols['functions'][func]['lineno'])
item.isMethod = True
item.setIcon(0, QIcon(resources.IMAGES['function']))
item.setToolTip(0, tooltip)
item.setExpanded(self._get_expand(item))
self.update_symbols_tree(
symbols['functions'][func]['functions'], parent=item)
if 'classes' in symbols and symbols['classes']:
classItem = ItemTree(parent, [self.tr("Classes")])
classItem.isClickable = False
classItem.isClass = True
classItem.setExpanded(self._get_expand(classItem))
for claz in sorted(symbols['classes']):
line_number = symbols['classes'][claz]['lineno']
item = ItemTree(classItem, [claz], lineno=line_number)
item.isClass = True
tooltip = self.create_tooltip(claz, line_number)
item.setToolTip(0, tooltip)
item.setIcon(0, QIcon(resources.IMAGES['class']))
item.setExpanded(self._get_expand(item))
self.update_symbols_tree(symbols['classes'][claz]['members'],
parent=item)
def _go_to_definition(self, item):
if item.isClickable:
self.emit(SIGNAL("goToDefinition(int)"), item.lineno - 1)
def create_tooltip(self, name, lineno):
doc = self.docstrings.get(lineno, None)
if doc is None:
doc = ''
else:
doc = '\n' + doc
tooltip = name + doc
return tooltip
def _item_collapsed(self, item):
super(TreeSymbolsWidget, self).collapseItem(item)
can_check = (not item.isClickable) or item.isClass or item.isMethod
if can_check:
n = self._get_unique_name(item)
filename = self.actualSymbols[0]
self.collapsedItems.setdefault(filename, [])
if not n in self.collapsedItems[filename]:
self.collapsedItems[filename].append(n)
def _item_expanded(self, item):
super(TreeSymbolsWidget, self).expandItem(item)
n = self._get_unique_name(item)
filename = self.actualSymbols[0]
if n in self.collapsedItems.get(filename, []):
self.collapsedItems[filename].remove(n)
if not len(self.collapsedItems[filename]):
# no more items, free space
del self.collapsedItems[filename]
def clean(self):
"""
Reset the tree and reset attributes
"""
self.clear()
self.collapsedItems = {}
class ItemTree(QTreeWidgetItem):
def __init__(self, parent, name, lineno=None):
QTreeWidgetItem.__init__(self, parent, name)
self.lineno = lineno
self.isClickable = True
self.isAttribute = False
self.isClass = False
self.isMethod = False
|