* Make the InspectorClientView inherit from QWebView instead of QWidget. This
[WebKit-https.git] / WebKit / qt / WebCoreSupport / InspectorClientQt.cpp
1 /*
2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2007 Trolltech ASA
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer. 
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution. 
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission. 
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "InspectorClientQt.h"
32
33 #include "qwebpage.h"
34 #include "qwebpage_p.h"
35 #include "qwebview.h"
36
37 #include <QtCore/QCoreApplication>
38
39 #include "InspectorController.h"
40 #include "NotImplemented.h"
41 #include "Page.h"
42 #include "PlatformString.h"
43
44 namespace WebCore {
45
46 class InspectorClientWebPage : public QWebPage
47 {
48 public:
49     QWebPage* createWindow()
50     {
51         QWidget *w = new QWebView(0);
52         QWebPage *page = new QWebPage(w);
53         page->setView(w);
54         connect(page, SIGNAL(destroyed()), w, SLOT(deleteLater()));
55         return page;
56     }
57 };
58
59
60 class InspectorClientView : public QWebView {
61 public:
62     InspectorClientView(InspectorController* controller)
63         : QWebView(0)
64         , m_controller(controller)
65     {
66         setPage(new InspectorClientWebPage);
67         connect(page(), SIGNAL(destroyed()), SLOT(deleteLater()));
68     }
69
70 protected:
71     void hideEvent(QHideEvent* ev)
72     {
73         QWidget::hideEvent(ev);
74         m_controller->setWindowVisible(false);
75     }
76
77     void closeEvent(QCloseEvent* ev)
78     {
79         QWidget::closeEvent(ev);
80         m_controller->setWindowVisible(false);
81     }
82
83 private:
84     InspectorController* m_controller;
85 };
86
87
88 InspectorClientQt::InspectorClientQt(QWebPage* page)
89     : m_inspectedWebPage(page)
90     , m_attached(false)
91 {}
92
93 void InspectorClientQt::inspectorDestroyed()
94 {
95     delete this;
96 }
97
98 Page* InspectorClientQt::createPage()
99 {
100     if (m_webPage)
101         return m_webPage->d->page;
102
103     InspectorClientView* view = new InspectorClientView(m_inspectedWebPage->d->page->inspectorController());
104     m_webPage.set(view->page());
105     m_webPage->mainFrame()->load(QString::fromLatin1("qrc:/webkit/inspector/inspector.html"));
106     m_webPage->view()->setMinimumSize(400,300);
107     return m_webPage->d->page;
108 }
109
110 String InspectorClientQt::localizedStringsURL()
111 {
112     notImplemented();
113     return String();
114 }
115
116 void InspectorClientQt::showWindow()
117 {
118     if (!m_webPage)
119         return;
120
121     updateWindowTitle();
122     m_webPage->view()->show();
123     m_inspectedWebPage->d->page->inspectorController()->setWindowVisible(true);
124 }
125
126 void InspectorClientQt::closeWindow()
127 {
128     if (!m_webPage)
129         return;
130
131     m_webPage->view()->hide();
132     m_inspectedWebPage->d->page->inspectorController()->setWindowVisible(false);
133 }
134
135 bool InspectorClientQt::windowVisible()
136 {
137     if (!m_webPage)
138         return false;
139     return m_webPage->view()->isVisible();
140 }
141
142 void InspectorClientQt::attachWindow()
143 {
144     ASSERT(m_inspectedWebPage);
145     ASSERT(m_webPage);
146     ASSERT(!m_attached);
147
148     m_attached = true;
149     notImplemented();
150 }
151
152 void InspectorClientQt::detachWindow()
153 {
154     ASSERT(m_inspectedWebPage);
155     ASSERT(m_webPage);
156     ASSERT(m_attached);
157
158     m_attached = false;
159     notImplemented();
160 }
161
162 void InspectorClientQt::highlight(Node* node)
163 {
164     notImplemented();
165 }
166
167 void InspectorClientQt::hideHighlight()
168 {
169     notImplemented();
170 }
171
172 void InspectorClientQt::inspectedURLChanged(const String& newURL)
173 {
174     m_inspectedURL = newURL;
175     updateWindowTitle();
176 }
177
178 void InspectorClientQt::updateWindowTitle()
179 {
180     if (!m_webPage)
181         return;
182
183     QString caption = QCoreApplication::translate("QWebPage", "Web Inspector - %2");
184     m_webPage->view()->setWindowTitle(caption.arg(m_inspectedURL));
185 }
186
187 }