"Post Window" -> "Post window" prevents it being seen as two separate
[supercollider.git] / QtCollider / widgets / QcWebView.cpp
blob3ef9ed853562182cfaaed6f66922fc98d6ca497e
1 /************************************************************************
3 * Copyright 2011-2012 Jakob Leben (jakob.leben@gmail.com)
5 * This file is part of SuperCollider Qt GUI.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ************************************************************************/
22 #include "QcWebView.h"
23 #include "../QcWidgetFactory.h"
24 #include <QWebPage>
25 #include <QWebFrame>
26 #include <QAction>
27 #include <QShortcut>
28 #include <QKeyEvent>
29 #include <QApplication>
30 #include <QStyle>
31 #include <QClipboard>
33 QC_DECLARE_QWIDGET_FACTORY(WebView);
35 namespace QtCollider {
37 WebView::WebView( QWidget *parent ) :
38 QWebView( parent ),
39 _interpretSelection(false)
41 QtCollider::WebPage *page = new WebPage(this);
42 page->setDelegateReload(true);
43 setPage( page );
45 // Set the style's standard palette to avoid system's palette incoherencies
46 // get in the way of rendering web pages
47 setPalette( style()->standardPalette() );
49 QShortcut *scutCopy = new QShortcut( QKeySequence::Copy, this );
50 scutCopy->setContext( Qt::WidgetWithChildrenShortcut );
51 connect( scutCopy, SIGNAL(activated()),
52 pageAction(QWebPage::Copy), SLOT(trigger()) );
54 connect( this, SIGNAL(linkClicked(QUrl)), this, SLOT(onLinkClicked(QUrl)) );
55 connect( page->action(QWebPage::Reload), SIGNAL(triggered(bool)),
56 this, SLOT(onPageReload()) );
58 connect( this, SIGNAL(interpret(QString)),
59 qApp, SLOT(interpret(QString)),
60 Qt::QueuedConnection );
62 connect( page, SIGNAL(jsConsoleMsg(const QString&, int, const QString&)),
63 this, SIGNAL(jsConsoleMsg(const QString&, int, const QString&)) );
66 QString WebView::url() const
68 return QWebView::url().toString();
71 void WebView::setUrl( const QString & str )
73 load( urlFromString(str) );
76 QString WebView::html () const
78 return page()->mainFrame()->toHtml();
81 void WebView::setHtml ( const QString &html, const QString &baseUrl )
83 QUrl url( baseUrl.isEmpty() ? QUrl() : urlFromString(baseUrl) );
84 QWebView::setHtml( html, url );
87 QString WebView::plainText () const
89 return page()->mainFrame()->toPlainText();
92 QWebPage::LinkDelegationPolicy WebView::linkDelegationPolicy () const
94 return page()->linkDelegationPolicy();
97 void WebView::setLinkDelegationPolicy ( QWebPage::LinkDelegationPolicy p )
99 page()->setLinkDelegationPolicy( p );
102 bool WebView::delegateReload() const
104 WebPage *p = qobject_cast<QtCollider::WebPage*>(page());
105 Q_ASSERT(p);
106 return p->delegateReload();
109 void WebView::setDelegateReload( bool flag )
111 WebPage *p = qobject_cast<QtCollider::WebPage*>(page());
112 Q_ASSERT(p);
113 p->setDelegateReload( flag );
116 void WebView::setFontFamily( int generic, const QString & specific )
118 settings()->setFontFamily( (QWebSettings::FontFamily) generic, specific );
121 void WebView::evaluateJavaScript ( const QString &script )
123 if( script.isEmpty() ) return;
124 QWebFrame *frame = page()->currentFrame();
125 if( frame ) frame->evaluateJavaScript( script );
128 void WebView::findText( const QString &searchText, bool reversed )
130 QWebPage::FindFlags flags( QWebPage::FindWrapsAroundDocument );
131 if( reversed ) flags |= QWebPage::FindBackward;
132 QWebView::findText( searchText, flags );
135 void WebView::onLinkClicked( const QUrl &url )
137 Q_EMIT( linkActivated( url.toString() ) );
140 void WebView::onPageReload()
142 Q_EMIT( reloadTriggered( url() ) );
145 void WebView::keyPressEvent( QKeyEvent *e )
147 if( _interpretSelection && e->modifiers() & (Qt::ControlModifier|Qt::ShiftModifier)
148 && ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter ) )
150 QString selection = selectedText();
151 if( !selection.isEmpty() ) {
152 Q_EMIT( interpret( selection ) );
153 return;
157 QWebView::keyPressEvent( e );
161 void WebPage::triggerAction ( WebAction action, bool checked )
163 switch ( action ) {
164 case QWebPage::Reload:
165 if( _delegateReload ) return;
166 break;
167 case QWebPage::Copy:
168 QApplication::clipboard()->setText( selectedText() );
169 return;
170 default: ;
173 QWebPage::triggerAction( action, checked );
176 void WebPage::javaScriptConsoleMessage ( const QString & msg, int line, const QString & src )
178 Q_EMIT( jsConsoleMsg(msg,line,src) );
181 } // namespace QtCollider