Salome HOME
Issue #8 : Provide Pop-up menu for windows management
[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   myViewer = new XGUI_Viewer(this);
44   connect(myViewer, SIGNAL(viewCreated(XGUI_ViewWindow*)), 
45           this, SLOT(onViewCreated(XGUI_ViewWindow*)));
46   connect(myViewer, SIGNAL(deleteView(XGUI_ViewWindow*)), 
47           this, SLOT(onViewCreated(XGUI_ViewWindow*)));
48 }
49
50 XGUI_MainWindow::~XGUI_MainWindow(void)
51 {
52 }
53
54 //******************************************************
55 QMdiArea* XGUI_MainWindow::mdiArea() const
56 {
57   return static_cast<QMdiArea*>(centralWidget());
58 }
59
60 //******************************************************
61 void XGUI_MainWindow::showPythonConsole()
62 {
63   if (!myPythonConsole) {
64
65     QDockWidget* aDoc = new QDockWidget(this);
66     aDoc->setFeatures(QDockWidget::AllDockWidgetFeatures | QDockWidget::DockWidgetVerticalTitleBar);
67     aDoc->setMinimumHeight(0);
68     aDoc->setWindowTitle("Console");
69     myPythonConsole = new PyConsole_EnhConsole( aDoc, new PyConsole_EnhInterp());
70     aDoc->setWidget(myPythonConsole);
71     addDockWidget(Qt::TopDockWidgetArea, aDoc);
72     tabifyDockWidget(myMenuBar->getLastDockWindow(), aDoc);
73   }
74   myPythonConsole->parentWidget()->show();
75 }
76
77 //******************************************************
78 void XGUI_MainWindow::hidePythonConsole()
79 {
80   if (myPythonConsole)
81     myPythonConsole->parentWidget()->hide();
82 }
83
84 //******************************************************
85 void XGUI_MainWindow::createSubWindow()
86 {
87   viewer()->createView();
88 }
89
90 //******************************************************
91 void XGUI_MainWindow::cascadeWindows()
92 {
93   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
94   QList<QMdiSubWindow*> aWindows = aMdiArea->subWindowList();
95   
96   QSize aSize = aMdiArea->size();
97   QRect aRect = aMdiArea->geometry();
98   const int aOffset = 30;
99   int i = 0, j = 0;
100   int x, y;
101   int w = aSize.width() / 2;
102   int h = aSize.height() / 2;
103   QMdiSubWindow* aLastWnd;
104   foreach(QMdiSubWindow* aWnd, aWindows) {
105     aWnd->showNormal();
106     aWnd->raise();
107     x = aOffset * i;
108     if ((x + w) > aSize.width()) {
109       x = 0;
110       i = 0;
111     }
112     y = aOffset * j;
113     if ((y + h) > aSize.height()) {
114       y = 0;
115       j = 0;
116     }
117     aWnd->setGeometry(QStyle::visualRect(aWnd->layoutDirection(), aRect, 
118       QRect(x, y, w, h)));
119     i++;
120     j++;
121     viewer()->onWindowActivated(aWnd);
122     aLastWnd = aWnd;
123     QApplication::processEvents();
124   }
125   aLastWnd->setFocus();
126 }
127
128 void XGUI_MainWindow::onViewCreated(XGUI_ViewWindow* theWindow)
129 {
130   QWidget* aSubWindow = theWindow->parentWidget();
131   QWidget* aMDIWidget = centralWidget();
132   QAction* aAction;
133   if (aMDIWidget->actions().size() == 3) {
134     aAction = new QAction(aMDIWidget);
135     aAction->setSeparator(true);
136     aMDIWidget->addAction(aAction);
137   }
138   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   foreach(QMdiSubWindow* aWnd, aWndList) {
176     if (aWnd->windowTitle() == aWndTitle) {
177       aWnd->raise();
178       aWnd->activateWindow();
179       aWnd->setFocus();
180       break;
181     }
182   }
183   QApplication::processEvents();
184 }
185
186 void XGUI_MainWindow::onViewActivated(QMdiSubWindow* theSubWnd)
187 {
188   if (!theSubWnd)
189     return;
190   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
191   QString aWndTitle = theSubWnd->windowTitle();
192   QList<QAction*> aActionList = aMdiArea->actions();
193   foreach(QAction* aAct, aActionList) {
194     if (aAct->isCheckable())
195       aAct->setChecked(aAct->text() == aWndTitle);
196   }
197 }