Application Example

#include "ribbonpopularpage.h"

#include <QPainter>
#include <QColorDialog>

/* RibbonPopularPage */
PreviewColor::PreviewColor(QWidget* parent)
    : QWidget(parent)
{
    resize(parent->size());
}

PreviewColor::~PreviewColor()
{
}

const QColor& PreviewColor::color() const
{
    return m_color;
}

void PreviewColor::setColor(const QColor& color)
{
    if (color != m_color)
    {
        m_color = color;
        repaint();
    }
}

void PreviewColor::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);
    QPainter p(this);
    QStyleOption opt;
    opt.initFrom(this);
    p.fillRect(opt.rect.adjusted(2, 2, -2, -2), QBrush(m_color));
}

/* 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 2007 Blue"),        RibbonPopularPage::Office2007Blue);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2007 Black"),       RibbonPopularPage::Office2007Black);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2007 Aqua"),        RibbonPopularPage::Office2007Aqua);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2007 Silver"),      RibbonPopularPage::Office2007Silver);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Windows 7-like Scenic"),   RibbonPopularPage::Windows7Scenic);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2010 Blue"),        RibbonPopularPage::Office2010Blue);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2010 Silver"),      RibbonPopularPage::Office2010Silver);
    m_pageRes.comboBoxTheme->addItem(QLatin1String("Office 2010 Black"),       RibbonPopularPage::Office2010Black);
    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_previewColor = new PreviewColor(m_pageRes.frameAccentColor);

    m_pageRes.pushButtonAccentColor->setIcon(QIcon(QLatin1String(":/res/arrowdown.png")));
    connect(m_pageRes.pushButtonAccentColor, SIGNAL(pressed()), this, SLOT(showColorDialog()));

    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 (Office2007Style* office2007 = qobject_cast<Office2007Style*>(qApp->style()))
    {
        if (office2007->theme() == Office2007Style::Blue)
            m_currentThemeId = RibbonPopularPage::Office2007Blue;
        else if (office2007->theme() == Office2007Style::Black)
            m_currentThemeId = RibbonPopularPage::Office2007Black;
        else if (office2007->theme() == Office2007Style::Silver)
            m_currentThemeId = RibbonPopularPage::Office2007Silver;
        else if (office2007->theme() == Office2007Style::Aqua)
            m_currentThemeId = RibbonPopularPage::Office2007Aqua;
    }
    else if (qobject_cast<Windows7ScenicStyle*>(qApp->style()))
    {
        m_currentThemeId = RibbonPopularPage::Windows7Scenic;
    }
    else if (Office2010Style* office2010 = qobject_cast<Office2010Style*>(qApp->style()))
    {
        if (office2010->theme() == Office2010Style::Black)
            m_currentThemeId = RibbonPopularPage::Office2010Blue;
        else if (office2010->theme() == Office2010Style::Blue)
            m_currentThemeId = RibbonPopularPage::Office2010Black;
        else if (office2010->theme() == Office2010Style::Silver)
            m_currentThemeId = RibbonPopularPage::Office2010Silver;
    }
    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());
    m_accentColor = officeStyle != Q_NULL ? officeStyle->accentColor() : Qt::black;
    m_previewColor->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()
{
    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;

    m_pageRes.labelAccentColor->setEnabled(isThemeModern);
    m_pageRes.pushButtonAccentColor->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::showColorDialog()
{
    QColor col = QColorDialog::getColor(m_accentColor, this);
    if (!col.isValid())
        return;

    m_accentColor = col;
    m_previewColor->setColor(m_accentColor);
}

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::Office2007Blue:
            strNameStyle = QLatin1String("Office2007Blue");
            break;
        case RibbonPopularPage::Office2007Black:
            strNameStyle = QLatin1String("Office2007Black");
            break;
        case RibbonPopularPage::Office2007Silver:
            strNameStyle = QLatin1String("Office2007Silver");
            break;
        case RibbonPopularPage::Office2007Aqua:
            strNameStyle = QLatin1String("Office2007Aqua");
            break;
        case RibbonPopularPage::Windows7Scenic:
            strNameStyle = QLatin1String("Windows7Scenic");
            break;
        case RibbonPopularPage::Office2010Silver:
            strNameStyle = QLatin1String("Office2010Silver");
            break;
        case RibbonPopularPage::Office2010Blue:
            strNameStyle = QLatin1String("Office2010Blue");
            break;
        case RibbonPopularPage::Office2010Black:
            strNameStyle = QLatin1String("Office2010Black");
            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);
    m_mainWindow->setBackgroundByName(m_backgroundName);
    m_mainWindow->update();
}

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