1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.6: mydecoration.cpp Example File (qws/simpledecoration/mydecoration.cpp)</title>
7
<link href="classic.css" rel="stylesheet" type="text/css" />
10
<table border="0" cellpadding="0" cellspacing="0" width="100%">
12
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
13
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="functions.html"><font color="#004faf">All Functions</font></a> · <a href="overviews.html"><font color="#004faf">Overviews</font></a></td><td class="searchBar" align="right" valign="center"><form action="http://www.google.com/cse" id="cse-search-box"><div><input type="hidden" name="cx" value="000136343326384750312:dhbxnqlakyu" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" /></div></form></td>
14
</tr></table><h1 class="title">mydecoration.cpp Example File<br /><span class="small-subtitle">qws/simpledecoration/mydecoration.cpp</span>
16
<pre><span class="comment"> /****************************************************************************
18
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
19
** All rights reserved.
20
** Contact: Nokia Corporation (qt-info@nokia.com)
22
** This file is part of the examples of the Qt Toolkit.
24
** $QT_BEGIN_LICENSE:LGPL$
25
** No Commercial Usage
26
** This file contains pre-release code and may not be distributed.
27
** You may use this file in accordance with the terms and conditions
28
** contained in the Technology Preview License Agreement accompanying
31
** GNU Lesser General Public License Usage
32
** Alternatively, this file may be used under the terms of the GNU Lesser
33
** General Public License version 2.1 as published by the Free Software
34
** Foundation and appearing in the file LICENSE.LGPL included in the
35
** packaging of this file. Please review the following information to
36
** ensure the GNU Lesser General Public License version 2.1 requirements
37
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
39
** In addition, as a special exception, Nokia gives you certain additional
40
** rights. These rights are described in the Nokia Qt LGPL Exception
41
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
43
** If you have questions regarding the use of this file, please contact
44
** Nokia at qt-info@nokia.com.
55
****************************************************************************/</span>
57
#include <QtGui>
58
#include "mydecoration.h"
60
<span class="comment"> /* XPM */</span>
61
static const char * const _close_button[] = {
62
"16 16 3 1",
64
". c #ffafaf",
65
"x c #000000",
67
" xxxxxxxxxxxxxx ",
68
" x............x ",
69
" x.x........x.x ",
70
" x..x......x..x ",
71
" x...x....x...x ",
72
" x....x..x....x ",
73
" x.....xx.....x ",
74
" x.....xx.....x ",
75
" x....x..x....x ",
76
" x...x....x...x ",
77
" x..x......x..x ",
78
" x.x........x.x ",
79
" x............x ",
80
" xxxxxxxxxxxxxx ",
83
static const char * const _normalize_button[] = {
84
"16 16 4 1",
86
". c #dfdfff",
87
"o c #7f7f7f",
88
"x c #000000",
90
" xxxxxxxxxxxxxx ",
91
" xx...........x ",
92
" x.x..........x ",
93
" x..x..x......x ",
94
" x...xxx......x ",
95
" x...xxx......x ",
96
" x..xxxx......x ",
97
" x............x ",
98
" x.......xxxx.x ",
99
" x.......x..x.x ",
100
" x.......x..x.x ",
101
" x.......xxxx.x ",
102
" x............x ",
103
" xxxxxxxxxxxxxx ",
106
static const char * const _maximize_button[] = {
107
"16 16 4 1",
109
". c #dfdfff",
110
"o c #7f7f7f",
111
"x c #000000",
113
" xxxxxxxxxxxxxx ",
114
" x............x ",
115
" x.......xxxx.x ",
116
" x........xxx.x ",
117
" x........xxx.x ",
118
" x.......x..x.x ",
119
" x......x.....x ",
120
" x.....x......x ",
121
" x.oooo.......x ",
122
" x.o..o.......x ",
123
" x.o..o.......x ",
124
" x.oooo.......x ",
125
" x............x ",
126
" xxxxxxxxxxxxxx ",
129
static const char * const _help_button[] = {
130
"16 16 3 1",
132
". c #afffdf",
133
"x c #000000",
135
" xxxxxxxxxxxxxx ",
136
" x............x ",
137
" x....xxxx....x ",
138
" x..xx....xx..x ",
139
" x.xx......xx.x ",
140
" x.xx......xx.x ",
141
" x........xx..x ",
142
" x......xx....x ",
143
" x.....xx.....x ",
144
" x.....xx.....x ",
145
" x............x ",
146
" x.....xx.....x ",
147
" x............x ",
148
" xxxxxxxxxxxxxx ",
151
MyDecoration::MyDecoration()
152
: QDecorationDefault()
159
buttonHints << Qt::Window
160
<< Qt::WindowMaximizeButtonHint
161
<< Qt::WindowContextHelpButtonHint;
162
//! [constructor start]
164
//! [map window flags to decoration regions]
165
buttonHintMap[Qt::Window] = Close;
166
buttonHintMap[Qt::WindowMaximizeButtonHint] = Maximize;
167
buttonHintMap[Qt::WindowContextHelpButtonHint] = Help;
168
//! [map window flags to decoration regions]
170
//! [map decoration regions to pixmaps]
171
normalButtonPixmaps[Close] = QPixmap(_close_button);
172
normalButtonPixmaps[Maximize] = QPixmap(_maximize_button);
173
normalButtonPixmaps[Normalize] = QPixmap(_normalize_button);
174
normalButtonPixmaps[Help] = QPixmap(_help_button);
176
maximizedButtonPixmaps[Close] = QPixmap(_close_button);
177
maximizedButtonPixmaps[Maximize] = QPixmap(_normalize_button);
178
maximizedButtonPixmaps[Normalize] = QPixmap(_normalize_button);
179
maximizedButtonPixmaps[Help] = QPixmap(_help_button);
180
//! [map decoration regions to pixmaps]
182
//! [constructor end]
183
stateRegions << Close << Maximize << Help;
186
QRegion MyDecoration::region(const QWidget *widget, const QRect &insideRect,
187
int decorationRegion)
190
//! [calculate the positions of buttons based on the window flags used]
191
QHash<DecorationRegion, int> buttons;
192
Qt::WindowFlags flags = widget->windowFlags();
193
int dx = -buttonMargin - buttonWidth;
195
foreach (Qt::WindowType button, buttonHints) {
196
if (flags & button) {
197
int x = (buttons.size() + 1) * dx;
198
buttons[buttonHintMap[button]] = x;
201
//! [calculate the positions of buttons based on the window flags used]
203
//! [calculate the extent of the title]
204
int titleRightMargin = buttons.size() * dx;
206
QRect outsideRect(insideRect.left() - border,
207
insideRect.top() - titleHeight - border,
208
insideRect.width() + 2 * border,
209
insideRect.height() + titleHeight + 2 * border);
210
//! [calculate the extent of the title]
212
//! [check for all regions]
215
if (decorationRegion == All) {
216
region += QRegion(outsideRect) - QRegion(insideRect);
219
//! [check for all regions]
221
//! [compose a region based on the decorations specified]
222
if (decorationRegion & Title) {
223
QRect rect = outsideRect.adjusted(border, border, -border, 0);
224
rect.setHeight(titleHeight);
226
<span class="comment">// Adjust the width to accommodate buttons.</span>
227
rect.setWidth(qMax(0, rect.width() + titleRightMargin));
230
if (decorationRegion & Top) {
231
QRect rect = outsideRect.adjusted(border, 0, -border, 0);
232
rect.setHeight(border);
235
if (decorationRegion & Left) {
236
QRect rect = outsideRect.adjusted(0, border, 0, -border);
237
rect.setWidth(border);
240
if (decorationRegion & Right) {
241
QRect rect = outsideRect.adjusted(0, border, 0, -border);
242
rect.setLeft(rect.right() + 1 - border);
245
if (decorationRegion & Bottom) {
246
QRect rect = outsideRect.adjusted(border, 0, -border, 0);
247
rect.setTop(rect.bottom() + 1 - border);
250
if (decorationRegion & TopLeft) {
251
QRect rect = outsideRect;
252
rect.setWidth(border);
253
rect.setHeight(border);
256
if (decorationRegion & TopRight) {
257
QRect rect = outsideRect;
258
rect.setLeft(rect.right() + 1 - border);
259
rect.setHeight(border);
262
if (decorationRegion & BottomLeft) {
263
QRect rect = outsideRect;
264
rect.setWidth(border);
265
rect.setTop(rect.bottom() + 1 - border);
268
if (decorationRegion & BottomRight) {
269
QRect rect = outsideRect;
270
rect.setLeft(rect.right() + 1 - border);
271
rect.setTop(rect.bottom() + 1 - border);
274
//! [compose a region based on the decorations specified]
276
//! [add a region for each button only if it is present]
277
foreach (QDecoration::DecorationRegion testRegion, stateRegions) {
278
if (decorationRegion & testRegion and buttons.contains(testRegion)) {
279
<span class="comment">// Inside the title rectangle</span>
280
QRect rect = outsideRect.adjusted(border, border, -border, 0);
281
rect.setHeight(titleHeight);
283
dx = buttons[testRegion];
284
rect.setLeft(rect.right() + 1 + dx);
285
rect.setWidth(buttonWidth + buttonMargin);
289
//! [add a region for each button only if it is present]
295
bool MyDecoration::paint(QPainter *painter, const QWidget *widget,
296
int decorationRegion, DecorationState state)
298
if (decorationRegion == None)
302
//! [paint different regions]
303
bool handled = false;
305
QPalette palette = QApplication::palette();
306
QHash<DecorationRegion, QPixmap> buttonPixmaps;
308
if (widget->windowState() == Qt::WindowMaximized)
309
buttonPixmaps = maximizedButtonPixmaps;
311
buttonPixmaps = normalButtonPixmaps;
313
if (decorationRegion & Title) {
314
QRect rect = QDecoration::region(widget, Title).boundingRect();
315
painter->fillRect(rect, palette.brush(QPalette::Base));
317
painter->setPen(QPen(palette.color(QPalette::Text)));
318
painter->drawText(rect, Qt::AlignCenter, widget->windowTitle());
319
painter->restore();
322
if (decorationRegion & Top) {
323
QRect rect = QDecoration::region(widget, Top).boundingRect();
324
painter->fillRect(rect, palette.brush(QPalette::Dark));
327
if (decorationRegion & Left) {
328
QRect rect = QDecoration::region(widget, Left).boundingRect();
329
painter->fillRect(rect, palette.brush(QPalette::Dark));
332
if (decorationRegion & Right) {
333
QRect rect = QDecoration::region(widget, Right).boundingRect();
334
painter->fillRect(rect, palette.brush(QPalette::Dark));
337
if (decorationRegion & Bottom) {
338
QRect rect = QDecoration::region(widget, Bottom).boundingRect();
339
painter->fillRect(rect, palette.brush(QPalette::Dark));
342
if (decorationRegion & TopLeft) {
343
QRect rect = QDecoration::region(widget, TopLeft).boundingRect();
344
painter->fillRect(rect, palette.brush(QPalette::Dark));
347
if (decorationRegion & TopRight) {
348
QRect rect = QDecoration::region(widget, TopRight).boundingRect();
349
painter->fillRect(rect, palette.brush(QPalette::Dark));
352
if (decorationRegion & BottomLeft) {
353
QRect rect = QDecoration::region(widget, BottomLeft).boundingRect();
354
painter->fillRect(rect, palette.brush(QPalette::Dark));
357
if (decorationRegion & BottomRight) {
358
QRect rect = QDecoration::region(widget, BottomRight).boundingRect();
359
painter->fillRect(rect, palette.brush(QPalette::Dark));
362
//! [paint different regions]
365
int margin = (titleHeight - 16) / 2;
366
Qt::WindowFlags flags = widget->windowFlags();
368
foreach (DecorationRegion testRegion, stateRegions) {
369
if (decorationRegion & testRegion && flags & buttonHintMap.key(testRegion)) {
370
QRect rect = QDecoration::region(
371
widget, testRegion).boundingRect();
372
painter->fillRect(rect, palette.brush(QPalette::Base));
374
QRect buttonRect = rect.adjusted(0, margin, -buttonMargin - margin,
376
painter->drawPixmap(buttonRect.topLeft(), buttonPixmaps[testRegion]);
385
<p /><address><hr /><div align="center">
386
<table width="100%" cellspacing="0" border="0"><tr class="address">
387
<td width="40%" align="left">Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
388
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
389
<td width="40%" align="right"><div align="right">Qt 4.6.0</div></td>
390
<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script><script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script></tr></table></div></address></body>