source: trunk/src/gui/kernel/qwhatsthis.cpp@ 846

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

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

File size: 22.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module 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 [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qwhatsthis.h"
43#ifndef QT_NO_WHATSTHIS
44#include "qpointer.h"
45#include "qapplication.h"
46#include "qdesktopwidget.h"
47#include "qevent.h"
48#include "qpixmap.h"
49#include "qpainter.h"
50#include "qtimer.h"
51#include "qhash.h"
52#include "qaction.h"
53#include "qcursor.h"
54#include "qbitmap.h"
55#include "qtextdocument.h"
56#include "../text/qtextdocumentlayout_p.h"
57#include "qtoolbutton.h"
58#include "qdebug.h"
59#ifndef QT_NO_ACCESSIBILITY
60#include "qaccessible.h"
61#endif
62#if defined(Q_WS_WIN)
63#include "qt_windows.h"
64#ifndef SPI_GETDROPSHADOW
65#define SPI_GETDROPSHADOW 0x1024
66#endif
67#endif
68#if defined(Q_WS_X11)
69#include "qx11info_x11.h"
70#include <qwidget.h>
71#endif
72
73QT_BEGIN_NAMESPACE
74
75/*!
76 \class QWhatsThis
77 \brief The QWhatsThis class provides a simple description of any
78 widget, i.e. answering the question "What's This?".
79
80 \ingroup helpsystem
81
82
83 "What's This?" help is part of an application's online help
84 system, and provides users with information about the
85 functionality and usage of a particular widget. "What's This?"
86 help texts are typically longer and more detailed than \link
87 QToolTip tooltips\endlink, but generally provide less information
88 than that supplied by separate help windows.
89
90 QWhatsThis provides a single window with an explanatory text that
91 pops up when the user asks "What's This?". The default way for
92 users to ask the question is to move the focus to the relevant
93 widget and press Shift+F1. The help text appears immediately; it
94 goes away as soon as the user does something else.
95 (Note that if there is a shortcut for Shift+F1, this mechanism
96 will not work.) Some dialogs provide a "?" button that users can
97 click to enter "What's This?" mode; they then click the relevant
98 widget to pop up the "What's This?" window. It is also possible to
99 provide a a menu option or toolbar button to switch into "What's
100 This?" mode.
101
102 To add "What's This?" text to a widget or an action, you simply
103 call QWidget::setWhatsThis() or QAction::setWhatsThis().
104
105 The text can be either rich text or plain text. If you specify a
106 rich text formatted string, it will be rendered using the default
107 stylesheet, making it possible to embed images in the displayed
108 text. To be as fast as possible, the default stylesheet uses a
109 simple method to determine whether the text can be rendered as
110 plain text. See Qt::mightBeRichText() for details.
111
112 \snippet doc/src/snippets/whatsthis/whatsthis.cpp 0
113
114 An alternative way to enter "What's This?" mode is to call
115 createAction(), and add the returned QAction to either a menu or
116 a tool bar. By invoking this context help action (in the picture
117 below, the button with the arrow and question mark icon) the user
118 switches into "What's This?" mode. If they now click on a widget
119 the appropriate help text is shown. The mode is left when help is
120 given or when the user presses Esc.
121
122 \img whatsthis.png
123
124 You can enter "What's This?" mode programmatically with
125 enterWhatsThisMode(), check the mode with inWhatsThisMode(), and
126 return to normal mode with leaveWhatsThisMode().
127
128 If you want to control the "What's This?" behavior of a widget
129 manually see Qt::WA_CustomWhatsThis.
130
131 It is also possible to show different help texts for different
132 regions of a widget, by using a QHelpEvent of type
133 QEvent::WhatsThis. Intercept the help event in your widget's
134 QWidget::event() function and call QWhatsThis::showText() with the
135 text you want to display for the position specified in
136 QHelpEvent::pos(). If the text is rich text and the user clicks
137 on a link, the widget also receives a QWhatsThisClickedEvent with
138 the link's reference as QWhatsThisClickedEvent::href(). If a
139 QWhatsThisClickedEvent is handled (i.e. QWidget::event() returns
140 true), the help window remains visible. Call
141 QWhatsThis::hideText() to hide it explicitly.
142
143 \sa QToolTip
144*/
145
146Q_CORE_EXPORT void qDeleteInEventHandler(QObject *o);
147
148class QWhatsThat : public QWidget
149{
150 Q_OBJECT
151
152public:
153 QWhatsThat(const QString& txt, QWidget* parent, QWidget *showTextFor);
154 ~QWhatsThat() ;
155
156 static QWhatsThat *instance;
157
158protected:
159 void showEvent(QShowEvent *e);
160 void mousePressEvent(QMouseEvent*);
161 void mouseReleaseEvent(QMouseEvent*);
162 void mouseMoveEvent(QMouseEvent*);
163 void keyPressEvent(QKeyEvent*);
164 void paintEvent(QPaintEvent*);
165
166private:
167 QPointer<QWidget>widget;
168 bool pressed;
169 QString text;
170 QTextDocument* doc;
171 QString anchor;
172 QPixmap background;
173};
174
175QWhatsThat *QWhatsThat::instance = 0;
176
177// shadowWidth not const, for XP drop-shadow-fu turns it to 0
178static int shadowWidth = 6; // also used as '5' and '6' and even '8' below
179static const int vMargin = 8;
180static const int hMargin = 12;
181
182QWhatsThat::QWhatsThat(const QString& txt, QWidget* parent, QWidget *showTextFor)
183 : QWidget(parent, Qt::Popup),
184 widget(showTextFor), pressed(false), text(txt)
185{
186 delete instance;
187 instance = this;