Salome HOME
Merge branch 'master' of salome:modules/gui
[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     Q_INIT_RESOURCE( Qtx );
77
78     setAttribute( Qt::WA_DeleteOnClose, false );
79   }
80   ~HelpBrowser()
81   {
82     if ( resourceMgr() )
83       resourceMgr()->save();
84   }
85   void about()
86   {
87     QStringList info;
88     QFile f( ":/COPYING" );
89     f.open( QIODevice::ReadOnly );
90     QTextStream in( &f );
91
92     info << QtxWebBrowser::tr( "%1 has been developed using %2" ).arg( QString( "SALOME %1").arg( tr( "Help Browser" ) ) ).arg( "Qt Solutions Component: Single Application." );
93     info << "";
94     info << in.readAll().split( "\n" );
95
96     QMessageBox::about( this, tr( "About %1" ).arg( tr( "Help Browser" ) ),
97                         info.join( "\n" ) );
98   }
99   void load( const QString& url )
100   {
101     QtxWebBrowser::load( url );
102     RaiseWindowHelper* helper = new RaiseWindowHelper( this );
103   }
104 };
105
106 int main( int argc, char **argv )
107 {
108   // set application name (for preferences)
109   
110   QtSingleApplication::setApplicationName( "salome" );
111
112   // create application instance
113
114   QtSingleApplication instance( argc, argv );
115
116   // parse command line arguments
117
118   bool showHelp = false;
119   QString language;
120   QString helpfile;
121
122   QRegExp rl( "--language=(.+)" );
123   rl.setMinimal( false );
124
125   for ( int a = 1; a < argc; ++a ) {
126     QString param = argv[a];
127     if ( param == "--help" || param == "-h" )
128       showHelp = true;
129     else if ( rl.exactMatch( param ) )
130       language = rl.cap( 1 );
131     else
132       helpfile = param;
133   }
134
135   // show help and exit if --help or -h option has been specified via command line
136
137   if ( showHelp ) {
138     printHelp();
139     exit( 0 );
140   }
141
142   if ( instance.sendMessage( helpfile ) )
143     return 0;
144
145   // load translations
146
147   QtxTranslator tqt, tsal;
148   if ( !language.isEmpty() ) {
149     if ( tqt.load( QString( "qt_%1" ).arg( language ), QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
150       instance.installTranslator( &tqt );
151
152     QDir appDir = QtSingleApplication::applicationDirPath();
153     appDir.cdUp(); appDir.cdUp(); 
154     
155     if ( tsal.load( QString( "Qtx_msg_%1" ).arg( language ), appDir.filePath( "share/salome/resources/gui" ) ) )
156       instance.installTranslator( &tsal );
157   }
158
159   // initialize resource manager (for preferences)
160
161   QtxResourceMgr* resMgr = new QtxResourceMgr( "HelpBrowser", "%1Config" );
162   resMgr->setCurrentFormat( "xml" );
163   QtxWebBrowser::setResourceMgr( resMgr );
164
165   // show main window
166
167   HelpBrowser browser;
168   browser.show();
169
170   // load file specified via command line
171
172   if ( helpfile.isEmpty() ) {
173     QString docdir = qgetenv( "DOCUMENTATION_ROOT_DIR" );
174     if ( !docdir.isEmpty() )
175       helpfile = QDir::toNativeSeparators( QString( "%1/index.html" ).arg( docdir ) );
176   }
177
178   if ( !helpfile.isEmpty() ) {
179     browser.load( helpfile );
180   }
181
182   // finalize main window activation
183
184   instance.setActivationWindow( &browser );
185   
186   QObject::connect( &instance, SIGNAL( messageReceived( QString ) ),
187                     &browser,  SLOT( load ( QString ) ) );
188
189   return instance.exec();
190 }