Salome HOME
7055cd082f588afc506015a31ba3c6af71db0ac0
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ZLevelsDlg.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_ZLevelsDlg.h"
24 #include "HYDROGUI_ZLevelsModel.h"
25
26 #include <QCheckBox>
27 #include <QLayout>
28 #include <QListView>
29 #include <QPushButton>
30 #include <QSignalMapper>
31 #include <QSortFilterProxyModel>
32
33
34 HYDROGUI_ZLevelsDlg::HYDROGUI_ZLevelsDlg( QWidget* theParent )
35 : QDialog( theParent )
36 {
37   QVBoxLayout* aMainLayout = new QVBoxLayout( this );
38   aMainLayout->setMargin( 5 );
39
40   QHBoxLayout* aListLayout = new QHBoxLayout();
41
42   myList = new QListView( this );
43   myList->setSelectionMode( QAbstractItemView::ExtendedSelection );
44   myList->setDragEnabled( true );
45   myList->setAcceptDrops( true );
46   myList->viewport()->setAcceptDrops( true );
47   myList->setDropIndicatorShown( true );
48   myList->setDragDropMode( QAbstractItemView::InternalMove );
49
50   HYDROGUI_ZLevelsModel* aModel = new HYDROGUI_ZLevelsModel();
51   QSortFilterProxyModel* aFilteredModel = new QSortFilterProxyModel();
52   aFilteredModel->setSourceModel( aModel );
53   aFilteredModel->setFilterKeyColumn( 0 );
54   aFilteredModel->setFilterRole( HYDROGUI_VisibleRole );
55
56   myList->setModel( aFilteredModel );
57
58   myTop = new QPushButton( tr("TOP") );
59   myUp = new QPushButton( tr("UP") );
60   myDown = new QPushButton( tr("DOWN") );
61   myBottom = new QPushButton( tr("BOTTOM") );
62   QVBoxLayout* aListButtonsLayout = new QVBoxLayout();
63   aListButtonsLayout->addWidget( myTop );
64   aListButtonsLayout->addWidget( myUp );
65   aListButtonsLayout->addWidget( myDown );
66   aListButtonsLayout->addWidget( myBottom );
67   aListButtonsLayout->addStretch();
68   aListLayout->addWidget( myList );
69   aListLayout->addLayout( aListButtonsLayout );
70   aMainLayout->addLayout( aListLayout );
71
72   myAllObjects = new QCheckBox( tr( "ALL_OBJECTS" ) );
73   aMainLayout->addWidget( myAllObjects );
74
75   QHBoxLayout* aDlgButtonsLayout = new QHBoxLayout();
76   myApply = new QPushButton( tr("APPLY") );
77   myClose = new QPushButton( tr("CLOSE") );
78   aDlgButtonsLayout->addWidget( myApply );
79   aDlgButtonsLayout->addWidget( myClose );
80   aDlgButtonsLayout->addStretch();
81   aMainLayout->addLayout( aDlgButtonsLayout );
82
83   QSignalMapper* aSignalMapper = new QSignalMapper( this );
84   aSignalMapper->setMapping( myTop, HYDROGUI_ZLevelsModel::Top );
85   aSignalMapper->setMapping( myUp, HYDROGUI_ZLevelsModel::Up );
86   aSignalMapper->setMapping( myDown, HYDROGUI_ZLevelsModel::Down );
87   aSignalMapper->setMapping( myBottom, HYDROGUI_ZLevelsModel::Bottom );
88   connect( myTop, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
89   connect( myUp, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
90   connect( myDown, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
91   connect( myBottom, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
92   connect( aSignalMapper, SIGNAL( mapped( int ) ), this, SLOT( onMove( int ) ) );
93
94   connect( myAllObjects, SIGNAL( stateChanged( int ) ), this, SLOT( OnStateChanged() ) );
95
96   connect( myClose, SIGNAL( clicked() ), this, SLOT( close() ) );
97
98   OnStateChanged();
99 }
100
101 HYDROGUI_ZLevelsDlg::~HYDROGUI_ZLevelsDlg()
102 {
103 }
104
105 void HYDROGUI_ZLevelsDlg::setObjects( const HYDROGUI_ZLevelsModel::Object2VisibleList& theObjects )
106 {
107   HYDROGUI_ZLevelsModel* aModel = getListSourceModel();
108   if( aModel ) {
109     aModel->setObjects( theObjects );
110   }
111 }
112
113 QList<Handle(HYDROData_Entity)> HYDROGUI_ZLevelsDlg::getObjects() const
114 {
115   QList<Handle(HYDROData_Entity)> anObjects;
116
117   HYDROGUI_ZLevelsModel* aModel = getListSourceModel();
118   if( aModel ) {
119     anObjects = aModel->getObjects();
120   }
121
122   return anObjects;
123 }
124
125 void HYDROGUI_ZLevelsDlg::onMove( int theType )
126 {
127   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
128   if( aFilterModel ) {
129     HYDROGUI_ZLevelsModel* aModel = dynamic_cast<HYDROGUI_ZLevelsModel*>( aFilterModel->sourceModel() );
130     if( aModel ) {
131       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
132       QModelIndexList aSelectedSourceIndexes;
133       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
134         aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
135       }
136       QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
137       aModel->move( aSelectedIds, ( HYDROGUI_ZLevelsModel::OpType )theType, 
138                     !myAllObjects->isChecked() );      
139     }
140   }
141 }
142
143 void HYDROGUI_ZLevelsDlg::OnStateChanged()
144 {
145   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
146   bool isAll = myAllObjects->isChecked();
147   QString anExpr = isAll ? "true|false" : "true";
148   aFilterModel->setFilterRegExp( anExpr );
149 }
150
151 HYDROGUI_ZLevelsModel* HYDROGUI_ZLevelsDlg::getListSourceModel() const
152 {
153   HYDROGUI_ZLevelsModel* aSourceModel = 0;
154
155   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
156   if( aFilterModel ) {
157     aSourceModel = dynamic_cast<HYDROGUI_ZLevelsModel*>( aFilterModel->sourceModel() );
158   }
159
160   return aSourceModel;
161 }