Salome HOME
Update copyrights 2014.
[samples/light.git] / src / LIGHTGUI / LIGHTGUI_Selection.cxx
1 // Copyright (C) 2005-2014  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, or (at your option) any later version.
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
20 // LIGHT : sample (no-corba-engine) SALOME module
21 // File   : LIGHTGUI_Selection.cxx
22 // Author : Julia DOROVSKIKH
23 //
24 #include "LIGHTGUI_Selection.h"
25 #include "LIGHTGUI_DataModel.h"
26 #include "LIGHTGUI.h"
27
28 #include <LightApp_Application.h>
29 #include <LightApp_Study.h>
30 #include <LightApp_DataOwner.h>
31 #include <LightApp_SelectionMgr.h>
32 #include <SOCC_ViewModel.h>
33 #include <SUIT_ViewManager.h>
34 #include <QtxPopupMgr.h>
35
36 /*!
37   \class LIGHTGUI_Selection
38   \brief Handles the data selection, builds the rules to be used in the
39          popup menu.
40 */
41
42 /*!
43   \brief Constructor.
44 */
45 LIGHTGUI_Selection::LIGHTGUI_Selection()
46 : LightApp_Selection()
47 {
48 }
49
50 /*!
51   \brief Destructor.
52 */
53 LIGHTGUI_Selection::~LIGHTGUI_Selection()
54 {
55 }
56
57 /*!
58   \brief Get selection parameter value.
59   \param index selected object index
60   \param p parameter name
61   \return parameter value
62 */
63 QVariant LIGHTGUI_Selection::parameter( const int index, const QString& p ) const
64 {
65   if ( p == "type" )
66     return type( index );
67   else if ( p == "empty" )
68     return isEmpty( index );
69   else if ( p == "visible" )
70     return isVisible( index );
71   return LightApp_Selection::parameter( p );
72 }
73
74 /*!
75   \brief Check if selected object is empty line (beginning of the paragraph)
76   \param index selected object index
77   \return \c true if selected object is empty line
78 */
79 bool LIGHTGUI_Selection::isEmpty( const int index ) const
80 {
81   bool empty = true;
82   LightApp_Application* app = dynamic_cast<LightApp_Application*>( study()->application() );
83   if ( app ) {
84     LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( app->activeModule()->dataModel() );
85     if ( dm ) {
86       QString line = dm->getLineText( LIGHTGUI_DataModel::id( entry( index ) ) );
87       empty = line.isEmpty();
88     }
89   }
90   return empty;
91 }
92
93 /*!
94   \brief Check if selected object is displayed in the active viewer.
95   \param index selected object index
96   \return \c true if selected object is displayed
97 */
98 bool LIGHTGUI_Selection::isVisible( const int index ) const
99 {
100   bool visible = false;
101   LightApp_Application* app = dynamic_cast<LightApp_Application*>( study()->application() );
102   if ( app ) {
103     SUIT_ViewManager* vm = app->activeViewManager();
104     if ( vm && vm->getType() == "OCCViewer" ) {
105       SOCC_Viewer* v = (SOCC_Viewer*)vm->getViewModel();  
106       if ( v ) {
107         QString e = entry( index );
108         int id = LIGHTGUI_DataModel::id( e );
109         visible = id != -1 && v->isVisible( new SALOME_InteractiveObject( e.toLatin1(), "" ) );
110       }
111     }
112   }
113   return visible;
114 }
115
116 /*!
117   \brief Get selected object type.
118   \param index selected object index
119   \return object type
120 */
121 QString LIGHTGUI_Selection::type( const int index ) const
122 {
123   return lineNb( entry( index ) ) > 0 ? "TextLine" : "Unknown";
124 }
125
126 /*!
127   \brief Get line number corresponding to the selected object.
128   \param index selected object index
129   \return line number or 0 if the pbject is not appropriate
130 */
131 int LIGHTGUI_Selection::lineNb( const QString& e ) const
132 {
133   int pos = 0; // invalid position
134   LightApp_Application* app = dynamic_cast<LightApp_Application*>( study()->application() );
135   if ( app ) {
136     LIGHTGUI_DataModel* dm = dynamic_cast<LIGHTGUI_DataModel*>( app->activeModule()->dataModel() );
137     if ( dm ) {
138       pos = dm->lineNb( e );
139     }
140   }
141   return pos;
142 }