1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the demonstration applications of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include <qmainwindow.h>
34
#include <qpainterpath.h>
39
static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &color)
41
int w = iconSize.width();
42
int h = iconSize.height();
44
QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
49
extern void render_qt_text(QPainter *, int, int, const QColor &);
50
render_qt_text(&p, w, h, color);
52
return QPixmap::fromImage(image, Qt::DiffuseDither | Qt::DiffuseAlphaDither);
55
static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color)
56
{ return genIcon(iconSize, QString::number(number), color); }
58
ToolBar::ToolBar(QWidget *parent)
59
: QToolBar(parent), spinbox(0), spinboxAction(0)
61
setWindowTitle(tr("Main Tool Bar"));
63
setIconSize(QSize(32, 32));
65
QColor bg(palette().background().color());
66
menu = new QMenu("One", this);
67
menu->setIcon(genIcon(iconSize(), 1, Qt::black));
68
menu->addAction(genIcon(iconSize(), "A", Qt::blue), "A");
69
menu->addAction(genIcon(iconSize(), "B", Qt::blue), "B");
70
menu->addAction(genIcon(iconSize(), "C", Qt::blue), "C");
71
addAction(menu->menuAction());
73
QAction *two = addAction(genIcon(iconSize(), 2, Qt::white), "Two");
75
boldFont.setBold(true);
76
two->setFont(boldFont);
78
addAction(genIcon(iconSize(), 3, Qt::red), "Three");
79
addAction(genIcon(iconSize(), 4, Qt::green), "Four");
80
addAction(genIcon(iconSize(), 5, Qt::blue), "Five");
81
addAction(genIcon(iconSize(), 6, Qt::yellow), "Six");
82
orderAction = new QAction(this);
83
orderAction->setText(tr("Order Items in Tool Bar"));
84
connect(orderAction, SIGNAL(triggered()), SLOT(order()));
86
randomizeAction = new QAction(this);
87
randomizeAction->setText(tr("Randomize Items in Tool Bar"));
88
connect(randomizeAction, SIGNAL(triggered()), SLOT(randomize()));
90
addSpinBoxAction = new QAction(this);
91
addSpinBoxAction->setText(tr("Add Spin Box"));
92
connect(addSpinBoxAction, SIGNAL(triggered()), SLOT(addSpinBox()));
94
removeSpinBoxAction = new QAction(this);
95
removeSpinBoxAction->setText(tr("Remove Spin Box"));
96
removeSpinBoxAction->setEnabled(false);
97
connect(removeSpinBoxAction, SIGNAL(triggered()), SLOT(removeSpinBox()));
99
movableAction = new QAction(tr("Movable"), this);
100
movableAction->setCheckable(true);
101
connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool)));
103
allowedAreasActions = new QActionGroup(this);
104
allowedAreasActions->setExclusive(false);
106
allowLeftAction = new QAction(tr("Allow on Left"), this);
107
allowLeftAction->setCheckable(true);
108
connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool)));
110
allowRightAction = new QAction(tr("Allow on Right"), this);
111
allowRightAction->setCheckable(true);
112
connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool)));
114
allowTopAction = new QAction(tr("Allow on Top"), this);
115
allowTopAction->setCheckable(true);
116
connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool)));
118
allowBottomAction = new QAction(tr("Allow on Bottom"), this);
119
allowBottomAction->setCheckable(true);
120
connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool)));
122
allowedAreasActions->addAction(allowLeftAction);
123
allowedAreasActions->addAction(allowRightAction);
124
allowedAreasActions->addAction(allowTopAction);
125
allowedAreasActions->addAction(allowBottomAction);
127
areaActions = new QActionGroup(this);
128
areaActions->setExclusive(true);
130
leftAction = new QAction(tr("Place on Left") , this);
131
leftAction->setCheckable(true);
132
connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool)));
134
rightAction = new QAction(tr("Place on Right") , this);
135
rightAction->setCheckable(true);
136
connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool)));
138
topAction = new QAction(tr("Place on Top") , this);
139
topAction->setCheckable(true);
140
connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool)));
142
bottomAction = new QAction(tr("Place on Bottom") , this);
143
bottomAction->setCheckable(true);
144
connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool)));
146
areaActions->addAction(leftAction);
147
areaActions->addAction(rightAction);
148
areaActions->addAction(topAction);
149
areaActions->addAction(bottomAction);
151
connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool)));
153
connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool)));
155
menu = new QMenu(tr("&Toolbar"), this);
156
menu->addAction(toggleViewAction());
157
menu->addSeparator();
158
menu->addAction(orderAction);
159
menu->addAction(randomizeAction);
160
menu->addSeparator();
161
menu->addAction(addSpinBoxAction);
162
menu->addAction(removeSpinBoxAction);
163
menu->addSeparator();
164
menu->addAction(movableAction);
165
menu->addSeparator();
166
menu->addActions(allowedAreasActions->actions());
167
menu->addSeparator();
168
menu->addActions(areaActions->actions());
174
void ToolBar::polishEvent(QEvent *)
176
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
177
Q_ASSERT(mainWindow != 0);
179
const Qt::ToolBarArea area = mainWindow->toolBarArea(this);
180
const Qt::ToolBarAreas areas = allowedAreas();
182
movableAction->setChecked(isMovable());
184
allowLeftAction->setChecked(isAreaAllowed(Qt::LeftToolBarArea));
185
allowRightAction->setChecked(isAreaAllowed(Qt::RightToolBarArea));
186
allowTopAction->setChecked(isAreaAllowed(Qt::TopToolBarArea));
187
allowBottomAction->setChecked(isAreaAllowed(Qt::BottomToolBarArea));
189
if (allowedAreasActions->isEnabled()) {
190
allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
191
allowRightAction->setEnabled(area != Qt::RightToolBarArea);
192
allowTopAction->setEnabled(area != Qt::TopToolBarArea);
193
allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
196
leftAction->setChecked(area == Qt::LeftToolBarArea);
197
rightAction->setChecked(area == Qt::RightToolBarArea);
198
topAction->setChecked(area == Qt::TopToolBarArea);
199
bottomAction->setChecked(area == Qt::BottomToolBarArea);
201
if (areaActions->isEnabled()) {
202
leftAction->setEnabled(areas & Qt::LeftToolBarArea);
203
rightAction->setEnabled(areas & Qt::RightToolBarArea);
204
topAction->setEnabled(areas & Qt::TopToolBarArea);
205
bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
209
void ToolBar::order()
211
QList<QAction *> ordered, actions1 = actions(),
212
actions2 = qFindChildren<QAction *>(this);
213
while (!actions2.isEmpty()) {
214
QAction *action = actions2.takeFirst();
215
if (!actions1.contains(action))
217
actions1.removeAll(action);
218
ordered.append(action);
224
orderAction->setEnabled(false);
227
void ToolBar::randomize()
229
QList<QAction *> randomized, actions = this->actions();
230
while (!actions.isEmpty()) {
231
QAction *action = actions.takeAt(rand() % actions.size());
232
randomized.append(action);
235
addActions(randomized);
237
orderAction->setEnabled(true);
240
void ToolBar::addSpinBox()
243
spinbox = new QSpinBox(this);
246
spinboxAction = addWidget(spinbox);
248
addAction(spinboxAction);
250
addSpinBoxAction->setEnabled(false);
251
removeSpinBoxAction->setEnabled(true);
254
void ToolBar::removeSpinBox()
257
removeAction(spinboxAction);
259
addSpinBoxAction->setEnabled(true);
260
removeSpinBoxAction->setEnabled(false);
263
void ToolBar::allow(Qt::ToolBarArea area, bool a)
265
Qt::ToolBarAreas areas = allowedAreas();
266
areas = a ? areas | area : areas & ~area;
267
setAllowedAreas(areas);
269
if (areaActions->isEnabled()) {
270
leftAction->setEnabled(areas & Qt::LeftToolBarArea);
271
rightAction->setEnabled(areas & Qt::RightToolBarArea);
272
topAction->setEnabled(areas & Qt::TopToolBarArea);
273
bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
277
void ToolBar::place(Qt::ToolBarArea area, bool p)
282
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
283
Q_ASSERT(mainWindow != 0);
285
mainWindow->addToolBar(area, this);
287
if (allowedAreasActions->isEnabled()) {
288
allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
289
allowRightAction->setEnabled(area != Qt::RightToolBarArea);
290
allowTopAction->setEnabled(area != Qt::TopToolBarArea);
291
allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
295
void ToolBar::changeMovable(bool movable)
296
{ setMovable(movable); }
298
void ToolBar::allowLeft(bool a)
299
{ allow(Qt::LeftToolBarArea, a); }
301
void ToolBar::allowRight(bool a)
302
{ allow(Qt::RightToolBarArea, a); }
304
void ToolBar::allowTop(bool a)
305
{ allow(Qt::TopToolBarArea, a); }
307
void ToolBar::allowBottom(bool a)
308
{ allow(Qt::BottomToolBarArea, a); }
310
void ToolBar::placeLeft(bool p)
311
{ place(Qt::LeftToolBarArea, p); }
313
void ToolBar::placeRight(bool p)
314
{ place(Qt::RightToolBarArea, p); }
316
void ToolBar::placeTop(bool p)
317
{ place(Qt::TopToolBarArea, p); }
319
void ToolBar::placeBottom(bool p)
320
{ place(Qt::BottomToolBarArea, p); }