0029571: Samples: build qt samples together with OCCT
[occt.git] / samples / qt / Common / src / MDIWindow.cxx
CommitLineData
7fd59977 1#include "MDIWindow.h"
2
3#include "View.h"
4#include "DocumentCommon.h"
5#include "ApplicationCommon.h"
6
7#include <QFrame>
8#include <QToolBar>
9#include <QFileDialog>
10#include <QMessageBox>
11#include <QApplication>
12#include <QDir>
13#include <QFile>
14#include <QFileInfo>
15#include <QMainWindow>
16#include <QVBoxLayout>
17
18MDIWindow::MDIWindow(View* aView,
19 DocumentCommon* aDocument,
20 QWidget* parent,
21 Qt::WindowFlags wflags )
22: QMainWindow( parent, wflags )
23{
7fd59977 24 myView = aView;
2ad228f6 25 myDocument = aDocument;
26}
27
861a7b03 28MDIWindow::MDIWindow( DocumentCommon* aDocument, QWidget* parent, Qt::WindowFlags wflags)
7fd59977 29: QMainWindow( parent, wflags )
30{
2ad228f6 31 QFrame *vb = new QFrame( this );
7fd59977 32
2ad228f6 33 QVBoxLayout *layout = new QVBoxLayout( vb );
34 layout->setMargin( 0 );
7fd59977 35
36 vb->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
37
2ad228f6 38 setCentralWidget( vb );
7fd59977 39
2ad228f6 40 myDocument = aDocument;
41 myView = new View (myDocument->getContext(), vb);
42 layout->addWidget (myView);
7fd59977 43
44 connect( myView, SIGNAL( selectionChanged() ),
45 this, SIGNAL( selectionChanged() ) );
2ad228f6 46
861a7b03 47 createViewActions();
48 createRaytraceActions();
7fd59977 49
50 resize( sizeHint() );
51
52 setFocusPolicy( Qt::StrongFocus );
53}
54
55MDIWindow::~MDIWindow()
56{
57}
58
59DocumentCommon* MDIWindow::getDocument()
60{
2ad228f6 61 return myDocument;
7fd59977 62}
63
de75ed09 64void MDIWindow::closeEvent(QCloseEvent* )
7fd59977 65{
2ad228f6 66 emit sendCloseView(this);
7fd59977 67}
68
69void MDIWindow::fitAll()
70{
2ad228f6 71 myView->fitAll();
7fd59977 72}
73
74void MDIWindow::createViewActions()
75{
76 // populate a tool bar with some actions
2ad228f6 77 QToolBar* aToolBar = addToolBar( tr( "View Operations" ) );
78
7fd59977 79 QList<QAction*>* aList = myView->getViewActions();
2ad228f6 80 aToolBar->addActions( *aList );
7fd59977 81
82 aToolBar->toggleViewAction()->setVisible(false);
83 aList->at(View::ViewHlrOffId)->setChecked( true );
84}
85
861a7b03 86void MDIWindow::createRaytraceActions()
87{
88 // populate a tool bar with some actions
2ad228f6 89 QToolBar* aToolBar = addToolBar( tr( "Ray-tracing Options" ) );
90
861a7b03 91 QList<QAction*>* aList = myView->getRaytraceActions();
2ad228f6 92 aToolBar->addActions( *aList );
861a7b03 93
94 aToolBar->toggleViewAction()->setVisible (true);
95 aList->at (View::ToolRaytracingId)->setChecked (false);
96 aList->at (View::ToolShadowsId)->setChecked (true);
2ad228f6 97 aList->at (View::ToolReflectionsId)->setChecked (false);
861a7b03 98 aList->at (View::ToolAntialiasingId)->setChecked (false);
99}
100
7fd59977 101void MDIWindow::onWindowActivated ()
102{
103 getDocument()->getApplication()->onSelectionChanged();
104}
105
7fd59977 106void MDIWindow::dump()
107{
aa17dac8 108 QString filter = "Images Files (*.bmp *.ppm *.png *.jpg *.tiff *.tga *.gif *.exr)";
2ad228f6 109 QFileDialog fd ( 0 );
110 fd.setModal( true );
111 fd.setNameFilter ( filter );
112 fd.setWindowTitle( QObject::tr("INF_APP_EXPORT") );
113 fd.setFileMode( QFileDialog::AnyFile );
114 int ret = fd.exec();
7fd59977 115
2ad228f6 116 /* update the desktop after the dialog is closed */
117 qApp->processEvents();
7fd59977 118
2ad228f6 119 QStringList fileNames;
120 fileNames = fd.selectedFiles();
7fd59977 121
2ad228f6 122 QString file ( (ret == QDialog::Accepted && !fileNames.isEmpty() )? fileNames[0] : QString::null);
7fd59977 123 if ( !file.isNull() )
124 {
2ad228f6 125 QApplication::setOverrideCursor( Qt::WaitCursor );
126 if ( !QFileInfo( file ).completeSuffix().length() )
7fd59977 127 file += QString( ".bmp" );
861a7b03 128
6cfc06f0
BB
129 const TCollection_AsciiString anUtf8Path (file.toUtf8().data());
130
131 bool res = myView->dump( anUtf8Path.ToCString() );
7fd59977 132 QApplication::restoreOverrideCursor();
133 if ( !res )
134 {
2ad228f6 135 QWidgetList list = qApp->allWidgets();
aa17dac8 136 QWidget* mainWidget = NULL;
7fd59977 137 for( int i = 0; i < list.size(); ++i )
2ad228f6 138 {
139 if( qobject_cast<ApplicationCommonWindow*>( list.at( i ) ) )
7fd59977 140 mainWidget = qobject_cast<ApplicationCommonWindow*>( list.at( i ) );
2ad228f6 141 }
7fd59977 142
2ad228f6 143 QMessageBox::information ( mainWidget, QObject::tr("TIT_ERROR"), QObject::tr("INF_ERROR"), QObject::tr("BTN_OK"),
144 QString::null, QString::null, 0, 0 );
145 qApp->processEvents();
7fd59977 146 }
2ad228f6 147 }
7fd59977 148}
149
150QSize MDIWindow::sizeHint() const
151{
2ad228f6 152 return QSize( 450, 300 );
7fd59977 153}