demos/mainwindow/colorswatch.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 "colorswatch.h"
00025 
00026 #include <QAction>
00027 #include <QtEvents>
00028 #include <QFrame>
00029 #include <QMainWindow>
00030 #include <QMenu>
00031 #include <QPainter>
00032 #include <QImage>
00033 #include <QColor>
00034 #include <QDialog>
00035 #include <QGridLayout>
00036 #include <QSpinBox>
00037 #include <QLabel>
00038 #include <QPainterPath>
00039 #include <QPushButton>
00040 #include <QHBoxLayout>
00041 #include <QtDebug>
00042 
00043 #undef DEBUG_SIZEHINTS
00044 
00045 QColor bgColorForName(const QString &name)
00046 {
00047     if (name == "Black")
00048         return QColor("#D8D8D8");
00049     else if (name == "White")
00050         return QColor("#F1F1F1");
00051     else if (name == "Red")
00052         return QColor("#F1D8D8");
00053     else if (name == "Green")
00054         return QColor("#D8E4D8");
00055     else if (name == "Blue")
00056         return QColor("#D8D8F1");
00057     else if (name == "Yellow")
00058         return QColor("#F1F0D8");
00059     return QColor(name).light(110);
00060 }
00061 
00062 QColor fgColorForName(const QString &name)
00063 {
00064     if (name == "Black")
00065         return QColor("#6C6C6C");
00066     else if (name == "White")
00067         return QColor("#F8F8F8");
00068     else if (name == "Red")
00069         return QColor("#F86C6C");
00070     else if (name == "Green")
00071         return QColor("#6CB26C");
00072     else if (name == "Blue")
00073         return QColor("#6C6CF8");
00074     else if (name == "Yellow")
00075         return QColor("#F8F76C");
00076     return QColor(name);
00077 }
00078 
00079 class ColorDock : public QFrame
00080 {
00081     Q_OBJECT
00082 public:
00083     ColorDock(const QString &c, QWidget *parent);
00084 
00085     virtual QSize sizeHint() const;
00086     virtual QSize minimumSizeHint() const;
00087 
00088 public slots:
00089     void changeSizeHints();
00090 
00091 protected:
00092     void paintEvent(QPaintEvent *);
00093     QString color;
00094     QSize szHint, minSzHint;
00095 };
00096 
00097 ColorDock::ColorDock(const QString &c, QWidget *parent)
00098     : QFrame(parent) , color(c)
00099 {
00100     QFont font = this->font();
00101     font.setPointSize(8);
00102     setFont(font);
00103     szHint = QSize(-1, -1);
00104     minSzHint = QSize(125, 75);
00105 }
00106 
00107 QSize ColorDock::sizeHint() const
00108 {
00109     return szHint;
00110 }
00111 
00112 QSize ColorDock::minimumSizeHint() const
00113 {
00114     return minSzHint;
00115 }
00116 
00117 void ColorDock::paintEvent(QPaintEvent *)
00118 {
00119     QPainter p(this);
00120     p.setRenderHint(QPainter::Antialiasing);
00121     p.fillRect(rect(), bgColorForName(color));
00122 
00123     p.save();
00124 
00125     extern void render_qt_text(QPainter *, int, int, const QColor &);
00126     render_qt_text(&p, width(), height(), fgColorForName(color));
00127 
00128     p.restore();
00129 
00130 #ifdef DEBUG_SIZEHINTS
00131     p.setRenderHint(QPainter::Antialiasing, false);
00132 
00133     QSize sz = size();
00134     QSize szHint = sizeHint();
00135     QSize minSzHint = minimumSizeHint();
00136     QSize maxSz = maximumSize();
00137     QString text = QString::fromLatin1("sz: %1x%2\nszHint: %3x%4\nminSzHint: %5x%6\n"
00138                                         "maxSz: %8x%9")
00139                     .arg(sz.width()).arg(sz.height())
00140                     .arg(szHint.width()).arg(szHint.height())
00141                     .arg(minSzHint.width()).arg(minSzHint.height())
00142                     .arg(maxSz.width()).arg(maxSz.height());
00143 
00144     QRect r = fontMetrics().boundingRect(rect(), Qt::AlignLeft|Qt::AlignTop, text);
00145     r.adjust(-2, -2, 1, 1);
00146     p.translate(4, 4);
00147     QColor bg = Qt::yellow;
00148     bg.setAlpha(120);
00149     p.setBrush(bg);
00150     p.setPen(Qt::black);
00151     p.drawRect(r);
00152     p.drawText(rect(), Qt::AlignLeft|Qt::AlignTop, text);
00153 #endif // DEBUG_SIZEHINTS
00154 }
00155 
00156 static QSpinBox *createSpinBox(int value, QWidget *parent, int max = 1000)
00157 {
00158     QSpinBox *result = new QSpinBox(parent);
00159     result->setMinimum(-1);
00160     result->setMaximum(max);
00161     result->setValue(value);
00162     return result;
00163 }
00164 
00165 void ColorDock::changeSizeHints()
00166 {
00167     QDialog dialog(this);
00168     dialog.setWindowTitle(color);
00169 
00170     QVBoxLayout *topLayout = new QVBoxLayout(&dialog);
00171 
00172     QGridLayout *inputLayout = new QGridLayout();
00173     topLayout->addLayout(inputLayout);
00174 
00175     inputLayout->addWidget(new QLabel(tr("Size Hint:"), &dialog), 0, 0);
00176     inputLayout->addWidget(new QLabel(tr("Min Size Hint:"), &dialog), 1, 0);
00177     inputLayout->addWidget(new QLabel(tr("Max Size:"), &dialog), 2, 0);
00178     inputLayout->addWidget(new QLabel(tr("Dockwgt Max Size:"), &dialog), 3, 0);
00179 
00180     QSpinBox *szHintW = createSpinBox(szHint.width(), &dialog);
00181     inputLayout->addWidget(szHintW, 0, 1);
00182     QSpinBox *szHintH = createSpinBox(szHint.height(), &dialog);
00183     inputLayout->addWidget(szHintH, 0, 2);
00184 
00185     QSpinBox *minSzHintW = createSpinBox(minSzHint.width(), &dialog);
00186     inputLayout->addWidget(minSzHintW, 1, 1);
00187     QSpinBox *minSzHintH = createSpinBox(minSzHint.height(), &dialog);
00188     inputLayout->addWidget(minSzHintH, 1, 2);
00189 
00190     QSize maxSz = maximumSize();
00191     QSpinBox *maxSzW = createSpinBox(maxSz.width(), &dialog, QWIDGETSIZE_MAX);
00192     inputLayout->addWidget(maxSzW, 2, 1);
00193     QSpinBox *maxSzH = createSpinBox(maxSz.height(), &dialog, QWIDGETSIZE_MAX);
00194     inputLayout->addWidget(maxSzH, 2, 2);
00195 
00196     QSize dwMaxSz = parentWidget()->maximumSize();
00197     QSpinBox *dwMaxSzW = createSpinBox(dwMaxSz.width(), &dialog, QWIDGETSIZE_MAX);
00198     inputLayout->addWidget(dwMaxSzW, 3, 1);
00199     QSpinBox *dwMaxSzH = createSpinBox(dwMaxSz.height(), &dialog, QWIDGETSIZE_MAX);
00200     inputLayout->addWidget(dwMaxSzH, 3, 2);
00201 
00202     inputLayout->setColumnStretch(1, 1);
00203     inputLayout->setColumnStretch(2, 1);
00204 
00205     topLayout->addStretch();
00206 
00207     QHBoxLayout *buttonBox = new QHBoxLayout();
00208     topLayout->addLayout(buttonBox);
00209 
00210     QPushButton *okButton = new QPushButton(tr("Ok"), &dialog);
00211     QPushButton *cancelButton = new QPushButton(tr("Cancel"), &dialog);
00212     connect(okButton, SIGNAL(clicked()), &dialog, SLOT(accept()));
00213     connect(cancelButton, SIGNAL(clicked()), &dialog, SLOT(reject()));
00214     buttonBox->addStretch();
00215     buttonBox->addWidget(cancelButton);
00216     buttonBox->addWidget(okButton);
00217 
00218 
00219     if (!dialog.exec())
00220         return;
00221 
00222     szHint = QSize(szHintW->value(), szHintH->value());
00223     minSzHint = QSize(minSzHintW->value(), minSzHintH->value());
00224     maxSz = QSize(maxSzW->value(), maxSzH->value());
00225     setMaximumSize(maxSz);
00226     dwMaxSz = QSize(dwMaxSzW->value(), dwMaxSzH->value());
00227     parentWidget()->setMaximumSize(dwMaxSz);
00228     updateGeometry();
00229     update();
00230 }
00231 
00232 ColorSwatch::ColorSwatch(const QString &colorName, QWidget *parent, Qt::WindowFlags flags)
00233     : QDockWidget(parent, flags)
00234 {
00235     setObjectName(colorName + QLatin1String(" Dock Widget"));
00236     setWindowTitle(objectName());
00237 
00238     QFrame *swatch = new ColorDock(colorName, this);
00239     swatch->setFrameStyle(QFrame::Box | QFrame::Sunken);
00240 
00241     setWidget(swatch);
00242 
00243     changeSizeHintsAction = new QAction(tr("Change Size Hints"), this);
00244     connect(changeSizeHintsAction, SIGNAL(triggered()), swatch, SLOT(changeSizeHints()));
00245 
00246     closableAction = new QAction(tr("Closable"), this);
00247     closableAction->setCheckable(true);
00248     connect(closableAction, SIGNAL(triggered(bool)), SLOT(changeClosable(bool)));
00249 
00250     movableAction = new QAction(tr("Movable"), this);
00251     movableAction->setCheckable(true);
00252     connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool)));
00253 
00254     floatableAction = new QAction(tr("Floatable"), this);
00255     floatableAction->setCheckable(true);
00256     connect(floatableAction, SIGNAL(triggered(bool)), SLOT(changeFloatable(bool)));
00257 
00258     floatingAction = new QAction(tr("Floating"), this);
00259     floatingAction->setCheckable(true);
00260     connect(floatingAction, SIGNAL(triggered(bool)), SLOT(changeFloating(bool)));
00261 
00262     allowedAreasActions = new QActionGroup(this);
00263     allowedAreasActions->setExclusive(false);
00264 
00265     allowLeftAction = new QAction(tr("Allow on Left"), this);
00266     allowLeftAction->setCheckable(true);
00267     connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool)));
00268 
00269     allowRightAction = new QAction(tr("Allow on Right"), this);
00270     allowRightAction->setCheckable(true);
00271     connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool)));
00272 
00273     allowTopAction = new QAction(tr("Allow on Top"), this);
00274     allowTopAction->setCheckable(true);
00275     connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool)));
00276 
00277     allowBottomAction = new QAction(tr("Allow on Bottom"), this);
00278     allowBottomAction->setCheckable(true);
00279     connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool)));
00280 
00281     allowedAreasActions->addAction(allowLeftAction);
00282     allowedAreasActions->addAction(allowRightAction);
00283     allowedAreasActions->addAction(allowTopAction);
00284     allowedAreasActions->addAction(allowBottomAction);
00285 
00286     areaActions = new QActionGroup(this);
00287     areaActions->setExclusive(true);
00288 
00289     leftAction = new QAction(tr("Place on Left") , this);
00290     leftAction->setCheckable(true);
00291     connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool)));
00292 
00293     rightAction = new QAction(tr("Place on Right") , this);
00294     rightAction->setCheckable(true);
00295     connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool)));
00296 
00297     topAction = new QAction(tr("Place on Top") , this);
00298     topAction->setCheckable(true);
00299     connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool)));
00300 
00301     bottomAction = new QAction(tr("Place on Bottom") , this);
00302     bottomAction->setCheckable(true);
00303     connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool)));
00304 
00305     areaActions->addAction(leftAction);
00306     areaActions->addAction(rightAction);
00307     areaActions->addAction(topAction);
00308     areaActions->addAction(bottomAction);
00309 
00310     connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool)));
00311 
00312     connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool)));
00313 
00314     connect(floatableAction, SIGNAL(triggered(bool)), floatingAction, SLOT(setEnabled(bool)));
00315 
00316     connect(floatingAction, SIGNAL(triggered(bool)), floatableAction, SLOT(setDisabled(bool)));
00317     connect(movableAction, SIGNAL(triggered(bool)), floatableAction, SLOT(setEnabled(bool)));
00318 
00319     tabMenu = new QMenu(this);
00320     tabMenu->setTitle(tr("Tab into"));
00321     connect(tabMenu, SIGNAL(triggered(QAction*)), this, SLOT(tabInto(QAction*)));
00322 
00323     splitHMenu = new QMenu(this);
00324     splitHMenu->setTitle(tr("Split horizontally into"));
00325     connect(splitHMenu, SIGNAL(triggered(QAction*)), this, SLOT(splitInto(QAction*)));
00326 
00327     splitVMenu = new QMenu(this);
00328     splitVMenu->setTitle(tr("Split vertically into"));
00329     connect(splitVMenu, SIGNAL(triggered(QAction*)), this, SLOT(splitInto(QAction*)));
00330 
00331     menu = new QMenu(colorName, this);
00332     menu->addAction(toggleViewAction());
00333     QAction *action = menu->addAction(tr("Raise"));
00334     connect(action, SIGNAL(triggered()), this, SLOT(raise()));
00335     menu->addAction(changeSizeHintsAction);
00336     menu->addSeparator();
00337     menu->addAction(closableAction);
00338     menu->addAction(movableAction);
00339     menu->addAction(floatableAction);
00340     menu->addAction(floatingAction);
00341     menu->addSeparator();
00342     menu->addActions(allowedAreasActions->actions());
00343     menu->addSeparator();
00344     menu->addActions(areaActions->actions());
00345     menu->addSeparator();
00346     menu->addMenu(splitHMenu);
00347     menu->addMenu(splitVMenu);
00348     menu->addMenu(tabMenu);
00349 
00350     connect(menu, SIGNAL(aboutToShow()), this, SLOT(updateContextMenu()));
00351 
00352     if(colorName == "Black") {
00353         leftAction->setShortcut(Qt::CTRL|Qt::Key_W);
00354         rightAction->setShortcut(Qt::CTRL|Qt::Key_E);
00355         toggleViewAction()->setShortcut(Qt::CTRL|Qt::Key_R);
00356     }
00357 }
00358 
00359 void ColorSwatch::updateContextMenu()
00360 {
00361     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
00362     const Qt::DockWidgetArea area = mainWindow->dockWidgetArea(this);
00363     const Qt::DockWidgetAreas areas = allowedAreas();
00364 
00365     closableAction->setChecked(features() & QDockWidget::DockWidgetClosable);
00366     if (windowType() == Qt::Drawer) {
00367         floatableAction->setEnabled(false);
00368         floatingAction->setEnabled(false);
00369         movableAction->setEnabled(false);
00370     } else {
00371         floatableAction->setChecked(features() & QDockWidget::DockWidgetFloatable);
00372         floatingAction->setChecked(isWindow());
00373         // done after floating, to get 'floatable' correctly initialized
00374         movableAction->setChecked(features() & QDockWidget::DockWidgetMovable);
00375     }
00376 
00377     allowLeftAction->setChecked(isAreaAllowed(Qt::LeftDockWidgetArea));
00378     allowRightAction->setChecked(isAreaAllowed(Qt::RightDockWidgetArea));
00379     allowTopAction->setChecked(isAreaAllowed(Qt::TopDockWidgetArea));
00380     allowBottomAction->setChecked(isAreaAllowed(Qt::BottomDockWidgetArea));
00381 
00382     if (allowedAreasActions->isEnabled()) {
00383         allowLeftAction->setEnabled(area != Qt::LeftDockWidgetArea);
00384         allowRightAction->setEnabled(area != Qt::RightDockWidgetArea);
00385         allowTopAction->setEnabled(area != Qt::TopDockWidgetArea);
00386         allowBottomAction->setEnabled(area != Qt::BottomDockWidgetArea);
00387     }
00388 
00389     leftAction->blockSignals(true);
00390     rightAction->blockSignals(true);
00391     topAction->blockSignals(true);
00392     bottomAction->blockSignals(true);
00393 
00394     leftAction->setChecked(area == Qt::LeftDockWidgetArea);
00395     rightAction->setChecked(area == Qt::RightDockWidgetArea);
00396     topAction->setChecked(area == Qt::TopDockWidgetArea);
00397     bottomAction->setChecked(area == Qt::BottomDockWidgetArea);
00398 
00399     leftAction->blockSignals(false);
00400     rightAction->blockSignals(false);
00401     topAction->blockSignals(false);
00402     bottomAction->blockSignals(false);
00403 
00404     if (areaActions->isEnabled()) {
00405         leftAction->setEnabled(areas & Qt::LeftDockWidgetArea);
00406         rightAction->setEnabled(areas & Qt::RightDockWidgetArea);
00407         topAction->setEnabled(areas & Qt::TopDockWidgetArea);
00408         bottomAction->setEnabled(areas & Qt::BottomDockWidgetArea);
00409     }
00410 
00411     tabMenu->clear();
00412     splitHMenu->clear();
00413     splitVMenu->clear();
00414     QList<ColorSwatch*> dock_list = qFindChildren<ColorSwatch*>(mainWindow);
00415     foreach (ColorSwatch *dock, dock_list) {
00416 //        if (!dock->isVisible() || dock->isFloating())
00417 //            continue;
00418         tabMenu->addAction(dock->windowTitle());
00419         splitHMenu->addAction(dock->windowTitle());
00420         splitVMenu->addAction(dock->windowTitle());
00421     }
00422 }
00423 
00424 void ColorSwatch::splitInto(QAction *action)
00425 {
00426     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
00427     QList<ColorSwatch*> dock_list = qFindChildren<ColorSwatch*>(mainWindow);
00428     ColorSwatch *target = 0;
00429     foreach (ColorSwatch *dock, dock_list) {
00430         if (action->text() == dock->windowTitle()) {
00431             target = dock;
00432             break;
00433         }
00434     }
00435     if (target == 0)
00436         return;
00437 
00438     Qt::Orientation o = action->parent() == splitHMenu
00439                         ? Qt::Horizontal : Qt::Vertical;
00440     mainWindow->splitDockWidget(target, this, o);
00441 }
00442 
00443 void ColorSwatch::tabInto(QAction *action)
00444 {
00445     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
00446     QList<ColorSwatch*> dock_list = qFindChildren<ColorSwatch*>(mainWindow);
00447     ColorSwatch *target = 0;
00448     foreach (ColorSwatch *dock, dock_list) {
00449         if (action->text() == dock->windowTitle()) {
00450             target = dock;
00451             break;
00452         }
00453     }
00454     if (target == 0)
00455         return;
00456 
00457     mainWindow->tabifyDockWidget(target, this);
00458 }
00459 
00460 void ColorSwatch::contextMenuEvent(QContextMenuEvent *event)
00461 {
00462     event->accept();
00463     menu->exec(event->globalPos());
00464 }
00465 
00466 void ColorSwatch::allow(Qt::DockWidgetArea area, bool a)
00467 {
00468     Qt::DockWidgetAreas areas = allowedAreas();
00469     areas = a ? areas | area : areas & ~area;
00470     setAllowedAreas(areas);
00471 
00472     if (areaActions->isEnabled()) {
00473         leftAction->setEnabled(areas & Qt::LeftDockWidgetArea);
00474         rightAction->setEnabled(areas & Qt::RightDockWidgetArea);
00475         topAction->setEnabled(areas & Qt::TopDockWidgetArea);
00476         bottomAction->setEnabled(areas & Qt::BottomDockWidgetArea);
00477     }
00478 }
00479 
00480 void ColorSwatch::place(Qt::DockWidgetArea area, bool p)
00481 {
00482     if (!p) return;
00483 
00484     QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
00485     mainWindow->addDockWidget(area, this);
00486 
00487     if (allowedAreasActions->isEnabled()) {
00488         allowLeftAction->setEnabled(area != Qt::LeftDockWidgetArea);
00489         allowRightAction->setEnabled(area != Qt::RightDockWidgetArea);
00490         allowTopAction->setEnabled(area != Qt::TopDockWidgetArea);
00491         allowBottomAction->setEnabled(area != Qt::BottomDockWidgetArea);
00492     }
00493 }
00494 
00495 void ColorSwatch::changeClosable(bool on)
00496 { setFeatures(on ? features() | DockWidgetClosable : features() & ~DockWidgetClosable); }
00497 
00498 void ColorSwatch::changeMovable(bool on)
00499 { setFeatures(on ? features() | DockWidgetMovable : features() & ~DockWidgetMovable); }
00500 
00501 void ColorSwatch::changeFloatable(bool on)
00502 { setFeatures(on ? features() | DockWidgetFloatable : features() & ~DockWidgetFloatable); }
00503 
00504 void ColorSwatch::changeFloating(bool floating)
00505 { setFloating(floating); }
00506 
00507 void ColorSwatch::allowLeft(bool a)
00508 { allow(Qt::LeftDockWidgetArea, a); }
00509 
00510 void ColorSwatch::allowRight(bool a)
00511 { allow(Qt::RightDockWidgetArea, a); }
00512 
00513 void ColorSwatch::allowTop(bool a)
00514 { allow(Qt::TopDockWidgetArea, a); }
00515 
00516 void ColorSwatch::allowBottom(bool a)
00517 { allow(Qt::BottomDockWidgetArea, a); }
00518 
00519 void ColorSwatch::placeLeft(bool p)
00520 { place(Qt::LeftDockWidgetArea, p); }
00521 
00522 void ColorSwatch::placeRight(bool p)
00523 { place(Qt::RightDockWidgetArea, p); }
00524 
00525 void ColorSwatch::placeTop(bool p)
00526 { place(Qt::TopDockWidgetArea, p); }
00527 
00528 void ColorSwatch::placeBottom(bool p)
00529 { place(Qt::BottomDockWidgetArea, p); }
00530 
00531 #include "colorswatch.moc"

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