Polish the examples/widgets/mainwindows example.

- Introduce Qt 5 signals & slot syntax.
- Replace QSignalMapper used for the corner/area
  actions by a by a functor.
- Improve command line parsing.
- Reorder class declarations.
- Remove commented-out code.
- Use QDialogButtonBox in dialogs.
- Fix minor issues in code, use multi-argument version
  of QString::arg(), QDir::toNativeSeparators() to
  present file paths to the user, static method
  invocations.

Change-Id: I865c56639c74135b59740797e9a9dfbfca2e72b6
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Friedemann Kleint 2015-07-17 10:41:44 +02:00
parent 7f77d4fcd5
commit 2fe56e37ed
7 changed files with 322 additions and 423 deletions

View File

@ -42,6 +42,7 @@
#include <QImage> #include <QImage>
#include <QColor> #include <QColor>
#include <QDialog> #include <QDialog>
#include <QDialogButtonBox>
#include <QGridLayout> #include <QGridLayout>
#include <QSpinBox> #include <QSpinBox>
#include <QLabel> #include <QLabel>
@ -57,15 +58,15 @@ QColor bgColorForName(const QString &name)
{ {
if (name == "Black") if (name == "Black")
return QColor("#D8D8D8"); return QColor("#D8D8D8");
else if (name == "White") if (name == "White")
return QColor("#F1F1F1"); return QColor("#F1F1F1");
else if (name == "Red") if (name == "Red")
return QColor("#F1D8D8"); return QColor("#F1D8D8");
else if (name == "Green") if (name == "Green")
return QColor("#D8E4D8"); return QColor("#D8E4D8");
else if (name == "Blue") if (name == "Blue")
return QColor("#D8D8F1"); return QColor("#D8D8F1");
else if (name == "Yellow") if (name == "Yellow")
return QColor("#F1F0D8"); return QColor("#F1F0D8");
return QColor(name).light(110); return QColor(name).light(110);
} }
@ -74,15 +75,15 @@ QColor fgColorForName(const QString &name)
{ {
if (name == "Black") if (name == "Black")
return QColor("#6C6C6C"); return QColor("#6C6C6C");
else if (name == "White") if (name == "White")
return QColor("#F8F8F8"); return QColor("#F8F8F8");
else if (name == "Red") if (name == "Red")
return QColor("#F86C6C"); return QColor("#F86C6C");
else if (name == "Green") if (name == "Green")
return QColor("#6CB26C"); return QColor("#6CB26C");
else if (name == "Blue") if (name == "Blue")
return QColor("#6C6CF8"); return QColor("#6C6CF8");
else if (name == "Yellow") if (name == "Yellow")
return QColor("#F8F76C"); return QColor("#F8F76C");
return QColor(name); return QColor(name);
} }
@ -91,10 +92,10 @@ class ColorDock : public QFrame
{ {
Q_OBJECT Q_OBJECT
public: public:
ColorDock(const QString &c, QWidget *parent); explicit ColorDock(const QString &c, QWidget *parent);
virtual QSize sizeHint() const Q_DECL_OVERRIDE; QSize sizeHint() const Q_DECL_OVERRIDE { return szHint; }
virtual QSize minimumSizeHint() const Q_DECL_OVERRIDE; QSize minimumSizeHint() const Q_DECL_OVERRIDE { return minSzHint; }
void setCustomSizeHint(const QSize &size); void setCustomSizeHint(const QSize &size);
@ -103,28 +104,22 @@ public slots:
protected: protected:
void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
QString color;
QSize szHint, minSzHint; private:
const QString color;
QSize szHint;
QSize minSzHint;
}; };
ColorDock::ColorDock(const QString &c, QWidget *parent) ColorDock::ColorDock(const QString &c, QWidget *parent)
: QFrame(parent) , color(c) : QFrame(parent)
, color(c)
, szHint(-1, -1)
, minSzHint(125, 75)
{ {
QFont font = this->font(); QFont font = this->font();
font.setPointSize(8); font.setPointSize(8);
setFont(font); setFont(font);
szHint = QSize(-1, -1);
minSzHint = QSize(125, 75);
}
QSize ColorDock::sizeHint() const
{
return szHint;
}
QSize ColorDock::minimumSizeHint() const
{
return minSzHint;
} }
void ColorDock::paintEvent(QPaintEvent *) void ColorDock::paintEvent(QPaintEvent *)
@ -178,6 +173,7 @@ static QSpinBox *createSpinBox(int value, QWidget *parent, int max = 1000)
void ColorDock::changeSizeHints() void ColorDock::changeSizeHints()
{ {
QDialog dialog(this); QDialog dialog(this);
dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
dialog.setWindowTitle(color); dialog.setWindowTitle(color);
QVBoxLayout *topLayout = new QVBoxLayout(&dialog); QVBoxLayout *topLayout = new QVBoxLayout(&dialog);
@ -188,7 +184,7 @@ void ColorDock::changeSizeHints()
inputLayout->addWidget(new QLabel(tr("Size Hint:"), &dialog), 0, 0); inputLayout->addWidget(new QLabel(tr("Size Hint:"), &dialog), 0, 0);
inputLayout->addWidget(new QLabel(tr("Min Size Hint:"), &dialog), 1, 0); inputLayout->addWidget(new QLabel(tr("Min Size Hint:"), &dialog), 1, 0);
inputLayout->addWidget(new QLabel(tr("Max Size:"), &dialog), 2, 0); inputLayout->addWidget(new QLabel(tr("Max Size:"), &dialog), 2, 0);
inputLayout->addWidget(new QLabel(tr("Dockwgt Max Size:"), &dialog), 3, 0); inputLayout->addWidget(new QLabel(tr("Dock Widget Max Size:"), &dialog), 3, 0);
QSpinBox *szHintW = createSpinBox(szHint.width(), &dialog); QSpinBox *szHintW = createSpinBox(szHint.width(), &dialog);
inputLayout->addWidget(szHintW, 0, 1); inputLayout->addWidget(szHintW, 0, 1);
@ -217,19 +213,13 @@ void ColorDock::changeSizeHints()
topLayout->addStretch(); topLayout->addStretch();
QHBoxLayout *buttonBox = new QHBoxLayout(); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
topLayout->addLayout(buttonBox); connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::reject);
QPushButton *okButton = new QPushButton(tr("Ok"), &dialog); topLayout->addWidget(buttonBox);
QPushButton *cancelButton = new QPushButton(tr("Cancel"), &dialog);
connect(okButton, SIGNAL(clicked()), &dialog, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), &dialog, SLOT(reject()));
buttonBox->addStretch();
buttonBox->addWidget(cancelButton);
buttonBox->addWidget(okButton);
if (dialog.exec() != QDialog::Accepted)
if (!dialog.exec())
return; return;
szHint = QSize(szHintW->value(), szHintH->value()); szHint = QSize(szHintW->value(), szHintH->value());
@ -244,8 +234,10 @@ void ColorDock::changeSizeHints()
void ColorDock::setCustomSizeHint(const QSize &size) void ColorDock::setCustomSizeHint(const QSize &size)
{ {
szHint = size; if (szHint != size) {
updateGeometry(); szHint = size;
updateGeometry();
}
} }
ColorSwatch::ColorSwatch(const QString &colorName, QMainWindow *parent, Qt::WindowFlags flags) ColorSwatch::ColorSwatch(const QString &colorName, QMainWindow *parent, Qt::WindowFlags flags)
@ -254,53 +246,50 @@ ColorSwatch::ColorSwatch(const QString &colorName, QMainWindow *parent, Qt::Wind
setObjectName(colorName + QLatin1String(" Dock Widget")); setObjectName(colorName + QLatin1String(" Dock Widget"));
setWindowTitle(objectName() + QLatin1String(" [*]")); setWindowTitle(objectName() + QLatin1String(" [*]"));
QFrame *swatch = new ColorDock(colorName, this); ColorDock *swatch = new ColorDock(colorName, this);
swatch->setFrameStyle(QFrame::Box | QFrame::Sunken); swatch->setFrameStyle(QFrame::Box | QFrame::Sunken);
setWidget(swatch); setWidget(swatch);
changeSizeHintsAction = new QAction(tr("Change Size Hints"), this);
connect(changeSizeHintsAction, SIGNAL(triggered()), swatch, SLOT(changeSizeHints()));
closableAction = new QAction(tr("Closable"), this); closableAction = new QAction(tr("Closable"), this);
closableAction->setCheckable(true); closableAction->setCheckable(true);
connect(closableAction, SIGNAL(triggered(bool)), SLOT(changeClosable(bool))); connect(closableAction, &QAction::triggered, this, &ColorSwatch::changeClosable);
movableAction = new QAction(tr("Movable"), this); movableAction = new QAction(tr("Movable"), this);
movableAction->setCheckable(true); movableAction->setCheckable(true);
connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool))); connect(movableAction, &QAction::triggered, this, &ColorSwatch::changeMovable);
floatableAction = new QAction(tr("Floatable"), this); floatableAction = new QAction(tr("Floatable"), this);
floatableAction->setCheckable(true); floatableAction->setCheckable(true);
connect(floatableAction, SIGNAL(triggered(bool)), SLOT(changeFloatable(bool))); connect(floatableAction, &QAction::triggered, this, &ColorSwatch::changeFloatable);
verticalTitleBarAction = new QAction(tr("Vertical title bar"), this); verticalTitleBarAction = new QAction(tr("Vertical title bar"), this);
verticalTitleBarAction->setCheckable(true); verticalTitleBarAction->setCheckable(true);
connect(verticalTitleBarAction, SIGNAL(triggered(bool)), connect(verticalTitleBarAction, &QAction::triggered,
SLOT(changeVerticalTitleBar(bool))); this, &ColorSwatch::changeVerticalTitleBar);
floatingAction = new QAction(tr("Floating"), this); floatingAction = new QAction(tr("Floating"), this);
floatingAction->setCheckable(true); floatingAction->setCheckable(true);
connect(floatingAction, SIGNAL(triggered(bool)), SLOT(changeFloating(bool))); connect(floatingAction, &QAction::triggered, this, &ColorSwatch::changeFloating);
allowedAreasActions = new QActionGroup(this); allowedAreasActions = new QActionGroup(this);
allowedAreasActions->setExclusive(false); allowedAreasActions->setExclusive(false);
allowLeftAction = new QAction(tr("Allow on Left"), this); allowLeftAction = new QAction(tr("Allow on Left"), this);
allowLeftAction->setCheckable(true); allowLeftAction->setCheckable(true);
connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool))); connect(allowLeftAction, &QAction::triggered, this, &ColorSwatch::allowLeft);
allowRightAction = new QAction(tr("Allow on Right"), this); allowRightAction = new QAction(tr("Allow on Right"), this);
allowRightAction->setCheckable(true); allowRightAction->setCheckable(true);
connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool))); connect(allowRightAction, &QAction::triggered, this, &ColorSwatch::allowRight);
allowTopAction = new QAction(tr("Allow on Top"), this); allowTopAction = new QAction(tr("Allow on Top"), this);
allowTopAction->setCheckable(true); allowTopAction->setCheckable(true);
connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool))); connect(allowTopAction, &QAction::triggered, this, &ColorSwatch::allowTop);
allowBottomAction = new QAction(tr("Allow on Bottom"), this); allowBottomAction = new QAction(tr("Allow on Bottom"), this);
allowBottomAction->setCheckable(true); allowBottomAction->setCheckable(true);
connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool))); connect(allowBottomAction, &QAction::triggered, this, &ColorSwatch::allowBottom);
allowedAreasActions->addAction(allowLeftAction); allowedAreasActions->addAction(allowLeftAction);
allowedAreasActions->addAction(allowRightAction); allowedAreasActions->addAction(allowRightAction);
@ -312,56 +301,56 @@ ColorSwatch::ColorSwatch(const QString &colorName, QMainWindow *parent, Qt::Wind
leftAction = new QAction(tr("Place on Left") , this); leftAction = new QAction(tr("Place on Left") , this);
leftAction->setCheckable(true); leftAction->setCheckable(true);
connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool))); connect(leftAction, &QAction::triggered, this, &ColorSwatch::placeLeft);
rightAction = new QAction(tr("Place on Right") , this); rightAction = new QAction(tr("Place on Right") , this);
rightAction->setCheckable(true); rightAction->setCheckable(true);
connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool))); connect(rightAction, &QAction::triggered, this, &ColorSwatch::placeRight);
topAction = new QAction(tr("Place on Top") , this); topAction = new QAction(tr("Place on Top") , this);
topAction->setCheckable(true); topAction->setCheckable(true);
connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool))); connect(topAction, &QAction::triggered, this, &ColorSwatch::placeTop);
bottomAction = new QAction(tr("Place on Bottom") , this); bottomAction = new QAction(tr("Place on Bottom") , this);
bottomAction->setCheckable(true); bottomAction->setCheckable(true);
connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool))); connect(bottomAction, &QAction::triggered, this, &ColorSwatch::placeBottom);
areaActions->addAction(leftAction); areaActions->addAction(leftAction);
areaActions->addAction(rightAction); areaActions->addAction(rightAction);
areaActions->addAction(topAction); areaActions->addAction(topAction);
areaActions->addAction(bottomAction); areaActions->addAction(bottomAction);
connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool))); connect(movableAction, &QAction::triggered, areaActions, &QActionGroup::setEnabled);
connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool))); connect(movableAction, &QAction::triggered, allowedAreasActions, &QActionGroup::setEnabled);
connect(floatableAction, SIGNAL(triggered(bool)), floatingAction, SLOT(setEnabled(bool))); connect(floatableAction, &QAction::triggered, floatingAction, &QAction::setEnabled);
connect(floatingAction, SIGNAL(triggered(bool)), floatableAction, SLOT(setDisabled(bool))); connect(floatingAction, &QAction::triggered, floatableAction, &QAction::setDisabled);
connect(movableAction, SIGNAL(triggered(bool)), floatableAction, SLOT(setEnabled(bool))); connect(movableAction, &QAction::triggered, floatableAction, &QAction::setEnabled);
tabMenu = new QMenu(this); tabMenu = new QMenu(this);
tabMenu->setTitle(tr("Tab into")); tabMenu->setTitle(tr("Tab into"));
connect(tabMenu, SIGNAL(triggered(QAction*)), this, SLOT(tabInto(QAction*))); connect(tabMenu, &QMenu::triggered, this, &ColorSwatch::tabInto);
splitHMenu = new QMenu(this); splitHMenu = new QMenu(this);
splitHMenu->setTitle(tr("Split horizontally into")); splitHMenu->setTitle(tr("Split horizontally into"));
connect(splitHMenu, SIGNAL(triggered(QAction*)), this, SLOT(splitInto(QAction*))); connect(splitHMenu, &QMenu::triggered, this, &ColorSwatch::splitInto);
splitVMenu = new QMenu(this); splitVMenu = new QMenu(this);
splitVMenu->setTitle(tr("Split vertically into")); splitVMenu->setTitle(tr("Split vertically into"));
connect(splitVMenu, SIGNAL(triggered(QAction*)), this, SLOT(splitInto(QAction*))); connect(splitVMenu, &QMenu::triggered, this, &ColorSwatch::splitInto);
windowModifiedAction = new QAction(tr("Modified"), this); QAction *windowModifiedAction = new QAction(tr("Modified"), this);
windowModifiedAction->setCheckable(true); windowModifiedAction->setCheckable(true);
windowModifiedAction->setChecked(false); windowModifiedAction->setChecked(false);
connect(windowModifiedAction, SIGNAL(toggled(bool)), this, SLOT(setWindowModified(bool))); connect(windowModifiedAction, &QAction::toggled, this, &QWidget::setWindowModified);
menu = new QMenu(colorName, this); menu = new QMenu(colorName, this);
menu->addAction(toggleViewAction()); menu->addAction(toggleViewAction());
QAction *action = menu->addAction(tr("Raise")); menu->addAction(tr("Raise"), this, &QWidget::raise);
connect(action, SIGNAL(triggered()), this, SLOT(raise())); menu->addAction(tr("Change Size Hints..."), swatch, &ColorDock::changeSizeHints);
menu->addAction(changeSizeHintsAction);
menu->addSeparator(); menu->addSeparator();
menu->addAction(closableAction); menu->addAction(closableAction);
menu->addAction(movableAction); menu->addAction(movableAction);
@ -379,12 +368,12 @@ ColorSwatch::ColorSwatch(const QString &colorName, QMainWindow *parent, Qt::Wind
menu->addSeparator(); menu->addSeparator();
menu->addAction(windowModifiedAction); menu->addAction(windowModifiedAction);
connect(menu, SIGNAL(aboutToShow()), this, SLOT(updateContextMenu())); connect(menu, &QMenu::aboutToShow, this, &ColorSwatch::updateContextMenu);
if(colorName == "Black") { if (colorName == QLatin1String("Black")) {
leftAction->setShortcut(Qt::CTRL|Qt::Key_W); leftAction->setShortcut(Qt::CTRL | Qt::Key_W);
rightAction->setShortcut(Qt::CTRL|Qt::Key_E); rightAction->setShortcut(Qt::CTRL | Qt::Key_E);
toggleViewAction()->setShortcut(Qt::CTRL|Qt::Key_R); toggleViewAction()->setShortcut(Qt::CTRL | Qt::Key_R);
} }
} }
@ -447,46 +436,36 @@ void ColorSwatch::updateContextMenu()
splitVMenu->clear(); splitVMenu->clear();
QList<ColorSwatch*> dock_list = mainWindow->findChildren<ColorSwatch*>(); QList<ColorSwatch*> dock_list = mainWindow->findChildren<ColorSwatch*>();
foreach (ColorSwatch *dock, dock_list) { foreach (ColorSwatch *dock, dock_list) {
// if (!dock->isVisible() || dock->isFloating())
// continue;
tabMenu->addAction(dock->objectName()); tabMenu->addAction(dock->objectName());
splitHMenu->addAction(dock->objectName()); splitHMenu->addAction(dock->objectName());
splitVMenu->addAction(dock->objectName()); splitVMenu->addAction(dock->objectName());
} }
} }
static ColorSwatch *findByName(const QMainWindow *mainWindow, const QString &name)
{
foreach (ColorSwatch *dock, mainWindow->findChildren<ColorSwatch*>()) {
if (name == dock->objectName())
return dock;
}
return Q_NULLPTR;
}
void ColorSwatch::splitInto(QAction *action) void ColorSwatch::splitInto(QAction *action)
{ {
QList<ColorSwatch*> dock_list = mainWindow->findChildren<ColorSwatch*>(); ColorSwatch *target = findByName(mainWindow, action->text());
ColorSwatch *target = 0; if (!target)
foreach (ColorSwatch *dock, dock_list) {
if (action->text() == dock->objectName()) {
target = dock;
break;
}
}
if (target == 0)
return; return;
Qt::Orientation o = action->parent() == splitHMenu const Qt::Orientation o = action->parent() == splitHMenu
? Qt::Horizontal : Qt::Vertical; ? Qt::Horizontal : Qt::Vertical;
mainWindow->splitDockWidget(target, this, o); mainWindow->splitDockWidget(target, this, o);
} }
void ColorSwatch::tabInto(QAction *action) void ColorSwatch::tabInto(QAction *action)
{ {
QList<ColorSwatch*> dock_list = mainWindow->findChildren<ColorSwatch*>(); if (ColorSwatch *target = findByName(mainWindow, action->text()))
ColorSwatch *target = 0; mainWindow->tabifyDockWidget(target, this);
foreach (ColorSwatch *dock, dock_list) {
if (action->text() == dock->objectName()) {
target = dock;
break;
}
}
if (target == 0)
return;
mainWindow->tabifyDockWidget(target, this);
} }
void ColorSwatch::contextMenuEvent(QContextMenuEvent *event) void ColorSwatch::contextMenuEvent(QContextMenuEvent *event)
@ -503,7 +482,6 @@ void ColorSwatch::resizeEvent(QResizeEvent *e)
QDockWidget::resizeEvent(e); QDockWidget::resizeEvent(e);
} }
void ColorSwatch::allow(Qt::DockWidgetArea area, bool a) void ColorSwatch::allow(Qt::DockWidgetArea area, bool a)
{ {
Qt::DockWidgetAreas areas = allowedAreas(); Qt::DockWidgetAreas areas = allowedAreas();
@ -520,7 +498,8 @@ void ColorSwatch::allow(Qt::DockWidgetArea area, bool a)
void ColorSwatch::place(Qt::DockWidgetArea area, bool p) void ColorSwatch::place(Qt::DockWidgetArea area, bool p)
{ {
if (!p) return; if (!p)
return;
mainWindow->addDockWidget(area, this); mainWindow->addDockWidget(area, this);
@ -592,10 +571,10 @@ QSize BlueTitleBar::minimumSizeHint() const
BlueTitleBar::BlueTitleBar(QWidget *parent) BlueTitleBar::BlueTitleBar(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, leftPm(QPixmap(":/res/titlebarLeft.png"))
, centerPm(QPixmap(":/res/titlebarCenter.png"))
, rightPm(QPixmap(":/res/titlebarRight.png"))
{ {
leftPm = QPixmap(":/res/titlebarLeft.png");
centerPm = QPixmap(":/res/titlebarCenter.png");
rightPm = QPixmap(":/res/titlebarRight.png");
} }
void BlueTitleBar::paintEvent(QPaintEvent*) void BlueTitleBar::paintEvent(QPaintEvent*)
@ -683,7 +662,7 @@ void BlueTitleBar::updateMask()
{ {
QPainter painter(&bitmap); QPainter painter(&bitmap);
///initialize to transparent // initialize to transparent
painter.fillRect(rect, Qt::color0); painter.fillRect(rect, Qt::color0);
QRect contents = rect; QRect contents = rect;
@ -692,10 +671,7 @@ void BlueTitleBar::updateMask()
contents.setBottom(contents.bottom()-y()); contents.setBottom(contents.bottom()-y());
painter.fillRect(contents, Qt::color1); painter.fillRect(contents, Qt::color1);
// let's paint the titlebar
//let's pait the titlebar
QRect titleRect = this->geometry(); QRect titleRect = this->geometry();
if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar) { if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar) {
@ -718,7 +694,6 @@ void BlueTitleBar::updateMask()
QRect rect = titleRect; QRect rect = titleRect;
painter.drawPixmap(rect.topLeft(), leftPm.mask()); painter.drawPixmap(rect.topLeft(), leftPm.mask());
painter.fillRect(rect.left() + leftPm.width(), rect.top(), painter.fillRect(rect.left() + leftPm.width(), rect.top(),
rect.width() - leftPm.width() - rightPm.width(), rect.width() - leftPm.width() - rightPm.width(),

View File

@ -44,47 +44,15 @@ class ColorSwatch : public QDockWidget
{ {
Q_OBJECT Q_OBJECT
QAction *closableAction;
QAction *movableAction;
QAction *floatableAction;
QAction *floatingAction;
QAction *verticalTitleBarAction;
QActionGroup *allowedAreasActions;
QAction *allowLeftAction;
QAction *allowRightAction;
QAction *allowTopAction;
QAction *allowBottomAction;
QActionGroup *areaActions;
QAction *leftAction;
QAction *rightAction;
QAction *topAction;
QAction *bottomAction;
QAction *changeSizeHintsAction;
QMenu *tabMenu;
QMenu *splitHMenu;
QMenu *splitVMenu;
QAction *windowModifiedAction;
QMainWindow *mainWindow;
public: public:
explicit ColorSwatch(const QString &colorName, QMainWindow *parent = 0, Qt::WindowFlags flags = 0); explicit ColorSwatch(const QString &colorName, QMainWindow *parent = Q_NULLPTR, Qt::WindowFlags flags = 0);
QMenu *menu;
void setCustomSizeHint(const QSize &size); void setCustomSizeHint(const QSize &size);
QMenu *colorSwatchMenu() const { return menu; }
protected: protected:
virtual void contextMenuEvent(QContextMenuEvent *event) Q_DECL_OVERRIDE; void contextMenuEvent(QContextMenuEvent *event) Q_DECL_OVERRIDE;
virtual void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE; void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE;
private:
void allow(Qt::DockWidgetArea area, bool allow);
void place(Qt::DockWidgetArea area, bool place);
private slots: private slots:
void changeClosable(bool on); void changeClosable(bool on);
@ -106,25 +74,57 @@ private slots:
void splitInto(QAction *action); void splitInto(QAction *action);
void tabInto(QAction *action); void tabInto(QAction *action);
private:
void allow(Qt::DockWidgetArea area, bool allow);
void place(Qt::DockWidgetArea area, bool place);
QAction *closableAction;
QAction *movableAction;
QAction *floatableAction;
QAction *floatingAction;
QAction *verticalTitleBarAction;
QActionGroup *allowedAreasActions;
QAction *allowLeftAction;
QAction *allowRightAction;
QAction *allowTopAction;
QAction *allowBottomAction;
QActionGroup *areaActions;
QAction *leftAction;
QAction *rightAction;
QAction *topAction;
QAction *bottomAction;
QMenu *tabMenu;
QMenu *splitHMenu;
QMenu *splitVMenu;
QMenu *menu;
QMainWindow *mainWindow;
}; };
class BlueTitleBar : public QWidget class BlueTitleBar : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
BlueTitleBar(QWidget *parent = 0); explicit BlueTitleBar(QWidget *parent = Q_NULLPTR);
QSize sizeHint() const Q_DECL_OVERRIDE { return minimumSizeHint(); } QSize sizeHint() const Q_DECL_OVERRIDE { return minimumSizeHint(); }
QSize minimumSizeHint() const Q_DECL_OVERRIDE; QSize minimumSizeHint() const Q_DECL_OVERRIDE;
protected: protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
public slots: public slots:
void updateMask(); void updateMask();
private: private:
QPixmap leftPm, centerPm, rightPm; const QPixmap leftPm;
const QPixmap centerPm;
const QPixmap rightPm;
}; };
#endif // COLORSWATCH_H
#endif

View File

@ -37,9 +37,10 @@
#include <QPainterPath> #include <QPainterPath>
#include <QPainter> #include <QPainter>
#include <QMap> #include <QMap>
#include <qdebug.h> #include <QDebug>
void render_qt_text(QPainter *painter, int w, int h, const QColor &color) { void render_qt_text(QPainter *painter, int w, int h, const QColor &color)
{
QPainterPath path; QPainterPath path;
path.moveTo(-0.083695, 0.283849); path.moveTo(-0.083695, 0.283849);
path.cubicTo(-0.049581, 0.349613, -0.012720, 0.397969, 0.026886, 0.428917); path.cubicTo(-0.049581, 0.349613, -0.012720, 0.397969, 0.026886, 0.428917);
@ -108,47 +109,67 @@ void render_qt_text(QPainter *painter, int w, int h, const QColor &color) {
painter->drawPath(path); painter->drawPath(path);
} }
void usage() static void usage()
{ {
qWarning() << "Usage: mainwindow [-SizeHint<color> <width>x<height>] ..."; qWarning() << "Usage: mainwindow [-SizeHint<color> <width>x<height>] ...";
exit(1); exit(1);
} }
QMap<QString, QSize> parseCustomSizeHints(int argc, char **argv) enum ParseCommandLineArgumentsResult {
CommandLineArgumentsOk,
CommandLineArgumentsError,
HelpRequested
};
static ParseCommandLineArgumentsResult
parseCustomSizeHints(const QStringList &arguments, MainWindow::CustomSizeHintMap *result)
{ {
QMap<QString, QSize> result; result->clear();
const int argumentCount = arguments.size();
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argumentCount; ++i) {
QString arg = QString::fromLocal8Bit(argv[i]); const QString &arg = arguments.at(i);
if (arg.startsWith(QLatin1String("-SizeHint"))) { if (arg.startsWith(QLatin1String("-SizeHint"))) {
QString name = arg.mid(9); const QString name = arg.mid(9);
if (name.isEmpty()) if (name.isEmpty())
usage(); return CommandLineArgumentsError;
if (++i == argc) if (++i == argumentCount)
usage(); return CommandLineArgumentsError;
QString sizeStr = QString::fromLocal8Bit(argv[i]); const QString sizeStr = arguments.at(i);
int idx = sizeStr.indexOf(QLatin1Char('x')); const int idx = sizeStr.indexOf(QLatin1Char('x'));
if (idx == -1) if (idx == -1)
usage(); return CommandLineArgumentsError;
bool ok; bool ok;
int w = sizeStr.left(idx).toInt(&ok); const int w = sizeStr.leftRef(idx).toInt(&ok);
if (!ok) if (!ok)
usage(); return CommandLineArgumentsError;
int h = sizeStr.mid(idx + 1).toInt(&ok); const int h = sizeStr.midRef(idx + 1).toInt(&ok);
if (!ok) if (!ok)
usage(); return CommandLineArgumentsError;
result[name] = QSize(w, h); result->insert(name, QSize(w, h));
} else if (arg == QLatin1String("-h") || arg == QLatin1String("--help")) {
return HelpRequested;
} else {
return CommandLineArgumentsError;
} }
} }
return result; return CommandLineArgumentsOk;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
QMap<QString, QSize> customSizeHints = parseCustomSizeHints(argc, argv); MainWindow::CustomSizeHintMap customSizeHints;
switch (parseCustomSizeHints(QCoreApplication::arguments(), &customSizeHints)) {
case CommandLineArgumentsOk:
break;
case CommandLineArgumentsError:
usage();
return -1;
case HelpRequested:
usage();
return 0;
}
MainWindow mainWin(customSizeHints); MainWindow mainWin(customSizeHints);
mainWin.resize(800, 600); mainWin.resize(800, 600);
mainWin.show(); mainWin.show();

View File

@ -44,6 +44,7 @@
#include <QFile> #include <QFile>
#include <QDataStream> #include <QDataStream>
#include <QFileDialog> #include <QFileDialog>
#include <QDialogButtonBox>
#include <QMessageBox> #include <QMessageBox>
#include <QSignalMapper> #include <QSignalMapper>
#include <QApplication> #include <QApplication>
@ -53,9 +54,10 @@
#include <QComboBox> #include <QComboBox>
#include <QLabel> #include <QLabel>
#include <QPushButton> #include <QPushButton>
#include <qdebug.h> #include <QTextEdit>
#include <QDebug>
static const char * const message = static const char message[] =
"<p><b>Qt Main Window Example</b></p>" "<p><b>Qt Main Window Example</b></p>"
"<p>This is a demonstration of the QMainWindow, QToolBar and " "<p>This is a demonstration of the QMainWindow, QToolBar and "
@ -75,14 +77,14 @@ static const char * const message =
Q_DECLARE_METATYPE(QDockWidget::DockWidgetFeatures) Q_DECLARE_METATYPE(QDockWidget::DockWidgetFeatures)
MainWindow::MainWindow(const QMap<QString, QSize> &customSizeHints, MainWindow::MainWindow(const CustomSizeHintMap &customSizeHints,
QWidget *parent, Qt::WindowFlags flags) QWidget *parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags) : QMainWindow(parent, flags)
{ {
setObjectName("MainWindow"); setObjectName("MainWindow");
setWindowTitle("Qt Main Window Example"); setWindowTitle("Qt Main Window Example");
center = new QTextEdit(this); QTextEdit *center = new QTextEdit(this);
center->setReadOnly(true); center->setReadOnly(true);
center->setMinimumSize(400, 205); center->setMinimumSize(400, 205);
setCentralWidget(center); setCentralWidget(center);
@ -116,54 +118,48 @@ void MainWindow::setupMenuBar()
{ {
QMenu *menu = menuBar()->addMenu(tr("&File")); QMenu *menu = menuBar()->addMenu(tr("&File"));
QAction *action = menu->addAction(tr("Save layout...")); menu->addAction(tr("Save layout..."), this, &MainWindow::saveLayout);
connect(action, SIGNAL(triggered()), this, SLOT(saveLayout())); menu->addAction(tr("Load layout..."), this, &MainWindow::loadLayout);
menu->addAction(tr("Switch layout direction"),this, &MainWindow::switchLayoutDirection);
action = menu->addAction(tr("Load layout..."));
connect(action, SIGNAL(triggered()), this, SLOT(loadLayout()));
action = menu->addAction(tr("Switch layout direction"));
connect(action, SIGNAL(triggered()), this, SLOT(switchLayoutDirection()));
menu->addSeparator(); menu->addSeparator();
menu->addAction(tr("&Quit"), this, &QWidget::close);
menu->addAction(tr("&Quit"), this, SLOT(close()));
mainWindowMenu = menuBar()->addMenu(tr("Main window")); mainWindowMenu = menuBar()->addMenu(tr("Main window"));
action = mainWindowMenu->addAction(tr("Animated docks")); QAction *action = mainWindowMenu->addAction(tr("Animated docks"));
action->setCheckable(true); action->setCheckable(true);
action->setChecked(dockOptions() & AnimatedDocks); action->setChecked(dockOptions() & AnimatedDocks);
connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); connect(action, &QAction::toggled, this, &MainWindow::setDockOptions);
action = mainWindowMenu->addAction(tr("Allow nested docks")); action = mainWindowMenu->addAction(tr("Allow nested docks"));
action->setCheckable(true); action->setCheckable(true);
action->setChecked(dockOptions() & AllowNestedDocks); action->setChecked(dockOptions() & AllowNestedDocks);
connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); connect(action, &QAction::toggled, this, &MainWindow::setDockOptions);
action = mainWindowMenu->addAction(tr("Allow tabbed docks")); action = mainWindowMenu->addAction(tr("Allow tabbed docks"));
action->setCheckable(true); action->setCheckable(true);
action->setChecked(dockOptions() & AllowTabbedDocks); action->setChecked(dockOptions() & AllowTabbedDocks);
connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); connect(action, &QAction::toggled, this, &MainWindow::setDockOptions);
action = mainWindowMenu->addAction(tr("Force tabbed docks")); action = mainWindowMenu->addAction(tr("Force tabbed docks"));
action->setCheckable(true); action->setCheckable(true);
action->setChecked(dockOptions() & ForceTabbedDocks); action->setChecked(dockOptions() & ForceTabbedDocks);
connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); connect(action, &QAction::toggled, this, &MainWindow::setDockOptions);
action = mainWindowMenu->addAction(tr("Vertical tabs")); action = mainWindowMenu->addAction(tr("Vertical tabs"));
action->setCheckable(true); action->setCheckable(true);
action->setChecked(dockOptions() & VerticalTabs); action->setChecked(dockOptions() & VerticalTabs);
connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); connect(action, &QAction::toggled, this, &MainWindow::setDockOptions);
action = mainWindowMenu->addAction(tr("Grouped dragging")); action = mainWindowMenu->addAction(tr("Grouped dragging"));
action->setCheckable(true); action->setCheckable(true);
action->setChecked(dockOptions() & GroupedDragging); action->setChecked(dockOptions() & GroupedDragging);
connect(action, SIGNAL(toggled(bool)), this, SLOT(setDockOptions())); connect(action, &QAction::toggled, this, &MainWindow::setDockOptions);
QMenu *toolBarMenu = menuBar()->addMenu(tr("Tool bars")); QMenu *toolBarMenu = menuBar()->addMenu(tr("Tool bars"));
for (int i = 0; i < toolBars.count(); ++i) for (int i = 0; i < toolBars.count(); ++i)
toolBarMenu->addMenu(toolBars.at(i)->menu); toolBarMenu->addMenu(toolBars.at(i)->toolbarMenu());
#ifdef Q_OS_OSX #ifdef Q_OS_OSX
toolBarMenu->addSeparator(); toolBarMenu->addSeparator();
@ -171,7 +167,7 @@ void MainWindow::setupMenuBar()
action = toolBarMenu->addAction(tr("Unified")); action = toolBarMenu->addAction(tr("Unified"));
action->setCheckable(true); action->setCheckable(true);
action->setChecked(unifiedTitleAndToolBarOnMac()); action->setChecked(unifiedTitleAndToolBarOnMac());
connect(action, SIGNAL(toggled(bool)), this, SLOT(setUnifiedTitleAndToolBarOnMac(bool))); connect(action, &QAction::toggled, this, &QMainWindow::setUnifiedTitleAndToolBarOnMac);
#endif #endif
dockWidgetMenu = menuBar()->addMenu(tr("&Dock Widgets")); dockWidgetMenu = menuBar()->addMenu(tr("&Dock Widgets"));
@ -207,8 +203,7 @@ void MainWindow::saveLayout()
QFile file(fileName); QFile file(fileName);
if (!file.open(QFile::WriteOnly)) { if (!file.open(QFile::WriteOnly)) {
QString msg = tr("Failed to open %1\n%2") QString msg = tr("Failed to open %1\n%2")
.arg(fileName) .arg(QDir::toNativeSeparators(fileName), file.errorString());
.arg(file.errorString());
QMessageBox::warning(this, tr("Error"), msg); QMessageBox::warning(this, tr("Error"), msg);
return; return;
} }
@ -224,8 +219,7 @@ void MainWindow::saveLayout()
if (!ok) { if (!ok) {
QString msg = tr("Error writing to %1\n%2") QString msg = tr("Error writing to %1\n%2")
.arg(fileName) .arg(QDir::toNativeSeparators(fileName), file.errorString());
.arg(file.errorString());
QMessageBox::warning(this, tr("Error"), msg); QMessageBox::warning(this, tr("Error"), msg);
return; return;
} }
@ -240,8 +234,7 @@ void MainWindow::loadLayout()
QFile file(fileName); QFile file(fileName);
if (!file.open(QFile::ReadOnly)) { if (!file.open(QFile::ReadOnly)) {
QString msg = tr("Failed to open %1\n%2") QString msg = tr("Failed to open %1\n%2")
.arg(fileName) .arg(QDir::toNativeSeparators(fileName), file.errorString());
.arg(file.errorString());
QMessageBox::warning(this, tr("Error"), msg); QMessageBox::warning(this, tr("Error"), msg);
return; return;
} }
@ -266,56 +259,65 @@ void MainWindow::loadLayout()
ok = restoreState(layout_data); ok = restoreState(layout_data);
if (!ok) { if (!ok) {
QString msg = tr("Error reading %1") QString msg = tr("Error reading %1").arg(QDir::toNativeSeparators(fileName));
.arg(fileName);
QMessageBox::warning(this, tr("Error"), msg); QMessageBox::warning(this, tr("Error"), msg);
return; return;
} }
} }
QAction *addAction(QMenu *menu, const QString &text, QActionGroup *group, QSignalMapper *mapper, class DockWidgetAreaCornerFunctor {
int id) public:
explicit DockWidgetAreaCornerFunctor(QMainWindow *mw, Qt::Corner c, Qt::DockWidgetArea a)
: m_mainWindow(mw), m_area(a), m_corner(c) {}
void operator()() const { m_mainWindow->setCorner(m_corner, m_area); }
private:
QMainWindow *m_mainWindow;
Qt::DockWidgetArea m_area;
Qt::Corner m_corner;
};
static QAction *addCornerAction(const QString &text, QMainWindow *mw, QMenu *menu, QActionGroup *group,
Qt::Corner c, Qt::DockWidgetArea a)
{ {
bool first = group->actions().isEmpty(); QAction *result = menu->addAction(text, mw, DockWidgetAreaCornerFunctor(mw, c, a));
QAction *result = menu->addAction(text);
result->setCheckable(true); result->setCheckable(true);
result->setChecked(first);
group->addAction(result); group->addAction(result);
QObject::connect(result, SIGNAL(triggered()), mapper, SLOT(map()));
mapper->setMapping(result, id);
return result; return result;
} }
void MainWindow::setupDockWidgets(const QMap<QString, QSize> &customSizeHints) void MainWindow::setupDockWidgets(const CustomSizeHintMap &customSizeHints)
{ {
qRegisterMetaType<QDockWidget::DockWidgetFeatures>(); qRegisterMetaType<QDockWidget::DockWidgetFeatures>();
mapper = new QSignalMapper(this); QMenu *cornerMenu = dockWidgetMenu->addMenu(tr("Top left corner"));
connect(mapper, SIGNAL(mapped(int)), this, SLOT(setCorner(int)));
QMenu *corner_menu = dockWidgetMenu->addMenu(tr("Top left corner"));
QActionGroup *group = new QActionGroup(this); QActionGroup *group = new QActionGroup(this);
group->setExclusive(true); group->setExclusive(true);
::addAction(corner_menu, tr("Top dock area"), group, mapper, 0); QAction *cornerAction = addCornerAction(tr("Top dock area"), this, cornerMenu, group, Qt::TopLeftCorner, Qt::TopDockWidgetArea);
::addAction(corner_menu, tr("Left dock area"), group, mapper, 1); cornerAction->setChecked(true);
addCornerAction(tr("Left dock area"), this, cornerMenu, group, Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
corner_menu = dockWidgetMenu->addMenu(tr("Top right corner")); cornerMenu = dockWidgetMenu->addMenu(tr("Top right corner"));
group = new QActionGroup(this); group = new QActionGroup(this);
group->setExclusive(true); group->setExclusive(true);
::addAction(corner_menu, tr("Top dock area"), group, mapper, 2); cornerAction = addCornerAction(tr("Top dock area"), this, cornerMenu, group, Qt::TopRightCorner, Qt::TopDockWidgetArea);
::addAction(corner_menu, tr("Right dock area"), group, mapper, 3); cornerAction->setChecked(true);
addCornerAction(tr("Right dock area"), this, cornerMenu, group, Qt::TopRightCorner, Qt::RightDockWidgetArea);
corner_menu = dockWidgetMenu->addMenu(tr("Bottom left corner")); cornerMenu = dockWidgetMenu->addMenu(tr("Bottom left corner"));
group = new QActionGroup(this); group = new QActionGroup(this);
group->setExclusive(true); group->setExclusive(true);
::addAction(corner_menu, tr("Bottom dock area"), group, mapper, 4); cornerAction = addCornerAction(tr("Bottom dock area"), this, cornerMenu, group, Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
::addAction(corner_menu, tr("Left dock area"), group, mapper, 5); cornerAction->setChecked(true);
addCornerAction(tr("Left dock area"), this, cornerMenu, group, Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
corner_menu = dockWidgetMenu->addMenu(tr("Bottom right corner")); cornerMenu = dockWidgetMenu->addMenu(tr("Bottom right corner"));
group = new QActionGroup(this); group = new QActionGroup(this);
group->setExclusive(true); group->setExclusive(true);
::addAction(corner_menu, tr("Bottom dock area"), group, mapper, 6); cornerAction = addCornerAction(tr("Bottom dock area"), this, cornerMenu, group, Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
::addAction(corner_menu, tr("Right dock area"), group, mapper, 7); cornerAction->setChecked(true);
addCornerAction(tr("Right dock area"), this, cornerMenu, group, Qt::BottomRightCorner, Qt::RightDockWidgetArea);
dockWidgetMenu->addSeparator(); dockWidgetMenu->addSeparator();
@ -337,16 +339,16 @@ void MainWindow::setupDockWidgets(const QMap<QString, QSize> &customSizeHints)
}; };
const int setCount = sizeof(sets) / sizeof(Set); const int setCount = sizeof(sets) / sizeof(Set);
const QIcon qtIcon(QPixmap(":/res/qt.png"));
for (int i = 0; i < setCount; ++i) { for (int i = 0; i < setCount; ++i) {
ColorSwatch *swatch = new ColorSwatch(tr(sets[i].name), this, Qt::WindowFlags(sets[i].flags)); ColorSwatch *swatch = new ColorSwatch(tr(sets[i].name), this, Qt::WindowFlags(sets[i].flags));
if (i%2) if (i % 2)
swatch->setWindowIcon(QIcon(QPixmap(":/res/qt.png"))); swatch->setWindowIcon(qtIcon);
if (qstrcmp(sets[i].name, "Blue") == 0) { if (qstrcmp(sets[i].name, "Blue") == 0) {
BlueTitleBar *titlebar = new BlueTitleBar(swatch); BlueTitleBar *titlebar = new BlueTitleBar(swatch);
swatch->setTitleBarWidget(titlebar); swatch->setTitleBarWidget(titlebar);
connect(swatch, SIGNAL(topLevelChanged(bool)), titlebar, SLOT(updateMask())); connect(swatch, &QDockWidget::topLevelChanged, titlebar, &BlueTitleBar::updateMask);
connect(swatch, SIGNAL(featuresChanged(QDockWidget::DockWidgetFeatures)), titlebar, SLOT(updateMask()), Qt::QueuedConnection); connect(swatch, &QDockWidget::featuresChanged, titlebar, &BlueTitleBar::updateMask, Qt::QueuedConnection);
} }
QString name = QString::fromLatin1(sets[i].name); QString name = QString::fromLatin1(sets[i].name);
@ -354,69 +356,32 @@ void MainWindow::setupDockWidgets(const QMap<QString, QSize> &customSizeHints)
swatch->setCustomSizeHint(customSizeHints.value(name)); swatch->setCustomSizeHint(customSizeHints.value(name));
addDockWidget(sets[i].area, swatch); addDockWidget(sets[i].area, swatch);
dockWidgetMenu->addMenu(swatch->menu); dockWidgetMenu->addMenu(swatch->colorSwatchMenu());
} }
createDockWidgetAction = new QAction(tr("Add dock widget..."), this);
connect(createDockWidgetAction, SIGNAL(triggered()), this, SLOT(createDockWidget()));
destroyDockWidgetMenu = new QMenu(tr("Destroy dock widget"), this); destroyDockWidgetMenu = new QMenu(tr("Destroy dock widget"), this);
destroyDockWidgetMenu->setEnabled(false); destroyDockWidgetMenu->setEnabled(false);
connect(destroyDockWidgetMenu, SIGNAL(triggered(QAction*)), this, SLOT(destroyDockWidget(QAction*))); connect(destroyDockWidgetMenu, &QMenu::triggered, this, &MainWindow::destroyDockWidget);
dockWidgetMenu->addSeparator(); dockWidgetMenu->addSeparator();
dockWidgetMenu->addAction(createDockWidgetAction); dockWidgetMenu->addAction(tr("Add dock widget..."), this, &MainWindow::createDockWidget);
dockWidgetMenu->addMenu(destroyDockWidgetMenu); dockWidgetMenu->addMenu(destroyDockWidgetMenu);
} }
void MainWindow::setCorner(int id)
{
switch (id) {
case 0:
QMainWindow::setCorner(Qt::TopLeftCorner, Qt::TopDockWidgetArea);
break;
case 1:
QMainWindow::setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
break;
case 2:
QMainWindow::setCorner(Qt::TopRightCorner, Qt::TopDockWidgetArea);
break;
case 3:
QMainWindow::setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
break;
case 4:
QMainWindow::setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
break;
case 5:
QMainWindow::setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
break;
case 6:
QMainWindow::setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
break;
case 7:
QMainWindow::setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
break;
}
}
void MainWindow::showEvent(QShowEvent *event)
{
QMainWindow::showEvent(event);
}
void MainWindow::switchLayoutDirection() void MainWindow::switchLayoutDirection()
{ {
if (layoutDirection() == Qt::LeftToRight) if (layoutDirection() == Qt::LeftToRight)
qApp->setLayoutDirection(Qt::RightToLeft); QApplication::setLayoutDirection(Qt::RightToLeft);
else else
qApp->setLayoutDirection(Qt::LeftToRight); QApplication::setLayoutDirection(Qt::LeftToRight);
} }
class CreateDockWidgetDialog : public QDialog class CreateDockWidgetDialog : public QDialog
{ {
public: public:
CreateDockWidgetDialog(QWidget *parent = 0); explicit CreateDockWidgetDialog(QWidget *parent = Q_NULLPTR);
QString objectName() const; QString enteredObjectName() const { return m_objectName->text(); }
Qt::DockWidgetArea location() const; Qt::DockWidgetArea location() const;
private: private:
@ -426,15 +391,17 @@ private:
CreateDockWidgetDialog::CreateDockWidgetDialog(QWidget *parent) CreateDockWidgetDialog::CreateDockWidgetDialog(QWidget *parent)
: QDialog(parent) : QDialog(parent)
, m_objectName(new QLineEdit(this))
, m_location(new QComboBox(this))
{ {
setWindowTitle(tr("Add Dock Widget"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
QGridLayout *layout = new QGridLayout(this); QGridLayout *layout = new QGridLayout(this);
layout->addWidget(new QLabel(tr("Object name:")), 0, 0); layout->addWidget(new QLabel(tr("Object name:")), 0, 0);
m_objectName = new QLineEdit;
layout->addWidget(m_objectName, 0, 1); layout->addWidget(m_objectName, 0, 1);
layout->addWidget(new QLabel(tr("Location:")), 1, 0); layout->addWidget(new QLabel(tr("Location:")), 1, 0);
m_location = new QComboBox;
m_location->setEditable(false); m_location->setEditable(false);
m_location->addItem(tr("Top")); m_location->addItem(tr("Top"));
m_location->addItem(tr("Left")); m_location->addItem(tr("Left"));
@ -443,23 +410,10 @@ CreateDockWidgetDialog::CreateDockWidgetDialog(QWidget *parent)
m_location->addItem(tr("Restore")); m_location->addItem(tr("Restore"));
layout->addWidget(m_location, 1, 1); layout->addWidget(m_location, 1, 1);
QHBoxLayout *buttonLayout = new QHBoxLayout; QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
layout->addLayout(buttonLayout, 2, 0, 1, 2); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
buttonLayout->addStretch(); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::reject);
layout->addWidget(buttonBox, 2, 0, 1, 2);
QPushButton *cancelButton = new QPushButton(tr("Cancel"));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
buttonLayout->addWidget(cancelButton);
QPushButton *okButton = new QPushButton(tr("Ok"));
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
buttonLayout->addWidget(okButton);
okButton->setDefault(true);
}
QString CreateDockWidgetDialog::objectName() const
{
return m_objectName->text();
} }
Qt::DockWidgetArea CreateDockWidgetDialog::location() const Qt::DockWidgetArea CreateDockWidgetDialog::location() const
@ -478,13 +432,13 @@ Qt::DockWidgetArea CreateDockWidgetDialog::location() const
void MainWindow::createDockWidget() void MainWindow::createDockWidget()
{ {
CreateDockWidgetDialog dialog(this); CreateDockWidgetDialog dialog(this);
int ret = dialog.exec(); if (dialog.exec() == QDialog::Rejected)
if (ret == QDialog::Rejected)
return; return;
QDockWidget *dw = new QDockWidget; QDockWidget *dw = new QDockWidget;
dw->setObjectName(dialog.objectName()); const QString name = dialog.enteredObjectName();
dw->setWindowTitle(dialog.objectName()); dw->setObjectName(name);
dw->setWindowTitle(name);
dw->setWidget(new QTextEdit); dw->setWidget(new QTextEdit);
Qt::DockWidgetArea area = dialog.location(); Qt::DockWidgetArea area = dialog.location();
@ -506,7 +460,7 @@ void MainWindow::createDockWidget()
extraDockWidgets.append(dw); extraDockWidgets.append(dw);
destroyDockWidgetMenu->setEnabled(true); destroyDockWidgetMenu->setEnabled(true);
destroyDockWidgetMenu->addAction(new QAction(dialog.objectName(), this)); destroyDockWidgetMenu->addAction(new QAction(name, this));
} }
void MainWindow::destroyDockWidget(QAction *action) void MainWindow::destroyDockWidget(QAction *action)

View File

@ -35,37 +35,25 @@
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QMainWindow>
#include <QTextEdit>
class ToolBar; class ToolBar;
QT_FORWARD_DECLARE_CLASS(QMenu) QT_FORWARD_DECLARE_CLASS(QMenu)
QT_FORWARD_DECLARE_CLASS(QSignalMapper)
class MainWindow : public QMainWindow class MainWindow : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
QTextEdit *center;
QList<ToolBar*> toolBars;
QMenu *dockWidgetMenu;
QMenu *mainWindowMenu;
QSignalMapper *mapper;
QList<QDockWidget*> extraDockWidgets;
QAction *createDockWidgetAction;
QMenu *destroyDockWidgetMenu;
public: public:
MainWindow(const QMap<QString, QSize> &customSizeHints, typedef QMap<QString, QSize> CustomSizeHintMap;
QWidget *parent = 0, Qt::WindowFlags flags = 0);
protected: explicit MainWindow(const CustomSizeHintMap &customSizeHints,
void showEvent(QShowEvent *event) Q_DECL_OVERRIDE; QWidget *parent = Q_NULLPTR,
Qt::WindowFlags flags = 0);
public slots: public slots:
void actionTriggered(QAction *action); void actionTriggered(QAction *action);
void saveLayout(); void saveLayout();
void loadLayout(); void loadLayout();
void setCorner(int id);
void switchLayoutDirection(); void switchLayoutDirection();
void setDockOptions(); void setDockOptions();
@ -75,8 +63,13 @@ public slots:
private: private:
void setupToolBar(); void setupToolBar();
void setupMenuBar(); void setupMenuBar();
void setupDockWidgets(const QMap<QString, QSize> &customSizeHints); void setupDockWidgets(const CustomSizeHintMap &customSizeHints);
QList<ToolBar*> toolBars;
QMenu *dockWidgetMenu;
QMenu *mainWindowMenu;
QList<QDockWidget *> extraDockWidgets;
QMenu *destroyDockWidgetMenu;
}; };
#endif // MAINWINDOW_H
#endif

View File

@ -63,15 +63,15 @@ static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color)
{ return genIcon(iconSize, QString::number(number), color); } { return genIcon(iconSize, QString::number(number), color); }
ToolBar::ToolBar(const QString &title, QWidget *parent) ToolBar::ToolBar(const QString &title, QWidget *parent)
: QToolBar(parent), spinbox(0), spinboxAction(0) : QToolBar(parent)
, spinbox(Q_NULLPTR)
, spinboxAction(Q_NULLPTR)
{ {
tip = 0;
setWindowTitle(title); setWindowTitle(title);
setObjectName(title); setObjectName(title);
setIconSize(QSize(32, 32)); setIconSize(QSize(32, 32));
QColor bg(palette().background().color());
menu = new QMenu("One", this); menu = new QMenu("One", this);
menu->setIcon(genIcon(iconSize(), 1, Qt::black)); menu->setIcon(genIcon(iconSize(), 1, Qt::black));
menu->addAction(genIcon(iconSize(), "A", Qt::blue), "A"); menu->addAction(genIcon(iconSize(), "A", Qt::blue), "A");
@ -90,43 +90,43 @@ ToolBar::ToolBar(const QString &title, QWidget *parent)
addAction(genIcon(iconSize(), 6, Qt::yellow), "Six"); addAction(genIcon(iconSize(), 6, Qt::yellow), "Six");
orderAction = new QAction(this); orderAction = new QAction(this);
orderAction->setText(tr("Order Items in Tool Bar")); orderAction->setText(tr("Order Items in Tool Bar"));
connect(orderAction, SIGNAL(triggered()), SLOT(order())); connect(orderAction, &QAction::triggered, this, &ToolBar::order);
randomizeAction = new QAction(this); randomizeAction = new QAction(this);
randomizeAction->setText(tr("Randomize Items in Tool Bar")); randomizeAction->setText(tr("Randomize Items in Tool Bar"));
connect(randomizeAction, SIGNAL(triggered()), SLOT(randomize())); connect(randomizeAction, &QAction::triggered, this, &ToolBar::randomize);
addSpinBoxAction = new QAction(this); addSpinBoxAction = new QAction(this);
addSpinBoxAction->setText(tr("Add Spin Box")); addSpinBoxAction->setText(tr("Add Spin Box"));
connect(addSpinBoxAction, SIGNAL(triggered()), SLOT(addSpinBox())); connect(addSpinBoxAction, &QAction::triggered, this, &ToolBar::addSpinBox);
removeSpinBoxAction = new QAction(this); removeSpinBoxAction = new QAction(this);
removeSpinBoxAction->setText(tr("Remove Spin Box")); removeSpinBoxAction->setText(tr("Remove Spin Box"));
removeSpinBoxAction->setEnabled(false); removeSpinBoxAction->setEnabled(false);
connect(removeSpinBoxAction, SIGNAL(triggered()), SLOT(removeSpinBox())); connect(removeSpinBoxAction, &QAction::triggered, this, &ToolBar::removeSpinBox);
movableAction = new QAction(tr("Movable"), this); movableAction = new QAction(tr("Movable"), this);
movableAction->setCheckable(true); movableAction->setCheckable(true);
connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool))); connect(movableAction, &QAction::triggered, this, &ToolBar::changeMovable);
allowedAreasActions = new QActionGroup(this); allowedAreasActions = new QActionGroup(this);
allowedAreasActions->setExclusive(false); allowedAreasActions->setExclusive(false);
allowLeftAction = new QAction(tr("Allow on Left"), this); allowLeftAction = new QAction(tr("Allow on Left"), this);
allowLeftAction->setCheckable(true); allowLeftAction->setCheckable(true);
connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool))); connect(allowLeftAction, &QAction::triggered, this, &ToolBar::allowLeft);
allowRightAction = new QAction(tr("Allow on Right"), this); allowRightAction = new QAction(tr("Allow on Right"), this);
allowRightAction->setCheckable(true); allowRightAction->setCheckable(true);
connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool))); connect(allowRightAction, &QAction::triggered, this, &ToolBar::allowRight);
allowTopAction = new QAction(tr("Allow on Top"), this); allowTopAction = new QAction(tr("Allow on Top"), this);
allowTopAction->setCheckable(true); allowTopAction->setCheckable(true);
connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool))); connect(allowTopAction, &QAction::triggered, this, &ToolBar::allowTop);
allowBottomAction = new QAction(tr("Allow on Bottom"), this); allowBottomAction = new QAction(tr("Allow on Bottom"), this);
allowBottomAction->setCheckable(true); allowBottomAction->setCheckable(true);
connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool))); connect(allowBottomAction, &QAction::triggered, this, &ToolBar::allowBottom);
allowedAreasActions->addAction(allowLeftAction); allowedAreasActions->addAction(allowLeftAction);
allowedAreasActions->addAction(allowRightAction); allowedAreasActions->addAction(allowRightAction);
@ -138,31 +138,28 @@ ToolBar::ToolBar(const QString &title, QWidget *parent)
leftAction = new QAction(tr("Place on Left") , this); leftAction = new QAction(tr("Place on Left") , this);
leftAction->setCheckable(true); leftAction->setCheckable(true);
connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool))); connect(leftAction, &QAction::triggered, this, &ToolBar::placeLeft);
rightAction = new QAction(tr("Place on Right") , this); rightAction = new QAction(tr("Place on Right") , this);
rightAction->setCheckable(true); rightAction->setCheckable(true);
connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool))); connect(rightAction, &QAction::triggered, this, &ToolBar::placeRight);
topAction = new QAction(tr("Place on Top") , this); topAction = new QAction(tr("Place on Top") , this);
topAction->setCheckable(true); topAction->setCheckable(true);
connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool))); connect(topAction, &QAction::triggered, this, &ToolBar::placeTop);
bottomAction = new QAction(tr("Place on Bottom") , this); bottomAction = new QAction(tr("Place on Bottom") , this);
bottomAction->setCheckable(true); bottomAction->setCheckable(true);
connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool))); connect(bottomAction, &QAction::triggered, this, &ToolBar::placeBottom);
areaActions->addAction(leftAction); areaActions->addAction(leftAction);
areaActions->addAction(rightAction); areaActions->addAction(rightAction);
areaActions->addAction(topAction); areaActions->addAction(topAction);
areaActions->addAction(bottomAction); areaActions->addAction(bottomAction);
toolBarBreakAction = new QAction(tr("Insert break"), this); connect(movableAction, &QAction::triggered, areaActions, &QActionGroup::setEnabled);
connect(toolBarBreakAction, SIGNAL(triggered(bool)), this, SLOT(insertToolBarBreak()));
connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool))); connect(movableAction, &QAction::triggered, allowedAreasActions, &QActionGroup::setEnabled);
connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool)));
menu = new QMenu(title, this); menu = new QMenu(title, this);
menu->addAction(toggleViewAction()); menu->addAction(toggleViewAction());
@ -179,9 +176,9 @@ ToolBar::ToolBar(const QString &title, QWidget *parent)
menu->addSeparator(); menu->addSeparator();
menu->addActions(areaActions->actions()); menu->addActions(areaActions->actions());
menu->addSeparator(); menu->addSeparator();
menu->addAction(toolBarBreakAction); menu->addAction(tr("Insert break"), this, &ToolBar::insertToolBarBreak);
connect(menu, SIGNAL(aboutToShow()), this, SLOT(updateMenu())); connect(menu, &QMenu::aboutToShow, this, &ToolBar::updateMenu);
randomize(); randomize();
} }
@ -223,10 +220,9 @@ void ToolBar::updateMenu()
void ToolBar::order() void ToolBar::order()
{ {
QList<QAction *> ordered, actions1 = actions(), QList<QAction *> ordered;
actions2 = findChildren<QAction *>(); QList<QAction *> actions1 = actions();
while (!actions2.isEmpty()) { foreach (QAction *action, findChildren<QAction *>()) {
QAction *action = actions2.takeFirst();
if (!actions1.contains(action)) if (!actions1.contains(action))
continue; continue;
actions1.removeAll(action); actions1.removeAll(action);
@ -241,7 +237,8 @@ void ToolBar::order()
void ToolBar::randomize() void ToolBar::randomize()
{ {
QList<QAction *> randomized, actions = this->actions(); QList<QAction *> randomized;
QList<QAction *> actions = this->actions();
while (!actions.isEmpty()) { while (!actions.isEmpty()) {
QAction *action = actions.takeAt(rand() % actions.size()); QAction *action = actions.takeAt(rand() % actions.size());
randomized.append(action); randomized.append(action);
@ -254,9 +251,8 @@ void ToolBar::randomize()
void ToolBar::addSpinBox() void ToolBar::addSpinBox()
{ {
if (!spinbox) { if (!spinbox)
spinbox = new QSpinBox(this); spinbox = new QSpinBox(this);
}
if (!spinboxAction) if (!spinboxAction)
spinboxAction = addWidget(spinbox); spinboxAction = addWidget(spinbox);
else else
@ -341,35 +337,3 @@ void ToolBar::insertToolBarBreak()
mainWindow->insertToolBarBreak(this); mainWindow->insertToolBarBreak(this);
} }
void ToolBar::enterEvent(QEvent*)
{
/*
These labels on top of toolbars look darn ugly
if (tip == 0) {
tip = new QLabel(windowTitle(), this);
QPalette pal = tip->palette();
QColor c = Qt::black;
c.setAlpha(100);
pal.setColor(QPalette::Window, c);
pal.setColor(QPalette::Foreground, Qt::white);
tip->setPalette(pal);
tip->setAutoFillBackground(true);
tip->setMargin(3);
tip->setText(windowTitle());
}
QPoint c = rect().center();
QSize hint = tip->sizeHint();
tip->setGeometry(c.x() - hint.width()/2, c.y() - hint.height()/2,
hint.width(), hint.height());
tip->show();
*/
}
void ToolBar::leaveEvent(QEvent*)
{
if (tip != 0)
tip->hide();
}

View File

@ -40,49 +40,15 @@ QT_FORWARD_DECLARE_CLASS(QAction)
QT_FORWARD_DECLARE_CLASS(QActionGroup) QT_FORWARD_DECLARE_CLASS(QActionGroup)
QT_FORWARD_DECLARE_CLASS(QMenu) QT_FORWARD_DECLARE_CLASS(QMenu)
QT_FORWARD_DECLARE_CLASS(QSpinBox) QT_FORWARD_DECLARE_CLASS(QSpinBox)
QT_FORWARD_DECLARE_CLASS(QLabel)
class ToolBar : public QToolBar class ToolBar : public QToolBar
{ {
Q_OBJECT Q_OBJECT
QSpinBox *spinbox;
QAction *spinboxAction;
QAction *orderAction;
QAction *randomizeAction;
QAction *addSpinBoxAction;
QAction *removeSpinBoxAction;
QAction *movableAction;
QActionGroup *allowedAreasActions;
QAction *allowLeftAction;
QAction *allowRightAction;
QAction *allowTopAction;
QAction *allowBottomAction;
QActionGroup *areaActions;
QAction *leftAction;
QAction *rightAction;
QAction *topAction;
QAction *bottomAction;
QAction *toolBarBreakAction;
public: public:
ToolBar(const QString &title, QWidget *parent); explicit ToolBar(const QString &title, QWidget *parent);
QMenu *menu; QMenu *toolbarMenu() const { return menu; }
protected:
void enterEvent(QEvent*) Q_DECL_OVERRIDE;
void leaveEvent(QEvent*) Q_DECL_OVERRIDE;
private:
void allow(Qt::ToolBarArea area, bool allow);
void place(Qt::ToolBarArea area, bool place);
QLabel *tip;
private slots: private slots:
void order(); void order();
@ -105,6 +71,32 @@ private slots:
void updateMenu(); void updateMenu();
void insertToolBarBreak(); void insertToolBarBreak();
private:
void allow(Qt::ToolBarArea area, bool allow);
void place(Qt::ToolBarArea area, bool place);
QSpinBox *spinbox;
QAction *spinboxAction;
QMenu *menu;
QAction *orderAction;
QAction *randomizeAction;
QAction *addSpinBoxAction;
QAction *removeSpinBoxAction;
QAction *movableAction;
QActionGroup *allowedAreasActions;
QAction *allowLeftAction;
QAction *allowRightAction;
QAction *allowTopAction;
QAction *allowBottomAction;
QActionGroup *areaActions;
QAction *leftAction;
QAction *rightAction;
QAction *topAction;
QAction *bottomAction;
}; };
#endif #endif // TOOLBAR_H