StyledWidgetsDemo Example

#include <QApplication>
#include <QScreen>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QStyleFactory>
#include <QDockWidget>
#include <QMdiArea>
#include <QTableWidget>
#include <QMdiSubWindow>
#include <QSettings>
#include <QToolButton>
#include <QTextEdit>
#include <QDialog>
#include <QActionGroup>
#include <qmath.h>

#include <aboutdialog.h>
#include "dockwidgets.h"
#include "mainwindow.h"
#include "ui_StyledDialog.h"

/* MainWindow */
MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr("StyledWidgets Demo"));
    m_styleName = qApp->style()->objectName();
    CommonStyle::ensureStyle();
    m_mdiArea = new QMdiArea;
#if 0
    m_mdiArea->setViewMode(QMdiArea::SubWindowView);
#else
    m_mdiArea->setViewMode(QMdiArea::TabbedView);
#endif
    m_mdiArea->setTabsClosable(true);
    setCentralWidget(m_mdiArea);
    setIconSize(QSize(16, 16));

    createActions();
    createDockWindows();
    createMenuBar();
    createToolBar();
    statusBar();
    createMdiChild();

    styleChanged(m_actDefaultStyle);
    readSettings();
}

MainWindow::~MainWindow()
{
}

void MainWindow::closeEvent(QCloseEvent* event)
{
    writeSettings();
    QMainWindow::closeEvent(event);
}

void MainWindow::createActions()
{
    // Group File
    m_actNew = new QAction(QIcon(QStringLiteral(":/res/new.png")), tr("&New"), this);
    m_actNew->setStatusTip(tr("Create a new document"));
    m_actNew->setPriority(QAction::LowPriority);
    m_actNew->setShortcut(QKeySequence::New);
    connect(m_actNew, SIGNAL(triggered()), this, SLOT(createMdiChild()));

    m_actOpen = new QAction(QIcon(QStringLiteral(":/res/open.png")), tr("&Open..."), this);
    m_actOpen->setStatusTip(tr("Open an existing document"));
    m_actOpen->setShortcut(QKeySequence::Open);

    m_actSave = new QAction(QIcon(QStringLiteral(":/res/save.png")), tr("&Save"), this);
    m_actSave->setStatusTip(tr("Save the active document"));
    m_actSave->setShortcut(QKeySequence::Save);

    m_actClose = new QAction(tr("&Close"), this);
    m_actClose->setStatusTip(tr("Close the active document"));

    m_actSaveAs = new QAction(tr("Save &As..."), this);
    m_actSaveAs->setPriority(QAction::LowPriority);
    m_actSaveAs->setStatusTip(tr("Save the active document with a new name"));

    m_actPrint = new QAction(QIcon(QStringLiteral(":/res/print.png")),tr("&Print..."), this);
    m_actPrint->setPriority(QAction::LowPriority);
    m_actPrint->setShortcut(QKeySequence::Print);
    m_actPrint->setStatusTip(tr("Print the active document"));

    m_actPrintPreview = new QAction(QIcon(QStringLiteral(":/res/printpreview.png")),tr("Print Preview..."), this);
    m_actPrintPreview->setStatusTip(tr("Display full pages"));

    m_actExit = new QAction(tr("E&xit"), this);
    m_actExit->setStatusTip(tr("Quit the application; prompts to save documents"));
    connect(m_actExit, SIGNAL(triggered()), this, SLOT(close()));
    // Group styles
    m_groupThemesActions = new QActionGroup(this);
    m_actDefaultStyle = new QAction(tr("Default"), this);
    m_actDefaultStyle->setStatusTip(tr("Default Style"));
    m_actDefaultStyle->setCheckable(true);
    m_actDefaultStyle->setChecked(true);
    m_groupThemesActions->addAction(m_actDefaultStyle);

    m_actFusionStyle = new QAction(tr("Fusion"), this);
    m_actFusionStyle->setStatusTip(tr("Fusion Style"));
    m_actFusionStyle->setCheckable(true);
    m_groupThemesActions->addAction(m_actFusionStyle);

    m_actionAdobeLightGray = new QAction(tr("AdobePhotoshop Light Gray"), this);
    m_actionAdobeLightGray->setStatusTip(tr("AdobePhotoshop Style"));
    m_actionAdobeLightGray->setCheckable(true);
    m_groupThemesActions->addAction(m_actionAdobeLightGray);

    m_actionAdobeGray = new QAction(tr("AdobePhotoshop Gray"), this);
    m_actionAdobeGray->setStatusTip(tr("AdobePhotoshop Style"));
    m_actionAdobeGray->setCheckable(true);
    m_groupThemesActions->addAction(m_actionAdobeGray);

    m_actionAdobeDarkGray = new QAction(tr("AdobePhotoshop Dark Gray"), this);
    m_actionAdobeDarkGray->setStatusTip(tr("AdobePhotoshop Style"));
    m_actionAdobeDarkGray->setCheckable(true);
    m_groupThemesActions->addAction(m_actionAdobeDarkGray);

    m_actionAdobeBlack = new QAction(tr("AdobePhotoshop Black"), this);
    m_actionAdobeBlack->setStatusTip(tr("AdobePhotoshop Style"));
    m_actionAdobeBlack->setCheckable(true);
    m_groupThemesActions->addAction(m_actionAdobeBlack);

    // Group themes
    m_actBlueTheme = new QAction(tr("Office 2007 Blue"), this);
    m_actBlueTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_actBlueTheme);

    m_actBlackTheme = new QAction(tr("Office 2007 Black"), this);
    m_actBlackTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_actBlackTheme);

    m_actSilverTheme = new QAction(tr("Office 2007 Silver"), this);
    m_actSilverTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_actSilverTheme);

    m_actAquaTheme = new QAction(tr("Office 2007 Aqua"), this);
    m_actAquaTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_actAquaTheme);

    m_actScenicTheme = new QAction(tr("Windows 7 Scenic"), this);
    m_actScenicTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_actScenicTheme);

    m_actBlue2010Theme = new QAction(tr("Office 2010 Blue"), this);
    m_actBlue2010Theme->setCheckable(true);
    m_groupThemesActions->addAction(m_actBlue2010Theme);

    m_actSilver2010Theme = new QAction(tr("Office 2010 Silver"), this);
    m_actSilver2010Theme->setCheckable(true);
    m_groupThemesActions->addAction(m_actSilver2010Theme);

    m_actBlack2010Theme = new QAction(tr("Office 2010 Black"), this);
    m_actBlack2010Theme->setCheckable(true);
    m_groupThemesActions->addAction(m_actBlack2010Theme);

    m_act2013WhiteTheme = new QAction(tr("Office 2013 White"), this);
    m_act2013WhiteTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_act2013WhiteTheme);

    m_act2013GrayTheme = new QAction(tr("Office 2013 Gray"), this);
    m_act2013GrayTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_act2013GrayTheme);

    m_act2013DarkTheme = new QAction(tr("Office 2013 Dark"), this);
    m_act2013DarkTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_act2013DarkTheme);

    m_act2016ColorfulTheme = new QAction(tr("Office 2016 Colorful"), this);
    m_act2016ColorfulTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_act2016ColorfulTheme);

    m_act2016WhiteTheme = new QAction(tr("Office 2016 White"), this);
    m_act2016WhiteTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_act2016WhiteTheme);

    m_act2016DarkGrayTheme = new QAction(tr("Office 2016 Dark Gray"), this);
    m_act2016DarkGrayTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_act2016DarkGrayTheme);

    m_act2016BlackTheme = new QAction(tr("Office 2016 Black"), this);
    m_act2016BlackTheme->setCheckable(true);
    m_groupThemesActions->addAction(m_act2016BlackTheme);
    connect(m_groupThemesActions, SIGNAL(triggered(QAction*)), this, SLOT(styleChanged(QAction*)));

    // Group help
    m_actHelp = new QAction(QIcon(QStringLiteral(":/shared/res/about.png")), tr("&About Custom Styles Sample..."), this);
    m_actHelp->setStatusTip(tr("Display program information, version number and copyright"));
    connect(m_actHelp, SIGNAL(triggered()), this, SLOT(about()));
}

void MainWindow::createMenuBar()
{
    QMenuBar* mainMenuBar = menuBar();
    QMenu* menuFile = new QMenu(tr("&File"), this);
    mainMenuBar->addMenu(menuFile);

    menuFile->addAction(m_actNew);
    menuFile->addAction(m_actOpen);
    menuFile->addAction(m_actClose);
    menuFile->addAction(m_actSave);
    menuFile->addAction(m_actSaveAs);

    menuFile->addSeparator();

    menuFile->addAction(m_actPrint);

    menuFile->addAction(m_actPrintPreview);
    menuFile->addSeparator();

    menuFile->addAction(m_actExit);

    m_menuView = new QMenu(tr("&View"), this);
    mainMenuBar->addMenu(m_menuView);
    m_menuView->addAction(m_styleDock->toggleViewAction());
    m_menuView->addAction(m_propDock->toggleViewAction());
    m_menuView->addAction(m_toolBoxDock->toggleViewAction());
    m_menuView->addSeparator();
    m_menuTheme = new QMenu(tr("&Custom Styles"), this);
    m_menuTheme->addAction(m_actDefaultStyle);
    m_menuTheme->addAction(m_actFusionStyle);
    m_menuTheme->addSeparator();
    m_menuTheme->addAction(m_actionAdobeLightGray);
    m_menuTheme->addAction(m_actionAdobeGray);
    m_menuTheme->addAction(m_actionAdobeDarkGray);
    m_menuTheme->addAction(m_actionAdobeBlack);
    m_menuTheme->addSeparator();
    m_menuTheme->addAction(m_actBlueTheme);
    m_menuTheme->addAction(m_actBlackTheme);
    m_menuTheme->addAction(m_actSilverTheme);
    m_menuTheme->addAction(m_actAquaTheme);
    m_menuTheme->addSeparator();
    m_menuTheme->addAction(m_actScenicTheme);
    m_menuTheme->addSeparator();
    m_menuTheme->addAction(m_actBlue2010Theme);
    m_menuTheme->addAction(m_actSilver2010Theme);
    m_menuTheme->addAction(m_actBlack2010Theme);
    m_menuTheme->addSeparator();
    m_menuTheme->addAction(m_act2013WhiteTheme);
    m_menuTheme->addAction(m_act2013GrayTheme);
    m_menuTheme->addAction(m_act2013DarkTheme);
    m_menuTheme->addSeparator();
    m_menuTheme->addAction(m_act2016ColorfulTheme);
    m_menuTheme->addAction(m_act2016WhiteTheme);
    m_menuTheme->addAction(m_act2016DarkGrayTheme);
    m_menuTheme->addAction(m_act2016BlackTheme);

    m_menuView->addMenu(m_menuTheme);
    m_menuView->addSeparator();
    QAction* actStyleDialog = new QAction(tr("Style Dialog..."), this);
    m_menuView->addAction(actStyleDialog);
    connect(actStyleDialog, SIGNAL(triggered()), this, SLOT(createStyleDialog()));

    QMenu* menuHelp = new QMenu(tr("&Help"), this);
    mainMenuBar->addMenu(menuHelp);
    menuHelp->addAction(m_actHelp);
}

void MainWindow::createToolBar()
{
    QToolBar* standardBar = new QToolBar(tr("StandardBar"), this);
    standardBar->setAllowedAreas(Qt::AllToolBarAreas);
    standardBar->addAction(m_actNew);
    standardBar->addAction(m_actOpen);
    standardBar->addAction(m_actSave);
    standardBar->addSeparator();
    standardBar->addAction(m_actPrint);
    standardBar->addAction(m_actPrintPreview);
    addToolBar(standardBar);

    QToolBar* customBar = new QToolBar(tr("Custom Styles"), this);
    customBar->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    customBar->setAllowedAreas(Qt::AllToolBarAreas);
    QAction* actTheme = new QAction(QIcon(QStringLiteral(":/res/customthemes.png")), tr("Custom Styles"), this);
    actTheme->setMenu(m_menuTheme);
    customBar->addAction(actTheme);

    QToolButton* button = qobject_cast<QToolButton*>(customBar->widgetForAction(actTheme));
    Q_ASSERT(button != Q_NULL);
    button->setPopupMode(QToolButton::InstantPopup);
    button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    addToolBar(customBar);

    QToolBar* explorerBar = new QToolBar(tr("Explorer"), this);
    explorerBar->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    explorerBar->setAllowedAreas(Qt::AllToolBarAreas);
    QAction* act = explorerBar->addAction(QIcon(QStringLiteral(":/res/back.png")), tr("Back"));
    act->setStatusTip(tr("Back"));
    act = explorerBar->addAction(QIcon(QStringLiteral(":/res/forward.png")), tr("Forward"));
    act->setStatusTip(tr("Forward"));
    explorerBar->addSeparator();
    act = explorerBar->addAction(QIcon(QStringLiteral(":/res/up.png")), tr("Up"));
    act->setStatusTip(tr("Up"));
    act = explorerBar->addAction(QIcon(QStringLiteral(":/res/viewsearch.png")), tr("Search"));
    act->setStatusTip(tr("Search"));
    act = explorerBar->addAction(QIcon(QStringLiteral(":/res/viewfolders.png")), tr("Folders"));
    act->setStatusTip(tr("Folders"));
    act = explorerBar->addAction(QIcon(QStringLiteral(":/res/viewhistory.png")), tr("History"));
    act->setStatusTip(tr("History"));
    explorerBar->addSeparator();
    act = explorerBar->addAction(QIcon(QStringLiteral(":/res/moveto.png")), tr("Move To"));
    act->setStatusTip(tr("Move To"));
    act = explorerBar->addAction(QIcon(QStringLiteral(":/res/copyto.png")), tr("Copy To"));
    act->setStatusTip(tr("Copy To"));
    act = explorerBar->addAction(QIcon(QStringLiteral(":/res/delete.png")), tr("Delete"));
    act->setStatusTip(tr("Delete"));
    act = explorerBar->addAction(QIcon(QStringLiteral(":/res/undo.png")), tr("Undo"));
    act->setStatusTip(tr("Undo"));
    explorerBar->addSeparator();
    act = explorerBar->addAction(QIcon(QStringLiteral(":/res/views.png")), tr("Views"));
    act->setStatusTip(tr("Views"));
    addToolBar(explorerBar);
}

void MainWindow::createDockWindows()
{
    m_styleDock = new QDockWidget(tr("Styles"), this);
    m_styleDock->setAllowedAreas(Qt::AllDockWidgetAreas);
    m_styleDock->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetVerticalTitleBar);
    m_styleDock->setWidget(new StyleDock());
    addDockWidget(Qt::LeftDockWidgetArea, m_styleDock);

    m_treeDock = new QDockWidget(tr("QTreeView"), this);
    m_treeDock->setAllowedAreas(Qt::AllDockWidgetAreas);
    m_treeDock->setWidget(new TreeDock());
    addDockWidget(Qt::BottomDockWidgetArea, m_treeDock);

    m_toolBoxDock = new QDockWidget(tr("ToolBox"), this);
    m_toolBoxDock->setAllowedAreas(Qt::AllDockWidgetAreas);
    m_toolBoxDock->setWidget(new ToolBoxDock());
    addDockWidget(Qt::LeftDockWidgetArea, m_toolBoxDock);

    m_propDock = new QDockWidget(tr("Modules"), this);
    m_propDock->setAllowedAreas(Qt::AllDockWidgetAreas);
    m_propDock->setWidget(new ModulesDock());
    addDockWidget(Qt::BottomDockWidgetArea, m_propDock);

    m_propDock = new QDockWidget(tr("QTreeWidget"), this);
    m_propDock->setAllowedAreas(Qt::AllDockWidgetAreas);
    m_propDock->setWidget(new TreeBoxDock());
    addDockWidget(Qt::RightDockWidgetArea, m_propDock);
}

void MainWindow::createStyleDialog()
{
    QDialog dlg(this, Qt::WindowCloseButtonHint);
    Ui::StyledDialog uiDialog;
    uiDialog.setupUi(&dlg);

    // QTreeWidget
    QStringList strings;
    strings.append(tr("Item 1"));
    QTreeWidgetItem* item = new QTreeWidgetItem(strings);
    uiDialog.treeWidget->insertTopLevelItem(0, item);
    strings.clear();
    strings.append(tr("Item 2"));
    uiDialog.treeWidget->insertTopLevelItem(1, new QTreeWidgetItem(item, strings));
    strings.clear();
    strings.append(tr("Item 3"));
    uiDialog.treeWidget->insertTopLevelItem(2, new QTreeWidgetItem(item, strings));
    strings.clear();
    strings.append(tr("Item 4"));
    uiDialog.treeWidget->insertTopLevelItem(3, new QTreeWidgetItem(item, strings));
    strings.clear();
    strings.append(tr("Item 5"));
    uiDialog.treeWidget->insertTopLevelItem(4, new QTreeWidgetItem(item, strings));
    strings.clear();
    strings.append(tr("Item 6"));
    uiDialog.treeWidget->insertTopLevelItem(6, new QTreeWidgetItem(item, strings));
    uiDialog.treeWidget->expandItem(item);

    // QtableView
    uiDialog.tableWidget->setColumnCount(2);
    uiDialog.tableWidget->setRowCount(6);
    uiDialog.tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    uiDialog.tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    uiDialog.tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);

    uiDialog.tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem(tr("Col 1")));
    uiDialog.tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem(tr("Col 2")));
    int height = uiDialog.tableWidget->horizontalHeader()->sizeHint().height();
    uiDialog.tableWidget->setRowHeight(0, height);
    uiDialog.tableWidget->setRowHeight(1, height);
    uiDialog.tableWidget->setRowHeight(2, height);
    uiDialog.tableWidget->setRowHeight(3, height);
    uiDialog.tableWidget->setRowHeight(4, height);
    uiDialog.tableWidget->setRowHeight(5, height);
    // QListWidget
    uiDialog.listWidget->insertItem(0, tr("Item 1"));
    uiDialog.listWidget->insertItem(1, tr("Item 2"));
    uiDialog.listWidget->insertItem(2, tr("Item 3"));
    uiDialog.listWidget->insertItem(3, tr("Item 4"));
    uiDialog.listWidget->insertItem(4, tr("Item 5"));
    uiDialog.listWidget->insertItem(5, tr("Item 6"));
    uiDialog.listWidget->setCurrentRow(3);

    uiDialog.comboBox->addItem(tr("Normal"));
    uiDialog.comboBox_2->addItem(tr("Disabled"));
    uiDialog.lineEdit->setText(tr("Normal"));

    QToolButton* button1 = new QToolButton(uiDialog.tab_2);
    button1->setIcon(style()->standardIcon(QStyle::SP_MessageBoxCritical));
    QMenu* menu = new QMenu(uiDialog.tab_2);
    menu->addAction(tr("Test1"));
    menu->addAction(tr("Test2"));
    menu->addAction(tr("Test3"));
    button1->setMenu(menu);
    button1->setPopupMode(QToolButton::InstantPopup);
    button1->setText(tr("Button1"));
    button1->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    button1->move(10, 10);
    button1->resize(button1->sizeHint() + QSize(10, 10));

    QToolButton* button2 = new QToolButton(uiDialog.tab_2);
    button2->setIcon(style()->standardIcon(QStyle::SP_MessageBoxCritical));
    button2->setMenu(menu);
    button2->setPopupMode(QToolButton::DelayedPopup);
    button2->setText(tr("Button2"));
    button2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    button2->move(button1->width() + 20, 10);
    button2->resize(button2->sizeHint() + QSize(10, 10));
    button2->setEnabled(false);

    ToggleSwitch* toggleSwitch = new ToggleSwitch(uiDialog.tab_2);
//    toggleSwitch->setEnabled(false);
//    toggleSwitch->setShape(ToggleSwitch::RoundedSmallGroove);
//    toggleSwitch->setLabelOff(tr("Label Off"));
//    toggleSwitch->setLabelOn(tr("Label On"));
    toggleSwitch->move(button1->width() + button2->width() + 30, 10);
//    toggleSwitch->resize(QSize(68, 30));

#ifdef QTN_CALCULATOR
    QRect rect = button1->geometry();
    QLabel* labCalc = new QLabel("Calculator:", uiDialog.tab_2);
    labCalc->setGeometry(QRect(QPoint(rect.bottomLeft() + QPoint(0, 10)), labCalc->sizeHint()));

    m_calculator = new CalculatorWidget(uiDialog.tab_2);
    rect = labCalc->geometry();
    m_calculator->setGeometry(QRect(QPoint(rect.bottomLeft() + QPoint(0, 6)), QSize(200, 250)));
    connect(m_calculator, SIGNAL(executeAction(int)), this, SLOT(executeAction(int)));
    m_calculator->show();

    QRect rcCalc = m_calculator->geometry();
    QCheckBox* extActions = new QCheckBox(QString("Extended actions"), uiDialog.tab_2);
    extActions->move(QPoint(rcCalc.bottomLeft() + QPoint(0, 10)));
    connect(extActions, SIGNAL(stateChanged(int)), this, SLOT(extActionsOn(int)));
    extActions->show();

    QRect extActionsRect = extActions->geometry();
    QCheckBox* userActions = new QCheckBox(QString("User actions"), uiDialog.tab_2);
    userActions->move(QPoint(extActionsRect.bottomLeft() + QPoint(0, 6)));
    connect(userActions, SIGNAL(stateChanged(int)), this, SLOT(userActionsOn(int)));
    userActions->show();

    rect = button1->geometry();
    QLabel* labCalcBox = new QLabel("Calculator box:", uiDialog.tab_2);
    labCalcBox->setGeometry(QRect(QPoint(QPoint(m_calculator->geometry().right() + 10, rect.bottom()) + QPoint(0, 10)), labCalcBox->sizeHint()));

    CalculatorComboBox* calcBox = new CalculatorComboBox(uiDialog.tab_2);
    calcBox->setLineEdit(new QLineEdit);
    calcBox->move(QPoint(rcCalc.topRight() + QPoint(10, 0)));
    calcBox->resize(QSize(180, calcBox->sizeHint().height()));
    calcBox->show();
#endif
    dlg.exec();
}

#ifdef QTN_CALCULATOR
void MainWindow::executeAction(int keyAction)
{
    double val = m_calculator->value();
    if (keyAction == CalculatorWidget::keyActionUser)
        val = qFabs(val); // Abs
    else if (keyAction == CalculatorWidget::keyActionUser + 1)
        val = qAsin(val);
    else if (keyAction == CalculatorWidget::keyActionUser + 2)
        val = qAcos(val);
    else if (keyAction == CalculatorWidget::keyActionUser + 3)
        val = qAtan(val);
    m_calculator->setValue(val);
}

void MainWindow::extActionsOn(int state)
{
    if (state == Qt::Checked)
    {
        QVector<uint> m_lstExtActions;
        m_lstExtActions.append(CalculatorWidget::keyActionAdvSin);
        m_lstExtActions.append(CalculatorWidget::keyActionAdvCos);
        m_lstExtActions.append(CalculatorWidget::keyActionAdvTan);
        m_lstExtActions.append(CalculatorWidget::keyActionAdvExp);
        m_lstExtActions.append(CalculatorWidget::keyActionAdvX3);
        m_lstExtActions.append(CalculatorWidget::keyActionAdvX2);
        m_lstExtActions.append(CalculatorWidget::keyActionAdvLn);
        m_lstExtActions.append(CalculatorWidget::keyActionAdvLog);
        m_lstExtActions.append(CalculatorWidget::keyActionAdv10x);
        m_lstExtActions.append(CalculatorWidget::keyActionAdvPi);
        m_calculator->addExtendedActions(m_lstExtActions);
    }
    else if (state == Qt::Unchecked)
    {
        m_calculator->removeExtendedActions();
    }
    m_calculator->setValue(0.0);
    m_calculator->setError(false);
}

void MainWindow::userActionsOn(int state)
{
    if (state == Qt::Checked)
    {
        QStringList m_lstUserActions;
        m_lstUserActions.append("abs");
        m_lstUserActions.append("sinh");
        m_lstUserActions.append("cosh");
        m_lstUserActions.append("tanh");
        m_calculator->addUserActions(m_lstUserActions);
    }
    else if (state == Qt::Unchecked)
    {
        m_calculator->removeUserActions();
    }
    m_calculator->setValue(0.0);
    m_calculator->setError(false);
}
#endif

QTextEdit* MainWindow::createMdiChild()
{
    static int sequenceNumber = 1;
    QString curFile = tr("document%1.txt").arg(sequenceNumber++);
    QTextEdit* child = new QTextEdit;
    child->setWindowIcon(QIcon(QStringLiteral(":/res/new.png")));
    child->setWindowTitle(curFile + QStringLiteral("[*]"));

    QMdiSubWindow* subWindow = m_mdiArea->addSubWindow(child);
    subWindow->setWindowState(Qt::WindowMaximized);
    child->show();
    return child;
}

void MainWindow::styleChanged(QAction* action)
{
    QString styleName = qApp->style()->objectName();
    if (m_actDefaultStyle == action && styleName != m_styleName)
        qApp->setStyle(QStyleFactory::create(m_styleName));
    else if (m_actFusionStyle == action && styleName != QStringLiteral("fusion"))
        qApp->setStyle(QStyleFactory::create(QStringLiteral("fusion")));
    else if (m_actionAdobeLightGray == action ||
             m_actionAdobeGray      == action ||
             m_actionAdobeDarkGray  == action ||
             m_actionAdobeBlack == action)
    {
        AdobePhotoshopStyle* style = qobject_cast<AdobePhotoshopStyle*>(qApp->style());
        if (style == Q_NULL)
            style = new AdobePhotoshopStyle();
        if (m_actionAdobeLightGray == action && style->theme() != AdobePhotoshopStyle::LightGray)
            style->setTheme(AdobePhotoshopStyle::LightGray);
        if (m_actionAdobeGray == action && style->theme() != AdobePhotoshopStyle::Gray)
            style->setTheme(AdobePhotoshopStyle::Gray);
        if (m_actionAdobeDarkGray == action && style->theme() != AdobePhotoshopStyle::DarkGray)
            style->setTheme(AdobePhotoshopStyle::DarkGray);
        if (m_actionAdobeBlack == action && style->theme() != AdobePhotoshopStyle::Black)
            style->setTheme(AdobePhotoshopStyle::Black);
        qApp->setStyle(style);
        qApp->style()->setObjectName(QStringLiteral("adobefamily"));
    }
    else if (m_actBlueTheme   == action ||
             m_actBlackTheme  == action ||
             m_actSilverTheme == action ||
             m_actAquaTheme   == action)
    {
        Office2007Style* style = qobject_cast<Office2007Style*>(qApp->style());
        if (style == Q_NULL)
            style = new Office2007Style();
        if (m_actBlueTheme == action && style->theme() != Office2007Style::Blue)
            style->setTheme(Office2007Style::Blue);
        else if (m_actBlackTheme == action && style->theme() != Office2007Style::Black)
            style->setTheme(Office2007Style::Black);
        else if (m_actSilverTheme == action && style->theme() != Office2007Style::Silver)
            style->setTheme(Office2007Style::Silver);
        else if (m_actAquaTheme == action && style->theme() != Office2007Style::Aqua)
            style->setTheme(Office2007Style::Aqua);
        qApp->setStyle(style);
        qApp->style()->setObjectName(QStringLiteral("office2007style"));
    }
    else if (m_actScenicTheme == action)
    {
        Windows7ScenicStyle* style = qobject_cast<Windows7ScenicStyle*>(qApp->style());
        if (style == Q_NULL)
            style = new Windows7ScenicStyle();
        qApp->setStyle(style);
        qApp->style()->setObjectName(QStringLiteral("windows7ScenicStyle"));
    }
    else if (m_actBlue2010Theme == action ||
             m_actSilver2010Theme == action ||
             m_actBlack2010Theme == action)
    {
        Office2010Style* style = qobject_cast<Office2010Style*>(qApp->style());
        if (style == Q_NULL)
            style = new Office2010Style();
        if (m_actBlue2010Theme == action && style->theme() != Office2010Style::Blue)
            style->setTheme(Office2010Style::Blue);
        else if (m_actSilver2010Theme == action && style->theme() != Office2010Style::Silver)
            style->setTheme(Office2010Style::Silver);
        else if (m_actBlack2010Theme == action && style->theme() != Office2010Style::Black)
            style->setTheme(Office2010Style::Black);
        qApp->setStyle(style);
        qApp->style()->setObjectName(QStringLiteral("office2010style"));
    }

    else if (m_act2013WhiteTheme == action ||
             m_act2013GrayTheme  == action ||
             m_act2013DarkTheme  == action)
    {
        Office2013Style* style = qobject_cast<Office2013Style*>(qApp->style());
        if (style == Q_NULL)
            style = new Office2013Style();
        if (m_act2013WhiteTheme == action && style->theme() != Office2013Style::White)
            style->setTheme(Office2013Style::White);
        else if (m_act2013GrayTheme == action && style->theme() != Office2013Style::Gray)
            style->setTheme(Office2013Style::Gray);
        else if (m_act2013DarkTheme == action && style->theme() != Office2013Style::Dark)
            style->setTheme(Office2013Style::Dark);
        qApp->setStyle(style);
        qApp->style()->setObjectName(QStringLiteral("office2013style"));
    }
    else if (m_act2016ColorfulTheme == action ||
             m_act2016WhiteTheme == action    ||
             m_act2016DarkGrayTheme == action ||
             m_act2016BlackTheme == action)
    {
        Office2016Style* style = qobject_cast<Office2016Style*>(qApp->style());
        if (style == Q_NULL)
            style = new Office2016Style();
        if (m_act2016ColorfulTheme == action && style->theme() != Office2016Style::Colorful)
            style->setTheme(Office2016Style::Colorful);
        else if (m_act2016WhiteTheme == action && style->theme() != Office2016Style::White)
            style->setTheme(Office2016Style::White);
        else if (m_act2016DarkGrayTheme == action && style->theme() != Office2016Style::DarkGray)
            style->setTheme(Office2016Style::DarkGray);
        else if (m_act2016BlackTheme == action && style->theme() != Office2016Style::Black)
            style->setTheme(Office2016Style::Black);
        qApp->setStyle(style);
        qApp->style()->setObjectName(QStringLiteral("office2016style"));
    }
    CommonStyle::ensureStyle();

    if (styleName != qApp->style()->objectName())
        QApplication::setPalette(QApplication::style()->standardPalette());
}

void MainWindow::readSettings()
{
    QSettings settings(QStringLiteral("MainWindow"), QStringLiteral("Qtitan MainWindow Sample"));
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    const QRect availableGeometry = screen()->availableGeometry();
    QRect geom = QRect(QPoint((availableGeometry.width() - width()) / 2, (availableGeometry.height() - height()) / 2),
        QSize(2 * availableGeometry.width() / 3, 2 * availableGeometry.height() / 3));
#else
    QRect geom = QRect(QPoint(200, 200), QSize(800, 640));
#endif
    QPoint pos = settings.value(QStringLiteral("pos"), geom.topLeft()).toPoint();
    QSize size = settings.value(QStringLiteral("size"), geom.size()).toSize();
    resize(size);
    move(pos);
}

void MainWindow::writeSettings()
{
    QSettings settings(QStringLiteral("MainWindow"), QStringLiteral("Qtitan MainWindow Sample"));
    settings.setValue(QStringLiteral("pos"), pos());
    settings.setValue(QStringLiteral("size"), size());
}

void MainWindow::about()
{
    AboutDialog::show(this, tr("About Qtitan MainWindow"), tr("OfficeStyle"), QStringLiteral(QTN_VERSION_RIBBON_STR));
}