def __init__(self, parent, ancestor): super().__init__(ancestor) self.textedit = parent self._language = None self.setWindowFlags(Qt.SubWindow | Qt.FramelessWindowHint) self.hide() self.itemActivated.connect(self.item_selected) # self.currentRowChanged.connect(self.row_changed) self.is_internal_console = False self.completion_list = None self.completion_position = None self.automatic = False self.display_index = [] # Setup item rendering self.setItemDelegate(HTMLDelegate(self, margin=3)) self.setMinimumWidth(DEFAULT_COMPLETION_ITEM_WIDTH) # Initial item height and width fm = QFontMetrics(self.textedit.font()) self.item_height = fm.height() self.item_width = self.width() self.setStyleSheet('QListWidget::item:selected {' 'background-color: lightgray;' '}')
def __naturalsh(self): fm = QFontMetrics(self.font()) spacing = self.__spacing N = len(self.__items) width = max((fm.width(text) for text in self.__items), default=0) height = N * fm.height() + (N - 1) * spacing return QSizeF(width, height)
def __naturalsh(self) -> QSizeF: """Return the natural size hint (preferred sh with no constraints).""" fm = QFontMetrics(self.font()) spacing = self.__spacing N = len(self.__items) width = self.__width_for_font(self.font()) height = N * fm.height() + max(N - 1, 0) * spacing return QSizeF(width, height)
def __layout(self): crect = self.contentsRect() spacing = self.__spacing N = len(self.__items) if not N: return fm = QFontMetrics(self.font()) naturalheight = fm.height() th = (crect.height() - (N - 1) * spacing) / N if th > naturalheight and N > 1: th = naturalheight spacing = (crect.height() - N * th) / (N - 1) for i, item in enumerate(self.__textitems): item.setPos(crect.left(), crect.top() + i * (th + spacing))
def paintEvent(self, event): super().paintEvent(event) p = QPainter(self) p.setRenderHint(QPainter.Antialiasing) p.setBrush(self.indicator_color) p.save() p.setPen(Qt.NoPen) fm = QFontMetrics(self.font()) width = self.rect().width() height = fm.height() + 6 rect = QRectF(0, 0, width, height) p.drawRoundedRect(rect, 5, 5) p.restore() textstart = (width - fm.width(self.indicator_text)) / 2 p.drawText(textstart, height / 2 + 5, self.indicator_text)
def __layout(self) -> None: margins = QMarginsF(*self.getContentsMargins()) if self.__orientation == Qt.Horizontal: # transposed margins margins = QMarginsF( margins.bottom(), margins.left(), margins.top(), margins.right() ) crect = self.rect().transposed().marginsRemoved(margins) else: crect = self.rect().marginsRemoved(margins) spacing = self.__spacing align_horizontal = self.__alignment & Qt.AlignHorizontal_Mask align_vertical = self.__alignment & Qt.AlignVertical_Mask if align_vertical == 0: align_vertical = Qt.AlignTop if align_horizontal == 0: align_horizontal = Qt.AlignLeft N = len(self.__items) if not N: return assert self.__group is not None font = self.font() fm = QFontMetrics(font) fontheight = fm.height() # the available vertical space vspace = crect.height() - (N - 1) * spacing cell_height = vspace / N if cell_height > fontheight: # use font height, adjust (widen) spacing. cell_height = fontheight spacing = (crect.height() - N * cell_height) / N elif self.__autoScale: # find a smaller font size to fit the height psize = effective_point_size_for_height(font, cell_height) font.setPointSizeF(psize) fm = QFontMetrics(font) fontheight = fm.height() if self.__autoScale and self.__effectiveFont != font: self.__effectiveFont = font apply_all(self.__textitems, lambda it: it.setFont(font)) advance = cell_height + spacing if align_vertical == Qt.AlignTop: align_dy = 0. elif align_vertical == Qt.AlignVCenter: align_dy = advance / 2.0 - fontheight / 2.0 else: align_dy = advance - fontheight if align_horizontal == Qt.AlignLeft: for i, item in enumerate(self.__textitems): item.setPos(crect.left(), crect.top() + i * advance + align_dy) elif align_horizontal == Qt.AlignHCenter: for i, item in enumerate(self.__textitems): item.setPos( crect.center().x() - item.boundingRect().width() / 2, crect.top() + i * advance + align_dy ) else: for i, item in enumerate(self.__textitems): item.setPos( crect.right() - item.boundingRect().width(), crect.top() + i * advance + align_dy ) if self.__orientation == Qt.Vertical: self.__group.setRotation(0) self.__group.setPos(0, 0) else: self.__group.setRotation(-90) self.__group.setPos(self.rect().bottomLeft())
def __layout(self) -> None: margins = QMarginsF(*self.getContentsMargins()) if self.__orientation == Qt.Horizontal: # transposed margins margins = QMarginsF( margins.bottom(), margins.left(), margins.top(), margins.right() ) crect = self.rect().transposed().marginsRemoved(margins) else: crect = self.rect().marginsRemoved(margins) spacing = self.__spacing align_horizontal = self.__alignment & Qt.AlignHorizontal_Mask align_vertical = self.__alignment & Qt.AlignVertical_Mask if align_vertical == 0: align_vertical = Qt.AlignTop if align_horizontal == 0: align_horizontal = Qt.AlignLeft N = len(self.__items) if not N: return assert self.__group is not None fm = QFontMetrics(self.font()) naturalheight = fm.height() cell_height = (crect.height() - (N - 1) * spacing) / N if cell_height > naturalheight and N > 1: cell_height = naturalheight spacing = (crect.height() - N * cell_height) / N advance = cell_height + spacing if align_vertical == Qt.AlignTop: align_dy = 0. elif align_vertical == Qt.AlignVCenter: align_dy = advance / 2.0 - naturalheight / 2.0 else: align_dy = advance - naturalheight if align_horizontal == Qt.AlignLeft: for i, item in enumerate(self.__textitems): item.setPos(crect.left(), crect.top() + i * advance + align_dy) elif align_horizontal == Qt.AlignHCenter: for i, item in enumerate(self.__textitems): item.setPos( crect.center().x() - item.boundingRect().width() / 2, crect.top() + i * advance + align_dy ) else: for i, item in enumerate(self.__textitems): item.setPos( crect.right() - item.boundingRect().width(), crect.top() + i * advance + align_dy ) if self.__orientation == Qt.Vertical: self.__group.setRotation(0) self.__group.setPos(0, 0) else: self.__group.setRotation(-90) self.__group.setPos(self.rect().bottomLeft())
def minimumSizeHint(self): fm = QFontMetrics(self.font()) width = round(fm.width(self.indicator_text)) + 10 height = fm.height() + 6 return QSize(width, height)
def minimumSizeHint(self): fm = QFontMetrics(self.font()) width = int(round(fm.horizontalAdvance(self.indicator_text)) + 10) height = fm.height() + 6 return QSize(width, height)
def setup_appearance(self, size, font): """Setup size and font of the completion widget.""" self.resize(*size) self.setFont(font) fm = QFontMetrics(font) self.item_height = fm.height()