source: trunk/doc/src/examples/taskmenuextension.qdoc

Last change on this file 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: 19.2 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 designer/taskmenuextension
30 \title Task Menu Extension Example
31
32 The Task Menu Extension example shows how to create a custom
33 widget plugin for \l {Qt Designer Manual}{\QD}, and how to to use
34 the QDesignerTaskMenuExtension class to provide custom task menu
35 entries associated with the plugin.
36
37 \image taskmenuextension-example-faded.png
38
39 To provide a custom widget that can be used with \QD, we need to
40 supply a self-contained implementation. In this example we use a
41 custom widget designed to show the task menu extension feature:
42 The TicTacToe widget.
43
44 An extension is an object which modifies the behavior of \QD. The
45 QDesignerTaskMenuExtension can provide custom task menu entries
46 when a widget with this extension is selected.
47
48 There are four available types of extensions in \QD:
49
50 \list
51 \o QDesignerContainerExtension provides an extension that allows
52 you to add (and delete) pages to a multi-page container plugin
53 in \QD.
54 \o QDesignerMemberSheetExtension provides an extension that allows
55 you to manipulate a widget's member functions which is displayed
56 when configuring connections using Qt Designer's mode for editing
57 signals and slots.
58 \o QDesignerPropertySheetExtension provides an extension that
59 allows you to manipulate a widget's properties which is displayed
60 in Qt Designer's property editor.
61 \o QDesignerTaskMenuExtension provides an extension that allows
62 you to add custom menu entries to \QD's task menu.
63 \endlist
64
65 You can use all the extensions following the same pattern as in
66 this example, only replacing the respective extension base
67 class. For more information, see the \l {QtDesigner Module}.
68
69 The Task Menu Extension example consists of five classes:
70
71 \list
72 \o \c TicTacToe is a custom widget that lets the user play
73 the Tic-Tac-Toe game.
74 \o \c TicTacToePlugin exposes the \c TicTacToe class to \QD.
75 \o \c TicTacToeTaskMenuFactory creates a \c TicTacToeTaskMenu object.
76 \o \c TicTacToeTaskMenu provides the task menu extension, i.e the
77 plugin's associated task menu entries.
78 \o \c TicTacToeDialog lets the user modify the state of a
79 Tic-Tac-Toe plugin loaded into \QD.
80 \endlist
81
82 The project file for custom widget plugins needs some additional
83 information to ensure that they will work within \QD. For example,
84 custom widget plugins rely on components supplied with \QD, and
85 this must be specified in the project file that we use. We will
86 first take a look at the plugin's project file.
87
88 Then we will continue by reviewing the \c TicTacToePlugin class,
89 and take a look at the \c TicTacToeTaskMenuFactory and \c
90 TicTacToeTaskMenu classes. Finally, we will review the \c
91 TicTacToeDialog class before we take a quick look at the \c
92 TicTacToe widget's class definition.
93
94 \section1 The Project File: taskmenuextension.pro
95
96 The project file must contain some additional information to
97 ensure that the plugin will work as expected:
98
99 \snippet examples/designer/taskmenuextension/taskmenuextension.pro 0
100 \snippet examples/designer/taskmenuextension/taskmenuextension.pro 1
101
102 The \c TEMPLATE variable's value makes \c qmake create the custom
103 widget as a library. Later, we will ensure that the widget will be
104 recognized as a plugin by Qt by using the Q_EXPORT_PLUGIN2() macro to
105 export the relevant widget information.
106
107 The \c CONFIG variable contains two values, \c designer and \c
108 plugin:
109
110 \list
111 \o \c designer: Since custom widgets plugins rely on components
112 supplied with \QD, this value ensures that our plugin links against
113 \QD's library (\c libQtDesigner.so).
114
115 \o \c plugin: We also need to ensure that \c qmake considers the
116 custom widget a \e plugin library.
117 \endlist
118
119 When Qt is configured to build in both debug and release modes,
120 \QD will be built in release mode. When this occurs, it is
121 necessary to ensure that plugins are also built in release
122 mode. For that reason we add the \c debug_and_release value to
123 the \c CONFIG variable. Otherwise, if a plugin is built in a mode
124 that is incompatible with \QD, it won't be loaded and
125 installed.
126
127 The header and source files for the widget are declared in the
128 usual way:
129
130 \snippet examples/designer/taskmenuextension/taskmenuextension.pro 2
131
132 We provide an implementation of the plugin interface so that \QD
133 can use the custom widget. In this particular example we also
134 provide implementations of the task menu extension and the
135 extension factory as well as a dialog implementation.
136
137 It is important to ensure that the plugin is installed in a
138 location that is searched by \QD. We do this by specifying a
139 target path for the project and adding it to the list of items to
140 install:
141
142 \snippet doc/src/snippets/code/doc_src_examples_taskmenuextension.qdoc 0
143
144 The task menu extension is created as a library, and will be
145 installed alongside the other \QD plugins when the project is
146 installed (using \c{make install} or an equivalent installation
147 procedure).
148
149 Note that if you want the plugins to appear in a Visual Studio
150 integration, the plugins must be built in release mode and their
151 libraries must be copied into the plugin directory in the install
152 path of the integration (for an example, see \c {C:/program
153 files/trolltech as/visual studio integration/plugins}).
154
155 For more information about plugins, see the \l {How to Create Qt
156 Plugins} documentation.
157
158 \section1 TicTacToePlugin Class Definition
159
160 The \c TicTacToePlugin class exposes \c the TicTacToe class to
161 \QD. Its definition is equivalent to the \l
162 {designer/customwidgetplugin}{Custom Widget Plugin} example's
163 plugin class which is explained in detail. The only part of the
164 class definition that is specific to this particular custom widget
165 is the class name:
166
167 \snippet examples/designer/taskmenuextension/tictactoeplugin.h 0
168
169 The plugin class provides \QD with basic information about our
170 plugin, such as its class name and its include file. Furthermore
171 it knows how to create instances of the \c TicTacToe widget.
172 TicTacToePlugin also defines the \l
173 {QDesignerCustomWidgetInterface::initialize()}{initialize()}
174 function which is called after the plugin is loaded into \QD. The
175 function's QDesignerFormEditorInterface parameter provides the
176 plugin with a gateway to all of \QD's API's.
177
178 The \c TicTacToePlugin class inherits from both QObject and
179 QDesignerCustomWidgetInterface. It is important to remember, when
180 using multiple inheritance, to ensure that all the interfaces
181 (i.e. the classes that doesn't inherit Q_OBJECT) are made known to
182 the meta object system using the Q_INTERFACES() macro. This
183 enables \QD to use \l qobject_cast() to query for supported
184 interfaces using nothing but a QObject pointer.
185
186 \section1 TicTacToePlugin Class Implementation
187
188 The TicTacToePlugin class implementation is in most parts
189 equivalent to the \l {designer/customwidgetplugin}{Custom Widget
190 Plugin} example's plugin class:
191
192 \snippet examples/designer/taskmenuextension/tictactoeplugin.cpp 0
193
194 The only function that differs significantly is the initialize()
195 function:
196
197 \snippet examples/designer/taskmenuextension/tictactoeplugin.cpp 1
198
199 The \c initialize() function takes a QDesignerFormEditorInterface
200 object as argument. The QDesignerFormEditorInterface class
201 provides access to Qt Designer's components.
202
203 In \QD you can create two kinds of plugins: custom widget plugins
204 and tool plugins. QDesignerFormEditorInterface provides access to
205 all the \QD components that you normally need to create a tool
206 plugin: the extension manager, the object inspector, the property
207 editor and the widget box. Custom widget plugins have access to
208 the same components.
209
210 \snippet examples/designer/taskmenuextension/tictactoeplugin.cpp 2
211
212 When creating extensions associated with custom widget plugins, we
213 need to access \QD's current extension manager which we retrieve
214 from the QDesignerFormEditorInterface parameter.
215
216 \QD's QDesignerFormEditorInterface holds information about all Qt
217 Designer's components: The action editor, the object inspector,
218 the property editor, the widget box, and the extension and form
219 window managers.
220
221 The QExtensionManager class provides extension management
222 facilities for \QD. Using \QD's current extension manager you can
223 retrieve the extension for a given object. You can also register
224 and unregister an extension for a given object. Remember that an
225 extension is an object which modifies the behavior of \QD.
226
227 When registrering an extension, it is actually the associated
228 extension factory that is registered. In \QD, extension factories
229 are used to look up and create named extensions as they are
230 required. So, in this example, the task menu extension itself is
231 not created until a task menu is requested by the user.
232
233 \snippet examples/designer/taskmenuextension/tictactoeplugin.cpp 3
234
235 We create a \c TicTacToeTaskMenuFactory object that we register
236 using \QD's current \l {QExtensionManager}{extension manager}
237 retrieved from the QDesignerFormEditorInterface parameter. The
238 first argument is the newly created factory and the second
239 argument is an extension identifier which is a string. The \c
240 Q_TYPEID() macro simply converts the string into a QLatin1String.
241
242 The \c TicTacToeTaskMenuFactory class is a subclass of
243 QExtensionFactory. When the user request a task menu by clicking
244 the right mouse button over a widget with the specified task menu
245 extension, \QD's extension manager will run through all its
246 registered factories invoking the first one that is able to create
247 a task menu extension for the selected widget. This factory will
248 in turn create a \c TicTacToeTaskMenu object (the extension).
249
250 We omit to reimplement the
251 QDesignerCustomWidgetInterface::domXml() function (which include
252 default settings for the widget in the standard XML format used by
253 Qt Designer), since no default values are necessary.
254
255 \snippet examples/designer/taskmenuextension/tictactoeplugin.cpp 4
256
257 Finally, we use the Q_EXPORT_PLUGIN2() macro to export the
258 TicTacToePlugin class for use with Qt's plugin handling classes:
259 This macro ensures that \QD can access and construct the custom
260 widget. Without this macro, there is no way for \QD to use the
261 widget.
262
263 \section1 TicTacToeTaskMenuFactory Class Definition
264
265 The \c TicTacToeTaskMenuFactory class inherits QExtensionFactory
266 which provides a standard extension factory for \QD.
267
268 \snippet examples/designer/taskmenuextension/tictactoetaskmenu.h 1
269
270 The subclass's purpose is to reimplement the
271 QExtensionFactory::createExtension() function, making it able to
272 create a \c TicTacToe task menu extension.
273
274 \section1 TicTacToeTaskMenuFactory Class Implementation
275
276 The class constructor simply calls the QExtensionFactory base
277 class constructor:
278
279 \snippet examples/designer/taskmenuextension/tictactoetaskmenu.cpp 4
280
281 As described above, the factory is invoked when the user request a
282 task menu by clicking the right mouse button over a widget with
283 the specified task menu extension in \QD.
284
285 \QD's behavior is the same whether the requested extension is
286 associated with a container, a member sheet, a property sheet or a
287 task menu: Its extension manager runs through all its registered
288 extension factories calling \c createExtension() for each until
289 one responds by creating the requested extension.
290
291 \snippet examples/designer/taskmenuextension/tictactoetaskmenu.cpp 5
292
293 So the first thing we do in \c
294 TicTacToeTaskMenuFactory::createExtension() is to check if the
295 requested extension is a task menu extension. If it is, and the
296 widget requesting it is a \c TicTacToe widget, we create and
297 return a \c TicTacToeTaskMenu object. Otherwise, we simply return
298 a null pointer, allowing \QD's extension manager to continue its
299 search through the registered factories.
300
301
302 \section1 TicTacToeTaskMenu Class Definition
303
304 \image taskmenuextension-menu.png
305
306 The \c TicTacToeTaskMenu class inherits QDesignerTaskMenuExtension
307 which allows you to add custom entries (in the form of QActions)
308 to the task menu in \QD.
309
310 \snippet examples/designer/taskmenuextension/tictactoetaskmenu.h 0
311
312 We reimplement the \c preferredEditAction() and \c taskActions()
313 functions. Note that we implement a constructor that takes \e two
314 arguments: the parent widget, and the \c TicTacToe widget for
315 which the task menu is requested.
316
317 In addition we declare the private \c editState() slot, our custom
318 \c editStateAction and a private pointer to the \c TicTacToe
319 widget which state we want to modify.
320
321 \section1 TicTacToeTaskMenu Class Implementation
322
323 \snippet examples/designer/taskmenuextension/tictactoetaskmenu.cpp 0
324
325 In the constructor we first save the reference to the \c TicTacToe
326 widget sent as parameter, i.e the widget which state we want to
327 modify. We will need this later when our custom action is
328 invoked. We also create our custom \c editStateAction and connect
329 it to the \c editState() slot.
330
331 \snippet examples/designer/taskmenuextension/tictactoetaskmenu.cpp 1
332
333 The \c editState() slot is called whenever the user chooses the
334 \gui {Edit State...} option in a \c TicTacToe widget's task menu. The
335 slot creates a \c TicTacToeDialog presenting the current state of
336 the widget, and allowing the user to edit its state by playing the
337 game.
338
339 \snippet examples/designer/taskmenuextension/tictactoetaskmenu.cpp 2
340
341 We reimplement the \c preferredEditAction() function to return our
342 custom \c editStateAction as the action that should be invoked
343 when selecting a \c TicTacToe widget and pressing \key F2 .
344
345 \snippet examples/designer/taskmenuextension/tictactoetaskmenu.cpp 3
346
347 We reimplement the \c taskActions() function to return a list of
348 our custom actions making these appear on top of the default menu
349 entries in a \c TicTacToe widget's task menu.
350
351 \section1 TicTacToeDialog Class Definition
352
353 \image taskmenuextension-dialog.png
354
355 The \c TicTacToeDialog class inherits QDialog. The dialog lets the
356 user modify the state of the currently selected Tic-Tac-Toe
357 plugin.
358
359 \snippet examples/designer/taskmenuextension/tictactoedialog.h 0
360
361 We reimplement the \c sizeHint() function. We also declare two
362 private slots: \c resetState() and \c saveState(). In addition to
363 the dialog's buttons and layouts we declare two \c TicTacToe
364 pointers, one to the widget the user can interact with and the
365 other to the original custom widget plugin which state the user
366 wants to edit.
367
368 \section1 TicTacToeDialog Class Implementation
369
370 \snippet examples/designer/taskmenuextension/tictactoedialog.cpp 0
371
372 In the constructor we first save the reference to the TicTacToe
373 widget sent as parameter, i.e the widget which state the user want
374 to modify. Then we create a new \c TicTacToe widget, and set its
375 state to be equivalent to the parameter widget's state.
376
377 Finally, we create the dialog's buttons and layout.
378
379 \snippet examples/designer/taskmenuextension/tictactoedialog.cpp 1
380
381 We reimplement the \c sizeHint() function to ensure that the
382 dialog is given a reasonable size.
383
384 \snippet examples/designer/taskmenuextension/tictactoedialog.cpp 2
385
386 The \c resetState() slot is called whenever the user press the
387 \gui Reset button. The only thing we do is to call the \c
388 clearBoard() function for the editor widget, i.e. the \c TicTacToe
389 widget we created in the dialog's constructor.
390
391 \snippet examples/designer/taskmenuextension/tictactoedialog.cpp 3
392
393 The \c saveState() slot is called whenever the user press the \gui
394 OK button, and transfers the state of the editor widget to the
395 widget which state we want to modify. In order to make the change
396 of state visible to \QD we need to set the latter widget's state
397 property using the QDesignerFormWindowInterface class.
398
399 QDesignerFormWindowInterface provides you with information about
400 the associated form window as well as allowing you to alter its
401 properties. The interface is not intended to be instantiated
402 directly, but to provide access to Qt Designer's current form
403 windows controlled by Qt Designer's form window manager.
404
405 If you are looking for the form window containing a specific
406 widget, you can use the static
407 QDesignerFormWindowInterface::findFormWindow() function:
408
409 \snippet examples/designer/taskmenuextension/tictactoedialog.cpp 4
410
411 After retrieving the form window of the widget (which state we
412 want to modify), we use the QDesignerFormWindowInterface::cursor()
413 function to retrieve the form window's cursor.
414
415 The QDesignerFormWindowCursorInterface class provides an interface
416 to the form window's text cursor. Once we have cursor, we can
417 finally set the state property using the
418 QDesignerFormWindowCursorInterface::setProperty() function.
419
420 \snippet examples/designer/taskmenuextension/tictactoedialog.cpp 5
421
422 In the end we call the QEvent::accept() function which sets the
423 accept flag of the event object. Setting the \c accept parameter
424 indicates that the event receiver wants the event. Unwanted events
425 might be propagated to the parent widget.
426
427 \section1 TicTacToe Class Definition
428
429 The \c TicTacToe class is a custom widget that lets the user play
430 the Tic-Tac-Toe game.
431
432 \snippet examples/designer/taskmenuextension/tictactoe.h 0
433
434 The main details to observe in the \c TicTacToe class defintion is
435 the declaration of the \c state property and its \c state() and \c
436 setState() functions.
437
438 We need to declare the \c TicTacToe widget's state as a property
439 to make it visible to \QD; allowing \QD to manage it in the same
440 way it manages the properties the \c TicTacToe widget inherits
441 from QWidget and QObject, for example featuring the property
442 editor.
443*/
Note: See TracBrowser for help on using the repository browser.