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

« back to all changes in this revision

Viewing changes to doc/html/widgets-charactermap.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

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
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/examples/charactermap.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Character Map Example</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<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="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Character Map Example</h1>
 
21
<p>Files:</p>
 
22
<ul>
 
23
<li><a href="widgets-charactermap-characterwidget-cpp.html">widgets/charactermap/characterwidget.cpp</a></li>
 
24
<li><a href="widgets-charactermap-characterwidget-h.html">widgets/charactermap/characterwidget.h</a></li>
 
25
<li><a href="widgets-charactermap-mainwindow-cpp.html">widgets/charactermap/mainwindow.cpp</a></li>
 
26
<li><a href="widgets-charactermap-mainwindow-h.html">widgets/charactermap/mainwindow.h</a></li>
 
27
<li><a href="widgets-charactermap-main-cpp.html">widgets/charactermap/main.cpp</a></li>
 
28
</ul>
 
29
<p>The Character Map example shows how to create a custom widget that can both display its own content and respond to user input.</p>
 
30
<p>The example displays an array of characters which the user can click on to enter text in a line edit. The contents of the line edit can then be copied into the clipboard, and pasted into other applications. The purpose behind this sort of tool is to allow users to enter characters that may be unavailable or difficult to locate on their keyboards.</p>
 
31
<center><img src="images/charactermap-example.png" alt="Screenshot of the Character Map example" /></center><p>The example consists of the following classes:</p>
 
32
<ul>
 
33
<li><tt>CharacterWidget</tt> displays the available characters in the current font and style.</li>
 
34
<li><tt>MainWindow</tt> provides a standard main window that contains font and style information, a view onto the characters, a line edit, and a push button for submitting text to the clipboard.</li>
 
35
</ul>
 
36
<a name="characterwidget-class-definition"></a>
 
37
<h2>CharacterWidget Class Definition</h2>
 
38
<p>The <tt>CharacterWidget</tt> class is used to display an array of characters in a user-specified font and style. For flexibility, we subclass <a href="qwidget.html">QWidget</a> and reimplement only the functions that we need to provide basic rendering and interaction features.</p>
 
39
<p>The class definition looks like this:</p>
 
40
<pre>&nbsp;   class CharacterWidget : public QWidget
 
41
    {
 
42
        Q_OBJECT
 
43
 
 
44
    public:
 
45
        CharacterWidget(QWidget *parent = 0);
 
46
        QSize sizeHint() const;
 
47
 
 
48
    public slots:
 
49
        void updateFont(const QString &amp;fontFamily);
 
50
        void updateStyle(const QString &amp;fontStyle);
 
51
 
 
52
    signals:
 
53
        void characterSelected(const QString &amp;character);
 
54
 
 
55
    protected:
 
56
        void mouseMoveEvent(QMouseEvent *event);
 
57
        void mousePressEvent(QMouseEvent *event);
 
58
        void paintEvent(QPaintEvent *event);
 
59
 
 
60
    private:
 
61
        QFont displayFont;
 
62
        int lastKey;
 
63
    };</pre>
 
64
<p>The widget does not contain any other widgets, so it must provide its own size hint to allow its contents to be displayed correctly. We reimplement <a href="qwidget.html#paintEvent">QWidget::paintEvent</a>() to draw custom content. We also reimplement <a href="qwidget.html#mousePressEvent">QWidget::mousePressEvent</a>() to allow the user to interact with the widget.</p>
 
65
<p>The updateFont() and updateStyle() slots are used to update the font and style of the characters in the widget whenever the user changes the settings in the application. The class defines the characterSelected() signal so that other parts of the application are informed whenever the user selects a character in the widget. As a courtesy, the widget provides a tooltip that shows the current character value. We reimplement the <a href="qwidget.html#mouseMoveEvent">QWidget::mouseMoveEvent</a>() event handler and define showToolTip() to enable this feature.</p>
 
66
<p>The <tt>displayFont</tt> and <tt>currentKey</tt> private data members are used to record the current font and the currently highlighted character in the widget.</p>
 
67
<a name="characterwidget-class-implementation"></a>
 
68
<h2>CharacterWidget Class Implementation</h2>
 
69
<p>Since the widget is to be used as a simple canvas, the constructor just calls the base class constructor and defines some default values for private data members.</p>
 
70
<pre>&nbsp;   CharacterWidget::CharacterWidget(QWidget *parent)
 
71
        : QWidget(parent)
 
72
    {
 
73
        lastKey = -1;
 
74
        setMouseTracking(true);
 
75
    }</pre>
 
76
<p>We initialize <tt>currentKey</tt> with a value of -1 to indicate that no character is initially selected. We enable mouse tracking to allow us to follow the movement of the cursor across the widget.</p>
 
77
<p>The class provides two functions to allow the font and style to be set up. Each of these modify the widget's display font and call update():</p>
 
78
<pre>&nbsp;   void CharacterWidget::updateFont(const QString &amp;fontFamily)
 
79
    {
 
80
        displayFont.setFamily(fontFamily);
 
81
        displayFont.setPixelSize(16);
 
82
        update();
 
83
    }
 
84
 
 
85
    void CharacterWidget::updateStyle(const QString &amp;fontStyle)
 
86
    {
 
87
        QFontDatabase fontDatabase;
 
88
        displayFont = fontDatabase.font(displayFont.family(), fontStyle, 12);
 
89
        displayFont.setPixelSize(16);
 
90
        update();
 
91
    }</pre>
 
92
<p>We use a fixed size font for the display. Similarly, a fixed size hint is provided by the sizeHint() function:</p>
 
93
<pre>&nbsp;   QSize CharacterWidget::sizeHint() const
 
94
    {
 
95
        return QSize(32*24, (65536/32)*24);
 
96
    }</pre>
 
97
<p>Three standard event functions are implemented so that the widget can respond to clicks, provide tooltips, and render the available characters. The paintEvent() shows how the contents of the widget are arranged and displayed:</p>
 
98
<pre>&nbsp;   void CharacterWidget::paintEvent(QPaintEvent *event)
 
99
    {
 
100
        QPainter painter(this);
 
101
        painter.fillRect(event-&gt;rect(), QBrush(Qt::white));
 
102
        painter.setFont(displayFont);</pre>
 
103
<p>A <a href="qpainter.html">QPainter</a> is created for the widget and, in all cases, we ensure that the widget's background is painted. The painter's font is set to the user-specified display font.</p>
 
104
<p>The area of the widget that needs to be redrawn is used to determine which characters need to be displayed:</p>
 
105
<pre>&nbsp;       QRect redrawRect = event-&gt;rect();
 
106
        int beginRow = redrawRect.top()/24;
 
107
        int endRow = redrawRect.bottom()/24;
 
108
        int beginColumn = redrawRect.left()/24;
 
109
        int endColumn = redrawRect.right()/24;</pre>
 
110
<p>Using integer division, we obtain the row and column numbers of each characters that should be displayed, and we draw a square on the widget for each character displayed.</p>
 
111
<pre>&nbsp;       painter.setPen(QPen(Qt::gray));
 
112
        for (int row = beginRow; row &lt;= endRow; ++row) {
 
113
            for (int column = beginColumn; column &lt;= endColumn; ++column) {
 
114
                painter.drawRect(column*24, row*24, 24, 24);
 
115
            }
 
116
        }</pre>
 
117
<p>The symbols for each character in the array are drawn within each square, with the symbol for the most recently selected character displayed in red:</p>
 
118
<pre>&nbsp;       QFontMetrics fontMetrics(displayFont);
 
119
        painter.setPen(QPen(Qt::black));
 
120
        for (int row = beginRow; row &lt;= endRow; ++row) {
 
121
 
 
122
            for (int column = beginColumn; column &lt;= endColumn; ++column) {
 
123
 
 
124
                int key = row*32 + column;
 
125
                painter.setClipRect(column*24, row*24, 24, 24);
 
126
 
 
127
                if (key == lastKey)
 
128
                    painter.fillRect(column*24, row*24, 24, 24, QBrush(Qt::red));
 
129
 
 
130
                painter.drawText(column*24 + 12 - fontMetrics.width(QChar(key))/2,
 
131
                                 row*24 + 4 + fontMetrics.ascent(),
 
132
                                 QString(QChar(key)));
 
133
            }
 
134
        }
 
135
    }</pre>
 
136
<p>We do not need to take into account the difference between the area displayed in the viewport and the area we are drawing on because everything outside the visible area will be clipped.</p>
 
137
<p>The mousePressEvent() defines how the widget responds to mouse clicks.</p>
 
138
<pre>&nbsp;   void CharacterWidget::mousePressEvent(QMouseEvent *event)
 
139
    {
 
140
        if (event-&gt;button() == Qt::LeftButton) {
 
141
            lastKey = (event-&gt;y()/24)*32 + event-&gt;x()/24;
 
142
            if (QChar(lastKey).category() != QChar::NoCategory)
 
143
                emit characterSelected(QString(QChar(lastKey)));
 
144
            update();
 
145
        }
 
146
        else
 
147
            QWidget::mousePressEvent(event);
 
148
    }</pre>
 
149
<p>We are only interested when the user clicks with the left mouse button over the widget. When this happens, we calculate which character was selected and emit the characterSelected() signal. The character's number is found by dividing the x and y-coordinates of the click by the size of each character's grid square. Since there are 32 of these to a row, we simply multiply the row index by 32 and add the column number to obtain the character number.</p>
 
150
<p>If any other mouse button is pressed, the event is passed on to the <a href="qwidget.html">QWidget</a> base class. This ensures that the event can be handled properly by any other interested widgets.</p>
 
151
<p>The mouseMoveEvent() maps the mouse cursor's position in global coordinates to widget coordinates, and determines the character that was clicked by performing the calculation</p>
 
152
<pre>&nbsp;   void CharacterWidget::mouseMoveEvent(QMouseEvent *event)
 
153
    {
 
154
        QPoint widgetPosition = mapFromGlobal(event-&gt;globalPos());
 
155
        int key = (widgetPosition.y()/24)*32 + widgetPosition.x()/24;
 
156
        QToolTip::showText(event-&gt;globalPos(), QString::number(key), this);
 
157
    }</pre>
 
158
<p>The tooltip is given a position defined in global coordinates.</p>
 
159
<a name="mainwindow-class-definition"></a>
 
160
<h2>MainWindow Class Definition</h2>
 
161
<p>The <tt>MainWindow</tt> class provides a minimal user interface for the example, with only a constructor, slots that respond to signals emitted by standard widgets, and some convenience functions that are used to set up the user interface.</p>
 
162
<p>The class definition looks like this:</p>
 
163
<pre>&nbsp;   class MainWindow : public QMainWindow
 
164
    {
 
165
        Q_OBJECT
 
166
 
 
167
    public:
 
168
        MainWindow();
 
169
 
 
170
    public slots:
 
171
        void findStyles();
 
172
        void insertCharacter(const QString &amp;character);
 
173
        void updateClipboard();
 
174
 
 
175
    private:
 
176
        void findFonts();
 
177
 
 
178
        CharacterWidget *characterWidget;
 
179
        QClipboard *clipboard;
 
180
        QComboBox *fontCombo;
 
181
        QComboBox *styleCombo;
 
182
        QLineEdit *lineEdit;
 
183
        QScrollArea *scrollArea;
 
184
    };</pre>
 
185
<p>The main window contains various widgets that are used to control how the characters will be displayed, and defines the findFonts() function for clarity and convenience. The findStyles() slot is used by the widgets to determine the styles that are available, insertCharacter() inserts a user-selected character into the window's line edit, and updateClipboard() synchronizes the clipboard with the contents of the line edit.</p>
 
186
<a name="mainwindow-class-implementation"></a>
 
187
<h2>MainWindow Class Implementation</h2>
 
188
<p>In the constructor, we set up the window's central widget and fill it with some standard widgets (two comboboxes, a line edit, and a push button). We also construct a CharacterWidget custom widget, and add a <a href="qscrollarea.html">QScrollArea</a> so that we can view its contents:</p>
 
189
<pre>&nbsp;   MainWindow::MainWindow()
 
190
    {
 
191
        QWidget *centralWidget = new QWidget;
 
192
 
 
193
        QLabel *fontLabel = new QLabel(tr(&quot;Font:&quot;));
 
194
        fontCombo = new QComboBox;
 
195
        QLabel *styleLabel = new QLabel(tr(&quot;Style:&quot;));
 
196
        styleCombo = new QComboBox;
 
197
 
 
198
        scrollArea = new QScrollArea;
 
199
        characterWidget = new CharacterWidget;
 
200
        scrollArea-&gt;setWidget(characterWidget);</pre>
 
201
<p><a href="qscrollarea.html">QScrollArea</a> provides a viewport onto the <tt>CharacterWidget</tt> when we set its widget and handles much of the work needed to provide a scrolling viewport.</p>
 
202
<p>We list the available fonts and styles in the comboboxes using the following functions:</p>
 
203
<pre>&nbsp;       findFonts();
 
204
        findStyles();</pre>
 
205
<p>The line edit and push button are used to supply text to the clipboard:</p>
 
206
<pre>&nbsp;       lineEdit = new QLineEdit;
 
207
        QPushButton *clipboardButton = new QPushButton(tr(&quot;&amp;To clipboard&quot;));</pre>
 
208
<p>We also obtain a clipboard object so that we can send text entered by the user to other applications.</p>
 
209
<p>Most of the signals emitted in the example come from standard widgets. We connect these signals to slots in this class, and to the slots provided by other widgets.</p>
 
210
<pre>&nbsp;       connect(fontCombo, SIGNAL(activated(const QString &amp;)),
 
211
                this, SLOT(findStyles()));
 
212
        connect(fontCombo, SIGNAL(activated(const QString &amp;)),
 
213
                characterWidget, SLOT(updateFont(const QString &amp;)));
 
214
        connect(styleCombo, SIGNAL(activated(const QString &amp;)),
 
215
                characterWidget, SLOT(updateStyle(const QString &amp;)));</pre>
 
216
<p>The font combobox's activated() signal is connected to the findStyles() function so that the list of available styles can be shown for each font that is used. Since both the font and the style can be changed by the user, the activated() signals from both the font and style comboboxes are connected directly to the character widget:</p>
 
217
<pre>&nbsp;       connect(characterWidget, SIGNAL(characterSelected(const QString &amp;)),
 
218
                this, SLOT(insertCharacter(const QString &amp;)));
 
219
        connect(clipboardButton, SIGNAL(clicked()), this, SLOT(updateClipboard()));</pre>
 
220
<p>The final two connections allow characters to be selected in the character widget, and text to be inserted into the clipboard. The character widget emits the characterSelected() custom signal when the user clicks on a character, and this is handled by the insertCharacter() function in this class. The clipboard is changed when the push button emits the clicked() signal, and we handle this with the updateClipboard() function.</p>
 
221
<p>The remaining code in the constructor sets up the layout of the central widget, and provides a window title:</p>
 
222
<pre>&nbsp;       QHBoxLayout *controlsLayout = new QHBoxLayout;
 
223
        controlsLayout-&gt;addWidget(fontLabel);
 
224
        controlsLayout-&gt;addWidget(fontCombo, 1);
 
225
        controlsLayout-&gt;addWidget(styleLabel);
 
226
        controlsLayout-&gt;addWidget(styleCombo, 1);
 
227
        controlsLayout-&gt;addStretch(1);
 
228
 
 
229
        QHBoxLayout *lineLayout = new QHBoxLayout;
 
230
        lineLayout-&gt;addWidget(lineEdit, 1);
 
231
        lineLayout-&gt;addSpacing(12);
 
232
        lineLayout-&gt;addWidget(clipboardButton);
 
233
 
 
234
        QVBoxLayout *centralLayout = new QVBoxLayout;
 
235
        centralLayout-&gt;addLayout(controlsLayout);
 
236
        centralLayout-&gt;addWidget(scrollArea, 1);
 
237
        centralLayout-&gt;addSpacing(4);
 
238
        centralLayout-&gt;addLayout(lineLayout);
 
239
        centralWidget-&gt;setLayout(centralLayout);
 
240
 
 
241
        setCentralWidget(centralWidget);
 
242
        setWindowTitle(tr(&quot;Character Map&quot;));
 
243
    }</pre>
 
244
<p>We implement two functions to set up the fonts because the display font family and style can be set independently. findFonts() uses a font database to provide items in the font combobox:</p>
 
245
<pre>&nbsp;   void MainWindow::findFonts()
 
246
    {
 
247
        QFontDatabase fontDatabase;
 
248
        fontCombo-&gt;clear();
 
249
 
 
250
        QString family;
 
251
        foreach (family, fontDatabase.families())
 
252
            fontCombo-&gt;addItem(family);
 
253
    }</pre>
 
254
<p>In this function, we clear the combobox, but only add entries for the available font families. The styles that can be used with each font are found by the findStyles() function. This function is called whenever the user selects a different font in the font combobox.</p>
 
255
<pre>&nbsp;   void MainWindow::findStyles()
 
256
    {
 
257
        QFontDatabase fontDatabase;
 
258
        QString currentItem = styleCombo-&gt;currentText();
 
259
        styleCombo-&gt;clear();</pre>
 
260
<p>We begin by recording the currently selected style, and we clear the style combobox so that we can insert the styles associated with the current font family.</p>
 
261
<pre>&nbsp;       QString style;
 
262
        foreach (style, fontDatabase.styles(fontCombo-&gt;currentText()))
 
263
            styleCombo-&gt;addItem(style);
 
264
 
 
265
        if (styleCombo-&gt;findText(currentItem) == -1)
 
266
            styleCombo-&gt;setCurrentIndex(0);
 
267
    }</pre>
 
268
<p>We use the font database to collect the styles that are available for the current font, and insert them into the style combobox. The current item is reset if the original style is not available for this font.</p>
 
269
<p>The last two functions are slots that respond to signals from the character widget and the main window's push button. The insertCharacter() function is used to insert characters from the character widget when the user clicks a character:</p>
 
270
<pre>&nbsp;   void MainWindow::insertCharacter(const QString &amp;character)
 
271
    {
 
272
        lineEdit-&gt;insert(character);
 
273
    }</pre>
 
274
<p>The character is inserted into the line edit at the current cursor position.</p>
 
275
<p>The main window's &quot;To clipboard&quot; push button is connected to the updateClipboard() function so that, when it is clicked, the clipboard is updated to contain the contents of the line edit:</p>
 
276
<pre>&nbsp;   void MainWindow::updateClipboard()
 
277
    {
 
278
        clipboard-&gt;setText(lineEdit-&gt;text(), QClipboard::Clipboard);
 
279
        clipboard-&gt;setText(lineEdit-&gt;text(), QClipboard::Selection);
 
280
    }</pre>
 
281
<p>We copy all the text from the line edit to the clipboard, but we do not clear the line edit.</p>
 
282
<p /><address><hr /><div align="center">
 
283
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
284
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
285
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
286
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
287
</tr></table></div></address></body>
 
288
</html>