2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-18 10:30:20 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the demonstration applications of the Qt Toolkit.
|
|
|
|
**
|
2016-01-18 10:30:20 +00:00
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-18 10:30:20 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "toolbar.h"
|
|
|
|
|
|
|
|
#include <QMainWindow>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPainterPath>
|
|
|
|
#include <QSpinBox>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QToolTip>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &color)
|
|
|
|
{
|
|
|
|
int w = iconSize.width();
|
|
|
|
int h = iconSize.height();
|
|
|
|
|
|
|
|
QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
|
|
|
|
image.fill(0);
|
|
|
|
|
|
|
|
QPainter p(&image);
|
|
|
|
|
|
|
|
extern void render_qt_text(QPainter *, int, int, const QColor &);
|
|
|
|
render_qt_text(&p, w, h, color);
|
|
|
|
|
|
|
|
return QPixmap::fromImage(image, Qt::DiffuseDither | Qt::DiffuseAlphaDither);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-07-17 08:41:44 +00:00
|
|
|
: QToolBar(parent)
|
2017-09-18 09:49:52 +00:00
|
|
|
, spinbox(nullptr)
|
|
|
|
, spinboxAction(nullptr)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
setWindowTitle(title);
|
|
|
|
setObjectName(title);
|
|
|
|
|
|
|
|
setIconSize(QSize(32, 32));
|
|
|
|
|
|
|
|
menu = new QMenu("One", this);
|
|
|
|
menu->setIcon(genIcon(iconSize(), 1, Qt::black));
|
|
|
|
menu->addAction(genIcon(iconSize(), "A", Qt::blue), "A");
|
|
|
|
menu->addAction(genIcon(iconSize(), "B", Qt::blue), "B");
|
|
|
|
menu->addAction(genIcon(iconSize(), "C", Qt::blue), "C");
|
|
|
|
addAction(menu->menuAction());
|
|
|
|
|
|
|
|
QAction *two = addAction(genIcon(iconSize(), 2, Qt::white), "Two");
|
|
|
|
QFont boldFont;
|
|
|
|
boldFont.setBold(true);
|
|
|
|
two->setFont(boldFont);
|
|
|
|
|
|
|
|
addAction(genIcon(iconSize(), 3, Qt::red), "Three");
|
|
|
|
addAction(genIcon(iconSize(), 4, Qt::green), "Four");
|
|
|
|
addAction(genIcon(iconSize(), 5, Qt::blue), "Five");
|
|
|
|
addAction(genIcon(iconSize(), 6, Qt::yellow), "Six");
|
|
|
|
orderAction = new QAction(this);
|
|
|
|
orderAction->setText(tr("Order Items in Tool Bar"));
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(orderAction, &QAction::triggered, this, &ToolBar::order);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
randomizeAction = new QAction(this);
|
|
|
|
randomizeAction->setText(tr("Randomize Items in Tool Bar"));
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(randomizeAction, &QAction::triggered, this, &ToolBar::randomize);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
addSpinBoxAction = new QAction(this);
|
|
|
|
addSpinBoxAction->setText(tr("Add Spin Box"));
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(addSpinBoxAction, &QAction::triggered, this, &ToolBar::addSpinBox);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
removeSpinBoxAction = new QAction(this);
|
|
|
|
removeSpinBoxAction->setText(tr("Remove Spin Box"));
|
|
|
|
removeSpinBoxAction->setEnabled(false);
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(removeSpinBoxAction, &QAction::triggered, this, &ToolBar::removeSpinBox);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
movableAction = new QAction(tr("Movable"), this);
|
|
|
|
movableAction->setCheckable(true);
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(movableAction, &QAction::triggered, this, &ToolBar::changeMovable);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
allowedAreasActions = new QActionGroup(this);
|
|
|
|
allowedAreasActions->setExclusive(false);
|
|
|
|
|
|
|
|
allowLeftAction = new QAction(tr("Allow on Left"), this);
|
|
|
|
allowLeftAction->setCheckable(true);
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(allowLeftAction, &QAction::triggered, this, &ToolBar::allowLeft);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
allowRightAction = new QAction(tr("Allow on Right"), this);
|
|
|
|
allowRightAction->setCheckable(true);
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(allowRightAction, &QAction::triggered, this, &ToolBar::allowRight);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
allowTopAction = new QAction(tr("Allow on Top"), this);
|
|
|
|
allowTopAction->setCheckable(true);
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(allowTopAction, &QAction::triggered, this, &ToolBar::allowTop);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
allowBottomAction = new QAction(tr("Allow on Bottom"), this);
|
|
|
|
allowBottomAction->setCheckable(true);
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(allowBottomAction, &QAction::triggered, this, &ToolBar::allowBottom);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
allowedAreasActions->addAction(allowLeftAction);
|
|
|
|
allowedAreasActions->addAction(allowRightAction);
|
|
|
|
allowedAreasActions->addAction(allowTopAction);
|
|
|
|
allowedAreasActions->addAction(allowBottomAction);
|
|
|
|
|
|
|
|
areaActions = new QActionGroup(this);
|
|
|
|
areaActions->setExclusive(true);
|
|
|
|
|
|
|
|
leftAction = new QAction(tr("Place on Left") , this);
|
|
|
|
leftAction->setCheckable(true);
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(leftAction, &QAction::triggered, this, &ToolBar::placeLeft);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
rightAction = new QAction(tr("Place on Right") , this);
|
|
|
|
rightAction->setCheckable(true);
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(rightAction, &QAction::triggered, this, &ToolBar::placeRight);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
topAction = new QAction(tr("Place on Top") , this);
|
|
|
|
topAction->setCheckable(true);
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(topAction, &QAction::triggered, this, &ToolBar::placeTop);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
bottomAction = new QAction(tr("Place on Bottom") , this);
|
|
|
|
bottomAction->setCheckable(true);
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(bottomAction, &QAction::triggered, this, &ToolBar::placeBottom);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
areaActions->addAction(leftAction);
|
|
|
|
areaActions->addAction(rightAction);
|
|
|
|
areaActions->addAction(topAction);
|
|
|
|
areaActions->addAction(bottomAction);
|
|
|
|
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(movableAction, &QAction::triggered, areaActions, &QActionGroup::setEnabled);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(movableAction, &QAction::triggered, allowedAreasActions, &QActionGroup::setEnabled);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
menu = new QMenu(title, this);
|
|
|
|
menu->addAction(toggleViewAction());
|
|
|
|
menu->addSeparator();
|
|
|
|
menu->addAction(orderAction);
|
|
|
|
menu->addAction(randomizeAction);
|
|
|
|
menu->addSeparator();
|
|
|
|
menu->addAction(addSpinBoxAction);
|
|
|
|
menu->addAction(removeSpinBoxAction);
|
|
|
|
menu->addSeparator();
|
|
|
|
menu->addAction(movableAction);
|
|
|
|
menu->addSeparator();
|
|
|
|
menu->addActions(allowedAreasActions->actions());
|
|
|
|
menu->addSeparator();
|
|
|
|
menu->addActions(areaActions->actions());
|
|
|
|
menu->addSeparator();
|
2015-07-17 08:41:44 +00:00
|
|
|
menu->addAction(tr("Insert break"), this, &ToolBar::insertToolBarBreak);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-07-17 08:41:44 +00:00
|
|
|
connect(menu, &QMenu::aboutToShow, this, &ToolBar::updateMenu);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
randomize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolBar::updateMenu()
|
|
|
|
{
|
|
|
|
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
|
|
|
|
Q_ASSERT(mainWindow != 0);
|
|
|
|
|
|
|
|
const Qt::ToolBarArea area = mainWindow->toolBarArea(this);
|
|
|
|
const Qt::ToolBarAreas areas = allowedAreas();
|
|
|
|
|
|
|
|
movableAction->setChecked(isMovable());
|
|
|
|
|
|
|
|
allowLeftAction->setChecked(isAreaAllowed(Qt::LeftToolBarArea));
|
|
|
|
allowRightAction->setChecked(isAreaAllowed(Qt::RightToolBarArea));
|
|
|
|
allowTopAction->setChecked(isAreaAllowed(Qt::TopToolBarArea));
|
|
|
|
allowBottomAction->setChecked(isAreaAllowed(Qt::BottomToolBarArea));
|
|
|
|
|
|
|
|
if (allowedAreasActions->isEnabled()) {
|
|
|
|
allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
|
|
|
|
allowRightAction->setEnabled(area != Qt::RightToolBarArea);
|
|
|
|
allowTopAction->setEnabled(area != Qt::TopToolBarArea);
|
|
|
|
allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
|
|
|
|
}
|
|
|
|
|
|
|
|
leftAction->setChecked(area == Qt::LeftToolBarArea);
|
|
|
|
rightAction->setChecked(area == Qt::RightToolBarArea);
|
|
|
|
topAction->setChecked(area == Qt::TopToolBarArea);
|
|
|
|
bottomAction->setChecked(area == Qt::BottomToolBarArea);
|
|
|
|
|
|
|
|
if (areaActions->isEnabled()) {
|
|
|
|
leftAction->setEnabled(areas & Qt::LeftToolBarArea);
|
|
|
|
rightAction->setEnabled(areas & Qt::RightToolBarArea);
|
|
|
|
topAction->setEnabled(areas & Qt::TopToolBarArea);
|
|
|
|
bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolBar::order()
|
|
|
|
{
|
2015-07-17 08:41:44 +00:00
|
|
|
QList<QAction *> ordered;
|
|
|
|
QList<QAction *> actions1 = actions();
|
|
|
|
foreach (QAction *action, findChildren<QAction *>()) {
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!actions1.contains(action))
|
|
|
|
continue;
|
|
|
|
actions1.removeAll(action);
|
|
|
|
ordered.append(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear();
|
|
|
|
addActions(ordered);
|
|
|
|
|
|
|
|
orderAction->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolBar::randomize()
|
|
|
|
{
|
2015-07-17 08:41:44 +00:00
|
|
|
QList<QAction *> randomized;
|
|
|
|
QList<QAction *> actions = this->actions();
|
2011-04-27 10:05:43 +00:00
|
|
|
while (!actions.isEmpty()) {
|
|
|
|
QAction *action = actions.takeAt(rand() % actions.size());
|
|
|
|
randomized.append(action);
|
|
|
|
}
|
|
|
|
clear();
|
|
|
|
addActions(randomized);
|
|
|
|
|
|
|
|
orderAction->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolBar::addSpinBox()
|
|
|
|
{
|
2015-07-17 08:41:44 +00:00
|
|
|
if (!spinbox)
|
2011-04-27 10:05:43 +00:00
|
|
|
spinbox = new QSpinBox(this);
|
|
|
|
if (!spinboxAction)
|
|
|
|
spinboxAction = addWidget(spinbox);
|
|
|
|
else
|
|
|
|
addAction(spinboxAction);
|
|
|
|
|
|
|
|
addSpinBoxAction->setEnabled(false);
|
|
|
|
removeSpinBoxAction->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolBar::removeSpinBox()
|
|
|
|
{
|
|
|
|
if (spinboxAction)
|
|
|
|
removeAction(spinboxAction);
|
|
|
|
|
|
|
|
addSpinBoxAction->setEnabled(true);
|
|
|
|
removeSpinBoxAction->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolBar::allow(Qt::ToolBarArea area, bool a)
|
|
|
|
{
|
|
|
|
Qt::ToolBarAreas areas = allowedAreas();
|
|
|
|
areas = a ? areas | area : areas & ~area;
|
|
|
|
setAllowedAreas(areas);
|
|
|
|
|
|
|
|
if (areaActions->isEnabled()) {
|
|
|
|
leftAction->setEnabled(areas & Qt::LeftToolBarArea);
|
|
|
|
rightAction->setEnabled(areas & Qt::RightToolBarArea);
|
|
|
|
topAction->setEnabled(areas & Qt::TopToolBarArea);
|
|
|
|
bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolBar::place(Qt::ToolBarArea area, bool p)
|
|
|
|
{
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
|
|
|
|
Q_ASSERT(mainWindow != 0);
|
|
|
|
|
|
|
|
mainWindow->addToolBar(area, this);
|
|
|
|
|
|
|
|
if (allowedAreasActions->isEnabled()) {
|
|
|
|
allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
|
|
|
|
allowRightAction->setEnabled(area != Qt::RightToolBarArea);
|
|
|
|
allowTopAction->setEnabled(area != Qt::TopToolBarArea);
|
|
|
|
allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolBar::changeMovable(bool movable)
|
|
|
|
{ setMovable(movable); }
|
|
|
|
|
|
|
|
void ToolBar::allowLeft(bool a)
|
|
|
|
{ allow(Qt::LeftToolBarArea, a); }
|
|
|
|
|
|
|
|
void ToolBar::allowRight(bool a)
|
|
|
|
{ allow(Qt::RightToolBarArea, a); }
|
|
|
|
|
|
|
|
void ToolBar::allowTop(bool a)
|
|
|
|
{ allow(Qt::TopToolBarArea, a); }
|
|
|
|
|
|
|
|
void ToolBar::allowBottom(bool a)
|
|
|
|
{ allow(Qt::BottomToolBarArea, a); }
|
|
|
|
|
|
|
|
void ToolBar::placeLeft(bool p)
|
|
|
|
{ place(Qt::LeftToolBarArea, p); }
|
|
|
|
|
|
|
|
void ToolBar::placeRight(bool p)
|
|
|
|
{ place(Qt::RightToolBarArea, p); }
|
|
|
|
|
|
|
|
void ToolBar::placeTop(bool p)
|
|
|
|
{ place(Qt::TopToolBarArea, p); }
|
|
|
|
|
|
|
|
void ToolBar::placeBottom(bool p)
|
|
|
|
{ place(Qt::BottomToolBarArea, p); }
|
|
|
|
|
|
|
|
void ToolBar::insertToolBarBreak()
|
|
|
|
{
|
|
|
|
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
|
|
|
|
Q_ASSERT(mainWindow != 0);
|
|
|
|
|
|
|
|
mainWindow->insertToolBarBreak(this);
|
|
|
|
}
|