| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 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 [email protected].
|
|---|
| 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 |
|
|---|
|
|---|