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 <QVBoxLayout>
#include <QCommandLinkButton>
#include <QPainter>
#include <qmath.h>
#include <QScrollArea>
#include <aboutdialog.h>
#include "dockwidgets.h"
#include "mainwindow.h"
#include "ui_StyledDialog.h"
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent), m_styleWindow(Q_NULL)
{
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()
{
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()));
m_groupThemesActions = new QActionGroup(this);
connect(m_groupThemesActions, SIGNAL(triggered(QAction*)), this, SLOT(styleChanged(QAction*)));
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);
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);
m_DCStyleLight = new QAction(tr("DCStyle Light"), this);
m_DCStyleLight->setCheckable(true);
m_groupThemesActions->addAction(m_DCStyleLight);
m_DCStyleDark = new QAction(tr("DCStyle Dark"), this);
m_DCStyleDark->setCheckable(true);
m_groupThemesActions->addAction(m_DCStyleDark);
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_menuTheme->addSeparator();
m_menuTheme->addAction(m_DCStyleLight);
m_menuTheme->addAction(m_DCStyleDark);
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()));
QAction* formAct = new QAction(tr("Style Window..."), this);
m_menuView->addAction(formAct);
connect(formAct, SIGNAL(triggered()), this, SLOT(showStyleWindow()));
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::showStyleWindow()
{
if (m_styleWindow == Q_NULLPTR)
m_styleWindow = new StyleWindow(this);
m_styleWindow->show();
}
void MainWindow::createStyleDialog()
{
QDialog dlg(this, Qt::WindowCloseButtonHint);
Ui::StyledDialog uiDialog;
uiDialog.setupUi(&dlg);
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);
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);
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->move(button1->width() + button2->width() + 30, 10);
#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);
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"));
}
#ifdef QTN_DC_STYLE
else if (action == m_DCStyleLight || action == m_DCStyleDark)
{
DCStyle* style = qobject_cast<DCStyle*>(qApp->style());
if (style == Q_NULL)
style = new DCStyle();
style->setTheme(action == m_DCStyleLight ? QLatin1String("Light Theme") : QLatin1String("Dark Theme"));
qApp->setStyle(style);
}
#endif
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_STYLE_STR));
}
StyleWindow::StyleWindow(QWidget* parent)
: QMainWindow(parent), m_forms(Q_NULL)
{
m_forms = new QTabWidget(this);
m_forms->addTab(createPaletteForm(), tr("Palette"));
m_forms->addTab(createIconsForm(), tr("Icons"));
m_forms->addTab(createButtonsForm(), tr("Buttons"));
setCentralWidget(m_forms);
m_forms->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
}
class PaletteColorItem: public QWidget
{
public:
PaletteColorItem(QPalette::ColorGroup group, QPalette::ColorRole role)
: QWidget(), m_group(group), m_role(role) {}
QSize sizeHint() const override
{
return QSize(70, 30);
}
protected:
void paintEvent(QPaintEvent* event) override
{
Q_UNUSED(event);
QPainter painter(this);
painter.fillRect(rect(), palette().color(m_group, m_role));
}
private:
QPalette::ColorGroup m_group;
QPalette::ColorRole m_role;
};
class StandardPixmapItem : public QWidget
{
public:
StandardPixmapItem(QStyle::StandardPixmap pixmap, QStyle::PixelMetric size, QIcon::Mode mode, QIcon::State state)
: QWidget(), m_pixmap(pixmap), m_size(size), m_mode(mode), m_state(state) {}
QSize sizeHint() const override
{
int val = style()->pixelMetric(m_size, Q_NULL, Q_NULL);
return QSize(val, val);
}
protected:
void paintEvent(QPaintEvent* event) override
{
Q_UNUSED(event);
QPainter painter(this);
QIcon icon = style()->standardIcon(m_pixmap, Q_NULL, Q_NULL);
icon.paint(&painter, rect(), Qt::AlignCenter, m_mode, m_state);
}
private:
QStyle::StandardPixmap m_pixmap;
QStyle::PixelMetric m_size;
QIcon::Mode m_mode;
QIcon::State m_state;
};
class PaletteForm : public QWidget
{
public:
PaletteForm(QWidget* parent = Q_NULL)
: QWidget(parent)
{
QGridLayout* layout = new QGridLayout(this);
layout->setSizeConstraint(QLayout::SetFixedSize);
QLabel* activeLabel = new QLabel(tr("Active"));
QLabel* disabledLabel = new QLabel(tr("Disabled"));
QLabel* inactiveLabel = new QLabel(tr("Inactive"));
int row = 0;
QSpacerItem* spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding);
layout->addItem(spacer, row, 0);
layout->addWidget(activeLabel, row, 1);
layout->addWidget(disabledLabel, row, 2);
layout->addWidget(inactiveLabel, row, 3);
++row;
for (int i = 0; i < QPalette::NColorRoles; ++i)
{
QLabel* roleName = new QLabel();
roleName->setText(getRoleName(QPalette::ColorRole(i)));
layout->addWidget(roleName, row, 0);
PaletteColorItem* activeItem = new PaletteColorItem(QPalette::Active, QPalette::ColorRole(i));
PaletteColorItem* disabledItem = new PaletteColorItem(QPalette::Disabled, QPalette::ColorRole(i));
PaletteColorItem* inactiveItem = new PaletteColorItem(QPalette::Inactive, QPalette::ColorRole(i));
layout->addWidget(activeItem, row, 1);
layout->addWidget(disabledItem, row, 2);
layout->addWidget(inactiveItem, row, 3);
++row;
}
}
protected:
QString getRoleName(QPalette::ColorRole role) {
switch (role)
{
case QPalette::WindowText: return QLatin1String("WindowText");
case QPalette::Button: return QLatin1String("Button");
case QPalette::Light: return QLatin1String("Light");
case QPalette::Midlight: return QLatin1String("Midlight");
case QPalette::Dark: return QLatin1String("Dark");
case QPalette::Mid: return QLatin1String("Mid");
case QPalette::Text: return QLatin1String("Text");
case QPalette::BrightText: return QLatin1String("BrightText");
case QPalette::ButtonText: return QLatin1String("ButtonText");
case QPalette::Base: return QLatin1String("Base");
case QPalette::Window: return QLatin1String("Window");
case QPalette::Shadow: return QLatin1String("Shadow");
case QPalette::Highlight: return QLatin1String("Highlight");
case QPalette::HighlightedText: return QLatin1String("HighlightedText");
case QPalette::Link: return QLatin1String("Link");
case QPalette::LinkVisited: return QLatin1String("LinkVisited");
case QPalette::AlternateBase: return QLatin1String("AlternateBase");
case QPalette::NoRole: return QLatin1String("NoRole");
case QPalette::ToolTipBase: return QLatin1String("ToolTipBase");
case QPalette::ToolTipText: return QLatin1String("ToolTipText");
case QPalette::PlaceholderText: return QLatin1String("PlaceholderText");
default:
Q_ASSERT(false);
}
return QString();
}
private:
};
class IconsForm : public QScrollArea
{
public:
IconsForm(QWidget* parent = Q_NULL)
: QScrollArea(parent)
{
QWidget* view = new QWidget();
QGridLayout* layout = new QGridLayout(view);
layout->setSizeConstraint(QLayout::SetFixedSize);
QLabel* normalLabel = new QLabel(tr("Normal"));
QLabel* activeLabel = new QLabel(tr("Active"));
QLabel* disabledLabel = new QLabel(tr("Disabled"));
QLabel* selectedLabel = new QLabel(tr("Selected"));
int row = 0;
QSpacerItem* spacer = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Expanding);
layout->addItem(spacer, row, 0);
layout->addWidget(normalLabel, row, 1);
layout->addWidget(activeLabel, row, 2);
layout->addWidget(disabledLabel, row, 3);
layout->addWidget(selectedLabel, row, 4);
++row;
#if (QT_VERSION > QT_VERSION_CHECK(6, 0, 0))
for (int i = 0; i < QStyle::StandardPixmap::NStandardPixmap; ++i)
#else
for (int i = 0; i <= QStyle::StandardPixmap::SP_RestoreDefaultsButton; ++i)
#endif
{
QLabel* pixmapName = new QLabel();
pixmapName->setText(getIconName(QStyle::StandardPixmap(i)));
layout->addWidget(pixmapName, row, 0);
StandardPixmapItem* normalItem = new StandardPixmapItem(QStyle::StandardPixmap(i), QStyle::PM_LargeIconSize, QIcon::Normal, QIcon::On);
StandardPixmapItem* activeItem = new StandardPixmapItem(QStyle::StandardPixmap(i), QStyle::PM_LargeIconSize, QIcon::Active, QIcon::On);
StandardPixmapItem* disabledItem = new StandardPixmapItem(QStyle::StandardPixmap(i), QStyle::PM_LargeIconSize, QIcon::Disabled, QIcon::On);
StandardPixmapItem* selectedItem = new StandardPixmapItem(QStyle::StandardPixmap(i), QStyle::PM_LargeIconSize, QIcon::Selected, QIcon::On);
layout->addWidget(normalItem, row, 1);
layout->addWidget(activeItem, row, 2);
layout->addWidget(disabledItem, row, 3);
layout->addWidget(selectedItem, row, 4);
++row;
}
setWidget(view);
}
protected:
private:
QString getIconName(QStyle::StandardPixmap pixmap) {
switch (pixmap)
{
case QStyle::SP_TitleBarMenuButton: return QLatin1String("SP_TitleBarMenuButton");
case QStyle::SP_TitleBarMinButton: return QLatin1String("SP_TitleBarMinButton");
case QStyle::SP_TitleBarMaxButton: return QLatin1String("SP_TitleBarMaxButton");
case QStyle::SP_TitleBarCloseButton: return QLatin1String("SP_TitleBarCloseButton");
case QStyle::SP_TitleBarNormalButton: return QLatin1String("SP_TitleBarNormalButton");
case QStyle::SP_TitleBarShadeButton: return QLatin1String("SP_TitleBarShadeButton");
case QStyle::SP_TitleBarUnshadeButton: return QLatin1String("SP_TitleBarUnshadeButton");
case QStyle::SP_TitleBarContextHelpButton: return QLatin1String("SP_TitleBarContextHelpButton");
case QStyle::SP_DockWidgetCloseButton: return QLatin1String("SP_DockWidgetCloseButton");
case QStyle::SP_MessageBoxInformation: return QLatin1String("SP_MessageBoxInformation");
case QStyle::SP_MessageBoxWarning: return QLatin1String("SP_MessageBoxWarning");
case QStyle::SP_MessageBoxCritical: return QLatin1String("SP_MessageBoxCritical");
case QStyle::SP_MessageBoxQuestion: return QLatin1String("SP_MessageBoxQuestion");
case QStyle::SP_DesktopIcon: return QLatin1String("SP_DesktopIcon");
case QStyle::SP_TrashIcon: return QLatin1String("SP_TrashIcon");
case QStyle::SP_ComputerIcon: return QLatin1String("SP_ComputerIcon");
case QStyle::SP_DriveFDIcon: return QLatin1String("SP_DriveFDIcon");
case QStyle::SP_DriveHDIcon: return QLatin1String("SP_DriveHDIcon");
case QStyle::SP_DriveCDIcon: return QLatin1String("SP_DriveCDIcon");
case QStyle::SP_DriveDVDIcon: return QLatin1String("SP_DriveDVDIcon");
case QStyle::SP_DriveNetIcon: return QLatin1String("SP_DriveNetIcon");
case QStyle::SP_DirOpenIcon: return QLatin1String("SP_DirOpenIcon");
case QStyle::SP_DirClosedIcon: return QLatin1String("SP_DirClosedIcon");
case QStyle::SP_DirLinkIcon: return QLatin1String("SP_DirLinkIcon");
case QStyle::SP_DirLinkOpenIcon: return QLatin1String("SP_DirLinkOpenIcon");
case QStyle::SP_FileIcon: return QLatin1String("SP_FileIcon");
case QStyle::SP_FileLinkIcon: return QLatin1String("SP_FileLinkIcon");
case QStyle::SP_ToolBarHorizontalExtensionButton: return QLatin1String("SP_ToolBarHorizontalExtensionButton");
case QStyle::SP_ToolBarVerticalExtensionButton: return QLatin1String("SP_ToolBarVerticalExtensionButton");
case QStyle::SP_FileDialogStart: return QLatin1String("SP_FileDialogStart");
case QStyle::SP_FileDialogEnd: return QLatin1String("SP_FileDialogEnd");
case QStyle::SP_FileDialogToParent: return QLatin1String("SP_FileDialogToParent");
case QStyle::SP_FileDialogNewFolder: return QLatin1String("SP_FileDialogNewFolder");
case QStyle::SP_FileDialogDetailedView: return QLatin1String("SP_FileDialogDetailedView");
case QStyle::SP_FileDialogInfoView: return QLatin1String("SP_FileDialogInfoView");
case QStyle::SP_FileDialogContentsView: return QLatin1String("SP_FileDialogContentsView");
case QStyle::SP_FileDialogListView: return QLatin1String("SP_FileDialogListView");
case QStyle::SP_FileDialogBack: return QLatin1String("SP_FileDialogBack");
case QStyle::SP_DirIcon: return QLatin1String("SP_DirIcon");
case QStyle::SP_DialogOkButton: return QLatin1String("SP_DialogOkButton");
case QStyle::SP_DialogCancelButton: return QLatin1String("SP_DialogCancelButton");
case QStyle::SP_DialogHelpButton: return QLatin1String("SP_DialogHelpButton");
case QStyle::SP_DialogOpenButton: return QLatin1String("SP_DialogOpenButton");
case QStyle::SP_DialogSaveButton: return QLatin1String("SP_DialogSaveButton");
case QStyle::SP_DialogCloseButton: return QLatin1String("SP_DialogCloseButton");
case QStyle::SP_DialogApplyButton: return QLatin1String("SP_DialogApplyButton");
case QStyle::SP_DialogResetButton: return QLatin1String("SP_DialogResetButton");
case QStyle::SP_DialogDiscardButton: return QLatin1String("SP_DialogDiscardButton");
case QStyle::SP_DialogYesButton: return QLatin1String("SP_DialogYesButton");
case QStyle::SP_DialogNoButton: return QLatin1String("SP_DialogNoButton");
case QStyle::SP_ArrowUp: return QLatin1String("SP_ArrowUp");
case QStyle::SP_ArrowDown: return QLatin1String("SP_ArrowDown");
case QStyle::SP_ArrowLeft: return QLatin1String("SP_ArrowLeft");
case QStyle::SP_ArrowRight: return QLatin1String("SP_ArrowRight");
case QStyle::SP_ArrowBack: return QLatin1String("SP_ArrowBack");
case QStyle::SP_ArrowForward: return QLatin1String("SP_ArrowForward");
case QStyle::SP_DirHomeIcon: return QLatin1String("SP_DirHomeIcon");
case QStyle::SP_CommandLink: return QLatin1String("SP_CommandLink");
case QStyle::SP_VistaShield: return QLatin1String("SP_VistaShield");
case QStyle::SP_BrowserReload: return QLatin1String("SP_BrowserReload");
case QStyle::SP_BrowserStop: return QLatin1String("SP_BrowserStop");
case QStyle::SP_MediaPlay: return QLatin1String("SP_MediaPlay");
case QStyle::SP_MediaStop: return QLatin1String("SP_MediaStop");
case QStyle::SP_MediaPause: return QLatin1String("SP_MediaPause");
case QStyle::SP_MediaSkipForward: return QLatin1String("SP_MediaSkipForward");
case QStyle::SP_MediaSkipBackward: return QLatin1String("SP_MediaSkipBackward");
case QStyle::SP_MediaSeekForward: return QLatin1String("SP_MediaSeekForward");
case QStyle::SP_MediaSeekBackward: return QLatin1String("SP_MediaSeekBackward");
case QStyle::SP_MediaVolume: return QLatin1String("SP_MediaVolume");
case QStyle::SP_MediaVolumeMuted: return QLatin1String("SP_MediaVolumeMuted");
case QStyle::SP_LineEditClearButton: return QLatin1String("SP_LineEditClearButton");
case QStyle::SP_DialogYesToAllButton: return QLatin1String("SP_DialogYesToAllButton");
case QStyle::SP_DialogNoToAllButton: return QLatin1String("SP_DialogNoToAllButton");
case QStyle::SP_DialogSaveAllButton: return QLatin1String("SP_DialogSaveAllButton");
case QStyle::SP_DialogAbortButton: return QLatin1String("SP_DialogAbortButton");
case QStyle::SP_DialogRetryButton: return QLatin1String("SP_DialogRetryButton");
case QStyle::SP_DialogIgnoreButton: return QLatin1String("SP_DialogIgnoreButton");
case QStyle::SP_RestoreDefaultsButton: return QLatin1String("SP_RestoreDefaultsButton");
#if (QT_VERSION > QT_VERSION_CHECK(6, 0, 0))
case QStyle::SP_TabCloseButton: return QLatin1String("SP_TabCloseButton");
#endif
default:
Q_ASSERT(false);
break;
}
return QString();
}
};
class ButtonsForm : public QWidget
{
public:
ButtonsForm(QWidget* parent = Q_NULL) : QWidget(parent) {
QVBoxLayout* l = new QVBoxLayout(this);
QPushButton* button1 = new QPushButton();
button1->setText("Text button 1");
l->addWidget(button1);
button1->setDefault(true);
QPushButton* button2 = new QPushButton();
button2->setText("Text button 2");
l->addWidget(button2);
QPushButton* button3 = new QPushButton();
button3->setText("Text button 3");
l->addWidget(button3);
button3->setEnabled(false);
QPushButton* button4 = new QPushButton();
button4->setText("Text button 4");
QMenu* m = new QMenu();
m->addAction("Click Me");
button4->setMenu(m);
l->addWidget(button4);
QCommandLinkButton* button5 = new QCommandLinkButton();
button5->setText("Text button 5");
button5->setDescription("Text button 5 Description");
l->addWidget(button5);
}
protected:
};
QSize StyleWindow::sizeHint() const
{
return m_forms->sizeHint();
}
QWidget* StyleWindow::createPaletteForm()
{
return new PaletteForm();
}
QWidget* StyleWindow::createIconsForm()
{
return new IconsForm();
}
QWidget* StyleWindow::createButtonsForm()
{
return new ButtonsForm();
}