Home | History | Annotate | Download | only in Api
      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     \brief The QWebInspector class allows the placement and control of a
     35     QWebPage's inspector.
     36     The inspector allows you to see a page current hierarchy and loading
     37     statistics.
     38 
     39     The QWebPage to be inspected is determined with the setPage() method.
     40 
     41     A typical use of QWebInspector follows:
     42 
     43     \snippet webkitsnippets/qtwebkit_qwebinspector_snippet.cpp 0
     44 
     45     \note A QWebInspector will display a blank widget if either:
     46     \list
     47         \o page() is null
     48         \o QWebSettings::DeveloperExtrasEnabled is false
     49     \endlist
     50 
     51     \section1 Resources
     52 
     53     This class acts mostly as a container and a controller for the inspector.
     54     Most of the resources needed by the inspector are owned by the associated
     55     QWebPage and are allocated the first time that:
     56     \list
     57         \o an element is inspected
     58         \o the QWebInspector is shown.
     59     \endlist
     60 
     61     \section1 Inspector configuration persistence
     62 
     63     The inspector allows the user to configure some options through its
     64     interface (e.g. the resource tracking "Always enable" option).
     65     These settings are persisted automatically by QtWebKit using QSettings.
     66 
     67     However since the QSettings object is instantiated using the empty
     68     constructor, QCoreApplication::setOrganizationName() and
     69     QCoreApplication::setApplicationName() must be called within your
     70     application to enable the persistence of these options.
     71 */
     72 
     73 /*!
     74     Constructs an empty QWebInspector with parent \a parent.
     75 */
     76 QWebInspector::QWebInspector(QWidget* parent)
     77     : QWidget(parent)
     78     , d(new QWebInspectorPrivate(this))
     79 {
     80 }
     81 
     82 /*!
     83     Destroys the inspector.
     84 */
     85 QWebInspector::~QWebInspector()
     86 {
     87     // Remove association principally to prevent deleting a child frontend
     88     setPage(0);
     89 }
     90 
     91 /*!
     92     Sets the QWebPage to be inspected.
     93 
     94     There can only be one QWebInspector associated with a QWebPage
     95     and vices versa.
     96 
     97     Calling with \a page as null will break the current association, if any.
     98 
     99     If \a page is already associated to another QWebInspector, the association
    100     will be replaced and the previous QWebInspector will have no page
    101     associated.
    102 
    103     \sa page()
    104 */
    105 void QWebInspector::setPage(QWebPage* page)
    106 {
    107     if (d->page) {
    108         // Break currentPage-->this
    109         d->page->d->setInspector(0);
    110     }
    111     if (page && page->d->inspector && page->d->inspector != this) {
    112         // Break newPage<->newPageCurrentInspector
    113         page->d->inspector->setPage(0);
    114     }
    115 
    116     d->page = page;
    117 
    118     if (page) {
    119         // Setup the reciprocal association
    120         page->d->setInspector(this);
    121     }
    122 }
    123 
    124 /*!
    125     Returns the inspected QWebPage.
    126     If no web page is currently associated, a null pointer is returned.
    127 */
    128 QWebPage* QWebInspector::page() const
    129 {
    130     return d->page;
    131 }
    132 
    133 /*! \reimp */
    134 QSize QWebInspector::sizeHint() const
    135 {
    136     return QSize(450, 300);
    137 }
    138 
    139 /*! \reimp */
    140 bool QWebInspector::event(QEvent* ev)
    141 {
    142     return QWidget::event(ev);
    143 }
    144 
    145 /*! \reimp */
    146 void QWebInspector::resizeEvent(QResizeEvent* event)
    147 {
    148     d->adjustFrontendSize(event->size());
    149 }
    150 
    151 /*! \reimp */
    152 void QWebInspector::showEvent(QShowEvent* event)
    153 {
    154 #if ENABLE(INSPECTOR)
    155     // Allows QWebInspector::show() to init the inspector.
    156     if (d->page)
    157         d->page->d->inspectorController()->show();
    158 #endif
    159 }
    160 
    161 /*! \reimp */
    162 void QWebInspector::hideEvent(QHideEvent* event)
    163 {
    164 }
    165 
    166 /*! \reimp */
    167 void QWebInspector::closeEvent(QCloseEvent* event)
    168 {
    169 #if ENABLE(INSPECTOR)
    170     if (d->page)
    171         d->page->d->inspectorController()->setWindowVisible(false);
    172 #endif
    173 }
    174 
    175 /*! \internal */
    176 void QWebInspectorPrivate::setFrontend(QWidget* newFrontend)
    177 {
    178     if (frontend)
    179         frontend->setParent(0);
    180 
    181     frontend = newFrontend;
    182 
    183     if (frontend) {
    184         frontend->setParent(q);
    185         frontend->show();
    186         adjustFrontendSize(q->size());
    187     }
    188 }
    189 
    190 void QWebInspectorPrivate::adjustFrontendSize(const QSize& size)
    191 {
    192     if (frontend)
    193         frontend->resize(size);
    194 }
    195 
    196