Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_MainWindow.cpp
1 #include "XGUI_MainWindow.h"
2 #include "XGUI_Constants.h"
3 #include "XGUI_MainMenu.h"
4 #include "XGUI_ViewWindow.h"
5 #include "XGUI_Viewer.h"
6 #include "XGUI_ObjectsBrowser.h"
7
8 #include <PyConsole_Console.h>
9 #include <PyConsole_EnhInterp.h>
10
11 #include <QMdiArea>
12 #include <QMdiSubWindow>
13 #include <QAction>
14 #include <QDockWidget>
15 #include <QApplication>
16 #include <QTimer>
17 #include <QCloseEvent>
18
19 XGUI_MainWindow::XGUI_MainWindow(QWidget* parent)
20     : QMainWindow(parent), 
21     myPythonConsole(0)
22 {
23   setWindowTitle(tr("New Geom"));
24   myMenuBar = new XGUI_MainMenu(this);
25
26   QMdiArea* aMdiArea = new QMdiArea(this);
27   aMdiArea->setContextMenuPolicy(Qt::ActionsContextMenu);
28   setCentralWidget(aMdiArea);
29   connect(aMdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), 
30           this, SLOT(onViewActivated(QMdiSubWindow*)));
31
32   // Create actions of MDI area
33   QAction* aAction = new QAction(QIcon(":pictures/new_view.png"), tr("Create Window"), aMdiArea);
34   aMdiArea->addAction(aAction);
35   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(createSubWindow()));
36   
37   aAction = new QAction(QIcon(":pictures/tile_views.png"), tr("Tile"), aMdiArea);
38   aMdiArea->addAction(aAction);
39   connect(aAction, SIGNAL(triggered(bool)), aMdiArea, SLOT(tileSubWindows()));
40   
41   aAction = new QAction(QIcon(":pictures/cascade_views.png"), tr("Cascade"), aMdiArea);
42   aMdiArea->addAction(aAction);
43   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(cascadeWindows()));
44
45   aAction = new QAction(aMdiArea);
46   aAction->setSeparator(true);
47   aMdiArea->addAction(aAction);
48
49   myViewer = new XGUI_Viewer(this);
50   connect(myViewer, SIGNAL(viewCreated(XGUI_ViewWindow*)), 
51           this, SLOT(onViewCreated(XGUI_ViewWindow*)));
52   connect(myViewer, SIGNAL(deleteView(XGUI_ViewWindow*)), 
53           this, SLOT(onDeleteView(XGUI_ViewWindow*)));
54 }
55
56 XGUI_MainWindow::~XGUI_MainWindow(void)
57 {
58 }
59
60 //******************************************************
61 QMdiArea* XGUI_MainWindow::mdiArea() const
62 {
63   return static_cast<QMdiArea*>(centralWidget());
64 }
65
66 //******************************************************
67 void XGUI_MainWindow::showPythonConsole()
68 {
69   if (!myPythonConsole) {
70
71     QDockWidget* aDoc = new QDockWidget(this);
72     aDoc->setFeatures(QDockWidget::AllDockWidgetFeatures | QDockWidget::DockWidgetVerticalTitleBar);
73     aDoc->setMinimumHeight(0);
74     aDoc->setWindowTitle("Console");
75     myPythonConsole = new PyConsole_EnhConsole( aDoc, new PyConsole_EnhInterp());
76     aDoc->setWidget(myPythonConsole);
77     addDockWidget(Qt::TopDockWidgetArea, aDoc);
78     tabifyDockWidget(myMenuBar->getLastDockWindow(), aDoc);
79   }
80   myPythonConsole->parentWidget()->show();
81 }
82
83 //******************************************************
84 void XGUI_MainWindow::hidePythonConsole()
85 {
86   if (myPythonConsole)
87     myPythonConsole->parentWidget()->hide();
88 }
89
90 //******************************************************
91 void XGUI_MainWindow::createSubWindow()
92 {
93   viewer()->createView();
94 }
95
96 //******************************************************
97 void XGUI_MainWindow::cascadeWindows()
98 {
99   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
100   QList<QMdiSubWindow*> aWindows = aMdiArea->subWindowList();
101   
102   QSize aSize = aMdiArea->size();
103   QRect aRect = aMdiArea->geometry();
104   const int aOffset = 30;
105   int i = 0, j = 0;
106   int x, y;
107   int w = aSize.width() / 2;
108   int h = aSize.height() / 2;
109   QMdiSubWindow* aLastWnd;
110   foreach(QMdiSubWindow* aWnd, aWindows) {
111     aWnd->showNormal();
112     aWnd->raise();
113     x = aOffset * i;
114     if ((x + w) > aSize.width()) {
115       x = 0;
116       i = 0;
117     }
118     y = aOffset * j;
119     if ((y + h) > aSize.height()) {
120       y = 0;
121       j = 0;
122     }
123     aWnd->setGeometry(QStyle::visualRect(aWnd->layoutDirection(), aRect, 
124       QRect(x, y, w, h)));
125     i++;
126     j++;
127     viewer()->onWindowActivated(aWnd);
128     aLastWnd = aWnd;
129     QApplication::processEvents();
130   }
131   aLastWnd->setFocus();
132 }
133
134 void XGUI_MainWindow::onViewCreated(XGUI_ViewWindow* theWindow)
135 {
136   QWidget* aSubWindow = theWindow->parentWidget();
137   QWidget* aMDIWidget = centralWidget();
138
139   QAction* aAction = new QAction(aSubWindow->windowTitle(), aMDIWidget);
140   aAction->setCheckable(true);
141   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(activateView()));
142   aMDIWidget->addAction(aAction);
143
144   QList<QAction*> aActions = aMDIWidget->actions();
145   foreach(QAction* aAct, aActions) {
146     if (aAct->isCheckable())
147       aAct->setChecked(false);
148   }
149   aAction->setChecked(true);
150 }
151
152 void XGUI_MainWindow::onDeleteView(XGUI_ViewWindow* theWindow)
153 {
154   QWidget* aSubWindow = theWindow->parentWidget();
155   QString aTitle = aSubWindow->windowTitle();
156   QWidget* aMDIWidget = centralWidget();
157   QList<QAction*> aActions = aMDIWidget->actions();
158
159   QAction* aDelAct = 0;
160   foreach(QAction* aAct, aActions) {
161     if (aAct->text() == aTitle) {
162       aDelAct = aAct;
163       break;
164     }
165   }
166   aMDIWidget->removeAction(aDelAct);
167 }
168
169 void XGUI_MainWindow::activateView()
170 {
171   QAction* aAction = static_cast<QAction*>(sender());
172   QString aWndTitle = aAction->text();
173   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
174
175   QList<QMdiSubWindow*> aWndList = aMdiArea->subWindowList();
176   QMdiSubWindow* aTargetView = 0;
177   foreach(QMdiSubWindow* aWnd, aWndList) {
178     if (aWnd->windowTitle() == aWndTitle) {
179       aWnd->raise();
180       aWnd->activateWindow();
181       aTargetView = aWnd;
182       break;
183     }
184   }
185   QApplication::processEvents();
186   if (aTargetView)
187     QTimer::singleShot(20, aTargetView, SLOT(setFocus()));
188 }
189
190 void XGUI_MainWindow::onViewActivated(QMdiSubWindow* theSubWnd)
191 {
192   if (!theSubWnd)
193     return;
194   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
195   QString aWndTitle = theSubWnd->windowTitle();
196   QList<QAction*> aActionList = aMdiArea->actions();
197   foreach(QAction* aAct, aActionList) {
198     if (aAct->isCheckable())
199       aAct->setChecked(aAct->text() == aWndTitle);
200   }
201 }
202
203 void XGUI_MainWindow::closeEvent(QCloseEvent * event)
204 {
205   emit exitKeySequence();
206   event->ignore();
207 }