1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the demonstration applications of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "colorswatch.h"
34
#include <qmainwindow.h>
39
#include <qpainterpath.h>
42
QColor bgColorForName(const QString &name)
45
return QColor("#D8D8D8");
46
else if (name == "White")
47
return QColor("#F1F1F1");
48
else if (name == "Red")
49
return QColor("#F1D8D8");
50
else if (name == "Green")
51
return QColor("#D8E4D8");
52
else if (name == "Blue")
53
return QColor("#D8D8F1");
54
else if (name == "Yellow")
55
return QColor("#F1F0D8");
59
QColor fgColorForName(const QString &name)
62
return QColor("#6C6C6C");
63
else if (name == "White")
64
return QColor("#F8F8F8");
65
else if (name == "Red")
66
return QColor("#F86C6C");
67
else if (name == "Green")
68
return QColor("#6CB26C");
69
else if (name == "Blue")
70
return QColor("#6C6CF8");
71
else if (name == "Yellow")
72
return QColor("#F8F76C");
76
class ColorDock : public QFrame
79
ColorDock(const QString &c, QWidget *parent)
86
void paintEvent(QPaintEvent *) {
88
p.setRenderHint(QPainter::Antialiasing);
89
p.fillRect(rect(), bgColorForName(color));
91
extern void render_qt_text(QPainter *, int, int, const QColor &);
92
render_qt_text(&p, width(), height(), fgColorForName(color));
98
ColorSwatch::ColorSwatch(const QString &colorName, QWidget *parent, Qt::WFlags flags)
99
: QDockWidget(parent, flags)
101
setObjectName(colorName + QLatin1String(" Dock Widget"));
102
setWindowTitle(objectName());
104
QFrame *swatch = new ColorDock(colorName, this);
105
swatch->setFrameStyle(QFrame::Box | QFrame::Sunken);
106
swatch->setMinimumSize(125, 75);
110
closableAction = new QAction(tr("Closable"), this);
111
closableAction->setCheckable(true);
112
connect(closableAction, SIGNAL(triggered(bool)), SLOT(changeClosable(bool)));
114
movableAction = new QAction(tr("Movable"), this);
115
movableAction->setCheckable(true);
116
connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool)));
118
floatableAction = new QAction(tr("Floatable"), this);
119
floatableAction->setCheckable(true);
120
connect(floatableAction, SIGNAL(triggered(bool)), SLOT(changeFloatable(bool)));
122
floatingAction = new QAction(tr("Floating"), this);
123
floatingAction->setCheckable(true);
124
connect(floatingAction, SIGNAL(triggered(bool)), SLOT(changeFloating(bool)));
126
allowedAreasActions = new QActionGroup(this);
127
allowedAreasActions->setExclusive(false);
129
allowLeftAction = new QAction(tr("Allow on Left"), this);
130
allowLeftAction->setCheckable(true);
131
connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool)));
133
allowRightAction = new QAction(tr("Allow on Right"), this);
134
allowRightAction->setCheckable(true);
135
connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool)));
137
allowTopAction = new QAction(tr("Allow on Top"), this);
138
allowTopAction->setCheckable(true);
139
connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool)));
141
allowBottomAction = new QAction(tr("Allow on Bottom"), this);
142
allowBottomAction->setCheckable(true);
143
connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool)));
145
allowedAreasActions->addAction(allowLeftAction);
146
allowedAreasActions->addAction(allowRightAction);
147
allowedAreasActions->addAction(allowTopAction);
148
allowedAreasActions->addAction(allowBottomAction);
150
areaActions = new QActionGroup(this);
151
areaActions->setExclusive(true);
153
leftAction = new QAction(tr("Place on Left") , this);
154
leftAction->setCheckable(true);
155
connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool)));
157
rightAction = new QAction(tr("Place on Right") , this);
158
rightAction->setCheckable(true);
159
connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool)));
161
topAction = new QAction(tr("Place on Top") , this);
162
topAction->setCheckable(true);
163
connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool)));
165
bottomAction = new QAction(tr("Place on Bottom") , this);
166
bottomAction->setCheckable(true);
167
connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool)));
169
areaActions->addAction(leftAction);
170
areaActions->addAction(rightAction);
171
areaActions->addAction(topAction);
172
areaActions->addAction(bottomAction);
174
connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool)));
176
connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool)));
178
connect(floatableAction, SIGNAL(triggered(bool)), floatingAction, SLOT(setEnabled(bool)));
180
connect(floatingAction, SIGNAL(triggered(bool)), floatableAction, SLOT(setDisabled(bool)));
181
connect(movableAction, SIGNAL(triggered(bool)), floatableAction, SLOT(setEnabled(bool)));
183
menu = new QMenu(colorName, this);
184
menu->addAction(toggleViewAction());
185
menu->addSeparator();
186
menu->addAction(closableAction);
187
menu->addAction(movableAction);
188
menu->addAction(floatableAction);
189
menu->addAction(floatingAction);
190
menu->addSeparator();
191
menu->addActions(allowedAreasActions->actions());
192
menu->addSeparator();
193
menu->addActions(areaActions->actions());
195
if(colorName == "Black") {
196
leftAction->setShortcut(Qt::CTRL|Qt::Key_W);
197
rightAction->setShortcut(Qt::CTRL|Qt::Key_E);
198
toggleViewAction()->setShortcut(Qt::CTRL|Qt::Key_R);
202
void ColorSwatch::contextMenuEvent(QContextMenuEvent *event)
205
menu->exec(event->globalPos());
208
bool ColorSwatch::event(QEvent *e)
210
if (e->type() != QEvent::Polish)
211
return QDockWidget::event(e);
213
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
214
const Qt::DockWidgetArea area = mainWindow->dockWidgetArea(this);
215
const Qt::DockWidgetAreas areas = allowedAreas();
217
closableAction->setChecked(features() & QDockWidget::DockWidgetClosable);
218
if (windowType() == Qt::Drawer) {
219
floatableAction->setEnabled(false);
220
floatingAction->setEnabled(false);
221
movableAction->setEnabled(false);
223
floatableAction->setChecked(features() & QDockWidget::DockWidgetFloatable);
224
floatingAction->setChecked(isWindow());
225
// done after floating, to get 'floatable' correctly initialized
226
movableAction->setChecked(features() & QDockWidget::DockWidgetMovable);
229
allowLeftAction->setChecked(isAreaAllowed(Qt::LeftDockWidgetArea));
230
allowRightAction->setChecked(isAreaAllowed(Qt::RightDockWidgetArea));
231
allowTopAction->setChecked(isAreaAllowed(Qt::TopDockWidgetArea));
232
allowBottomAction->setChecked(isAreaAllowed(Qt::BottomDockWidgetArea));
234
if (allowedAreasActions->isEnabled()) {
235
allowLeftAction->setEnabled(area != Qt::LeftDockWidgetArea);
236
allowRightAction->setEnabled(area != Qt::RightDockWidgetArea);
237
allowTopAction->setEnabled(area != Qt::TopDockWidgetArea);
238
allowBottomAction->setEnabled(area != Qt::BottomDockWidgetArea);
241
leftAction->blockSignals(true);
242
rightAction->blockSignals(true);
243
topAction->blockSignals(true);
244
bottomAction->blockSignals(true);
246
leftAction->setChecked(area == Qt::LeftDockWidgetArea);
247
rightAction->setChecked(area == Qt::RightDockWidgetArea);
248
topAction->setChecked(area == Qt::TopDockWidgetArea);
249
bottomAction->setChecked(area == Qt::BottomDockWidgetArea);
251
leftAction->blockSignals(false);
252
rightAction->blockSignals(false);
253
topAction->blockSignals(false);
254
bottomAction->blockSignals(false);
256
if (areaActions->isEnabled()) {
257
leftAction->setEnabled(areas & Qt::LeftDockWidgetArea);
258
rightAction->setEnabled(areas & Qt::RightDockWidgetArea);
259
topAction->setEnabled(areas & Qt::TopDockWidgetArea);
260
bottomAction->setEnabled(areas & Qt::BottomDockWidgetArea);
262
return QDockWidget::event(e);
265
void ColorSwatch::allow(Qt::DockWidgetArea area, bool a)
267
Qt::DockWidgetAreas areas = allowedAreas();
268
areas = a ? areas | area : areas & ~area;
269
setAllowedAreas(areas);
271
if (areaActions->isEnabled()) {
272
leftAction->setEnabled(areas & Qt::LeftDockWidgetArea);
273
rightAction->setEnabled(areas & Qt::RightDockWidgetArea);
274
topAction->setEnabled(areas & Qt::TopDockWidgetArea);
275
bottomAction->setEnabled(areas & Qt::BottomDockWidgetArea);
279
void ColorSwatch::place(Qt::DockWidgetArea area, bool p)
283
QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
284
mainWindow->addDockWidget(area, this);
286
if (allowedAreasActions->isEnabled()) {
287
allowLeftAction->setEnabled(area != Qt::LeftDockWidgetArea);
288
allowRightAction->setEnabled(area != Qt::RightDockWidgetArea);
289
allowTopAction->setEnabled(area != Qt::TopDockWidgetArea);
290
allowBottomAction->setEnabled(area != Qt::BottomDockWidgetArea);
294
void ColorSwatch::changeClosable(bool on)
295
{ setFeatures(on ? features() | DockWidgetClosable : features() & ~DockWidgetClosable); }
297
void ColorSwatch::changeMovable(bool on)
298
{ setFeatures(on ? features() | DockWidgetMovable : features() & ~DockWidgetMovable); }
300
void ColorSwatch::changeFloatable(bool on)
301
{ setFeatures(on ? features() | DockWidgetFloatable : features() & ~DockWidgetFloatable); }
303
void ColorSwatch::changeFloating(bool floating)
304
{ setFloating(floating); }
306
void ColorSwatch::allowLeft(bool a)
307
{ allow(Qt::LeftDockWidgetArea, a); }
309
void ColorSwatch::allowRight(bool a)
310
{ allow(Qt::RightDockWidgetArea, a); }
312
void ColorSwatch::allowTop(bool a)
313
{ allow(Qt::TopDockWidgetArea, a); }
315
void ColorSwatch::allowBottom(bool a)
316
{ allow(Qt::BottomDockWidgetArea, a); }
318
void ColorSwatch::placeLeft(bool p)
319
{ place(Qt::LeftDockWidgetArea, p); }
321
void ColorSwatch::placeRight(bool p)
322
{ place(Qt::RightDockWidgetArea, p); }
324
void ColorSwatch::placeTop(bool p)
325
{ place(Qt::TopDockWidgetArea, p); }
327
void ColorSwatch::placeBottom(bool p)
328
{ place(Qt::BottomDockWidgetArea, p); }