34c55d98e013cddc01c9b68bdbf0204485869b23
[occt.git] / samples / qt / Interface / src / Application.cxx
1 #include "Application.h"
2
3 #include "Translate.h"
4
5 #include <QMessageBox>
6 #include <stdlib.h>
7 #include <QMdiSubWindow>
8
9 ApplicationWindow::ApplicationWindow()
10     : ApplicationCommonWindow( ),
11       myImportPopup( 0 ),
12       myExportPopup( 0 )
13 {
14   createTranslatePopups();
15 }
16
17 ApplicationWindow::~ApplicationWindow()
18 {
19 }
20
21 void ApplicationWindow::createTranslatePopups()
22 {
23     if ( !myImportPopup )
24         myImportPopup = new QMenu( QObject::tr( "MNU_FILE_IMPORT" ), this );
25
26     if ( !myExportPopup )
27             myExportPopup = new QMenu( QObject::tr( "MNU_FILE_EXPORT" ), this );
28
29         QAction* a;
30         a = new QAction( QObject::tr("MNU_IMPORT_BREP"), this );
31         a->setStatusTip( QObject::tr("TBR_IMPORT_BREP") );
32         connect( a, SIGNAL( triggered() ), this, SLOT( onImport() ) );
33         myCasCadeTranslateActions.insert( FileImportBREPId, a );
34         myImportPopup->addAction( a );
35
36         a = new QAction( QObject::tr("MNU_EXPORT_BREP"), this );
37         a->setStatusTip( QObject::tr("TBR_EXPORT_BREP") );
38         connect( a, SIGNAL( triggered() ), this, SLOT( onExport() ) );
39         myCasCadeTranslateActions.insert( FileExportBREPId, a );
40         myExportPopup->addAction( a );
41
42         a = new QAction( QObject::tr("MNU_IMPORT_CSFDB"), this );
43         a->setStatusTip( QObject::tr("TBR_IMPORT_CSFDB") );
44         connect( a, SIGNAL( triggered() ), this, SLOT( onImport() ) );
45         myCasCadeTranslateActions.insert( FileImportCSFDBId, a );
46         myImportPopup->addAction( a );
47
48         a = new QAction( QObject::tr("MNU_EXPORT_CSFDB"), this );
49         a->setStatusTip( QObject::tr("TBR_EXPORT_CSFDB") );
50         connect( a, SIGNAL( triggered() ), this, SLOT( onExport() ) );
51         myCasCadeTranslateActions.insert( FileExportCSFDBId, a );
52         myExportPopup->addAction( a );
53
54         a = new QAction( QObject::tr("MNU_IMPORT_IGES"), this );
55         a->setStatusTip( QObject::tr("TBR_IMPORT_IGES") );
56         connect( a, SIGNAL( triggered() ), this, SLOT( onImport() ) );
57         myCasCadeTranslateActions.insert( FileImportIGESId, a );
58         myImportPopup->addAction( a );
59
60         a = new QAction( QObject::tr("MNU_EXPORT_IGES"), this );
61         a->setStatusTip( QObject::tr("TBR_EXPORT_IGES") );
62         connect( a, SIGNAL( triggered() ), this, SLOT( onExport() ) );
63         myCasCadeTranslateActions.insert( FileExportIGESId, a );
64         myExportPopup->addAction( a );
65
66         a = new QAction( QObject::tr("MNU_IMPORT_STEP"), this );
67         a->setStatusTip( QObject::tr("TBR_IMPORT_STEP") );
68         connect( a, SIGNAL( triggered() ), this, SLOT( onImport() ) );
69         myCasCadeTranslateActions.insert( FileImportSTEPId, a );
70         myImportPopup->addAction( a );
71
72         a = new QAction( QObject::tr("MNU_EXPORT_STEP"), this );
73         a->setStatusTip( QObject::tr("TBR_EXPORT_STEP") );
74         connect( a, SIGNAL( triggered() ), this, SLOT( onExport() ) );
75         myCasCadeTranslateActions.insert( FileExportSTEPId, a );
76         myExportPopup->addAction( a );
77
78         a = new QAction( QObject::tr("MNU_EXPORT_STL"), this );
79         a->setStatusTip( QObject::tr("TBR_EXPORT_STL") );
80         connect( a, SIGNAL( triggered() ), this, SLOT( onExport() ) );
81         myCasCadeTranslateActions.insert( FileExportSTLId, a );
82         myExportPopup->addAction( a );
83
84         a = new QAction( QObject::tr("MNU_EXPORT_VRML"), this );
85         a->setStatusTip( QObject::tr("TBR_EXPORT_VRML") );
86         connect( a, SIGNAL( triggered() ), this, SLOT( onExport() ) );
87         myCasCadeTranslateActions.insert( FileExportVRMLId, a );
88         myExportPopup->addAction( a );
89
90         myExportPopup->addSeparator();
91
92         a = new QAction( QObject::tr("MNU_EXPORT_IMAGE"), this );
93         a->setStatusTip( QObject::tr("TBR_EXPORT_IMAGE") );
94         connect( a, SIGNAL( triggered() ), this, SLOT( onExportImage() ) );
95         myExportPopup->addAction( a );
96 }
97
98 void ApplicationWindow::updateFileActions()
99 {
100   if ( myDocuments.isEmpty() )
101   {
102     if ( !isDocument() )
103     {
104       getFilePopup()->insertMenu( getFileSeparator(), myExportPopup );
105             getFilePopup()->insertMenu( myExportPopup->menuAction(), myImportPopup );
106       mySeparator = getFilePopup()->insertSeparator( myImportPopup->menuAction() );
107           }
108     else
109     {
110             getFilePopup()->removeAction( myImportPopup->menuAction() );
111             getFilePopup()->removeAction( myExportPopup->menuAction() );
112             getFilePopup()->removeAction( mySeparator );
113           }
114   }
115   ApplicationCommonWindow::updateFileActions();
116 }
117
118 void ApplicationWindow::onImport()
119 {
120     QAction* a = (QAction*)sender();
121     int type = translationFormat( a );
122     if ( type < 0 )
123         return;
124
125     bool stat = translate( type, true );
126     if ( stat )
127     {
128         DocumentCommon* doc = qobject_cast<MDIWindow*>( getWorkspace()->activeSubWindow()->widget() )->getDocument();
129         doc->fitAll();
130     }
131 }
132
133 void ApplicationWindow::onExport()
134 {
135     QAction* a = (QAction*)sender();
136     int type = translationFormat( a );
137     if ( type < 0 )
138         return;
139
140     translate( type, false );
141 }
142
143 int ApplicationWindow::translationFormat( const QAction* a )
144 {
145     int type = -1;
146     for ( int i = FileImportBREPId; i <= FileExportVRMLId; i++ )
147     {
148         if ( myCasCadeTranslateActions.at( i ) == a )
149         {
150             type = i;
151             break;
152         }
153     }
154     switch ( type )
155     {
156     case FileImportBREPId:
157     case FileExportBREPId:
158         type = Translate::FormatBREP;
159         break;
160     case FileImportIGESId:
161     case FileExportIGESId:
162         type = Translate::FormatIGES;
163         break;
164     case FileImportSTEPId:
165     case FileExportSTEPId:
166         type =  Translate::FormatSTEP;
167         break;
168     case FileImportCSFDBId:
169     case FileExportCSFDBId:
170         type = Translate::FormatCSFDB;
171         break;
172     case FileExportSTLId:
173         type = Translate::FormatSTL;
174         break;
175     case FileExportVRMLId:
176         type = Translate::FormatVRML;
177         break;
178     }
179     return type;
180 }
181
182 bool ApplicationWindow::translate( const int format, const bool import )
183 {
184     static Translate* anTrans = createTranslator();
185     DocumentCommon* doc = qobject_cast<MDIWindow*>( getWorkspace()->activeSubWindow()->widget() )->getDocument();
186     Handle(AIS_InteractiveContext) context = doc->getContext();
187     bool status;
188     if ( import )
189         status = anTrans->importModel( format, context );
190     else
191         status = anTrans->exportModel( format, context );
192
193     if ( !status )
194     {
195         QString msg = QObject::tr( "INF_TRANSLATE_ERROR" );
196         if ( !anTrans->info().isEmpty() )
197             msg += QString( "\n" ) + anTrans->info();
198         QMessageBox::critical( this, QObject::tr( "TIT_ERROR" ), msg, QObject::tr( "BTN_OK" ), QString::null, QString::null, 0, 0 );
199     }
200     
201     return status;
202 }
203
204 Translate* ApplicationWindow::createTranslator()
205 {
206   Translate* anTrans = new Translate( this/*, "Translator"*/ );
207   return anTrans;
208 }
209
210 void ApplicationWindow::onSelectionChanged()
211 {
212   ApplicationCommonWindow::onSelectionChanged();
213
214   QMdiArea* ws = getWorkspace();
215   DocumentCommon* doc = qobject_cast<MDIWindow*>( ws->activeSubWindow()->widget() )->getDocument();
216   Handle(AIS_InteractiveContext) context = doc->getContext();
217   int numSel = context->NbSelected();
218
219   myCasCadeTranslateActions.at( FileExportBREPId )->setEnabled( numSel );
220   myCasCadeTranslateActions.at( FileExportCSFDBId )->setEnabled( numSel );
221   myCasCadeTranslateActions.at( FileExportIGESId )->setEnabled( numSel );
222   myCasCadeTranslateActions.at( FileExportSTEPId )->setEnabled( numSel );
223   myCasCadeTranslateActions.at( FileExportSTLId )->setEnabled( numSel );
224   myCasCadeTranslateActions.at( FileExportVRMLId )->setEnabled( numSel );
225 }
226
227 QString ApplicationWindow::getIEResourceDir()
228 {
229   static QString aResourceDir =
230     QString::fromUtf8 (qgetenv ("CSF_IEResourcesDefaults").constData());
231   
232   return aResourceDir;
233 }
234
235 void ApplicationWindow::onExportImage()
236 {
237     MDIWindow* w = qobject_cast<MDIWindow*>( getWorkspace()->activeSubWindow()->widget() );
238     if ( w )
239         w->dump();
240 }
241
242