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   createMainMenu();
25   QMdiArea* aMdiArea = new QMdiArea(this);
26   aMdiArea->setContextMenuPolicy(Qt::ActionsContextMenu);
27   setCentralWidget(aMdiArea);
28   connect(aMdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), 
29           this, SLOT(onViewActivated(QMdiSubWindow*)));
30
31   // Create actions of MDI area
32   QAction* aAction = new QAction(QIcon(":pictures/new_view.png"), tr("Create Window"), aMdiArea);
33   aMdiArea->addAction(aAction);
34   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(createSubWindow()));
35   
36   aAction = new QAction(QIcon(":pictures/tile_views.png"), tr("Tile"), aMdiArea);
37   aMdiArea->addAction(aAction);
38   connect(aAction, SIGNAL(triggered(bool)), aMdiArea, SLOT(tileSubWindows()));
39   
40   aAction = new QAction(QIcon(":pictures/cascade_views.png"), tr("Cascade"), aMdiArea);
41   aMdiArea->addAction(aAction);
42   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(cascadeWindows()));
43
44   aAction = new QAction(aMdiArea);
45   aAction->setSeparator(true);
46   aMdiArea->addAction(aAction);
47
48   myViewer = new XGUI_Viewer(this);
49   connect(myViewer, SIGNAL(viewCreated(XGUI_ViewWindow*)), 
50           this, SLOT(onViewCreated(XGUI_ViewWindow*)));
51   connect(myViewer, SIGNAL(deleteView(XGUI_ViewWindow*)), 
52           this, SLOT(onDeleteView(XGUI_ViewWindow*)));
53 }
54
55 XGUI_MainWindow::~XGUI_MainWindow(void)
56 {
57 }
58
59 //******************************************************
60 QMdiArea* XGUI_MainWindow::mdiArea() const
61 {
62   return static_cast<QMdiArea*>(centralWidget());
63 }
64
65 //******************************************************
66 void XGUI_MainWindow::showPythonConsole()
67 {
68   // TODO: Check why PyConsole can not be created
69   //if (!myPythonConsole) {
70   //  myPythonConsole = new PyConsole_EnhConsole(this, new PyConsole_EnhInterp());
71   //  undockPythonConsole();
72   //}
73   //myPythonConsole->parentWidget()->show();
74 }
75
76 //******************************************************
77 void XGUI_MainWindow::hidePythonConsole()
78 {
79   if (myPythonConsole)
80     myPythonConsole->parentWidget()->hide();
81 }
82
83 //******************************************************
84 void XGUI_MainWindow::dockPythonConsole()
85 {
86   if (!myPythonConsole)
87     return;
88   myMenuBar->removeConsole();
89   QDockWidget* aDock = new QDockWidget(this);
90   aDock->setFeatures(QDockWidget::AllDockWidgetFeatures |
91                      QDockWidget::DockWidgetVerticalTitleBar);
92   aDock->setAllowedAreas(Qt::LeftDockWidgetArea  |
93                          Qt::RightDockWidgetArea |
94                          Qt::BottomDockWidgetArea);
95   aDock->setMinimumHeight(0);
96   aDock->setWindowTitle("Console");
97   aDock->setWidget(myPythonConsole);
98   addDockWidget(Qt::BottomDockWidgetArea, aDock);
99   // Undock python console if widget is closed...
100   CloseEventWatcher* aWatcher =  new CloseEventWatcher(aDock);
101   connect(aWatcher, SIGNAL(widgetClosed()),
102           this,     SLOT(undockPythonConsole()));
103   aDock->installEventFilter(aWatcher);
104 }
105
106 void XGUI_MainWindow::undockPythonConsole()
107 {
108   if (!myPythonConsole)
109     return;
110   QDockWidget* aDock = qobject_cast<QDockWidget*>(myPythonConsole->parentWidget());
111   //When the application starts console will be displayed as
112   //a wokbench tab, so there is no dock yet
113   if(aDock) {
114     aDock->hide();
115     aDock->setWidget(NULL);
116     aDock->deleteLater();
117   }
118   myMenuBar->insertConsole(myPythonConsole);
119 }
120
121 //******************************************************
122 void XGUI_MainWindow::createSubWindow()
123 {
124   viewer()->createView();
125 }
126
127 //******************************************************
128 void XGUI_MainWindow::cascadeWindows()
129 {
130   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
131   QList<QMdiSubWindow*> aWindows = aMdiArea->subWindowList();
132   
133   QSize aSize = aMdiArea->size();
134   QRect aRect = aMdiArea->geometry();
135   const int aOffset = 30;
136   int i = 0, j = 0;
137   int x, y;
138   int w = aSize.width() / 2;
139   int h = aSize.height() / 2;
140   QMdiSubWindow* aLastWnd;
141   foreach(QMdiSubWindow* aWnd, aWindows) {
142     aWnd->showNormal();
143     aWnd->raise();
144     x = aOffset * i;
145     if ((x + w) > aSize.width()) {
146       x = 0;
147       i = 0;
148     }
149     y = aOffset * j;
150     if ((y + h) > aSize.height()) {
151       y = 0;
152       j = 0;
153     }
154     aWnd->setGeometry(QStyle::visualRect(aWnd->layoutDirection(), aRect, 
155       QRect(x, y, w, h)));
156     i++;
157     j++;
158     viewer()->onWindowActivated(aWnd);
159     aLastWnd = aWnd;
160     QApplication::processEvents();
161   }
162   aLastWnd->setFocus();
163 }
164
165 void XGUI_MainWindow::onViewCreated(XGUI_ViewWindow* theWindow)
166 {
167   QWidget* aSubWindow = theWindow->parentWidget();
168   QWidget* aMDIWidget = centralWidget();
169
170   QAction* aAction = new QAction(aSubWindow->windowTitle(), aMDIWidget);
171   aAction->setCheckable(true);
172   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(activateView()));
173   aMDIWidget->addAction(aAction);
174
175   QList<QAction*> aActions = aMDIWidget->actions();
176   foreach(QAction* aAct, aActions) {
177     if (aAct->isCheckable())
178       aAct->setChecked(false);
179   }
180   aAction->setChecked(true);
181 }
182
183 void XGUI_MainWindow::onDeleteView(XGUI_ViewWindow* theWindow)
184 {
185   QWidget* aSubWindow = theWindow->parentWidget();
186   QString aTitle = aSubWindow->windowTitle();
187   QWidget* aMDIWidget = centralWidget();
188   QList<QAction*> aActions = aMDIWidget->actions();
189
190   QAction* aDelAct = 0;
191   foreach(QAction* aAct, aActions) {
192     if (aAct->text() == aTitle) {
193       aDelAct = aAct;
194       break;
195     }
196   }
197   aMDIWidget->removeAction(aDelAct);
198 }
199
200 void XGUI_MainWindow::activateView()
201 {
202   QAction* aAction = static_cast<QAction*>(sender());
203   QString aWndTitle = aAction->text();
204   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
205
206   QList<QMdiSubWindow*> aWndList = aMdiArea->subWindowList();
207   QMdiSubWindow* aTargetView = 0;
208   foreach(QMdiSubWindow* aWnd, aWndList) {
209     if (aWnd->windowTitle() == aWndTitle) {
210       aWnd->raise();
211       aWnd->activateWindow();
212       aTargetView = aWnd;
213       break;
214     }
215   }
216   QApplication::processEvents();
217   if (aTargetView)
218     QTimer::singleShot(20, aTargetView, SLOT(setFocus()));
219 }
220
221 void XGUI_MainWindow::onViewActivated(QMdiSubWindow* theSubWnd)
222 {
223   if (!theSubWnd)
224     return;
225   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
226   QString aWndTitle = theSubWnd->windowTitle();
227   QList<QAction*> aActionList = aMdiArea->actions();
228   foreach(QAction* aAct, aActionList) {
229     if (aAct->isCheckable())
230       aAct->setChecked(aAct->text() == aWndTitle);
231   }
232 }
233
234 void XGUI_MainWindow::closeEvent(QCloseEvent * event)
235 {
236   emit exitKeySequence();
237   event->ignore();
238 }
239
240 void XGUI_MainWindow::createMainMenu()
241 {
242   myMenuBar = new XGUI_MainMenu(this);
243   QDockWidget* aMenuDock = new QDockWidget(this);
244   aMenuDock->setWidget(myMenuBar);
245   aMenuDock->setAllowedAreas(Qt::TopDockWidgetArea);
246   aMenuDock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar);
247   aMenuDock->setWindowTitle(tr("General"));
248   addDockWidget(Qt::TopDockWidgetArea, aMenuDock);
249 }
250
251 CloseEventWatcher::CloseEventWatcher(QObject* theParent)
252     : QObject(theParent)
253 {}
254
255 bool CloseEventWatcher::eventFilter(QObject *obj, QEvent *event) {
256   if (event->type() == QEvent::Close) {
257     emit widgetClosed();
258     event->ignore();
259     return true;
260   } else {
261     // standard event processing
262     return QObject::eventFilter(obj, event);
263   }
264 }
265