Debian Codegraph @

src/csoundhtmlview.cpp

raw symbols SCIP
1 #if defined(QCS_QTHTML)
2 #include "basedocument.h"
3 #include "documentpage.h"
4 #include "csoundhtmlview.h"
5 #include "ui_html5guidisplay.h"
6 #include <QLabel>
7 #include <QVBoxLayout>
8 #include <QWaitCondition>
9 #include <QFile>
10 #include <QObject>
11
12 CsoundHtmlView::CsoundHtmlView(QWidget *parent) :
13 QDockWidget(parent),
14 webView(nullptr),
15 ui(new Ui::Html5GuiDisplay),
16 documentPage(0),
17 m_csoundEngine(nullptr),
18 m_options(nullptr)
19 {
20 ui->setupUi(this);
21
22 #ifdef USE_WEBKIT
23 webView = new QWebView(this);
24 ui->inspectRow->hide(); // inspector included in QtWebKit, no need for that
25 #else
26 webView = new QWebEngineView(this);
27 //webView->page()->profile()->clearHttpCache();
28 QObject::connect(webView, &QWebEngineView::loadFinished, this, &CsoundHtmlView::removeTemporaryHtmlFile );
29 #endif
30 csoundHtmlWrapper.setCsoundHtmlView(this);
31 csoundHtmlOnlyWrapper.setCsoundHtmlView(this);
32 ui->mainLayout->addWidget(webView); // mainLayout is vertical layout box
33 webView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
34
35 #ifdef USE_WEBKIT
36 QObject::connect(webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
37 this, SLOT(addJSObject())); // to enable adding the object after reload
38 // add javascript inspector - open with right click on htmlview
39 webView->page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
40 QWebInspector inspector;
41 inspector.setPage(webView->page());
42 inspector.setVisible(true);
43 #else
44 // Enable dev tools by default for the test browser
45 connect(ui->inspectButton, SIGNAL(clicked()),this, SLOT(showDebugWindow()));
46 webView->page()->setWebChannel(&channel);
47 //qDebug() << "Setting JavaScript object on init.";
48 channel.registerObject("csound", &csoundHtmlWrapper);
49 #endif
50 }
51
52 CsoundHtmlView::~CsoundHtmlView()
53 {
54 delete ui;
55 }
56
57 QString getElement(const QString &text, const QString &tag)
58 {
59 QString::SectionFlags sectionflags = QString::SectionIncludeLeadingSep | QString::SectionIncludeTrailingSep | QString::SectionCaseInsensitiveSeps;
60 QString element = text.section("<" + tag, 1, 1, sectionflags);
61 element = element.section("</" + tag + ">", 0, 0, sectionflags);
62 return element;
63 }
64
65 void CsoundHtmlView::load(DocumentPage *documentPage_)
66 {
67 qDebug();
68 documentPage = documentPage_;
69 auto text = documentPage.load()->getFullText();
70 auto filename = documentPage.load()->getFileName();
71 QFile csdfile(filename);
72 csdfile.open(QIODevice::WriteOnly);
73 QTextStream out(&csdfile);
74 out << text;
75 csdfile.close();
76 auto html = getElement(text, "html");
77 if (html.size() > 0) {
78 #ifdef USE_WEBENGINE
79 // Inject necessary code to load qtwebchannel/qwebchannel.js.
80 QString injection = R"(
81 <script type="text/javascript" src="qrc:///qtwebchannel/qwebchannel.js"></script>
82 <script type="text/javascript">
83 "use strict";
84 document.addEventListener("DOMContentLoaded", function () {
85 try {
86 console.log("Initializing window.csound...");
87 window.channel = new QWebChannel(qt.webChannelTransport, function(channel) {
88 window.csound = channel.objects.csound;
89 if (typeof window.csound === 'undefined') {
90 alert('window.csound is undefined.');
91 return;
92 }
93 if (window.csound === null) {
94 alert('window.csound is null.');
95 return;
96 }
97 csound.message("Initialized csound.\n");
98 });
99 } catch (e) {
100 alert("initialize_csound error: " + e.message);
101 console.log(e.message);
102 }
103 });
104 </script>
105 )";
106 // Tricky because now HTML doesn't have to have a <head> element,
107 // and both <html> and <head> can have attributes. So we need to find an
108 // injection point that is the very first place allowed to put a <script>
109 // element.
110 int injection_index = html.indexOf("<head", 0, Qt::CaseInsensitive);
111 if (injection_index != -1) {
112 injection_index = html.indexOf(">", injection_index) + 1;
113 } else {
114 injection_index = html.indexOf("<html", 0, Qt::CaseInsensitive);
115 injection_index = html.indexOf(">", injection_index) + 1;
116 }
117 html = html.insert(injection_index, injection);
118 #endif
119 QString htmlFilename; // maybe use also tempHtml herer
120
121 QFileInfo fileInfo(QFileInfo(filename).path());
122
123
124 if (filename.startsWith(":/") || !fileInfo.isWritable()) { // if an example file or the folder is not writable
125 htmlFilename = QDir::tempPath()+"/html-example.html"; // create it in a temporary location
126 } else {
127 htmlFilename = filename + ".html";
128 }
129 htmlFile.setFileName(htmlFilename);
130 htmlFile.open(QIODevice::WriteOnly);
131 QTextStream out(&htmlFile);
132 out << html;
133 htmlFile.close();
134 loadFromUrl(QUrl::fromLocalFile(htmlFilename));
135
136 #ifdef USE_WEBENGINE
137 webView->page()->setWebChannel(&channel);
138 if (filename.endsWith(".html", Qt::CaseInsensitive)) {
139 // Register CsoundHtmlOnlyWrapper when performing HTML files.
140 qDebug() << "Setting CsoundHtmlOnlyWrapper JavaScript object on load.";
141 channel.registerObject("csound", &csoundHtmlOnlyWrapper);
142 csoundHtmlOnlyWrapper.registerConsole(documentPage_->getConsole());
143 csoundHtmlOnlyWrapper.setOptions(m_options);
144 } else {
145 // Register CsoundHtmlWrapper when performing CSD files with embedded <html> element.
146 qDebug() << "Setting CsoundWrapper JavaScript object on load.";
147 channel.registerObject("csound", &csoundHtmlWrapper);
148 }
149 #endif
150 }
151 repaint();
152 }
153
154 void CsoundHtmlView::setCsoundEngine(CsoundEngine *csEngine)
155 {
156 csoundHtmlWrapper.setCsoundEngine(csEngine);
157 }
158
159
160 void CsoundHtmlView::stop()
161 {
162 qDebug() ;
163 documentPage = 0;
164 csoundHtmlOnlyWrapper.stop();
165 }
166
167
168 #ifdef USE_WEBKIT
169 void CsoundHtmlView::addJSObject()
170 {
171 if (webView) {
172 QString filename = documentPage.load()->getFileName();
173 qDebug()<<"Adding Csound as JavaScript object";
174 if (filename.endsWith(".html", Qt::CaseInsensitive)) {
175 // Register CsoundHtmlOnlyWrapper when performing HTML files.
176 webView->page()->mainFrame()->addToJavaScriptWindowObject("csound", &csoundHtmlOnlyWrapper);
177 csoundHtmlOnlyWrapper.registerConsole(documentPage.load()->getConsole());
178 csoundHtmlOnlyWrapper.setOptions(m_options);
179 } else {
180 // Register CsoundHtmlWrapper when performing CSD files with embedded <html> element.
181 webView->page()->mainFrame()->addToJavaScriptWindowObject("csound", &csoundHtmlWrapper);
182 }
183
184
185 }
186
187
188
189 }
190 #endif
191
192 void CsoundHtmlView::loadFromUrl(const QUrl &url)
193 {
194 qDebug();
195
196 if(webView != 0) {
197 webView->setUrl(url);
198 }
199 if (!this->isVisible()) {
200 this->show();
201 this->raise();
202 }
203 }
204
205 void CsoundHtmlView::clear()
206 {
207 this->hide(); // just hide the panel, keep qwebchannel and other connections
208 //loadFromUrl(QUrl()); // empty URL to clear - this causes "qt is not defined" error
209 }
210
211 void CsoundHtmlView::setOptions(CsoundOptions *options)
212 {
213 m_options = options;
214 }
215
216 #ifdef USE_WEBENGINE
217 void CsoundHtmlView::showDebugWindow()
218 {
219 qDebug();
220 QByteArray debugPort = qgetenv("QTWEBENGINE_REMOTE_DEBUGGING");
221 if (!debugPort.isNull()) {
222 QWidget * debugger = new QWidget();
223 debugger->resize(600,400);
224 QWebEngineView * debuggerView= new QWebEngineView(debugger);
225 QVBoxLayout *layout = new QVBoxLayout(debugger);
226 layout->addWidget(debuggerView);
227 debugger->setAttribute(Qt::WA_DeleteOnClose);
228 debugger->setLayout(layout);
229 qDebug()<<"Opening window for localhost:"<<debugPort;
230 debuggerView->setUrl(QUrl("http://localhost:"+debugPort));
231 debugger->show();
232 } else {
233 qDebug()<<"Debugging port not set or reading failed";
234 }
235 }
236
237 void CsoundHtmlView::removeTemporaryHtmlFile(bool ok)
238 {
239 if (ok) {
240 qDebug() << "Removing temporary html: " << htmlFile.fileName();
241 htmlFile.remove();
242
243 }
244 }
245
246 #endif
247 #endif