📄 thumbnaillist.cpp.svn-base
字号:
//BEGIN widget events void ThumbnailList::keyPressEvent( QKeyEvent * keyEvent ){ if ( m_thumbnails.count() < 1 ) return keyEvent->ignore(); int nextPage = -1; if ( keyEvent->key() == Qt::Key_Up ) { if ( !m_selected ) nextPage = 0; else if ( m_vectorIndex > 0 ) nextPage = m_thumbnails[ m_vectorIndex - 1 ]->pageNumber(); } else if ( keyEvent->key() == Qt::Key_Down ) { if ( !m_selected ) nextPage = 0; else if ( m_vectorIndex < (int)m_thumbnails.count() - 1 ) nextPage = m_thumbnails[ m_vectorIndex + 1 ]->pageNumber(); } else if ( keyEvent->key() == Qt::Key_PageUp ) verticalScrollBar()->triggerAction( QScrollBar::SliderPageStepSub ); else if ( keyEvent->key() == Qt::Key_PageDown ) verticalScrollBar()->triggerAction( QScrollBar::SliderPageStepAdd ); else if ( keyEvent->key() == Qt::Key_Home ) nextPage = m_thumbnails[ 0 ]->pageNumber(); else if ( keyEvent->key() == Qt::Key_End ) nextPage = m_thumbnails[ m_thumbnails.count() - 1 ]->pageNumber(); if ( nextPage == -1 ) return keyEvent->ignore(); keyEvent->accept(); if ( m_selected ) m_selected->setSelected( false ); m_selected = 0; m_document->setViewportPage( nextPage );}bool ThumbnailList::viewportEvent( QEvent * e ){ switch ( e->type() ) { case QEvent::Resize: { viewportResizeEvent( (QResizeEvent*)e ); break; } default: ; } return QScrollArea::viewportEvent( e );}void ThumbnailList::viewportResizeEvent( QResizeEvent * e ){ if ( m_thumbnails.count() < 1 || width() < 1 ) return; // if width changed resize all the Thumbnails, reposition them to the // right place and recalculate the contents area if ( e->size().width() != e->oldSize().width() ) { // runs the timer avoiding a thumbnail regeneration by 'contentsMoving' delayedRequestVisiblePixmaps( 2000 ); // resize and reposition items int newWidth = contentsRect().width() - verticalScrollBar()->width(); int newHeight = 0; QVector<ThumbnailWidget *>::iterator tIt = m_thumbnails.begin(), tEnd = m_thumbnails.end(); for ( ; tIt != tEnd; ++tIt ) { ThumbnailWidget *t = *tIt; t->resizeFitWidth( newWidth ); newHeight += t->height() + m_pagesLayout->spacing(); } // update scrollview's contents size (sets scrollbars limits) newHeight -= m_pagesLayout->spacing(); m_pagesWidget->resize( newWidth, newHeight ); // ensure selected item remains visible if ( m_selected ) ensureVisible( 0, m_selected->mapToParent( QPoint( 0, 0 ) ).y() + m_selected->height()/2, 0, viewport()->height()/2 ); } else if ( e->size().height() <= e->oldSize().height() ) return; // invalidate the bookmark overlay if ( m_bookmarkOverlay ) { delete m_bookmarkOverlay; m_bookmarkOverlay = 0; } // update Thumbnails since width has changed or height has increased delayedRequestVisiblePixmaps( 500 );}void ThumbnailList::dragEnterEvent( QDragEnterEvent * ev ){ ev->accept();}void ThumbnailList::dropEvent( QDropEvent * ev ){ if ( KUrl::List::canDecode( ev->mimeData() ) ) emit urlDropped( KUrl::List::fromMimeData( ev->mimeData() ).first() );}//END widget events//BEGIN internal SLOTS void ThumbnailList::slotRequestVisiblePixmaps( int /*newContentsY*/ ){ // if an update is already scheduled or the widget is hidden, don't proceed if ( (m_delayTimer && m_delayTimer->isActive()) || isHidden() ) return; // scroll from the top to the last visible thumbnail m_visibleThumbnails.clear(); QLinkedList< PixmapRequest * > requestedPixmaps; QVector<ThumbnailWidget *>::iterator tIt = m_thumbnails.begin(), tEnd = m_thumbnails.end(); for ( ; tIt != tEnd; ++tIt ) { ThumbnailWidget * t = *tIt; QRect thumbRect = t->rect().translated( m_pagesWidget->mapToParent( t->pos() ) ); if ( !thumbRect.intersects( viewport()->rect() ) ) continue; // add ThumbnailWidget to visible list m_visibleThumbnails.push_back( t ); // if pixmap not present add it to requests if ( !t->page()->hasPixmap( THUMBNAILS_ID, t->pixmapWidth(), t->pixmapHeight() ) ) { PixmapRequest * p = new PixmapRequest( THUMBNAILS_ID, t->pageNumber(), t->pixmapWidth(), t->pixmapHeight(), THUMBNAILS_PRIO, true ); requestedPixmaps.push_back( p ); } } // actually request pixmaps if ( !requestedPixmaps.isEmpty() ) m_document->requestPixmaps( requestedPixmaps );}void ThumbnailList::slotDelayTimeout(){ // resize the bookmark overlay delete m_bookmarkOverlay; int expectedWidth = viewport()->width() / 4; if ( expectedWidth > 10 ) m_bookmarkOverlay = new QPixmap( DesktopIcon( "attach", expectedWidth ) ); else m_bookmarkOverlay = 0; // request pixmaps slotRequestVisiblePixmaps();}//END internal SLOTSvoid ThumbnailList::delayedRequestVisiblePixmaps( int delayMs ){ if ( !m_delayTimer ) { m_delayTimer = new QTimer( this ); m_delayTimer->setSingleShot( true ); connect( m_delayTimer, SIGNAL( timeout() ), this, SLOT( slotDelayTimeout() ) ); } m_delayTimer->start( delayMs );}/** ThumbnailWidget implementation **/ThumbnailWidget::ThumbnailWidget( QWidget * parent, const KPDFPage * kp, ThumbnailList * tl ) : QWidget( parent ), m_tl( tl ), m_page( kp ), m_selected( false ), m_pixmapWidth( 10 ), m_pixmapHeight( 10 ){ m_labelNumber = m_page->number() + 1; m_labelHeight = QFontMetrics( font() ).height();}void ThumbnailWidget::resizeFitWidth( int width ){ m_pixmapWidth = width - m_margin; m_pixmapHeight = (int)round( m_page->ratio() * (double)m_pixmapWidth ); resize( width, heightHint() );}void ThumbnailWidget::setSelected( bool selected ){ // update selected state if ( m_selected != selected ) { m_selected = selected; update(); }}void ThumbnailWidget::setVisibleRect( const NormalizedRect & rect ){ if ( rect == m_visibleRect ) return; m_visibleRect = rect; update();}QSize ThumbnailWidget::sizeHint() const{ return QSize( width(), heightHint() );}void ThumbnailWidget::mouseReleaseEvent( QMouseEvent * e ){ // don't handle the mouse click, forward it to the thumbnail list m_tl->forwardClick( m_page, e->globalPos(), e->button() );}void ThumbnailWidget::contextMenuEvent( QContextMenuEvent * e ){ // don't handle the mouse click, forward it to the thumbnail list m_tl->forwardClick( m_page, e->globalPos(), Qt::RightButton );}void ThumbnailWidget::paintEvent( QPaintEvent * e ){ int width = m_pixmapWidth + m_margin; QRect clipRect = e->rect(); if ( !clipRect.isValid() ) return; QPainter p( this ); QPalette pal = palette(); // draw the bottom label + highlight mark QColor fillColor = m_selected ? pal.color( QPalette::Active, QPalette::Highlight ) : pal.color( QPalette::Active, QPalette::Base ); p.fillRect( clipRect, fillColor ); p.setPen( m_selected ? pal.color( QPalette::Active, QPalette::HighlightedText ) : pal.color( QPalette::Active, QPalette::Text ) ); p.drawText( 0, m_pixmapHeight + m_margin, width, m_labelHeight, Qt::AlignCenter, QString::number( m_labelNumber ) ); // draw page outline and pixmap if ( clipRect.top() < m_pixmapHeight + m_margin ) { // if page is bookmarked draw a colored border bool isBookmarked = m_page->hasBookmark(); // draw the inner rect p.setPen( isBookmarked ? QColor( 0xFF8000 ) : Qt::black ); p.drawRect( m_margin/2 - 1, m_margin/2 - 1, m_pixmapWidth + 2, m_pixmapHeight + 2 ); // draw the clear rect p.setPen( isBookmarked ? QColor( 0x804000 ) : pal.color( QPalette::Active, QPalette::Base ) ); // draw the bottom and right shadow edges if ( !isBookmarked ) { int left, right, bottom, top; left = m_margin/2 + 1; right = m_margin/2 + m_pixmapWidth + 1; bottom = m_pixmapHeight + m_margin/2 + 1; top = m_margin/2 + 1; p.setPen( Qt::gray ); p.drawLine( left, bottom, right, bottom ); p.drawLine( right, top, right, bottom ); } // draw the page using the shared PagePainter class p.translate( m_margin/2, m_margin/2 ); clipRect.translate( -m_margin/2, -m_margin/2 ); clipRect = clipRect.intersect( QRect( 0, 0, m_pixmapWidth, m_pixmapHeight ) ); if ( clipRect.isValid() ) { int flags = PagePainter::Accessibility | PagePainter::Highlights | PagePainter::Annotations; PagePainter::paintPageOnPainter( &p, m_page, THUMBNAILS_ID, flags, m_pixmapWidth, m_pixmapHeight, clipRect ); } if ( !m_visibleRect.isNull() ) { p.setPen( QPen( QBrush( Qt::red ), 2 ) ); p.setBrush( Qt::NoBrush ); p.drawRect( m_visibleRect.geometry( m_pixmapWidth, m_pixmapHeight ) ); } // draw the bookmark overlay on the top-right corner const QPixmap * bookmarkPixmap = m_tl->getBookmarkOverlay(); if ( isBookmarked && bookmarkPixmap ) { int pixW = bookmarkPixmap->width(), pixH = bookmarkPixmap->height(); clipRect = clipRect.intersect( QRect( m_pixmapWidth - pixW, 0, pixW, pixH ) ); if ( clipRect.isValid() ) p.drawPixmap( m_pixmapWidth - pixW, -pixH/8, *bookmarkPixmap ); } }}/** ThumbnailsController implementation **/#define FILTERB_ID 1ThumbnailController::ThumbnailController( QWidget * parent, ThumbnailList * list ) : QToolBar( parent ){ setObjectName( "ThumbsControlBar" ); // change toolbar appearance setIconSize( QSize( 16, 16 ) ); setMovable( false ); QSizePolicy sp = sizePolicy(); sp.setVerticalPolicy( QSizePolicy::Minimum ); setSizePolicy( sp ); // insert a togglebutton [show only bookmarked pages] //insertSeparator(); QAction * showBoomarkOnlyAction = addAction( KIcon( "bookmark" ), i18n( "Show bookmarked pages only" ) ); showBoomarkOnlyAction->setCheckable( true ); connect( showBoomarkOnlyAction, SIGNAL( toggled( bool ) ), list, SLOT( slotFilterBookmarks( bool ) ) ); showBoomarkOnlyAction->setChecked( KpdfSettings::filterBookmarks() ); //insertLineSeparator();}#include "thumbnaillist.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -