Application Example

#include "ribbonpopularpage.h"

#include <QPainter>
#include <QHBoxLayout>

/* RibbonPopularPage */
RibbonPopularPage::RibbonPopularPage(MainWindow* mainWindow, QWidget* parent)
    : QWidget(parent)
    , m_mainWindow(mainWindow)
    , m_wasDisplayed(false)
{
    m_pageRes.setupUi(this);
    m_pageRes.labelTitle->setAutoFillBackground(true);
#if 0
    QPalette palette = m_pageRes.labelTitle->palette();
    palette.setColor(QPalette::Window, QColor(238, 238, 238));
    m_pageRes.labelTitle->setPalette(palette);
#endif
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Default"),                 RibbonPopularPage::DefaultStyle);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Fusion"),                  RibbonPopularPage::FusionStyle);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Photoshop LightGray"),     RibbonPopularPage::AdobePhotoshopLightGray);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Photoshop Gray"),          RibbonPopularPage::AdobePhotoshopGray);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Photoshop DarkGray"),      RibbonPopularPage::AdobePhotoshopDarkGray);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Photoshop Black"),         RibbonPopularPage::AdobePhotoshopBlack);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2013 White"),       RibbonPopularPage::Office2013White);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2013 Light Gray"),  RibbonPopularPage::Office2013Gray);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2013 Dark Gray"),   RibbonPopularPage::Office2013Dark);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2016 Colorful"),    RibbonPopularPage::Office2016Colorful);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2016 Dark Gray"),   RibbonPopularPage::Office2016DarkGray);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2016 Black"),       RibbonPopularPage::Office2016Black);
    connect(m_pageRes.comboBoxTheme, SIGNAL(currentIndexChanged(int)), this, SLOT(currentThemeChanged(int)));

    m_accentButton = new Qtitan::ColorPickerButton(Qt::white, m_pageRes.frameAccentColor);
    m_accentButton->setGeometry(QRect(QPoint(0, 0), m_pageRes.frameAccentColor->size()));
    connect(m_accentButton, SIGNAL(colorSelected(const QColor&)), this, SLOT(accentColorSelected(const QColor&)));

    m_pageRes.comboBoxBackground->addItem(tr("None"), QString());
    m_pageRes.comboBoxBackground->addItem(tr("Calligraphy"), QLatin1String("calligraphy"));
    m_pageRes.comboBoxBackground->addItem(tr("Clouds"), QLatin1String("clouds"));
    m_pageRes.comboBoxBackground->addItem(tr("Tree Rings"), QLatin1String("treerings"));
    m_pageRes.comboBoxBackground->addItem(tr("Green Fantasy"), QLatin1String("greenfantasy"));
    m_pageRes.comboBoxBackground->addItem(tr("Autumn Mood"), QLatin1String("autumnmood"));
    connect(m_pageRes.comboBoxBackground, SIGNAL(currentIndexChanged(int)), this, SLOT(currentBackgroundChanged(int)));
    updateEnabledWidgets();
}

RibbonPopularPage::~RibbonPopularPage()
{
}

void RibbonPopularPage::setupPage()
{
    m_currentThemeId = RibbonPopularPage::DefaultStyle;
    if (qApp->style()->objectName() == QLatin1String("fusion"))
    {
        m_currentThemeId = RibbonPopularPage::FusionStyle;
    }
    else if (AdobePhotoshopStyle* adobePhotoshop = qobject_cast<AdobePhotoshopStyle*>(qApp->style()))
    {
        if (adobePhotoshop->theme() == AdobePhotoshopStyle::LightGray)
            m_currentThemeId = RibbonPopularPage::AdobePhotoshopLightGray;
        else if (adobePhotoshop->theme() == AdobePhotoshopStyle::Gray)
            m_currentThemeId = RibbonPopularPage::AdobePhotoshopGray;
        else if (adobePhotoshop->theme() == AdobePhotoshopStyle::DarkGray)
            m_currentThemeId = RibbonPopularPage::AdobePhotoshopDarkGray;
        else if (adobePhotoshop->theme() == AdobePhotoshopStyle::Black)
            m_currentThemeId = RibbonPopularPage::AdobePhotoshopBlack;
    }
    else if (Office2013Style* office2013 = qobject_cast<Office2013Style*>(qApp->style()))
    {
        if (office2013->theme() == Office2013Style::White)
            m_currentThemeId = RibbonPopularPage::Office2013White;
        else if (office2013->theme() == Office2013Style::Gray)
            m_currentThemeId = RibbonPopularPage::Office2013Gray;
        else if (office2013->theme() == Office2013Style::Dark)
            m_currentThemeId = RibbonPopularPage::Office2013Dark;
    }
    else if (Office2016Style* office2016 = qobject_cast<Office2016Style*>(qApp->style()))
    {
        if (office2016->theme() == Office2016Style::Colorful)
            m_currentThemeId = RibbonPopularPage::Office2016Colorful;
        else if (office2016->theme() == Office2016Style::White)
            m_currentThemeId = RibbonPopularPage::Office2016White;
        else if (office2016->theme() == Office2016Style::DarkGray)
            m_currentThemeId = RibbonPopularPage::Office2016DarkGray;
        else if (office2016->theme() == Office2016Style::Black)
            m_currentThemeId = RibbonPopularPage::Office2016Black;
    }
    m_pageRes.comboBoxTheme->setCurrentIndex(m_currentThemeId);

    OfficeStyle* officeStyle = qobject_cast<OfficeStyle*>(qApp->style());
#if (QT_VERSION >= QT_VERSION_CHECK(6, 6, 0))
    m_accentColor = officeStyle != Q_NULL ? officeStyle->accentColor() : qApp->palette().color(QPalette::Accent);
#else
    m_accentColor = officeStyle != Q_NULL ? officeStyle->accentColor() : Qt::white;
#endif
    m_accentButton->setColor(m_accentColor);

    int indexBackground = 0;
    m_backgroundName = m_mainWindow->backgroundName();
    for (int i = 0; i < m_pageRes.comboBoxBackground->count(); ++i)
    {
        if (m_backgroundName == m_pageRes.comboBoxBackground->itemData(i).toString())
        {
            indexBackground = i;
            break;
        }
    }
    m_pageRes.comboBoxBackground->setCurrentIndex(indexBackground);

    updateEnabledWidgets();
}

void RibbonPopularPage::updateEnabledWidgets()
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 6, 0))
    bool isThemeModern = true;
#else
    bool isThemeModern = m_currentThemeId == RibbonPopularPage::Office2013White ||
        m_currentThemeId == RibbonPopularPage::Office2013Gray || m_currentThemeId == RibbonPopularPage::Office2013Dark ||
        m_currentThemeId == RibbonPopularPage::Office2016Colorful || m_currentThemeId == RibbonPopularPage::Office2016DarkGray ||
        m_currentThemeId == RibbonPopularPage::Office2016Black;
#endif
    m_pageRes.labelAccentColor->setEnabled(isThemeModern);
    m_pageRes.frameAccentColor->setEnabled(isThemeModern);
}

void RibbonPopularPage::currentThemeChanged(int index)
{
    QVariant var = m_pageRes.comboBoxTheme->itemData(index);
    if (var.isValid())
        m_currentThemeId = static_cast<RibbonPopularPage::Theme>(var.toUInt());
    updateEnabledWidgets();
}

void RibbonPopularPage::currentBackgroundChanged(int index)
{
    QVariant variant = m_pageRes.comboBoxBackground->itemData(index);
    m_backgroundName = variant.toString();
}

void RibbonPopularPage::accentColorSelected(const QColor& color)
{
    m_accentColor = color;
}

void RibbonPopularPage::accepted()
{
    if (!m_wasDisplayed)
        return;
    QString strNameStyle = QLatin1String("Default");
    switch (m_currentThemeId)
    {
        case RibbonPopularPage::FusionStyle:
            strNameStyle = QLatin1String("Fusion");
            break;
        case RibbonPopularPage::AdobePhotoshopLightGray:
            strNameStyle = QLatin1String("AdobePhotoshopLightGray");
            break;
        case RibbonPopularPage::AdobePhotoshopGray:
            strNameStyle = QLatin1String("AdobePhotoshopGray");
            break;
        case RibbonPopularPage::AdobePhotoshopDarkGray:
            strNameStyle = QLatin1String("AdobePhotoshopDarkGray");
            break;
        case RibbonPopularPage::AdobePhotoshopBlack:
            strNameStyle = QLatin1String("AdobePhotoshopBlack");
            break;
        case RibbonPopularPage::Office2013White:
            strNameStyle = QLatin1String("Office2013White");
            break;
        case RibbonPopularPage::Office2013Gray:
            strNameStyle = QLatin1String("Office2013Gray");
            break;
        case RibbonPopularPage::Office2013Dark:
            strNameStyle = QLatin1String("Office2013Dark");
            break;
        case RibbonPopularPage::Office2016Colorful:
            strNameStyle = QLatin1String("Office2016Colorful");
            break;
        case RibbonPopularPage::Office2016White:
            strNameStyle = QLatin1String("Office2016White");
            break;
        case RibbonPopularPage::Office2016DarkGray:
            strNameStyle = QLatin1String("Office2016DarkGray");
            break;
        case RibbonPopularPage::Office2016Black:
            strNameStyle = QLatin1String("Office2016Black");
            break;
        default:
            break;
    }

    m_mainWindow->setStyleByName(strNameStyle);
    if (OfficeStyle* officeStyle = qobject_cast<OfficeStyle*>(qApp->style()))
        officeStyle->setAccentColor(m_accentColor);
    else
    {
#if (QT_VERSION >= QT_VERSION_CHECK(6, 6, 0))
        QPalette p = qApp->palette();
        p.setColor(QPalette::Accent, m_accentColor);
        qApp->setPalette(p);
#endif
    }
    m_mainWindow->setBackgroundByName(m_backgroundName);
    m_mainWindow->update();
}

void RibbonPopularPage::showEvent(QShowEvent* event)
{
    QWidget::showEvent(event);
    m_wasDisplayed = true;
    setupPage();
}