1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example draganddrop/fridgemagnets
|
---|
30 | \title Fridge Magnets Example
|
---|
31 |
|
---|
32 | The Fridge Magnets example shows how to supply more than one type
|
---|
33 | of MIME-encoded data with a drag and drop operation.
|
---|
34 |
|
---|
35 | \image fridgemagnets-example.png
|
---|
36 |
|
---|
37 | With this application the user can play around with a collection
|
---|
38 | of fridge magnets, using drag and drop to form new sentences from
|
---|
39 | the words on the magnets. The example consists of two classes:
|
---|
40 |
|
---|
41 | \list
|
---|
42 | \o \c DragLabel is a custom widget representing one
|
---|
43 | single fridge magnet.
|
---|
44 | \o \c DragWidget provides the main application window.
|
---|
45 | \endlist
|
---|
46 |
|
---|
47 | We will first take a look at the \c DragLabel class, then we will
|
---|
48 | examine the \c DragWidget class.
|
---|
49 |
|
---|
50 | \section1 DragLabel Class Definition
|
---|
51 |
|
---|
52 | Each fridge magnet is represented by an instance of the \c
|
---|
53 | DragLabel class:
|
---|
54 |
|
---|
55 | \snippet examples/draganddrop/fridgemagnets/draglabel.h 0
|
---|
56 |
|
---|
57 | Each instance of this QLabel subclass will be used to display an
|
---|
58 | pixmap generated from a text string. Since we cannot store both
|
---|
59 | text and a pixmap in a standard label, we declare a private variable
|
---|
60 | to hold the original text, and we define an additional member
|
---|
61 | function to allow it to be accessed.
|
---|
62 |
|
---|
63 | \section1 DragLabel Class Implementation
|
---|
64 |
|
---|
65 | In the \c DragLabel constructor, we first create a QImage object
|
---|
66 | on which we will draw the fridge magnet's text and frame:
|
---|
67 |
|
---|
68 | \snippet examples/draganddrop/fridgemagnets/draglabel.cpp 0
|
---|
69 |
|
---|
70 | Its size depends on the current font size, and its format is
|
---|
71 | QImage::Format_ARGB32_Premultiplied; i.e., the image is stored
|
---|
72 | using a premultiplied 32-bit ARGB format (0xAARRGGBB).
|
---|
73 |
|
---|
74 | We then construct a font object that uses the application's
|
---|
75 | default font, and set its style strategy. The style strategy tells
|
---|
76 | the font matching algorithm what type of fonts should be used to
|
---|
77 | find an appropriate default family. The QFont::ForceOutline forces
|
---|
78 | the use of outline fonts.
|
---|
79 |
|
---|
80 | To draw the text and frame onto the image, we use the QPainter
|
---|
81 | class. QPainter provides highly optimized methods to do most of
|
---|
82 | the drawing GUI programs require. It can draw everything from
|
---|
83 | simple lines to complex shapes like pies and chords. It can also
|
---|
84 | draw aligned text and pixmaps.
|
---|
85 |
|
---|
86 | \snippet examples/draganddrop/fridgemagnets/draglabel.cpp 1
|
---|
87 |
|
---|
88 | A painter can be activated by passing a paint device to the
|
---|
89 | constructor, or by using the \l{QPainter::}{begin()} method as we
|
---|
90 | do in this example. The \l{QPainter::}{end()} method deactivates
|
---|
91 | it. Note that the latter function is called automatically upon
|
---|
92 | destruction when the painter is actived by its constructor. The
|
---|
93 | QPainter::Antialiasing render hint ensures that the paint engine
|
---|
|
---|