[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[556] | 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
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[556] | 15 | **
|
---|
[846] | 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.
|
---|
[556] | 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 tools/inputpanel
|
---|
| 30 | \title Input Panel Example
|
---|
| 31 |
|
---|
| 32 | The Input Panel example shows how to create an input panel that
|
---|
| 33 | can be used to input text into widgets using only the pointer and
|
---|
| 34 | no keyboard.
|
---|
| 35 |
|
---|
| 36 | \image inputpanel-example.png
|
---|
| 37 |
|
---|
| 38 | The input fields in the main window have no function other than
|
---|
| 39 | to accept input. The main focus is on how the extra input panel
|
---|
| 40 | can be used to input text without the need for a real keyboard or
|
---|
| 41 | keypad.
|
---|
| 42 |
|
---|
| 43 | \section1 Main Form Class Definition
|
---|
| 44 |
|
---|
| 45 | Because the main window has no other function than to accept
|
---|
| 46 | input, it has no class definition. Instead, its whole layout is
|
---|
| 47 | made in Qt Designer. This emphasizes the point that no widget
|
---|
| 48 | specific code is needed to use input panels with Qt.
|
---|
| 49 |
|
---|
| 50 | \section1 MyInputPanelContext Class Definition
|
---|
| 51 |
|
---|
| 52 | \snippet examples/tools/inputpanel/myinputpanelcontext.h 0
|
---|
| 53 |
|
---|
| 54 | The \c MyInputPanelContext class inherits QInputContext, which is
|
---|
| 55 | Qt's base class for handling input methods.
|
---|
| 56 | \c MyInputPanelContext is responsible for managing the state of
|
---|
| 57 | the input panel and sending input method events to the receiving
|
---|
| 58 | widgets.
|
---|
| 59 |
|
---|
| 60 | The \c inputPanel member is a pointer to the input panel widget
|
---|
| 61 | itself; in other words, the window that will display the buttons
|
---|
| 62 | used for input.
|
---|
| 63 |
|
---|
| 64 | The \c identifierName(), \c language(), \c isComposing() and
|
---|
| 65 | \c reset() functions are there mainly to fill in the pure virtual
|
---|
| 66 | functions in the base class, QInputContext, but they can be
|
---|
| 67 | useful in other scenarios. The important functions and slots are
|
---|
| 68 | the following:
|
---|
| 69 |
|
---|
| 70 | \list
|
---|
| 71 | \o \c filterEvent() is where we receive events telling us to open
|
---|
| 72 | or close the input panel.
|
---|
| 73 | \o \c sendCharacter() is a slot which is called when we want to
|
---|
| 74 | send a character to the focused widget.
|
---|
| 75 | \o \c updatePosition() is used to position the input panel
|
---|
| 76 | relative to the focused widget, and will be used when opening
|
---|
| 77 | the input panel.
|
---|
| 78 | \endlist
|
---|
| 79 |
|
---|
| 80 | \section1 MyInputPanelContext Class Implementation
|
---|
| 81 |
|
---|
| 82 | In the constructor we connect to the \c characterGenerated()
|
---|
| 83 | signal of the input panel, in order to receive key presses. We'll
|
---|
| 84 | see how it works in detail later on.
|
---|
| 85 |
|
---|
| 86 | \snippet examples/tools/inputpanel/myinputpanelcontext.cpp 0
|
---|
| 87 |
|
---|
| 88 | In the \c filterEvent() function, we must look for the two event
|
---|
| 89 | types: \c RequestSoftwareInputPanel and \c CloseSoftwareInputPanel.
|
---|
| 90 |
|
---|
| 91 | \snippet examples/tools/inputpanel/myinputpanelcontext.cpp 1
|
---|
| 92 |
|
---|
| 93 | The first type will be sent whenever
|
---|
| 94 | an input capable widget wants to ask for an input panel. Qt's
|
---|
| 95 | input widgets do this automatically. If we receive that type of
|
---|
| 96 | event, we call \c updatePosition() \mdash we'll see later on what it
|
---|
| 97 | does \mdash then show the actual input panel widget. If we receive
|
---|
| 98 | the \c CloseSoftwareInputPanel event, we do the opposite, and
|
---|
| 99 | hide the input panel.
|
---|
| 100 |
|
---|
| 101 | \snippet examples/tools/inputpanel/myinputpanelcontext.cpp 2
|
---|
| 102 |
|
---|
| 103 | We implement the \c sendCharacter() function so that it sends the
|
---|
| 104 | supplied character to the focused widget. All QInputContext based
|
---|
| 105 | classes are always supposed to send events to the widget returned
|
---|
| 106 | by QInputContext::focusWidget(). Note the QPointer guards to make
|
---|
| 107 | sure that the widget does not get destroyed in between events.
|
---|
| 108 |
|
---|
| 109 | Also note that we chose to use key press events in this example.
|
---|
| 110 | For more complex use cases with composed text it might be more
|
---|
| 111 | appropriate to send QInputMethodEvent events.
|
---|
| 112 |
|
---|
| 113 | The \c updatePosition() function is implemented to position the
|
---|
| 114 | actual input panel window directly below the focused widget.
|
---|
| 115 |
|
---|
| 116 | \snippet examples/tools/inputpanel/myinputpanelcontext.cpp 3
|
---|
| 117 |
|
---|
| 118 | It performs the positioning by obtaining the coordinates of the
|
---|
| 119 | focused widget and translating them to global coordinates.
|
---|
| 120 |
|
---|
| 121 | \section1 MyInputPanel Class Definition
|
---|
| 122 |
|
---|
| 123 | The \c MyInputPanel class inherits QWidget and is used to display
|
---|
| 124 | the input panel widget and its buttons.
|
---|
| 125 |
|
---|
| 126 | \snippet examples/tools/inputpanel/myinputpanel.h 0
|
---|
| 127 |
|
---|
| 128 | If we look at the member variables first, we see that there is
|
---|
| 129 | \c form, which is made with Qt Designer, that contains the layout
|
---|
| 130 | of buttons to click. Note that all the buttons in the layout have
|
---|
| 131 | been declared with the \c NoFocus focus policy so that we can
|
---|
| 132 | maintain focus on the window receiving input instead of the
|
---|
| 133 | window containing buttons.
|
---|
| 134 |
|
---|
| 135 | The \c lastFocusedWidget is a helper variable, which also aids in
|
---|
| 136 | maintaining focus.
|
---|
| 137 |
|
---|
| 138 | \c signalMapper is an instance of the QSignalMapper class and is
|
---|
| 139 | there to help us tell which button was clicked. Since they are
|
---|
| 140 | all very similar this is a better solution than creating a separate
|
---|
| 141 | slot for each one.
|
---|
| 142 |
|
---|
| 143 | The functions that we implement in \c MyInputPanel are the
|
---|
| 144 | following:
|
---|
| 145 |
|
---|
| 146 | \list
|
---|
| 147 | \o \c event() is used to intercept and manipulate focus events,
|
---|
| 148 | so we can maintain focus in the main window.
|
---|
| 149 | \o \c saveFocusWidget() is a slot which will be called whenever
|
---|
| 150 | focus changes, and allows us to store the newly focused widget
|
---|
| 151 | in \c lastFocusedWidget, so that its focus can be restored
|
---|
| 152 | if it loses it to the input panel.
|
---|
| 153 | \o \c buttonClicked() is a slot which will be called by the
|
---|
| 154 | \c signalMapper whenever it receives a \c clicked() signal
|
---|
| 155 | from any of the buttons.
|
---|
| 156 | \endlist
|
---|
| 157 |
|
---|
| 158 | \section1 MyInputPanel Class Implementation
|
---|
| 159 |
|
---|
| 160 | If we look at the constructor first, we have a lot of signals to
|
---|
| 161 | connect to!
|
---|
| 162 |
|
---|
| 163 | We connect the QApplication::focusChanged() signal
|
---|
| 164 | to the \c saveFocusWidget() signal in order to get focus updates.
|
---|
| 165 | Then comes the interesting part with the signal mapper: the
|
---|
| 166 | series of \c setMapping() calls sets the mapper up so that each
|
---|
| 167 | signal from one of the buttons will result in a
|
---|
| 168 | QSignalMapper::mapped() signal, with the given widget as a
|
---|
| 169 | parameter. This allows us to do general processing of clicks.
|
---|
| 170 |
|
---|
| 171 | \snippet examples/tools/inputpanel/myinputpanel.cpp 0
|
---|
| 172 |
|
---|
| 173 | The next series of connections then connect each button's
|
---|
| 174 | \c clicked() signal to the signal mapper. Finally, we create
|
---|
| 175 | a connection from the \c mapped() signal to the
|
---|
| 176 | \c buttonClicked() slot, where we will handle it.
|
---|
| 177 |
|
---|
| 178 | \snippet examples/tools/inputpanel/myinputpanel.cpp 3
|
---|
| 179 |
|
---|
| 180 | In the \c buttonClicked() slot, we extract the value of the
|
---|
| 181 | "buttonValue" property. This is a custom property which was
|
---|
| 182 | created in Qt Designer and set to the character that we wish the
|
---|
| 183 | button to produce. Then we emit the \c characterGenerated()
|
---|
| 184 | signal, which \c MyInputPanelContext is connected to. This will
|
---|
| 185 | in turn cause it to send the input to the focused widget.
|
---|
| 186 |
|
---|
| 187 | In the \c saveFocusWidget() slot, we test whether the newly
|
---|
| 188 | focused widget is a child of the input panel or not, using the
|
---|
| 189 | QWidget::isAncestorOf() call.
|
---|
| 190 |
|
---|
| 191 | \snippet examples/tools/inputpanel/myinputpanel.cpp 2
|
---|
| 192 |
|
---|
| 193 | If it isn't, it means that the widget is outside the input panel,
|
---|
| 194 | and we store a pointer to that widget for later.
|
---|
| 195 |
|
---|
| 196 | In the \c event() function we handle QEvent::WindowActivate
|
---|
| 197 | event, which occurs if the focus switches to the input panel.
|
---|
| 198 |
|
---|
| 199 | \snippet examples/tools/inputpanel/myinputpanel.cpp 1
|
---|
| 200 |
|
---|
| 201 | Since we want avoid focus on the input panel, we immediately call
|
---|
| 202 | QWidget::activateWindow() on the widget that last had focus, so
|
---|
| 203 | that input into that widget can continue. We ignore any other events
|
---|
| 204 | that we receive.
|
---|
| 205 |
|
---|
| 206 | \section1 Setting the Input Context
|
---|
| 207 |
|
---|
| 208 | The main function for the example is very similar to those for other
|
---|
| 209 | examples. The only real difference is that it creates a
|
---|
| 210 | \c MyInputPanelContext and sets it as the application-wide input
|
---|
| 211 | context.
|
---|
| 212 |
|
---|
| 213 | \snippet examples/tools/inputpanel/main.cpp main
|
---|
| 214 |
|
---|
| 215 | With the input context in place, we set up and show the user interface
|
---|
| 216 | made in Qt Designer before running the event loop.
|
---|
| 217 |
|
---|
| 218 | \section1 Further Reading
|
---|
| 219 |
|
---|
| 220 | This example shows a specific kind of input context that uses interaction
|
---|
| 221 | with a widget to provide input for another. Qt's input context system can
|
---|
| 222 | also be used to create other kinds of input methods. We recommend starting
|
---|
| 223 | with the QInputContext documentation if you want to explore further.
|
---|
| 224 | */
|
---|