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

« back to all changes in this revision

Viewing changes to doc/html/qws-simpledecoration-mydecoration-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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">
 
5
<head>
 
6
  <title>Qt 4.6: mydecoration.cpp Example File (qws/simpledecoration/mydecoration.cpp)</title>
 
7
  <link href="classic.css" rel="stylesheet" type="text/css" />
 
8
</head>
 
9
<body>
 
10
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
11
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <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>
 
15
</h1>
 
16
<pre><span class="comment"> /****************************************************************************
 
17
 **
 
18
 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 
19
 ** All rights reserved.
 
20
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 
21
 **
 
22
 ** This file is part of the examples of the Qt Toolkit.
 
23
 **
 
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
 
29
 ** this package.
 
30
 **
 
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.
 
38
 **
 
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.
 
42
 **
 
43
 ** If you have questions regarding the use of this file, please contact
 
44
 ** Nokia at qt-info@nokia.com.
 
45
 **
 
46
 **
 
47
 **
 
48
 **
 
49
 **
 
50
 **
 
51
 **
 
52
 **
 
53
 ** $QT_END_LICENSE$
 
54
 **
 
55
 ****************************************************************************/</span>
 
56
 
 
57
 #include &lt;QtGui&gt;
 
58
 #include &quot;mydecoration.h&quot;
 
59
 
 
60
<span class="comment"> /* XPM */</span>
 
61
 static const char * const _close_button[] = {
 
62
 &quot;16 16 3 1&quot;,
 
63
 &quot;  c none&quot;,
 
64
 &quot;. c #ffafaf&quot;,
 
65
 &quot;x c #000000&quot;,
 
66
 &quot;                &quot;,
 
67
 &quot; xxxxxxxxxxxxxx &quot;,
 
68
 &quot; x............x &quot;,
 
69
 &quot; x.x........x.x &quot;,
 
70
 &quot; x..x......x..x &quot;,
 
71
 &quot; x...x....x...x &quot;,
 
72
 &quot; x....x..x....x &quot;,
 
73
 &quot; x.....xx.....x &quot;,
 
74
 &quot; x.....xx.....x &quot;,
 
75
 &quot; x....x..x....x &quot;,
 
76
 &quot; x...x....x...x &quot;,
 
77
 &quot; x..x......x..x &quot;,
 
78
 &quot; x.x........x.x &quot;,
 
79
 &quot; x............x &quot;,
 
80
 &quot; xxxxxxxxxxxxxx &quot;,
 
81
 &quot;                &quot;};
 
82
 
 
83
 static const char * const _normalize_button[] = {
 
84
 &quot;16 16 4 1&quot;,
 
85
 &quot;  c none&quot;,
 
86
 &quot;. c #dfdfff&quot;,
 
87
 &quot;o c #7f7f7f&quot;,
 
88
 &quot;x c #000000&quot;,
 
89
 &quot;                &quot;,
 
90
 &quot; xxxxxxxxxxxxxx &quot;,
 
91
 &quot; xx...........x &quot;,
 
92
 &quot; x.x..........x &quot;,
 
93
 &quot; x..x..x......x &quot;,
 
94
 &quot; x...xxx......x &quot;,
 
95
 &quot; x...xxx......x &quot;,
 
96
 &quot; x..xxxx......x &quot;,
 
97
 &quot; x............x &quot;,
 
98
 &quot; x.......xxxx.x &quot;,
 
99
 &quot; x.......x..x.x &quot;,
 
100
 &quot; x.......x..x.x &quot;,
 
101
 &quot; x.......xxxx.x &quot;,
 
102
 &quot; x............x &quot;,
 
103
 &quot; xxxxxxxxxxxxxx &quot;,
 
104
 &quot;                &quot;};
 
105
 
 
106
 static const char * const _maximize_button[] = {
 
107
 &quot;16 16 4 1&quot;,
 
108
 &quot;  c none&quot;,
 
109
 &quot;. c #dfdfff&quot;,
 
110
 &quot;o c #7f7f7f&quot;,
 
111
 &quot;x c #000000&quot;,
 
112
 &quot;                &quot;,
 
113
 &quot; xxxxxxxxxxxxxx &quot;,
 
114
 &quot; x............x &quot;,
 
115
 &quot; x.......xxxx.x &quot;,
 
116
 &quot; x........xxx.x &quot;,
 
117
 &quot; x........xxx.x &quot;,
 
118
 &quot; x.......x..x.x &quot;,
 
119
 &quot; x......x.....x &quot;,
 
120
 &quot; x.....x......x &quot;,
 
121
 &quot; x.oooo.......x &quot;,
 
122
 &quot; x.o..o.......x &quot;,
 
123
 &quot; x.o..o.......x &quot;,
 
124
 &quot; x.oooo.......x &quot;,
 
125
 &quot; x............x &quot;,
 
126
 &quot; xxxxxxxxxxxxxx &quot;,
 
127
 &quot;                &quot;};
 
128
 
 
129
 static const char * const _help_button[] = {
 
130
 &quot;16 16 3 1&quot;,
 
131
 &quot;  c none&quot;,
 
132
 &quot;. c #afffdf&quot;,
 
133
 &quot;x c #000000&quot;,
 
134
 &quot;                &quot;,
 
135
 &quot; xxxxxxxxxxxxxx &quot;,
 
136
 &quot; x............x &quot;,
 
137
 &quot; x....xxxx....x &quot;,
 
138
 &quot; x..xx....xx..x &quot;,
 
139
 &quot; x.xx......xx.x &quot;,
 
140
 &quot; x.xx......xx.x &quot;,
 
141
 &quot; x........xx..x &quot;,
 
142
 &quot; x......xx....x &quot;,
 
143
 &quot; x.....xx.....x &quot;,
 
144
 &quot; x.....xx.....x &quot;,
 
145
 &quot; x............x &quot;,
 
146
 &quot; x.....xx.....x &quot;,
 
147
 &quot; x............x &quot;,
 
148
 &quot; xxxxxxxxxxxxxx &quot;,
 
149
 &quot;                &quot;};
 
150
 
 
151
 MyDecoration::MyDecoration()
 
152
     : QDecorationDefault()
 
153
 {
 
154
     border = 4;
 
155
     titleHeight = 24;
 
156
     buttonWidth = 20;
 
157
     buttonHeight = 20;
 
158
     buttonMargin = 2;
 
159
     buttonHints &lt;&lt; Qt::Window
 
160
                 &lt;&lt; Qt::WindowMaximizeButtonHint
 
161
                 &lt;&lt; Qt::WindowContextHelpButtonHint;
 
162
     //! [constructor start]
 
163
 
 
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]
 
169
 
 
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);
 
175
 
 
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]
 
181
 
 
182
     //! [constructor end]
 
183
     stateRegions &lt;&lt; Close &lt;&lt; Maximize &lt;&lt; Help;
 
184
 }
 
185
 
 
186
 QRegion MyDecoration::region(const QWidget *widget, const QRect &amp;insideRect,
 
187
                              int decorationRegion)
 
188
 {
 
189
     //! [region start]
 
190
     //! [calculate the positions of buttons based on the window flags used]
 
191
     QHash&lt;DecorationRegion, int&gt; buttons;
 
192
     Qt::WindowFlags flags = widget-&gt;windowFlags();
 
193
     int dx = -buttonMargin - buttonWidth;
 
194
 
 
195
     foreach (Qt::WindowType button, buttonHints) {
 
196
         if (flags &amp; button) {
 
197
             int x = (buttons.size() + 1) * dx;
 
198
             buttons[buttonHintMap[button]] = x;
 
199
         }
 
200
     }
 
201
     //! [calculate the positions of buttons based on the window flags used]
 
202
 
 
203
     //! [calculate the extent of the title]
 
204
     int titleRightMargin = buttons.size() * dx;
 
205
 
 
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]
 
211
 
 
212
     //! [check for all regions]
 
213
     QRegion region;
 
214
 
 
215
     if (decorationRegion == All) {
 
216
         region += QRegion(outsideRect) - QRegion(insideRect);
 
217
         return region;
 
218
     }
 
219
     //! [check for all regions]
 
220
 
 
221
     //! [compose a region based on the decorations specified]
 
222
     if (decorationRegion &amp; Title) {
 
223
         QRect rect = outsideRect.adjusted(border, border, -border, 0);
 
224
         rect.setHeight(titleHeight);
 
225
 
 
226
         <span class="comment">// Adjust the width to accommodate buttons.</span>
 
227
         rect.setWidth(qMax(0, rect.width() + titleRightMargin));
 
228
         region += rect;
 
229
     }
 
230
     if (decorationRegion &amp; Top) {
 
231
         QRect rect = outsideRect.adjusted(border, 0, -border, 0);
 
232
         rect.setHeight(border);
 
233
         region += rect;
 
234
     }
 
235
     if (decorationRegion &amp; Left) {
 
236
         QRect rect = outsideRect.adjusted(0, border, 0, -border);
 
237
         rect.setWidth(border);
 
238
         region += rect;
 
239
     }
 
240
     if (decorationRegion &amp; Right) {
 
241
         QRect rect = outsideRect.adjusted(0, border, 0, -border);
 
242
         rect.setLeft(rect.right() + 1 - border);
 
243
         region += rect;
 
244
     }
 
245
     if (decorationRegion &amp; Bottom) {
 
246
         QRect rect = outsideRect.adjusted(border, 0, -border, 0);
 
247
         rect.setTop(rect.bottom() + 1 - border);
 
248
         region += rect;
 
249
     }
 
250
     if (decorationRegion &amp; TopLeft) {
 
251
         QRect rect = outsideRect;
 
252
         rect.setWidth(border);
 
253
         rect.setHeight(border);
 
254
         region += rect;
 
255
     }
 
256
     if (decorationRegion &amp; TopRight) {
 
257
         QRect rect = outsideRect;
 
258
         rect.setLeft(rect.right() + 1 - border);
 
259
         rect.setHeight(border);
 
260
         region += rect;
 
261
     }
 
262
     if (decorationRegion &amp; BottomLeft) {
 
263
         QRect rect = outsideRect;
 
264
         rect.setWidth(border);
 
265
         rect.setTop(rect.bottom() + 1 - border);
 
266
         region += rect;
 
267
     }
 
268
     if (decorationRegion &amp; BottomRight) {
 
269
         QRect rect = outsideRect;
 
270
         rect.setLeft(rect.right() + 1 - border);
 
271
         rect.setTop(rect.bottom() + 1 - border);
 
272
         region += rect;
 
273
     }
 
274
     //! [compose a region based on the decorations specified]
 
275
 
 
276
     //! [add a region for each button only if it is present]
 
277
     foreach (QDecoration::DecorationRegion testRegion, stateRegions) {
 
278
         if (decorationRegion &amp; 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);
 
282
 
 
283
             dx = buttons[testRegion];
 
284
             rect.setLeft(rect.right() + 1 + dx);
 
285
             rect.setWidth(buttonWidth + buttonMargin);
 
286
             region += rect;
 
287
         }
 
288
     }
 
289
     //! [add a region for each button only if it is present]
 
290
 
 
291
     //! [region end]
 
292
     return region;
 
293
 }
 
294
 
 
295
 bool MyDecoration::paint(QPainter *painter, const QWidget *widget,
 
296
                          int decorationRegion, DecorationState state)
 
297
 {
 
298
     if (decorationRegion == None)
 
299
         return false;
 
300
     //! [paint start]
 
301
 
 
302
     //! [paint different regions]
 
303
     bool handled = false;
 
304
 
 
305
     QPalette palette = QApplication::palette();
 
306
     QHash&lt;DecorationRegion, QPixmap&gt; buttonPixmaps;
 
307
 
 
308
     if (widget-&gt;windowState() == Qt::WindowMaximized)
 
309
         buttonPixmaps = maximizedButtonPixmaps;
 
310
     else
 
311
         buttonPixmaps = normalButtonPixmaps;
 
312
 
 
313
     if (decorationRegion &amp; Title) {
 
314
         QRect rect = QDecoration::region(widget, Title).boundingRect();
 
315
         painter-&gt;fillRect(rect, palette.brush(QPalette::Base));
 
316
         painter-&gt;save();
 
317
         painter-&gt;setPen(QPen(palette.color(QPalette::Text)));
 
318
         painter-&gt;drawText(rect, Qt::AlignCenter, widget-&gt;windowTitle());
 
319
         painter-&gt;restore();
 
320
         handled = true;
 
321
     }
 
322
     if (decorationRegion &amp; Top) {
 
323
         QRect rect = QDecoration::region(widget, Top).boundingRect();
 
324
         painter-&gt;fillRect(rect, palette.brush(QPalette::Dark));
 
325
         handled = true;
 
326
     }
 
327
     if (decorationRegion &amp; Left) {
 
328
         QRect rect = QDecoration::region(widget, Left).boundingRect();
 
329
         painter-&gt;fillRect(rect, palette.brush(QPalette::Dark));
 
330
         handled = true;
 
331
     }
 
332
     if (decorationRegion &amp; Right) {
 
333
         QRect rect = QDecoration::region(widget, Right).boundingRect();
 
334
         painter-&gt;fillRect(rect, palette.brush(QPalette::Dark));
 
335
         handled = true;
 
336
     }
 
337
     if (decorationRegion &amp; Bottom) {
 
338
         QRect rect = QDecoration::region(widget, Bottom).boundingRect();
 
339
         painter-&gt;fillRect(rect, palette.brush(QPalette::Dark));
 
340
         handled = true;
 
341
     }
 
342
     if (decorationRegion &amp; TopLeft) {
 
343
         QRect rect = QDecoration::region(widget, TopLeft).boundingRect();
 
344
         painter-&gt;fillRect(rect, palette.brush(QPalette::Dark));
 
345
         handled = true;
 
346
     }
 
347
     if (decorationRegion &amp; TopRight) {
 
348
         QRect rect = QDecoration::region(widget, TopRight).boundingRect();
 
349
         painter-&gt;fillRect(rect, palette.brush(QPalette::Dark));
 
350
         handled = true;
 
351
     }
 
352
     if (decorationRegion &amp; BottomLeft) {
 
353
         QRect rect = QDecoration::region(widget, BottomLeft).boundingRect();
 
354
         painter-&gt;fillRect(rect, palette.brush(QPalette::Dark));
 
355
         handled = true;
 
356
     }
 
357
     if (decorationRegion &amp; BottomRight) {
 
358
         QRect rect = QDecoration::region(widget, BottomRight).boundingRect();
 
359
         painter-&gt;fillRect(rect, palette.brush(QPalette::Dark));
 
360
         handled = true;
 
361
     }
 
362
     //! [paint different regions]
 
363
 
 
364
     //! [paint buttons]
 
365
     int margin = (titleHeight - 16) / 2;
 
366
     Qt::WindowFlags flags = widget-&gt;windowFlags();
 
367
 
 
368
     foreach (DecorationRegion testRegion, stateRegions) {
 
369
         if (decorationRegion &amp; testRegion &amp;&amp; flags &amp; buttonHintMap.key(testRegion)) {
 
370
             QRect rect = QDecoration::region(
 
371
                 widget, testRegion).boundingRect();
 
372
             painter-&gt;fillRect(rect, palette.brush(QPalette::Base));
 
373
 
 
374
             QRect buttonRect = rect.adjusted(0, margin, -buttonMargin - margin,
 
375
                                              -buttonMargin);
 
376
             painter-&gt;drawPixmap(buttonRect.topLeft(), buttonPixmaps[testRegion]);
 
377
             handled = true;
 
378
         }
 
379
     }
 
380
     //! [paint buttons]
 
381
 
 
382
     //! [paint end]
 
383
     return handled;
 
384
 }</pre>
 
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 &copy; 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>
 
391
</html>