Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_MainWindow.cpp
index 25ea218c9519b3771d8e7507805341737061f50f..7fc26f0bfb5f8fb38556a89734e28a7b2f32cdec 100644 (file)
@@ -9,38 +9,49 @@
 #include <PyConsole_EnhInterp.h>
 
 #include <QMdiArea>
-#include <QTreeWidget>
-#include <QDockWidget>
-#include <QTextEdit>
-#include <QLabel>
-#include <QToolBar>
-#include <QToolButton>
-#include <QTreeWidgetItem>
-#include <QLayout>
-#include <QLineEdit>
-#include <QGroupBox>
-#include <QFormLayout>
-#include <QDoubleSpinBox>
-#include <QPushButton>
-#include <QScrollArea>
-#include <QComboBox>
+#include <QMdiSubWindow>
 #include <QAction>
+#include <QDockWidget>
+#include <QApplication>
+#include <QTimer>
+#include <QCloseEvent>
+
 
 XGUI_MainWindow::XGUI_MainWindow(QWidget* parent)
-    : QMainWindow(parent), 
-    myObjectBrowser(NULL),
-    myPythonConsole(NULL),
-    myPropertyPanelDock(NULL)
+    : 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*)));
 
-  myViewer = new XGUI_Viewer(this);
+  // 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()));
 
-  createDockWidgets();
+  aAction = new QAction(QIcon(":pictures/cascade_views.png"), tr("Cascade"), aMdiArea);
+  aMdiArea->addAction(aAction);
+  connect(aAction, SIGNAL(triggered(bool)), this, SLOT(cascadeWindows()));
+
+  aAction = new QAction(aMdiArea);
+  aAction->setSeparator(true);
+  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*)));
 }
 
 XGUI_MainWindow::~XGUI_MainWindow(void)
@@ -54,123 +65,232 @@ QMdiArea* XGUI_MainWindow::mdiArea() const
 }
 
 //******************************************************
-void XGUI_MainWindow::showObjectBrowser()
+void XGUI_MainWindow::showPythonConsole()
 {
-  myObjectBrowser->parentWidget()->show();
+  // TODO: Check why PyConsole can not be created
+  if (!myPythonConsole) {
+    myPythonConsole = new PyConsole_EnhConsole(this, new PyConsole_EnhInterp());
+    myPythonConsole->setObjectName("PythonConsole");
+    undockPythonConsole();
+  }
+  myPythonConsole->parentWidget()->show();
 }
 
 //******************************************************
-void XGUI_MainWindow::hideObjectBrowser()
+void XGUI_MainWindow::hidePythonConsole()
 {
-  myObjectBrowser->parentWidget()->hide();
+  if (myPythonConsole)
+    myPythonConsole->parentWidget()->hide();
 }
 
 //******************************************************
-void XGUI_MainWindow::showPythonConsole()
+void XGUI_MainWindow::dockPythonConsole()
 {
-  if (!myPythonConsole) {
+  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);
+}
 
-    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());
-    //myPythonConsole = new QTextEdit(aDoc);
-    //myPythonConsole->setGeometry(0,0,200, 50);
-    //myPythonConsole->setText(">>>");
-    aDoc->setWidget(myPythonConsole);
-    //myPythonConsole->setMinimumHeight(0);
-    addDockWidget(Qt::TopDockWidgetArea, aDoc);
-    tabifyDockWidget(myMenuBar->getLastDockWindow(), aDoc);
+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();
   }
-  myPythonConsole->parentWidget()->show();
+  myMenuBar->insertConsole(myPythonConsole);
 }
 
 //******************************************************
-void XGUI_MainWindow::hidePythonConsole()
+void XGUI_MainWindow::createSubWindow()
 {
-  if (myPythonConsole)
-    myPythonConsole->parentWidget()->hide();
+  viewer()->createView();
 }
 
-void XGUI_MainWindow::showPropertyPanel()
+//******************************************************
+void XGUI_MainWindow::cascadeWindows()
 {
-  QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
-  //<! Restore ability to close panel from the window's menu
-  aViewAct->setEnabled(true);
-  myPropertyPanelDock->show();
-  myPropertyPanelDock->raise();
+  QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
+  QList<QMdiSubWindow*> aWindows = aMdiArea->subWindowList();
+
+  QSize aSize = aMdiArea->size();
+  QRect aRect = aMdiArea->geometry();
+  const int aOffset = 30;
+  int i = 0, j = 0;
+  int x, y;
+  int w = aSize.width() / 2;
+  int h = aSize.height() / 2;
+  QMdiSubWindow* aLastWnd;
+  foreach(QMdiSubWindow* aWnd, aWindows)
+  {
+    aWnd->showNormal();
+    aWnd->raise();
+    x = aOffset * i;
+    if ((x + w) > aSize.width()) {
+      x = 0;
+      i = 0;
+    }
+    y = aOffset * j;
+    if ((y + h) > aSize.height()) {
+      y = 0;
+      j = 0;
+    }
+    aWnd->setGeometry(QStyle::visualRect(aWnd->layoutDirection(), aRect, QRect(x, y, w, h)));
+    i++;
+    j++;
+    viewer()->onWindowActivated(aWnd);
+    aLastWnd = aWnd;
+    QApplication::processEvents();
+  }
+  aLastWnd->setFocus();
 }
 
-void XGUI_MainWindow::hidePropertyPanel()
+void XGUI_MainWindow::onViewCreated(XGUI_ViewWindow* theWindow)
 {
-  QAction* aViewAct = myPropertyPanelDock->toggleViewAction();
-  //<! Do not allow to show empty property panel
-  aViewAct->setEnabled(false);
-  myPropertyPanelDock->hide();
+  QWidget* aSubWindow = theWindow->parentWidget();
+  QWidget* aMDIWidget = centralWidget();
+
+  QAction* aAction = new QAction(aSubWindow->windowTitle(), aMDIWidget);
+  aAction->setCheckable(true);
+  connect(aAction, SIGNAL(triggered(bool)), this, SLOT(activateView()));
+  aMDIWidget->addAction(aAction);
+
+  QList<QAction*> aActions = aMDIWidget->actions();
+  foreach(QAction* aAct, aActions)
+  {
+    if (aAct->isCheckable())
+      aAct->setChecked(false);
+  }
+  aAction->setChecked(true);
 }
 
-/*
- * Creates dock widgets, places them in corresponding area
- * and tabifies if necessary.
- */
-void XGUI_MainWindow::createDockWidgets()
+void XGUI_MainWindow::onDeleteView(XGUI_ViewWindow* theWindow)
 {
-  QDockWidget* aObjDock = createObjectBrowser();
-  addDockWidget(Qt::LeftDockWidgetArea, aObjDock);
-  myPropertyPanelDock = createPropertyPanel();
-  addDockWidget(Qt::LeftDockWidgetArea, myPropertyPanelDock);
-  hidePropertyPanel(); //<! Invisible by default
-  hideObjectBrowser();
-  tabifyDockWidget(aObjDock, myPropertyPanelDock);
+  QWidget* aSubWindow = theWindow->parentWidget();
+  QString aTitle = aSubWindow->windowTitle();
+  QWidget* aMDIWidget = centralWidget();
+  QList<QAction*> aActions = aMDIWidget->actions();
+
+  QAction* aDelAct = 0;
+  foreach(QAction* aAct, aActions)
+  {
+    if (aAct->text() == aTitle) {
+      aDelAct = aAct;
+      break;
+    }
+  }
+  aMDIWidget->removeAction(aDelAct);
 }
 
+void XGUI_MainWindow::activateView()
+{
+  QAction* aAction = static_cast<QAction*>(sender());
+  QString aWndTitle = aAction->text();
+  QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
+
+  QList<QMdiSubWindow*> aWndList = aMdiArea->subWindowList();
+  QMdiSubWindow* aTargetView = 0;
+  foreach(QMdiSubWindow* aWnd, aWndList)
+  {
+    if (aWnd->windowTitle() == aWndTitle) {
+      aWnd->raise();
+      aWnd->activateWindow();
+      aTargetView = aWnd;
+      break;
+    }
+  }
+  QApplication::processEvents();
+  if (aTargetView)
+    QTimer::singleShot(20, aTargetView, SLOT(setFocus()));
+}
 
-QDockWidget* XGUI_MainWindow::createPropertyPanel()
+void XGUI_MainWindow::onViewActivated(QMdiSubWindow* theSubWnd)
 {
-  QDockWidget* aPropPanel = new QDockWidget(this);
-  aPropPanel->setWindowTitle(tr("Property Panel"));
-  aPropPanel->setObjectName(XGUI::PROP_PANEL);
+  if (!theSubWnd)
+    return;
+  QMdiArea* aMdiArea = static_cast<QMdiArea*>(centralWidget());
+  QString aWndTitle = theSubWnd->windowTitle();
+  QList<QAction*> aActionList = aMdiArea->actions();
+  foreach(QAction* aAct, aActionList)
+  {
+    if (aAct->isCheckable())
+      aAct->setChecked(aAct->text() == aWndTitle);
+  }
+}
 
-  QWidget* aContent = new QWidget(aPropPanel);
-  QVBoxLayout* aMainLay = new QVBoxLayout(aContent);
-  aMainLay->setContentsMargins(3, 3, 3, 3);
-  aPropPanel->setWidget(aContent);
+void XGUI_MainWindow::closeEvent(QCloseEvent * event)
+{
+  emit exitKeySequence();
+  event->ignore();
+}
 
-  QWidget* aCustomWidget = new QWidget(aContent);
-  aCustomWidget->setObjectName(XGUI::PROP_PANEL_WDG);
-  aMainLay->addWidget(aCustomWidget);
-  aMainLay->addStretch(1);
+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);
+}
 
-  QFrame* aFrm = new QFrame(aContent);
-  aFrm->setFrameStyle(QFrame::Sunken);
-  aFrm->setFrameShape(QFrame::Panel);
-  QHBoxLayout* aBtnLay = new QHBoxLayout(aFrm);
-  aBtnLay->setContentsMargins(0, 0, 0, 0);
-  aMainLay->addWidget(aFrm);
 
-  QPushButton* aBtn = new QPushButton(QIcon(":pictures/button_help.png"), "", aFrm);
-  aBtn->setFlat(true);
-  aBtnLay->addWidget(aBtn);
-  aBtnLay->addStretch(1);
-  aBtn = new QPushButton(QIcon(":pictures/button_ok.png"), "", aFrm);
-  aBtn->setObjectName(XGUI::PROP_PANEL_OK);
-  aBtn->setFlat(true);
-  aBtnLay->addWidget(aBtn);
-  aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
-  aBtn->setObjectName(XGUI::PROP_PANEL_CANCEL);
-  aBtn->setFlat(true);
-  aBtnLay->addWidget(aBtn);
+void XGUI_MainWindow::updateTitle()
+{
+  QString aTitle = myTitle;
+  if (!myCurrentDir.isNull())
+    aTitle += " - " + myCurrentDir;
+  if (myIsModified)
+    aTitle += "*";
+  setWindowTitle(aTitle);
+}
 
-  return aPropPanel;
+void XGUI_MainWindow::setCurrentDir(const QString& theDir, bool toUpdate)
+{
+  myCurrentDir = theDir;
+  if (toUpdate)
+    updateTitle();
 }
 
-QDockWidget* XGUI_MainWindow::createObjectBrowser()
+void XGUI_MainWindow::setModifiedState(bool isModified, bool toUpdate)
 {
-  QDockWidget* aObjDock = new QDockWidget(this);
-  aObjDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
-  aObjDock->setWindowTitle(tr("Object browser"));
-  myObjectBrowser = new XGUI_ObjectsBrowser(aObjDock);
-  aObjDock->setWidget(myObjectBrowser);
-  return aObjDock;
+  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);
+  }
+}
+