|
1 /* |
|
2 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) |
|
3 |
|
4 This library is free software; you can redistribute it and/or |
|
5 modify it under the terms of the GNU Library General Public |
|
6 License as published by the Free Software Foundation; either |
|
7 version 2 of the License, or (at your option) any later version. |
|
8 |
|
9 This library is distributed in the hope that it will be useful, |
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 Library General Public License for more details. |
|
13 |
|
14 You should have received a copy of the GNU Library General Public License |
|
15 along with this library; see the file COPYING.LIB. If not, write to |
|
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
17 Boston, MA 02110-1301, USA. |
|
18 */ |
|
19 |
|
20 #include "config.h" |
|
21 #include "qwebinspector.h" |
|
22 |
|
23 #include "Element.h" |
|
24 #include "InspectorController.h" |
|
25 #include "qwebelement.h" |
|
26 #include "qwebinspector_p.h" |
|
27 #include "qwebpage_p.h" |
|
28 |
|
29 #include <QResizeEvent> |
|
30 |
|
31 /*! |
|
32 \class QWebInspector |
|
33 \since 4.6 |
|
34 \inmodule QtWebKit |
|
35 \brief The QWebInspector class allows the placement and control of a |
|
36 QWebPage's inspector. |
|
37 The inspector can display a page's hierarchy, its loading statistics and |
|
38 the current state of its individual elements. It is mostly used by web |
|
39 developers. |
|
40 |
|
41 The QWebPage to be inspected must be specified using the setPage() method. |
|
42 |
|
43 A typical use of QWebInspector follows: |
|
44 |
|
45 \snippet webkitsnippets/qtwebkit_qwebinspector_snippet.cpp 0 |
|
46 |
|
47 A QWebInspector can be made visible either programmatically using |
|
48 setVisible(), or by the user through the attached QWebPage's context |
|
49 menu. |
|
50 |
|
51 \note A QWebInspector will display a blank widget if either: |
|
52 \list |
|
53 \o page() is null |
|
54 \o QWebSettings::DeveloperExtrasEnabled is false |
|
55 \endlist |
|
56 |
|
57 \section1 Resources |
|
58 |
|
59 This class acts mostly as a container and a controller for the inspector. |
|
60 Most of the resources needed by the inspector are owned by the associated |
|
61 QWebPage and are allocated the first time that: |
|
62 \list |
|
63 \o an element is inspected |
|
64 \o the QWebInspector is shown. |
|
65 \endlist |
|
66 |
|
67 \section1 Inspector configuration persistence |
|
68 |
|
69 The inspector allows the user to configure some options through its |
|
70 user interface (e.g. the resource tracking "Always enable" option). |
|
71 These settings will be persisted automatically by QtWebKit only if |
|
72 your application previously called QCoreApplication::setOrganizationName() |
|
73 and QCoreApplication::setApplicationName(). |
|
74 See QSettings's default constructor documentation for an explanation |
|
75 of why this is necessary. |
|
76 */ |
|
77 |
|
78 /*! |
|
79 Constructs an unbound QWebInspector with \a parent as its parent. |
|
80 */ |
|
81 QWebInspector::QWebInspector(QWidget* parent) |
|
82 : QWidget(parent) |
|
83 , d(new QWebInspectorPrivate(this)) |
|
84 { |
|
85 } |
|
86 |
|
87 /*! |
|
88 Destroys the inspector. |
|
89 */ |
|
90 QWebInspector::~QWebInspector() |
|
91 { |
|
92 // Remove association principally to prevent deleting a child frontend |
|
93 setPage(0); |
|
94 } |
|
95 |
|
96 /*! |
|
97 Bind this inspector to the QWebPage to be inspected. |
|
98 |
|
99 \bold {Notes:} |
|
100 \list |
|
101 \o There can only be one QWebInspector associated with a QWebPage |
|
102 and vice versa. |
|
103 \o Calling this method with a null \a page will break the current association, if any. |
|
104 \o If \a page is already associated to another QWebInspector, the association |
|
105 will be replaced and the previous QWebInspector will become unbound |
|
106 \endlist |
|
107 |
|
108 \sa page() |
|
109 */ |
|
110 void QWebInspector::setPage(QWebPage* page) |
|
111 { |
|
112 if (d->page) { |
|
113 // Break currentPage-->this |
|
114 d->page->d->setInspector(0); |
|
115 } |
|
116 if (page && page->d->inspector && page->d->inspector != this) { |
|
117 // Break newPage<->newPageCurrentInspector |
|
118 page->d->inspector->setPage(0); |
|
119 } |
|
120 |
|
121 d->page = page; |
|
122 |
|
123 if (page) { |
|
124 // Setup the reciprocal association |
|
125 page->d->setInspector(this); |
|
126 } |
|
127 } |
|
128 |
|
129 /*! |
|
130 Returns the inspected QWebPage. |
|
131 If no web page is currently associated, a null pointer is returned. |
|
132 */ |
|
133 QWebPage* QWebInspector::page() const |
|
134 { |
|
135 return d->page; |
|
136 } |
|
137 |
|
138 /*! \reimp */ |
|
139 QSize QWebInspector::sizeHint() const |
|
140 { |
|
141 return QSize(450, 300); |
|
142 } |
|
143 |
|
144 /*! \reimp */ |
|
145 bool QWebInspector::event(QEvent* ev) |
|
146 { |
|
147 return QWidget::event(ev); |
|
148 } |
|
149 |
|
150 /*! \reimp */ |
|
151 void QWebInspector::resizeEvent(QResizeEvent* event) |
|
152 { |
|
153 d->adjustFrontendSize(event->size()); |
|
154 } |
|
155 |
|
156 /*! \reimp */ |
|
157 void QWebInspector::showEvent(QShowEvent* event) |
|
158 { |
|
159 #if ENABLE(INSPECTOR) |
|
160 // Allows QWebInspector::show() to init the inspector. |
|
161 if (d->page) |
|
162 d->page->d->inspectorController()->show(); |
|
163 #endif |
|
164 } |
|
165 |
|
166 /*! \reimp */ |
|
167 void QWebInspector::hideEvent(QHideEvent* event) |
|
168 { |
|
169 #if ENABLE(INSPECTOR) |
|
170 if (d->page) |
|
171 d->page->d->inspectorController()->close(); |
|
172 #endif |
|
173 } |
|
174 |
|
175 /*! \reimp */ |
|
176 void QWebInspector::closeEvent(QCloseEvent* event) |
|
177 { |
|
178 #if ENABLE(INSPECTOR) |
|
179 if (d->page) |
|
180 d->page->d->inspectorController()->close(); |
|
181 #endif |
|
182 } |
|
183 |
|
184 /*! \internal */ |
|
185 void QWebInspectorPrivate::setFrontend(QWidget* newFrontend) |
|
186 { |
|
187 if (frontend) |
|
188 frontend->setParent(0); |
|
189 |
|
190 frontend = newFrontend; |
|
191 |
|
192 if (frontend) { |
|
193 frontend->setParent(q); |
|
194 frontend->show(); |
|
195 adjustFrontendSize(q->size()); |
|
196 } |
|
197 } |
|
198 |
|
199 void QWebInspectorPrivate::adjustFrontendSize(const QSize& size) |
|
200 { |
|
201 if (frontend) |
|
202 frontend->resize(size); |
|
203 } |
|
204 |