1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESHGUI_MeshOrderDlg.cxx
23 // Author : Pavel TELKOV, Open CASCADE S.A.S.
26 #include "SMESHGUI_MeshOrderDlg.h"
33 #include <QSpacerItem>
34 #include <QToolButton>
35 #include <QListWidget>
36 #include <QListWidgetItem>
42 * Enumeartion of list widget item types (mesh name or separator)
44 enum MeshOrderItemType { MeshItem = QListWidgetItem::UserType, SeparatorItem };
46 // =========================================================================================
50 // =========================================================================================
52 SMESHGUI_MeshOrderBox::SMESHGUI_MeshOrderBox(QWidget* theParent)
53 : QGroupBox( theParent ), myIsChanged( false ), myUpBtn(0), myDownBtn(0)
55 QHBoxLayout* hBoxLayout = new QHBoxLayout(this);
56 hBoxLayout->setMargin( MARGIN );
57 hBoxLayout->setSpacing( SPACING );
59 myMeshNames = new QListWidget(this);
60 myMeshNames->setSelectionMode(QAbstractItemView::SingleSelection);
61 myMeshNames->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding ));
62 hBoxLayout->addWidget(myMeshNames);
64 QGroupBox* btnGrp = new QGroupBox(this);
65 hBoxLayout->addWidget(btnGrp);
67 myUpBtn = new QToolButton(btnGrp);
68 myDownBtn = new QToolButton(btnGrp);
69 myUpBtn-> setArrowType( Qt::UpArrow );
70 myDownBtn->setArrowType( Qt::DownArrow );
72 QVBoxLayout* vBoxLayout = new QVBoxLayout(btnGrp);
73 vBoxLayout->addSpacerItem( new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding ) );
74 vBoxLayout->addWidget( myUpBtn );
75 vBoxLayout->addWidget( myDownBtn );
76 vBoxLayout->addSpacerItem( new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding ) );
78 connect( myUpBtn, SIGNAL( clicked() ), this, SLOT( onMoveItem() ) );
79 connect( myDownBtn, SIGNAL( clicked() ), this, SLOT( onMoveItem() ) );
80 connect( myMeshNames, SIGNAL( itemSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
85 // =========================================================================================
89 //=======================================================================
91 SMESHGUI_MeshOrderBox::~SMESHGUI_MeshOrderBox()
95 // =========================================================================================
97 * \brief add separator item
99 // =========================================================================================
101 static void addSeparator( QListWidget* theList )
103 QListWidgetItem* item = new QListWidgetItem( theList, SeparatorItem );
104 QFrame* hline = new QFrame( theList );
105 hline->setFrameStyle( QFrame::HLine | QFrame::Sunken );
106 theList->addItem( item );
107 theList->setItemWidget( item, hline );
110 // =========================================================================================
112 * \brief add sub-mesh item
114 // =========================================================================================
116 static void addMeshItem( QListWidget* theList,
117 const QString& theName,
120 QListWidgetItem* item = new QListWidgetItem( theName, theList, MeshItem );
121 item->setData( Qt::UserRole, theId );
122 theList->addItem( item );
125 // =========================================================================================
127 * \brief Clear submesh names and indeces
129 // =========================================================================================
131 void SMESHGUI_MeshOrderBox::Clear()
133 myMeshNames->clear();
137 // =========================================================================================
139 * \brief Set submesh names and indeces
141 // =========================================================================================
143 void SMESHGUI_MeshOrderBox::SetMeshes(const ListListName& theMeshNames,
144 const ListListId& theMeshIds)
147 ListListName::const_iterator nLIt = theMeshNames.constBegin();
148 ListListId::const_iterator idLIt = theMeshIds.constBegin();
149 for ( ; nLIt != theMeshNames.constEnd(); ++nLIt, ++idLIt )
151 const QStringList& names = (*nLIt);
152 const QList<int>& ids = (*idLIt);
153 if ( myMeshNames->count() )
154 addSeparator( myMeshNames );
155 QStringList::const_iterator nameIt = names.constBegin();
156 QList<int>::const_iterator idIt = ids.constBegin();
157 for ( ; nameIt != names.constEnd(); ++nameIt, ++idIt )
158 addMeshItem( myMeshNames, *nameIt, *idIt );
162 // =========================================================================================
164 * \brief cehck that item exists and not a separator
166 // =========================================================================================
168 static bool checkItem(QListWidgetItem* theItem)
170 return theItem && (int)theItem->type() != (int)SeparatorItem;
173 // =========================================================================================
175 * \brief Returns result (ordered by user) mesh names
177 // =========================================================================================
179 ListListId SMESHGUI_MeshOrderBox::GetMeshIds() const
182 aLLIds.append( QList<int>() );
183 for ( int i = 0, n = myMeshNames->count(); i < n; i++ )
185 QListWidgetItem* it = myMeshNames->item( i );
187 aLLIds.last().append( it->data( Qt::UserRole ).toInt() );
188 else // separator before next list of mesh items
189 aLLIds.append( QList<int>() );
194 // =========================================================================================
196 * \brief Returns result (ordered by user) mesh indeces
198 // =========================================================================================
200 ListListName SMESHGUI_MeshOrderBox::GetMeshNames() const
202 ListListName aLLNames;
203 aLLNames.append( QStringList() );
204 for ( int i = 0, n = myMeshNames->count(); i < n; i++ )
206 QListWidgetItem* it = myMeshNames->item( i );
208 aLLNames.last().append( it->text() );
209 else // separator before next list of mesh items
210 aLLNames.append( QStringList() );
215 // =========================================================================================
217 * \brief update state of arrow buttons according to selection
219 // =========================================================================================
221 void SMESHGUI_MeshOrderBox::onSelectionChanged()
225 QList<QListWidgetItem *> items = myMeshNames->selectedItems();
226 if ( !items.isEmpty() )
228 QListWidgetItem* selItem = (*(items.begin()));
229 if (checkItem(selItem))
231 const int rowId = myMeshNames->row( selItem );
232 isUp = checkItem( myMeshNames->item( rowId - 1 ) );
233 isDown = checkItem( myMeshNames->item( rowId + 1 ) );
236 myUpBtn-> setEnabled( isUp );
237 myDownBtn->setEnabled( isDown );
240 // =========================================================================================
242 * \brief move item according to clicked arrow button
244 // =========================================================================================
246 void SMESHGUI_MeshOrderBox::onMoveItem()
248 moveItem( sender() == myUpBtn );
251 // =========================================================================================
253 * \brief move mesh in order up or down
255 // =========================================================================================
257 void SMESHGUI_MeshOrderBox::moveItem(const bool theIsUp)
259 // move selected list item up or down
260 QList<QListWidgetItem *> items = myMeshNames->selectedItems();
261 if ( items.isEmpty() )
263 QListWidgetItem * selItem = (*(items.begin()));
264 if (!checkItem(selItem))
266 int rowId = myMeshNames->row( selItem );
270 // move item in list widget
272 myMeshNames->takeItem( rowId );
273 myMeshNames->insertItem(theIsUp ? rowId-1 : rowId+1, selItem );
275 // restore selection and current status
276 selItem->setSelected( true );
277 myMeshNames->setCurrentItem( selItem );
280 // =========================================================================================
282 * \brief returns status is order changed by user
284 // =========================================================================================
286 bool SMESHGUI_MeshOrderBox:: IsOrderChanged() const
291 // =========================================================================================
295 // =========================================================================================
297 SMESHGUI_MeshOrderDlg::SMESHGUI_MeshOrderDlg(QWidget* theParent)
298 : SMESHGUI_Dialog( theParent, false, false, OK | Cancel | Help )
300 setWindowTitle( tr( "SMESH_MESHORDER_TITLE") );
301 QFrame* main = mainFrame();
303 QVBoxLayout* aDlgLay = new QVBoxLayout (main);
304 aDlgLay->setMargin( 0 );
305 aDlgLay->setSpacing( SPACING );
307 myBox = new SMESHGUI_MeshOrderBox( main );
309 aDlgLay->addWidget(myBox);
310 aDlgLay->setStretchFactor(main, 1);
313 // =========================================================================================
317 // =========================================================================================
319 SMESHGUI_MeshOrderDlg::~SMESHGUI_MeshOrderDlg()
323 // =========================================================================================
325 * \brief return Box widget to show mesh order
327 // =========================================================================================
329 SMESHGUI_MeshOrderBox* SMESHGUI_MeshOrderDlg::GetMeshOrderBox() const