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>
17 #include <QCloseEvent>
20 XGUI_MainWindow::XGUI_MainWindow(QWidget* parent)
21 : QMainWindow(parent),
22 myPythonConsole(0), myIsModified(false)
24 myTitle = tr("New Geom");
27 QMdiArea* aMdiArea = new QMdiArea(this);
28 aMdiArea->setContextMenuPolicy(Qt::ActionsContextMenu);
29 setCentralWidget(aMdiArea);
30 connect(aMdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this,
31 SLOT(onViewActivated(QMdiSubWindow*)));
33 // Create actions of MDI area
34 QAction* aAction = new QAction(QIcon(":pictures/new_view.png"), tr("Create Window"), aMdiArea);
35 aMdiArea->addAction(aAction);
36 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(createSubWindow()));
38 aAction = new QAction(QIcon(":pictures/tile_views.png"), tr("Tile"), aMdiArea);
39 aMdiArea->addAction(aAction);
40 connect(aAction, SIGNAL(triggered(bool)), aMdiArea, SLOT(tileSubWindows()));
42 aAction = new QAction(QIcon(":pictures/cascade_views.png"), tr("Cascade"), aMdiArea);
43 aMdiArea->addAction(aAction);
44 connect(aAction, SIGNAL(triggered(bool)), this, SLOT(cascadeWindows()));
46 aAction = new QAction(aMdiArea);
47 aAction->setSeparator(true);
48 aMdiArea->addAction(aAction);
50 myViewer = new XGUI_Viewer(this);
51 connect(myViewer, SIGNAL(viewCreated(XGUI_ViewWindow*)), this,
52 SLOT(onViewCreated(XGUI_ViewWindow*)));
53 connect(myViewer, SIGNAL(deleteView(XGUI_ViewWindow*)), this,
54 SLOT(onDeleteView(XGUI_ViewWindow*)));
57 XGUI_MainWindow::~XGUI_MainWindow(void)
61 //******************************************************
62 QMdiArea* XGUI_MainWindow::mdiArea() const
64 return static_cast<QMdiArea*>(centralWidget());
67 //******************************************************
68 void XGUI_MainWindow::showPythonConsole()
70 // TODO: Check why PyConsole can not be created
71 if (!myPythonConsole) {
72 myPythonConsole = new PyConsole_EnhConsole(this, new PyConsole_EnhInterp());
73 myPythonConsole->setObjectName("PythonConsole");
74 undockPythonConsole();
76 myPythonConsole->parentWidget()->show();
79 //******************************************************
80 void XGUI_MainWindow::hidePythonConsole()
83 myPythonConsole->parentWidget()->hide();
86 //******************************************************
87 void XGUI_MainWindow::dockPythonConsole()
91 myMenuBar->removeConsole();
92 QDockWidget* aDock = new QDockWidget(this);
93 aDock->setFeatures(QDockWidget::AllDockWidgetFeatures | QDockWidget::DockWidgetVerticalTitleBar);
94 aDock->setAllowedAreas(
95 Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
96 aDock->setMinimumHeight(0);
97 aDock->setWindowTitle("Console");
98 aDock->setWidget(myPythonConsole);
99 addDockWidget(Qt::BottomDockWidgetArea, aDock);
100 // Undock python console if widget is closed...
101 CloseEventWatcher* aWatcher = new CloseEventWatcher(aDock);
102 connect(aWatcher, SIGNAL(widgetClosed()), this, SLOT(undockPythonConsole()));
103 aDock->installEventFilter(aWatcher);
106 void XGUI_MainWindow::undockPythonConsole()
108 if (!myPythonConsole)
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
115 aDock->setWidget(NULL);
116 aDock->deleteLater();
118 myMenuBar->insertConsole(myPythonConsole);
121 //******************************************************
122 void XGUI_MainWindow::createSubWindow()
124 viewer()->createView();
127 //******************************************************
128 void XGUI_MainWindow::cascadeWindows()
130 QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
131 QList<QMdiSubWindow*> aWindows = aMdiArea->subWindowList();
133 QSize aSize = aMdiArea->size();
134 QRect aRect = aMdiArea->geometry();
135 const int aOffset = 30;
138 int w = aSize.width() / 2;
139 int h = aSize.height() / 2;
140 QMdiSubWindow* aLastWnd;
141 foreach(QMdiSubWindow* aWnd, aWindows)
146 if ((x + w) > aSize.width()) {
151 if ((y + h) > aSize.height()) {
155 aWnd->setGeometry(QStyle::visualRect(aWnd->layoutDirection(), aRect, QRect(x, y, w, h)));
158 viewer()->onWindowActivated(aWnd);
160 QApplication::processEvents();
162 aLastWnd->setFocus();
165 void XGUI_MainWindow::onViewCreated(XGUI_ViewWindow* theWindow)
167 QWidget* aSubWindow = theWindow->parentWidget();
168 QWidget* aMDIWidget = centralWidget();
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);
175 QList<QAction*> aActions = aMDIWidget->actions();
176 foreach(QAction* aAct, aActions)
178 if (aAct->isCheckable())
179 aAct->setChecked(false);
181 aAction->setChecked(true);
184 void XGUI_MainWindow::onDeleteView(XGUI_ViewWindow* theWindow)
186 QWidget* aSubWindow = theWindow->parentWidget();
187 QString aTitle = aSubWindow->windowTitle();
188 QWidget* aMDIWidget = centralWidget();
189 QList<QAction*> aActions = aMDIWidget->actions();
191 QAction* aDelAct = 0;
192 foreach(QAction* aAct, aActions)
194 if (aAct->text() == aTitle) {
199 aMDIWidget->removeAction(aDelAct);
202 void XGUI_MainWindow::activateView()
204 QAction* aAction = static_cast<QAction*>(sender());
205 QString aWndTitle = aAction->text();
206 QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
208 QList<QMdiSubWindow*> aWndList = aMdiArea->subWindowList();
209 QMdiSubWindow* aTargetView = 0;
210 foreach(QMdiSubWindow* aWnd, aWndList)
212 if (aWnd->windowTitle() == aWndTitle) {
214 aWnd->activateWindow();
219 QApplication::processEvents();
221 QTimer::singleShot(20, aTargetView, SLOT(setFocus()));
224 void XGUI_MainWindow::onViewActivated(QMdiSubWindow* theSubWnd)
228 QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
229 QString aWndTitle = theSubWnd->windowTitle();
230 QList<QAction*> aActionList = aMdiArea->actions();
231 foreach(QAction* aAct, aActionList)
233 if (aAct->isCheckable())
234 aAct->setChecked(aAct->text() == aWndTitle);
238 void XGUI_MainWindow::closeEvent(QCloseEvent * event)
240 emit exitKeySequence();
244 void XGUI_MainWindow::createMainMenu()
246 myMenuBar = new XGUI_MainMenu(this);
247 QDockWidget* aMenuDock = new QDockWidget(this);
248 aMenuDock->setWidget(myMenuBar);
249 aMenuDock->setAllowedAreas(Qt::TopDockWidgetArea);
250 aMenuDock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar);
251 aMenuDock->setWindowTitle(tr("General"));
252 addDockWidget(Qt::TopDockWidgetArea, aMenuDock);
256 void XGUI_MainWindow::updateTitle()
258 QString aTitle = myTitle;
259 if (!myCurrentDir.isNull())
260 aTitle += " - " + myCurrentDir;
263 setWindowTitle(aTitle);
266 void XGUI_MainWindow::setCurrentDir(const QString& theDir, bool toUpdate)
268 myCurrentDir = theDir;
273 void XGUI_MainWindow::setModifiedState(bool isModified, bool toUpdate)
275 myIsModified = isModified;
280 CloseEventWatcher::CloseEventWatcher(QObject* theParent)
285 bool CloseEventWatcher::eventFilter(QObject *obj, QEvent *event)
287 if (event->type() == QEvent::Close) {
292 // standard event processing
293 return QObject::eventFilter(obj, event);