Salome HOME
This commit was generated by cvs2git to create tag 'V1_3_0'.
[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   /*
113   // look for index.html and set homeDir
114   // 1. $(SALOME_ROOT_DIR)/doc/index.html
115   // 2. $(SALOME_ROOT_DIR)/doc/html/index.html
116   // 3. $(SALOME_ROOT_DIR)/doc/html/html/index.html
117   // 4. /usr/local/doc/html/index.html
118
119   QCString dir;
120   QString root;
121   if ( (dir = getenv("KERNEL_ROOT_DIR")) ) {
122   root = QAD_Tools::addSlash( QAD_Tools::addSlash(dir) + QAD_Tools::addSlash("share")  + QAD_Tools::addSlash("salome")  + "doc" );
123   if ( QFileInfo( root + "index.html" ).exists() ) {
124   homeDir = root;
125   }
126   else {
127   root = QAD_Tools::addSlash( root + "html" );
128   if ( QFileInfo( root + "index.html" ).exists() ) {
129   homeDir = root;
130   }
131   else {
132   root = QAD_Tools::addSlash( root + "html" );
133   if ( QFileInfo( root + "index.html" ).exists() ) {
134   homeDir = root;
135   }
136   }
137   }
138   }
139   if ( root.isEmpty() ) {
140   if ( QFileInfo( "/usr/local/doc/html/index.html" ).exists() ) {
141   homeDir = "/usr/local/doc/html/";
142   }
143   }
144   if ( root.isEmpty() ) 
145   root = "./doc/";
146   
147   browser->setSource( QFileInfo( homeDir + "index.html" ).absFilePath() );
148   */
149   browser->setFocus();
150
151   QSize dSize = QApplication::desktop()->size();
152   int x = ( dSize.width()  - 750 ) / 2;
153   int y = ( dSize.height() - 680 ) / 2;
154   setGeometry( x, y, 750, 680 );
155 }
156
157 /*!
158   Destructor
159 */
160 QAD_HelpWindow::~QAD_HelpWindow()
161 {
162   emit(helpWindowClosed());
163 }
164
165 /*!
166   enables/disables <Backward> button
167 */
168 void QAD_HelpWindow::setBackwardAvailable( bool b)
169 {
170   menuBar()->setItemEnabled( backwardId, b);
171 }
172
173 /*!
174   enables/disables <Forward> button
175   */
176 void QAD_HelpWindow::setForwardAvailable( bool b)
177 {
178   menuBar()->setItemEnabled( forwardId, b);
179 }
180
181 /*!
182   called when new document is loaded into browser
183 */
184 void QAD_HelpWindow::textChanged()
185 {
186   if ( browser->documentTitle().isNull() && browser->context().isNull() && browser->source().isNull() )
187     browser->clear();
188   selectedURL = browser->source(); //caption();
189   if ( !selectedURL.isEmpty() && pathCombo ) {
190     bool exists = FALSE;
191     int i;
192     for ( i = 0; i < pathCombo->count(); ++i ) {
193       if ( pathCombo->text( i ) == selectedURL ) {
194         exists = TRUE;
195         break;
196       }
197     }
198     if ( !exists ) {
199       pathCombo->insertItem( selectedURL, 0 );
200       pathCombo->setCurrentItem( 0 );
201     } else {
202       pathCombo->setCurrentItem( i );
203     }
204     selectedURL = QString::null;
205   }
206 }
207
208 /*!
209   called when users selectes dosument from the combo box
210 */
211 void QAD_HelpWindow::pathSelected( const QString& _path )
212 {
213   browser->setSource( _path );
214 }
215
216 /*!
217   goes back to the contents
218 */
219 void QAD_HelpWindow::contents()
220 {
221   browser->home();
222 }
223
224 /*!
225   opens new document in browser window and makes context search
226   <_source>  is a filename, can be relative, if empty, index.html name is used
227   <_context> is a context, e.g. link in the file
228 */
229 void QAD_HelpWindow::context( const QString& _source, const QString& _context)
230 {
231   QString src;
232   if ( _source.isEmpty() ) {
233     src = homeDir + "index.html";
234   }
235   else {
236     QFileInfo fi( _source );
237     if ( fi.isRelative() )
238       src = homeDir + _source;
239     else
240       src = _source;
241   }
242   if ( !_context.isEmpty() )
243     src += "#" + _context;
244   browser->setSource( src );
245 }