Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_MainWindow.cpp
index a0cefc1d3e881a4ac865ae3de153f12786b4c4b6..7fc26f0bfb5f8fb38556a89734e28a7b2f32cdec 100644 (file)
 #include <QAction>
 #include <QDockWidget>
 #include <QApplication>
+#include <QTimer>
+#include <QCloseEvent>
+
 
 XGUI_MainWindow::XGUI_MainWindow(QWidget* parent)
-    : QMainWindow(parent), 
-    myPythonConsole(0)
+    : QMainWindow(parent),
+      myPythonConsole(0), myIsModified(false)
 {
-  setWindowTitle(tr("New Geom"));
-  myMenuBar = new XGUI_MainMenu(this);
-
+  myTitle = tr("New Geom");
+  updateTitle();
+  createMainMenu();
   QMdiArea* aMdiArea = new QMdiArea(this);
   aMdiArea->setContextMenuPolicy(Qt::ActionsContextMenu);
   setCentralWidget(aMdiArea);
-  connect(aMdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), 
-          this, SLOT(onViewActivated(QMdiSubWindow*)));
+  connect(aMdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this,
+          SLOT(onViewActivated(QMdiSubWindow*)));
 
   // Create actions of MDI area
   QAction* aAction = new QAction(QIcon(":pictures/new_view.png"), tr("Create Window"), aMdiArea);
   aMdiArea->addAction(aAction);
   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(createSubWindow()));
-  
+
   aAction = new QAction(QIcon(":pictures/tile_views.png"), tr("Tile"), aMdiArea);
   aMdiArea->addAction(aAction);
   connect(aAction, SIGNAL(triggered(bool)), aMdiArea, SLOT(tileSubWindows()));
-  
+
   aAction = new QAction(QIcon(":pictures/cascade_views.png"), tr("Cascade"), aMdiArea);
   aMdiArea->addAction(aAction);
   connect(aAction, SIGNAL(triggered(bool)), this, SLOT(cascadeWindows()));
@@ -45,10 +48,10 @@ XGUI_MainWindow::XGUI_MainWindow(QWidget* parent)
   aMdiArea->addAction(aAction);
 
   myViewer = new XGUI_Viewer(this);
-  connect(myViewer, SIGNAL(viewCreated(XGUI_ViewWindow*)), 
-          this, SLOT(onViewCreated(XGUI_ViewWindow*)));
-  connect(myViewer, SIGNAL(deleteView(XGUI_ViewWindow*)), 
-          this, SLOT(onDeleteView(XGUI_ViewWindow*)));
+  connect(myViewer, SIGNAL(viewCreated(XGUI_ViewWindow*)), this,
+          SLOT(onViewCreated(XGUI_ViewWindow*)));
+  connect(myViewer, SIGNAL(deleteView(XGUI_ViewWindow*)), this,
+          SLOT(onDeleteView(XGUI_ViewWindow*)));
 }
 
 XGUI_MainWindow::~XGUI_MainWindow(void)
@@ -64,16 +67,11 @@ QMdiArea* XGUI_MainWindow::mdiArea() const
 //******************************************************
 void XGUI_MainWindow::showPythonConsole()
 {
+  // TODO: Check why PyConsole can not be created
   if (!myPythonConsole) {
-
-    QDockWidget* aDoc = new QDockWidget(this);
-    aDoc->setFeatures(QDockWidget::AllDockWidgetFeatures | QDockWidget::DockWidgetVerticalTitleBar);
-    aDoc->setMinimumHeight(0);
-    aDoc->setWindowTitle("Console");
-    myPythonConsole = new PyConsole_EnhConsole( aDoc, new PyConsole_EnhInterp());
-    aDoc->setWidget(myPythonConsole);
-    addDockWidget(Qt::TopDockWidgetArea, aDoc);
-    tabifyDockWidget(myMenuBar->getLastDockWindow(), aDoc);
+    myPythonConsole = new PyConsole_EnhConsole(this, new PyConsole_EnhInterp());
+    myPythonConsole->setObjectName("PythonConsole");
+    undockPythonConsole();
   }
   myPythonConsole->parentWidget()->show();
 }
@@ -85,6 +83,41 @@ void XGUI_MainWindow::hidePythonConsole()
     myPythonConsole->parentWidget()->hide();
 }
 
+//******************************************************
+void XGUI_MainWindow::dockPythonConsole()
+{
+  if (!myPythonConsole)
+    return;
+  myMenuBar->removeConsole();
+  QDockWidget* aDock = new QDockWidget(this);
+  aDock->setFeatures(QDockWidget::AllDockWidgetFeatures | QDockWidget::DockWidgetVerticalTitleBar);
+  aDock->setAllowedAreas(
+      Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::BottomDockWidgetArea);
+  aDock->setMinimumHeight(0);
+  aDock->setWindowTitle("Console");
+  aDock->setWidget(myPythonConsole);
+  addDockWidget(Qt::BottomDockWidgetArea, aDock);
+  // Undock python console if widget is closed...
+  CloseEventWatcher* aWatcher = new CloseEventWatcher(aDock);
+  connect(aWatcher, SIGNAL(widgetClosed()), this, SLOT(undockPythonConsole()));
+  aDock->installEventFilter(aWatcher);
+}
+
+void XGUI_MainWindow::undockPythonConsole()
+{
+  if (!myPythonConsole)
+    return;
+  QDockWidget* aDock = qobject_cast<QDockWidget*>(myPythonConsole->parentWidget());
+  //When the application starts console will be displayed as
+  //a wokbench tab, so there is no dock yet
+  if (aDock) {
+    aDock->hide();
+    aDock->setWidget(NULL);
+    aDock->deleteLater();
+  }
+  myMenuBar->insertConsole(myPythonConsole);
+}
+
 //******************************************************
 void XGUI_MainWindow::createSubWindow()
 {
@@ -96,7 +129,7 @@ void XGUI_MainWindow::cascadeWindows()
 {
   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
   QList<QMdiSubWindow*> aWindows = aMdiArea->subWindowList();
-  
+
   QSize aSize = aMdiArea->size();
   QRect aRect = aMdiArea->geometry();
   const int aOffset = 30;
@@ -105,7 +138,8 @@ void XGUI_MainWindow::cascadeWindows()
   int w = aSize.width() / 2;
   int h = aSize.height() / 2;
   QMdiSubWindow* aLastWnd;
-  foreach(QMdiSubWindow* aWnd, aWindows) {
+  foreach(QMdiSubWindow* aWnd, aWindows)
+  {
     aWnd->showNormal();
     aWnd->raise();
     x = aOffset * i;
@@ -118,8 +152,7 @@ void XGUI_MainWindow::cascadeWindows()
       y = 0;
       j = 0;
     }
-    aWnd->setGeometry(QStyle::visualRect(aWnd->layoutDirection(), aRect, 
-      QRect(x, y, w, h)));
+    aWnd->setGeometry(QStyle::visualRect(aWnd->layoutDirection(), aRect, QRect(x, y, w, h)));
     i++;
     j++;
     viewer()->onWindowActivated(aWnd);
@@ -140,7 +173,8 @@ void XGUI_MainWindow::onViewCreated(XGUI_ViewWindow* theWindow)
   aMDIWidget->addAction(aAction);
 
   QList<QAction*> aActions = aMDIWidget->actions();
-  foreach(QAction* aAct, aActions) {
+  foreach(QAction* aAct, aActions)
+  {
     if (aAct->isCheckable())
       aAct->setChecked(false);
   }
@@ -155,7 +189,8 @@ void XGUI_MainWindow::onDeleteView(XGUI_ViewWindow* theWindow)
   QList<QAction*> aActions = aMDIWidget->actions();
 
   QAction* aDelAct = 0;
-  foreach(QAction* aAct, aActions) {
+  foreach(QAction* aAct, aActions)
+  {
     if (aAct->text() == aTitle) {
       aDelAct = aAct;
       break;
@@ -171,15 +206,19 @@ void XGUI_MainWindow::activateView()
   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
 
   QList<QMdiSubWindow*> aWndList = aMdiArea->subWindowList();
-  foreach(QMdiSubWindow* aWnd, aWndList) {
+  QMdiSubWindow* aTargetView = 0;
+  foreach(QMdiSubWindow* aWnd, aWndList)
+  {
     if (aWnd->windowTitle() == aWndTitle) {
       aWnd->raise();
       aWnd->activateWindow();
-      aWnd->setFocus();
+      aTargetView = aWnd;
       break;
     }
   }
   QApplication::processEvents();
+  if (aTargetView)
+    QTimer::singleShot(20, aTargetView, SLOT(setFocus()));
 }
 
 void XGUI_MainWindow::onViewActivated(QMdiSubWindow* theSubWnd)
@@ -189,8 +228,69 @@ void XGUI_MainWindow::onViewActivated(QMdiSubWindow* theSubWnd)
   QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
   QString aWndTitle = theSubWnd->windowTitle();
   QList<QAction*> aActionList = aMdiArea->actions();
-  foreach(QAction* aAct, aActionList) {
+  foreach(QAction* aAct, aActionList)
+  {
     if (aAct->isCheckable())
       aAct->setChecked(aAct->text() == aWndTitle);
   }
-}
\ No newline at end of file
+}
+
+void XGUI_MainWindow::closeEvent(QCloseEvent * event)
+{
+  emit exitKeySequence();
+  event->ignore();
+}
+
+void XGUI_MainWindow::createMainMenu()
+{
+  myMenuBar = new XGUI_MainMenu(this);
+  QDockWidget* aMenuDock = new QDockWidget(this);
+  aMenuDock->setWidget(myMenuBar);
+  aMenuDock->setAllowedAreas(Qt::TopDockWidgetArea);
+  aMenuDock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar);
+  aMenuDock->setWindowTitle(tr("General"));
+  addDockWidget(Qt::TopDockWidgetArea, aMenuDock);
+}
+
+
+void XGUI_MainWindow::updateTitle()
+{
+  QString aTitle = myTitle;
+  if (!myCurrentDir.isNull())
+    aTitle += " - " + myCurrentDir;
+  if (myIsModified)
+    aTitle += "*";
+  setWindowTitle(aTitle);
+}
+
+void XGUI_MainWindow::setCurrentDir(const QString& theDir, bool toUpdate)
+{
+  myCurrentDir = theDir;
+  if (toUpdate)
+    updateTitle();
+}
+
+void XGUI_MainWindow::setModifiedState(bool isModified, bool toUpdate)
+{
+  myIsModified = isModified;
+  if (toUpdate)
+    updateTitle();
+}
+
+CloseEventWatcher::CloseEventWatcher(QObject* theParent)
+    : QObject(theParent)
+{
+}
+
+bool CloseEventWatcher::eventFilter(QObject *obj, QEvent *event)
+{
+  if (event->type() == QEvent::Close) {
+    emit widgetClosed();
+    event->ignore();
+    return true;
+  } else {
+    // standard event processing
+    return QObject::eventFilter(obj, event);
+  }
+}
+