Salome HOME
Copyright update 2022
[modules/gui.git] / src / Qtx / QtxWebBrowser.cxx
index 304ce6145c5d28d63b6eea47c92c60be9c082039..b1e3acfad74b5435730cf408a068c940a71f161d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 #include <QStatusBar>
 #include <QToolBar>
 #include <QVBoxLayout>
-#include <QWebView>
+#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
+  #include <QWebView>
+#else
+  #include <QWebEngineView>
+#endif
 #include <QProcess>
 
+// RNV:
+// Since from Qt 5.6.0 version QtWebKit tool was removed,
+// QtxWebBroswer is ported on QtWebEngine. So if it is built with Qt-5.6.0
+// and newer, it uses QtWebEngine. But for Qt-5.5.1 and Qt4 QtWebKit tool
+// is used, to provide backward compatibility.
+
 namespace
 {
   bool isLocalFile( const QUrl& url )
@@ -64,7 +74,7 @@ namespace
 class QtxWebBrowser::Searcher : public QtxSearchTool::Searcher
 {
 public:
-  Searcher( QWebView* );
+  Searcher( WebView* );
   ~Searcher();
 
   bool find( const QString&, QtxSearchTool* );
@@ -74,7 +84,7 @@ public:
   bool findLast( const QString&, QtxSearchTool* );
 
 private:
-  QWebView* myView;
+  WebView* myView;
 };
 
 /*!
@@ -82,7 +92,7 @@ private:
   \param view web view
   \internal
 */
-QtxWebBrowser::Searcher::Searcher( QWebView* view ) : myView( view )
+QtxWebBrowser::Searcher::Searcher( WebView* view ) : myView( view )
 {
 }
 
@@ -103,10 +113,16 @@ QtxWebBrowser::Searcher::~Searcher()
 */
 bool QtxWebBrowser::Searcher::find( const QString& text, QtxSearchTool* st )
 {
-  QWebPage::FindFlags fl = 0;
-  if ( st->isCaseSensitive() ) fl = fl | QWebPage::FindCaseSensitively;
-  if ( st->isSearchWrapped() ) fl = fl | QWebPage::FindWrapsAroundDocument;
+  WebPage::FindFlags fl = 0;
+  if ( st->isCaseSensitive() ) fl = fl | WebPage::FindCaseSensitively;
+#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0) 
+  if ( st->isSearchWrapped() ) fl = fl | WebPage::FindWrapsAroundDocument;
   return myView->findText( text, fl );
+#else
+  bool textFound;
+  myView->findText( text, fl, [&](bool found) { textFound = found; });
+  return textFound;
+#endif    
 }
 
 /*!
@@ -130,10 +146,16 @@ bool QtxWebBrowser::Searcher::findNext( const QString& text, QtxSearchTool* st )
 */
 bool QtxWebBrowser::Searcher::findPrevious( const QString& text, QtxSearchTool* st )
 {
-  QWebPage::FindFlags fl = QWebPage::FindBackward;
-  if ( st->isCaseSensitive() ) fl = fl | QWebPage::FindCaseSensitively;
-  if ( st->isSearchWrapped() ) fl = fl | QWebPage::FindWrapsAroundDocument;
+  WebPage::FindFlags fl = WebPage::FindBackward;
+  if ( st->isCaseSensitive() ) fl = fl | WebPage::FindCaseSensitively;
+#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0) 
+  if ( st->isSearchWrapped() ) fl = fl | WebPage::FindWrapsAroundDocument;
   return myView->findText( text, fl );
+#else
+  bool textFound;
+  myView->findText( text, fl, [&](bool found) { textFound = found; });
+  return textFound;
+#endif 
 }
 
 /*!
@@ -204,10 +226,10 @@ QtxWebBrowser::Downloader::Downloader( const QString& fileName, int action, cons
 
   QGridLayout* l = new QGridLayout( this );
   l->addWidget( new QLabel( QtxWebBrowser::tr( "You are opening the file" ), this ), 
-                           0, 0, 1, 4 );
+                            0, 0, 1, 4 );
   l->addWidget( myFileName, 1, 1, 1, 3 );
   l->addWidget( new QLabel( QtxWebBrowser::tr( "Please choose the action to be done" ), this ), 
-                           3, 0, 1, 4 );
+                            3, 0, 1, 4 );
   l->addWidget( rbOpen,     4, 1, 1, 1 );
   l->addWidget( myBrowse,   4, 2, 1, 1 );
   l->addWidget( rbSave,     5, 1, 1, 3 );
@@ -333,55 +355,62 @@ QtxWebBrowser::QtxWebBrowser( ) : QMainWindow( 0 )
 
   QWidget* frame = new QWidget( this );
 
-  myWebView = new QWebView( frame );
-
-  myWebView->pageAction( QWebPage::Copy )->setShortcut( QKeySequence::Copy );
-  myWebView->addAction( myWebView->pageAction( QWebPage::Copy ) );
-  myWebView->pageAction( QWebPage::OpenLinkInNewWindow )->setVisible( false );
-  myWebView->pageAction( QWebPage::Back )->setText( tr( "Go Back" ) );
-  myWebView->pageAction( QWebPage::Forward )->setText( tr( "Go Forward" ) );
-  myWebView->pageAction( QWebPage::Reload )->setText( tr( "Refresh" ) );
-
-  myWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
-
+  myWebView = new WebView( frame );
+
+  myWebView->pageAction( WebPage::Copy )->setShortcut( QKeySequence::Copy );
+  myWebView->addAction( myWebView->pageAction( WebPage::Copy ) );
+#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
+  myWebView->pageAction( WebPage::OpenLinkInNewWindow )->setVisible( false );
+#endif  
+  myWebView->pageAction( WebPage::Back )->setText( tr( "Go Back" ) );
+  myWebView->pageAction( WebPage::Forward )->setText( tr( "Go Forward" ) );
+  myWebView->pageAction( WebPage::Reload )->setText( tr( "Refresh" ) );
+#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
+  myWebView->page()->setLinkDelegationPolicy( WebPage::DelegateAllLinks );
+#endif
+  
   myFindPanel = new QtxSearchTool( frame, myWebView,
-                                  QtxSearchTool::Basic | QtxSearchTool::Case | QtxSearchTool::Wrap, 
-                                  Qt::Horizontal );
+#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
+                                   QtxSearchTool::Basic | QtxSearchTool::Case | QtxSearchTool::Wrap,
+#else
+                                   QtxSearchTool::Basic | QtxSearchTool::Case,
+#endif                             
+                                   Qt::Horizontal );
   myFindPanel->setFrameStyle( QFrame::NoFrame | QFrame::Plain );
   myFindPanel->setActivators( QtxSearchTool::SlashKey );
   myFindPanel->setSearcher( new Searcher( myWebView ) );
   myFindPanel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
 
   QToolBar* toolbar = addToolBar( tr( "Navigation" ) );
-  toolbar->addAction( myWebView->pageAction( QWebPage::Back ) );
-  toolbar->addAction( myWebView->pageAction( QWebPage::Forward ) );
-  toolbar->addAction( myWebView->pageAction( QWebPage::Reload ) );
+  toolbar->addAction( myWebView->pageAction( WebPage::Back ) );
+  toolbar->addAction( myWebView->pageAction( WebPage::Forward ) );
+  toolbar->addAction( myWebView->pageAction( WebPage::Reload ) );
 
   QMenu* fileMenu = menuBar()->addMenu( tr( "&File" ) );
   fileMenu->addAction( QPixmap( ":/images/open.png" ), tr( "&Open..." ), 
-                      this, SLOT( open() ),
-                      QKeySequence( QKeySequence::Open ) );
+                       this, SLOT( open() ),
+                       QKeySequence( QKeySequence::Open ) );
   fileMenu->addSeparator();
-  fileMenu->addAction( myWebView->pageAction( QWebPage::Back ) );
-  fileMenu->addAction( myWebView->pageAction( QWebPage::Forward ) );
-  fileMenu->addAction( myWebView->pageAction( QWebPage::Reload ) );
+  fileMenu->addAction( myWebView->pageAction( WebPage::Back ) );
+  fileMenu->addAction( myWebView->pageAction( WebPage::Forward ) );
+  fileMenu->addAction( myWebView->pageAction( WebPage::Reload ) );
   fileMenu->addSeparator();
   fileMenu->addAction( tr( "&Find in text..." ),
-                      myFindPanel, SLOT( find() ),
-                      QKeySequence( QKeySequence::Find ) );
+                       myFindPanel, SLOT( find() ),
+                       QKeySequence( QKeySequence::Find ) );
   fileMenu->addAction( tr( "&Find next" ),
-                      myFindPanel, SLOT( findNext() ),
-                      QKeySequence( QKeySequence::FindNext ) );
+                       myFindPanel, SLOT( findNext() ),
+                       QKeySequence( QKeySequence::FindNext ) );
   fileMenu->addAction( tr( "&Find previous" ),
-                      myFindPanel, SLOT( findPrevious() ),
-                      QKeySequence( QKeySequence::FindPrevious ) );
+                       myFindPanel, SLOT( findPrevious() ),
+                       QKeySequence( QKeySequence::FindPrevious ) );
   fileMenu->addSeparator();
   fileMenu->addAction( QPixmap( ":/images/close.png" ), tr( "&Close" ),
-                      this, SLOT( close() ) );
+                       this, SLOT( close() ) );
 
   QMenu* helpMenu = menuBar()->addMenu( tr( "&Help" ) );
   helpMenu->addAction( tr( "&About..." ),
-                      this, SLOT( about() ) );
+                       this, SLOT( about() ) );
   
   QVBoxLayout* main = new QVBoxLayout( frame );
   main->addWidget( myWebView );
@@ -390,16 +419,26 @@ QtxWebBrowser::QtxWebBrowser( ) : QMainWindow( 0 )
   main->setSpacing( 3 );
 
   connect( myWebView, SIGNAL( titleChanged( QString ) ), SLOT( adjustTitle() ) ); 
-  connect( myWebView, SIGNAL( loadFinished( bool ) ),    SLOT( finished( bool ) ) ); 
-  connect( myWebView, SIGNAL( linkClicked( QUrl ) ),     SLOT( linkClicked( QUrl ) ) ); 
-  connect( myWebView->page(), SIGNAL( linkHovered( QString, QString, QString ) ), 
-          SLOT( linkHovered( QString, QString, QString ) ) ); 
-  connect( myWebView->pageAction( QWebPage::DownloadLinkToDisk ), SIGNAL( triggered() ),
-          SLOT( linkAction() ) );
-  disconnect( myWebView->pageAction( QWebPage::OpenLink ), 0, 0, 0 );
-  connect( myWebView->pageAction( QWebPage::OpenLink ), SIGNAL( triggered() ),
-          SLOT( linkAction() ) );
+  connect( myWebView, SIGNAL( loadFinished( bool ) ),    SLOT( finished( bool ) ) );
   
+  connect( myWebView->pageAction( WebPage::DownloadLinkToDisk ), SIGNAL( triggered() ),
+           SLOT( linkAction() ) );
+#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
+  //QtWebKit case:
+  connect( myWebView, SIGNAL( linkClicked( QUrl ) ),     SLOT( linkClicked( QUrl ) ) );
+  connect( myWebView->page(), SIGNAL( linkHovered( QString, QString, QString ) ), 
+           SLOT( linkHovered( QString, QString, QString ) ) ); 
+  disconnect( myWebView->pageAction( WebPage::OpenLink ), 0, 0, 0 );
+  connect( myWebView->pageAction( WebPage::OpenLink ), SIGNAL( triggered() ),
+           SLOT( linkAction() ) );
+#else
+  //QtWebEngine (Qt-5.6.0) case:
+  connect( myWebView->page(), SIGNAL( linkHovered( QString ) ), 
+           SLOT( linkHovered( QString ) ) );
+  disconnect( myWebView->pageAction( WebPage::OpenLinkInThisWindow ), 0, 0, 0 );
+  connect( myWebView->pageAction( WebPage::OpenLinkInThisWindow ), SIGNAL( triggered() ),
+           SLOT( linkAction() ) );
+#endif  
   setCentralWidget( frame );
   setFocusProxy( myWebView );
   setWindowIcon( QPixmap( ":/images/appicon.png" ) );
@@ -478,7 +517,7 @@ QtxResourceMgr* QtxWebBrowser::resourceMgr() const
 void QtxWebBrowser::about()
 {
   QMessageBox::about( this, tr( "About %1" ).arg( tr( "Help Browser" ) ),
-                     QString( "SALOME %1" ).arg( tr( "Help Browser" ) ) );
+                      QString( "SALOME %1" ).arg( tr( "Help Browser" ) ) );
 }
 
 /*!
@@ -488,9 +527,13 @@ void QtxWebBrowser::about()
 */
 void QtxWebBrowser::linkClicked( const QUrl& url )
 {
-  myWebView->page()->setLinkDelegationPolicy( QWebPage::DontDelegateLinks );
+#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
+  myWebView->page()->setLinkDelegationPolicy( WebPage::DontDelegateLinks );
+#endif  
   myWebView->load( url );
-  myWebView->page()->setLinkDelegationPolicy( QWebPage::DelegateAllLinks );
+#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)  
+  myWebView->page()->setLinkDelegationPolicy( WebPage::DelegateAllLinks );
+#endif  
 }
 
 /*!
@@ -500,11 +543,17 @@ void QtxWebBrowser::linkClicked( const QUrl& url )
   \param content provides text within the link element, e.g., text inside an HTML anchor tag
   \internal
 */
+
 void QtxWebBrowser::linkHovered( const QString& link, const QString& /*title*/, const QString& /*context*/ )
+{
+  linkHovered(link);
+}
+
+void QtxWebBrowser::linkHovered( const QString& link)
 {
   QUrl url = link;
   if ( !link.isEmpty() && isLocalFile( url ) ) myLastUrl = url;
-  statusBar()->showMessage( link );
+  statusBar()->showMessage( link );  
 }
 
 /*!
@@ -540,10 +589,14 @@ void QtxWebBrowser::finished( bool ok )
 void QtxWebBrowser::linkAction()
 {
   QObject* s = sender();
-  if ( s == myWebView->pageAction( QWebPage::DownloadLinkToDisk ) ) {
+  if ( s == myWebView->pageAction( WebPage::DownloadLinkToDisk ) ) {
     saveLink( myLastUrl.path() );
   }
-  else if ( s == myWebView->pageAction( QWebPage::OpenLink ) ) {
+#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
+  if ( s == myWebView->pageAction( WebPage::OpenLink ) ) {
+#else
+  if ( s == myWebView->pageAction( WebPage::OpenLinkInThisWindow ) ) {  
+#endif    
     QString fileName  = myLastUrl.path();
     QString extension = QFileInfo( fileName ).suffix();
     if ( extension != "html" && extension != "htm" ) {
@@ -587,7 +640,7 @@ void QtxWebBrowser::openLink( const QString& fileName, bool force )
       myResourceMgr->setValue( resSection, actionParam, defAction );
       myResourceMgr->setValue( resSection, repeatParam, defRepeat );
       if ( defAction == Downloader::mOpen )
-       myResourceMgr->setValue( resSection, programParam, defProgram );
+        myResourceMgr->setValue( resSection, programParam, defProgram );
     }
   }
   switch( defAction ) {
@@ -640,12 +693,13 @@ void QtxWebBrowser::load( const QString& link )
 void QtxWebBrowser::saveLink( const QString& fileName )
 {
   QString newFileName = QFileDialog::getSaveFileName( this, tr( "Save file" ), fileName, 
-                                                     QString( "*.%1" ).arg( QFileInfo( fileName ).suffix() ) );
+                                                      QString( "*.%1" ).arg( QFileInfo( fileName ).suffix() ) );
   if ( !newFileName.isEmpty() && 
        QFileInfo( newFileName ).canonicalFilePath() != QFileInfo( fileName ).canonicalFilePath() ) {
     QFile toFile( newFileName );
     QFile fromFile( fileName );
-    if ( toFile.exists() && !toFile.remove() || !fromFile.copy( newFileName ) )
+    if (( toFile.exists() && !toFile.remove() ) ||
+        ( !fromFile.copy( newFileName ) ))
       QMessageBox::warning( this, tr( "Error"), tr( "Can't save file:\n%1" ).arg( newFileName ) );
   }
 }