Salome HOME
Update copyrights 2014.
[samples/light.git] / src / LIGHTGUI / LIGHTGUI_Selection.cxx
index 93658248d3a3812351a6f9a143def72ff3974848..1fef02ca2c81a5719f49b44de8e602995671fb2c 100644 (file)
-//  LIGHT : sample (no-corba-engine) SALOME module
+// Copyright (C) 2005-2014  OPEN CASCADE
 //
-//  Copyright (C) 2003  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 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.
+// 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.
 //
-//  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
 //
-//  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
 //
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
-//
-//  Author : Julia DOROVSKIKH
-//  Date   : 01/01/2005
-//  $Header$
 
+// LIGHT : sample (no-corba-engine) SALOME module
+// File   : LIGHTGUI_Selection.cxx
+// Author : Julia DOROVSKIKH
+//
 #include "LIGHTGUI_Selection.h"
-#include "LightApp_DataOwner.h"
+#include "LIGHTGUI_DataModel.h"
+#include "LIGHTGUI.h"
 
+#include <LightApp_Application.h>
+#include <LightApp_Study.h>
+#include <LightApp_DataOwner.h>
 #include <LightApp_SelectionMgr.h>
+#include <SOCC_ViewModel.h>
+#include <SUIT_ViewManager.h>
+#include <QtxPopupMgr.h>
 
-//=================================================================================
-// function : LIGHTGUI_Selection()
-// purpose  : constructor
-//=================================================================================
-LIGHTGUI_Selection::LIGHTGUI_Selection ()
+/*!
+  \class LIGHTGUI_Selection
+  \brief Handles the data selection, builds the rules to be used in the
+         popup menu.
+*/
+
+/*!
+  \brief Constructor.
+*/
+LIGHTGUI_Selection::LIGHTGUI_Selection()
 : LightApp_Selection()
 {
 }
 
-//=================================================================================
-// function : ~LIGHTGUI_Selection()
-// purpose  : destructor
-//=================================================================================
+/*!
+  \brief Destructor.
+*/
 LIGHTGUI_Selection::~LIGHTGUI_Selection()
 {
 }
 
-//=================================================================================
-// function : init()
-// purpose  : initialization
-//=================================================================================
-void LIGHTGUI_Selection::init( const QString& client, LightApp_SelectionMgr* mgr)
+/*!
+  \brief Get selection parameter value.
+  \param index selected object index
+  \param p parameter name
+  \return parameter value
+*/
+QVariant LIGHTGUI_Selection::parameter( const int index, const QString& p ) const
 {
-  if ( mgr ) {
-    SUIT_DataOwnerPtrList sel;
-    mgr->selected( sel);
-    SUIT_DataOwnerPtrList::const_iterator anIt  = sel.begin(),
-                                          aLast = sel.end();
-    for ( ; anIt != aLast; anIt++ ) {
-      SUIT_DataOwner* owner = (SUIT_DataOwner*)( (*anIt).get() );
-      LightApp_DataOwner* sowner = dynamic_cast<LightApp_DataOwner*>( owner );
-      QString anEntry = sowner->entry();
-      int anIndex = anEntry.find("_");
-      int aPosition = (anEntry.mid(anIndex+1, anEntry.length() - anIndex)).toInt();//lineNb();
-      if ( sowner && aPosition > 0 )
-        myTypes.append( "TextLine" );
-      else
-        myTypes.append( "Unknown" );
+  if ( p == "type" )
+    return type( index );
+  else if ( p == "empty" )
+    return isEmpty( index );
+  else if ( p == "visible" )
+    return isVisible( index );
+  return LightApp_Selection::parameter( p );
+}
+
+/*!
+  \brief Check if selected object is empty line (beginning of the paragraph)
+  \param index selected object index
+  \return \c true if selected object is empty line
+*/
+bool LIGHTGUI_Selection::isEmpty( const int index ) const
+{
+  bool empty = true;
+  LightApp_Application* app = dynamic_cast<LightApp_Application*>( study()->application() );
+  if ( app ) {
+    LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( app->activeModule()->dataModel() );
+    if ( dm ) {
+      QString line = dm->getLineText( LIGHTGUI_DataModel::id( entry( index ) ) );
+      empty = line.isEmpty();
     }
   }
-  LightApp_Selection::init(client, mgr);
+  return empty;
 }
 
-//=================================================================================
-// function : count()
-// purpose  : returns number of supported types
-//=================================================================================
-int LIGHTGUI_Selection::count() const
+/*!
+  \brief Check if selected object is displayed in the active viewer.
+  \param index selected object index
+  \return \c true if selected object is displayed
+*/
+bool LIGHTGUI_Selection::isVisible( const int index ) const
 {
-  return myTypes.count();
+  bool visible = false;
+  LightApp_Application* app = dynamic_cast<LightApp_Application*>( study()->application() );
+  if ( app ) {
+    SUIT_ViewManager* vm = app->activeViewManager();
+    if ( vm && vm->getType() == "OCCViewer" ) {
+      SOCC_Viewer* v = (SOCC_Viewer*)vm->getViewModel();  
+      if ( v ) {
+       QString e = entry( index );
+       int id = LIGHTGUI_DataModel::id( e );
+       visible = id != -1 && v->isVisible( new SALOME_InteractiveObject( e.toLatin1(), "" ) );
+      }
+    }
+  }
+  return visible;
+}
+
+/*!
+  \brief Get selected object type.
+  \param index selected object index
+  \return object type
+*/
+QString LIGHTGUI_Selection::type( const int index ) const
+{
+  return lineNb( entry( index ) ) > 0 ? "TextLine" : "Unknown";
 }
 
-//=================================================================================
-// function : param()
-// purpose  : returns selection parameter value
-//=================================================================================
-QtxValue LIGHTGUI_Selection::param ( const int ind, const QString& p ) const
+/*!
+  \brief Get line number corresponding to the selected object.
+  \param index selected object index
+  \return line number or 0 if the pbject is not appropriate
+*/
+int LIGHTGUI_Selection::lineNb( const QString& e ) const
 {
-  if ( p == "client" )
-    return globalParam(p);
-  else
-    return myTypes[ind];
+  int pos = 0; // invalid position
+  LightApp_Application* app = dynamic_cast<LightApp_Application*>( study()->application() );
+  if ( app ) {
+    LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( app->activeModule()->dataModel() );
+    if ( dm ) {
+      pos = dm->lineNb( e );
+    }
+  }
+  return pos;
 }