diff --git a/ui/pageview.cpp b/ui/pageview.cpp --- a/ui/pageview.cpp +++ b/ui/pageview.cpp @@ -1411,11 +1411,7 @@ } } - QLinkedList< Okular::Annotation * >::ConstIterator annIt = qFind( annots, d->mouseAnnotation->annotation() ); - if ( annIt == annItEnd ) - { - d->mouseAnnotation->cancel(); - } + d->mouseAnnotation->notifyAnnotationChanged( pageNumber ); } if ( changedFlags & DocumentObserver::BoundingBox ) diff --git a/ui/pageviewmouseannotation.h b/ui/pageviewmouseannotation.h --- a/ui/pageviewmouseannotation.h +++ b/ui/pageviewmouseannotation.h @@ -46,9 +46,10 @@ { public: AnnotationDescription() - : annotation( nullptr ), pageViewItem( nullptr ), pageNumber( 0 ) {} + : annotation( nullptr ), pageViewItem( nullptr ), pageNumber( -1 ) {} AnnotationDescription( PageViewItem * newPageViewItem, const QPoint& eventPos ); bool isValid() const; + bool isContainedInPage( const Okular::Document * document, int pageNumber ) const; void invalidate(); bool operator==( const AnnotationDescription & rhs ) const { @@ -116,7 +117,10 @@ Qt::CursorShape cursor() const; - // needs to be called after document save + /* Forward DocumentObserver::notifyPageChanged to this method. */ + void notifyAnnotationChanged( int pageNumber ); + + /* Forward DocumentObserver::notifySetup to this method. */ void updateAnnotationPointers(); enum MouseAnnotationState { diff --git a/ui/pageviewmouseannotation.cpp b/ui/pageviewmouseannotation.cpp --- a/ui/pageviewmouseannotation.cpp +++ b/ui/pageviewmouseannotation.cpp @@ -42,6 +42,23 @@ return ( annotation != nullptr ); } +bool AnnotationDescription::isContainedInPage( const Okular::Document * document, int pageNumber ) const +{ + if ( AnnotationDescription::pageNumber == pageNumber ) + { + /* Don't access page via pageViewItem here. pageViewItem might have been deleted. */ + const Okular::Page * page = document->page( pageNumber ); + if ( page != nullptr ) + { + if ( page->annotations().contains( annotation ) ) + { + return true; + } + } + } + return false; +} + void AnnotationDescription::invalidate() { annotation = nullptr; @@ -403,6 +420,24 @@ return Qt::ArrowCursor; } +void MouseAnnotation::notifyAnnotationChanged( int pageNumber ) +{ + const AnnotationDescription emptyAd; + + if ( m_focusedAnnotation.isValid() && + ! m_focusedAnnotation.isContainedInPage( m_document, pageNumber ) ) + { + setState( StateInactive, emptyAd ); + } + + if ( m_mouseOverAnnotation.isValid() && + ! m_mouseOverAnnotation.isContainedInPage( m_document, pageNumber ) ) + { + m_mouseOverAnnotation = emptyAd; + m_pageView->updateCursor(); + } +} + void MouseAnnotation::updateAnnotationPointers() { if (m_focusedAnnotation.annotation) @@ -552,9 +587,12 @@ void MouseAnnotation::updateViewport( const AnnotationDescription & ad ) const { const QRect & changedPageViewItemRect = getFullBoundingRect( ad ); - m_pageView->viewport()->update( changedPageViewItemRect - .translated( ad.pageViewItem->uncroppedGeometry().topLeft() ) - .translated( -m_pageView->contentAreaPosition() ) ); + if ( changedPageViewItemRect.isValid() ) + { + m_pageView->viewport()->update( changedPageViewItemRect + .translated( ad.pageViewItem->uncroppedGeometry().topLeft() ) + .translated( -m_pageView->contentAreaPosition() ) ); + } } /* eventPos: Mouse position in uncropped page coordinates.