Salome HOME
ed33f6d09e67c8c720c2206484fd5b0b5d7a6c38
[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 <SUIT_Session.h>
27 #include <SUIT_ResourceMgr.h>
28
29 #include <QCheckBox>
30 #include <QLayout>
31 #include <QListView>
32 #include <QPushButton>
33 #include <QToolButton>
34 #include <QSignalMapper>
35 #include <QSortFilterProxyModel>
36
37
38 /**
39   Constructor.
40   @param theParent the parent widget
41 */
42 HYDROGUI_ZLevelsDlg::HYDROGUI_ZLevelsDlg( QWidget* theParent )
43 : QDialog( theParent )
44 {
45   // Dialog title
46   setWindowTitle( tr( "CHANGE_LAYER_ORDER" ) );
47
48   // Main layout
49   QVBoxLayout* aMainLayout = new QVBoxLayout( this );
50   aMainLayout->setMargin( 5 );
51   aMainLayout->setSpacing( 5 );
52
53   // List and buttons top, up, down, bottom
54   QHBoxLayout* aListLayout = new QHBoxLayout();
55
56   // list
57   myList = new QListView( this );
58   myList->setSelectionMode( QAbstractItemView::ExtendedSelection );
59   myList->setDragEnabled( true );
60   myList->setAcceptDrops( true );
61   myList->viewport()->setAcceptDrops( true );
62   myList->setDropIndicatorShown( true );
63   myList->setDragDropMode( QAbstractItemView::InternalMove );
64
65   HYDROGUI_ZLevelsModel* aModel = new HYDROGUI_ZLevelsModel();
66   QSortFilterProxyModel* aFilteredModel = new QSortFilterProxyModel();
67   aFilteredModel->setSourceModel( aModel );
68   aFilteredModel->setFilterKeyColumn( 0 );
69   aFilteredModel->setFilterRole( HYDROGUI_VisibleRole );
70
71   myList->setModel( aFilteredModel );
72
73   // buttons top, up, down, bottom
74   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
75   myTop = new QToolButton;
76   myTop->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_TOP_ICO" ) ) );
77   myTop->setIconSize( QSize( 32, 32 ) );
78   myUp = new QToolButton;
79   myUp->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_UP_ICO" ) ) );
80   myUp->setIconSize( myTop->iconSize() );
81   myDown = new QToolButton;
82   myDown->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_DOWN_ICO" ) ) );
83   myDown->setIconSize( myTop->iconSize() );
84   myBottom = new QToolButton;
85   myBottom->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_BOTTOM_ICO" ) ) );
86   myBottom->setIconSize( myTop->iconSize() );
87
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   aListLayout->addWidget( myList );
95
96   aListLayout->addLayout( aListButtonsLayout );
97   aMainLayout->addLayout( aListLayout );
98
99   // "All objects" check box
100   myAllObjects = new QCheckBox( tr( "ALL_OBJECTS" ) );
101   aMainLayout->addWidget( myAllObjects );
102
103   // Apply and close buttons
104   QHBoxLayout* aDlgButtonsLayout = new QHBoxLayout();
105   myApply = new QPushButton( tr("APPLY") );
106   myClose = new QPushButton( tr("CLOSE") );
107   aDlgButtonsLayout->addWidget( myApply );
108   aDlgButtonsLayout->addWidget( myClose );
109   aDlgButtonsLayout->addStretch();
110
111   aMainLayout->addLayout( aDlgButtonsLayout );
112
113   // Connections
114   QSignalMapper* aSignalMapper = new QSignalMapper( this );
115   aSignalMapper->setMapping( myTop, HYDROGUI_ZLevelsModel::Top );
116   aSignalMapper->setMapping( myUp, HYDROGUI_ZLevelsModel::Up );
117   aSignalMapper->setMapping( myDown, HYDROGUI_ZLevelsModel::Down );
118   aSignalMapper->setMapping( myBottom, HYDROGUI_ZLevelsModel::Bottom );
119   connect( myTop, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
120   connect( myUp, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
121   connect( myDown, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
122   connect( myBottom, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
123   connect( aSignalMapper, SIGNAL( mapped( int ) ), this, SLOT( onMove( int ) ) );
124
125   connect( myAllObjects, SIGNAL( stateChanged( int ) ), this, SLOT( onStateChanged() ) );
126
127   connect( myApply, SIGNAL( clicked() ), this, SIGNAL( applyOrder() ) );
128   connect( myClose, SIGNAL( clicked() ), this, SLOT( close() ) );
129
130   // Initialize
131   onStateChanged();
132 }
133
134 /**
135   Destructor.
136 */
137 HYDROGUI_ZLevelsDlg::~HYDROGUI_ZLevelsDlg()
138 {
139 }
140
141 /**
142   Set the list of objects (which supposed to be ordered by the dialog).
143   @param theObjects the list of objects
144 */
145 void HYDROGUI_ZLevelsDlg::setObjects( const HYDROGUI_ZLevelsModel::Object2VisibleList& theObjects )
146 {
147   HYDROGUI_ZLevelsModel* aModel = getListSourceModel();
148   if( aModel ) {
149     aModel->setObjects( theObjects );
150   }
151 }
152
153 /**
154   Returns the ordered list of objects.
155   @return the list of objects
156 */
157 QList<Handle(HYDROData_Entity)> HYDROGUI_ZLevelsDlg::getObjects() const
158 {
159   QList<Handle(HYDROData_Entity)> anObjects;
160
161   HYDROGUI_ZLevelsModel* aModel = getListSourceModel();
162   if( aModel ) {
163     anObjects = aModel->getObjects();
164   }
165
166   return anObjects;
167 }
168
169 /**
170   Slot called on top, up, down and bottom button click.
171   @param theType the move operation type
172 */
173 void HYDROGUI_ZLevelsDlg::onMove( int theType )
174 {
175   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
176   if( aFilterModel ) {
177     HYDROGUI_ZLevelsModel* aModel = dynamic_cast<HYDROGUI_ZLevelsModel*>( aFilterModel->sourceModel() );
178     if( aModel ) {
179       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
180       QModelIndexList aSelectedSourceIndexes;
181       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
182         aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
183       }
184       QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
185       aModel->move( aSelectedIds, ( HYDROGUI_ZLevelsModel::OpType )theType, 
186                     !myAllObjects->isChecked() );      
187     }
188   }
189 }
190
191 /**
192   Slot called on "All objects" check box state change.
193 */
194 void HYDROGUI_ZLevelsDlg::onStateChanged()
195 {
196   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
197   bool isAll = myAllObjects->isChecked();
198   QString anExpr = isAll ? "true|false" : "true";
199   aFilterModel->setFilterRegExp( anExpr );
200 }
201
202 /**
203   Returns the list source model.
204   @return the source model
205 */
206 HYDROGUI_ZLevelsModel* HYDROGUI_ZLevelsDlg::getListSourceModel() const
207 {
208   HYDROGUI_ZLevelsModel* aSourceModel = 0;
209
210   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
211   if( aFilterModel ) {
212     aSourceModel = dynamic_cast<HYDROGUI_ZLevelsModel*>( aFilterModel->sourceModel() );
213   }
214
215   return aSourceModel;
216 }