Salome HOME
createApplication() made with 2 parameters.
[modules/gui.git] / src / CAF / CAF_Application.cxx
1 #include "CAF_Application.h"
2
3 #include "CAF_Study.h"
4
5 #include "SUIT_Desktop.h"
6 #include "SUIT_Session.h"
7 #include "SUIT_ViewModel.h"
8 #include "SUIT_Operation.h"
9 #include "SUIT_MessageBox.h"
10 #include "SUIT_ResourceMgr.h"
11
12 #include "QtxListAction.h"
13
14 #include <qtoolbar.h>
15 #include <qmenubar.h>
16 #include <qpopupmenu.h>
17 #include <qstatusbar.h>
18 #include <qapplication.h>
19
20 extern "C" CAF_EXPORT SUIT_Application* createApplication( int, char** )
21 {
22   return new CAF_Application();
23 }
24
25 CAF_Application::CAF_Application()
26 : STD_Application()
27 {
28 }
29
30 CAF_Application::~CAF_Application()
31 {
32 }
33
34 QString CAF_Application::applicationName() const
35 {
36   return QString( "CAFApplication" );
37 }
38
39 void CAF_Application::createActions()
40 {
41   STD_Application::createActions();
42
43   SUIT_Desktop* desk = desktop();
44   SUIT_ResourceMgr* resMgr = resourceMgr();
45
46   QtxListAction* editUndo =
47     new QtxListAction( tr( "TOT_APP_EDIT_UNDO" ), resMgr->loadPixmap( "CAF", tr( "ICON_APP_EDIT_UNDO" ) ),
48                                          tr( "MEN_APP_EDIT_UNDO" ), CTRL+Key_Z, desk );
49   registerAction( EditUndoId, editUndo );
50
51   QtxListAction* editRedo =
52     new QtxListAction( tr( "TOT_APP_EDIT_REDO" ), resMgr->loadPixmap( "CAF", tr( "ICON_APP_EDIT_REDO" ) ),
53                                          tr( "MEN_APP_EDIT_REDO" ), CTRL+Key_Y, desk );
54   registerAction( EditRedoId, editRedo );
55
56   editUndo->setComment( tr( "INF_APP_UNDOACTIONS" ) );
57   editRedo->setComment( tr( "INF_APP_REDOACTIONS" ) );
58
59   connect( editUndo, SIGNAL( activated( int ) ), this, SLOT( onUndo( int ) ) );
60   connect( editRedo, SIGNAL( activated( int ) ), this, SLOT( onRedo( int ) ) );
61
62
63   int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
64
65   createMenu( EditUndoId, editMenu, 0 );
66   createMenu( EditRedoId, editMenu, 0 );
67   createMenu( separator(), editMenu, -1, 0 );
68
69   int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
70
71   createTool( separator(), stdTBar );
72   createTool( EditUndoId, stdTBar );
73   createTool( EditRedoId, stdTBar );
74   createTool( separator(), stdTBar );
75 }
76
77 /*!
78     Undo operation on the given document. [ virtual protected ]
79 */
80 bool CAF_Application::undo( CAF_Study* doc )
81 {
82   bool success = false;
83   if ( doc )
84   {
85     if ( success = doc->undo() )
86       doc->update();
87   }
88   return success;
89 }
90
91 /*!
92     Redo operation on the given document. [ virtual protected ]
93 */
94 bool CAF_Application::redo(CAF_Study* doc)
95 {
96   bool success = false;
97   if ( doc )
98   {
99     if ( success = doc->redo() )
100       doc->update();
101   }
102   return success;
103 }
104
105 /*!
106     Undo operation on the active document. [ virtual protected slot ]
107 */
108 bool CAF_Application::onUndo( int numActions )
109 {
110   bool ok = true;
111   while ( numActions > 0 )
112   {
113           CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
114                 if ( cafStudy )
115     {
116             if ( !undo( cafStudy ) )
117                   {
118                           ok = false;
119                                 break;
120                         }
121                         numActions--;
122                 }
123   }
124   updateCommandsStatus();     /* enable/disable undo/redo */
125   return ok;
126 }
127
128 /*!
129     Redo operation on the active document. [ virtual protected slot ]
130 */
131 bool CAF_Application::onRedo( int numActions )
132 {
133   bool ok = true;
134   while ( numActions > 0 )
135   {
136           CAF_Study* cafStudy = dynamic_cast<CAF_Study*>( activeStudy() );
137                 if ( cafStudy )
138     {
139                         if ( !redo( cafStudy ) )
140                         {
141               ok = false;
142                     break;
143                         }
144                         numActions--;
145                 }
146   }
147   updateCommandsStatus();     /* enable/disable undo/redo */
148   return ok;
149 }
150
151 /*!
152   Enables / disables the actions according to the application state. [ virtual protected ]
153 */
154 void CAF_Application::updateCommandsStatus()
155 {
156         STD_Application::updateCommandsStatus();
157
158   CAF_Study* cafStudy = 0;
159   if ( activeStudy() && activeStudy()->inherits( "CAF_Study" ) )
160     cafStudy = (CAF_Study*)activeStudy();
161
162   QAction* undo = action( EditUndoId );
163   if ( cafStudy && undo->inherits( "QtxListAction" ) )
164     ((QtxListAction*)undo)->addNames( cafStudy->undoNames() );
165
166   QAction* redo = action( EditRedoId );
167   if ( cafStudy && redo->inherits( "QtxListAction" ) )
168     ((QtxListAction*)redo)->addNames( cafStudy->redoNames() );
169
170   undo->setEnabled( cafStudy && cafStudy->canUndo() );
171   redo->setEnabled( cafStudy && cafStudy->canRedo() );
172 }
173
174 void CAF_Application::onHelpAbout()
175 {
176   SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
177 }