Python qtpy.QtGui.QFontMetrics() Examples

The following are 9 code examples of qtpy.QtGui.QFontMetrics(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module qtpy.QtGui , or try the search function .
Example #1
Source File: qt_range_slider_popup.py    From napari with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, value='', parent=None, get_pos=None):
        super().__init__(value, parent=parent)
        self.fm = QFontMetrics(QFont("", 0))
        self.setObjectName('slice_label')
        self.min_width = 30
        self.max_width = 200
        self.setCursor(Qt.IBeamCursor)
        self.setValidator(QDoubleValidator())
        self.textChanged.connect(self._on_text_changed)
        self._on_text_changed(value)

        self.get_pos = get_pos
        if parent is not None:
            self.min_width = 50
            self.slider = parent.slider
            self.setAlignment(Qt.AlignCenter) 
Example #2
Source File: qt_dims.py    From napari with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _resize_axis_labels(self):
        """When any of the labels get updated, this method updates all label
        widths to the width of the longest label. This keeps the sliders
        left-aligned and allows the full label to be visible at all times,
        with minimal space, without setting stretch on the layout.
        """
        fm = QFontMetrics(QFont("", 0))
        labels = self.findChildren(QLineEdit, 'axis_label')
        newwidth = max([fm.boundingRect(lab.text()).width() for lab in labels])

        if any(self._displayed_sliders):
            # set maximum width to no more than 20% of slider width
            maxwidth = self.slider_widgets[0].width() * 0.2
            newwidth = min([newwidth, maxwidth])
        for labl in labels:
            labl.setFixedWidth(newwidth + 10) 
Example #3
Source File: qt_dims.py    From napari with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _resize_slice_labels(self):
        """When the size of any dimension changes, we want to resize all of the
        slice labels to width of the longest label, to keep all the sliders
        right aligned.  The width is determined by the number of digits in the
        largest dimensions, plus a little padding.
        """
        width = 0
        for ax, maxi in enumerate(self.dims.max_indices):
            if self._displayed_sliders[ax]:
                length = len(str(int(maxi)))
                if length > width:
                    width = length
        # gui width of a string of length `width`
        fm = QFontMetrics(QFont("", 0))
        width = fm.boundingRect("8" * width).width()
        for labl in self.findChildren(QWidget, 'slice_label'):
            labl.setFixedWidth(width + 6) 
Example #4
Source File: console_widget.py    From qtconsole with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _format_as_columns(self, items, separator='  '):
        """ Transform a list of strings into a single string with columns.

        Parameters
        ----------
        items : sequence of strings
            The strings to process.

        separator : str, optional [default is two spaces]
            The string that separates columns.

        Returns
        -------
        The formatted string.
        """
        # Calculate the number of characters available.
        width = self._control.document().textWidth()
        char_width = QtGui.QFontMetrics(self.font).boundingRect(' ').width()
        displaywidth = max(10, (width / char_width) - 1)

        return columnize(items, separator, displaywidth) 
Example #5
Source File: console_widget.py    From qtconsole with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def sizeHint(self):
        """ Reimplemented to suggest a size that is 80 characters wide and
            25 lines high.
        """
        font_metrics = QtGui.QFontMetrics(self.font)
        margin = (self._control.frameWidth() +
                  self._control.document().documentMargin()) * 2
        style = self.style()
        splitwidth = style.pixelMetric(QtWidgets.QStyle.PM_SplitterWidth)

        # Note 1: Despite my best efforts to take the various margins into
        # account, the width is still coming out a bit too small, so we include
        # a fudge factor of one character here.
        # Note 2: QFontMetrics.maxWidth is not used here or anywhere else due
        # to a Qt bug on certain Mac OS systems where it returns 0.
        width = font_metrics.boundingRect(' ').width() * self.console_width + margin
        width += style.pixelMetric(QtWidgets.QStyle.PM_ScrollBarExtent)

        if self.paging == 'hsplit':
            width = width * 2 + splitwidth

        height = font_metrics.height() * self.console_height + margin
        if self.paging == 'vsplit':
            height = height * 2 + splitwidth

        return QtCore.QSize(int(width), int(height))

    #---------------------------------------------------------------------------
    # 'ConsoleWidget' public interface
    #--------------------------------------------------------------------------- 
Example #6
Source File: console_widget.py    From qtconsole with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _set_font(self, font):
        """ Sets the base font for the ConsoleWidget to the specified QFont.
        """
        font_metrics = QtGui.QFontMetrics(font)
        self._control.setTabStopWidth(
            self.tab_width * font_metrics.boundingRect(' ').width()
        )

        self._completion_widget.setFont(font)
        self._control.document().setDefaultFont(font)
        if self._page_control:
            self._page_control.document().setDefaultFont(font)

        self.font_changed.emit(font) 
Example #7
Source File: console_widget.py    From qtconsole with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _set_tab_width(self, tab_width):
        """ Sets the width (in terms of space characters) for tab characters.
        """
        font_metrics = QtGui.QFontMetrics(self.font)
        self._control.setTabStopWidth(tab_width * font_metrics.boundingRect(' ').width())

        self._tab_width = tab_width 
Example #8
Source File: console_widget.py    From qtconsole with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _page(self, text, html=False):
        """ Displays text using the pager if it exceeds the height of the
        viewport.

        Parameters
        ----------
        html : bool, optional (default False)
            If set, the text will be interpreted as HTML instead of plain text.
        """
        line_height = QtGui.QFontMetrics(self.font).height()
        minlines = self._control.viewport().height() / line_height
        if self.paging != 'none' and \
                re.match("(?:[^\n]*\n){%i}" % minlines, text):
            if self.paging == 'custom':
                self.custom_page_requested.emit(text)
            else:
                # disable buffer truncation during paging
                self._control.document().setMaximumBlockCount(0)
                self._page_control.clear()
                cursor = self._page_control.textCursor()
                if html:
                    self._insert_html(cursor, text)
                else:
                    self._insert_plain_text(cursor, text)
                self._page_control.moveCursor(QtGui.QTextCursor.Start)

                self._page_control.viewport().resize(self._control.size())
                if self._splitter:
                    self._page_control.show()
                    self._page_control.setFocus()
                else:
                    self.layout().setCurrentWidget(self._page_control)
        elif html:
            self._append_html(text)
        else:
            self._append_plain_text(text) 
Example #9
Source File: spinbox.py    From pyrpl with GNU General Public License v3.0 5 votes vote down vote up
def set_min_size(self):
        """
        sets the min size for content to fit.
        """
        font = QtGui.QFont("", 0)
        font_metric = QtGui.QFontMetrics(font)
        pixel_wide = font_metric.width("0"*self.max_num_letter)
        self.line.setFixedWidth(pixel_wide)