1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "toolbar.h"
43 
44 #include <QMainWindow>
45 #include <QMenu>
46 #include <QPainter>
47 #include <QPainterPath>
48 #include <QSpinBox>
49 #include <QLabel>
50 #include <QToolTip>
51 
52 #include <stdlib.h>
53 
genIcon(const QSize & iconSize,const QString &,const QColor & color)54 static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &color)
55 {
56     int w = iconSize.width();
57     int h = iconSize.height();
58 
59     QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
60     image.fill(0);
61 
62     QPainter p(&image);
63 
64     extern void render_qt_text(QPainter *, int, int, const QColor &);
65     render_qt_text(&p, w, h, color);
66 
67     return QPixmap::fromImage(image, Qt::DiffuseDither | Qt::DiffuseAlphaDither);
68 }
69 
genIcon(const QSize & iconSize,int number,const QColor & color)70 static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color)
71 { return genIcon(iconSize, QString::number(number), color); }
72 
ToolBar(const QString & title,QWidget * parent)73 ToolBar::ToolBar(const QString &title, QWidget *parent)
74     : QToolBar(parent), spinbox(0), spinboxAction(0)
75 {
76     tip = 0;
77     setWindowTitle(title);
78     setObjectName(title);
79 
80     setIconSize(QSize(32, 32));
81 
82     QColor bg(palette().background().color());
83     menu = new QMenu("One", this);
84     menu->setIcon(genIcon(iconSize(), 1, Qt::black));
85     menu->addAction(genIcon(iconSize(), "A", Qt::blue), "A");
86     menu->addAction(genIcon(iconSize(), "B", Qt::blue), "B");
87     menu->addAction(genIcon(iconSize(), "C", Qt::blue), "C");
88     addAction(menu->menuAction());
89 
90     QAction *two = addAction(genIcon(iconSize(), 2, Qt::white), "Two");
91     QFont boldFont;
92     boldFont.setBold(true);
93     two->setFont(boldFont);
94 
95     addAction(genIcon(iconSize(), 3, Qt::red), "Three");
96     addAction(genIcon(iconSize(), 4, Qt::green), "Four");
97     addAction(genIcon(iconSize(), 5, Qt::blue), "Five");
98     addAction(genIcon(iconSize(), 6, Qt::yellow), "Six");
99     orderAction = new QAction(this);
100     orderAction->setText(tr("Order Items in Tool Bar"));
101     connect(orderAction, SIGNAL(triggered()), SLOT(order()));
102 
103     randomizeAction = new QAction(this);
104     randomizeAction->setText(tr("Randomize Items in Tool Bar"));
105     connect(randomizeAction, SIGNAL(triggered()), SLOT(randomize()));
106 
107     addSpinBoxAction = new QAction(this);
108     addSpinBoxAction->setText(tr("Add Spin Box"));
109     connect(addSpinBoxAction, SIGNAL(triggered()), SLOT(addSpinBox()));
110 
111     removeSpinBoxAction = new QAction(this);
112     removeSpinBoxAction->setText(tr("Remove Spin Box"));
113     removeSpinBoxAction->setEnabled(false);
114     connect(removeSpinBoxAction, SIGNAL(triggered()), SLOT(removeSpinBox()));
115 
116     movableAction = new QAction(tr("Movable"), this);
117     movableAction->setCheckable(true);
118     connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool)));
119 
120     allowedAreasActions = new QActionGroup(this);
121     allowedAreasActions->setExclusive(false);
122 
123     allowLeftAction = new QAction(tr("Allow on Left"), this);
124     allowLeftAction->setCheckable(true);
125     connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool)));
126 
127     allowRightAction = new QAction(tr("Allow on Right"), this);
128     allowRightAction->setCheckable(true);
129     connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool)));
130 
131     allowTopAction = new QAction(tr("Allow on Top"), this);
132     allowTopAction->setCheckable(true);
133     connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool)));
134 
135     allowBottomAction = new QAction(tr("Allow on Bottom"), this);
136     allowBottomAction->setCheckable(true);
137     connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool)));
138 
139     allowedAreasActions->addAction(allowLeftAction);
140     allowedAreasActions->addAction(allowRightAction);
141     allowedAreasActions->addAction(allowTopAction);
142     allowedAreasActions->addAction(allowBottomAction);
143 
144     areaActions = new QActionGroup(this);
145     areaActions->setExclusive(true);
146 
147     leftAction = new QAction(tr("Place on Left") , this);
148     leftAction->setCheckable(true);
149     connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool)));
150 
151     rightAction = new QAction(tr("Place on Right") , this);
152     rightAction->setCheckable(true);
153     connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool)));
154 
155     topAction = new QAction(tr("Place on Top") , this);
156     topAction->setCheckable(true);
157     connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool)));
158 
159     bottomAction = new QAction(tr("Place on Bottom") , this);
160     bottomAction->setCheckable(true);
161     connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool)));
162 
163     areaActions->addAction(leftAction);
164     areaActions->addAction(rightAction);
165     areaActions->addAction(topAction);
166     areaActions->addAction(bottomAction);
167 
168     toolBarBreakAction = new QAction(tr("Insert break"), this);
169     connect(toolBarBreakAction, SIGNAL(triggered(bool)), this, SLOT(insertToolBarBreak()));
170 
171     connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool)));
172 
173     connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool)));
174 
175     menu = new QMenu(title, this);
176     menu->addAction(toggleViewAction());
177     menu->addSeparator();
178     menu->addAction(orderAction);
179     menu->addAction(randomizeAction);
180     menu->addSeparator();
181     menu->addAction(addSpinBoxAction);
182     menu->addAction(removeSpinBoxAction);
183     menu->addSeparator();
184     menu->addAction(movableAction);
185     menu->addSeparator();
186     menu->addActions(allowedAreasActions->actions());
187     menu->addSeparator();
188     menu->addActions(areaActions->actions());
189     menu->addSeparator();
190     menu->addAction(toolBarBreakAction);
191 
192     connect(menu, SIGNAL(aboutToShow()), this, SLOT(updateMenu()));
193 
194     randomize();
195 }
196 
updateMenu()197 void ToolBar::updateMenu()
198 {
199     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
200     Q_ASSERT(mainWindow != 0);
201 
202     const Qt::ToolBarArea area = mainWindow->toolBarArea(this);
203     const Qt::ToolBarAreas areas = allowedAreas();
204 
205     movableAction->setChecked(isMovable());
206 
207     allowLeftAction->setChecked(isAreaAllowed(Qt::LeftToolBarArea));
208     allowRightAction->setChecked(isAreaAllowed(Qt::RightToolBarArea));
209     allowTopAction->setChecked(isAreaAllowed(Qt::TopToolBarArea));
210     allowBottomAction->setChecked(isAreaAllowed(Qt::BottomToolBarArea));
211 
212     if (allowedAreasActions->isEnabled()) {
213         allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
214         allowRightAction->setEnabled(area != Qt::RightToolBarArea);
215         allowTopAction->setEnabled(area != Qt::TopToolBarArea);
216         allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
217     }
218 
219     leftAction->setChecked(area == Qt::LeftToolBarArea);
220     rightAction->setChecked(area == Qt::RightToolBarArea);
221     topAction->setChecked(area == Qt::TopToolBarArea);
222     bottomAction->setChecked(area == Qt::BottomToolBarArea);
223 
224     if (areaActions->isEnabled()) {
225         leftAction->setEnabled(areas & Qt::LeftToolBarArea);
226         rightAction->setEnabled(areas & Qt::RightToolBarArea);
227         topAction->setEnabled(areas & Qt::TopToolBarArea);
228         bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
229     }
230 }
231 
order()232 void ToolBar::order()
233 {
234     QList<QAction *> ordered, actions1 = actions(),
235                               actions2 = findChildren<QAction *>();
236     while (!actions2.isEmpty()) {
237         QAction *action = actions2.takeFirst();
238         if (!actions1.contains(action))
239             continue;
240         actions1.removeAll(action);
241         ordered.append(action);
242     }
243 
244     clear();
245     addActions(ordered);
246 
247     orderAction->setEnabled(false);
248 }
249 
randomize()250 void ToolBar::randomize()
251 {
252     QList<QAction *> randomized, actions = this->actions();
253     while (!actions.isEmpty()) {
254         QAction *action = actions.takeAt(rand() % actions.size());
255         randomized.append(action);
256     }
257     clear();
258     addActions(randomized);
259 
260     orderAction->setEnabled(true);
261 }
262 
addSpinBox()263 void ToolBar::addSpinBox()
264 {
265     if (!spinbox) {
266         spinbox = new QSpinBox(this);
267     }
268     if (!spinboxAction)
269         spinboxAction = addWidget(spinbox);
270     else
271         addAction(spinboxAction);
272 
273     addSpinBoxAction->setEnabled(false);
274     removeSpinBoxAction->setEnabled(true);
275 }
276 
removeSpinBox()277 void ToolBar::removeSpinBox()
278 {
279     if (spinboxAction)
280         removeAction(spinboxAction);
281 
282     addSpinBoxAction->setEnabled(true);
283     removeSpinBoxAction->setEnabled(false);
284 }
285 
allow(Qt::ToolBarArea area,bool a)286 void ToolBar::allow(Qt::ToolBarArea area, bool a)
287 {
288     Qt::ToolBarAreas areas = allowedAreas();
289     areas = a ? areas | area : areas & ~area;
290     setAllowedAreas(areas);
291 
292     if (areaActions->isEnabled()) {
293         leftAction->setEnabled(areas & Qt::LeftToolBarArea);
294         rightAction->setEnabled(areas & Qt::RightToolBarArea);
295         topAction->setEnabled(areas & Qt::TopToolBarArea);
296         bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
297     }
298 }
299 
place(Qt::ToolBarArea area,bool p)300 void ToolBar::place(Qt::ToolBarArea area, bool p)
301 {
302     if (!p)
303         return;
304 
305     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
306     Q_ASSERT(mainWindow != 0);
307 
308     mainWindow->addToolBar(area, this);
309 
310     if (allowedAreasActions->isEnabled()) {
311         allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
312         allowRightAction->setEnabled(area != Qt::RightToolBarArea);
313         allowTopAction->setEnabled(area != Qt::TopToolBarArea);
314         allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
315     }
316 }
317 
changeMovable(bool movable)318 void ToolBar::changeMovable(bool movable)
319 { setMovable(movable); }
320 
allowLeft(bool a)321 void ToolBar::allowLeft(bool a)
322 { allow(Qt::LeftToolBarArea, a); }
323 
allowRight(bool a)324 void ToolBar::allowRight(bool a)
325 { allow(Qt::RightToolBarArea, a); }
326 
allowTop(bool a)327 void ToolBar::allowTop(bool a)
328 { allow(Qt::TopToolBarArea, a); }
329 
allowBottom(bool a)330 void ToolBar::allowBottom(bool a)
331 { allow(Qt::BottomToolBarArea, a); }
332 
placeLeft(bool p)333 void ToolBar::placeLeft(bool p)
334 { place(Qt::LeftToolBarArea, p); }
335 
placeRight(bool p)336 void ToolBar::placeRight(bool p)
337 { place(Qt::RightToolBarArea, p); }
338 
placeTop(bool p)339 void ToolBar::placeTop(bool p)
340 { place(Qt::TopToolBarArea, p); }
341 
placeBottom(bool p)342 void ToolBar::placeBottom(bool p)
343 { place(Qt::BottomToolBarArea, p); }
344 
insertToolBarBreak()345 void ToolBar::insertToolBarBreak()
346 {
347     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
348     Q_ASSERT(mainWindow != 0);
349 
350     mainWindow->insertToolBarBreak(this);
351 }
352 
enterEvent(QEvent *)353 void ToolBar::enterEvent(QEvent*)
354 {
355 /*
356     These labels on top of toolbars look darn ugly
357 
358     if (tip == 0) {
359         tip = new QLabel(windowTitle(), this);
360         QPalette pal = tip->palette();
361         QColor c = Qt::black;
362         c.setAlpha(100);
363         pal.setColor(QPalette::Window, c);
364         pal.setColor(QPalette::Foreground, Qt::white);
365         tip->setPalette(pal);
366         tip->setAutoFillBackground(true);
367         tip->setMargin(3);
368         tip->setText(windowTitle());
369     }
370     QPoint c = rect().center();
371     QSize hint = tip->sizeHint();
372     tip->setGeometry(c.x() - hint.width()/2, c.y() - hint.height()/2,
373                         hint.width(), hint.height());
374 
375     tip->show();
376 */
377 }
378 
leaveEvent(QEvent *)379 void ToolBar::leaveEvent(QEvent*)
380 {
381     if (tip != 0)
382         tip->hide();
383 }
384