From ddf4878ec7780b6d6a30e85b7d13834769edd5f7 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Fri, 15 Jan 2016 16:55:23 +0100 Subject: [PATCH] Apply SelectionFilter in Partition{Labels,Bars}View. --- src/modules/partition/gui/PartitionBarsView.cpp | 14 +++++++++++++- src/modules/partition/gui/PartitionBarsView.h | 7 +++++++ src/modules/partition/gui/PartitionLabelsView.cpp | 5 ++++- src/modules/partition/gui/PartitionLabelsView.h | 7 +++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/modules/partition/gui/PartitionBarsView.cpp b/src/modules/partition/gui/PartitionBarsView.cpp index 77c8ccf47..2599a1c8f 100644 --- a/src/modules/partition/gui/PartitionBarsView.cpp +++ b/src/modules/partition/gui/PartitionBarsView.cpp @@ -53,6 +53,7 @@ static const int SELECTION_MARGIN = qMin( ( EXTENDED_PARTITION_MARGIN - 2 ) / 2, PartitionBarsView::PartitionBarsView( QWidget* parent ) : QAbstractItemView( parent ) , m_hoveredIndex( QModelIndex() ) + , canBeSelected( []( const QModelIndex& ) { return true; } ) { setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ); setFrameStyle( QFrame::NoFrame ); @@ -397,6 +398,13 @@ PartitionBarsView::setSelectionModel( QItemSelectionModel* selectionModel ) } +void +PartitionBarsView::setSelectionFilter( std::function< bool ( const QModelIndex& ) > canBeSelected ) +{ + this->canBeSelected = canBeSelected; +} + + QModelIndex PartitionBarsView::moveCursor( CursorAction cursorAction, Qt::KeyboardModifiers modifiers ) { @@ -430,7 +438,11 @@ PartitionBarsView::setSelection( const QRect& rect, QItemSelectionModel::Selecti // TL;DR: this sucks, look away. -- Teo 12/2015 int x1, y1, x2, y2; rect.getCoords( &x1, &y1, &x2, &y2 ); - selectionModel()->select( indexAt( QPoint( x2, y2 ) ), flags ); + + QModelIndex eventIndex = indexAt( QPoint( x2, y2 ) ); + if ( canBeSelected( eventIndex ) ) + selectionModel()->select( eventIndex, flags ); + viewport()->repaint(); } diff --git a/src/modules/partition/gui/PartitionBarsView.h b/src/modules/partition/gui/PartitionBarsView.h index 0d4ff624f..f8c5fd71d 100644 --- a/src/modules/partition/gui/PartitionBarsView.h +++ b/src/modules/partition/gui/PartitionBarsView.h @@ -19,8 +19,11 @@ #ifndef PARTITIONPREVIEW_H #define PARTITIONPREVIEW_H +#include "PartitionViewSelectionFilter.h" + #include + /** * A Qt model view which displays the partitions inside a device as a colored bar. * @@ -48,6 +51,8 @@ public: void setSelectionModel( QItemSelectionModel* selectionModel ) override; + void setSelectionFilter( SelectionFilter canBeSelected ); + protected: // QAbstractItemView API QRegion visualRegionForSelection( const QItemSelection& selection ) const override; @@ -69,6 +74,8 @@ private: QModelIndex indexAt( const QPoint& point, const QRect& rect, const QModelIndex& parent ) const; QRect visualRect( const QModelIndex& index, const QRect& rect, const QModelIndex& parent ) const; + SelectionFilter canBeSelected; + struct Item { qreal size; diff --git a/src/modules/partition/gui/PartitionLabelsView.cpp b/src/modules/partition/gui/PartitionLabelsView.cpp index 916569721..6bb90c4e6 100644 --- a/src/modules/partition/gui/PartitionLabelsView.cpp +++ b/src/modules/partition/gui/PartitionLabelsView.cpp @@ -53,6 +53,7 @@ buildUnknownDisklabelTexts( Device* dev ) PartitionLabelsView::PartitionLabelsView( QWidget* parent ) : QAbstractItemView( parent ) + , canBeSelected( []( const QModelIndex& ) { return true; } ) { setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ); setFrameStyle( QFrame::NoFrame ); @@ -523,7 +524,9 @@ PartitionLabelsView::isIndexHidden( const QModelIndex& index ) const void PartitionLabelsView::setSelection( const QRect& rect, QItemSelectionModel::SelectionFlags flags ) { - selectionModel()->select( indexAt( rect.topLeft() ), flags ); + QModelIndex eventIndex = indexAt( rect.topLeft() ); + if ( canBeSelected( eventIndex ) ) + selectionModel()->select( eventIndex, flags ); } diff --git a/src/modules/partition/gui/PartitionLabelsView.h b/src/modules/partition/gui/PartitionLabelsView.h index 8a9f39f2b..6b68fe582 100644 --- a/src/modules/partition/gui/PartitionLabelsView.h +++ b/src/modules/partition/gui/PartitionLabelsView.h @@ -20,6 +20,8 @@ #ifndef PARTITIONLABELSVIEW_H #define PARTITIONLABELSVIEW_H +#include "PartitionViewSelectionFilter.h" + #include /** @@ -51,6 +53,8 @@ public: void setSelectionModel( QItemSelectionModel* selectionModel ) override; + void setSelectionFilter( SelectionFilter canBeSelected ); + protected: // QAbstractItemView API QRegion visualRegionForSelection( const QItemSelection& selection ) const override; @@ -75,6 +79,9 @@ private: const QPoint& pos , bool selected ); QModelIndexList getIndexesToDraw( const QModelIndex& parent ) const; QStringList buildTexts( const QModelIndex& index ) const; + + SelectionFilter canBeSelected; + QString m_customNewRootLabel; QPersistentModelIndex m_hoveredIndex; };