| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the demonstration applications of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 70 | static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color)
|
|---|
| 71 | { return genIcon(iconSize, QString::number(number), color); }
|
|---|
| 72 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 232 | void ToolBar::order()
|
|---|
| 233 | {
|
|---|
| 234 | QList<QAction *> ordered, actions1 = actions(),
|
|---|
| 235 | actions2 = qFindChildren<QAction *>(this);
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 277 | void ToolBar::removeSpinBox()
|
|---|
| 278 | {
|
|---|
| 279 | if (spinboxAction)
|
|---|
| 280 | removeAction(spinboxAction);
|
|---|
| 281 |
|
|---|
| 282 | addSpinBoxAction->setEnabled(true);
|
|---|
| 283 | removeSpinBoxAction->setEnabled(false);
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 318 | void ToolBar::changeMovable(bool movable)
|
|---|
| 319 | { setMovable(movable); }
|
|---|
| 320 |
|
|---|
| 321 | void ToolBar::allowLeft(bool a)
|
|---|
| 322 | { allow(Qt::LeftToolBarArea, a); }
|
|---|
| 323 |
|
|---|
| 324 | void ToolBar::allowRight(bool a)
|
|---|
| 325 | { allow(Qt::RightToolBarArea, a); }
|
|---|
| 326 |
|
|---|
| 327 | void ToolBar::allowTop(bool a)
|
|---|
| 328 | { allow(Qt::TopToolBarArea, a); }
|
|---|
| 329 |
|
|---|
| 330 | void ToolBar::allowBottom(bool a)
|
|---|
| 331 | { allow(Qt::BottomToolBarArea, a); }
|
|---|
| 332 |
|
|---|
| 333 | void ToolBar::placeLeft(bool p)
|
|---|
| 334 | { place(Qt::LeftToolBarArea, p); }
|
|---|
| 335 |
|
|---|
| 336 | void ToolBar::placeRight(bool p)
|
|---|
| 337 | { place(Qt::RightToolBarArea, p); }
|
|---|
| 338 |
|
|---|
| 339 | void ToolBar::placeTop(bool p)
|
|---|
| 340 | { place(Qt::TopToolBarArea, p); }
|
|---|
| 341 |
|
|---|
| 342 | void ToolBar::placeBottom(bool p)
|
|---|
| 343 | { place(Qt::BottomToolBarArea, p); }
|
|---|
| 344 |
|
|---|
| 345 | void ToolBar::insertToolBarBreak()
|
|---|
| 346 | {
|
|---|
| 347 | QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
|
|---|
| 348 | Q_ASSERT(mainWindow != 0);
|
|---|
| 349 |
|
|---|
| 350 | mainWindow->insertToolBarBreak(this);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 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 |
|
|---|
| 379 | void ToolBar::leaveEvent(QEvent*)
|
|---|
| 380 | {
|
|---|
| 381 | if (tip != 0)
|
|---|
| 382 | tip->hide();
|
|---|
| 383 | }
|
|---|