Salome HOME
Fix linkage problem on Windows
[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   // create application instance
111
112   QtSingleApplication instance( argc, argv );
113
114   // parse command line arguments
115
116   bool showHelp = false;
117   QString language;
118   QString helpfile;
119
120   QRegExp rl( "--language=(.+)" );
121   rl.setMinimal( false );
122
123   for ( int a = 1; a < argc; ++a ) {
124     QString param = argv[a];
125     if ( param == "--help" || param == "-h" )
126       showHelp = true;
127     else if ( rl.exactMatch( param ) )
128       language = rl.cap( 1 );
129     else
130       helpfile = param;
131   }
132
133   // show help and exit if --help or -h option has been specified via command line
134
135   if ( showHelp ) {
136     printHelp();
137     exit( 0 );
138   }
139
140   if ( instance.sendMessage( helpfile ) )
141     return 0;
142
143   // load translations
144
145   QtxTranslator tqt, tsal;
146   if ( !language.isEmpty() ) {
147     if ( tqt.load( QString( "qt_%1" ).arg( language ), QLibraryInfo::location( QLibraryInfo::TranslationsPath ) ) )
148       instance.installTranslator( &tqt );
149
150     QDir appDir = QtSingleApplication::applicationDirPath();
151     appDir.cdUp(); appDir.cdUp(); 
152     
153     if ( tsal.load( QString( "Qtx_msg_%1" ).arg( language ), appDir.filePath( "share/salome/resources/gui" ) ) )
154       instance.installTranslator( &tsal );
155   }
156
157   // initialize resource manager (for preferences)
158
159   QtxResourceMgr* resMgr = new QtxResourceMgr( "HelpBrowser", "%1Config" );
160   resMgr->setCurrentFormat( "xml" );
161   QtxWebBrowser::setResourceMgr( resMgr );
162
163   // show main window
164
165   HelpBrowser browser;
166   browser.show();
167
168   // load file specified via command line
169
170   if ( helpfile.isEmpty() ) {
171     QString docdir = qgetenv( "DOCUMENTATION_ROOT_DIR" );
172     if ( !docdir.isEmpty() )
173       helpfile = QDir::toNativeSeparators( QString( "%1/index.html" ).arg( docdir ) );
174   }
175
176   if ( !helpfile.isEmpty() ) {
177     browser.load( helpfile );
178   }
179
180   // finalize main window activation
181
182   instance.setActivationWindow( &browser );
183   
184   QObject::connect( &instance, SIGNAL( messageReceived( QString ) ),
185                     &browser,  SLOT( load ( QString ) ) );
186
187   return instance.exec();
188 }