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