Salome HOME
823d09355ca235f1be9d4460911535d4686f7f23
[modules/gui.git] / src / CAF / CAF_Application.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "CAF_Application.h"
20
21 #include "CAF_Tools.h"
22 #include "CAF_Study.h"
23
24 #include <SUIT_Desktop.h>
25 #include <SUIT_Session.h>
26 #include <SUIT_ViewModel.h>
27 #include <SUIT_Operation.h>
28 #include <SUIT_MessageBox.h>
29 #include <SUIT_ResourceMgr.h>
30
31 #include <QtxListAction.h>
32
33 #include <qtoolbar.h>
34 #include <qmenubar.h>
35 #include <qpopupmenu.h>
36 #include <qstatusbar.h>
37 #include <qapplication.h>
38
39 #include <Resource_Manager.hxx>
40
41 #include <TColStd_SequenceOfExtendedString.hxx>
42
43 extern "C" CAF_EXPORT SUIT_Application* createApplication()
44 {
45   return new CAF_Application();
46 }
47
48 CAF_Application::CAF_Application()
49 : STD_Application()
50 {
51 }
52
53 CAF_Application::CAF_Application( const Handle(TDocStd_Application)& app )
54 : STD_Application(),
55 myStdApp( app )
56 {
57 }
58
59 CAF_Application::~CAF_Application()
60 {
61 }
62
63 QString CAF_Application::applicationName() const
64 {
65   return QString( "CAFApplication" );
66 }
67
68 Handle(TDocStd_Application) CAF_Application::stdApp() const
69 {
70   return myStdApp;
71 }
72
73 QString CAF_Application::getFileFilter() const
74 {
75   if ( stdApp().IsNull() )
76     return QString::null;
77
78   TColStd_SequenceOfExtendedString formats;
79   stdApp()->Formats( formats );
80
81   QStringList allWC;
82   QMap<QString, QStringList> wildCards;
83   Handle(Resource_Manager) resMgr = new Resource_Manager( stdApp()->ResourcesName() );
84   for ( int i = 1; i <= formats.Length(); i++ )
85   {
86     QString extension;
87     QString extResStr = CAF_Tools::toQString( formats.Value( i ) ) + QString( ".FileExtension" );
88     if ( resMgr->Find( (char*)extResStr.latin1() ) )
89       extension = QString( resMgr->Value( (char*)extResStr.latin1() ) );
90
91     QString descr;
92     QString descrResStr = CAF_Tools::toQString( formats.Value( i ) ) + QString( ".Description" );
93     if ( resMgr->Find( (char*)descrResStr.latin1() ) )
94       descr = QString( resMgr->Value( (char*)descrResStr.latin1() ) );
95
96     if ( !descr.isEmpty() && !extension.isEmpty() )
97     {
98       if ( !wildCards.contains( descr ) )
99         wildCards.insert( descr, QStringList() );
100       wildCards[descr].append( QString( "*.%1" ).arg( extension ) );
101       allWC.append( QString( "*.%1" ).arg( extension ) );
102     }
103   }
104
105   if ( wildCards.isEmpty() )
106     return QString::null;
107
108   QStringList filters;
109   for ( QMap<QString, QStringList>::ConstIterator it = wildCards.begin(); it != wildCards.end(); ++it )
110     filters.append( QString( "%1 (%2)" ).arg( it.key() ).arg( it.data().join( "; " ) ) );
111
112   if ( wildCards.count() > 1 )
113     filters.prepend( QString( "%1 (%2)" ).arg( tr( "INF_ALL_DOCUMENTS_FILTER" ) ).arg( allWC.join( "; " ) ) );
114
115   if ( !filters.isEmpty() )
116     filters.append( tr( "INF_ALL_FILTER" ) );
117
118   return filters.join( ";;" );
119 }
120
121 void CAF_Application::createActions()
122 {
123   STD_Application::createActions();
124
125   SUIT_Desktop* desk = desktop();
126   SUIT_ResourceMgr* resMgr = resourceMgr();
127
128   QtxListAction* editUndo =
129     new QtxListAction( tr( "TOT_APP_EDIT_UNDO" ), resMgr->loadPixmap( "CAF", tr( "ICON_APP_EDIT_UNDO" ) ),
130                                          tr( "MEN_APP_EDIT_UNDO" ), CTRL+Key_Z, desk );
131   registerAction( EditUndoId, editUndo );
132
133   QtxListAction* editRedo =
134     new QtxListAction( tr( "TOT_APP_EDIT_REDO" ), resMgr->loadPixmap( "CAF", tr( "ICON_APP_EDIT_REDO" ) ),
135                                          tr( "MEN_APP_EDIT_REDO" ), CTRL+Key_Y, desk );
136   registerAction( EditRedoId, editRedo );
137
138   editUndo->setComment( tr( "INF_APP_UNDOACTIONS" ) );
139   editRedo->setComment( tr( "INF_APP_REDOACTIONS" ) );
140
141   connect( editUndo, SIGNAL( activated( int ) ), this, SLOT( onUndo( int ) ) );
142   connect( editRedo, SIGNAL( activated( int ) ), this, SLOT( onRedo( int ) ) );
143
144
145   int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
146
147   createMenu( EditUndoId, editMenu, 0 );
148   createMenu( EditRedoId, editMenu, 0 );
149   createMenu( separator(), editMenu, -1, 0 );
150
151   int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
152
153   createTool( separator(), stdTBar );
154   createTool( EditUndoId, stdTBar );
155   createTool( EditRedoId, stdTBar );
156   createTool( separator(), stdTBar );
157 }
158
159 /*!
160     Undo operation on the given document. [ virtual protected ]
161 */
162 bool CAF_Application::undo( CAF_Study* doc )
163 {
164   bool success = false;
165   if ( doc )
166   {
167     if ( success = doc->undo() )
168       doc->update();
169   }
170   return success;
171 }
172
173 /*!
174     Redo operation on the given document. [ virtual protected ]
175 */
176 bool CAF_Application::redo(CAF_Study* doc)
177 {
178   bool success = false;
179   if ( doc )
180   {
181     if ( success = doc->redo() )
182       doc->update();
183   }
184   return success;
185 }
186
187 /*!
188     Undo operation on the active document. [ virtual protected slot ]
189 */
190 bool CAF_Application::onUndo( int numActions )
191 {
192   bool ok = true;
193   while ( numActions > 0 )
194   {
195           CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
196                 if ( cafStudy )
197     {
198             if ( !undo( cafStudy ) )
199                   {
200                           ok = false;
201                                 break;
202                         }
203                         numActions--;
204                 }
205   }
206   updateCommandsStatus();     /* enable/disable undo/redo */
207   return ok;
208 }
209
210 /*!
211     Redo operation on the active document. [ virtual protected slot ]
212 */
213 bool CAF_Application::onRedo( int numActions )
214 {
215   bool ok = true;
216   while ( numActions > 0 )
217   {
218           CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
219                 if ( cafStudy )
220     {
221                         if ( !redo( cafStudy ) )
222                         {
223               ok = false;
224                     break;
225                         }
226                         numActions--;
227                 }
228   }
229   updateCommandsStatus();     /* enable/disable undo/redo */
230   return ok;
231 }
232
233 /*!
234   Enables / disables the actions according to the application state. [ virtual protected ]
235 */
236 void CAF_Application::updateCommandsStatus()
237 {
238         STD_Application::updateCommandsStatus();
239
240   CAF_Study* cafStudy = 0;
241   if ( activeStudy() && activeStudy()->inherits( "CAF_Study" ) )
242     cafStudy = (CAF_Study*)activeStudy();
243
244   QAction* undo = action( EditUndoId );
245   if ( cafStudy && undo )
246     undo->setProperty( "names", cafStudy->undoNames() );
247
248   QAction* redo = action( EditRedoId );
249   if ( cafStudy && redo )
250     redo->setProperty( "names", cafStudy->redoNames() );
251
252   if ( undo )
253     undo->setEnabled( cafStudy && cafStudy->canUndo() );
254   if ( redo )
255     redo->setEnabled( cafStudy && cafStudy->canRedo() );
256 }
257
258 void CAF_Application::onHelpAbout()
259 {
260   SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
261 }
262
263 SUIT_Study* CAF_Application::createNewStudy()
264 {
265   return new CAF_Study( this );
266 }
267
268 void CAF_Application::setStdApp( const Handle(TDocStd_Application)& app )
269 {
270   myStdApp = app;
271 }