Salome HOME
debug of profileOp
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ListSelector.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ListSelector.h"
20
21 #include "HYDROGUI_OrderedListWidget.h"
22
23 #ifdef TEST_MODE
24   #include <SUIT_DataOwner.h>
25   class TestOwner : public SUIT_DataOwner
26   {
27   public:
28     TestOwner( const QString& entry ) { myEntry = entry; }
29     virtual ~TestOwner() {}
30
31     QString keyString() const { return myEntry; }
32     QString entry() const { return myEntry; }
33
34   private:
35     QString myEntry;
36   };
37   #define OWNER_CLASS TestOwner
38
39 #else
40   #include <LightApp_DataOwner.h>
41   #define OWNER_CLASS LightApp_DataOwner
42 #endif
43
44
45 HYDROGUI_ListSelector::HYDROGUI_ListSelector( HYDROGUI_OrderedListWidget* theListWidget,
46                                               SUIT_SelectionMgr* theSelectionMgr )
47 : SUIT_Selector( theSelectionMgr, theListWidget ),
48   myListWidget( theListWidget )
49 {
50   if ( myListWidget ) {
51     connect( myListWidget, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
52   }
53 }
54
55 /**
56   Destructor.
57 */
58 HYDROGUI_ListSelector::~HYDROGUI_ListSelector()
59 {
60 }
61
62 /*!
63   Get selector type.
64   @return the selector type
65 */
66 QString HYDROGUI_ListSelector::type() const
67
68   return "ListSelector"; 
69 }
70
71 /**
72 */
73 void HYDROGUI_ListSelector::getSelection( SUIT_DataOwnerPtrList& theList ) const
74 {
75   QStringList aSelectedEntries = myListWidget->getSelectedEntries();
76
77   foreach ( const QString& anEntry, aSelectedEntries ) {
78     if ( !anEntry.isEmpty() ) {
79       theList.append( SUIT_DataOwnerPtr( new OWNER_CLASS( anEntry ) ) );
80     }
81   }
82 }
83
84 /**
85 */
86 void HYDROGUI_ListSelector::setSelection( const SUIT_DataOwnerPtrList& theList )
87 {
88   if ( !myListWidget ) {
89     return;
90   }
91
92   QStringList aSelectedEntries;
93   SUIT_DataOwnerPtrList::const_iterator anIt = theList.begin();
94   for ( ; anIt != theList.end(); ++anIt ) {
95     const OWNER_CLASS* anOwner = dynamic_cast<const OWNER_CLASS*>( (*anIt).operator->() );
96     if ( anOwner ) {
97       aSelectedEntries << anOwner->entry();
98     }
99   }
100  
101   myListWidget->setSelectedEntries( aSelectedEntries );
102 }
103
104 /**
105   Called when the list selection is changed.
106 */
107 void HYDROGUI_ListSelector::onSelectionChanged()
108 {
109   selectionChanged();
110 }