1 // Copyright (C) 2013 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 #include "CurveCreator_TreeView.h"
21 #include "CurveCreator_ICurve.hxx"
23 #include <SUIT_Session.h>
24 #include <SUIT_ResourceMgr.h>
26 #include <QHeaderView>
27 #include <QtAlgorithms>
31 CurveCreator_TreeViewModel::CurveCreator_TreeViewModel( CurveCreator_ICurve* theCurve, QObject* parent ) :
32 QAbstractItemModel(parent), myCurve(theCurve)
34 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
35 QPixmap aSplineIcon(aResMgr->loadPixmap("GEOM", tr("ICON_CC_SPLINE")));
36 QPixmap aPolylineIcon(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POLYLINE")));
37 QPixmap aClosedSplineIcon(aResMgr->loadPixmap("GEOM", tr("ICON_CC_CLOSED_SPLINE")));
38 QPixmap aClosedPolylineIcon(aResMgr->loadPixmap("GEOM", tr("ICON_CC_CLOSED_POLYLINE")));
39 QPixmap aPointIcon(aResMgr->loadPixmap("GEOM", tr("ICON_CC_POINT")));
41 /* QPixmap aSplineIcon(tr(":images/ICON_SPLINE"));
42 QPixmap aPolylineIcon(tr(":images/ICON_POLYLINE"));
43 QPixmap aClosedPolylineIcon(tr(":images/ICON_CLOSED_POLYLINE"));
44 QPixmap aClosedSplineIcon(tr(":images/ICON_CLOSED_SPLINE"));
45 QPixmap aPointIcon(tr(":images/ICON_POINT")); */
47 if( !aSplineIcon.isNull() )
48 myCachedIcons[ICON_SPLINE] = aSplineIcon;
50 if( !aPolylineIcon.isNull() )
51 myCachedIcons[ICON_POLYLINE] = aPolylineIcon;
53 if( !aPolylineIcon.isNull() )
54 myCachedIcons[ICON_CLOSED_POLYLINE] = aClosedPolylineIcon;
56 if( !aPolylineIcon.isNull() )
57 myCachedIcons[ICON_CLOSED_SPLINE] = aClosedSplineIcon;
59 if( !aPointIcon.isNull() )
60 myCachedIcons[ICON_POINT] = aPointIcon;
62 setHeaderData(1, Qt::Horizontal, QVariant("Name"), Qt::DisplayRole);
63 setHeaderData(2, Qt::Horizontal, QVariant("Nb points"), Qt::DisplayRole);
66 int CurveCreator_TreeViewModel::columnCount(const QModelIndex & parent ) const
68 if( parent.internalId() == ID_SECTION )
74 QVariant CurveCreator_TreeViewModel::data(const QModelIndex & index, int role ) const
76 int aRow = index.row();
77 int aColumn = index.column();
79 if( index.internalId() == ID_SECTION ){
80 if( role == Qt::DisplayRole ){
82 return QString::fromStdString(myCurve->getSectionName(aRow));
83 else if( aColumn == 1 )
84 return QString::number(myCurve->getNbPoints(aRow));
87 else if( role == Qt::DecorationRole ){
89 CurveCreator::SectionType aSectionType = myCurve->getSectionType(aRow);
90 if( aSectionType == CurveCreator::Polyline ){
91 if( myCurve->isClosed(aRow) ){
92 return myCachedIcons[ICON_CLOSED_POLYLINE];
95 return myCachedIcons[ICON_POLYLINE];
99 if( myCurve->isClosed(aRow) ){
100 return myCachedIcons[ICON_CLOSED_SPLINE];
103 return myCachedIcons[ICON_SPLINE];
110 if( role == Qt::DisplayRole ){
114 else if( aColumn == 0 ){
115 CurveCreator::Coordinates aCoords = myCurve->getCoordinates(index.internalId(),index.row() );
117 if( myCurve->getDimension() == CurveCreator::Dim2d ){
118 anOut = QString(tr("X=%1, Y=%2")).arg(aCoords[0]).arg(aCoords[1]);
121 anOut = QString(tr("X=%1, Y=%2, Z=%3")).arg(aCoords[0]).arg(aCoords[1]).arg(aCoords[2]);
126 else if( role == Qt::DecorationRole ){
128 return myCachedIcons[ICON_POINT];
136 QModelIndex CurveCreator_TreeViewModel::index(int row, int column, const QModelIndex & parent ) const
138 if( parent.isValid() ){
139 return createIndex(row, column, parent.row() );
142 QModelIndex aParent = createIndex(row, column, ID_SECTION );
145 return QModelIndex();
148 QModelIndex CurveCreator_TreeViewModel::parent(const QModelIndex & theIndex) const
150 if( !theIndex.isValid() )
151 return QModelIndex();
153 if( theIndex.internalId() == ID_SECTION ){
154 return QModelIndex();
156 return createIndex( theIndex.internalId(), 0, ID_SECTION );
159 int CurveCreator_TreeViewModel::rowCount(const QModelIndex & parent ) const
162 if( myCurve != NULL ){
163 if( !parent.isValid() ){
165 aRowCnt = myCurve->getNbSections();
168 if( parent.internalId() == ID_SECTION ){
170 aRowCnt = myCurve->getNbPoints(parent.row());
177 QModelIndex CurveCreator_TreeViewModel::sectionIndex( int theSection ) const
179 return createIndex( theSection, 0, ID_SECTION );
182 QModelIndex CurveCreator_TreeViewModel::nbPointsIndex( int theSection ) const
184 return createIndex( theSection, 1, ID_SECTION );
187 QModelIndex CurveCreator_TreeViewModel::pointIndex( int theSection, int thePoint ) const
189 return createIndex( thePoint, 0, theSection );
192 bool CurveCreator_TreeViewModel::isSection( const QModelIndex& theIndx ) const
194 if( theIndx.internalId() == ID_SECTION )
199 int CurveCreator_TreeViewModel::getSection( const QModelIndex& theIndx ) const
201 if( theIndx.internalId() == ID_SECTION )
202 return theIndx.row();
203 return theIndx.internalId();
206 int CurveCreator_TreeViewModel::getPoint( const QModelIndex& theIndx ) const
208 if( theIndx.internalId() == ID_SECTION )
210 return theIndx.row();
213 void CurveCreator_TreeViewModel::setCurve( CurveCreator_ICurve* theCurve )
219 /*****************************************************************************************/
220 CurveCreator_TreeView::CurveCreator_TreeView( CurveCreator_ICurve* theCurve, QWidget *parent) :
224 header()->setResizeMode(QHeaderView::ResizeToContents);
225 setUniformRowHeights(true);
226 setContextMenuPolicy( Qt::CustomContextMenu );
227 CurveCreator_TreeViewModel* aModel = new CurveCreator_TreeViewModel(theCurve, this);
229 setSelectionBehavior(SelectRows);
230 setSelectionMode(SingleSelection);
231 setRootIsDecorated(false);
232 setItemsExpandable(false);
233 setAllColumnsShowFocus(true);
234 connect( selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
235 this, SIGNAL(selectionChanged()) );
236 connect( this, SIGNAL(activated(QModelIndex)), this, SLOT(onActivated(QModelIndex)));
239 QList<int> CurveCreator_TreeView::getSelectedSections() const
242 CurveCreator_TreeViewModel* aModel = dynamic_cast<CurveCreator_TreeViewModel*>(model());
245 // QModelIndexList anIndxs = selectionModel()->selectedIndexes();
246 QModelIndexList anIndxs = selectionModel()->selectedRows();
247 for( int i = 0 ; i < anIndxs.size() ; i++ ){
248 if( aModel->isSection(anIndxs[i]) ){
249 aSect << aModel->getSection( anIndxs[i] );
255 void CurveCreator_TreeView::sectionAdded( int theSection )
257 CurveCreator_TreeViewModel* aModel = dynamic_cast<CurveCreator_TreeViewModel*>(model());
259 int nbRows = aModel->rowCount();
260 int aSection = (theSection == -1 ? (nbRows==0 ? 0 : nbRows-1) : theSection);
261 rowsInserted(QModelIndex(), aSection, aSection );
262 QModelIndex aSectIndx = aModel->sectionIndex(aSection);
263 selectionModel()->select(aSectIndx, QItemSelectionModel::Rows | QItemSelectionModel::ClearAndSelect);
267 void CurveCreator_TreeView::sectionChanged( int theSection, int aSectCnt )
269 CurveCreator_TreeViewModel* aModel = dynamic_cast<CurveCreator_TreeViewModel*>(model());
271 QModelIndex aFirstSectIndx = aModel->sectionIndex( theSection );
272 QModelIndex aLastSectIndx = aModel->sectionIndex( theSection + aSectCnt - 1);
273 dataChanged( aFirstSectIndx, aLastSectIndx );
277 void CurveCreator_TreeView::sectionsRemoved( int theSection, int theSectionCnt )
279 CurveCreator_TreeViewModel* aModel = dynamic_cast<CurveCreator_TreeViewModel*>(model());
281 for( int i = 0 ; i < theSectionCnt ; i++ ){
282 QModelIndex aSectIndx = aModel->sectionIndex(theSection + i);
283 this->selectionModel()->select(aSectIndx,QItemSelectionModel::Deselect);
285 rowsRemoved( QModelIndex(), theSection, theSection+theSectionCnt-1 );
289 void CurveCreator_TreeView::setIndexState( const QModelIndex& theIndx, bool& isExpanded, bool& isSelected, bool& isCurrent )
291 setExpanded( theIndx, isExpanded );
292 QItemSelectionModel::SelectionFlags aFlag = QItemSelectionModel::Select;
294 aFlag = QItemSelectionModel::Deselect;
296 selectionModel()->select( theIndx, aFlag );
299 void CurveCreator_TreeView::getIndexInfo( const QModelIndex& theIndx, bool& isExpand, bool& isSelected, bool& isCurrent )
301 isExpand = isExpanded(theIndx);
302 isSelected = selectionModel()->isSelected(theIndx);
303 isCurrent = (theIndx == selectionModel()->currentIndex());
306 void CurveCreator_TreeView::swapIndexes( const QModelIndex& theFirst, const QModelIndex& theSecond )
308 bool isFirstSelected;
309 bool isFirstExpanded;
311 getIndexInfo( theFirst, isFirstExpanded, isFirstSelected, isFirstCurrent );
313 bool isSecondSelected;
314 bool isSecondExpanded;
315 bool isSecondCurrent;
316 getIndexInfo( theSecond, isSecondExpanded, isSecondSelected, isSecondCurrent );
318 setIndexState( theFirst, isSecondExpanded, isSecondSelected, isSecondCurrent );
319 setIndexState( theSecond, isFirstExpanded, isFirstSelected, isFirstCurrent );
320 dataChanged(theFirst,theFirst);
321 dataChanged(theSecond,theSecond);
324 void CurveCreator_TreeView::sectionsSwapped( int theSection, int theOffset )
326 CurveCreator_TreeViewModel* aModel = dynamic_cast<CurveCreator_TreeViewModel*>(model());
328 QModelIndex aFirstIndex = aModel->sectionIndex( theSection );
329 QModelIndex aSecondIndex = aModel->sectionIndex( theSection + theOffset );
330 swapIndexes( aFirstIndex, aSecondIndex );
334 void CurveCreator_TreeView::setSelectedSections( const QList<int>& theList )
336 CurveCreator_TreeViewModel* aModel = dynamic_cast<CurveCreator_TreeViewModel*>(model());
338 selectionModel()->clearSelection();
339 for( int i = 0 ; i < theList.size() ; i++ ){
340 QModelIndex aSectIndx = aModel->sectionIndex(theList[i]);
341 selectionModel()->select(aSectIndx, QItemSelectionModel::Select | QItemSelectionModel::Rows );
346 bool pointLessThan(const QPair<int,int> &s1, const QPair<int,int> &s2)
348 if( s1.first < s2.first )
350 if( s1.first > s2.first )
352 return s1.second < s2.second;
355 CurveCreator_TreeView::SelectionType CurveCreator_TreeView::getSelectionType() const
357 CurveCreator_TreeViewModel* aModel = dynamic_cast<CurveCreator_TreeViewModel*>(model());
360 bool isPointSel = false;
361 bool isSectSel = false;
362 bool isOneSection = true;
364 QModelIndexList aLst = selectionModel()->selectedIndexes();
365 for( int i = 0 ; i < aLst.size() ; i++ ){
366 if( aModel->isSection( aLst[i] ) ){
371 if( aSectNum == -1 ){
372 aSectNum = aModel->getSection(aLst[i]);
375 if( aSectNum != aModel->getSection( aLst[i] ) ){
376 isOneSection = false;
381 if( isSectSel && !isPointSel )
383 if( isPointSel && !isSectSel ){
385 return ST_POINTS_ONE_SECTION;
389 if( isPointSel && isSectSel )
394 void CurveCreator_TreeView::onActivated( QModelIndex theIndx )
396 CurveCreator_TreeViewModel* aModel = dynamic_cast<CurveCreator_TreeViewModel*>(model());
399 int aSect = aModel->getSection(theIndx);
400 if( aModel->isSection(theIndx) ){
401 emit sectionEntered( aSect );
405 void CurveCreator_TreeView::setCurve( CurveCreator_ICurve* theCurve )
407 CurveCreator_TreeViewModel* aModel = dynamic_cast<CurveCreator_TreeViewModel*>(model());
409 aModel->setCurve(theCurve);
413 void CurveCreator_TreeView::reset()
415 QList<int> aSelSections = getSelectedSections();
417 setSelectedSections(aSelSections);