1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SMESHGUI_MeshOrderDlg.cxx
21 // Author : Pavel TELKOV, Open CASCADE S.A.S.
24 #include "SMESHGUI_MeshOrderDlg.h"
31 #include <QSpacerItem>
32 #include <QToolButton>
33 #include <QListWidget>
34 #include <QListWidgetItem>
40 * Enumeartion of list widget item types (mesh name or separator)
42 enum MeshOrderItemType { MeshItem = QListWidgetItem::UserType, SeparatorItem };
44 // =========================================================================================
48 // =========================================================================================
50 SMESHGUI_MeshOrderBox::SMESHGUI_MeshOrderBox(QWidget* theParent)
51 : QGroupBox( theParent ), myIsChanged( false ), myUpBtn(0), myDownBtn(0)
53 QHBoxLayout* hBoxLayout = new QHBoxLayout(this);
54 hBoxLayout->setMargin( MARGIN );
55 hBoxLayout->setSpacing( SPACING );
57 myMeshNames = new QListWidget(this);
58 myMeshNames->setSelectionMode(QAbstractItemView::SingleSelection);
59 myMeshNames->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding ));
60 hBoxLayout->addWidget(myMeshNames);
62 QGroupBox* btnGrp = new QGroupBox(this);
63 hBoxLayout->addWidget(btnGrp);
65 myUpBtn = new QToolButton(btnGrp);
66 myDownBtn = new QToolButton(btnGrp);
67 myUpBtn-> setArrowType( Qt::UpArrow );
68 myDownBtn->setArrowType( Qt::DownArrow );
70 QVBoxLayout* vBoxLayout = new QVBoxLayout(btnGrp);
71 vBoxLayout->addSpacerItem( new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding ) );
72 vBoxLayout->addWidget( myUpBtn );
73 vBoxLayout->addWidget( myDownBtn );
74 vBoxLayout->addSpacerItem( new QSpacerItem( 0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding ) );
76 connect( myUpBtn, SIGNAL( clicked() ), this, SLOT( onMoveItem() ) );
77 connect( myDownBtn, SIGNAL( clicked() ), this, SLOT( onMoveItem() ) );
78 connect( myMeshNames, SIGNAL( itemSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
83 // =========================================================================================
87 //=======================================================================
89 SMESHGUI_MeshOrderBox::~SMESHGUI_MeshOrderBox()
93 // =========================================================================================
95 * \brief add separator item
97 // =========================================================================================
99 static void addSeparator( QListWidget* theList )
101 QListWidgetItem* item = new QListWidgetItem( theList, SeparatorItem );
102 QFrame* hline = new QFrame( theList );
103 hline->setFrameStyle( QFrame::HLine | QFrame::Sunken );
104 theList->addItem( item );
105 theList->setItemWidget( item, hline );
108 // =========================================================================================
110 * \brief add sub-mesh item
112 // =========================================================================================
114 static void addMeshItem( QListWidget* theList,
115 const QString& theName,
118 QListWidgetItem* item = new QListWidgetItem( theName, theList, MeshItem );
119 item->setData( Qt::UserRole, theId );
120 theList->addItem( item );
123 // =========================================================================================
125 * \brief Clear submesh names and indeces
127 // =========================================================================================
129 void SMESHGUI_MeshOrderBox::Clear()
131 myMeshNames->clear();
135 // =========================================================================================
137 * \brief Set submesh names and indeces
139 // =========================================================================================
141 void SMESHGUI_MeshOrderBox::SetMeshes(const ListListName& theMeshNames,
142 const ListListId& theMeshIds)
145 ListListName::const_iterator nLIt = theMeshNames.constBegin();
146 ListListId::const_iterator idLIt = theMeshIds.constBegin();
147 for ( ; nLIt != theMeshNames.constEnd(); ++nLIt, ++idLIt )
149 const QStringList& names = (*nLIt);
150 const QList<int>& ids = (*idLIt);
151 if ( myMeshNames->count() )
152 addSeparator( myMeshNames );
153 QStringList::const_iterator nameIt = names.constBegin();
154 QList<int>::const_iterator idIt = ids.constBegin();
155 for ( ; nameIt != names.constEnd(); ++nameIt, ++idIt )
156 addMeshItem( myMeshNames, *nameIt, *idIt );
160 // =========================================================================================
162 * \brief cehck that item exists and not a separator
164 // =========================================================================================
166 static bool checkItem(QListWidgetItem* theItem)
168 return theItem && (int)theItem->type() != (int)SeparatorItem;
171 // =========================================================================================
173 * \brief Returns result (ordered by user) mesh names
175 // =========================================================================================
177 ListListId SMESHGUI_MeshOrderBox::GetMeshIds() const
180 aLLIds.append( QList<int>() );
181 for ( int i = 0, n = myMeshNames->count(); i < n; i++ )
183 QListWidgetItem* it = myMeshNames->item( i );
185 aLLIds.last().append( it->data( Qt::UserRole ).toInt() );
186 else // separator before next list of mesh items
187 aLLIds.append( QList<int>() );
192 // =========================================================================================
194 * \brief Returns result (ordered by user) mesh indeces
196 // =========================================================================================
198 ListListName SMESHGUI_MeshOrderBox::GetMeshNames() const
200 ListListName aLLNames;
201 aLLNames.append( QStringList() );
202 for ( int i = 0, n = myMeshNames->count(); i < n; i++ )
204 QListWidgetItem* it = myMeshNames->item( i );
206 aLLNames.last().append( it->text() );
207 else // separator before next list of mesh items
208 aLLNames.append( QStringList() );
213 // =========================================================================================
215 * \brief update state of arrow buttons according to selection
217 // =========================================================================================
219 void SMESHGUI_MeshOrderBox::onSelectionChanged()
223 QList<QListWidgetItem *> items = myMeshNames->selectedItems();
224 if ( !items.isEmpty() )
226 QListWidgetItem* selItem = (*(items.begin()));
227 if (checkItem(selItem))
229 const int rowId = myMeshNames->row( selItem );
230 isUp = checkItem( myMeshNames->item( rowId - 1 ) );
231 isDown = checkItem( myMeshNames->item( rowId + 1 ) );
234 myUpBtn-> setEnabled( isUp );
235 myDownBtn->setEnabled( isDown );
238 // =========================================================================================
240 * \brief move item according to clicked arrow button
242 // =========================================================================================
244 void SMESHGUI_MeshOrderBox::onMoveItem()
246 moveItem( sender() == myUpBtn );
249 // =========================================================================================
251 * \brief move mesh in order up or down
253 // =========================================================================================
255 void SMESHGUI_MeshOrderBox::moveItem(const bool theIsUp)
257 // move selected list item up or down
258 QList<QListWidgetItem *> items = myMeshNames->selectedItems();
259 if ( items.isEmpty() )
261 QListWidgetItem * selItem = (*(items.begin()));
262 if (!checkItem(selItem))
264 int rowId = myMeshNames->row( selItem );
268 // move item in list widget
270 myMeshNames->takeItem( rowId );
271 myMeshNames->insertItem(theIsUp ? rowId-1 : rowId+1, selItem );
273 // restore selection and current status
274 selItem->setSelected( true );
275 myMeshNames->setCurrentItem( selItem );
278 // =========================================================================================
280 * \brief returns status is order changed by user
282 // =========================================================================================
284 bool SMESHGUI_MeshOrderBox:: IsOrderChanged() const
289 // =========================================================================================
293 // =========================================================================================
295 SMESHGUI_MeshOrderDlg::SMESHGUI_MeshOrderDlg(QWidget* theParent)
296 : SMESHGUI_Dialog( theParent, false, false, OK | Cancel | Help )
298 setWindowTitle( tr( "SMESH_MESHORDER_TITLE") );
299 QFrame* main = mainFrame();
301 QVBoxLayout* aDlgLay = new QVBoxLayout (main);
302 aDlgLay->setMargin( 0 );
303 aDlgLay->setSpacing( SPACING );
305 myBox = new SMESHGUI_MeshOrderBox( main );
307 aDlgLay->addWidget(myBox);
308 aDlgLay->setStretchFactor(main, 1);
311 // =========================================================================================
315 // =========================================================================================
317 SMESHGUI_MeshOrderDlg::~SMESHGUI_MeshOrderDlg()
321 // =========================================================================================
323 * \brief return Box widget to show mesh order
325 // =========================================================================================
327 SMESHGUI_MeshOrderBox* SMESHGUI_MeshOrderDlg::GetMeshOrderBox() const