Salome HOME
5237e75ececd44bb18604c4f65be9fc3311cb422
[modules/kernel.git] / src / SALOMEGUI / QAD_HelpWindow.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : QAD_HelpWindow.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 /*!
31   \class QAD_HelpWindow QAD_HelpWindow.h
32   \brief Html browser for help.
33 */
34
35 #include "QAD_HelpWindow.h"
36 #include "QAD_ResourceMgr.h"
37 #include "QAD_Desktop.h"
38 #include "QAD_Application.h"
39 #include "QAD_Tools.h"
40
41 // QT Includes
42 #include <qapplication.h>
43 #include <qtoolbar.h>
44 #include <qmenubar.h>
45 #include <qiconset.h>
46 #include <qtoolbutton.h>
47 #include <qcombobox.h>
48 #include <qfileinfo.h>
49
50 /*!
51   Constructor
52 */
53 QAD_HelpWindow::QAD_HelpWindow()
54   : QMainWindow( 0, "SALOME Professional Help", WStyle_Customize | WType_TopLevel | WDestructiveClose ),
55     pathCombo( 0 ), selectedURL()
56 {
57   QAD_ResourceMgr* rmgr = QAD_Desktop::getResourceManager();
58
59   // alternative palette
60   setPalette( QAD_Application::getPalette(true) );
61   // caption
62   setCaption( tr("SALOMEGUI_HELP_TITLE") );
63   // icon
64   QPixmap icon ( rmgr->loadPixmap( "CLIENT", tr("ICON_DESK_DEFAULTICON") ) );
65   if ( !icon.isNull() ) setIcon( icon );
66
67   // create browser
68   browser = new QTextBrowser( this );
69   browser->setFrameStyle( QFrame::Panel | QFrame::Sunken );
70   connect( browser, SIGNAL( textChanged() ), this, SLOT( textChanged() ) );
71   setCentralWidget( browser );
72
73   // menu/toolbar actions
74   menuBar()->setItemEnabled( forwardId, FALSE);
75   menuBar()->setItemEnabled( backwardId, FALSE);
76   connect( browser, SIGNAL( backwardAvailable( bool ) ),
77            this, SLOT( setBackwardAvailable( bool ) ) );
78   connect( browser, SIGNAL( forwardAvailable( bool ) ),
79            this, SLOT( setForwardAvailable( bool ) ) );
80   
81   QIconSet icon_back( QPixmap(rmgr->loadPixmap("QAD", tr("ICON_APP_HELP_BACK")) ));
82   QIconSet icon_forward( QPixmap(rmgr->loadPixmap("QAD", tr("ICON_APP_HELP_FORWARD")) ));
83   QIconSet icon_home( QPixmap(rmgr->loadPixmap("QAD", tr("ICON_APP_HELP_HOME")) ));
84   
85   QToolBar* toolbar = new QToolBar( this );
86   addToolBar( toolbar, "Toolbar");
87   QToolButton* button;
88   
89   button = new QToolButton( icon_back, tr("TOT_HELPWINDOW_BACKWARD"), "", browser, SLOT(backward()), toolbar );
90   connect( browser, SIGNAL( backwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
91   button->setEnabled( FALSE );
92   button = new QToolButton( icon_forward, tr("TOT_HELPWINDOW_FORWARD"), "", browser, SLOT(forward()), toolbar );
93   connect( browser, SIGNAL( forwardAvailable(bool) ), button, SLOT( setEnabled(bool) ) );
94   button->setEnabled( FALSE );
95   button = new QToolButton( icon_home, tr("TOT_HELPWINDOW_HOME"), "", browser, SLOT(home()), toolbar );
96   
97   toolbar->addSeparator();
98   
99   pathCombo = new QComboBox( TRUE, toolbar );
100   pathCombo->setInsertionPolicy(QComboBox::NoInsertion);
101   pathCombo->setDuplicatesEnabled(false);
102   connect( pathCombo, SIGNAL( activated( const QString & ) ),
103            this, SLOT( pathSelected( const QString & ) ) );
104   toolbar->setStretchableWidget( pathCombo );
105   setRightJustification( TRUE );
106   setDockEnabled( Left, FALSE );
107   setDockEnabled( Right, FALSE );
108   setDockEnabled( Bottom, FALSE );
109   setDockEnabled( TornOff, FALSE );
110   setDockMenuEnabled( false );
111
112   // look for index.html and set homeDir
113   // 1. $(SALOME_ROOT_DIR)/doc/index.html
114   // 2. $(SALOME_ROOT_DIR)/doc/html/index.html
115   // 3. $(SALOME_ROOT_DIR)/doc/html/html/index.html
116   // 4. /usr/local/doc/html/index.html
117
118   QCString dir;
119   QString root;
120   if ( (dir = getenv("SALOME_ROOT_DIR")) ) {
121     root = QAD_Tools::addSlash( QAD_Tools::addSlash(dir) + "doc" );
122     if ( QFileInfo( root + "index.html" ).exists() ) {
123       homeDir = root;
124     }
125     else {
126       root = QAD_Tools::addSlash( root + "html" );
127       if ( QFileInfo( root + "index.html" ).exists() ) {
128         homeDir = root;
129       }
130       else {
131         root = QAD_Tools::addSlash( root + "html" );
132         if ( QFileInfo( root + "index.html" ).exists() ) {
133           homeDir = root;
134         }
135       }
136     }
137   }
138   if ( root.isEmpty() ) {
139     if ( QFileInfo( "/usr/local/doc/html/index.html" ).exists() ) {
140       homeDir = "/usr/local/doc/html/";
141     }
142   }
143   if ( root.isEmpty() ) 
144     root = "./doc/";
145
146   browser->setSource( QFileInfo( homeDir + "index.html" ).absFilePath() );
147   browser->setFocus();
148
149   QSize dSize = QApplication::desktop()->size();
150   int x = ( dSize.width()  - 750 ) / 2;
151   int y = ( dSize.height() - 680 ) / 2;
152   setGeometry( x, y, 750, 680 );
153 }
154
155 /*!
156   Destructor
157 */
158 QAD_HelpWindow::~QAD_HelpWindow()
159 {
160   emit(helpWindowClosed());
161 }
162
163 /*!
164   enables/disables <Backward> button
165 */
166 void QAD_HelpWindow::setBackwardAvailable( bool b)
167 {
168   menuBar()->setItemEnabled( backwardId, b);
169 }
170
171 /*!
172   enables/disables <Forward> button
173   */
174 void QAD_HelpWindow::setForwardAvailable( bool b)
175 {
176   menuBar()->setItemEnabled( forwardId, b);
177 }
178
179 /*!
180   called when new document is loaded into browser
181 */
182 void QAD_HelpWindow::textChanged()
183 {
184   if ( browser->documentTitle().isNull() && browser->context().isNull() && browser->source().isNull() )
185     browser->clear();
186   selectedURL = browser->source(); //caption();
187   if ( !selectedURL.isEmpty() && pathCombo ) {
188     bool exists = FALSE;
189     int i;
190     for ( i = 0; i < pathCombo->count(); ++i ) {
191       if ( pathCombo->text( i ) == selectedURL ) {
192         exists = TRUE;
193         break;
194       }
195     }
196     if ( !exists ) {
197       pathCombo->insertItem( selectedURL, 0 );
198       pathCombo->setCurrentItem( 0 );
199     } else {
200       pathCombo->setCurrentItem( i );
201     }
202     selectedURL = QString::null;
203   }
204 }
205
206 /*!
207   called when users selectes dosument from the combo box
208 */
209 void QAD_HelpWindow::pathSelected( const QString& _path )
210 {
211   browser->setSource( _path );
212 }
213
214 /*!
215   goes back to the contents
216 */
217 void QAD_HelpWindow::contents()
218 {
219   browser->home();
220 }
221
222 /*!
223   opens new document in browser window and makes context search
224   <_source>  is a filename, can be relative, if empty, index.html name is used
225   <_context> is a context, e.g. link in the file
226 */
227 void QAD_HelpWindow::context( const QString& _source, const QString& _context)
228 {
229   QString src;
230   if ( _source.isEmpty() ) {
231     src = homeDir + "index.html";
232   }
233   else {
234     QFileInfo fi( _source );
235     if ( fi.isRelative() )
236       src = homeDir + _source;
237     else
238       src = _source;
239   }
240   if ( !_context.isEmpty() )
241     src += "#" + _context;
242   browser->setSource( src );
243 }