Back to index

salome-gui  6.5.0
QtxWebBrowser.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:      QtxWebBrowser.cxx
00024 // Author:    Roman NIKOLAEV
00025 //
00026 #include "QtxWebBrowser.h"
00027 #include "QtxSearchTool.h"
00028 
00029 #include <QApplication>
00030 #include <QFileInfo>
00031 #include <QWebView>
00032 #include <QMenuBar>
00033 #include <QToolBar>
00034 #include <QMenu>
00035 #include <QStatusBar>
00036 #include <QVBoxLayout>
00037 
00043 class WebViewSearcher : public QtxSearchTool::Searcher
00044 {
00045 public:
00046   WebViewSearcher( QWebView* );
00047   ~WebViewSearcher();
00048 
00049   bool find( const QString&, QtxSearchTool* );
00050   bool findNext( const QString&, QtxSearchTool* );
00051   bool findPrevious( const QString&, QtxSearchTool* );
00052   bool findFirst( const QString&, QtxSearchTool* );
00053   bool findLast( const QString&, QtxSearchTool* );
00054 
00055 private:
00056   QWebView* myView;
00057 };
00058 
00059 WebViewSearcher::WebViewSearcher( QWebView* view ) : myView( view )
00060 {
00061 }
00062 
00063 WebViewSearcher::~WebViewSearcher()
00064 {
00065 }
00066 
00067 bool WebViewSearcher::find( const QString& text, QtxSearchTool* st )
00068 {
00069   QWebPage::FindFlags fl = 0;
00070   if ( st->isCaseSensitive() ) fl = fl | QWebPage::FindCaseSensitively;
00071   if ( st->isSearchWrapped() ) fl = fl | QWebPage::FindWrapsAroundDocument;
00072   return myView->findText( text, fl );
00073 }
00074 
00075 bool WebViewSearcher::findNext( const QString& text, QtxSearchTool* st )
00076 {
00077   return find( text, st );
00078 }
00079 
00080 bool WebViewSearcher::findPrevious( const QString& text, QtxSearchTool* st )
00081 {
00082   QWebPage::FindFlags fl = QWebPage::FindBackward;
00083   if ( st->isCaseSensitive() ) fl = fl | QWebPage::FindCaseSensitively;
00084   if ( st->isSearchWrapped() ) fl = fl | QWebPage::FindWrapsAroundDocument;
00085   return myView->findText( text, fl );
00086 }
00087 
00088 bool WebViewSearcher::findFirst( const QString&, QtxSearchTool* )
00089 {
00090   return false;
00091 }
00092 
00093 bool WebViewSearcher::findLast( const QString&, QtxSearchTool* )
00094 {
00095   return false;
00096 }
00097 
00138 
00139 QtxWebBrowser* QtxWebBrowser::myBrowser = 0;
00140 
00142 QMap<QString, QVariant> QtxWebBrowser::myData;
00143 
00149 QtxWebBrowser::QtxWebBrowser() : QMainWindow( 0 )
00150 {
00151   setAttribute( Qt::WA_DeleteOnClose );
00152   statusBar();
00153 
00154   QWidget* frame = new QWidget( this );
00155 
00156   myWebView = new QWebView( frame );
00157   myWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
00158   myFindPanel = new QtxSearchTool( frame, myWebView,
00159                                QtxSearchTool::Basic | QtxSearchTool::Case | QtxSearchTool::Wrap, 
00160                                Qt::Horizontal );
00161   myFindPanel->setFrameStyle( QFrame::NoFrame | QFrame::Plain );
00162   myFindPanel->setActivators( QtxSearchTool::SlashKey );
00163   myFindPanel->setSearcher( new WebViewSearcher( myWebView ) );
00164   myFindPanel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
00165 
00166   myToolbar = addToolBar( tr( "Navigation" ) );
00167   myToolbar->addAction( myWebView->pageAction( QWebPage::Back ) );
00168   myToolbar->addAction( myWebView->pageAction( QWebPage::Forward ) );
00169 
00170   myMenus[ File ]        = menuBar()->addMenu( tr( "&File" ) );
00171   myActions[ Find ]      = myMenus[ File ]->addAction( tr( "&Find in text..." ), myFindPanel, SLOT( find() ),         QKeySequence( QKeySequence::Find ) );
00172   myActions[ FindNext ]  = myMenus[ File ]->addAction( tr( "&Find next" ),       myFindPanel, SLOT( findNext() ),     QKeySequence( QKeySequence::FindNext ) );
00173   myActions[ FindPrev ]  = myMenus[ File ]->addAction( tr( "&Find previous" ),   myFindPanel, SLOT( findPrevious() ), QKeySequence( QKeySequence::FindPrevious ) );
00174   myMenus[ File ]->addSeparator();
00175   myActions[ Close ]     = myMenus[ File ]->addAction( tr( "&Close" ),           this, SLOT( close() ) );
00176 
00177   QVBoxLayout* main = new QVBoxLayout( frame );
00178   main->addWidget( myWebView );
00179   main->addWidget( myFindPanel );
00180   main->setMargin( 0 );
00181   main->setSpacing( 3 );
00182 
00183   connect( myWebView, SIGNAL( titleChanged( QString ) ), SLOT( adjustTitle() ) ); 
00184   connect( myWebView, SIGNAL( linkClicked( QUrl ) ),     SLOT( linkClicked( QUrl ) ) ); 
00185   connect( myWebView->page(), SIGNAL( linkHovered( QString, QString, QString ) ), SLOT( linkHovered( QString, QString, QString ) ) ); 
00186   
00187   setCentralWidget( frame );
00188   setFocusProxy( myWebView );
00189   updateData();
00190   qAddPostRoutine( QtxWebBrowser::clearData );
00191 }
00192 
00196 QtxWebBrowser::~QtxWebBrowser()
00197 {
00198   myBrowser = 0;
00199 }
00200 
00205 QtxWebBrowser* QtxWebBrowser::webBrowser()
00206 {
00207   if ( !myBrowser )
00208     myBrowser = new QtxWebBrowser();
00209   return myBrowser;
00210 }
00211 
00217 void QtxWebBrowser::loadUrl( const QString& url, const QString& anchor )
00218 {
00219   QString anUrl = url;
00220   if( !anchor.isEmpty() ) anUrl += "#" + anchor;
00221   anUrl.replace('\\', '/');
00222 
00223   Qtx::alignWidget( webBrowser(), (QWidget*)QApplication::desktop(), Qtx::AlignCenter );
00224 
00225   webBrowser()->show();
00226   webBrowser()->myWebView->load( QUrl( anUrl ) );
00227   webBrowser()->setFocus();
00228   webBrowser()->activateWindow();
00229   webBrowser()->raise();
00230 }
00231 
00257 void QtxWebBrowser::setData( const QString& key, const QVariant& val )
00258 {
00259   myData.insert( key, val );
00260   if ( myBrowser ) myBrowser->updateData();
00261 }
00262 
00269 QString QtxWebBrowser::getStringValue( const QString& key )
00270 {
00271   QString val;
00272   if ( myData.contains( key ) && myData[key].canConvert( QVariant::String ) )
00273     val = myData[key].toString();
00274   return val;
00275 }
00276 
00283 QIcon QtxWebBrowser::getIconValue(const QString& key)
00284 {
00285   QIcon val;
00286   if ( myData.contains( key ) ) {
00287     if ( myData[key].canConvert( QVariant::Pixmap ) )
00288       val = myData[key].value<QPixmap>();
00289     else if ( myData[key].canConvert( QVariant::Icon ) )
00290       val = myData[key].value<QIcon>();
00291   }
00292   return val;
00293 }
00294 
00298 void QtxWebBrowser::updateData()
00299 {
00300   // main title
00301   adjustTitle();
00302 
00303   // window icon
00304   QIcon icon = getIconValue( "browser:icon" );
00305   if ( !icon.isNull() )
00306     setWindowIcon( icon );
00307 
00308   // toolbar title
00309   QString tbTitle = getStringValue( "toolbar:title" );
00310   if ( myToolbar && !tbTitle.isEmpty() )
00311     myToolbar->setWindowTitle( tbTitle );
00312 
00313   // File menu
00314   QString fmenu = getStringValue( "menu:file:title" );
00315   if ( myMenus.contains( File ) && !fmenu.isEmpty() )
00316     myMenus[ File ]->setTitle( fmenu );
00317 
00318   // File/Close menu
00319   QString closeTlt = getStringValue( "action:close:title" );
00320   QIcon closeIco = getIconValue( "action:close:icon" );
00321   if ( myActions.contains( Close ) ) {
00322     if ( !closeTlt.isEmpty() )
00323       myActions[ Close ]->setText( closeTlt );
00324     if ( !closeIco.isNull() )
00325       myActions[ Close ]->setIcon( closeIco );
00326   }
00327 
00328   // Navigation/Go Back menu
00329   QString backTlt = getStringValue( "action:back:title" );
00330   QIcon backIco = getIconValue( "action:back:icon" );
00331   if ( !backTlt.isEmpty() )
00332     myWebView->pageAction( QWebPage::Back )->setText( backTlt );
00333   if ( !backIco.isNull() )
00334     myWebView->pageAction( QWebPage::Back )->setIcon( backIco );
00335 
00336   // Navigation/Go Forward menu
00337   QString fwdTlt = getStringValue( "action:forward:title" );
00338   QIcon fwdIco = getIconValue( "action:forward:icon" );
00339   if ( !fwdTlt.isEmpty() )
00340     myWebView->pageAction( QWebPage::Forward )->setText( fwdTlt );
00341   if ( !fwdIco.isNull() )
00342     myWebView->pageAction( QWebPage::Forward )->setIcon( fwdIco );
00343 
00344   // File/Find menu
00345   QString findTlt = getStringValue( "action:find:title" );
00346   QIcon findIco = getIconValue( "action:find:icon" );
00347   if ( myActions.contains( Find ) ) {
00348     if ( !findTlt.isEmpty() )
00349       myActions[ Find ]->setText( findTlt );
00350     if ( !findIco.isNull() )
00351       myActions[ Find ]->setIcon( findIco );
00352   }
00353 
00354   // File/Find Next menu
00355   QString findNextTlt = getStringValue( "action:findnext:title" );
00356   QIcon findNextIco = getIconValue( "action:findnext:icon" );
00357   if ( myActions.contains( FindNext ) ) {
00358     if ( !findNextTlt.isEmpty() )
00359       myActions[ FindNext ]->setText( findNextTlt );
00360     if ( !findNextIco.isNull() )
00361       myActions[ FindNext ]->setIcon( findNextIco );
00362   }
00363 
00364   // File/Find Previous menu
00365   QString findPrevTlt = getStringValue( "action:findprev:title" );
00366   QIcon findPrevIco = getIconValue( "action:findprev:icon" );
00367   if ( myActions.contains( FindPrev ) ) {
00368     if ( !findPrevTlt.isEmpty() )
00369       myActions[ FindPrev ]->setText( findPrevTlt );
00370     if ( !findPrevIco.isNull() )
00371       myActions[ FindPrev ]->setIcon( findPrevIco );
00372   }
00373 }
00374 
00379 void QtxWebBrowser::clearData()
00380 {
00381   myData.clear();
00382 }
00383 
00389 void QtxWebBrowser::linkClicked( const QUrl& url )
00390 {
00391   myWebView->page()->setLinkDelegationPolicy( QWebPage::DontDelegateLinks );
00392   myWebView->load( url );
00393   if ( url.scheme() == "file" ) {
00394     QString filename = url.toLocalFile();
00395     if ( QFileInfo( filename ).suffix().toLower() == "pdf" ) {
00396 #ifdef WIN32
00397       ::system( QString( "start %2" ).arg( filename ).toLatin1().constData() );
00398 #else
00399       // special processing of PDF files
00400       QStringList readers;
00401       readers << "xdg-open" << "acroread" << "kpdf" << "kghostview" << "xpdf";
00402       foreach ( QString r, readers ) {
00403        QString reader = QString( "/usr/bin/%1" ).arg( r );
00404        if ( QFileInfo( reader ).exists() ) {
00405          ::system( QString( "unset LD_LIBRARY_PATH; %1 %2 &" ).arg( reader ).arg( url.toLocalFile() ).toLatin1().constData() );
00406          break;
00407        }
00408       }
00409 #endif // WIN32
00410     }
00411   }
00412   myWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
00413 }
00414 
00422 void QtxWebBrowser::linkHovered( const QString& link, const QString& /*title*/, const QString& /*context*/ )
00423 {
00424   statusBar()->showMessage( link );
00425 }
00426 
00431 void QtxWebBrowser::adjustTitle()
00432 {
00433   QString title = getStringValue( "browser:title" );
00434   setWindowTitle( title.isEmpty() ? myWebView->title() : title + QString( " [%1]" ).arg( myWebView->title() ) );
00435 }