From d96919480cbf05fe5ea808c0b97052b6f212f00f Mon Sep 17 00:00:00 2001 From: mzn Date: Wed, 19 Mar 2014 12:27:04 +0000 Subject: [PATCH] First implementation of Up/Down buttons. --- src/ZLEVEL/HYDROGUI_ZLevelsDlg.cxx | 32 ++++++++++++ src/ZLEVEL/HYDROGUI_ZLevelsDlg.h | 1 + src/ZLEVEL/HYDROGUI_ZLevelsModel.cxx | 73 +++++++++++++++++++++++++++- src/ZLEVEL/HYDROGUI_ZLevelsModel.h | 15 +++++- 4 files changed, 118 insertions(+), 3 deletions(-) diff --git a/src/ZLEVEL/HYDROGUI_ZLevelsDlg.cxx b/src/ZLEVEL/HYDROGUI_ZLevelsDlg.cxx index 267a1869..7448ad36 100644 --- a/src/ZLEVEL/HYDROGUI_ZLevelsDlg.cxx +++ b/src/ZLEVEL/HYDROGUI_ZLevelsDlg.cxx @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -39,6 +40,7 @@ HYDROGUI_ZLevelsDlg::HYDROGUI_ZLevelsDlg( QWidget* theParent ) QHBoxLayout* aListLayout = new QHBoxLayout(); myList = new QListView( this ); + myList->setSelectionMode( QAbstractItemView::ExtendedSelection ); HYDROGUI_ZLevelsModel* aModel = new HYDROGUI_ZLevelsModel(); QSortFilterProxyModel* aFilteredModel = new QSortFilterProxyModel(); @@ -73,8 +75,21 @@ HYDROGUI_ZLevelsDlg::HYDROGUI_ZLevelsDlg( QWidget* theParent ) aDlgButtonsLayout->addStretch(); aMainLayout->addLayout( aDlgButtonsLayout ); + QSignalMapper* aSignalMapper = new QSignalMapper( this ); + aSignalMapper->setMapping( myTop, HYDROGUI_ZLevelsModel::Top ); + aSignalMapper->setMapping( myUp, HYDROGUI_ZLevelsModel::Up ); + aSignalMapper->setMapping( myDown, HYDROGUI_ZLevelsModel::Down ); + aSignalMapper->setMapping( myBottom, HYDROGUI_ZLevelsModel::Bottom ); + connect( myTop, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) ); + connect( myUp, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) ); + connect( myDown, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) ); + connect( myBottom, SIGNAL( clicked() ), aSignalMapper, SLOT( map() ) ); + connect( aSignalMapper, SIGNAL( mapped( int ) ), this, SLOT( onMove( int ) ) ); + connect( myAllObjects, SIGNAL( stateChanged( int ) ), this, SLOT( OnStateChanged() ) ); + connect( myClose, SIGNAL( clicked() ), this, SLOT( close() ) ); + OnStateChanged(); } @@ -93,6 +108,23 @@ void HYDROGUI_ZLevelsDlg::setObjects( const QList& theObjects ) } } +void HYDROGUI_ZLevelsDlg::onMove( int theType ) +{ + QSortFilterProxyModel* aFilterModel = dynamic_cast( myList->model() ); + if( aFilterModel ) { + HYDROGUI_ZLevelsModel* aModel = dynamic_cast( aFilterModel->sourceModel() ); + if( aModel ) { + QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes(); + QModelIndexList aSelectedSourceIndexes; + foreach ( const QModelIndex& anIndex, aSelectedIndexes ) { + aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex ); + } + QList aSelectedIds = aModel->getIds( aSelectedSourceIndexes ); + aModel->move( aSelectedIds, theType ); + } + } +} + void HYDROGUI_ZLevelsDlg::OnStateChanged() { QSortFilterProxyModel* aFilterModel = dynamic_cast( myList->model() ); diff --git a/src/ZLEVEL/HYDROGUI_ZLevelsDlg.h b/src/ZLEVEL/HYDROGUI_ZLevelsDlg.h index 44e3343d..2ca17ef2 100644 --- a/src/ZLEVEL/HYDROGUI_ZLevelsDlg.h +++ b/src/ZLEVEL/HYDROGUI_ZLevelsDlg.h @@ -44,6 +44,7 @@ public: void setObjects( const QList& theObjects ); private slots: + void onMove( int theType ); void OnStateChanged(); private: diff --git a/src/ZLEVEL/HYDROGUI_ZLevelsModel.cxx b/src/ZLEVEL/HYDROGUI_ZLevelsModel.cxx index 9702bf77..fe49aac2 100644 --- a/src/ZLEVEL/HYDROGUI_ZLevelsModel.cxx +++ b/src/ZLEVEL/HYDROGUI_ZLevelsModel.cxx @@ -82,12 +82,17 @@ int HYDROGUI_ZLevelsModel::rowCount( const QModelIndex &theParent ) const void HYDROGUI_ZLevelsModel::setObjects( const QList& theObjects ) { myObjects = theObjects; + reset(); } bool HYDROGUI_ZLevelsModel::IsObjectVisible( int theIndex ) const { - return theIndex%2==0;//TODO: implement real visibility state + //TODO: reimplement + return myObjects[ theIndex ] == "A" || + myObjects[ theIndex ] == "C" || + myObjects[ theIndex ] == "E" || + myObjects[ theIndex ] == "G"; } QVariant HYDROGUI_ZLevelsModel::headerData( int theSection, @@ -106,3 +111,69 @@ QVariant HYDROGUI_ZLevelsModel::headerData( int theSection, } return QVariant(); } + +QList HYDROGUI_ZLevelsModel::getIds( const QModelIndexList& theIndexes, bool theIsToSort ) const +{ + QList anIds; + foreach( const QModelIndex& anIndex, theIndexes ) { + anIds << anIndex.row(); + } + + if ( theIsToSort ) { + qSort( anIds ); + } + + return anIds; +} + +bool HYDROGUI_ZLevelsModel::move( const int theItem, const int theType, + const int theDropItem ) +{ + bool aRes = false; + + switch ( theType ) { + case Up: + if ( theItem > 0 ) { + myObjects.swap( theItem, theItem - 1 ); + aRes = true; + } + break; + case Down: + if ( theItem < myObjects.count() - 1 ) { + myObjects.swap( theItem, theItem + 1 ); + aRes = true; + } + break; + } + + return aRes; +} + +bool HYDROGUI_ZLevelsModel::move( const QList& theItems, const int theType, + const int theDropItem ) +{ + bool aRes = true; + + bool isReverse = theType == Top || theType == Down; + QListIterator anIt( theItems ); + if ( isReverse ) { + anIt.toBack(); + while ( anIt.hasPrevious() ) { + if ( !move( anIt.previous(), theType, theDropItem ) ) { + aRes = false; + break; + } + } + } else { + while ( anIt.hasNext() ) { + if ( !move( anIt.next(), theType, theDropItem ) ) { + aRes = false; + break; + } + } + } + + reset(); //TODO dataChanged? + + return aRes; +} \ No newline at end of file diff --git a/src/ZLEVEL/HYDROGUI_ZLevelsModel.h b/src/ZLEVEL/HYDROGUI_ZLevelsModel.h index bdacefc7..5dbd2a9f 100644 --- a/src/ZLEVEL/HYDROGUI_ZLevelsModel.h +++ b/src/ZLEVEL/HYDROGUI_ZLevelsModel.h @@ -36,19 +36,30 @@ class HYDROGUI_ZLevelsModel : public QAbstractListModel { Q_OBJECT +public: + enum OpType { Top, Up, Down, Bottom, DragAndDrop }; + public: HYDROGUI_ZLevelsModel( QObject* theParent = 0 ); virtual ~HYDROGUI_ZLevelsModel(); virtual QVariant data( const QModelIndex &theIndex, int theRole = Qt::DisplayRole ) const; - virtual int rowCount( const QModelIndex& theParent = QModelIndex() ) const; + virtual int rowCount( const QModelIndex &theParent = QModelIndex() ) const; + virtual QVariant headerData( int theSection, Qt::Orientation theOrientation, int theRole = Qt::DisplayRole ) const; + QList getIds( const QModelIndexList& theIndexes, bool theIsToSort = true ) const; + void setObjects( const QList& theObjects ); + bool move( const int theItem, const int theType, + const int theDropItem = -1 ); + bool move( const QList& theItems, const int theType, + const int theDropItem = -1 ); + protected: bool IsObjectVisible( int theIndex ) const; @@ -57,4 +68,4 @@ private: QPixmap myEmpty, myEye; }; -#endif +#endif \ No newline at end of file -- 2.39.2