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"
8 #include <PyConsole_Console.h>
9 #include <PyConsole_EnhInterp.h>
12 #include <QMdiSubWindow>
14 #include <QDockWidget>
15 #include <QApplication>
18 XGUI_MainWindow::XGUI_MainWindow(QWidget* parent)
19 : QMainWindow(parent),
22 setWindowTitle(tr("New Geom"));
23 myMenuBar = new XGUI_MainMenu(this);
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*)));
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()));
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()));
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()));
44 aAction = new QAction(aMdiArea);
45 aAction->setSeparator(true);
46 aMdiArea->addAction(aAction);
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*)));
55 XGUI_MainWindow::~XGUI_MainWindow(void)
59 //******************************************************
60 QMdiArea* XGUI_MainWindow::mdiArea() const
62 return static_cast<QMdiArea*>(centralWidget());
65 //******************************************************
66 void XGUI_MainWindow::showPythonConsole()
68 if (!myPythonConsole) {
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);
79 myPythonConsole->parentWidget()->show();
82 //******************************************************
83 void XGUI_MainWindow::hidePythonConsole()
86 myPythonConsole->parentWidget()->hide();
89 //******************************************************
90 void XGUI_MainWindow::createSubWindow()
92 viewer()->createView();
95 //******************************************************
96 void XGUI_MainWindow::cascadeWindows()
98 QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
99 QList<QMdiSubWindow*> aWindows = aMdiArea->subWindowList();
101 QSize aSize = aMdiArea->size();
102 QRect aRect = aMdiArea->geometry();
103 const int aOffset = 30;
106 int w = aSize.width() / 2;
107 int h = aSize.height() / 2;
108 QMdiSubWindow* aLastWnd;
109 foreach(QMdiSubWindow* aWnd, aWindows) {
113 if ((x + w) > aSize.width()) {
118 if ((y + h) > aSize.height()) {
122 aWnd->setGeometry(QStyle::visualRect(aWnd->layoutDirection(), aRect,
126 viewer()->onWindowActivated(aWnd);
128 QApplication::processEvents();
130 aLastWnd->setFocus();
133 void XGUI_MainWindow::onViewCreated(XGUI_ViewWindow* theWindow)
135 QWidget* aSubWindow = theWindow->parentWidget();
136 QWidget* aMDIWidget = centralWidget();
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);
143 QList<QAction*> aActions = aMDIWidget->actions();
144 foreach(QAction* aAct, aActions) {
145 if (aAct->isCheckable())
146 aAct->setChecked(false);
148 aAction->setChecked(true);
151 void XGUI_MainWindow::onDeleteView(XGUI_ViewWindow* theWindow)
153 QWidget* aSubWindow = theWindow->parentWidget();
154 QString aTitle = aSubWindow->windowTitle();
155 QWidget* aMDIWidget = centralWidget();
156 QList<QAction*> aActions = aMDIWidget->actions();
158 QAction* aDelAct = 0;
159 foreach(QAction* aAct, aActions) {
160 if (aAct->text() == aTitle) {
165 aMDIWidget->removeAction(aDelAct);
168 void XGUI_MainWindow::activateView()
170 QAction* aAction = static_cast<QAction*>(sender());
171 QString aWndTitle = aAction->text();
172 QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
174 QList<QMdiSubWindow*> aWndList = aMdiArea->subWindowList();
175 QMdiSubWindow* aTargetView = 0;
176 foreach(QMdiSubWindow* aWnd, aWndList) {
177 if (aWnd->windowTitle() == aWndTitle) {
179 aWnd->activateWindow();
184 QApplication::processEvents();
186 QTimer::singleShot(20, aTargetView, SLOT(setFocus()));
189 void XGUI_MainWindow::onViewActivated(QMdiSubWindow* theSubWnd)
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);