Main Window▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** 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
** a written agreement between you and The Qt Company. For licensing terms
** 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."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"toolbar.h"
#include <QRandomGenerator>
#include <QMainWindow>
#include <QMenu>
#include <QPainter>
#include <QPainterPath>
#include <QSpinBox>
#include <QLabel>
#include <QToolTip>
#include <stdlib.h>
static
QPixmap genIcon(const
QSize &
amp;iconSize, const
QString &
amp;, const
QColor &
amp;color, qreal pixelRatio)
{
int
w =
qRound(iconSize.width() *
pixelRatio);
int
h =
qRound(iconSize.height() *
pixelRatio);
QImage image(w, h, QImage::
Format_ARGB32_Premultiplied);
image.fill(0
);
QPainter p(&
amp;image);
extern
void
render_qt_text(QPainter *
, int
, int
, const
QColor &
amp;);
render_qt_text(&
amp;p, w, h, color);
QPixmap pm =
QPixmap::
fromImage(image, Qt::
DiffuseDither |
Qt::
DiffuseAlphaDither);
pm.setDevicePixelRatio(pixelRatio);
return
pm;
}
static
QPixmap genIcon(const
QSize &
amp;iconSize, int
number, const
QColor &
amp;color, qreal pixelRatio)
{
return
genIcon(iconSize, QString::
number(number), color, pixelRatio); }
ToolBar::
ToolBar(const
QString &
amp;title, QWidget *
parent)
:
QToolBar(parent)
, spinbox(nullptr
)
, spinboxAction(nullptr
)
{
setWindowTitle(title);
setObjectName(title);
setIconSize(QSize(32
, 32
));
qreal dpr =
devicePixelRatioF();
menu =
new
QMenu("One"
, this
);
menu-&
gt;setIcon(genIcon(iconSize(), 1
, Qt::
black, dpr));
menu-&
gt;addAction(genIcon(iconSize(), "A"
, Qt::
blue, dpr), "A"
);
menu-&
gt;addAction(genIcon(iconSize(), "B"
, Qt::
blue, dpr), "B"
);
menu-&
gt;addAction(genIcon(iconSize(), "C"
, Qt::
blue, dpr), "C"
);
addAction(menu-&
gt;menuAction());
QAction *
two =
addAction(genIcon(iconSize(), 2
, Qt::
white, dpr), "Two"
);
QFont boldFont;
boldFont.setBold(true
);
two-&
gt;setFont(boldFont);
addAction(genIcon(iconSize(), 3
, Qt::
red, dpr), "Three"
);
addAction(genIcon(iconSize(), 4
, Qt::
green, dpr), "Four"
);
addAction(genIcon(iconSize(), 5
, Qt::
blue, dpr), "Five"
);
addAction(genIcon(iconSize(), 6
, Qt::
yellow, dpr), "Six"
);
orderAction =
new
QAction(this
);
orderAction-&
gt;setText(tr("Order Items in Tool Bar"
));
connect(orderAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
order);
randomizeAction =
new
QAction(this
);
randomizeAction-&
gt;setText(tr("Randomize Items in Tool Bar"
));
connect(randomizeAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
randomize);
addSpinBoxAction =
new
QAction(this
);
addSpinBoxAction-&
gt;setText(tr("Add Spin Box"
));
connect(addSpinBoxAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
addSpinBox);
removeSpinBoxAction =
new
QAction(this
);
removeSpinBoxAction-&
gt;setText(tr("Remove Spin Box"
));
removeSpinBoxAction-&
gt;setEnabled(false
);
connect(removeSpinBoxAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
removeSpinBox);
movableAction =
new
QAction(tr("Movable"
), this
);
movableAction-&
gt;setCheckable(true
);
connect(movableAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
changeMovable);
allowedAreasActions =
new
QActionGroup(this
);
allowedAreasActions-&
gt;setExclusive(false
);
allowLeftAction =
new
QAction(tr("Allow on Left"
), this
);
allowLeftAction-&
gt;setCheckable(true
);
connect(allowLeftAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
allowLeft);
allowRightAction =
new
QAction(tr("Allow on Right"
), this
);
allowRightAction-&
gt;setCheckable(true
);
connect(allowRightAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
allowRight);
allowTopAction =
new
QAction(tr("Allow on Top"
), this
);
allowTopAction-&
gt;setCheckable(true
);
connect(allowTopAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
allowTop);
allowBottomAction =
new
QAction(tr("Allow on Bottom"
), this
);
allowBottomAction-&
gt;setCheckable(true
);
connect(allowBottomAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
allowBottom);
allowedAreasActions-&
gt;addAction(allowLeftAction);
allowedAreasActions-&
gt;addAction(allowRightAction);
allowedAreasActions-&
gt;addAction(allowTopAction);
allowedAreasActions-&
gt;addAction(allowBottomAction);
areaActions =
new
QActionGroup(this
);
areaActions-&
gt;setExclusive(true
);
leftAction =
new
QAction(tr("Place on Left"
) , this
);
leftAction-&
gt;setCheckable(true
);
connect(leftAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
placeLeft);
rightAction =
new
QAction(tr("Place on Right"
) , this
);
rightAction-&
gt;setCheckable(true
);
connect(rightAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
placeRight);
topAction =
new
QAction(tr("Place on Top"
) , this
);
topAction-&
gt;setCheckable(true
);
connect(topAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
placeTop);
bottomAction =
new
QAction(tr("Place on Bottom"
) , this
);
bottomAction-&
gt;setCheckable(true
);
connect(bottomAction, &
amp;QAction::
triggered, this
, &
amp;ToolBar::
placeBottom);
areaActions-&
gt;addAction(leftAction);
areaActions-&
gt;addAction(rightAction);
areaActions-&
gt;addAction(topAction);
areaActions-&
gt;addAction(bottomAction);
connect(movableAction, &
amp;QAction::
triggered, areaActions, &
amp;QActionGroup::
setEnabled);
connect(movableAction, &
amp;QAction::
triggered, allowedAreasActions, &
amp;QActionGroup::
setEnabled);
menu =
new
QMenu(title, this
);
menu-&
gt;addAction(toggleViewAction());
menu-&
gt;addSeparator();
menu-&
gt;addAction(orderAction);
menu-&
gt;addAction(randomizeAction);
menu-&
gt;addSeparator();
menu-&
gt;addAction(addSpinBoxAction);
menu-&
gt;addAction(removeSpinBoxAction);
menu-&
gt;addSeparator();
menu-&
gt;addAction(movableAction);
menu-&
gt;addSeparator();
menu-&
gt;addActions(allowedAreasActions-&
gt;actions());
menu-&
gt;addSeparator();
menu-&
gt;addActions(areaActions-&
gt;actions());
menu-&
gt;addSeparator();
menu-&
gt;addAction(tr("Insert break"
), this
, &
amp;ToolBar::
insertToolBarBreak);
connect(menu, &
amp;QMenu::
aboutToShow, this
, &
amp;ToolBar::
updateMenu);
randomize();
}
void
ToolBar::
updateMenu()
{
QMainWindow *
mainWindow =
qobject_cast&
lt;QMainWindow *&
gt;(parentWidget());
Q_ASSERT(mainWindow !=
0
);
const
Qt::
ToolBarArea area =
mainWindow-&
gt;toolBarArea(this
);
const
Qt::
ToolBarAreas areas =
allowedAreas();
movableAction-&
gt;setChecked(isMovable());
allowLeftAction-&
gt;setChecked(isAreaAllowed(Qt::
LeftToolBarArea));
allowRightAction-&
gt;setChecked(isAreaAllowed(Qt::
RightToolBarArea));
allowTopAction-&
gt;setChecked(isAreaAllowed(Qt::
TopToolBarArea));
allowBottomAction-&
gt;setChecked(isAreaAllowed(Qt::
BottomToolBarArea));
if
(allowedAreasActions-&
gt;isEnabled()) {
allowLeftAction-&
gt;setEnabled(area !=
Qt::
LeftToolBarArea);
allowRightAction-&
gt;setEnabled(area !=
Qt::
RightToolBarArea);
allowTopAction-&
gt;setEnabled(area !=
Qt::
TopToolBarArea);
allowBottomAction-&
gt;setEnabled(area !=
Qt::
BottomToolBarArea);
}
leftAction-&
gt;setChecked(area ==
Qt::
LeftToolBarArea);
rightAction-&
gt;setChecked(area ==
Qt::
RightToolBarArea);
topAction-&
gt;setChecked(area ==
Qt::
TopToolBarArea);
bottomAction-&
gt;setChecked(area ==
Qt::
BottomToolBarArea);
if
(areaActions-&
gt;isEnabled()) {
leftAction-&
gt;setEnabled(areas &
amp; Qt::
LeftToolBarArea);
rightAction-&
gt;setEnabled(areas &
amp; Qt::
RightToolBarArea);
topAction-&
gt;setEnabled(areas &
amp; Qt::
TopToolBarArea);
bottomAction-&
gt;setEnabled(areas &
amp; Qt::
BottomToolBarArea);
}
}
void
ToolBar::
order()
{
QList&
lt;QAction *&
gt; ordered;
QList&
lt;QAction *&
gt; actions1 =
actions();
foreach (QAction *
action, findChildren&
lt;QAction *&
gt;()) {
if
(!
actions1.contains(action))
continue
;
actions1.removeAll(action);
ordered.append(action);
}
clear();
addActions(ordered);
orderAction-&
gt;setEnabled(false
);
}
void
ToolBar::
randomize()
{
QList&
lt;QAction *&
gt; randomized;
QList&
lt;QAction *&
gt; actions =
this
-&
gt;actions();
while
(!
actions.isEmpty()) {
QAction *
action =
actions.takeAt(QRandomGenerator::
global()-&
gt;bounded(actions.size()));
randomized.append(action);
}
clear();
addActions(randomized);
orderAction-&
gt;setEnabled(true
);
}
void
ToolBar::
addSpinBox()
{
if
(!
spinbox)
spinbox =
new
QSpinBox(this
);
if
(!
spinboxAction)
spinboxAction =
addWidget(spinbox);
else
addAction(spinboxAction);
addSpinBoxAction-&
gt;setEnabled(false
);
removeSpinBoxAction-&
gt;setEnabled(true
);
}
void
ToolBar::
removeSpinBox()
{
if
(spinboxAction)
removeAction(spinboxAction);
addSpinBoxAction-&
gt;setEnabled(true
);
removeSpinBoxAction-&
gt;setEnabled(false
);
}
void
ToolBar::
allow(Qt::
ToolBarArea area, bool
a)
{
Qt::
ToolBarAreas areas =
allowedAreas();
areas =
a ? areas |
area : areas &
amp; ~
area;
setAllowedAreas(areas);
if
(areaActions-&
gt;isEnabled()) {
leftAction-&
gt;setEnabled(areas &
amp; Qt::
LeftToolBarArea);
rightAction-&
gt;setEnabled(areas &
amp; Qt::
RightToolBarArea);
topAction-&
gt;setEnabled(areas &
amp; Qt::
TopToolBarArea);
bottomAction-&
gt;setEnabled(areas &
amp; Qt::
BottomToolBarArea);
}
}
void
ToolBar::
place(Qt::
ToolBarArea area, bool
p)
{
if
(!
p)
return
;
QMainWindow *
mainWindow =
qobject_cast&
lt;QMainWindow *&
gt;(parentWidget());
Q_ASSERT(mainWindow !=
0
);
mainWindow-&
gt;addToolBar(area, this
);
if
(allowedAreasActions-&
gt;isEnabled()) {
allowLeftAction-&
gt;setEnabled(area !=
Qt::
LeftToolBarArea);
allowRightAction-&
gt;setEnabled(area !=
Qt::
RightToolBarArea);
allowTopAction-&
gt;setEnabled(area !=
Qt::
TopToolBarArea);
allowBottomAction-&
gt;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&
lt;QMainWindow *&
gt;(parentWidget());
Q_ASSERT(mainWindow !=
0
);
mainWindow-&
gt;insertToolBarBreak(this
);
}