demos/mainwindow/toolbar.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2004-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the demonstration applications of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "toolbar.h"
00025 
00026 #include <QMainWindow>
00027 #include <QMenu>
00028 #include <QPainter>
00029 #include <QPainterPath>
00030 #include <QSpinBox>
00031 
00032 #include <stdlib.h>
00033 
00034 static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &color)
00035 {
00036     int w = iconSize.width();
00037     int h = iconSize.height();
00038 
00039     QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
00040     image.fill(0);
00041 
00042     QPainter p(&image);
00043 
00044     extern void render_qt_text(QPainter *, int, int, const QColor &);
00045     render_qt_text(&p, w, h, color);
00046 
00047     return QPixmap::fromImage(image, Qt::DiffuseDither | Qt::DiffuseAlphaDither);
00048 }
00049 
00050 static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color)
00051 { return genIcon(iconSize, QString::number(number), color); }
00052 
00053 ToolBar::ToolBar(QWidget *parent)
00054     : QToolBar(parent), spinbox(0), spinboxAction(0)
00055 {
00056     setWindowTitle(tr("Main Tool Bar"));
00057     setObjectName("MainToolBar");
00058 
00059     setIconSize(QSize(32, 32));
00060 
00061     QColor bg(palette().background().color());
00062     menu = new QMenu("One", this);
00063     menu->setIcon(genIcon(iconSize(), 1, Qt::black));
00064     menu->addAction(genIcon(iconSize(), "A", Qt::blue), "A");
00065     menu->addAction(genIcon(iconSize(), "B", Qt::blue), "B");
00066     menu->addAction(genIcon(iconSize(), "C", Qt::blue), "C");
00067     addAction(menu->menuAction());
00068 
00069     QAction *two = addAction(genIcon(iconSize(), 2, Qt::white), "Two");
00070     QFont boldFont;
00071     boldFont.setBold(true);
00072     two->setFont(boldFont);
00073 
00074     addAction(genIcon(iconSize(), 3, Qt::red), "Three");
00075     addAction(genIcon(iconSize(), 4, Qt::green), "Four");
00076     addAction(genIcon(iconSize(), 5, Qt::blue), "Five");
00077     addAction(genIcon(iconSize(), 6, Qt::yellow), "Six");
00078     orderAction = new QAction(this);
00079     orderAction->setText(tr("Order Items in Tool Bar"));
00080     connect(orderAction, SIGNAL(triggered()), SLOT(order()));
00081 
00082     randomizeAction = new QAction(this);
00083     randomizeAction->setText(tr("Randomize Items in Tool Bar"));
00084     connect(randomizeAction, SIGNAL(triggered()), SLOT(randomize()));
00085 
00086     addSpinBoxAction = new QAction(this);
00087     addSpinBoxAction->setText(tr("Add Spin Box"));
00088     connect(addSpinBoxAction, SIGNAL(triggered()), SLOT(addSpinBox()));
00089 
00090     removeSpinBoxAction = new QAction(this);
00091     removeSpinBoxAction->setText(tr("Remove Spin Box"));
00092     removeSpinBoxAction->setEnabled(false);
00093     connect(removeSpinBoxAction, SIGNAL(triggered()), SLOT(removeSpinBox()));
00094 
00095     movableAction = new QAction(tr("Movable"), this);
00096     movableAction->setCheckable(true);
00097     connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool)));
00098 
00099     allowedAreasActions = new QActionGroup(this);
00100     allowedAreasActions->setExclusive(false);
00101 
00102     allowLeftAction = new QAction(tr("Allow on Left"), this);
00103     allowLeftAction->setCheckable(true);
00104     connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool)));
00105 
00106     allowRightAction = new QAction(tr("Allow on Right"), this);
00107     allowRightAction->setCheckable(true);
00108     connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool)));
00109 
00110     allowTopAction = new QAction(tr("Allow on Top"), this);
00111     allowTopAction->setCheckable(true);
00112     connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool)));
00113 
00114     allowBottomAction = new QAction(tr("Allow on Bottom"), this);
00115     allowBottomAction->setCheckable(true);
00116     connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool)));
00117 
00118     allowedAreasActions->addAction(allowLeftAction);
00119     allowedAreasActions->addAction(allowRightAction);
00120     allowedAreasActions->addAction(allowTopAction);
00121     allowedAreasActions->addAction(allowBottomAction);
00122 
00123     areaActions = new QActionGroup(this);
00124     areaActions->setExclusive(true);
00125 
00126     leftAction = new QAction(tr("Place on Left") , this);
00127     leftAction->setCheckable(true);
00128     connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool)));
00129 
00130     rightAction = new QAction(tr("Place on Right") , this);
00131     rightAction->setCheckable(true);
00132     connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool)));
00133 
00134     topAction = new QAction(tr("Place on Top") , this);
00135     topAction->setCheckable(true);
00136     connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool)));
00137 
00138     bottomAction = new QAction(tr("Place on Bottom") , this);
00139     bottomAction->setCheckable(true);
00140     connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool)));
00141 
00142     areaActions->addAction(leftAction);
00143     areaActions->addAction(rightAction);
00144     areaActions->addAction(topAction);
00145     areaActions->addAction(bottomAction);
00146 
00147     connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool)));
00148 
00149     connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool)));
00150 
00151     menu = new QMenu(tr("&Toolbar"), this);
00152     menu->addAction(toggleViewAction());
00153     menu->addSeparator();
00154     menu->addAction(orderAction);
00155     menu->addAction(randomizeAction);
00156     menu->addSeparator();
00157     menu->addAction(addSpinBoxAction);
00158     menu->addAction(removeSpinBoxAction);
00159     menu->addSeparator();
00160     menu->addAction(movableAction);
00161     menu->addSeparator();
00162     menu->addActions(allowedAreasActions->actions());
00163     menu->addSeparator();
00164     menu->addActions(areaActions->actions());
00165 
00166     connect(menu, SIGNAL(aboutToShow()), this, SLOT(updateMenu()));
00167 
00168     randomize();
00169 }
00170 
00171 void ToolBar::updateMenu()
00172 {
00173     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
00174     Q_ASSERT(mainWindow != 0);
00175 
00176     const Qt::ToolBarArea area = mainWindow->toolBarArea(this);
00177     const Qt::ToolBarAreas areas = allowedAreas();
00178 
00179     movableAction->setChecked(isMovable());
00180 
00181     allowLeftAction->setChecked(isAreaAllowed(Qt::LeftToolBarArea));
00182     allowRightAction->setChecked(isAreaAllowed(Qt::RightToolBarArea));
00183     allowTopAction->setChecked(isAreaAllowed(Qt::TopToolBarArea));
00184     allowBottomAction->setChecked(isAreaAllowed(Qt::BottomToolBarArea));
00185 
00186     if (allowedAreasActions->isEnabled()) {
00187         allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
00188         allowRightAction->setEnabled(area != Qt::RightToolBarArea);
00189         allowTopAction->setEnabled(area != Qt::TopToolBarArea);
00190         allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
00191     }
00192 
00193     leftAction->setChecked(area == Qt::LeftToolBarArea);
00194     rightAction->setChecked(area == Qt::RightToolBarArea);
00195     topAction->setChecked(area == Qt::TopToolBarArea);
00196     bottomAction->setChecked(area == Qt::BottomToolBarArea);
00197 
00198     if (areaActions->isEnabled()) {
00199         leftAction->setEnabled(areas & Qt::LeftToolBarArea);
00200         rightAction->setEnabled(areas & Qt::RightToolBarArea);
00201         topAction->setEnabled(areas & Qt::TopToolBarArea);
00202         bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
00203     }
00204 }
00205 
00206 void ToolBar::order()
00207 {
00208     QList<QAction *> ordered, actions1 = actions(),
00209                               actions2 = qFindChildren<QAction *>(this);
00210     while (!actions2.isEmpty()) {
00211         QAction *action = actions2.takeFirst();
00212         if (!actions1.contains(action))
00213             continue;
00214         actions1.removeAll(action);
00215         ordered.append(action);
00216     }
00217 
00218     clear();
00219     addActions(ordered);
00220 
00221     orderAction->setEnabled(false);
00222 }
00223 
00224 void ToolBar::randomize()
00225 {
00226     QList<QAction *> randomized, actions = this->actions();
00227     while (!actions.isEmpty()) {
00228         QAction *action = actions.takeAt(rand() % actions.size());
00229         randomized.append(action);
00230     }
00231     clear();
00232     addActions(randomized);
00233 
00234     orderAction->setEnabled(true);
00235 }
00236 
00237 void ToolBar::addSpinBox()
00238 {
00239     if (!spinbox) {
00240         spinbox = new QSpinBox(this);
00241     }
00242     if (!spinboxAction)
00243         spinboxAction = addWidget(spinbox);
00244     else
00245         addAction(spinboxAction);
00246 
00247     addSpinBoxAction->setEnabled(false);
00248     removeSpinBoxAction->setEnabled(true);
00249 }
00250 
00251 void ToolBar::removeSpinBox()
00252 {
00253     if (spinboxAction)
00254         removeAction(spinboxAction);
00255 
00256     addSpinBoxAction->setEnabled(true);
00257     removeSpinBoxAction->setEnabled(false);
00258 }
00259 
00260 void ToolBar::allow(Qt::ToolBarArea area, bool a)
00261 {
00262     Qt::ToolBarAreas areas = allowedAreas();
00263     areas = a ? areas | area : areas & ~area;
00264     setAllowedAreas(areas);
00265 
00266     if (areaActions->isEnabled()) {
00267         leftAction->setEnabled(areas & Qt::LeftToolBarArea);
00268         rightAction->setEnabled(areas & Qt::RightToolBarArea);
00269         topAction->setEnabled(areas & Qt::TopToolBarArea);
00270         bottomAction->setEnabled(areas & Qt::BottomToolBarArea);
00271     }
00272 }
00273 
00274 void ToolBar::place(Qt::ToolBarArea area, bool p)
00275 {
00276     if (!p)
00277         return;
00278 
00279     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
00280     Q_ASSERT(mainWindow != 0);
00281 
00282     mainWindow->addToolBar(area, this);
00283 
00284     if (allowedAreasActions->isEnabled()) {
00285         allowLeftAction->setEnabled(area != Qt::LeftToolBarArea);
00286         allowRightAction->setEnabled(area != Qt::RightToolBarArea);
00287         allowTopAction->setEnabled(area != Qt::TopToolBarArea);
00288         allowBottomAction->setEnabled(area != Qt::BottomToolBarArea);
00289     }
00290 }
00291 
00292 void ToolBar::changeMovable(bool movable)
00293 { setMovable(movable); }
00294 
00295 void ToolBar::allowLeft(bool a)
00296 { allow(Qt::LeftToolBarArea, a); }
00297 
00298 void ToolBar::allowRight(bool a)
00299 { allow(Qt::RightToolBarArea, a); }
00300 
00301 void ToolBar::allowTop(bool a)
00302 { allow(Qt::TopToolBarArea, a); }
00303 
00304 void ToolBar::allowBottom(bool a)
00305 { allow(Qt::BottomToolBarArea, a); }
00306 
00307 void ToolBar::placeLeft(bool p)
00308 { place(Qt::LeftToolBarArea, p); }
00309 
00310 void ToolBar::placeRight(bool p)
00311 { place(Qt::RightToolBarArea, p); }
00312 
00313 void ToolBar::placeTop(bool p)
00314 { place(Qt::TopToolBarArea, p); }
00315 
00316 void ToolBar::placeBottom(bool p)
00317 { place(Qt::BottomToolBarArea, p); }

Generated on Thu Mar 15 11:52:34 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1