Salome HOME
A revision of documentation changes in the new version. Very minor corrections.
[modules/gui.git] / src / HelpBrowser / HelpBrowser.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : HelpBrowser.cxx
23 // Author : Vadim SANDLER, OpenCASCADE S.A.S. (vadim.sandler@opencascade.com)
24
25 #include "qtsingleapplication.h"
26 #include <QDir>
27 #include <QFile>
28 #include <QFileInfo>
29 #include <QLibraryInfo>
30 #include <QMessageBox>
31 #include <QTextStream>
32 #include <QTimer>
33 #include <QtxResourceMgr.h>
34 #include <QtxTranslator.h>
35 #include <QtxWebBrowser.h>
36
37 #include <iostream>
38
39 namespace
40 {
41   void printHelp()
42   {
43     QFileInfo fi( QtSingleApplication::arguments().at(0) );
44
45     std::cout << std::endl;
46     std::cout << "SALOME Help Browser" << std::endl;
47     std::cout << std::endl;
48     std::cout << "usage: " << qPrintable( fi.fileName() ) << " [options] file" << std::endl;
49     std::cout << "    file is a help file to be opened" << std::endl;
50     std::cout << std::endl;
51     std::cout << "Options:" << std::endl;
52     std::cout << "-h, --help         Prints this help and quits." << std::endl;
53     std::cout << "--language=LANG    Use LANG language in menus." << std::endl;
54     std::cout << std::endl;
55   }
56 }
57
58 class RaiseWindowHelper: public QDialog
59 {
60 public:
61   RaiseWindowHelper( QWidget* parent ) : QDialog( parent, Qt::FramelessWindowHint )
62   {
63     setAttribute( Qt::WA_DeleteOnClose, true );
64     resize( 1, 1 );
65     show();
66     QTimer::singleShot( 100, this, SLOT( close() ) );
67     QTimer::singleShot( 500, parent, SLOT( setFocus() ) );
68   }
69 };
70
71 class HelpBrowser: public QtxWebBrowser
72 {
73 public:
74   HelpBrowser() : QtxWebBrowser()
75   {
76     setAttribute( Qt::WA_DeleteOnClose, false );
77   }
78   ~HelpBrowser()
79   {
80     if ( resourceMgr() )
81       resourceMgr()->save();
82   }
83   void about()
84   {
85     QStringList info;
86     QFile f( ":/COPYING" );
87     f.open( QIODevice::ReadOnly );
88     QTextStream in( &f );
89
90     info << QtxWebBrowser::tr( "%1 has been developed using %2" ).arg( QString( "SALOME %1").arg( tr( "Help Browser" ) ) ).arg( "Qt Solutions Component: Single Application." );
91     info << "";
92     info << in.readAll().split( "\n" );
93
94     QMessageBox::about( this, tr( "About %1" ).arg( tr( "Help Browser" ) ),
95                         info.join( "\n" ) );
96   }
97   void load( const QString& url )
98   {
99     QtxWebBrowser::load( url );
100     RaiseWindowHelper* helper = new RaiseWindowHelper( this );
101   }
102 };
103
104 int main( int argc, char **argv )
105 {
106   // set application name (for preferences)
107   
108   QtSingleApplication::setApplicationName( "salome" );
109
110   // specify application identifier via its name
111   QFileInfo fi( argv[0] );
112
113   // create application instance
114
115   QtSingleApplication instance( fi.fileName(), argc, argv );
116
117   // parse command line arguments
118
119   bool showHelp = false;
120   QString language;
121   QString helpfile;
122
123   QRegExp rl( "--language=(.+)" );
124   rl.setMinimal( false );
125
126   for ( int a = 1; a < argc; ++a ) {
127     QString param = argv[a];
128     if ( param == "--help" || param == "-h" )
129       showHelp = true;
130     else if ( rl.exactMatch( param ) )
131       language = rl.cap( 1 );
132     else
133       helpfile = param;
134   }
135
136   // show help and exit if --help or -h option has been specified via command line
137
138   if ( showHelp ) {
139     printHelp();
140     exit( 0 );
141   }
142
143   if ( instance.sendMessage( helpfile ) )
144     return 0;
145
146   // load translations
147
148   QtxTranslator tqt, tsal;
149   if ( !language.isEmpty() ) {
150     if ( tqt.load( QString( "qt_%1" ).arg( language ), QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
151       instance.installTranslator( &tqt );
152
153     QDir appDir = QtSingleApplication::applicationDirPath();
154     appDir.cdUp(); appDir.cdUp(); 
155     
156     if ( tsal.load( QString( "Qtx_msg_%1" ).arg( language ), appDir.filePath( "share/salome/resources/gui" ) ) )
157       instance.installTranslator( &tsal );
158   }
159
160   // initialize resource manager (for preferences)
161
162   QtxResourceMgr* resMgr = new QtxResourceMgr( "HelpBrowser", "%1Config" );
163   resMgr->setCurrentFormat( "xml" );
164   QtxWebBrowser::setResourceMgr( resMgr );
165
166   // show main window
167
168   HelpBrowser browser;
169   browser.show();
170
171   // load file specified via command line
172
173   if ( helpfile.isEmpty() ) {
174     QString docdir = qgetenv( "DOCUMENTATION_ROOT_DIR" );
175     if ( !docdir.isEmpty() )
176       helpfile = QDir::toNativeSeparators( QString( "%1/index.html" ).arg( docdir ) );
177   }
178
179   if ( !helpfile.isEmpty() ) {
180     browser.load( helpfile );
181   }
182
183   // finalize main window activation
184
185   instance.setActivationWindow( &browser );
186   
187   QObject::connect( &instance, SIGNAL( messageReceived( QString ) ),
188                     &browser,  SLOT( load ( QString ) ) );
189
190   return instance.exec();
191 }