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