Adding svn:eol-style=LF property
[occt.git] / samples / qt / Graphic3dDemo / src / MDIWindow.cxx
1 #include <qlayout.h>
2 #include <qframe.h>
3 #include <qtoolbar.h>
4 #include <qfiledialog.h>
5 #include <qapplication.h>
6 #include <qmessagebox.h>
7 #include <qaction.h>
8 #include <qsplitter.h>
9 #include <qmenu.h>
10 #include <qevent.h>
11
12 #include "MDIWindow.h"
13 #include "global.h"
14 #include "Document.h"
15 #include "View.h"
16 #include "ViewOperations.h"
17 #include "Graph.h"
18 #include "Application.h"
19
20 MDIWindow::MDIWindow( Document* aDocument, QWidget* parent, V3d_TypeOfView type )
21 : QMainWindow( parent )
22 {
23         resize( 500, 300 );
24   QSplitter* central = new QSplitter(Qt::Vertical, this);
25
26         myDocument = aDocument;
27   Handle(AIS_InteractiveContext) cxt = myDocument->getContext();
28   cxt->CurrentViewer()->SetDefaultTypeOfView( type );
29         myView = new View( cxt, central, this );
30         verify(connect(myView, SIGNAL(ViewInitialized()), 
31   Application::getApplication(), SLOT(updateViewDlg())));
32
33   // Graph initialization
34   myGraphView = new GraphSplitView(central);
35   GraphView* pGraph = myGraphView->getGraph();
36   GraphItem* pItem = new GraphItem(pGraph, tr("LEG_NONOPT_DISPLAY"));
37   pItem->setSorted(true);
38   pItem->setMarker(0, QColor( Qt::blue ), Qt::SolidLine);
39   pItem->addNode(0, 0);
40   pItem = new GraphItem(pGraph, tr("LEG_OPT_DISPLAY"));
41   pItem->setSorted(true);
42   pItem->setMarker(1, QColor( Qt::red ), Qt::SolidLine);
43   pItem->addNode(0, 0);
44   pItem = new GraphItem(pGraph, tr("LEG_NONOPT_UPDATE"));
45   pItem->setSorted(true);
46   pItem->setMarker(2, QColor( Qt::blue ), Qt::DotLine);
47   pItem->addNode(0, 0);
48   pItem = new GraphItem(pGraph, tr("LEG_OPT_UPDATE"));
49   pItem->setSorted(true);
50   pItem->setMarker(3, QColor( Qt::red ), Qt::DotLine);
51   pItem->addNode(0, 0);
52   pGraph->setXTitle(tr("LAB_TRIANGLES"));
53   pGraph->setYTitle(tr("LAB_SECONDS"));
54   pGraph->installEventFilter(this);
55   pGraph->setOperationEnabled(GraphView::voRect,  true);
56   pGraph->setOperationEnabled(GraphView::voZoom,  true);
57   pGraph->setOperationEnabled(GraphView::voZoomX, true);
58   pGraph->setOperationEnabled(GraphView::voZoomY, true);
59   pGraph->setOperationEnabled(GraphView::voPan,   true);
60   pGraph->setOperationEnabled(GraphView::voPanX,  true);
61   pGraph->setOperationEnabled(GraphView::voPanY,  true);
62
63   central->setStretchFactor(0, 5);
64   central->setStretchFactor(1, 1);
65         setCentralWidget( central );
66
67   myGraphPopup = new QMenu(pGraph);
68
69         myOperations = new ViewOperations( myDocument->getContext() );
70
71         verify( connect( myView,       SIGNAL( noActiveActions() ), 
72                    myOperations, SLOT( onNoActiveActions() ) ) );
73         verify( connect( myOperations, SIGNAL( setCursor( const QCursor& ) ),
74                                  SLOT( setCursor( const QCursor& ) ) ) );
75         
76         createViewActions();
77   createGraphActions();
78 }
79
80 MDIWindow::~MDIWindow()
81 {
82   delete myGraphView;
83 }
84
85 Document* MDIWindow::getDocument()
86 {
87   return myDocument;
88 }
89
90 int MDIWindow::getDisplayMode()
91 {
92   return myOperations->getDisplayMode();
93 }
94
95 void MDIWindow::closeEvent(QCloseEvent* e)
96 {
97   emit sendCloseView( this );
98 }
99
100 void MDIWindow::createViewActions()
101 {
102   QToolBar* aToolBar = new QToolBar( tr( "View Operations" ), this );
103   addToolBar( Qt::TopToolBarArea, aToolBar );
104         
105         QList<QAction*> aList = myOperations->getToolActions();
106
107   QActionGroup* dModeGroup = new QActionGroup( aToolBar );
108   QAction* a;
109
110   int i = 0;
111   foreach( a, aList ) {
112     aToolBar->addAction( a );
113 //        if ( i == 1 )
114 //            aToolBar->addSeparator();
115     if ( i == ViewOperations::ToolWireframeId || i == ViewOperations::ToolShadingId ) {
116       dModeGroup->addAction( a );
117     }
118     i++;
119   }
120   aToolBar->addSeparator();
121
122   aList = myOperations->getViewActions();
123   foreach( a, aList ) aToolBar->addAction( a );
124 }
125
126 void MDIWindow::setCursor(const QCursor& aCursor)
127 {
128   myView->setCursor(aCursor);
129 }
130
131 void MDIWindow::defineMagView()
132 {
133   myView->setCurrentAction();
134 }
135
136 Handle(V3d_View)& MDIWindow::getView()
137 {
138   return myView->getView();
139 }
140
141 void MDIWindow::activateAction( ViewOperations::Action action )
142 {
143   myView->startAction( action );
144 }
145
146 void MDIWindow::addResult(ResultType item, int x, double y)
147 {
148   if (item == Undefined) return;
149
150   GraphView* pGraph = myGraphView->getGraph();
151   pGraph->getItem((int)item)->addNode(x, y);
152   double xMin, xMax, yMin, yMax;
153   pGraph->getRange(xMin, yMin, xMax, yMax);
154   pGraph->fitData(0, xMax, 0, yMax);
155 }
156
157 bool MDIWindow::eventFilter(QObject* dstObj, QEvent* event)
158 {
159   if (event->type() == QEvent::MouseButtonPress)
160   {
161     QMouseEvent* mEvent = (QMouseEvent*)event;
162     if (mEvent->button() == Qt::RightButton &&
163       !(mEvent->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::ShiftModifier)))
164       graphPopup(mEvent->globalPos());
165   }
166   return FALSE;
167 }
168
169 void MDIWindow::createGraphActions()
170 {
171   QAction* a = new QAction( tr( "MEN_GRAPH_FIT_ALL" ), this );
172   a->setToolTip( tr( "TBR_GRAPH_FIT_ALL" ) );
173         verify( connect( a, SIGNAL( activated() ) , this, SLOT( onGraphFitAll() ) ) );
174   myGraphPopup->addAction( a );
175
176   myGraphPopup->addSeparator();
177
178   a = new QAction( tr( "MEN_GRAPH_CLEAR" ), this );
179   a->setToolTip( tr( "TBR_GRAPH_CLEAR" ) );
180         verify( connect( a, SIGNAL( activated() ) , this, SLOT( onGraphClear() ) ) );
181   myGraphPopup->addAction( a );
182 }
183
184 void MDIWindow::graphPopup(const QPoint& pos)
185 {
186   myGraphPopup->popup(pos);
187 }
188
189 void MDIWindow::onGraphClear()
190 {
191   GraphView* graph = myGraphView->getGraph();
192   for (int i = 0; i < graph->getNbItems(); i++)
193   {
194     graph->getItem(i)->clear();
195     graph->getItem(i)->addNode(0, 0);
196   }
197   graph->repaint();
198 }
199
200 void MDIWindow::onGraphFitAll()
201 {
202   GraphView* graph = myGraphView->getGraph();
203   double xMin, xMax, yMin, yMax;
204   graph->getRange(xMin, yMin, xMax, yMax);
205   graph->fitData(0, xMax, 0, yMax);
206 }