Salome HOME
Copyright update 2022
[modules/gui.git] / src / SUIT / SUIT_Desktop.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 3d41900..6058b18
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
 #include "SUIT_Desktop.h"
 
-#include "SUIT_Tools.h"
 #include "SUIT_ViewWindow.h"
 
+#include <QtxLogoMgr.h>
 #include <QtxActionMenuMgr.h>
 #include <QtxActionToolMgr.h>
 
-#include <qtoolbar.h>
-#include <qmenubar.h>
-#include <qdockarea.h>
-#include <qstatusbar.h>
-#include <qapplication.h>
-#include <qhbox.h>
-#include <qlabel.h>
-#include <qstyle.h>
-
-/*!
- Class: SUIT_Desktop::LogoMgr
- Level: Internal
-*/
-
-class SUIT_Desktop::LogoMgr : public QObject
-{
-public:
-  LogoMgr( QMenuBar* );
-  virtual ~LogoMgr();
-
-  int                    count() const;
-
-  void                   insert( const QString&, const QPixmap&, const int = -1 );
-  void                   remove( const QString& );
-  void                   clear();
-
-  QMenuBar*              menuBar() const;
-
-private:
-  void                   generate();
-  int                    find( const QString& ) const;
-
-private:
-  typedef struct { QString id; QPixmap pix; } LogoInfo;
-  typedef QValueList<LogoInfo>                LogoList;
-
-private:
-  int                    myId;
-  QMenuBar*              myMenus;
-  LogoList               myLogos;
-};
-
-SUIT_Desktop::LogoMgr::LogoMgr( QMenuBar* mb )
-: QObject( mb ),
-myMenus( mb ),
-myId( 0 )
-{
-} 
-
-SUIT_Desktop::LogoMgr::~LogoMgr()
-{
-}
-
-QMenuBar* SUIT_Desktop::LogoMgr::menuBar() const
-{
-  return myMenus;
-}
-
-int SUIT_Desktop::LogoMgr::count() const
-{
-  return myLogos.count();
-}
-
-void SUIT_Desktop::LogoMgr::insert( const QString& id, const QPixmap& pix, const int index )
-{
-  if ( pix.isNull() )
-    return;
-
-  LogoInfo* inf = 0;
-
-  int idx = find( id );
-  if ( idx < 0 )
-  {
-    idx = index < (int)myLogos.count() ? index : -1;
-    if ( idx < 0 )
-      inf = &( *myLogos.append( LogoInfo() ) );
-    else
-      inf = &( *myLogos.insert( myLogos.at( idx ), LogoInfo() ) );
-  }
-  else
-    inf = &( *myLogos.at( idx ) );
-
-
-  inf->id = id;
-  inf->pix = pix;
-
-  generate();
-}
-
-void SUIT_Desktop::LogoMgr::remove( const QString& id )
-{
-  int idx = find( id );
-  if ( idx < 0 )
-    return;
-
-  myLogos.remove( myLogos.at( idx ) );
-
-  generate();
-}
-
-void SUIT_Desktop::LogoMgr::clear()
-{
-  myLogos.clear();
-  generate();
-}
-
-void SUIT_Desktop::LogoMgr::generate()
-{
-  if ( !menuBar() )
-    return;
-
-  if ( myId ) 
-    menuBar()->removeItem( myId );
-
-  myId = 0;
-
-  if ( myLogos.isEmpty() )
-    return;
-
-  class LogoBox : public QHBox
-  {
-  public:
-    LogoBox( QWidget* parent = 0, const char* name = 0, WFlags f = 0 ) : QHBox( parent, name, f ) {};
-
-    void addSpacing( int spacing )
-    {
-      QApplication::sendPostedEvents( this, QEvent::ChildInserted );
-      ((QHBoxLayout*)layout())->addSpacing( spacing );
-    }
-
-  protected:
-    void drawContents( QPainter* p )
-    {
-      if ( parentWidget()->inherits( "QMenuBar" ) )
-        style().drawControl( QStyle::CE_MenuBarEmptyArea, p, this, contentsRect(), colorGroup() );
-      else
-        QHBox::drawContents( p );
-    }
-  };
-
-  LogoBox* cnt = new LogoBox( menuBar() );
-  cnt->setSpacing( 2 );
-
-  for ( LogoList::const_iterator it = myLogos.begin(); it != myLogos.end(); ++it )
-  {
-    QLabel* logoLab = new QLabel( cnt );
-    logoLab->setPixmap( (*it).pix );
-    logoLab->setScaledContents( false );
-    logoLab->setAlignment( QLabel::AlignCenter ); 
-  }
-  QApplication::sendPostedEvents( cnt, QEvent::ChildInserted );
-  cnt->addSpacing( 2 );
-
-  myId = menuBar()->insertItem( cnt );
-
-  QApplication::sendPostedEvents( menuBar()->parentWidget(), QEvent::LayoutHint );
-  QApplication::postEvent( menuBar()->parentWidget(), new QEvent( QEvent::LayoutHint ) );
-}
-
-int SUIT_Desktop::LogoMgr::find( const QString& id ) const
-{
-  int idx = -1;
-  for ( uint i = 0; i < myLogos.count() && idx < 0; i++ )
-  {
-    if ( (*myLogos.at( i ) ).id == id )
-      idx = i;
-  }
-  return idx;
-}
+#include <QPointer>
+#include <QCloseEvent>
+#include <QApplication>
 
 /*!\class SUIT_Desktop
  * Provide desktop management:\n
@@ -183,6 +39,16 @@ int SUIT_Desktop::LogoMgr::find( const QString& id ) const
  * \li windows
  */
 
+class SUIT_Desktop::ReparentEvent : public QEvent
+{
+public:
+  ReparentEvent( Type t, QObject* obj ) : QEvent( t ), myObj( obj ) {};
+
+  QObject* object() const { return myObj; }
+
+private:
+  QPointer<QObject> myObj;
+};
 
 /*!
   Constructor.
@@ -192,7 +58,7 @@ SUIT_Desktop::SUIT_Desktop()
 {
   myMenuMgr = new QtxActionMenuMgr( this );
   myToolMgr = new QtxActionToolMgr( this );
-  myLogoMgr = new LogoMgr( menuBar() );
+  myLogoMgr = new QtxLogoMgr( menuBar() );
 }
 
 /*!
@@ -218,9 +84,14 @@ bool SUIT_Desktop::event( QEvent* e )
   case QEvent::WindowDeactivate:
     emit deactivated();
     break;
+ /*case QEvent::Move:
+    emit moved();
+    break;*/
+  default:
+    break;
   }
 
-  return QMainWindow::event( e );
+  return QtxMainWindow::event( e );
 }
 
 /*!
@@ -237,16 +108,37 @@ void SUIT_Desktop::closeEvent( QCloseEvent* e )
 */
 void SUIT_Desktop::childEvent( QChildEvent* e )
 {
-  if ( e->type() == QEvent::ChildInserted && parentArea() &&
-       e->child()->isWidgetType() && e->child()->inherits( "SUIT_ViewWindow" ) )
-  {
-    QWidget* wid = (QWidget*)e->child();
-    bool vis = wid->isVisibleTo( wid->parentWidget() );
-    wid->reparent( parentArea(), QPoint( 0, 0 ), vis );
-    wid->setShown( vis );
+  if ( e->type() == QEvent::ChildAdded && e->child()->isWidgetType() ) {
+    // The following line is a workaround to avoid showing view window as a top-level window
+    // before re-parenting it to workstack (issue #23467).
+    // See SUIT_ViewWindow::setVisible() and SUIT_Desktop::customEvent().
+    e->child()->setProperty("blockShow", true );
+    QApplication::postEvent( this, new ReparentEvent( QEvent::Type( Reparent ), e->child() ) );
   }
-  else
+  else {
     QtxMainWindow::childEvent( e );
+  }
+}
+
+void SUIT_Desktop::customEvent( QEvent* e )
+{
+  if ( (int)e->type() != Reparent )
+    return;
+
+  ReparentEvent* re = (ReparentEvent*)e;
+  SUIT_ViewWindow* wid = ::qobject_cast<SUIT_ViewWindow*>( re->object() );
+  if ( wid )
+  {
+    bool invis = wid->testAttribute( Qt::WA_WState_ExplicitShowHide ) &&
+                 wid->testAttribute( Qt::WA_WState_Hidden );
+
+    // The following line is a workaround to avoid showing view window as a top-level window
+    // before re-parenting it to workstack (issue #23467).
+    // See SUIT_ViewWindow::setVisible() and SUIT_Desktop::childEvent().
+    wid->setProperty("blockShow", false);
+    addWindow( wid );
+    wid->setVisible( !invis );
+  }
 }
 
 /*!
@@ -265,11 +157,21 @@ QtxActionToolMgr* SUIT_Desktop::toolMgr() const
   return myToolMgr;
 }
 
+/*!
+  Gets logo manager.
+*/
+QtxLogoMgr* SUIT_Desktop::logoMgr() const
+{
+  return myLogoMgr;
+}
+
 /*!
   Returns the count of the existed logos.
 */
 int SUIT_Desktop::logoCount() const
 {
+  return 0;
+
   if ( !myLogoMgr )
     return 0;
   else
@@ -277,23 +179,12 @@ int SUIT_Desktop::logoCount() const
 }
 
 /*!
-  Adds new logo to the menu bar area.
-  Obsolete. Not should be used.
-  Use SUIT_Desktop::logoInsert();
-*/
-void SUIT_Desktop::addLogo( const QString& id, const QPixmap& pix )
-{
-  logoInsert( id, pix );
-}
-
-/*!
-  Removes a logo.
-  Obsolete. Not should be used.
-  Use SUIT_Desktop::logoRemove();
+  Adds new logo to the menu bar area
 */
-void SUIT_Desktop::removeLogo( const QString& id )
+void SUIT_Desktop::logoInsert( const QString& logoID, QMovie* logo, const int idx )
 {
-  logoRemove( id );
+  if ( myLogoMgr )
+    myLogoMgr->insert( logoID, logo, idx );
 }
 
 /*!
@@ -323,4 +214,26 @@ void SUIT_Desktop::logoClear()
     myLogoMgr->clear();
 }
 
+/*!
+  Emits activated signal
+*/
+void SUIT_Desktop::emitActivated()
+{
+  emit activated();
+}
+
+/*!
+  Emits message signal
+*/
+void SUIT_Desktop::emitMessage( const QString& theMessage )
+{
+  emit message( theMessage );
+}
 
+/*!
+  Activate window (default implementation just sets focus to the window.
+*/
+void SUIT_Desktop::setActiveWindow(SUIT_ViewWindow* wnd)
+{
+  if (wnd) wnd->setFocus();
+}