def __init__(self,
                 args,
                 bk,
                 app_icon=None,
                 match_fonts=True,
                 match_highdpi=True,
                 match_dark_palette=False,
                 match_whats_this=True,
                 dont_use_native_menubars=False,
                 load_qtbase_translations=True,
                 load_qtplugin_translations=True,
                 plugin_trans_folder=None):

        # Keep menubars in the application windows on all platforms
        if dont_use_native_menubars:
            self.setAttribute(Qt.AA_DontUseNativeMenuBar)

        self.bk = bk
        program_name = '{}'.format(bk._w.plugin_name)
        if plugin_trans_folder is None:
            plugin_trans_folder = os.path.join(self.bk._w.plugin_dir,
                                               self.bk._w.plugin_name,
                                               'translations')

        # Match Sigil highdpi settings if necessary and if available
        if tuple_version(qVersion()) < (6, 0, 0):
            self.setAttribute(Qt.AA_UseHighDpiPixmaps)
        if match_highdpi:
            self.match_sigil_highdpi()

        # Initialize the QApplication to be used by the plugin
        args = [program_name] + args[1:]
        QtWidgets.QApplication.__init__(self, args)

        # set the app icon (used by all child windows)
        if app_icon is not None:
            self.setWindowIcon(QtGui.QIcon(app_icon))
            if iswindows:
                ensure_windows_taskbar_icon()

        # Match Sigil's dark palette if possible and/or wanted
        if match_dark_palette:
            self.match_sigil_darkmode()

        # Sigil disables the little unused question mark context button - so does Python
        if match_whats_this:
            if tuple_version(qVersion()) >= (5, 10, 0) and tuple_version(
                    qVersion()) < (5, 15, 0):
                self.setAttribute(Qt.AA_DisableWindowContextHelpButton)

        # Load Qt base dialog translations if available
        if load_qtbase_translations:
            self.load_base_qt_translations()
        # Load plugin dialog translations if available
        if load_qtplugin_translations:
            self.load_plugin_translations(plugin_trans_folder)

        # Match Sigil UI font if possible and/or wanted
        if match_fonts:
            self.match_sigil_font()
Пример #2
0
    def paintEvent(self, event):
        rect = QRect(10, 20, 80, 60)

        path = QPainterPath()
        path.moveTo(20, 80)
        path.lineTo(20, 30)
        path.cubicTo(80, 0, 50, 50, 80, 80)

        startAngle = 30 * 16
        arcLength = 120 * 16

        painter = QPainter(self)
        painter.setPen(self.pen)
        painter.setBrush(self.brush)
        if self.antialiased:
            painter.setRenderHint(QPainter.Antialiasing)

        for x in range(0, self.width(), 100):
            for y in range(0, self.height(), 100):
                painter.save()
                painter.translate(x, y)
                if self.transformed:
                    painter.translate(50, 50)
                    painter.rotate(60.0)
                    painter.scale(0.6, 0.9)
                    painter.translate(-50, -50)

                if self.shape == RenderArea.Line:
                    painter.drawLine(rect.bottomLeft(), rect.topRight())
                elif self.shape == RenderArea.Points:
                    painter.drawPoints(RenderArea.points)
                elif self.shape == RenderArea.Polyline:
                    painter.drawPolyline(RenderArea.points)
                elif self.shape == RenderArea.Polygon:
                    painter.drawPolygon(RenderArea.points)
                elif self.shape == RenderArea.Rect:
                    painter.drawRect(rect)
                elif self.shape == RenderArea.RoundedRect:
                    painter.drawRoundedRect(rect, 25, 25, Qt.RelativeSize)
                elif self.shape == RenderArea.Ellipse:
                    painter.drawEllipse(rect)
                elif self.shape == RenderArea.Arc:
                    painter.drawArc(rect, startAngle, arcLength)
                elif self.shape == RenderArea.Chord:
                    painter.drawChord(rect, startAngle, arcLength)
                elif self.shape == RenderArea.Pie:
                    painter.drawPie(rect, startAngle, arcLength)
                elif self.shape == RenderArea.Path:
                    painter.drawPath(path)
                elif self.shape == RenderArea.Text:
                    painter.drawText(rect, Qt.AlignCenter,
                                     "PySide 2\nQt %s" % qVersion())
                elif self.shape == RenderArea.Pixmap:
                    painter.drawPixmap(10, 10, self.pixmap)

                painter.restore()

        painter.setPen(self.palette().dark().color())
        painter.setBrush(Qt.NoBrush)
        painter.drawRect(QRect(0, 0, self.width() - 1, self.height() - 1))
Пример #3
0
def get_pyside_version_from_import():
    """Determine the exact Qt version by importing."""
    qversion_string = None
    try:
        from PySide6.QtCore import qVersion
        qversion_string = qVersion()
    except ImportError:
        try:
            from PySide2.QtCore import qVersion
            qversion_string = qVersion()
        except ImportError:
            print('Unable to determine PySide version; could not import any version',
                  file=sys.stderr)
    if qversion_string:
        major, minor, patch = qVersion().split('.')
        return int(major), int(minor), int(patch)
    return 0, 0, 0
 def match_sigil_highdpi(self):
     if not (self.bk.launcher_version() >= 20200326):  # Sigil 1.2.0
         print('Highdpi-matching not available before Sigil 1.2.0')
         return
     # macos handles highdpi in both Qt5 and Qt6
     if not ismacos:
         # Linux and Windows don't need to do anything to enable highdpi after Qt6
         if tuple_version(qVersion()) < (6, 0, 0):
             try:
                 self._setup_highdpi_(self.bk._w.highdpi)
             except Exception:
                 pass
def help_url(page):
    """Build a Qt help URL from the page name"""
    major_version = qVersion().split('.')[0]
    return "https://doc.qt.io/qt-{}/{}.html".format(major_version, page)
    def __init__(self):
        super(WidgetGallery, self).__init__()

        self.setWindowIcon(QIcon(':/qt-project.org/logos/pysidelogo.png'))
        self._progress_bar = self.create_progress_bar()

        self._style_combobox = QComboBox()
        init_widget(self._style_combobox, "styleComboBox")
        self._style_combobox.addItems(style_names())

        style_label = QLabel("Style:")
        init_widget(style_label, "style_label")
        style_label.setBuddy(self._style_combobox)

        help_label = QLabel("Press F1 over a widget to see Documentation")
        init_widget(help_label, "help_label")

        disable_widgets_checkbox = QCheckBox("Disable widgets")
        init_widget(disable_widgets_checkbox, "disable_widgets_checkbox")

        buttons_groupbox = self.create_buttons_groupbox()
        itemview_tabwidget = self.create_itemview_tabwidget()
        simple_input_widgets_groupbox = self.create_simple_inputwidgets_groupbox(
        )
        text_toolbox = self.create_text_toolbox()

        self._style_combobox.textActivated.connect(self.change_style)
        disable_widgets_checkbox.toggled.connect(buttons_groupbox.setDisabled)
        disable_widgets_checkbox.toggled.connect(text_toolbox.setDisabled)
        disable_widgets_checkbox.toggled.connect(
            itemview_tabwidget.setDisabled)
        disable_widgets_checkbox.toggled.connect(
            simple_input_widgets_groupbox.setDisabled)

        help_shortcut = QShortcut(self)
        help_shortcut.setKey(QKeySequence.HelpContents)
        help_shortcut.activated.connect(self.help_on_current_widget)

        top_layout = QHBoxLayout()
        top_layout.addWidget(style_label)
        top_layout.addWidget(self._style_combobox)
        top_layout.addStretch(1)
        top_layout.addWidget(help_label)
        top_layout.addStretch(1)
        top_layout.addWidget(disable_widgets_checkbox)

        dialog_buttonbox = QDialogButtonBox(QDialogButtonBox.Help
                                            | QDialogButtonBox.Close)
        init_widget(dialog_buttonbox, "dialogButtonBox")
        dialog_buttonbox.helpRequested.connect(launch_module_help)
        dialog_buttonbox.rejected.connect(self.reject)

        main_layout = QGridLayout(self)
        main_layout.addLayout(top_layout, 0, 0, 1, 2)
        main_layout.addWidget(buttons_groupbox, 1, 0)
        main_layout.addWidget(simple_input_widgets_groupbox, 1, 1)
        main_layout.addWidget(itemview_tabwidget, 2, 0)
        main_layout.addWidget(text_toolbox, 2, 1)
        main_layout.addWidget(self._progress_bar, 3, 0, 1, 2)
        main_layout.addWidget(dialog_buttonbox, 4, 0, 1, 2)

        self.setWindowTitle("Widget Gallery Qt {}".format(qVersion()))
    except ImportError:
        print('QtWebEngine PyQt5 Python bindings not found.')
        print(
            'If this plugin needs QtWebEngine make sure those bindings are installed'
        )
        print('(or use Sigil 1.6 or newer, which has it bundled).')
        pass
    else:
        if DEBUG:
            print('QtWebEngine PyQt5 Python bindings found.')

    from PyQt5.QtCore import Qt, pyqtSignal as Signal, pyqtSlot as Slot, qVersion  # noqa: F401
    from PyQt5.QtWidgets import QAction, QActionGroup  # noqa: F401
    from PyQt5 import uic  # noqa: F401

PLUGIN_QT_MAJOR_VERSION = tuple(map(int, (qVersion().split("."))))[0]

# Function alias used to surround translatable strings
_t = QtCore.QCoreApplication.translate

if DEBUG:
    if 'PySide6' in sys.modules:
        print('plugin_utilities is using PySide6')
    else:
        print('plugin_utilities is using PyQt5')
    print('Sigil Qt: ', os.environ.get('SIGIL_QT_RUNTIME_VERSION'))
    print('Sigil Qt major version: ', SIGIL_QT_MAJOR_VERSION)

_plat = sys.platform.lower()
iswindows = 'win32' in _plat or 'win64' in _plat
ismacos = isosx = 'darwin' in _plat