Salome HOME
The first implementation of drag and drop + dialog buttons icons and translations.
[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 HYDROGUI_ZLevelsDlg::HYDROGUI_ZLevelsDlg( QWidget* theParent )
39 : QDialog( theParent )
40 {
41   setWindowTitle( tr( "CHANGE_LAYER_ORDER" ) );
42
43   QVBoxLayout* aMainLayout = new QVBoxLayout( this );
44   aMainLayout->setMargin( 5 );
45
46   QHBoxLayout* aListLayout = new QHBoxLayout();
47
48   myList = new QListView( this );
49   myList->setSelectionMode( QAbstractItemView::ExtendedSelection );
50   myList->setDragEnabled( true );
51   myList->setAcceptDrops( true );
52   myList->viewport()->setAcceptDrops( true );
53   myList->setDropIndicatorShown( true );
54   myList->setDragDropMode( QAbstractItemView::InternalMove );
55
56   HYDROGUI_ZLevelsModel* aModel = new HYDROGUI_ZLevelsModel();
57   QSortFilterProxyModel* aFilteredModel = new QSortFilterProxyModel();
58   aFilteredModel->setSourceModel( aModel );
59   aFilteredModel->setFilterKeyColumn( 0 );
60   aFilteredModel->setFilterRole( HYDROGUI_VisibleRole );
61
62   myList->setModel( aFilteredModel );
63
64   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
65   myTop = new QToolButton;
66   myTop->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_TOP_ICO" ) ) );
67   myTop->setIconSize( QSize( 32, 32 ) );
68   myUp = new QToolButton;
69   myUp->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_UP_ICO" ) ) );
70   myUp->setIconSize( myTop->iconSize() );
71   myDown = new QToolButton;
72   myDown->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_DOWN_ICO" ) ) );
73   myDown->setIconSize( myTop->iconSize() );
74   myBottom = new QToolButton;
75   myBottom->setIcon( aResMgr->loadPixmap( "HYDRO", tr( "ARROW_BOTTOM_ICO" ) ) );
76   myBottom->setIconSize( myTop->iconSize() );
77   QVBoxLayout* aListButtonsLayout = new QVBoxLayout();
78   aListButtonsLayout->addWidget( myTop );
79   aListButtonsLayout->addWidget( myUp );
80   aListButtonsLayout->addWidget( myDown );
81   aListButtonsLayout->addWidget( myBottom );
82   aListButtonsLayout->addStretch();
83   aListLayout->addWidget( myList );
84   aListLayout->addLayout( aListButtonsLayout );
85   aMainLayout->addLayout( aListLayout );
86
87   myAllObjects = new QCheckBox( tr( "ALL_OBJECTS" ) );
88   aMainLayout->addWidget( myAllObjects );
89
90   QHBoxLayout* aDlgButtonsLayout = new QHBoxLayout();
91   myApply = new QPushButton( tr("APPLY") );
92   myClose = new QPushButton( tr("CLOSE") );
93   aDlgButtonsLayout->addWidget( myApply );
94   aDlgButtonsLayout->addWidget( myClose );
95   aDlgButtonsLayout->addStretch();
96   aMainLayout->addLayout( aDlgButtonsLayout );
97
98   QSignalMapper* aSignalMapper = new QSignalMapper( this );
99   aSignalMapper->setMapping( myTop, HYDROGUI_ZLevelsModel::Top );
100   aSignalMapper->setMapping( myUp, HYDROGUI_ZLevelsModel::Up );
101   aSignalMapper->setMapping( myDown, HYDROGUI_ZLevelsModel::Down );
102   aSignalMapper->setMapping( myBottom, HYDROGUI_ZLevelsModel::Bottom );
103   connect( myTop, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
104   connect( myUp, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
105   connect( myDown, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
106   connect( myBottom, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) );
107   connect( aSignalMapper, SIGNAL( mapped( int ) ), this, SLOT( onMove( int ) ) );
108
109   connect( myAllObjects, SIGNAL( stateChanged( int ) ), this, SLOT( OnStateChanged() ) );
110
111   connect( myClose, SIGNAL( clicked() ), this, SLOT( close() ) );
112
113   OnStateChanged();
114 }
115
116 HYDROGUI_ZLevelsDlg::~HYDROGUI_ZLevelsDlg()
117 {
118 }
119
120 void HYDROGUI_ZLevelsDlg::setObjects( const HYDROGUI_ZLevelsModel::Object2VisibleList& theObjects )
121 {
122   HYDROGUI_ZLevelsModel* aModel = getListSourceModel();
123   if( aModel ) {
124     aModel->setObjects( theObjects );
125   }
126 }
127
128 QList<Handle(HYDROData_Entity)> HYDROGUI_ZLevelsDlg::getObjects() const
129 {
130   QList<Handle(HYDROData_Entity)> anObjects;
131
132   HYDROGUI_ZLevelsModel* aModel = getListSourceModel();
133   if( aModel ) {
134     anObjects = aModel->getObjects();
135   }
136
137   return anObjects;
138 }
139
140 void HYDROGUI_ZLevelsDlg::onMove( int theType )
141 {
142   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
143   if( aFilterModel ) {
144     HYDROGUI_ZLevelsModel* aModel = dynamic_cast<HYDROGUI_ZLevelsModel*>( aFilterModel->sourceModel() );
145     if( aModel ) {
146       QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
147       QModelIndexList aSelectedSourceIndexes;
148       foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
149         aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
150       }
151       QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
152       aModel->move( aSelectedIds, ( HYDROGUI_ZLevelsModel::OpType )theType, 
153                     !myAllObjects->isChecked() );      
154     }
155   }
156 }
157
158 void HYDROGUI_ZLevelsDlg::OnStateChanged()
159 {
160   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
161   bool isAll = myAllObjects->isChecked();
162   QString anExpr = isAll ? "true|false" : "true";
163   aFilterModel->setFilterRegExp( anExpr );
164 }
165
166 HYDROGUI_ZLevelsModel* HYDROGUI_ZLevelsDlg::getListSourceModel() const
167 {
168   HYDROGUI_ZLevelsModel* aSourceModel = 0;
169
170   QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
171   if( aFilterModel ) {
172     aSourceModel = dynamic_cast<HYDROGUI_ZLevelsModel*>( aFilterModel->sourceModel() );
173   }
174
175   return aSourceModel;
176 }