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
18 XGUI_MainWindow::XGUI_MainWindow(QWidget* parent)
19     : QMainWindow(parent), 
20     myPythonConsole(0)
21 {
22   setWindowTitle(tr("New Geom"));
23   myMenuBar = new XGUI_MainMenu(this);
24
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   if (!myPythonConsole) {
69
70     QDockWidget* aDoc = new QDockWidget(this);
71     aDoc->setFeatures(QDockWidget::AllDockWidgetFeatures | QDockWidget::DockWidgetVerticalTitleBar);
72     aDoc->setMinimumHeight(0);
73     aDoc->setWindowTitle("Console");
74     myPythonConsole = new PyConsole_EnhConsole( aDoc, new PyConsole_EnhInterp());
75     aDoc->setWidget(myPythonConsole);
76     addDockWidget(Qt::TopDockWidgetArea, aDoc);
77     tabifyDockWidget(myMenuBar->getLastDockWindow(), aDoc);
78   }
79   myPythonConsole->parentWidget()->show();
80 }
81
82 //******************************************************
83 void XGUI_MainWindow::hidePythonConsole()
84 {
85   if (myPythonConsole)
86     myPythonConsole->parentWidget()->hide();
87 }
88
89 //******************************************************
90 void XGUI_MainWindow::createSubWindow()
91 {
92   viewer()->createView();
93 }
94
95 //******************************************************
96 void XGUI_MainWindow::cascadeWindows()
97 {
98   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
99   QList<QMdiSubWindow*> aWindows = aMdiArea->subWindowList();
100   
101   QSize aSize = aMdiArea->size();
102   QRect aRect = aMdiArea->geometry();
103   const int aOffset = 30;
104   int i = 0, j = 0;
105   int x, y;
106   int w = aSize.width() / 2;
107   int h = aSize.height() / 2;
108   QMdiSubWindow* aLastWnd;
109   foreach(QMdiSubWindow* aWnd, aWindows) {
110     aWnd->showNormal();
111     aWnd->raise();
112     x = aOffset * i;
113     if ((x + w) > aSize.width()) {
114       x = 0;
115       i = 0;
116     }
117     y = aOffset * j;
118     if ((y + h) > aSize.height()) {
119       y = 0;
120       j = 0;
121     }
122     aWnd->setGeometry(QStyle::visualRect(aWnd->layoutDirection(), aRect, 
123       QRect(x, y, w, h)));
124     i++;
125     j++;
126     viewer()->onWindowActivated(aWnd);
127     aLastWnd = aWnd;
128     QApplication::processEvents();
129   }
130   aLastWnd->setFocus();
131 }
132
133 void XGUI_MainWindow::onViewCreated(XGUI_ViewWindow* theWindow)
134 {
135   QWidget* aSubWindow = theWindow->parentWidget();
136   QWidget* aMDIWidget = centralWidget();
137
138   QAction* aAction = new QAction(aSubWindow->windowTitle(), aMDIWidget);
139   aAction->setCheckable(true);
140   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(activateView()));
141   aMDIWidget->addAction(aAction);
142
143   QList<QAction*> aActions = aMDIWidget->actions();
144   foreach(QAction* aAct, aActions) {
145     if (aAct->isCheckable())
146       aAct->setChecked(false);
147   }
148   aAction->setChecked(true);
149 }
150
151 void XGUI_MainWindow::onDeleteView(XGUI_ViewWindow* theWindow)
152 {
153   QWidget* aSubWindow = theWindow->parentWidget();
154   QString aTitle = aSubWindow->windowTitle();
155   QWidget* aMDIWidget = centralWidget();
156   QList<QAction*> aActions = aMDIWidget->actions();
157
158   QAction* aDelAct = 0;
159   foreach(QAction* aAct, aActions) {
160     if (aAct->text() == aTitle) {
161       aDelAct = aAct;
162       break;
163     }
164   }
165   aMDIWidget->removeAction(aDelAct);
166 }
167
168 void XGUI_MainWindow::activateView()
169 {
170   QAction* aAction = static_cast<QAction*>(sender());
171   QString aWndTitle = aAction->text();
172   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
173
174   QList<QMdiSubWindow*> aWndList = aMdiArea->subWindowList();
175   QMdiSubWindow* aTargetView = 0;
176   foreach(QMdiSubWindow* aWnd, aWndList) {
177     if (aWnd->windowTitle() == aWndTitle) {
178       aWnd->raise();
179       aWnd->activateWindow();
180       aTargetView = aWnd;
181       break;
182     }
183   }
184   QApplication::processEvents();
185   if (aTargetView)
186     QTimer::singleShot(20, aTargetView, SLOT(setFocus()));
187 }
188
189 void XGUI_MainWindow::onViewActivated(QMdiSubWindow* theSubWnd)
190 {
191   if (!theSubWnd)
192     return;
193   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
194   QString aWndTitle = theSubWnd->windowTitle();
195   QList<QAction*> aActionList = aMdiArea->actions();
196   foreach(QAction* aAct, aActionList) {
197     if (aAct->isCheckable())
198       aAct->setChecked(aAct->text() == aWndTitle);
199   }
200 }