1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "toolbar.h"
52
53#include <QRandomGenerator>
54
55#include <QMainWindow>
56#include <QMenu>
57#include <QPainter>
58#include <QPainterPath>
59#include <QSpinBox>
60#include <QLabel>
61#include <QToolTip>
62
63#include <stdlib.h>
64
65static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &color, qreal pixelRatio)
66{
67 int w = qRound(iconSize.width() * pixelRatio);
68 int h = qRound(iconSize.height() * pixelRatio);
69
70 QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
71 image.fill(0);
72
73 QPainter p(&image);
74
75 extern void render_qt_text(QPainter *, int, int, const QColor &);
76 render_qt_text(&p, w, h, color);
77
78 QPixmap pm = QPixmap::fromImage(image, Qt::DiffuseDither | Qt::DiffuseAlphaDither);
79 pm.setDevicePixelRatio(pixelRatio);
80 return pm;
81}
82
83static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color, qreal pixelRatio)
84{ return genIcon(iconSize, QString::number(number), color, pixelRatio); }
85
86ToolBar::ToolBar(const QString &title, QWidget *parent)
87 : QToolBar(parent)
88 , spinbox(nullptr)
89 , spinboxAction(nullptr)
90{
91 setWindowTitle(title);
92 setObjectName(title);
93
94 setIconSize(QSize(32, 32));
95
96 qreal dpr = devicePixelRatioF();
97 menu = new QMenu("One", this);
98 menu->setIcon(genIcon(iconSize(), 1, Qt::black, dpr));
99 menu->addAction(genIcon(iconSize(), "A", Qt::blue, dpr), "A");
100 menu->addAction(genIcon(iconSize(), "B", Qt::blue, dpr), "B");
101 menu->addAction(genIcon(iconSize(), "C", Qt::blue, dpr), "C");
102 addAction(menu->menuAction());
103
104 QAction *two = addAction(genIcon(iconSize(), 2, Qt::white, dpr), "Two");
105 QFont boldFont;
106 boldFont.setBold(true);
107 two->setFont(boldFont);
108
109 addAction(genIcon(iconSize(), 3, Qt::red, dpr), "Three");
110 addAction(genIcon(iconSize(), 4, Qt::green, dpr), "Four");
111 addAction(genIcon(iconSize(), 5, Qt::blue, dpr), "Five");
112 addAction(genIcon(iconSize(), 6, Qt::yellow, dpr), "Six");
113 orderAction = new QAction(this);
114 orderAction->setText(tr("Order Items in Tool Bar"));
115 connect(orderAction, &QAction::triggered, this, &ToolBar::order);
116
117 randomizeAction = new QAction(this);
118 randomizeAction->setText(tr("Randomize Items in Tool Bar"));
119 connect(randomizeAction, &QAction::triggered, this, &ToolBar::randomize);
120
121 addSpinBoxAction = new QAction(this);
122 addSpinBoxAction->setText(tr("Add Spin Box"));
123 connect(addSpinBoxAction, &QAction::triggered, this, &ToolBar::addSpinBox);
124
125 removeSpinBoxAction = new QAction(this);
126 removeSpinBoxAction->setText(tr("Remove Spin Box"));
127 removeSpinBoxAction->setEnabled(false);
128 connect(removeSpinBoxAction, &QAction::triggered, this, &ToolBar::removeSpinBox);
129
130 movableAction = new QAction(tr("Movable"), this);
131 movableAction->setCheckable(true);
132 connect(movableAction, &QAction::triggered, this, &ToolBar::changeMovable);
133
134 allowedAreasActions = new QActionGroup(this);
135 allowedAreasActions->setExclusive(false);
136
137 allowLeftAction = new QAction(tr("Allow on Left"), this);
138 allowLeftAction->setCheckable(true);
139 connect(allowLeftAction, &QAction::triggered, this, &ToolBar::allowLeft);
140
141 allowRightAction = new QAction(tr("Allow on Right"), this);
142 allowRightAction->setCheckable(true);
143 connect(allowRightAction, &QAction::triggered, this, &ToolBar::allowRight);
144
145 allowTopAction = new QAction(tr("Allow on Top"), this);
146 allowTopAction->setCheckable(true);
147 connect(allowTopAction, &QAction::triggered, this, &ToolBar::allowTop);
148
149 allowBottomAction = new QAction(tr("Allow on Bottom"), this);
150 allowBottomAction->setCheckable(true);
151 connect(allowBottomAction, &QAction::triggered, this, &ToolBar::allowBottom);
152
153 allowedAreasActions->addAction(allowLeftAction);
154 allowedAreasActions->addAction(allowRightAction);
155 allowedAreasActions->addAction(allowTopAction);
156 allowedAreasActions->addAction(allowBottomAction);
157
158 areaActions = new QActionGroup(this);
159 areaActions->setExclusive(true);
160
161 leftAction = new QAction(tr("Place on Left") , this);
162 leftAction->setCheckable(true);
163 connect(leftAction, &QAction::triggered, this, &ToolBar::placeLeft);
164
165 rightAction = new QAction(tr("Place on Right") , this);
166 rightAction->setCheckable(true);
167 connect(rightAction, &QAction::triggered, this, &ToolBar::placeRight);
168
169 topAction = new QAction(tr("Place on Top") , this);
170 topAction->setCheckable(true);
171 connect(topAction, &QAction::triggered, this, &ToolBar::placeTop);
172
173 bottomAction = new QAction(tr("Place on Bottom") , this);
174 bottomAction->setCheckable(true);
175 connect(bottomAction, &QAction::triggered, this, &ToolBar::placeBottom);
176
177 areaActions->addAction(leftAction);
178 areaActions->addAction(rightAction);
179 areaActions->addAction(topAction);
180 areaActions->addAction(bottomAction);
181
182 connect(movableAction, &QAction::triggered, areaActions, &QActionGroup::setEnabled);
183
184 connect(movableAction, &QAction::triggered, allowedAreasActions, &QActionGroup::setEnabled);
185
186 menu = new QMenu(title, this);
187 menu->addAction(toggleViewAction());
188 menu->addSeparator();
189 menu->addAction(orderAction);
190 menu->addAction(randomizeAction);
191 menu->addSeparator();
192 menu->addAction(addSpinBoxAction);
193 menu->addAction(removeSpinBoxAction);
194 menu->addSeparator();
195 menu->addAction(movableAction);
196 menu->addSeparator();
197 menu->addActions(allowedAreasActions->actions());
198 menu->addSeparator();
199 menu->addActions(areaActions->actions());
200 menu->addSeparator();
201 menu->addAction(tr("Insert break"), this, &ToolBar::insertToolBarBreak);
202
203 connect(menu, &QMenu::aboutToShow, this, &ToolBar::updateMenu);
204
205 randomize();
206}
207
208void ToolBar::updateMenu()
209{
210 QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
211 Q_ASSERT(mainWindow != 0);
212
213 const Qt::ToolBarArea area = mainWindow->toolBarArea(this);
214 const Qt::ToolBarAreas areas = allowedAreas();
215
216 movableAction->setChecked(isMovable());
217
218 allowLeftAction->setChecked(isAreaAllowed(Qt::LeftToolBarArea));
219 allowRightAction->setChecked(isAreaAllowed(Qt::RightToolBarArea));
220 allowTopAction->setChecked(isAreaAllowed(Qt::TopToolBarArea));
221 allowBottomAction->setChecked(isAreaAllowed(Qt::BottomToolBarArea));
222
223 if (allowedAreasActions->isEnabled()) {
224 allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
225 allowRightAction->setEnabled(area != Qt::RightToolBarArea);
226 allowTopAction->setEnabled(area != Qt::TopToolBarArea);
227 allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
228 }
229
230 leftAction->setChecked(area == Qt::LeftToolBarArea);
231 rightAction->setChecked(area == Qt::RightToolBarArea);
232 topAction->setChecked(area == Qt::TopToolBarArea);
233 bottomAction->setChecked(area == Qt::BottomToolBarArea);
234
235 if (areaActions->isEnabled()) {
236 leftAction->setEnabled(areas & Qt::LeftToolBarArea);
237 rightAction->setEnabled(areas & Qt::RightToolBarArea);
238 topAction->setEnabled(areas & Qt::TopToolBarArea);
239 bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
240 }
241}
242
243void ToolBar::order()
244{
245 QList<QAction *> ordered;
246 QList<QAction *> actions1 = actions();
247 foreach (QAction *action, findChildren<QAction *>()) {
248 if (!actions1.contains(action))
249 continue;
250 actions1.removeAll(action);
251 ordered.append(action);
252 }
253
254 clear();
255 addActions(ordered);
256
257 orderAction->setEnabled(false);
258}
259
260void ToolBar::randomize()
261{
262 QList<QAction *> randomized;
263 QList<QAction *> actions = this->actions();
264 while (!actions.isEmpty()) {
265 QAction *action = actions.takeAt(QRandomGenerator::global()->bounded(actions.size()));
266 randomized.append(action);
267 }
268 clear();
269 addActions(randomized);
270
271 orderAction->setEnabled(true);
272}
273
274void ToolBar::addSpinBox()
275{
276 if (!spinbox)
277 spinbox = new QSpinBox(this);
278 if (!spinboxAction)
279 spinboxAction = addWidget(spinbox);
280 else
281 addAction(spinboxAction);
282
283 addSpinBoxAction->setEnabled(false);
284 removeSpinBoxAction->setEnabled(true);
285}
286
287void ToolBar::removeSpinBox()
288{
289 if (spinboxAction)
290 removeAction(spinboxAction);
291
292 addSpinBoxAction->setEnabled(true);
293 removeSpinBoxAction->setEnabled(false);
294}
295
296void ToolBar::allow(Qt::ToolBarArea area, bool a)
297{
298 Qt::ToolBarAreas areas = allowedAreas();
299 areas = a ? areas | area : areas & ~area;
300 setAllowedAreas(areas);
301
302 if (areaActions->isEnabled()) {
303 leftAction->setEnabled(areas & Qt::LeftToolBarArea);
304 rightAction->setEnabled(areas & Qt::RightToolBarArea);
305 topAction->setEnabled(areas & Qt::TopToolBarArea);
306 bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
307 }
308}
309
310void ToolBar::place(Qt::ToolBarArea area, bool p)
311{
312 if (!p)
313 return;
314
315 QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
316 Q_ASSERT(mainWindow != 0);
317
318 mainWindow->addToolBar(area, this);
319
320 if (allowedAreasActions->isEnabled()) {
321 allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
322 allowRightAction->setEnabled(area != Qt::RightToolBarArea);
323 allowTopAction->setEnabled(area != Qt::TopToolBarArea);
324 allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
325 }
326}
327
328void ToolBar::changeMovable(bool movable)
329{ setMovable(movable); }
330
331void ToolBar::allowLeft(bool a)
332{ allow(Qt::LeftToolBarArea, a); }
333
334void ToolBar::allowRight(bool a)
335{ allow(Qt::RightToolBarArea, a); }
336
337void ToolBar::allowTop(bool a)
338{ allow(Qt::TopToolBarArea, a); }
339
340void ToolBar::allowBottom(bool a)
341{ allow(Qt::BottomToolBarArea, a); }
342
343void ToolBar::placeLeft(bool p)
344{ place(Qt::LeftToolBarArea, p); }
345
346void ToolBar::placeRight(bool p)
347{ place(Qt::RightToolBarArea, p); }
348
349void ToolBar::placeTop(bool p)
350{ place(Qt::TopToolBarArea, p); }
351
352void ToolBar::placeBottom(bool p)
353{ place(Qt::BottomToolBarArea, p); }
354
355void ToolBar::insertToolBarBreak()
356{
357 QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
358 Q_ASSERT(mainWindow != 0);
359
360 mainWindow->insertToolBarBreak(this);
361}
362