source: trunk/doc/src/examples/charactermap.qdoc@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 11.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
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
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43\example widgets/charactermap
44\title Character Map Example
45
46The Character Map example shows how to create a custom widget that can
47both display its own content and respond to user input.
48
49The example displays an array of characters which the user can click on
50to enter text in a line edit. The contents of the line edit can then be
51copied into the clipboard, and pasted into other applications. The
52purpose behind this sort of tool is to allow users to enter characters
53that may be unavailable or difficult to locate on their keyboards.
54
55\image charactermap-example.png Screenshot of the Character Map example
56
57The example consists of the following classes:
58
59\list
60\i \c CharacterWidget displays the available characters in the current
61 font and style.
62\i \c MainWindow provides a standard main window that contains font and
63 style information, a view onto the characters, a line edit, and a push
64 button for submitting text to the clipboard.
65\endlist
66
67\section1 CharacterWidget Class Definition
68
69The \c CharacterWidget class is used to display an array of characters in
70a user-specified font and style. For flexibility, we subclass QWidget and
71reimplement only the functions that we need to provide basic rendering
72and interaction features.
73
74The class definition looks like this:
75
76\snippet examples/widgets/charactermap/characterwidget.h 0
77
78The widget does not contain any other widgets, so it must provide its own
79size hint to allow its contents to be displayed correctly.
80We reimplement \l{QWidget::paintEvent()} to draw custom content. We also
81reimplement \l{QWidget::mousePressEvent()} to allow the user to interact
82with the widget.
83
84The updateFont() and updateStyle() slots are used to update the font and
85style of the characters in the widget whenever the user changes the
86settings in the application.
87The class defines the characterSelected() signal so that other parts
88of the application are informed whenever the user selects a character in
89the widget.
90As a courtesy, the widget provides a tooltip that shows the current
91character value. We reimplement the \l{QWidget::mouseMoveEvent()} event
92handler and define showToolTip() to enable this feature.
93
94The \c columns, \c displayFont and \c currentKey private data members
95are used to record the number of columns to be shown, the current font,
96and the currently highlighted character in the widget.
97
98\section1 CharacterWidget Class Implementation
99
100Since the widget is to be used as a simple canvas, the constructor just
101calls the base class constructor and defines some default values for
102private data members.
103
104\snippet examples/widgets/charactermap/characterwidget.cpp 0
105
106We initialize \c currentKey with a value of -1 to indicate
107that no character is initially selected. We enable mouse tracking to
108allow us to follow the movement of the cursor across the widget.
109
110The class provides two functions to allow the font and style to be set up.
111Each of these modify the widget's display font and call update():
112
113\snippet examples/widgets/charactermap/characterwidget.cpp 1
114\codeline
115\snippet examples/widgets/charactermap/characterwidget.cpp 2
116
117We use a fixed size font for the display. Similarly, a fixed size hint is
118provided by the sizeHint() function:
119
120\snippet examples/widgets/charactermap/characterwidget.cpp 3
121
122Three standard event functions are implemented so that the widget
123can respond to clicks, provide tooltips, and render the available
124characters. The paintEvent() shows how the contents of the widget are
125arranged and displayed:
126
127\snippet examples/widgets/charactermap/characterwidget.cpp 6
128
129A QPainter is created for the widget and, in all cases, we ensure that the
130widget's background is painted. The painter's font is set to the
131user-specified display font.
132
133The area of the widget that needs to be redrawn is used to determine which
134characters need to be displayed:
135
136\snippet examples/widgets/charactermap/characterwidget.cpp 7
137
138Using integer division, we obtain the row and column numbers of each
139characters that should be displayed, and we draw a square on the widget
140for each character displayed.
141
142\snippet examples/widgets/charactermap/characterwidget.cpp 8
143\snippet examples/widgets/charactermap/characterwidget.cpp 9
144
145The symbols for each character in the array are drawn within each square,
146with the symbol for the most recently selected character displayed in red:
147
148\snippet examples/widgets/charactermap/characterwidget.cpp 10
149
150We do not need to take into account the difference between the area
151displayed in the viewport and the area we are drawing on because
152everything outside the visible area will be clipped.
153
154The mousePressEvent() defines how the widget responds to mouse clicks.
155
156\snippet examples/widgets/charactermap/characterwidget.cpp 5
157
158We are only interested when the user clicks with the left mouse button
159over the widget. When this happens, we calculate which character was
160selected and emit the characterSelected() signal.
161The character's number is found by dividing the x and y-coordinates of
162the click by the size of each character's grid square. Since the number
163of columns in the widget is defined by the \c columns variable, we
164simply multiply the row index by that value and add the column number
165to obtain the character number.
166
167If any other mouse button is pressed, the event is passed on to the
168QWidget base class. This ensures that the event can be handled properly
169by any other interested widgets.
170
171The mouseMoveEvent() maps the mouse cursor's position in global
172coordinates to widget coordinates, and determines the character that
173was clicked by performing the calculation
174
175\snippet examples/widgets/charactermap/characterwidget.cpp 4
176
177The tooltip is given a position defined in global coordinates.
178
179\section1 MainWindow Class Definition
180
181The \c MainWindow class provides a minimal user interface for the example,
182with only a constructor, slots that respond to signals emitted by standard
183widgets, and some convenience functions that are used to set up the user
184interface.
185
186The class definition looks like this:
187
188\snippet examples/widgets/charactermap/mainwindow.h 0
189
190The main window contains various widgets that are used to control how
191the characters will be displayed, and defines the findFonts() function
192for clarity and convenience. The findStyles() slot is used by the widgets
193to determine the styles that are available, insertCharacter() inserts
194a user-selected character into the window's line edit, and
195updateClipboard() synchronizes the clipboard with the contents of the
196line edit.
197
198\section1 MainWindow Class Implementation
199
200In the constructor, we set up the window's central widget and fill it with
201some standard widgets (two comboboxes, a line edit, and a push button).
202We also construct a CharacterWidget custom widget, and add a QScrollArea
203so that we can view its contents:
204
205\snippet examples/widgets/charactermap/mainwindow.cpp 0
206
207QScrollArea provides a viewport onto the \c CharacterWidget when we set
208its widget and handles much of the work needed to provide a scrolling
209viewport.
210
211The font combo box is automatically popuplated with a list of available
212fonts. We list the available styles for the current font in the style
213combobox using the following function:
214
215\snippet examples/widgets/charactermap/mainwindow.cpp 1
216
217The line edit and push button are used to supply text to the clipboard:
218
219\snippet examples/widgets/charactermap/mainwindow.cpp 2
220
221We also obtain a clipboard object so that we can send text entered by the
222user to other applications.
223
224Most of the signals emitted in the example come from standard widgets.
225We connect these signals to slots in this class, and to the slots provided
226by other widgets.
227
228\snippet examples/widgets/charactermap/mainwindow.cpp 4
229
230The font combobox's
231\l{QFontComboBox::currentFontChanged()}{currentFontChanged()} signal is
232connected to the findStyles() function so that the list of available styles
233can be shown for each font that is used. Since both the font and the style
234can be changed by the user, the font combobox's currentFontChanged() signal
235and the style combobox's
236\l{QComboBox::currentIndexChanged()}{currentIndexChanged()} are connected
237directly to the character widget.
238
239The final two connections allow characters to be selected in the character
240widget, and text to be inserted into the clipboard:
241
242\snippet examples/widgets/charactermap/mainwindow.cpp 5
243
244The character widget emits the characterSelected() custom signal when
245the user clicks on a character, and this is handled by the insertCharacter()
246function in this class. The clipboard is changed when the push button emits
247the clicked() signal, and we handle this with the updateClipboard() function.
248
249The remaining code in the constructor sets up the layout of the central widget,
250and provides a window title:
251
252\snippet examples/widgets/charactermap/mainwindow.cpp 6
253
254The font combobox is automatically populated with a list of available font
255families. The styles that can be used with each font are found by the
256findStyles() function. This function is called whenever the user selects a
257different font in the font combobox.
258
259\snippet examples/widgets/charactermap/mainwindow.cpp 7
260
261We begin by recording the currently selected style, and we clear the
262style combobox so that we can insert the styles associated with the
263current font family.
264
265\snippet examples/widgets/charactermap/mainwindow.cpp 8
266
267We use the font database to collect the styles that are available for the
268current font, and insert them into the style combobox. The current item is
269reset if the original style is not available for this font.
270
271The last two functions are slots that respond to signals from the character
272widget and the main window's push button. The insertCharacter() function is
273used to insert characters from the character widget when the user clicks a
274character:
275
276\snippet examples/widgets/charactermap/mainwindow.cpp 9
277
278The character is inserted into the line edit at the current cursor position.
279
280The main window's "To clipboard" push button is connected to the
281updateClipboard() function so that, when it is clicked, the clipboard is
282updated to contain the contents of the line edit:
283
284\snippet examples/widgets/charactermap/mainwindow.cpp 10
285
286We copy all the text from the line edit to the clipboard, but we do not clear
287the line edit.
288*/
Note: See TracBrowser for help on using the repository browser.