Salome HOME
Merge from BR_V5_DEV 17Feb09
[samples/light.git] / src / LIGHTGUI / LIGHTGUI_Selection.cxx
1 //  Copyright (C) 2005-2008  OPEN CASCADE
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // LIGHT : sample (no-corba-engine) SALOME module
20 // File   : LIGHTGUI_Selection.cxx
21 // Author : Julia DOROVSKIKH
22 //
23 #include "LIGHTGUI_Selection.h"
24 #include "LIGHTGUI_DataModel.h"
25 #include "LIGHTGUI.h"
26
27 #include <LightApp_Application.h>
28 #include <LightApp_Study.h>
29 #include <LightApp_DataOwner.h>
30 #include <LightApp_SelectionMgr.h>
31 #include <SOCC_ViewModel.h>
32 #include <SUIT_ViewManager.h>
33 #include <QtxPopupMgr.h>
34
35 /*!
36   \class LIGHTGUI_Selection
37   \brief Handles the data selection, builds the rules to be used in the
38          popup menu.
39 */
40
41 /*!
42   \brief Constructor.
43 */
44 LIGHTGUI_Selection::LIGHTGUI_Selection()
45 : LightApp_Selection()
46 {
47 }
48
49 /*!
50   \brief Destructor.
51 */
52 LIGHTGUI_Selection::~LIGHTGUI_Selection()
53 {
54 }
55
56 /*!
57   \brief Get selection parameter value.
58   \param index selected object index
59   \param p parameter name
60   \return parameter value
61 */
62 QVariant LIGHTGUI_Selection::parameter( const int index, const QString& p ) const
63 {
64   if ( p == "type" )
65     return type( index );
66   else if ( p == "empty" )
67     return isEmpty( index );
68   else if ( p == "visible" )
69     return isVisible( index );
70   return LightApp_Selection::parameter( p );
71 }
72
73 /*!
74   \brief Check if selected object is empty line (beginning of the paragraph)
75   \param index selected object index
76   \return \c true if selected object is empty line
77 */
78 bool LIGHTGUI_Selection::isEmpty( const int index ) const
79 {
80   bool empty = true;
81   LightApp_Application* app = dynamic_cast<LightApp_Application*>( study()->application() );
82   if ( app ) {
83     LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( app->activeModule()->dataModel() );
84     if ( dm ) {
85       QString line = dm->getLineText( LIGHTGUI_DataModel::id( entry( index ) ) );
86       empty = line.isEmpty();
87     }
88   }
89   return empty;
90 }
91
92 /*!
93   \brief Check if selected object is displayed in the active viewer.
94   \param index selected object index
95   \return \c true if selected object is displayed
96 */
97 bool LIGHTGUI_Selection::isVisible( const int index ) const
98 {
99   bool visible = false;
100   LightApp_Application* app = dynamic_cast<LightApp_Application*>( study()->application() );
101   if ( app ) {
102     SUIT_ViewManager* vm = app->activeViewManager();
103     if ( vm && vm->getType() == "OCCViewer" ) {
104       SOCC_Viewer* v = (SOCC_Viewer*)vm->getViewModel();  
105       if ( v ) {
106         QString e = entry( index );
107         int id = LIGHTGUI_DataModel::id( e );
108         visible = id != -1 && v->isVisible( new SALOME_InteractiveObject( e.toLatin1(), "" ) );
109       }
110     }
111   }
112   return visible;
113 }
114
115 /*!
116   \brief Get selected object type.
117   \param index selected object index
118   \return object type
119 */
120 QString LIGHTGUI_Selection::type( const int index ) const
121 {
122   return lineNb( entry( index ) ) > 0 ? "TextLine" : "Unknown";
123 }
124
125 /*!
126   \brief Get line number corresponding to the selected object.
127   \param index selected object index
128   \return line number or 0 if the pbject is not appropriate
129 */
130 int LIGHTGUI_Selection::lineNb( const QString& e ) const
131 {
132   int pos = 0; // invalid position
133   LightApp_Application* app = dynamic_cast<LightApp_Application*>( study()->application() );
134   if ( app ) {
135     LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( app->activeModule()->dataModel() );
136     if ( dm ) {
137       pos = dm->lineNb( e );
138     }
139   }
140   return pos;
141 }