Esempio n. 1
0
 def on_bridge_ready(self):
     f = QApplication.instance().font()
     fi = QFontInfo(f)
     family = f.family()
     if family in ('.AppleSystemUIFont', 'MS Shell Dlg 2'):
         family = 'system-ui'
     ui_data = {
         'all_font_families':
         QFontDatabase().families(),
         'ui_font_family':
         family,
         'ui_font_sz':
         '{}px'.format(fi.pixelSize()),
         'show_home_page_on_ready':
         self.show_home_page_on_ready,
         'system_colors':
         system_colors(),
         'QT_VERSION':
         QT_VERSION,
         'short_time_fmt':
         QLocale.system().timeFormat(QLocale.FormatType.ShortFormat),
     }
     self.bridge.create_view(vprefs['session_data'],
                             vprefs['local_storage'],
                             field_metadata.all_metadata(), ui_data)
     performance_monitor('bridge ready')
     for func, args in iteritems(self.pending_bridge_ready_actions):
         getattr(self.bridge, func)(*args)
Esempio n. 2
0
    def updateEditorGeometry(self, editor, option, index):
        if editor is None:
            return
        fm = editor.fontMetrics()

        # get the original size of the edit widget
        opt = QStyleOptionViewItem(option)
        self.initStyleOption(opt, index)
        opt.showDecorationSelected = True
        opt.decorationSize = QSize(
            0, 0)  # We want the editor to cover the decoration
        style = QApplication.style()
        initial_geometry = style.subElementRect(
            QStyle.SubElement.SE_ItemViewItemText, opt, None)
        orig_width = initial_geometry.width()

        # Compute the required width: the width that can show all of the current value
        if hasattr(self, 'get_required_width'):
            new_width = self.get_required_width(editor, style, fm)
        else:
            # The line edit box seems to extend by the space consumed by an 'M'.
            # So add that to the text
            text = self.displayText(index.data(Qt.ItemDataRole.DisplayRole),
                                    QLocale()) + 'M'
            srect = style.itemTextRect(fm, editor.geometry(),
                                       Qt.AlignmentFlag.AlignLeft, False, text)
            new_width = srect.width()

        # Now get the size of the combo/spinner arrows and add them to the needed width
        if isinstance(editor, (QComboBox, QDateTimeEdit)):
            r = style.subControlRect(QStyle.ComplexControl.CC_ComboBox,
                                     QStyleOptionComboBox(),
                                     QStyle.SubControl.SC_ComboBoxArrow,
                                     editor)
            new_width += r.width()
        elif isinstance(editor, (QSpinBox, QDoubleSpinBox)):
            r = style.subControlRect(QStyle.ComplexControl.CC_SpinBox,
                                     QStyleOptionSpinBox(),
                                     QStyle.SubControl.SC_SpinBoxUp, editor)
            new_width += r.width()

        # Compute the maximum we can show if we consume the entire viewport
        pin_view = self.table_widget.pin_view
        is_pin_view, p = False, editor.parent()
        while p is not None:
            if p is pin_view:
                is_pin_view = True
                break
            p = p.parent()
        if is_pin_view:
            max_width = pin_view.horizontalScrollBar().geometry().width()
        else:
            view = self.table_widget
            max_width = view.horizontalScrollBar().geometry().width(
            ) - view.verticalHeader().width()
        # What we have to display might not fit. If so, adjust down
        new_width = new_width if new_width < max_width else max_width

        # See if we need to change the editor's geometry
        if new_width <= orig_width:
            delta_x = 0
            delta_width = 0
        else:
            # Compute the space available from the left edge of the widget to
            # the right edge of the displayed table (the viewport) and the left
            # edge of the widget to the left edge of the viewport. These are
            # used to position the edit box
            space_left = initial_geometry.x()
            space_right = max_width - space_left

            if editor.layoutDirection() == Qt.LayoutDirection.RightToLeft:
                # If language is RtL, align to the cell's right edge if possible
                cw = initial_geometry.width()
                consume_on_left = min(space_left, new_width - cw)
                consume_on_right = max(0, new_width - (consume_on_left + cw))
                delta_x = -consume_on_left
                delta_width = consume_on_right
            else:
                # If language is LtR, align to the left if possible
                consume_on_right = min(space_right, new_width)
                consume_on_left = max(0, new_width - consume_on_right)
                delta_x = -consume_on_left
                delta_width = consume_on_right - initial_geometry.width()

        initial_geometry.adjust(delta_x, 0, delta_width, 0)
        editor.setGeometry(initial_geometry)