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