From ab8aaeec125324f0ce8432b36e0b394f37b829bf Mon Sep 17 00:00:00 2001 From: vsr Date: Sun, 16 Oct 2011 16:54:45 +0000 Subject: [PATCH] Issue 0020512: EDF 1113 OTHER : Using webkit to display documentation --- src/LightApp/LightApp_Application.cxx | 10 +- src/LightApp/resources/LightApp_images.ts | 4 + src/LightApp/resources/LightApp_msg_en.ts | 8 +- src/LightApp/resources/LightApp_msg_fr.ts | 6 +- src/Qtx/QtxWebBrowser.cxx | 212 +++++++++++++--------- src/Qtx/QtxWebBrowser.h | 29 ++- 6 files changed, 165 insertions(+), 104 deletions(-) diff --git a/src/LightApp/LightApp_Application.cxx b/src/LightApp/LightApp_Application.cxx index 8e2df790b..be59d31e4 100644 --- a/src/LightApp/LightApp_Application.cxx +++ b/src/LightApp/LightApp_Application.cxx @@ -270,10 +270,12 @@ LightApp_Application::LightApp_Application() SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr(); QPixmap aLogo = aResMgr->loadPixmap( "LightApp", tr( "APP_DEFAULT_ICO" ), false ); - QtxWebBrowser::setData("BROWSER_ICON", aResMgr->loadPixmap( "LightApp", tr( "BROWSER_ICON" ))); - QtxWebBrowser::setData("BROWSER_TITLE", tr("BROWSER_TITLE")); - QtxWebBrowser::setData("BROWSER_FILEMENU", tr("BROWSER_FILEMENU")); - QtxWebBrowser::setData("BROWSER_EXIT", tr("BROWSER_EXIT")); + QtxWebBrowser::setData("browser:icon", aResMgr->loadPixmap( "LightApp", tr( "BROWSER_ICON" ) ) ); + QtxWebBrowser::setData("browser:title", tr( "BROWSER_TITLE" ) ); + QtxWebBrowser::setData("toolbar:title", tr( "BROWSER_TOOLBAR_TITLE" ) ); + QtxWebBrowser::setData("menu:file:title", tr( "BROWSER_FILEMENU" ) ); + QtxWebBrowser::setData("action:close:title", tr( "BROWSER_CLOSE" ) ); + QtxWebBrowser::setData("action:close:icon", aResMgr->loadPixmap( "LightApp", tr( "BROWSER_CLOSE_ICON" ) ) ); desktop()->setWindowIcon( aLogo ); desktop()->setDockableMenuBar( false ); diff --git a/src/LightApp/resources/LightApp_images.ts b/src/LightApp/resources/LightApp_images.ts index 40c6ee93b..dae481992 100644 --- a/src/LightApp/resources/LightApp_images.ts +++ b/src/LightApp/resources/LightApp_images.ts @@ -35,5 +35,9 @@ BROWSER_ICON icon_default.png + + BROWSER_CLOSE_ICON + close.png + diff --git a/src/LightApp/resources/LightApp_msg_en.ts b/src/LightApp/resources/LightApp_msg_en.ts index 14dbc1b0d..70ebab37e 100644 --- a/src/LightApp/resources/LightApp_msg_en.ts +++ b/src/LightApp/resources/LightApp_msg_en.ts @@ -54,13 +54,17 @@ CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITASBROWSER_TITLE SALOME Help Browser + + BROWSER_TOOLBAR_TITLE + Navigation + BROWSER_FILEMENU &File - BROWSER_EXIT - &Exit + BROWSER_CLOSE + &Close diff --git a/src/LightApp/resources/LightApp_msg_fr.ts b/src/LightApp/resources/LightApp_msg_fr.ts index c827a5ae4..c46785564 100755 --- a/src/LightApp/resources/LightApp_msg_fr.ts +++ b/src/LightApp/resources/LightApp_msg_fr.ts @@ -54,13 +54,17 @@ CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITASBROWSER_TITLE Aide de SALOME + + BROWSER_TOOLBAR_TITLE + Navigation + BROWSER_FILEMENU &Fichier BROWSER_EXIT - &Quitter + &Fermer diff --git a/src/Qtx/QtxWebBrowser.cxx b/src/Qtx/QtxWebBrowser.cxx index 0c4286ca0..79a2fa983 100644 --- a/src/Qtx/QtxWebBrowser.cxx +++ b/src/Qtx/QtxWebBrowser.cxx @@ -34,91 +34,70 @@ //! The only one instance of web browser QtxWebBrowser* QtxWebBrowser::myBrowser = 0; -//!Internal data map to store resources of the browser. +//! Internal data map to store resources of the browser. QMap QtxWebBrowser::myData; /*! \class QtxWebBrowser - \brief The QtxWebBrowser provides a window that can display a html pages. + \brief The QtxWebBrowser provides a window that can display html pages. Only one instance of the QtxWebBrowser class can be created. To access the browser window, use static method QtxWebBrowser::webBrowser(), which creates an - instance of the QtxWebBrowser widget (if it is not yet creaed) and returns a + instance of the QtxWebBrowser widget (if it is not yet created) and returns a pointer to it. You should not destroy this instance - it is done automatically after - closing of the browser window. To closing window programmatically use + closing of the browser window. To close window programmatically use method close(). To set visual properties of the browser use static method setData(). - Following code demonstrate how to use web browser: + The following sample demonstrates how to use web browser. + In this code the browser window is created, /data/index.html file is opened + and scrolled to the "anchor1" anchor on this page. \code int main(int argc, char *argv[]) { QApplication app(argc, argv); - //Set icon, title and menu items. - QtxWebBrowser::setData("BROWSER_TITLE", tr("Web Browser")); + // set icon, title and menu items. + QtxWebBrowser::setData("browser:title", tr("Web Browser")); + QtxWebBrowser::setData("browser:icon", QPixmap(":/icon.png")); + QtxWebBrowser::setData("menu:file:title", tr("&File")); + QtxWebBrowser::setData("action:close:title", tr("&Close")); - QPixmap pixmap(":/icon.png"); - QtxWebBrowser::setData("BROWSER_ICON", pixmap); - - QtxWebBrowser::setData("BROWSER_FILEMENU", tr("&File")); - QtxWebBrowser::setData("BROWSER_EXIT", tr("&Exit")); - - - //Show web page + // show HTML page QtxWebBrowser::loadUrl("file:///data/index.html", "anchor1"); return app.exec(); } - \endcode - This code will create the browser window and open /data/index.html - file in "anchor1" context. */ /*! \brief Constructor. - + Construct the web browser. */ -QtxWebBrowser::QtxWebBrowser() -: QMainWindow(0) +QtxWebBrowser::QtxWebBrowser() : QMainWindow( 0 ) { setAttribute( Qt::WA_DeleteOnClose ); myWebView = new QWebView(this); - QToolBar *toolBar = addToolBar(tr("Navigation")); - toolBar->addAction(myWebView->pageAction(QWebPage::Back)); - toolBar->addAction(myWebView->pageAction(QWebPage::Forward)); + myToolbar = addToolBar( tr( "Navigation" ) ); + myToolbar->addAction( myWebView->pageAction( QWebPage::Back ) ); + myToolbar->addAction( myWebView->pageAction( QWebPage::Forward ) ); - QString title = getStringValue("BROWSER_TITLE"); - if(!title.isEmpty()) - setWindowTitle(title); - - QIcon icon = getIconValue("BROWSER_ICON"); - if(!icon.isNull()) - setWindowIcon(icon); + myMenus[ File ] = menuBar()->addMenu( tr( "&File" ) ); + myActions[ Close ] = myMenus[ File ]->addAction( tr( "&Close" ), this, SLOT( close() ) ); - QString fmenu = getStringValue("BROWSER_FILEMENU"); - if ( fmenu.isEmpty() ) { - fmenu = tr("&File"); - } - QMenu *menu = menuBar()->addMenu(fmenu); - - QString item = getStringValue("BROWSER_EXIT"); - if ( item.isEmpty() ) { - item = tr("&Exit"); - } - menu->addAction( item, this, SLOT(close())); - - connect(myWebView, SIGNAL(titleChanged(QString)), SLOT(adjustTitle())); - setCentralWidget(myWebView); + connect( myWebView, SIGNAL( titleChanged( QString ) ), SLOT( adjustTitle() ) ); + setCentralWidget( myWebView ); + + updateData(); } /*! @@ -130,28 +109,30 @@ QtxWebBrowser::~QtxWebBrowser() } /*! - \brief Create instance of the QtxWebBrowser. - \return instance of the QtxWebBrowser. + \brief Return the only instance of the QtxWebBrowser + \return instance of the QtxWebBrowser */ -QtxWebBrowser* QtxWebBrowser::webBrowser() { - if( myBrowser == 0 ) +QtxWebBrowser* QtxWebBrowser::webBrowser() +{ + if ( !myBrowser ) myBrowser = new QtxWebBrowser(); return myBrowser; } /*! - \brief Load given url addres and context. - \param theUlr url address to load. - \param theContext url context to load. + \brief Load given url address and optional scroll to the specified anchor + \param url an url address to load + \param anchor an anchor to scroll page to */ -void QtxWebBrowser::loadUrl(const QString& theUrl , const QString theContext ) { - QString anUrl = theUrl; - if( !theContext.isEmpty() ) - anUrl += "#" + theContext; +void QtxWebBrowser::loadUrl( const QString& url, const QString& anchor ) +{ + QString anUrl = url; + if( !anchor.isEmpty() ) anUrl += "#" + anchor; + + Qtx::alignWidget( webBrowser(), (QWidget*)QApplication::desktop(), Qtx::AlignCenter ); - Qtx::alignWidget(webBrowser(), (QWidget*)QApplication::desktop(),Qtx::AlignHCenter); webBrowser()->show(); - webBrowser()->myWebView->load(QUrl(anUrl)); + webBrowser()->myWebView->load( QUrl( anUrl ) ); webBrowser()->setFocus(); webBrowser()->activateWindow(); webBrowser()->raise(); @@ -160,59 +141,116 @@ void QtxWebBrowser::loadUrl(const QString& theUrl , const QString theContext ) { /*! \brief Set browser settings from. - This method can be used to setup the browser look-n-feel. - - All the browser parameters can be defined via resources file: - - \c "BROWSER_TITLE" : title of the browser - - \c "BROWSER_ICON" : icon of the browser - - \c "BROWSER_FILEMENU" : name of the browser menu - - \c "BROWSER_EXIT" : name of the browser menu item + This method can be used to setup the browser properties. + - \c "browser:title" : title of the browser window + - \c "browser:icon" : icon of the browser window + - \c "toolbar:title" : title of the toolbar + - \c "menu:file:title" : File menu of the browser + - \c "action:close:title" : File/Close menu item title + - \c "action:close:icon" : File/Close menu item icon + - \c "action:back:title" : Navigation/Back menu item title + - \c "action:back:icon" : Navigation/Back menu item icon + - \c "action:forward:title" : Navigation/Forward menu item title + - \c "action:forward:icon" : Navigation/Forward menu item icon \param key name of the property \param val value of the property */ -void QtxWebBrowser::setData( const QString& key, const QVariant& val ) { - myData.insert(key, val); +void QtxWebBrowser::setData( const QString& key, const QVariant& val ) +{ + myData.insert( key, val ); + if ( myBrowser ) myBrowser->updateData(); } /*! - \brief Get string by name from the internal data map. + \brief Get string value by key from the internal data map + \param key data key identifier + \return string value assigned to the key (null string if data is not assigned to the key) \internal */ -QString QtxWebBrowser::getStringValue(const QString& key) { +QString QtxWebBrowser::getStringValue( const QString& key ) +{ QString val; - if(myData.contains(key)) { - QVariant v = myData[key]; - if( v.type() == QVariant::String ) { - val = v.toString(); - } - } + if ( myData.contains( key ) && myData[key].canConvert( QVariant::String ) ) + val = myData[key].toString(); return val; } /*! - \brief Get icon by name from the internal data map. + \brief Get icon value by key from the internal data map + \param key data key identifier + \return icon assigned to the key (null icon if data is not assigned to the key) \internal */ -QIcon QtxWebBrowser::getIconValue(const QString& key) { +QIcon QtxWebBrowser::getIconValue(const QString& key) +{ QIcon val; - if(myData.contains(key)) { - QVariant v = myData[key]; - if( v.type() == QVariant::Pixmap ) { - val = v.value(); - } - } + if ( myData.contains( key ) ) { + if ( myData[key].canConvert( QVariant::Pixmap ) ) + val = myData[key].value(); + else if ( myData[key].canConvert( QVariant::Icon ) ) + val = myData[key].value(); + } return val; } /*! - \brief Update title of the window. + \brief Update web browser properties from internal data map +*/ +void QtxWebBrowser::updateData() +{ + // main title + adjustTitle(); + + // window icon + QIcon icon = getIconValue( "browser:icon" ); + if ( !icon.isNull() ) + setWindowIcon( icon ); + + // toolbar title + QString tbTitle = getStringValue( "toolbar:title" ); + if ( myToolbar && !tbTitle.isEmpty() ) + myToolbar->setWindowTitle( tbTitle ); + + // File menu + QString fmenu = getStringValue( "menu:file:title" ); + if ( myMenus.contains( File ) && !fmenu.isEmpty() ) + myMenus[ File ]->setTitle( fmenu ); + + // File/Close menu + QString closeTlt = getStringValue( "action:close:title" ); + QIcon closeIco = getIconValue( "action:close:icon" ); + if ( myActions.contains( Close ) ) { + if ( !closeTlt.isEmpty() ) + myActions[ Close ]->setText( closeTlt ); + if ( !closeIco.isNull() ) + myActions[ Close ]->setIcon( closeIco ); + } + + // Navigation/Go Back menu + QString backTlt = getStringValue( "action:back:title" ); + QIcon backIco = getIconValue( "action:back:icon" ); + if ( !backTlt.isEmpty() ) + myWebView->pageAction( QWebPage::Back )->setText( backTlt ); + if ( !backIco.isNull() ) + myWebView->pageAction( QWebPage::Back )->setIcon( backIco ); + + // Navigation/Go Forward menu + QString fwdTlt = getStringValue( "action:forward:title" ); + QIcon fwdIco = getIconValue( "action:forward:icon" ); + if ( !fwdTlt.isEmpty() ) + myWebView->pageAction( QWebPage::Forward )->setText( fwdTlt ); + if ( !fwdIco.isNull() ) + myWebView->pageAction( QWebPage::Forward )->setIcon( fwdIco ); +} + +/*! + \brief Update title of the window \internal */ void QtxWebBrowser::adjustTitle() { - QString title = getStringValue("BROWSER_TITLE"); - title.isEmpty() ? title = myWebView->title() : title += " : " + myWebView->title(); - setWindowTitle(title); + QString title = getStringValue( "browser:title" ); + setWindowTitle( title.isEmpty() ? myWebView->title() : title + QString( " [%1]" ).arg( myWebView->title() ) ); } diff --git a/src/Qtx/QtxWebBrowser.h b/src/Qtx/QtxWebBrowser.h index e0b777ab4..ecdf82f0e 100644 --- a/src/Qtx/QtxWebBrowser.h +++ b/src/Qtx/QtxWebBrowser.h @@ -30,36 +30,45 @@ #include "Qtx.h" #include -#include #include +class QAction; +class QMenu; +class QToolBar; +class QWebView; class QTX_EXPORT QtxWebBrowser : public QMainWindow { Q_OBJECT + enum { File }; + enum { Close }; + private: QtxWebBrowser(); public: virtual ~QtxWebBrowser(); - static QtxWebBrowser* webBrowser(); - static void loadUrl( const QString& theUrl, const QString theContext = QString() ); - static void setData( const QString& key, const QVariant& val ); + static QtxWebBrowser* webBrowser(); + static void loadUrl( const QString&, const QString& = QString() ); + static void setData( const QString&, const QVariant& ); private: - QString getStringValue(const QString& key); - QIcon getIconValue(const QString& key); + static QString getStringValue( const QString& ); + static QIcon getIconValue( const QString& ); + void updateData(); private slots: - void adjustTitle(); + void adjustTitle(); private: - static QtxWebBrowser* myBrowser; - QWebView* myWebView; static QMap myData; - + static QtxWebBrowser* myBrowser; + QWebView* myWebView; + QToolBar* myToolbar; + QMap myMenus; + QMap myActions; }; #endif // QTXWEBBROWSER_H -- 2.39.2