Application Example

from DevMachines import __pyside2__, __pyside6__

if __pyside2__:
    from PySide2 import QtCore
    from PySide2.QtGui import QIcon, QColor, QPalette
    from PySide2.QtWidgets import QWidget, QColorDialog

if __pyside6__:
    from PySide6 import QtCore
    from PySide6.QtGui import QIcon, QColor, QPalette
    from PySide6.QtWidgets import QWidget, QColorDialog

from ui_ribbonpopularpage import Ui_customizePopularPage as ui

class PreviewColor(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self, parent)
        self.colorval = QColor()
        self.resize(parent.size())

    def color(self):
        return self.colorval;

    def setColor(self, color):
        self.colorval = color
        self.repaint()

    def paintEvent(self, event):
        p = QPainter(self)
        opt = QStyleOption()
        opt.init(self)
        p.fillRect(opt.rect.adjusted(2, 2, -2, -2), QBrush(self.colorval))

class RibbonPopularPage(QWidget):
    def __init__(self, mainWindow, parent = None):
        QWidget.__init__(self, parent)
        self.mainWindow = mainWindow
        self.wasDisplayed = False

        self.pageRes = ui()
        self.pageRes.setupUi(self)
        self.accentColor = QColor()

        self.pageRes.labelTitle.setAutoFillBackground(True)
        pal = self.pageRes.labelTitle.palette()
        pal.setColor(QPalette.Window, QColor(238, 238, 238))
        self.pageRes.labelTitle.setPalette(pal)

        #        Qtitan.RibbonStyle* ribbonStyle = qobject_cast<Qtitan.RibbonStyle*>(qApp.style());
        #        self.currentThemeId = ribbonStyle.getTheme();

        #        self.pageRes.comboBoxTheme.addItem("Office 2007 Blue",        OfficeStyle.Office2007Blue);
        #        self.pageRes.comboBoxTheme.addItem("Office 2007 Black",       OfficeStyle.Office2007Black);
        #        self.pageRes.comboBoxTheme.addItem("Office 2007 Aqua",        OfficeStyle.Office2007Aqua);
        #        self.pageRes.comboBoxTheme.addItem("Office 2007 Silver",      OfficeStyle.Office2007Silver);
        #        self.pageRes.comboBoxTheme.addItem("Windows 7-like Scenic",   OfficeStyle.Windows7Scenic);
        #        self.pageRes.comboBoxTheme.addItem("Office 2010 Blue",        OfficeStyle.Office2010Blue);
        #        self.pageRes.comboBoxTheme.addItem("Office 2010 Silver",      OfficeStyle.Office2010Silver);
        #        self.pageRes.comboBoxTheme.addItem("Office 2010 Black",       OfficeStyle.Office2010Black);
        #        self.pageRes.comboBoxTheme.addItem("Office 2013 White",       OfficeStyle.Office2013White);
        #        self.pageRes.comboBoxTheme.addItem("Office 2013 Light Gray",  OfficeStyle.Office2013Gray);
        #        self.pageRes.comboBoxTheme.addItem("Office 2013 Dark Gray",   OfficeStyle.Office2013Dark);
        #        self.pageRes.comboBoxTheme.addItem("Office 2016 Colorful",    OfficeStyle.Office2016Colorful);
        #        self.pageRes.comboBoxTheme.addItem("Office 2016 Dark Gray",   OfficeStyle.Office2016DarkGray);
        #        self.pageRes.comboBoxTheme.addItem("Office 2016 Black",       OfficeStyle.Office2016Black);
        #        self.pageRes.comboBoxTheme.setCurrentIndex(self.currentThemeId);
        #        connect(self.pageRes.comboBoxTheme, SIGNAL(currentIndexChanged(int)), this, SLOT(currentThemeChanged(int)));
        #        self.accentColor = ribbonStyle.accentColor()

        self.previewColor = PreviewColor(self.pageRes.frameAccentColor)
        self.previewColor.setColor(self.accentColor)

        self.pageRes.pushButtonAccentColor.setIcon(QIcon(":/res/arrowdown.png"))
        self.connect(self.pageRes.pushButtonAccentColor, QtCore.SIGNAL("pressed()"), self.showColorDialog)

        strCalligraphy = ":/res/calligraphy.png"
        strClouds      = ":/res/clouds.png"
        strTreeRings   = ":/res/treerings.png"
        self.pageRes.comboBoxBackground.addItem("None",        "");
        self.pageRes.comboBoxBackground.addItem("Calligraphy", strCalligraphy);
        self.pageRes.comboBoxBackground.addItem("Clouds",      strClouds     );
        self.pageRes.comboBoxBackground.addItem("Tree Rings",  strTreeRings  );
        self.curRibbonBackground =  self.mainWindow.ribbonBackground()
        indexBackground = 0
        if self.curRibbonBackground == strCalligraphy:
            indexBackground = 1
        elif self.curRibbonBackground == strClouds:
            indexBackground = 2
        elif self.curRibbonBackground == strTreeRings:
            indexBackground = 3
        self.pageRes.comboBoxBackground.setCurrentIndex(indexBackground)
        self.connect(self.pageRes.comboBoxBackground, QtCore.SIGNAL("currentIndexChanged(int)"), self.currentBackgroundChanged)
        self.updateEnabledWidgets()

    def setupPage(self):
        #    Qtitan.RibbonStyle* ribbonStyle = qobject_cast<Qtitan.RibbonStyle*>(qApp.style());
        #    Q_ASSERT(ribbonStyle != Q_NULL);
        #    self.currentThemeId = ribbonStyle.getTheme();
        #    self.pageRes.comboBoxTheme.setCurrentIndex(self.currentThemeId);
        return

    def updateEnabledWidgets(self):
        #    bool isThemeModern = self.currentThemeId == OfficeStyle.Office2013White ||
        #        self.currentThemeId == OfficeStyle.Office2013Gray || self.currentThemeId == OfficeStyle.Office2013Dark ||
        #        self.currentThemeId == OfficeStyle.Office2016Colorful || self.currentThemeId == OfficeStyle.Office2016DarkGray ||
        #        self.currentThemeId == OfficeStyle.Office2016Black;

        #    self.pageRes.labelAccentColor.setEnabled(isThemeModern);
        #    self.pageRes.pushButtonAccentColor.setEnabled(isThemeModern);
        #    self.pageRes.labelBackgroundImage.setEnabled(isThemeModern);
        #    self.pageRes.comboBoxBackground.setEnabled(isThemeModern);
        #    self.pageRes.frameAccentColor.setEnabled(isThemeModern);
        return

    @QtCore.Slot(int)
    def currentThemeChanged(self, index):
        val = self.pageRes.comboBoxTheme.itemData(index);
        #    if val.isValid():
        #        self.currentThemeId = RibbonStyle.Theme(val.toUInt())
        self.updateEnabledWidgets()

    @QtCore.Slot(int)
    def currentBackgroundChanged(self, index):
        val = self.pageRes.comboBoxBackground.itemData(index)
        if val.isValid():
            self.curRibbonBackground = val.toString()

    @QtCore.Slot()
    def showColorDialog(self):
        color = QColorDialog.getColor(self.accentColor, self)
        if not color.isValid():
            return
        self.accentColor = color
        self.previewColor.setColor(color)

    @QtCore.Slot()
    def accepted(self):
        if not self.wasDisplayed:
            return

    #    Qtitan.RibbonStyle* ribbonStyle = qobject_cast<Qtitan.RibbonStyle*>(qApp.style());
    #    Q_ASSERT(ribbonStyle != Q_NULL);
    #    ribbonStyle.setTheme(self.currentThemeId);
    #    ribbonStyle.setAccentColor(self.accentColor);
        self.mainWindow.setRibbonBackground(self.curRibbonBackground)

    def showEvent(self, event):
        super(QWidget, self).showEvent(event);
        self.wasDisplayed = True
        self.setupPage()