Salome HOME
Handle Alt+F4 as regular exit action. Do not exit on exit->save->cancel. Fixes #43
[modules/shaper.git] / src / XGUI / XGUI_MainWindow.cpp
index 0e6ee35b8e93f2b1c26a53ac2cac8ce072367f34..1400fc0ccc7f4c502b092cbdae6bc5b93cd4532b 100644 (file)
@@ -1,53 +1,56 @@
 #include "XGUI_MainWindow.h"
+#include "XGUI_Constants.h"
 #include "XGUI_MainMenu.h"
 #include "XGUI_ViewWindow.h"
 #include "XGUI_Viewer.h"
+#include "XGUI_ObjectsBrowser.h"
 
 #include <PyConsole_Console.h>
 #include <PyConsole_EnhInterp.h>
 
 #include <QMdiArea>
-#include <QTreeWidget>
+#include <QMdiSubWindow>
+#include <QAction>
 #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 <QApplication>
+#include <QTimer>
+#include <QCloseEvent>
 
 XGUI_MainWindow::XGUI_MainWindow(QWidget* parent)
     : QMainWindow(parent), 
-    myObjectBrowser(0), 
     myPythonConsole(0)
 {
-  setWindowTitle(tr("WINDOW_TITLE"));
+  setWindowTitle(tr("New Geom"));
   myMenuBar = new XGUI_MainMenu(this);
 
-  QDockWidget* aDoc = new QDockWidget(this);
-  aDoc->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
-  aDoc->setWindowTitle(tr("OBJECT_BROWSER_TITLE"));
-  myObjectBrowser = new QTreeWidget(aDoc);
-  myObjectBrowser->setColumnCount(1);
-  myObjectBrowser->setHeaderHidden(true);
-  aDoc->setWidget(myObjectBrowser);
-  addDockWidget(Qt::LeftDockWidgetArea, aDoc);
-  //aDoc->hide();
-
   QMdiArea* aMdiArea = new QMdiArea(this);
+  aMdiArea->setContextMenuPolicy(Qt::ActionsContextMenu);
   setCentralWidget(aMdiArea);
+  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()));
+
+  aAction = new QAction(aMdiArea);
+  aAction->setSeparator(true);
+  aMdiArea->addAction(aAction);
 
   myViewer = new XGUI_Viewer(this);
-
-  //fillObjectBrowser();
-  //addPropertyPanel();
+  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)
@@ -60,18 +63,6 @@ QMdiArea* XGUI_MainWindow::mdiArea() const
   return static_cast<QMdiArea*>(centralWidget());
 }
 
-//******************************************************
-void XGUI_MainWindow::showObjectBrowser()
-{
-  myObjectBrowser->parentWidget()->show();
-}
-
-//******************************************************
-void XGUI_MainWindow::hideObjectBrowser()
-{
-  myObjectBrowser->parentWidget()->hide();
-}
-
 //******************************************************
 void XGUI_MainWindow::showPythonConsole()
 {
@@ -82,11 +73,7 @@ void XGUI_MainWindow::showPythonConsole()
     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);
   }
@@ -100,145 +87,121 @@ void XGUI_MainWindow::hidePythonConsole()
     myPythonConsole->parentWidget()->hide();
 }
 
-
-
 //******************************************************
-
-// TEST FUNCTIONS
+void XGUI_MainWindow::createSubWindow()
+{
+  viewer()->createView();
+}
 
 //******************************************************
-void XGUI_MainWindow::fillObjectBrowser()
+void XGUI_MainWindow::cascadeWindows()
 {
-  QStringList aNames;
-  aNames << "Parameters" << "Constructions";
-  aNames << "Part 1" << "Part 2" << "Part 3";
-  aNames << "Properties";
-
-  QStringList aIcons;
-  aIcons << ":pictures/params_folder.png";
-  aIcons << ":pictures/constr_folder.png";
-  aIcons << ":pictures/part_ico.png";
-  aIcons << ":pictures/part_ico.png";
-  aIcons << ":pictures/part_ico.png";
-  aIcons << ":pictures/properties.png";
-
-  QList<QTreeWidgetItem*> aItems;
-  foreach(QString aName, aNames)
-  {
-    QTreeWidgetItem* aItem = new QTreeWidgetItem(myObjectBrowser);
-    aItem->setText(0, aName);
-    aItems.append(aItem);
-  }
-  for(int i = 0; i < aItems.length(); i++) {
-    aItems[i]->setIcon(0, QIcon(aIcons[i]));
+  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();
   }
-  myObjectBrowser->addTopLevelItems(aItems);
+  aLastWnd->setFocus();
+}
 
-  for(int i = 2; i < 5; i++) {
-    QTreeWidgetItem* aItem = new QTreeWidgetItem(aItems[i]);
-    aItem->setText(0, "Parameters");
-    aItem->setIcon(0, QIcon(":pictures/params_folder.png"));
+void XGUI_MainWindow::onViewCreated(XGUI_ViewWindow* theWindow)
+{
+  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);
+}
 
-    aItem = new QTreeWidgetItem(aItems[i]);
-    aItem->setText(0, "Construction");
-    aItem->setIcon(0, QIcon(":pictures/constr_folder.png"));
+void XGUI_MainWindow::onDeleteView(XGUI_ViewWindow* theWindow)
+{
+  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);
+}
 
-    aItem = new QTreeWidgetItem(aItems[i]);
-    aItem->setText(0, "Bodies");
-    aItem->setIcon(0, QIcon(":pictures/part_ico.png"));
+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()));
+}
 
-    aItem = new QTreeWidgetItem(aItems[i]);
-    aItem->setText(0, "Features");
-    aItem->setIcon(0, QIcon(":pictures/features.png"));
+void XGUI_MainWindow::onViewActivated(QMdiSubWindow* theSubWnd)
+{
+  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);
   }
 }
 
-void XGUI_MainWindow::addPropertyPanel()
+void XGUI_MainWindow::closeEvent(QCloseEvent * event)
 {
-  QDockWidget* aPropPanel = new QDockWidget(this);
-  aPropPanel->setWindowTitle("Property Panel");
-
-  QWidget* aContent = new QWidget(aPropPanel);
-  QVBoxLayout* aMainLay = new QVBoxLayout(aContent);
-  aMainLay->setContentsMargins(3, 3, 3, 3);
-  aPropPanel->setWidget(aContent);
-
-  QWidget* aCustomWidget = new QWidget(aContent);
-  aCustomWidget->setObjectName("PropertyPanelWidget");
-  aMainLay->addWidget(aCustomWidget);
-
-  /*
-  QWidget* aNameWgt = new QWidget(aContent);
-  QHBoxLayout* aNameLay = new QHBoxLayout(aNameWgt);
-  aNameLay->setContentsMargins(0,0,0,0);
-  aMainLay->addWidget(aNameWgt);
-
-  aNameLay->addWidget(new QLabel("Name", aNameWgt));
-  aNameLay->addWidget(new QLineEdit(aNameWgt));
-  */
-
-/* Moved into PartSetModule by sbh
-  QComboBox* aCombo = new QComboBox(aContent);
-  aCombo->addItem("By coordinates");
-  aMainLay->addWidget(aCombo);
-
-  QWidget* aGrpBox1 = new QWidget(aContent);
-  QFormLayout* aFrmLay = new QFormLayout(aGrpBox1);
-  aFrmLay->setContentsMargins(0, 6, 0, 0);
-  aMainLay->addWidget(aGrpBox1);
-
-  QLabel* aLbl = new QLabel(aGrpBox1);
-  aLbl->setPixmap(QPixmap(":pictures/x_point.png"));
-  aFrmLay->addRow(aLbl, new QDoubleSpinBox(aGrpBox1));
-
-  aLbl = new QLabel(aGrpBox1);
-  aLbl->setPixmap(QPixmap(":pictures/y_point.png"));
-  aFrmLay->addRow(aLbl, new QDoubleSpinBox(aGrpBox1));
-
-  aLbl = new QLabel(aGrpBox1);
-  aLbl->setPixmap(QPixmap(":pictures/z_point.png"));
-  aFrmLay->addRow(aLbl, new QDoubleSpinBox(aGrpBox1));
-  */
-
-  /*
-  aGrpBox1 = new QGroupBox("Normal vector", aContent);
-  aGrpBox1->setFlat(true);
-  aFrmLay = new QFormLayout(aGrpBox1);
-  aFrmLay->setContentsMargins(0, 6, 0, 0);
-  aMainLay->addWidget(aGrpBox1);
-
-  aLbl = new QLabel(aGrpBox1);
-  aLbl->setPixmap(QPixmap(":pictures/x_size.png"));
-  aFrmLay->addRow(aLbl, new QDoubleSpinBox(aGrpBox1));
-
-  aLbl = new QLabel(aGrpBox1);
-  aLbl->setPixmap(QPixmap(":pictures/y_size.png"));
-  aFrmLay->addRow(aLbl, new QDoubleSpinBox(aGrpBox1));
-
-  aLbl = new QLabel(aGrpBox1);
-  aLbl->setPixmap(QPixmap(":pictures/z_size.png"));
-  aFrmLay->addRow(aLbl, new QDoubleSpinBox(aGrpBox1));
-  */
-
-  aMainLay->addStretch(1);
-
-  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->setFlat(true);
-  aBtnLay->addWidget(aBtn);
-  aBtn = new QPushButton(QIcon(":pictures/button_cancel.png"), "", aFrm);
-  aBtn->setFlat(true);
-  aBtnLay->addWidget(aBtn);
-
-  addDockWidget(Qt::RightDockWidgetArea, aPropPanel);
+  emit exitKeySequence();
+  event->ignore();
 }