1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "colorswatch.h"
52
53#include <QAction>
54#include <QtEvents>
55#include <QFrame>
56#include <QMainWindow>
57#include <QMenu>
58#include <QPainter>
59#include <QImage>
60#include <QColor>
61#include <QDialog>
62#include <QDialogButtonBox>
63#include <QGridLayout>
64#include <QSignalBlocker>
65#include <QSpinBox>
66#include <QLabel>
67#include <QPainterPath>
68#include <QPushButton>
69#include <QHBoxLayout>
70#include <QBitmap>
71#include <QtDebug>
72
73#undef DEBUG_SIZEHINTS
74
75QColor bgColorForName(const QString &name)
76{
77 if (name == "Black")
78 return QColor("#D8D8D8");
79 if (name == "White")
80 return QColor("#F1F1F1");
81 if (name == "Red")
82 return QColor("#F1D8D8");
83 if (name == "Green")
84 return QColor("#D8E4D8");
85 if (name == "Blue")
86 return QColor("#D8D8F1");
87 if (name == "Yellow")
88 return QColor("#F1F0D8");
89 return QColor(name).light(110);
90}
91
92QColor fgColorForName(const QString &name)
93{
94 if (name == "Black")
95 return QColor("#6C6C6C");
96 if (name == "White")
97 return QColor("#F8F8F8");
98 if (name == "Red")
99 return QColor("#F86C6C");
100 if (name == "Green")
101 return QColor("#6CB26C");
102 if (name == "Blue")
103 return QColor("#6C6CF8");
104 if (name == "Yellow")
105 return QColor("#F8F76C");
106 return QColor(name);
107}
108
109class ColorDock : public QFrame
110{
111 Q_OBJECT
112public:
113 explicit ColorDock(const QString &c, QWidget *parent);
114
115 QSize sizeHint() const override { return szHint; }
116 QSize minimumSizeHint() const override { return minSzHint; }
117
118 void setCustomSizeHint(const QSize &size);
119
120public slots:
121 void changeSizeHints();
122
123protected:
124 void paintEvent(QPaintEvent *) override;
125
126private:
127 const QString color;
128 QSize szHint;
129 QSize minSzHint;
130};
131
132ColorDock::ColorDock(const QString &c, QWidget *parent)
133 : QFrame(parent)
134 , color(c)
135 , szHint(-1, -1)
136 , minSzHint(125, 75)
137{
138 QFont font = this->font();
139 font.setPointSize(8);
140 setFont(font);
141}
142
143void ColorDock::paintEvent(QPaintEvent *)
144{
145 QPainter p(this);
146 p.setRenderHint(QPainter::Antialiasing);
147 p.fillRect(rect(), bgColorForName(color));
148
149 p.save();
150
151 extern void render_qt_text(QPainter *, int, int, const QColor &);
152 render_qt_text(&p, width(), height(), fgColorForName(color));
153
154 p.restore();
155
156#ifdef DEBUG_SIZEHINTS
157 p.setRenderHint(QPainter::Antialiasing, false);
158
159 QSize sz = size();
160 QSize szHint = sizeHint();
161 QSize minSzHint = minimumSizeHint();
162 QSize maxSz = maximumSize();
163 QString text = QString::fromLatin1("sz: %1x%2\nszHint: %3x%4\nminSzHint: %5x%6\n"
164 "maxSz: %8x%9")
165 .arg(sz.width()).arg(sz.height())
166 .arg(szHint.width()).arg(szHint.height())
167 .arg(minSzHint.width()).arg(minSzHint.height())
168 .arg(maxSz.width()).arg(maxSz.height());
169
170 QRect r = fontMetrics().boundingRect(rect(), Qt::AlignLeft|Qt::AlignTop, text);
171 r.adjust(-2, -2, 1, 1);
172 p.translate(4, 4);
173 QColor bg = Qt::yellow;
174 bg.setAlpha(120);
175 p.setBrush(bg);
176 p.setPen(Qt::black);
177 p.drawRect(r);
178 p.drawText(rect(), Qt::AlignLeft|Qt::AlignTop, text);
179#endif // DEBUG_SIZEHINTS
180}
181
182static QSpinBox *createSpinBox(int value, QWidget *parent, int max = 1000)
183{
184 QSpinBox *result = new QSpinBox(parent);
185 result->setMinimum(-1);
186 result->setMaximum(max);
187 result->setValue(value);
188 return result;
189}
190
191void ColorDock::changeSizeHints()
192{
193 QDialog dialog(this);
194 dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
195 dialog.setWindowTitle(color);
196
197 QVBoxLayout *topLayout = new QVBoxLayout(&dialog);
198
199 QGridLayout *inputLayout = new QGridLayout();
200 topLayout->addLayout(inputLayout);
201
202 inputLayout->addWidget(new QLabel(tr("Size Hint:"), &dialog), 0, 0);
203 inputLayout->addWidget(new QLabel(tr("Min Size Hint:"), &dialog), 1, 0);
204 inputLayout->addWidget(new QLabel(tr("Max Size:"), &dialog), 2, 0);
205 inputLayout->addWidget(new QLabel(tr("Dock Widget Max Size:"), &dialog), 3, 0);
206
207 QSpinBox *szHintW = createSpinBox(szHint.width(), &dialog);
208 inputLayout->addWidget(szHintW, 0, 1);
209 QSpinBox *szHintH = createSpinBox(szHint.height(), &dialog);
210 inputLayout->addWidget(szHintH, 0, 2);
211
212 QSpinBox *minSzHintW = createSpinBox(minSzHint.width(), &dialog);
213 inputLayout->addWidget(minSzHintW, 1, 1);
214 QSpinBox *minSzHintH = createSpinBox(minSzHint.height(), &dialog);
215 inputLayout->addWidget(minSzHintH, 1, 2);
216
217 QSize maxSz = maximumSize();
218 QSpinBox *maxSzW = createSpinBox(maxSz.width(), &dialog, QWIDGETSIZE_MAX);
219 inputLayout->addWidget(maxSzW, 2, 1);
220 QSpinBox *maxSzH = createSpinBox(maxSz.height(), &dialog, QWIDGETSIZE_MAX);
221 inputLayout->addWidget(maxSzH, 2, 2);
222
223 QSize dwMaxSz = parentWidget()->maximumSize();
224 QSpinBox *dwMaxSzW = createSpinBox(dwMaxSz.width(), &dialog, QWIDGETSIZE_MAX);
225 inputLayout->addWidget(dwMaxSzW, 3, 1);
226 QSpinBox *dwMaxSzH = createSpinBox(dwMaxSz.height(), &dialog, QWIDGETSIZE_MAX);
227 inputLayout->addWidget(dwMaxSzH, 3, 2);
228
229 inputLayout->setColumnStretch(1, 1);
230 inputLayout->setColumnStretch(2, 1);
231
232 topLayout->addStretch();
233
234 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
235 connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
236 connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::reject);
237
238 topLayout->addWidget(buttonBox);
239
240 if (dialog.exec() != QDialog::Accepted)
241 return;
242
243 szHint = QSize(szHintW->value(), szHintH->value());
244 minSzHint = QSize(minSzHintW->value(), minSzHintH->value());
245 maxSz = QSize(maxSzW->value(), maxSzH->value());
246 setMaximumSize(maxSz);
247 dwMaxSz = QSize(dwMaxSzW->value(), dwMaxSzH->value());
248 parentWidget()->setMaximumSize(dwMaxSz);
249 updateGeometry();
250 update();
251}
252
253void ColorDock::setCustomSizeHint(const QSize &size)
254{
255 if (szHint != size) {
256 szHint = size;
257 updateGeometry();
258 }
259}
260
261ColorSwatch::ColorSwatch(const QString &colorName, QMainWindow *parent, Qt::WindowFlags flags)
262 : QDockWidget(parent, flags), mainWindow(parent)
263{
264 setObjectName(colorName + QLatin1String(" Dock Widget"));
265 setWindowTitle(objectName() + QLatin1String(" [*]"));
266
267 ColorDock *swatch = new ColorDock(colorName, this);
268 swatch->setFrameStyle(QFrame::Box | QFrame::Sunken);
269
270 setWidget(swatch);
271
272 closableAction = new QAction(tr("Closable"), this);
273 closableAction->setCheckable(true);
274 connect(closableAction, &QAction::triggered, this, &ColorSwatch::changeClosable);
275
276 movableAction = new QAction(tr("Movable"), this);
277 movableAction->setCheckable(true);
278 connect(movableAction, &QAction::triggered, this, &ColorSwatch::changeMovable);
279
280 floatableAction = new QAction(tr("Floatable"), this);
281 floatableAction->setCheckable(true);
282 connect(floatableAction, &QAction::triggered, this, &ColorSwatch::changeFloatable);
283
284 verticalTitleBarAction = new QAction(tr("Vertical title bar"), this);
285 verticalTitleBarAction->setCheckable(true);
286 connect(verticalTitleBarAction, &QAction::triggered,
287 this, &ColorSwatch::changeVerticalTitleBar);
288
289 floatingAction = new QAction(tr("Floating"), this);
290 floatingAction->setCheckable(true);
291 connect(floatingAction, &QAction::triggered, this, &ColorSwatch::changeFloating);
292
293 allowedAreasActions = new QActionGroup(this);
294 allowedAreasActions->setExclusive(false);
295
296 allowLeftAction = new QAction(tr("Allow on Left"), this);
297 allowLeftAction->setCheckable(true);
298 connect(allowLeftAction, &QAction::triggered, this, &ColorSwatch::allowLeft);
299
300 allowRightAction = new QAction(tr("Allow on Right"), this);
301 allowRightAction->setCheckable(true);
302 connect(allowRightAction, &QAction::triggered, this, &ColorSwatch::allowRight);
303
304 allowTopAction = new QAction(tr("Allow on Top"), this);
305 allowTopAction->setCheckable(true);
306 connect(allowTopAction, &QAction::triggered, this, &ColorSwatch::allowTop);
307
308 allowBottomAction = new QAction(tr("Allow on Bottom"), this);
309 allowBottomAction->setCheckable(true);
310 connect(allowBottomAction, &QAction::triggered, this, &ColorSwatch::allowBottom);
311
312 allowedAreasActions->addAction(allowLeftAction);
313 allowedAreasActions->addAction(allowRightAction);
314 allowedAreasActions->addAction(allowTopAction);
315 allowedAreasActions->addAction(allowBottomAction);
316
317 areaActions = new QActionGroup(this);
318 areaActions->setExclusive(true);
319
320 leftAction = new QAction(tr("Place on Left") , this);
321 leftAction->setCheckable(true);
322 connect(leftAction, &QAction::triggered, this, &ColorSwatch::placeLeft);
323
324 rightAction = new QAction(tr("Place on Right") , this);
325 rightAction->setCheckable(true);
326 connect(rightAction, &QAction::triggered, this, &ColorSwatch::placeRight);
327
328 topAction = new QAction(tr("Place on Top") , this);
329 topAction->setCheckable(true);
330 connect(topAction, &QAction::triggered, this, &ColorSwatch::placeTop);
331
332 bottomAction = new QAction(tr("Place on Bottom") , this);
333 bottomAction->setCheckable(true);
334 connect(bottomAction, &QAction::triggered, this, &ColorSwatch::placeBottom);
335
336 areaActions->addAction(leftAction);
337 areaActions->addAction(rightAction);
338 areaActions->addAction(topAction);
339 areaActions->addAction(bottomAction);
340
341 connect(movableAction, &QAction::triggered, areaActions, &QActionGroup::setEnabled);
342
343 connect(movableAction, &QAction::triggered, allowedAreasActions, &QActionGroup::setEnabled);
344
345 connect(floatableAction, &QAction::triggered, floatingAction, &QAction::setEnabled);
346
347 connect(floatingAction, &QAction::triggered, floatableAction, &QAction::setDisabled);
348 connect(movableAction, &QAction::triggered, floatableAction, &QAction::setEnabled);
349
350 tabMenu = new QMenu(this);
351 tabMenu->setTitle(tr("Tab into"));
352 connect(tabMenu, &QMenu::triggered, this, &ColorSwatch::tabInto);
353
354 splitHMenu = new QMenu(this);
355 splitHMenu->setTitle(tr("Split horizontally into"));
356 connect(splitHMenu, &QMenu::triggered, this, &ColorSwatch::splitInto);
357
358 splitVMenu = new QMenu(this);
359 splitVMenu->setTitle(tr("Split vertically into"));
360 connect(splitVMenu, &QMenu::triggered, this, &ColorSwatch::splitInto);
361
362 QAction *windowModifiedAction = new QAction(tr("Modified"), this);
363 windowModifiedAction->setCheckable(true);
364 windowModifiedAction->setChecked(false);
365 connect(windowModifiedAction, &QAction::toggled, this, &QWidget::setWindowModified);
366
367 menu = new QMenu(colorName, this);
368 menu->addAction(toggleViewAction());
369 menu->addAction(tr("Raise"), this, &QWidget::raise);
370 menu->addAction(tr("Change Size Hints..."), swatch, &ColorDock::changeSizeHints);
371
372 menu->addSeparator();
373 menu->addAction(closableAction);
374 menu->addAction(movableAction);
375 menu->addAction(floatableAction);
376 menu->addAction(floatingAction);
377 menu->addAction(verticalTitleBarAction);
378 menu->addSeparator();
379 menu->addActions(allowedAreasActions->actions());
380 menu->addSeparator();
381 menu->addActions(areaActions->actions());
382 menu->addSeparator();
383 menu->addMenu(splitHMenu);
384 menu->addMenu(splitVMenu);
385 menu->addMenu(tabMenu);
386 menu->addSeparator();
387 menu->addAction(windowModifiedAction);
388
389 connect(menu, &QMenu::aboutToShow, this, &ColorSwatch::updateContextMenu);
390
391 if (colorName == QLatin1String("Black")) {
392 leftAction->setShortcut(Qt::CTRL | Qt::Key_W);
393 rightAction->setShortcut(Qt::CTRL | Qt::Key_E);
394 toggleViewAction()->setShortcut(Qt::CTRL | Qt::Key_R);
395 }
396}
397
398void ColorSwatch::updateContextMenu()
399{
400 const Qt::DockWidgetArea area = mainWindow->dockWidgetArea(this);
401 const Qt::DockWidgetAreas areas = allowedAreas();
402
403 closableAction->setChecked(features() & QDockWidget::DockWidgetClosable);
404 if (windowType() == Qt::Drawer) {
405 floatableAction->setEnabled(false);
406 floatingAction->setEnabled(false);
407 movableAction->setEnabled(false);
408 verticalTitleBarAction->setChecked(false);
409 } else {
410 floatableAction->setChecked(features() & QDockWidget::DockWidgetFloatable);
411 floatingAction->setChecked(isWindow());
412 // done after floating, to get 'floatable' correctly initialized
413 movableAction->setChecked(features() & QDockWidget::DockWidgetMovable);
414 verticalTitleBarAction
415 ->setChecked(features() & QDockWidget::DockWidgetVerticalTitleBar);
416 }
417
418 allowLeftAction->setChecked(isAreaAllowed(Qt::LeftDockWidgetArea));
419 allowRightAction->setChecked(isAreaAllowed(Qt::RightDockWidgetArea));
420 allowTopAction->setChecked(isAreaAllowed(Qt::TopDockWidgetArea));
421 allowBottomAction->setChecked(isAreaAllowed(Qt::BottomDockWidgetArea));
422
423 if (allowedAreasActions->isEnabled()) {
424 allowLeftAction->setEnabled(area != Qt::LeftDockWidgetArea);
425 allowRightAction->setEnabled(area != Qt::RightDockWidgetArea);
426 allowTopAction->setEnabled(area != Qt::TopDockWidgetArea);
427 allowBottomAction->setEnabled(area != Qt::BottomDockWidgetArea);
428 }
429
430 {
431 const QSignalBlocker blocker(leftAction);
432 leftAction->setChecked(area == Qt::LeftDockWidgetArea);
433 }
434 {
435 const QSignalBlocker blocker(rightAction);
436 rightAction->setChecked(area == Qt::RightDockWidgetArea);
437 }
438 {
439 const QSignalBlocker blocker(topAction);
440 topAction->setChecked(area == Qt::TopDockWidgetArea);
441 }
442 {
443 const QSignalBlocker blocker(bottomAction);
444 bottomAction->setChecked(area == Qt::BottomDockWidgetArea);
445 }
446
447 if (areaActions->isEnabled()) {
448 leftAction->setEnabled(areas & Qt::LeftDockWidgetArea);
449 rightAction->setEnabled(areas & Qt::RightDockWidgetArea);
450 topAction->setEnabled(areas & Qt::TopDockWidgetArea);
451 bottomAction->setEnabled(areas & Qt::BottomDockWidgetArea);
452 }
453
454 tabMenu->clear();
455 splitHMenu->clear();
456 splitVMenu->clear();
457 QList<ColorSwatch*> dock_list = mainWindow->findChildren<ColorSwatch*>();
458 foreach (ColorSwatch *dock, dock_list) {
459 tabMenu->addAction(dock->objectName());
460 splitHMenu->addAction(dock->objectName());
461 splitVMenu->addAction(dock->objectName());
462 }
463}
464
465static ColorSwatch *findByName(const QMainWindow *mainWindow, const QString &name)
466{
467 foreach (ColorSwatch *dock, mainWindow->findChildren<ColorSwatch*>()) {
468 if (name == dock->objectName())
469 return dock;
470 }
471 return nullptr;
472}
473
474void ColorSwatch::splitInto(QAction *action)
475{
476 ColorSwatch *target = findByName(mainWindow, action->text());
477 if (!target)
478 return;
479
480 const Qt::Orientation o = action->parent() == splitHMenu
481 ? Qt::Horizontal : Qt::Vertical;
482 mainWindow->splitDockWidget(target, this, o);
483}
484
485void ColorSwatch::tabInto(QAction *action)
486{
487 if (ColorSwatch *target = findByName(mainWindow, action->text()))
488 mainWindow->tabifyDockWidget(target, this);
489}
490
491#ifndef QT_NO_CONTEXTMENU
492void ColorSwatch::contextMenuEvent(QContextMenuEvent *event)
493{
494 event->accept();
495 menu->exec(event->globalPos());
496}
497#endif // QT_NO_CONTEXTMENU
498
499void ColorSwatch::resizeEvent(QResizeEvent *e)
500{
501 if (BlueTitleBar *btb = qobject_cast<BlueTitleBar*>(titleBarWidget()))
502 btb->updateMask();
503
504 QDockWidget::resizeEvent(e);
505}
506
507void ColorSwatch::allow(Qt::DockWidgetArea area, bool a)
508{
509 Qt::DockWidgetAreas areas = allowedAreas();
510 areas = a ? areas | area : areas & ~area;
511 setAllowedAreas(areas);
512
513 if (areaActions->isEnabled()) {
514 leftAction->setEnabled(areas & Qt::LeftDockWidgetArea);
515 rightAction->setEnabled(areas & Qt::RightDockWidgetArea);
516 topAction->setEnabled(areas & Qt::TopDockWidgetArea);
517 bottomAction->setEnabled(areas & Qt::BottomDockWidgetArea);
518 }
519}
520
521void ColorSwatch::place(Qt::DockWidgetArea area, bool p)
522{
523 if (!p)
524 return;
525
526 mainWindow->addDockWidget(area, this);
527
528 if (allowedAreasActions->isEnabled()) {
529 allowLeftAction->setEnabled(area != Qt::LeftDockWidgetArea);
530 allowRightAction->setEnabled(area != Qt::RightDockWidgetArea);
531 allowTopAction->setEnabled(area != Qt::TopDockWidgetArea);
532 allowBottomAction->setEnabled(area != Qt::BottomDockWidgetArea);
533 }
534}
535
536void ColorSwatch::setCustomSizeHint(const QSize &size)
537{
538 if (ColorDock *dock = qobject_cast<ColorDock*>(widget()))
539 dock->setCustomSizeHint(size);
540}
541
542void ColorSwatch::changeClosable(bool on)
543{ setFeatures(on ? features() | DockWidgetClosable : features() & ~DockWidgetClosable); }
544
545void ColorSwatch::changeMovable(bool on)
546{ setFeatures(on ? features() | DockWidgetMovable : features() & ~DockWidgetMovable); }
547
548void ColorSwatch::changeFloatable(bool on)
549{ setFeatures(on ? features() | DockWidgetFloatable : features() & ~DockWidgetFloatable); }
550
551void ColorSwatch::changeFloating(bool floating)
552{ setFloating(floating); }
553
554void ColorSwatch::allowLeft(bool a)
555{ allow(Qt::LeftDockWidgetArea, a); }
556
557void ColorSwatch::allowRight(bool a)
558{ allow(Qt::RightDockWidgetArea, a); }
559
560void ColorSwatch::allowTop(bool a)
561{ allow(Qt::TopDockWidgetArea, a); }
562
563void ColorSwatch::allowBottom(bool a)
564{ allow(Qt::BottomDockWidgetArea, a); }
565
566void ColorSwatch::placeLeft(bool p)
567{ place(Qt::LeftDockWidgetArea, p); }
568
569void ColorSwatch::placeRight(bool p)
570{ place(Qt::RightDockWidgetArea, p); }
571
572void ColorSwatch::placeTop(bool p)
573{ place(Qt::TopDockWidgetArea, p); }
574
575void ColorSwatch::placeBottom(bool p)
576{ place(Qt::BottomDockWidgetArea, p); }
577
578void ColorSwatch::changeVerticalTitleBar(bool on)
579{
580 setFeatures(on ? features() | DockWidgetVerticalTitleBar
581 : features() & ~DockWidgetVerticalTitleBar);
582}
583
584QSize BlueTitleBar::minimumSizeHint() const
585{
586 QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
587 Q_ASSERT(dw != 0);
588 QSize result(leftPm.width() + rightPm.width(), centerPm.height());
589 if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar)
590 result.transpose();
591 return result;
592}
593
594BlueTitleBar::BlueTitleBar(QWidget *parent)
595 : QWidget(parent)
596 , leftPm(QPixmap(":/res/titlebarLeft.png"))
597 , centerPm(QPixmap(":/res/titlebarCenter.png"))
598 , rightPm(QPixmap(":/res/titlebarRight.png"))
599{
600}
601
602void BlueTitleBar::paintEvent(QPaintEvent*)
603{
604 QPainter painter(this);
605 QRect rect = this->rect();
606
607 QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
608 Q_ASSERT(dw != 0);
609
610 if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar) {
611 QSize s = rect.size();
612 s.transpose();
613 rect.setSize(s);
614
615 painter.translate(rect.left(), rect.top() + rect.width());
616 painter.rotate(-90);
617 painter.translate(-rect.left(), -rect.top());
618 }
619
620 painter.drawPixmap(rect.topLeft(), leftPm);
621 painter.drawPixmap(rect.topRight() - QPoint(rightPm.width() - 1, 0), rightPm);
622 QBrush brush(centerPm);
623 painter.fillRect(rect.left() + leftPm.width(), rect.top(),
624 rect.width() - leftPm.width() - rightPm.width(),
625 centerPm.height(), centerPm);
626}
627
628void BlueTitleBar::mouseReleaseEvent(QMouseEvent *event)
629{
630 QPoint pos = event->pos();
631
632 QRect rect = this->rect();
633
634 QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
635 Q_ASSERT(dw != 0);
636
637 if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar) {
638 QPoint p = pos;
639 pos.setX(rect.left() + rect.bottom() - p.y());
640 pos.setY(rect.top() + p.x() - rect.left());
641
642 QSize s = rect.size();
643 s.transpose();
644 rect.setSize(s);
645 }
646
647 const int buttonRight = 7;
648 const int buttonWidth = 20;
649 int right = rect.right() - pos.x();
650 int button = (right - buttonRight)/buttonWidth;
651 switch (button) {
652 case 0:
653 event->accept();
654 dw->close();
655 break;
656 case 1:
657 event->accept();
658 dw->setFloating(!dw->isFloating());
659 break;
660 case 2: {
661 event->accept();
662 QDockWidget::DockWidgetFeatures features = dw->features();
663 if (features & QDockWidget::DockWidgetVerticalTitleBar)
664 features &= ~QDockWidget::DockWidgetVerticalTitleBar;
665 else
666 features |= QDockWidget::DockWidgetVerticalTitleBar;
667 dw->setFeatures(features);
668 break;
669 }
670 default:
671 event->ignore();
672 break;
673 }
674}
675
676void BlueTitleBar::updateMask()
677{
678 QDockWidget *dw = qobject_cast<QDockWidget*>(parent());
679 Q_ASSERT(dw != 0);
680
681 QRect rect = dw->rect();
682 QPixmap bitmap(dw->size());
683
684 {
685 QPainter painter(&bitmap);
686
687 // initialize to transparent
688 painter.fillRect(rect, Qt::color0);
689
690 QRect contents = rect;
691 contents.setTopLeft(geometry().bottomLeft());
692 contents.setRight(geometry().right());
693 contents.setBottom(contents.bottom()-y());
694 painter.fillRect(contents, Qt::color1);
695
696 // let's paint the titlebar
697 QRect titleRect = this->geometry();
698
699 if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar) {
700 QSize s = rect.size();
701 s.transpose();
702 rect.setSize(s);
703
704 QSize s2 = size();
705 s2.transpose();
706 titleRect.setSize(s2);
707
708 painter.translate(rect.left(), rect.top() + rect.width());
709 painter.rotate(-90);
710 painter.translate(-rect.left(), -rect.top());
711 }
712
713 contents.setTopLeft(titleRect.bottomLeft());
714 contents.setRight(titleRect.right());
715 contents.setBottom(rect.bottom()-y());
716
717 QRect rect = titleRect;
718
719 painter.drawPixmap(rect.topLeft(), leftPm.mask());
720 painter.fillRect(rect.left() + leftPm.width(), rect.top(),
721 rect.width() - leftPm.width() - rightPm.width(),
722 centerPm.height(), Qt::color1);
723 painter.drawPixmap(rect.topRight() - QPoint(rightPm.width() - 1, 0), rightPm.mask());
724
725 painter.fillRect(contents, Qt::color1);
726 }
727
728 dw->setMask(bitmap);
729}
730
731#include "colorswatch.moc"
732