#include <QLayout>
#include <QListView>
#include <QPushButton>
+#include <QSignalMapper>
#include <QSortFilterProxyModel>
QHBoxLayout* aListLayout = new QHBoxLayout();
myList = new QListView( this );
+ myList->setSelectionMode( QAbstractItemView::ExtendedSelection );
HYDROGUI_ZLevelsModel* aModel = new HYDROGUI_ZLevelsModel();
QSortFilterProxyModel* aFilteredModel = new QSortFilterProxyModel();
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();
}
}
}
+void HYDROGUI_ZLevelsDlg::onMove( int theType )
+{
+ QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
+ if( aFilterModel ) {
+ HYDROGUI_ZLevelsModel* aModel = dynamic_cast<HYDROGUI_ZLevelsModel*>( aFilterModel->sourceModel() );
+ if( aModel ) {
+ QModelIndexList aSelectedIndexes = myList->selectionModel()->selectedIndexes();
+ QModelIndexList aSelectedSourceIndexes;
+ foreach ( const QModelIndex& anIndex, aSelectedIndexes ) {
+ aSelectedSourceIndexes << aFilterModel->mapToSource( anIndex );
+ }
+ QList<int> aSelectedIds = aModel->getIds( aSelectedSourceIndexes );
+ aModel->move( aSelectedIds, theType );
+ }
+ }
+}
+
void HYDROGUI_ZLevelsDlg::OnStateChanged()
{
QSortFilterProxyModel* aFilterModel = dynamic_cast<QSortFilterProxyModel*>( myList->model() );
void setObjects( const QList<QString>& theObjects );
private slots:
+ void onMove( int theType );
void OnStateChanged();
private:
void HYDROGUI_ZLevelsModel::setObjects( const QList<QString>& 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,
}
return QVariant();
}
+
+QList<int> HYDROGUI_ZLevelsModel::getIds( const QModelIndexList& theIndexes, bool theIsToSort ) const
+{
+ QList<int> 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<int>& theItems, const int theType,
+ const int theDropItem )
+{
+ bool aRes = true;
+
+ bool isReverse = theType == Top || theType == Down;
+ QListIterator<int> 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
{
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<int> getIds( const QModelIndexList& theIndexes, bool theIsToSort = true ) const;
+
void setObjects( const QList<QString>& theObjects );
+ bool move( const int theItem, const int theType,
+ const int theDropItem = -1 );
+ bool move( const QList<int>& theItems, const int theType,
+ const int theDropItem = -1 );
+
protected:
bool IsObjectVisible( int theIndex ) const;
QPixmap myEmpty, myEye;
};
-#endif
+#endif
\ No newline at end of file