def __init__(self, text, parent, isVertical=False, margin=SPLITTER_BUTTON_MARGIN): super(QSplitterButton, self).__init__(text, parent) self.rawText = str(text) self.isVertical = isVertical fontSize = "10" if sys.platform == "darwin" else "7" grad = "x1: 0, y1: 0, x2: 0, y2: 1" self.setStyleSheet(""" QPushButton { font-size: """ + fontSize + """pt; color: #151575; padding-bottom: 0px; padding-top: -1px; text-align: bottom; border-radius: 4px; background-color: qlineargradient( """ + grad + """, stop: 0 #e6e7ea, stop: 1 #cacbce);} QPushButton:pressed { background-color: qlineargradient( """ + grad + """, stop: 0 #cacbce, stop: 1 #e6e7ea);} """) myFont = qt.QFont() myFont.setPointSize(int(float(fontSize))) fm = qt.QFontMetrics(myFont) width = fm.width(text) + 2 * margin if isVertical: self.setFixedSize(SPLITTER_WIDTH + 1, width) else: self.setFixedSize(width, SPLITTER_WIDTH + 1)
def __updateMinimumSizeHint(self, text=None): if text is None: text = self.text() font = self.font() metrics = qt.QFontMetrics(font) width = metrics.width( text + " ") # Extra splace to ensure the full text is visible self.setMinimumWidth(width)
def __updateText(self): metrics = qt.QFontMetrics(self.font()) elidedText = metrics.elidedText(self.__text, self.__elideMode, self.width()) qt.QLabel.setText(self, elidedText) wasElided = self.__textIsElided self.__textIsElided = elidedText != self.__text if self.__textIsElided or wasElided != self.__textIsElided: self.__updateToolTip()
def __updateSize(self, *args): """Update widget's maximum size according to bounds""" bottom, top = self.getRange() nbchar = max(len(str(bottom)), len(str(top))) font = self.font() font.setStyle(qt.QFont.StyleItalic) fontMetrics = qt.QFontMetrics(font) self.setMaximumWidth( fontMetrics.boundingRect('0' * (nbchar + 1)).width()) self.setMaxLength(nbchar)
def rasterText(text, font, size=-1, weight=-1, italic=False): """Raster text using Qt. It supports multiple lines. :param str text: The text to raster :param font: Font name or QFont to use :type font: str or :class:`QFont` :param int size: Font size in points Used only if font is given as name. :param int weight: Font weight in [0, 99], see QFont.Weight. Used only if font is given as name. :param bool italic: True for italic font (default: False). Used only if font is given as name. :return: Corresponding image in gray scale and baseline offset from top :rtype: (HxW numpy.ndarray of uint8, int) """ if not text: _logger.info("Trying to raster empty text, replaced by white space") text = ' ' # Replace empty text by white space to produce an image if not isinstance(font, qt.QFont): font = qt.QFont(font, size, weight, italic) metrics = qt.QFontMetrics(font) size = metrics.size(qt.Qt.TextExpandTabs, text) bounds = metrics.boundingRect(qt.QRect(0, 0, size.width(), size.height()), qt.Qt.TextExpandTabs, text) width = bounds.width() + 2 # Add extra border # align line size to 32 bits to ease conversion to numpy array width = 4 * ((width + 3) // 4) image = qt.QImage(width, bounds.height(), qt.QImage.Format_RGB888) # TODO if Qt5 use Format_Grayscale8 instead image.fill(0) # Raster text painter = qt.QPainter() painter.begin(image) painter.setPen(qt.Qt.white) painter.setFont(font) painter.drawText(bounds, qt.Qt.TextExpandTabs, text) painter.end() array = convertQImageToArray(image) # RGB to R array = numpy.ascontiguousarray(array[:, :, 0]) return array, metrics.ascent()
def __updateItem(self): superSelf = super(MenuItem, self) if self.__mode == self.TextMode: superSelf.setText(self.__text) fontMetrics = qt.QFontMetrics(self.font()) width = fontMetrics.width(self.text()) width += self.listWidget().iconSize().width() + 20 superSelf.setSizeHint(qt.QSize(width, 60)) elif self.__mode == self.IconMode: superSelf.setText(None) width = self.listWidget().iconSize().width() + 7 superSelf.setSizeHint(qt.QSize(width, 60)) else: assert (False) if self.__warnings is None: superSelf.setIcon(self.__icon) else: if self.__warningIcon is None: icon = self.__createWarningIcon(self.__icon) self.__warningIcon = icon superSelf.setIcon(self.__warningIcon) if self.__warnings is None and self.__mode == self.TextMode: superSelf.setToolTip("") else: toolTip = self.__text toolTip = "" if self.__mode == self.IconMode: toolTip += self.__text if self.__warnings is not None: if toolTip != "": toolTip += "<br/>" toolTip += self.__warnings superSelf.setToolTip(toolTip)
def __updateText(self): metrics = qt.QFontMetrics(self.font()) elidedText = metrics.elidedText(self.__text, self.__elideMode, self.width()) qt.QLabel.setText(self, elidedText) self.__setValueAsTooltip(elidedText != self.__text)
def __updateMinimumSize(self): metrics = qt.QFontMetrics(self.font()) width = metrics.width("...") self.setMinimumWidth(width)
def _resizeToContent(self, edit, text): # edit = self.sender() fm = qt.QFontMetrics(edit.font()) edit.setFixedWidth(fm.width('['+text+']')) self.adjustSize()
def __init__(self, node=None, parent=None): super(DataTreeView, self).__init__(parent) self.node = node self.plotDimension = 1 if node is None else self.node.plotDimension if csi.model is None: csi.model = DataTreeModel() self.setModel(csi.model) if csi.selectionModel is None: csi.selectionModel = SelectionModel(csi.model) self.setSelectionModel(csi.selectionModel) self.setSelectionMode(qt.QAbstractItemView.ExtendedSelection) self.setCustomSelectionMode() csi.selectionModel.selectionChanged.connect(self.selChanged) self.setHeader(EyeHeader(node=self.node)) horHeaders = self.header() # QHeaderView instance if 'pyqt4' in qt.BINDING.lower(): horHeaders.setMovable(False) horHeaders.setResizeMode(0, qt.QHeaderView.Stretch) horHeaders.setResizeMode(1, qt.QHeaderView.Fixed) else: horHeaders.setSectionsMovable(False) horHeaders.setSectionResizeMode(0, qt.QHeaderView.Stretch) horHeaders.setSectionResizeMode(1, qt.QHeaderView.Fixed) horHeaders.setStretchLastSection(False) horHeaders.setMinimumSectionSize(5) self.setColumnWidth(0, COLUMN_NAME_WIDTH) self.setColumnWidth(1, COLUMN_EYE_WIDTH) if node is not None: totalWidth = 0 leadingColumns = len(csi.modelLeadingColumns) for i, col in enumerate(csi.modelDataColumns): isHidden = col[0] is not node self.setColumnHidden(i + leadingColumns, isHidden) if isHidden: continue fm = qt.QFontMetrics(self.font()) role = col[0].getProp(col[1], 'role') if role.startswith('0'): width = fm.width(col[0].getProp(col[1], 'qLabel')) + 20 else: width = LEGEND_WIDTH totalWidth += width self.setColumnWidth(i + leadingColumns, width) if 'pyqt4' in qt.BINDING.lower(): horHeaders.setResizeMode(i + leadingColumns, qt.QHeaderView.Fixed) else: horHeaders.setSectionResizeMode(i + leadingColumns, qt.QHeaderView.Fixed) lineStyleDelegate = LineStyleDelegate(self) self.setItemDelegateForColumn(i + leadingColumns, lineStyleDelegate) self.setMinimumSize( qt.QSize(COLUMN_NAME_WIDTH + COLUMN_EYE_WIDTH + totalWidth, 100)) self.collapsed.connect(self.collapse) self.expanded.connect(self.expand) # self.setAlternatingRowColors(True) self.setUniformRowHeights(True) self.setDragDropMode(qt.QAbstractItemView.DragDrop) self.isInnerDragNDropAllowed = False self.setDragEnabled(self.isInnerDragNDropAllowed) self.setAcceptDrops(True) self.setDropIndicatorShown(True) # self.setHeaderHidden(True) if "qt4" in qt.BINDING.lower(): horHeaders.setClickable(True) else: horHeaders.setSectionsClickable(True) horHeaders.sectionClicked.connect(self.headerClicked) self.setContextMenuPolicy(qt.Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.onCustomContextMenu) self.setHorizontalScrollBarPolicy(qt.Qt.ScrollBarAlwaysOff) self.makeActions() self.dataChanged()