def setPenColor(self, color): """ setPenColor(color) Set the color of the pen. Color can be anything that can be passed to Qcolor(). """ pen = QtGui.QPen() if isinstance(color, tuple): pen.setColor(QtGui.QColor(*color)) else: pen.setColor(QtGui.QColor(color)) self._painter.setPen(pen)
def SetItems(parentItem, fictiveObjects, level): level += 1 for object in fictiveObjects: type = object.type if not type in showTypes: continue # Construct text if type=='cell': type = '##' elif type=='attribute': type = 'attr' # if type == 'import': text = "%s (%s)" % (object.name, object.text) elif type=='todo': text = object.name else: text = "%s %s" % (type, object.name) # Create item thisItem = QtGui.QTreeWidgetItem(parentItem, [text]) color = QtGui.QColor(colours[object.type]) thisItem.setForeground(0, QtGui.QBrush(color)) font = thisItem.font(0) font.setBold(True) thisItem.setFont(0, font) thisItem.linenr = object.linenr # Is this the current item? if ln and object.linenr <= ln and object.linenr2 > ln: selectedItem[0] = thisItem # Any children that we should display? if object.children: SetItems(thisItem, object.children, level) # Set visibility thisItem.setExpanded( bool(level < showLevel) )
def updateItems(self): """ updateItems() Update the appearance of the items. """ # Get items and tab bar items = self.items() tabBar = self.tabBar() for i in range(len(items)): # Get item item = items[i] if item is None: continue # Update name and tooltip if item.dirty: #tabBar.setTabText(i, '*'+item.name) tabBar.setTabToolTip(i, item.filename + ' [modified]') else: tabBar.setTabText(i, item.name) tabBar.setTabToolTip(i, item.filename) # Determine text color. Is main file? Is current? if self._mainFile == item.id: tabBar.setTabTextColor(i, QtGui.QColor('#008')) elif i == self.currentIndex(): tabBar.setTabTextColor(i, QtGui.QColor('#000')) else: tabBar.setTabTextColor(i, QtGui.QColor('#444')) # Get number of blocks nBlocks = item.editor.blockCount() if nBlocks == 1 and not item.editor.toPlainText(): nBlocks = 0 # Update appearance of icon but = tabBar.tabButton(i, QtGui.QTabBar.LeftSide) but.updateIcon(item.dirty, self._mainFile==item.id, item.pinned, nBlocks)
def _getPixmap(self, icon): # Get icon if given by name if isinstance(icon, str): icon = iep.icons[icon] # Create pixmap if icon is None: pm = QtGui.QPixmap(16, 16) pm.fill(QtGui.QColor(0, 0, 0, 0)) return pm elif isinstance(icon, tuple): pm = QtGui.QPixmap(icon[0], icon[1]) pm.fill(QtGui.QColor(0, 0, 0, 0)) return pm elif isinstance(icon, QtGui.QPixmap): return icon elif isinstance(icon, QtGui.QIcon): return icon.pixmap(16, 16) else: raise ValueError( 'Icon for IconArtis should be icon, pixmap or name.')
def updateStructure(self): """ Updates the tree. """ # Get editor editor = iep.editors.getCurrentEditor() if not editor: return # Something to show result = iep.parser._getResult() if result is None: return # Do the ids match? id0, id1, id2 = self._currentEditorId, id(editor), result.editorId if id0 != id1 or id0 != id2: return # Get current line number and the structure ln = editor.textCursor().blockNumber() ln += 1 # is ln as in line number area # Define colours colours = {'cell':'#007F00', 'class':'#0000FF', 'def':'#007F7F', 'attribute':'#444444', 'import':'#8800BB', 'todo':'#FF3333'} # Define what to show showTypes = self._config.showTypes # Define to what level to show (now is also a good time to save) showLevel = int( self._slider.value() ) self._config.level = showLevel # Define function to set items selectedItem = [None] def SetItems(parentItem, fictiveObjects, level): level += 1 for object in fictiveObjects: type = object.type if not type in showTypes: continue # Construct text if type=='cell': type = '##' elif type=='attribute': type = 'attr' # if type == 'import': text = "%s (%s)" % (object.name, object.text) elif type=='todo': text = object.name else: text = "%s %s" % (type, object.name) # Create item thisItem = QtGui.QTreeWidgetItem(parentItem, [text]) color = QtGui.QColor(colours[object.type]) thisItem.setForeground(0, QtGui.QBrush(color)) font = thisItem.font(0) font.setBold(True) thisItem.setFont(0, font) thisItem.linenr = object.linenr # Is this the current item? if ln and object.linenr <= ln and object.linenr2 > ln: selectedItem[0] = thisItem # Any children that we should display? if object.children: SetItems(thisItem, object.children, level) # Set visibility thisItem.setExpanded( bool(level < showLevel) ) # Go self._tree.setUpdatesEnabled(False) self._tree.clear() SetItems(self._tree, result.rootItem.children, 0) self._tree.setUpdatesEnabled(True) # Handle selected item selectedItem = selectedItem[0] if selectedItem: selectedItem.setBackground(0, QtGui.QBrush(QtGui.QColor('#CCC'))) self._tree.scrollToItem(selectedItem) # ensure visible
def write(self, text, prompt=0, color=None): """ write(text, prompt=0, color=None) Write to the shell. If prompt is 0 (default) the text is printed before the prompt. If prompt is 1, the text is printed after the prompt, the new prompt becomes null. If prompt is 2, the given text becomes the new prompt. The color of the text can also be specified (as a hex-string). """ # From The Qt docs: Note that a cursor always moves when text is # inserted before the current position of the cursor, and it always # keeps its position when text is inserted after the current position # of the cursor. # Make sure there's text and make sure its a string if not text: return if isinstance(text, bytes): text = text.decode('utf-8') # Prepare format format = QtGui.QTextCharFormat() if color: format.setForeground(QtGui.QColor(color)) #pos1, pos2 = self._cursor1.position(), self._cursor2.position() # Just in case, clear any selection of the cursors self._cursor1.clearSelection() self._cursor2.clearSelection() if prompt == 0: # Insert text behind prompt (normal streams) self._cursor1.setKeepPositionOnInsert(False) self._cursor2.setKeepPositionOnInsert(False) text = self._handleBackspaces(text) if len(text) < 1024: # Insert text self._cursor1.insertText(text, format) else: # Insert per line (very long lines are split in smaller ones) for line in self._splitLinesForPrinting(text): self._cursor1.insertText(line, format) elif prompt == 1: # Insert command text after prompt, prompt becomes null (input) self._lastCommandCursor.setPosition(self._cursor2.position()) self._cursor1.setKeepPositionOnInsert(False) self._cursor2.setKeepPositionOnInsert(False) self._cursor2.insertText(text, format) self._cursor1.setPosition(self._cursor2.position(), A_MOVE) elif prompt == 2 and text == '\b': # Remove prompt (used when closing the kernel) self._cursor1.setPosition(self._cursor2.position(), A_KEEP) self._cursor1.removeSelectedText() self._cursor2.setPosition(self._cursor1.position(), A_MOVE) elif prompt == 2: # Insert text after prompt, inserted text becomes new prompt self._cursor1.setPosition(self._cursor2.position(), A_MOVE) self._cursor1.setKeepPositionOnInsert(True) self._cursor2.setKeepPositionOnInsert(False) self._cursor1.insertText(text, format) # Reset cursor states for the user to type his/her commands self._cursor1.setKeepPositionOnInsert(True) self._cursor2.setKeepPositionOnInsert(True) # Make sure that cursor is visible (only when cursor is at edit line) if not self.isReadOnly(): self.ensureCursorVisible() # Scroll along with the text if lines are popped from the top elif self.blockCount() == MAXBLOCKCOUNT: n = text.count('\n') sb = self.verticalScrollBar() sb.setValue(sb.value() - n)