class library: PriorityQueue - implement removeValue, hide array
[supercollider.git] / QtCollider / widgets / QcWebView.cpp
blob1da340e9b317b508f37e9f18edaed85ddc51f0ae
1 /************************************************************************
3 * Copyright 2011 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 static QcWidgetFactory<QtCollider::WebView> factory;
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 );
63 QString WebView::url() const
65 return QWebView::url().toString();
68 void WebView::setUrl( const QString & str )
70 load( urlFromString(str) );
73 QString WebView::html () const
75 return page()->mainFrame()->toHtml();
78 void WebView::setHtml ( const QString &html, const QString &baseUrl )
80 QUrl url( baseUrl.isEmpty() ? QUrl() : urlFromString(baseUrl) );
81 QWebView::setHtml( html, url );
84 QString WebView::plainText () const
86 return page()->mainFrame()->toPlainText();
89 QWebPage::LinkDelegationPolicy WebView::linkDelegationPolicy () const
91 return page()->linkDelegationPolicy();
94 void WebView::setLinkDelegationPolicy ( QWebPage::LinkDelegationPolicy p )
96 page()->setLinkDelegationPolicy( p );
99 bool WebView::delegateReload() const
101 WebPage *p = qobject_cast<QtCollider::WebPage*>(page());
102 Q_ASSERT(p);
103 return p->delegateReload();
106 void WebView::setDelegateReload( bool flag )
108 WebPage *p = qobject_cast<QtCollider::WebPage*>(page());
109 Q_ASSERT(p);
110 p->setDelegateReload( flag );
113 void WebView::setFontFamily( int generic, const QString & specific )
115 settings()->setFontFamily( (QWebSettings::FontFamily) generic, specific );
118 void WebView::evaluateJavaScript ( const QString &script )
120 if( script.isEmpty() ) return;
121 QWebFrame *frame = page()->currentFrame();
122 if( frame ) frame->evaluateJavaScript( script );
125 void WebView::findText( const QString &searchText, bool reversed )
127 QWebPage::FindFlags flags( QWebPage::FindWrapsAroundDocument );
128 if( reversed ) flags |= QWebPage::FindBackward;
129 QWebView::findText( searchText, flags );
132 void WebView::onLinkClicked( const QUrl &url )
134 Q_EMIT( linkActivated( url.toString() ) );
137 void WebView::onPageReload()
139 Q_EMIT( reloadTriggered( url() ) );
142 void WebView::keyPressEvent( QKeyEvent *e )
144 if( _interpretSelection && e->modifiers() & (Qt::ControlModifier|Qt::ShiftModifier)
145 && ( e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter ) )
147 QString selection = selectedText();
148 if( !selection.isEmpty() ) {
149 Q_EMIT( interpret( selection ) );
150 return;
154 QWebView::keyPressEvent( e );
158 void WebPage::triggerAction ( WebAction action, bool checked )
160 switch ( action ) {
161 case QWebPage::Reload:
162 if( _delegateReload ) return;
163 break;
164 case QWebPage::Copy:
165 QApplication::clipboard()->setText( selectedText() );
166 return;
167 default: ;
170 QWebPage::triggerAction( action, checked );
173 } // namespace QtCollider