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