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

View File

@ -44,47 +44,15 @@ class ColorSwatch : public QDockWidget
{
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:
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);
QMenu *colorSwatchMenu() const { return menu; }
protected:
virtual void contextMenuEvent(QContextMenuEvent *event) Q_DECL_OVERRIDE;
virtual void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE;
private:
void allow(Qt::DockWidgetArea area, bool allow);
void place(Qt::DockWidgetArea area, bool place);
void contextMenuEvent(QContextMenuEvent *event) Q_DECL_OVERRIDE;
void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE;
private slots:
void changeClosable(bool on);
@ -106,25 +74,57 @@ private slots:
void splitInto(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
{
Q_OBJECT
public:
BlueTitleBar(QWidget *parent = 0);
explicit BlueTitleBar(QWidget *parent = Q_NULLPTR);
QSize sizeHint() const Q_DECL_OVERRIDE { return minimumSizeHint(); }
QSize minimumSizeHint() const Q_DECL_OVERRIDE;
protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
public slots:
void updateMask();
private:
QPixmap leftPm, centerPm, rightPm;
const QPixmap leftPm;
const QPixmap centerPm;
const QPixmap rightPm;
};
#endif
#endif // COLORSWATCH_H

View File

@ -37,9 +37,10 @@
#include <QPainterPath>
#include <QPainter>
#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;
path.moveTo(-0.083695, 0.283849);
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);
}
void usage()
static void usage()
{
qWarning() << "Usage: mainwindow [-SizeHint<color> <width>x<height>] ...";
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;
for (int i = 1; i < argc; ++i) {
QString arg = QString::fromLocal8Bit(argv[i]);
result->clear();
const int argumentCount = arguments.size();
for (int i = 1; i < argumentCount; ++i) {
const QString &arg = arguments.at(i);
if (arg.startsWith(QLatin1String("-SizeHint"))) {
QString name = arg.mid(9);
const QString name = arg.mid(9);
if (name.isEmpty())
usage();
if (++i == argc)
usage();
QString sizeStr = QString::fromLocal8Bit(argv[i]);
int idx = sizeStr.indexOf(QLatin1Char('x'));
return CommandLineArgumentsError;
if (++i == argumentCount)
return CommandLineArgumentsError;
const QString sizeStr = arguments.at(i);
const int idx = sizeStr.indexOf(QLatin1Char('x'));
if (idx == -1)
usage();
return CommandLineArgumentsError;
bool ok;
int w = sizeStr.left(idx).toInt(&ok);
const int w = sizeStr.leftRef(idx).toInt(&ok);
if (!ok)
usage();
int h = sizeStr.mid(idx + 1).toInt(&ok);
return CommandLineArgumentsError;
const int h = sizeStr.midRef(idx + 1).toInt(&ok);
if (!ok)
usage();
result[name] = QSize(w, h);
return CommandLineArgumentsError;
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)
{
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);
mainWin.resize(800, 600);
mainWin.show();

View File

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

View File

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

View File

@ -63,15 +63,15 @@ static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color)
{ return genIcon(iconSize, QString::number(number), color); }
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);
setObjectName(title);
setIconSize(QSize(32, 32));
QColor bg(palette().background().color());
menu = new QMenu("One", this);
menu->setIcon(genIcon(iconSize(), 1, Qt::black));
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");
orderAction = new QAction(this);
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->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->setText(tr("Add Spin Box"));
connect(addSpinBoxAction, SIGNAL(triggered()), SLOT(addSpinBox()));
connect(addSpinBoxAction, &QAction::triggered, this, &ToolBar::addSpinBox);
removeSpinBoxAction = new QAction(this);
removeSpinBoxAction->setText(tr("Remove Spin Box"));
removeSpinBoxAction->setEnabled(false);
connect(removeSpinBoxAction, SIGNAL(triggered()), SLOT(removeSpinBox()));
connect(removeSpinBoxAction, &QAction::triggered, this, &ToolBar::removeSpinBox);
movableAction = new QAction(tr("Movable"), this);
movableAction->setCheckable(true);
connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool)));
connect(movableAction, &QAction::triggered, this, &ToolBar::changeMovable);
allowedAreasActions = new QActionGroup(this);
allowedAreasActions->setExclusive(false);
allowLeftAction = new QAction(tr("Allow on Left"), this);
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->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->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->setCheckable(true);
connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool)));
connect(allowBottomAction, &QAction::triggered, this, &ToolBar::allowBottom);
allowedAreasActions->addAction(allowLeftAction);
allowedAreasActions->addAction(allowRightAction);
@ -138,31 +138,28 @@ ToolBar::ToolBar(const QString &title, QWidget *parent)
leftAction = new QAction(tr("Place on Left") , this);
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->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->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->setCheckable(true);
connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool)));
connect(bottomAction, &QAction::triggered, this, &ToolBar::placeBottom);
areaActions->addAction(leftAction);
areaActions->addAction(rightAction);
areaActions->addAction(topAction);
areaActions->addAction(bottomAction);
toolBarBreakAction = new QAction(tr("Insert break"), this);
connect(toolBarBreakAction, SIGNAL(triggered(bool)), this, SLOT(insertToolBarBreak()));
connect(movableAction, &QAction::triggered, areaActions, &QActionGroup::setEnabled);
connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool)));
connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool)));
connect(movableAction, &QAction::triggered, allowedAreasActions, &QActionGroup::setEnabled);
menu = new QMenu(title, this);
menu->addAction(toggleViewAction());
@ -179,9 +176,9 @@ ToolBar::ToolBar(const QString &title, QWidget *parent)
menu->addSeparator();
menu->addActions(areaActions->actions());
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();
}
@ -223,10 +220,9 @@ void ToolBar::updateMenu()
void ToolBar::order()
{
QList<QAction *> ordered, actions1 = actions(),
actions2 = findChildren<QAction *>();
while (!actions2.isEmpty()) {
QAction *action = actions2.takeFirst();
QList<QAction *> ordered;
QList<QAction *> actions1 = actions();
foreach (QAction *action, findChildren<QAction *>()) {
if (!actions1.contains(action))
continue;
actions1.removeAll(action);
@ -241,7 +237,8 @@ void ToolBar::order()
void ToolBar::randomize()
{
QList<QAction *> randomized, actions = this->actions();
QList<QAction *> randomized;
QList<QAction *> actions = this->actions();
while (!actions.isEmpty()) {
QAction *action = actions.takeAt(rand() % actions.size());
randomized.append(action);
@ -254,9 +251,8 @@ void ToolBar::randomize()
void ToolBar::addSpinBox()
{
if (!spinbox) {
if (!spinbox)
spinbox = new QSpinBox(this);
}
if (!spinboxAction)
spinboxAction = addWidget(spinbox);
else
@ -341,35 +337,3 @@ void ToolBar::insertToolBarBreak()
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(QMenu)
QT_FORWARD_DECLARE_CLASS(QSpinBox)
QT_FORWARD_DECLARE_CLASS(QLabel)
class ToolBar : public QToolBar
{
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:
ToolBar(const QString &title, QWidget *parent);
explicit ToolBar(const QString &title, QWidget *parent);
QMenu *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;
QMenu *toolbarMenu() const { return menu; }
private slots:
void order();
@ -105,6 +71,32 @@ private slots:
void updateMenu();
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