~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to demos/mainwindow/toolbar.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the demonstration applications of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "toolbar.h"
 
30
 
 
31
#include <qmainwindow.h>
 
32
#include <qmenu.h>
 
33
#include <qpainter.h>
 
34
#include <qpainterpath.h>
 
35
#include <qspinbox.h>
 
36
 
 
37
#include <stdlib.h>
 
38
 
 
39
static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &color)
 
40
{
 
41
    int w = iconSize.width();
 
42
    int h = iconSize.height();
 
43
 
 
44
    QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
 
45
    image.fill(0);
 
46
 
 
47
    QPainter p(&image);
 
48
 
 
49
    extern void render_qt_text(QPainter *, int, int, const QColor &);
 
50
    render_qt_text(&p, w, h, color);
 
51
 
 
52
    return QPixmap::fromImage(image, Qt::DiffuseDither | Qt::DiffuseAlphaDither);
 
53
}
 
54
 
 
55
static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color)
 
56
{ return genIcon(iconSize, QString::number(number), color); }
 
57
 
 
58
ToolBar::ToolBar(QWidget *parent)
 
59
    : QToolBar(parent), spinbox(0), spinboxAction(0)
 
60
{
 
61
    setWindowTitle(tr("Main Tool Bar"));
 
62
 
 
63
    setIconSize(QSize(32, 32));
 
64
 
 
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());
 
72
 
 
73
    QAction *two = addAction(genIcon(iconSize(), 2, Qt::white), "Two");
 
74
    QFont boldFont;
 
75
    boldFont.setBold(true);
 
76
    two->setFont(boldFont);
 
77
 
 
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()));
 
85
 
 
86
    randomizeAction = new QAction(this);
 
87
    randomizeAction->setText(tr("Randomize Items in Tool Bar"));
 
88
    connect(randomizeAction, SIGNAL(triggered()), SLOT(randomize()));
 
89
 
 
90
    addSpinBoxAction = new QAction(this);
 
91
    addSpinBoxAction->setText(tr("Add Spin Box"));
 
92
    connect(addSpinBoxAction, SIGNAL(triggered()), SLOT(addSpinBox()));
 
93
 
 
94
    removeSpinBoxAction = new QAction(this);
 
95
    removeSpinBoxAction->setText(tr("Remove Spin Box"));
 
96
    removeSpinBoxAction->setEnabled(false);
 
97
    connect(removeSpinBoxAction, SIGNAL(triggered()), SLOT(removeSpinBox()));
 
98
 
 
99
    movableAction = new QAction(tr("Movable"), this);
 
100
    movableAction->setCheckable(true);
 
101
    connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool)));
 
102
 
 
103
    allowedAreasActions = new QActionGroup(this);
 
104
    allowedAreasActions->setExclusive(false);
 
105
 
 
106
    allowLeftAction = new QAction(tr("Allow on Left"), this);
 
107
    allowLeftAction->setCheckable(true);
 
108
    connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool)));
 
109
 
 
110
    allowRightAction = new QAction(tr("Allow on Right"), this);
 
111
    allowRightAction->setCheckable(true);
 
112
    connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool)));
 
113
 
 
114
    allowTopAction = new QAction(tr("Allow on Top"), this);
 
115
    allowTopAction->setCheckable(true);
 
116
    connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool)));
 
117
 
 
118
    allowBottomAction = new QAction(tr("Allow on Bottom"), this);
 
119
    allowBottomAction->setCheckable(true);
 
120
    connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool)));
 
121
 
 
122
    allowedAreasActions->addAction(allowLeftAction);
 
123
    allowedAreasActions->addAction(allowRightAction);
 
124
    allowedAreasActions->addAction(allowTopAction);
 
125
    allowedAreasActions->addAction(allowBottomAction);
 
126
 
 
127
    areaActions = new QActionGroup(this);
 
128
    areaActions->setExclusive(true);
 
129
 
 
130
    leftAction = new QAction(tr("Place on Left") , this);
 
131
    leftAction->setCheckable(true);
 
132
    connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool)));
 
133
 
 
134
    rightAction = new QAction(tr("Place on Right") , this);
 
135
    rightAction->setCheckable(true);
 
136
    connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool)));
 
137
 
 
138
    topAction = new QAction(tr("Place on Top") , this);
 
139
    topAction->setCheckable(true);
 
140
    connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool)));
 
141
 
 
142
    bottomAction = new QAction(tr("Place on Bottom") , this);
 
143
    bottomAction->setCheckable(true);
 
144
    connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool)));
 
145
 
 
146
    areaActions->addAction(leftAction);
 
147
    areaActions->addAction(rightAction);
 
148
    areaActions->addAction(topAction);
 
149
    areaActions->addAction(bottomAction);
 
150
 
 
151
    connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool)));
 
152
 
 
153
    connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool)));
 
154
 
 
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());
 
169
 
 
170
    randomize();
 
171
}
 
172
 
 
173
 
 
174
void ToolBar::polishEvent(QEvent *)
 
175
{
 
176
    QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
 
177
    Q_ASSERT(mainWindow != 0);
 
178
 
 
179
    const Qt::ToolBarArea area = mainWindow->toolBarArea(this);
 
180
    const Qt::ToolBarAreas areas = allowedAreas();
 
181
 
 
182
    movableAction->setChecked(isMovable());
 
183
 
 
184
    allowLeftAction->setChecked(isAreaAllowed(Qt::LeftToolBarArea));
 
185
    allowRightAction->setChecked(isAreaAllowed(Qt::RightToolBarArea));
 
186
    allowTopAction->setChecked(isAreaAllowed(Qt::TopToolBarArea));
 
187
    allowBottomAction->setChecked(isAreaAllowed(Qt::BottomToolBarArea));
 
188
 
 
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);
 
194
    }
 
195
 
 
196
    leftAction->setChecked(area == Qt::LeftToolBarArea);
 
197
    rightAction->setChecked(area == Qt::RightToolBarArea);
 
198
    topAction->setChecked(area == Qt::TopToolBarArea);
 
199
    bottomAction->setChecked(area == Qt::BottomToolBarArea);
 
200
 
 
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);
 
206
    }
 
207
}
 
208
 
 
209
void ToolBar::order()
 
210
{
 
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))
 
216
            continue;
 
217
        actions1.removeAll(action);
 
218
        ordered.append(action);
 
219
    }
 
220
 
 
221
    clear();
 
222
    addActions(ordered);
 
223
 
 
224
    orderAction->setEnabled(false);
 
225
}
 
226
 
 
227
void ToolBar::randomize()
 
228
{
 
229
    QList<QAction *> randomized, actions = this->actions();
 
230
    while (!actions.isEmpty()) {
 
231
        QAction *action = actions.takeAt(rand() % actions.size());
 
232
        randomized.append(action);
 
233
    }
 
234
    clear();
 
235
    addActions(randomized);
 
236
 
 
237
    orderAction->setEnabled(true);
 
238
}
 
239
 
 
240
void ToolBar::addSpinBox()
 
241
{
 
242
    if (!spinbox) {
 
243
        spinbox = new QSpinBox(this);
 
244
    }
 
245
    if (!spinboxAction)
 
246
        spinboxAction = addWidget(spinbox);
 
247
    else
 
248
        addAction(spinboxAction);
 
249
 
 
250
    addSpinBoxAction->setEnabled(false);
 
251
    removeSpinBoxAction->setEnabled(true);
 
252
}
 
253
 
 
254
void ToolBar::removeSpinBox()
 
255
{
 
256
    if (spinboxAction)
 
257
        removeAction(spinboxAction);
 
258
 
 
259
    addSpinBoxAction->setEnabled(true);
 
260
    removeSpinBoxAction->setEnabled(false);
 
261
}
 
262
 
 
263
void ToolBar::allow(Qt::ToolBarArea area, bool a)
 
264
{
 
265
    Qt::ToolBarAreas areas = allowedAreas();
 
266
    areas = a ? areas | area : areas & ~area;
 
267
    setAllowedAreas(areas);
 
268
 
 
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);
 
274
    }
 
275
}
 
276
 
 
277
void ToolBar::place(Qt::ToolBarArea area, bool p)
 
278
{
 
279
    if (!p)
 
280
        return;
 
281
 
 
282
    QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
 
283
    Q_ASSERT(mainWindow != 0);
 
284
 
 
285
    mainWindow->addToolBar(area, this);
 
286
 
 
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);
 
292
    }
 
293
}
 
294
 
 
295
void ToolBar::changeMovable(bool movable)
 
296
{ setMovable(movable); }
 
297
 
 
298
void ToolBar::allowLeft(bool a)
 
299
{ allow(Qt::LeftToolBarArea, a); }
 
300
 
 
301
void ToolBar::allowRight(bool a)
 
302
{ allow(Qt::RightToolBarArea, a); }
 
303
 
 
304
void ToolBar::allowTop(bool a)
 
305
{ allow(Qt::TopToolBarArea, a); }
 
306
 
 
307
void ToolBar::allowBottom(bool a)
 
308
{ allow(Qt::BottomToolBarArea, a); }
 
309
 
 
310
void ToolBar::placeLeft(bool p)
 
311
{ place(Qt::LeftToolBarArea, p); }
 
312
 
 
313
void ToolBar::placeRight(bool p)
 
314
{ place(Qt::RightToolBarArea, p); }
 
315
 
 
316
void ToolBar::placeTop(bool p)
 
317
{ place(Qt::TopToolBarArea, p); }
 
318
 
 
319
void ToolBar::placeBottom(bool p)
 
320
{ place(Qt::BottomToolBarArea, p); }