Salome HOME
debug of local CS
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_OrderedListWidget.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_OrderedListWidget.h"
24
25 #include "HYDROGUI_ListModel.h"
26
27 #include <SUIT_Session.h>
28 #include <SUIT_ResourceMgr.h>
29
30 #include <QLayout>
31 #include <QListView>
32 #include <QPushButton>
33 #include <QSignalMapper>
34 #include <QSortFilterProxyModel>
35
36
37 /**
38   Constructor.
39   @param theParent the parent widget
40 */
41 HYDROGUI_OrderedListWidget::HYDROGUI_OrderedListWidget( QWidget* theParent )
42 : QWidget( theParent )
43 {
44   // Main layout
45   QHBoxLayout* aMainLayout = new QHBoxLayout( this );
46   aMainLayout->setSpacing( 5 );
47
48   // List view
49   myList = new QListView( this );
50   myList->setSelectionMode( QAbstractItemView::ExtendedSelection );
51   // enable drag and drop
52   myList->setDragEnabled( true );
53   myList->setAcceptDrops( true );
54   // configure drag and drop
55   myList->setDropIndicatorShown( true );
56   myList->setDragDropMode( QAbstractItemView::InternalMove );
57
58   // Set the custom model
59   HYDROGUI_ListModel* aModel = new HYDROGUI_ListModel();
60   QSortFilterProxyModel* aFilteredModel = new QSortFilterProxyModel();
61   aFilteredModel->setSourceModel( aModel );
62   aFilteredModel->setFilterKeyColumn( 0 );
63   aFilteredModel->setFilterRole( HYDROGUI_VisibleRole );
64
65   myList->setModel( aFilteredModel );
66
67   // Buttons top, up, down, bottom
68   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
69   myTop = new QPushButton( this );
70   myTop->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_TOP_ICO" ) ) );
71   myTop->setIconSize( QSize( 32, 32 ) );
72   myTop->setToolTip( tr( "TOP_TLT" ) );
73   myUp = new QPushButton( this );
74   myUp->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_UP_ICO" ) ) );
75   myUp->setIconSize( myTop->iconSize() );
76   myUp->setToolTip( tr( "UP_TLT" ) );
77   myDown = new QPushButton( this );
78   myDown->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_DOWN_ICO" ) ) );
79   myDown->setIconSize( myTop->iconSize() );
80   myDown->setToolTip( tr( "DOWN_TLT" ) );
81   myBottom = new QPushButton( this );
82   myBottom->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_BOTTOM_ICO" ) ) );
83   myBottom->setIconSize( myTop->iconSize() );
84   myBottom->setToolTip( tr( "BOTTOM_TLT" ) );
85
86   // Layout
87   // buttons
88   QVBoxLayout* aListButtonsLayout = new QVBoxLayout();
89   aListButtonsLayout->addWidget( myTop );
90   aListButtonsLayout->addWidget( myUp );
91   aListButtonsLayout->addWidget( myDown );
92   aListButtonsLayout->addWidget( myBottom );
93   aListButtonsLayout->addStretch();
94   // main
95   aMainLayout->addWidget( myList );
96   aMainLayout->addLayout( aListButtonsLayout );
97
98   // Connections
99   QSignalMapper* aSignalMapper = new QSignalMapper( this );
100   aSignalMapper->setMapping( myTop, HYDROGUI_ListModel::Top );
101   aSignalMapper->setMapping( myUp, HYDROGUI_ListModel::Up );
102   aSignalMapper->setMapping( myDown, HYDROGUI_ListModel::Down );
103   aSignalMapper->setMapping( myBottom, HYDROGUI_ListModel::Bottom );
104   connect( myTop, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
105   connect( myUp, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
106   connect( myDown, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
107   connect( myBottom, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
108   connect( aSignalMapper, SIGNAL( mapped( int ) ), this, SLOT( onMove( int ) ) );
109
110   connect ( myList->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ), 
111             this, SIGNAL( selectionChanged() ) );
112
113   // Initialize
114   setHiddenObjectsShown( true );
115 }
116
117 /**
118   Destructor.
119 */
120 HYDROGUI_OrderedListWidget::~HYDROGUI_OrderedListWidget()
121 {
122 }
123
124 /**
125   Set the list of objects (which supposed to be ordered by the widget).
126   @param theObjects the list of pairs (object; visibility)
127 */
128 void HYDROGUI_OrderedListWidget::setObjects( const HYDROGUI_ListModel::Object2VisibleList& theObjects )
129 {
130   HYDROGUI_ListModel* aModel = getSourceModel();
131   if( aModel ) {
132     aModel->setObjects( theObjects );
133   }
134 }
135
136 /**
137   Returns the ordered list of objects.
138   @return the list of objects
139 */
140 QList<Handle(HYDROData_Entity)> HYDROGUI_OrderedListWidget::getObjects() const
141 {
142   QList<Handle(HYDROData_Entity)> anObjects;
143
144   HYDROGUI_ListModel* aModel = getSourceModel();
145   if( aModel ) {
146     anObjects = aModel->getObjects();
147   }
148
149   return anObjects;
150 }
151
152 /**
153   Set whether the hidden objects are presented in the list.
154   @param theIsToShow if true - the hidden objects will be shown in the list
155 */
156 void HYDROGUI_OrderedListWidget::setHiddenObjectsShown( const bool theIsToShow )
157 {
158   myIsHiddenObjectsShown = theIsToShow; 
159
160   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
161   QString anExpr = theIsToShow ? "true|false" : "true";
162   aFilterModel->setFilterRegExp( anExpr );
163 }
164
165 /**
166   Get entries of the selected objects.
167   @return the list of entries
168 */
169 QStringList HYDROGUI_OrderedListWidget::getSelectedEntries() const
170
171   QStringList anEntries;
172
173   QSortFilterProxyModel* aFilterModel = 
174     dynamic_cast<QSortFilterProxyModel*>( myList->model() );
175   if( aFilterModel ) {
176     HYDROGUI_ListModel* aSourceModel = 
177       dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
178     if ( aSourceModel ) {
179       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
180       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
181         QModelIndex aSourceIndex = aFilterModel->mapToSource( anIndex );
182         QString anEntry = aSourceModel->data( aSourceIndex, HYDROGUI_EntryRole ).toString();
183         anEntries << anEntry;
184       }
185     }
186   }
187
188   return anEntries;
189 }
190
191 /**
192   Set objects with the given entries selected (other objects will deselected).
193   @param theEntries the list of entries
194 */
195 void HYDROGUI_OrderedListWidget::setSelectedEntries( const QStringList& theEntries ) const
196 {
197   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
198   if( !aFilterModel ) {
199     return;
200   }
201   HYDROGUI_ListModel* aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
202   if ( !aSourceModel ) {
203     return;
204   }
205   QItemSelectionModel* aSelectionModel = myList->selectionModel();
206   if ( !aSelectionModel ) {
207     return;
208   }
209   
210   for ( int aRow = 0 ; aRow < aSourceModel->rowCount() ; aRow++ ) {
211     QModelIndex anIndex = aSourceModel->index( aRow, 0 );
212     if ( !anIndex.isValid() ) {
213       continue;
214     }
215
216     QString anEntry = aSourceModel->data( anIndex, HYDROGUI_EntryRole ).toString();
217
218     bool isToSelect = theEntries.contains( anEntry );
219     QModelIndex aProxyModelIndex = aFilterModel->mapFromSource( anIndex );
220     QItemSelectionModel::SelectionFlags aSelectionFlags =
221       isToSelect ? QItemSelectionModel::Select : QItemSelectionModel::Deselect;
222     aSelectionModel->select( aProxyModelIndex, aSelectionFlags );
223   }
224 }
225
226 /**
227   Slot called on top, up, down and bottom button click.
228   @param theType the move operation type
229 */
230 void HYDROGUI_OrderedListWidget::onMove( int theType )
231 {
232   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
233   if( aFilterModel ) {
234     HYDROGUI_ListModel* aModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
235     if( aModel ) {
236       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
237       QModelIndexList aSelectedSourceIndexes;
238       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
239         aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
240       }
241       QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
242       aModel->move( aSelectedIds, ( HYDROGUI_ListModel::OpType )theType, 
243                     !myIsHiddenObjectsShown );      
244     }
245   }
246 }
247
248 /**
249   Returns the list source model.
250   @return the source model
251 */
252 HYDROGUI_ListModel* HYDROGUI_OrderedListWidget::getSourceModel() const
253 {
254   HYDROGUI_ListModel* aSourceModel = 0;
255
256   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
257   if( aFilterModel ) {
258     aSourceModel = dynamic_cast<HYDROGUI_ListModel*>( aFilterModel->sourceModel() );
259   }
260
261   return aSourceModel;
262 }