~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to demos/mainwindow/colorswatch.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the demonstration applications of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "colorswatch.h"
 
30
 
 
31
#include <qaction.h>
 
32
#include <qevent.h>
 
33
#include <qframe.h>
 
34
#include <qmainwindow.h>
 
35
#include <qmenu.h>
 
36
#include <qpainter.h>
 
37
#include <qimage.h>
 
38
#include <qcolor.h>
 
39
#include <qpainterpath.h>
 
40
#include <qdebug.h>
 
41
 
 
42
QColor bgColorForName(const QString &name)
 
43
{
 
44
    if (name == "Black")
 
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");
 
56
    return QColor();
 
57
}
 
58
 
 
59
QColor fgColorForName(const QString &name)
 
60
{
 
61
    if (name == "Black")
 
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");
 
73
    return QColor();
 
74
}
 
75
 
 
76
class ColorDock : public QFrame
 
77
{
 
78
public:
 
79
    ColorDock(const QString &c, QWidget *parent)
 
80
        : QFrame(parent)
 
81
        , color(c)
 
82
    {
 
83
    }
 
84
 
 
85
protected:
 
86
    void paintEvent(QPaintEvent *) {
 
87
        QPainter p(this);
 
88
        p.setRenderHint(QPainter::Antialiasing);
 
89
        p.fillRect(rect(), bgColorForName(color));
 
90
 
 
91
        extern void render_qt_text(QPainter *, int, int, const QColor &);
 
92
        render_qt_text(&p, width(), height(), fgColorForName(color));
 
93
    }
 
94
 
 
95
    QString color;
 
96
};
 
97
 
 
98
ColorSwatch::ColorSwatch(const QString &colorName, QWidget *parent, Qt::WFlags flags)
 
99
    : QDockWidget(parent, flags)
 
100
{
 
101
    setObjectName(colorName + QLatin1String(" Dock Widget"));
 
102
    setWindowTitle(objectName());
 
103
 
 
104
    QFrame *swatch = new ColorDock(colorName, this);
 
105
    swatch->setFrameStyle(QFrame::Box | QFrame::Sunken);
 
106
    swatch->setMinimumSize(125, 75);
 
107
 
 
108
    setWidget(swatch);
 
109
 
 
110
    closableAction = new QAction(tr("Closable"), this);
 
111
    closableAction->setCheckable(true);
 
112
    connect(closableAction, SIGNAL(triggered(bool)), SLOT(changeClosable(bool)));
 
113
 
 
114
    movableAction = new QAction(tr("Movable"), this);
 
115
    movableAction->setCheckable(true);
 
116
    connect(movableAction, SIGNAL(triggered(bool)), SLOT(changeMovable(bool)));
 
117
 
 
118
    floatableAction = new QAction(tr("Floatable"), this);
 
119
    floatableAction->setCheckable(true);
 
120
    connect(floatableAction, SIGNAL(triggered(bool)), SLOT(changeFloatable(bool)));
 
121
 
 
122
    floatingAction = new QAction(tr("Floating"), this);
 
123
    floatingAction->setCheckable(true);
 
124
    connect(floatingAction, SIGNAL(triggered(bool)), SLOT(changeFloating(bool)));
 
125
 
 
126
    allowedAreasActions = new QActionGroup(this);
 
127
    allowedAreasActions->setExclusive(false);
 
128
 
 
129
    allowLeftAction = new QAction(tr("Allow on Left"), this);
 
130
    allowLeftAction->setCheckable(true);
 
131
    connect(allowLeftAction, SIGNAL(triggered(bool)), SLOT(allowLeft(bool)));
 
132
 
 
133
    allowRightAction = new QAction(tr("Allow on Right"), this);
 
134
    allowRightAction->setCheckable(true);
 
135
    connect(allowRightAction, SIGNAL(triggered(bool)), SLOT(allowRight(bool)));
 
136
 
 
137
    allowTopAction = new QAction(tr("Allow on Top"), this);
 
138
    allowTopAction->setCheckable(true);
 
139
    connect(allowTopAction, SIGNAL(triggered(bool)), SLOT(allowTop(bool)));
 
140
 
 
141
    allowBottomAction = new QAction(tr("Allow on Bottom"), this);
 
142
    allowBottomAction->setCheckable(true);
 
143
    connect(allowBottomAction, SIGNAL(triggered(bool)), SLOT(allowBottom(bool)));
 
144
 
 
145
    allowedAreasActions->addAction(allowLeftAction);
 
146
    allowedAreasActions->addAction(allowRightAction);
 
147
    allowedAreasActions->addAction(allowTopAction);
 
148
    allowedAreasActions->addAction(allowBottomAction);
 
149
 
 
150
    areaActions = new QActionGroup(this);
 
151
    areaActions->setExclusive(true);
 
152
 
 
153
    leftAction = new QAction(tr("Place on Left") , this);
 
154
    leftAction->setCheckable(true);
 
155
    connect(leftAction, SIGNAL(triggered(bool)), SLOT(placeLeft(bool)));
 
156
 
 
157
    rightAction = new QAction(tr("Place on Right") , this);
 
158
    rightAction->setCheckable(true);
 
159
    connect(rightAction, SIGNAL(triggered(bool)), SLOT(placeRight(bool)));
 
160
 
 
161
    topAction = new QAction(tr("Place on Top") , this);
 
162
    topAction->setCheckable(true);
 
163
    connect(topAction, SIGNAL(triggered(bool)), SLOT(placeTop(bool)));
 
164
 
 
165
    bottomAction = new QAction(tr("Place on Bottom") , this);
 
166
    bottomAction->setCheckable(true);
 
167
    connect(bottomAction, SIGNAL(triggered(bool)), SLOT(placeBottom(bool)));
 
168
 
 
169
    areaActions->addAction(leftAction);
 
170
    areaActions->addAction(rightAction);
 
171
    areaActions->addAction(topAction);
 
172
    areaActions->addAction(bottomAction);
 
173
 
 
174
    connect(movableAction, SIGNAL(triggered(bool)), areaActions, SLOT(setEnabled(bool)));
 
175
 
 
176
    connect(movableAction, SIGNAL(triggered(bool)), allowedAreasActions, SLOT(setEnabled(bool)));
 
177
 
 
178
    connect(floatableAction, SIGNAL(triggered(bool)), floatingAction, SLOT(setEnabled(bool)));
 
179
 
 
180
    connect(floatingAction, SIGNAL(triggered(bool)), floatableAction, SLOT(setDisabled(bool)));
 
181
    connect(movableAction, SIGNAL(triggered(bool)), floatableAction, SLOT(setEnabled(bool)));
 
182
 
 
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());
 
194
 
 
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);
 
199
    }
 
200
}
 
201
 
 
202
void ColorSwatch::contextMenuEvent(QContextMenuEvent *event)
 
203
{
 
204
    event->accept();
 
205
    menu->exec(event->globalPos());
 
206
}
 
207
 
 
208
bool ColorSwatch::event(QEvent *e)
 
209
{
 
210
    if (e->type() != QEvent::Polish)
 
211
        return QDockWidget::event(e);
 
212
 
 
213
    QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
 
214
    const Qt::DockWidgetArea area = mainWindow->dockWidgetArea(this);
 
215
    const Qt::DockWidgetAreas areas = allowedAreas();
 
216
 
 
217
    closableAction->setChecked(features() & QDockWidget::DockWidgetClosable);
 
218
    if (windowType() == Qt::Drawer) {
 
219
        floatableAction->setEnabled(false);
 
220
        floatingAction->setEnabled(false);
 
221
        movableAction->setEnabled(false);
 
222
    } else {
 
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);
 
227
    }
 
228
 
 
229
    allowLeftAction->setChecked(isAreaAllowed(Qt::LeftDockWidgetArea));
 
230
    allowRightAction->setChecked(isAreaAllowed(Qt::RightDockWidgetArea));
 
231
    allowTopAction->setChecked(isAreaAllowed(Qt::TopDockWidgetArea));
 
232
    allowBottomAction->setChecked(isAreaAllowed(Qt::BottomDockWidgetArea));
 
233
 
 
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);
 
239
    }
 
240
 
 
241
    leftAction->blockSignals(true);
 
242
    rightAction->blockSignals(true);
 
243
    topAction->blockSignals(true);
 
244
    bottomAction->blockSignals(true);
 
245
 
 
246
    leftAction->setChecked(area == Qt::LeftDockWidgetArea);
 
247
    rightAction->setChecked(area == Qt::RightDockWidgetArea);
 
248
    topAction->setChecked(area == Qt::TopDockWidgetArea);
 
249
    bottomAction->setChecked(area == Qt::BottomDockWidgetArea);
 
250
 
 
251
    leftAction->blockSignals(false);
 
252
    rightAction->blockSignals(false);
 
253
    topAction->blockSignals(false);
 
254
    bottomAction->blockSignals(false);
 
255
 
 
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);
 
261
    }
 
262
    return QDockWidget::event(e);
 
263
}
 
264
 
 
265
void ColorSwatch::allow(Qt::DockWidgetArea area, bool a)
 
266
{
 
267
    Qt::DockWidgetAreas areas = allowedAreas();
 
268
    areas = a ? areas | area : areas & ~area;
 
269
    setAllowedAreas(areas);
 
270
 
 
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);
 
276
    }
 
277
}
 
278
 
 
279
void ColorSwatch::place(Qt::DockWidgetArea area, bool p)
 
280
{
 
281
    if (!p) return;
 
282
 
 
283
    QMainWindow *mainWindow = qobject_cast<QMainWindow *>(parentWidget());
 
284
    mainWindow->addDockWidget(area, this);
 
285
 
 
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);
 
291
    }
 
292
}
 
293
 
 
294
void ColorSwatch::changeClosable(bool on)
 
295
{ setFeatures(on ? features() | DockWidgetClosable : features() & ~DockWidgetClosable); }
 
296
 
 
297
void ColorSwatch::changeMovable(bool on)
 
298
{ setFeatures(on ? features() | DockWidgetMovable : features() & ~DockWidgetMovable); }
 
299
 
 
300
void ColorSwatch::changeFloatable(bool on)
 
301
{ setFeatures(on ? features() | DockWidgetFloatable : features() & ~DockWidgetFloatable); }
 
302
 
 
303
void ColorSwatch::changeFloating(bool floating)
 
304
{ setFloating(floating); }
 
305
 
 
306
void ColorSwatch::allowLeft(bool a)
 
307
{ allow(Qt::LeftDockWidgetArea, a); }
 
308
 
 
309
void ColorSwatch::allowRight(bool a)
 
310
{ allow(Qt::RightDockWidgetArea, a); }
 
311
 
 
312
void ColorSwatch::allowTop(bool a)
 
313
{ allow(Qt::TopDockWidgetArea, a); }
 
314
 
 
315
void ColorSwatch::allowBottom(bool a)
 
316
{ allow(Qt::BottomDockWidgetArea, a); }
 
317
 
 
318
void ColorSwatch::placeLeft(bool p)
 
319
{ place(Qt::LeftDockWidgetArea, p); }
 
320
 
 
321
void ColorSwatch::placeRight(bool p)
 
322
{ place(Qt::RightDockWidgetArea, p); }
 
323
 
 
324
void ColorSwatch::placeTop(bool p)
 
325
{ place(Qt::TopDockWidgetArea, p); }
 
326
 
 
327
void ColorSwatch::placeBottom(bool p)
 
328
{ place(Qt::BottomDockWidgetArea, p); }