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

Last change on this file since 1147 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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