9968bd7cef4117c6778079ad9347dac2be753bb4
[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 <QtCore/QCoreApplication>
36
37 #include "InspectorController.h"
38 #include "NotImplemented.h"
39 #include "Page.h"
40 #include "PlatformString.h"
41
42 namespace WebCore {
43
44 class InspectorClientWebPage : public QWebPage
45 {
46 public:
47     QWebPage* createWindow()
48     {
49         QWidget *w = new QWidget(0);
50         QWebPage *page = new QWebPage(w);
51         page->setView(w);
52         connect(page, SIGNAL(destroyed()), w, SLOT(deleteLater()));
53         return page;
54     }
55 };
56     
57
58 class InspectorClientView : public QWidget {
59 public:
60     InspectorClientView(InspectorController* controller)
61         : QWidget(0)
62         , m_controller(controller)
63         , m_page(new InspectorClientWebPage)
64     {
65         m_page->setView(this);
66         connect(m_page, SIGNAL(destroyed()), SLOT(deleteLater()));
67     }
68
69     QWebPage* page() const { return m_page; }
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     QWebPage* m_page;
86 };
87
88
89 InspectorClientQt::InspectorClientQt(QWebPage* page)
90     : m_inspectedWebPage(page)
91     , m_attached(false)
92 {}
93
94 void InspectorClientQt::inspectorDestroyed()
95 {
96     delete this;
97 }
98
99 Page* InspectorClientQt::createPage()
100 {
101     if (m_webPage)
102         return m_webPage->d->page;
103
104     InspectorClientView* view = new InspectorClientView(m_inspectedWebPage->d->page->inspectorController());
105     m_webPage.set(view->page());
106     m_webPage->mainFrame()->load(QString::fromLatin1("qrc:/webkit/inspector/inspector.html"));
107     m_webPage->view()->setMinimumSize(400,300);
108     return m_webPage->d->page;
109 }
110
111 String InspectorClientQt::localizedStringsURL()
112 {
113     notImplemented();
114     return String();
115 }
116
117 void InspectorClientQt::showWindow()
118 {
119     if (!m_webPage)
120         return;
121
122     updateWindowTitle();
123     m_webPage->view()->show();
124     m_inspectedWebPage->d->page->inspectorController()->setWindowVisible(true);
125 }
126
127 void InspectorClientQt::closeWindow()
128 {
129     if (!m_webPage)
130         return;
131
132     m_webPage->view()->hide();
133     m_inspectedWebPage->d->page->inspectorController()->setWindowVisible(false);
134 }
135
136 bool InspectorClientQt::windowVisible()
137 {
138     if (!m_webPage)
139         return false;
140     return m_webPage->view()->isVisible();
141 }
142
143 void InspectorClientQt::attachWindow()
144 {
145     ASSERT(m_inspectedWebPage);
146     ASSERT(m_webPage);
147     ASSERT(!m_attached);
148
149     m_attached = true;
150     notImplemented();
151 }
152
153 void InspectorClientQt::detachWindow()
154 {
155     ASSERT(m_inspectedWebPage);
156     ASSERT(m_webPage);
157     ASSERT(m_attached);
158
159     m_attached = false;
160     notImplemented();
161 }
162
163 void InspectorClientQt::highlight(Node* node)
164 {
165     notImplemented();
166 }
167
168 void InspectorClientQt::hideHighlight()
169 {
170     notImplemented();
171 }
172
173 void InspectorClientQt::inspectedURLChanged(const String& newURL)
174 {
175     m_inspectedURL = newURL;
176     updateWindowTitle();
177 }
178
179 void InspectorClientQt::updateWindowTitle()
180 {
181     if (!m_webPage)
182         return;
183
184     QString caption = QCoreApplication::translate("QWebPage", "Web Inspector - %2");
185     m_webPage->view()->setWindowTitle(caption.arg(m_inspectedURL));
186 }
187
188 }