compile
[kdegraphics.git] / okular / ui / side_reviews.cpp
blobbce820cf316c5bcadb4be05432facc557b9a37b0
1 /***************************************************************************
2 * Copyright (C) 2005 by Enrico Ros <eros.kde@email.it> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 ***************************************************************************/
10 #include "side_reviews.h"
12 // qt/kde includes
13 #include <QtCore/QStringList>
14 #include <QtGui/QHeaderView>
15 #include <QtGui/QLayout>
16 #include <QtGui/QPaintEvent>
17 #include <QtGui/QPainter>
18 #include <QtGui/QSizePolicy>
19 #include <QtGui/QTextDocument>
20 #include <QtGui/QToolBar>
21 #include <QtGui/QTreeView>
23 #include <kaction.h>
24 #include <klocale.h>
25 #include <kiconloader.h>
26 #include <kicon.h>
28 // local includes
29 #include "core/annotations.h"
30 #include "core/document.h"
31 #include "core/page.h"
32 #include "settings.h"
33 #include "annotationpopup.h"
34 #include "annotationproxymodels.h"
35 #include "annotationmodel.h"
36 #include "ktreeviewsearchline.h"
38 class TreeView : public QTreeView
40 public:
41 TreeView( Okular::Document *document, QWidget *parent = 0 )
42 : QTreeView( parent ), m_document( document )
46 protected:
47 virtual void paintEvent( QPaintEvent *event )
49 bool hasAnnotations = false;
50 for ( uint i = 0; i < m_document->pages(); ++i )
51 if ( m_document->page( i )->hasAnnotations() ) {
52 hasAnnotations = true;
53 break;
55 if ( !hasAnnotations ) {
56 QPainter p( viewport() );
57 p.setRenderHint( QPainter::Antialiasing, true );
58 p.setClipRect( event->rect() );
60 QTextDocument document;
61 document.setHtml( i18n( "<div align=center><h3>No annotations</h3>"
62 "To create new annotations press F6 or select <i>Tools -&gt; Review</i>"
63 " from the menu.</div>" ) );
64 document.setTextWidth( width() - 50 );
66 const uint w = document.size().width() + 20;
67 const uint h = document.size().height() + 20;
69 p.setBrush( palette().background() );
70 p.translate( 0.5, 0.5 );
71 p.drawRoundRect( 15, 15, w, h, (8*200)/w, (8*200)/h );
72 p.translate( 20, 20 );
73 document.drawContents( &p );
75 } else {
76 QTreeView::paintEvent( event );
80 private:
81 Okular::Document *m_document;
85 Reviews::Reviews( QWidget * parent, Okular::Document * document )
86 : QWidget( parent ), m_document( document )
88 // create widgets and layout them vertically
89 QVBoxLayout * vLayout = new QVBoxLayout( this );
90 vLayout->setMargin( 0 );
91 vLayout->setSpacing( 6 );
93 m_view = new TreeView( m_document, this );
94 m_view->setAlternatingRowColors( true );
95 m_view->setSelectionMode( QAbstractItemView::ExtendedSelection );
96 m_view->header()->hide();
98 QToolBar *toolBar = new QToolBar( this );
99 toolBar->setObjectName( "reviewOptsBar" );
100 QSizePolicy sp = toolBar->sizePolicy();
101 sp.setVerticalPolicy( QSizePolicy::Minimum );
102 toolBar->setSizePolicy( sp );
104 m_model = new AnnotationModel( m_document, m_view );
106 m_filterProxy = new PageFilterProxyModel( m_view );
107 m_groupProxy = new PageGroupProxyModel( m_view );
108 m_authorProxy = new AuthorGroupProxyModel( m_view );
110 m_filterProxy->setSourceModel( m_model );
111 m_groupProxy->setSourceModel( m_filterProxy );
112 m_authorProxy->setSourceModel( m_groupProxy );
115 m_view->setModel( m_authorProxy );
117 vLayout->addWidget( new KTreeViewSearchLine( this, m_view ) );
118 vLayout->addWidget( m_view );
119 vLayout->addWidget( toolBar );
121 toolBar->setIconSize( QSize( 16, 16 ) );
122 toolBar->setMovable( false );
123 // - add Page button
124 QAction * groupByPageAction = toolBar->addAction( KIcon( "text-x-generic" ), i18n( "Group by Page" ) );
125 groupByPageAction->setCheckable( true );
126 connect( groupByPageAction, SIGNAL( toggled( bool ) ), this, SLOT( slotPageEnabled( bool ) ) );
127 groupByPageAction->setChecked( Okular::Settings::groupByPage() );
128 // - add Author button
129 QAction * groupByAuthorAction = toolBar->addAction( KIcon( "user-identity" ), i18n( "Group by Author" ) );
130 groupByAuthorAction->setCheckable( true );
131 connect( groupByAuthorAction, SIGNAL( toggled( bool ) ), this, SLOT( slotAuthorEnabled( bool ) ) );
132 groupByAuthorAction->setChecked( Okular::Settings::groupByAuthor() );
134 // - add separator
135 toolBar->addSeparator();
136 // - add Current Page Only button
137 QAction * curPageOnlyAction = toolBar->addAction( KIcon( "arrow-down" ), i18n( "Show reviews for current page only" ) );
138 curPageOnlyAction->setCheckable( true );
139 connect( curPageOnlyAction, SIGNAL( toggled( bool ) ), this, SLOT( slotCurrentPageOnly( bool ) ) );
140 curPageOnlyAction->setChecked( Okular::Settings::currentPageOnly() );
142 connect( m_view, SIGNAL( activated( const QModelIndex& ) ),
143 this, SLOT( activated( const QModelIndex& ) ) );
145 m_view->setContextMenuPolicy( Qt::CustomContextMenu );
146 connect( m_view, SIGNAL( customContextMenuRequested( const QPoint& ) ),
147 this, SLOT( contextMenuRequested( const QPoint& ) ) );
151 //BEGIN DocumentObserver Notifies
152 void Reviews::notifyViewportChanged( bool )
154 m_filterProxy->setCurrentPage( m_document->currentPage() );
156 //END DocumentObserver Notifies
158 //BEGIN GUI Slots -> requestListViewUpdate
159 void Reviews::slotPageEnabled( bool on )
161 // store toggle state in Settings and update the listview
162 Okular::Settings::setGroupByPage( on );
163 m_groupProxy->groupByPage( on );
165 m_view->expandAll();
168 void Reviews::slotAuthorEnabled( bool on )
170 // store toggle state in Settings and update the listview
171 Okular::Settings::setGroupByAuthor( on );
172 m_authorProxy->groupByAuthor( on );
174 m_view->expandAll();
177 void Reviews::slotCurrentPageOnly( bool on )
179 // store toggle state in Settings and update the listview
180 Okular::Settings::setCurrentPageOnly( on );
181 m_filterProxy->groupByCurrentPage( on );
183 m_view->expandAll();
185 //END GUI Slots
188 void Reviews::activated( const QModelIndex &index )
190 const QModelIndex authorIndex = m_authorProxy->mapToSource( index );
191 const QModelIndex filterIndex = m_groupProxy->mapToSource( authorIndex );
192 const QModelIndex annotIndex = m_filterProxy->mapToSource( filterIndex );
194 Okular::Annotation *annotation = m_model->annotationForIndex( annotIndex );
195 if ( !annotation )
196 return;
198 int pageNumber = m_model->data( annotIndex, AnnotationModel::PageRole ).toInt();
199 const Okular::Page * page = m_document->page( pageNumber );
201 // calculating the right coordinates to center the view on the annotation
202 QRect rect = Okular::AnnotationUtils::annotationGeometry( annotation, page->width(), page->height() );
203 Okular::NormalizedRect nr( rect, (int)page->width(), (int)page->height() );
204 // set the viewport parameters
205 Okular::DocumentViewport vp;
206 vp.pageNumber = pageNumber;
207 vp.rePos.enabled = true;
208 vp.rePos.pos = Okular::DocumentViewport::Center;
209 vp.rePos.normalizedX = ( nr.right + nr.left ) / 2.0;
210 vp.rePos.normalizedY = ( nr.bottom + nr.top ) / 2.0;
211 // setting the viewport
212 m_document->setViewport( vp, -1, true );
215 QModelIndexList Reviews::retrieveAnnotations(const QModelIndex& idx) const
217 QModelIndexList ret;
218 if ( idx.isValid() )
220 if ( idx.model()->hasChildren( idx ) )
222 int rowCount = idx.model()->rowCount( idx );
223 for ( int i = 0; i < rowCount; i++ )
225 ret += retrieveAnnotations( idx.child( i, idx.column() ) );
228 else
230 ret += idx;
234 return ret;
237 void Reviews::contextMenuRequested( const QPoint &pos )
239 AnnotationPopup popup( m_document, this );
240 connect( &popup, SIGNAL( setAnnotationWindow( Okular::Annotation* ) ),
241 this, SIGNAL( setAnnotationWindow( Okular::Annotation* ) ) );
242 connect( &popup, SIGNAL( removeAnnotationWindow( Okular::Annotation* ) ),
243 this, SIGNAL( removeAnnotationWindow( Okular::Annotation* ) ) );
245 QModelIndexList indexes = m_view->selectionModel()->selectedIndexes();
246 Q_FOREACH ( const QModelIndex &index, indexes )
248 QModelIndexList annotations = retrieveAnnotations(index);
249 Q_FOREACH ( const QModelIndex &idx, annotations )
251 const QModelIndex authorIndex = m_authorProxy->mapToSource( idx );
252 const QModelIndex filterIndex = m_groupProxy->mapToSource( authorIndex );
253 const QModelIndex annotIndex = m_filterProxy->mapToSource( filterIndex );
254 Okular::Annotation *annotation = m_model->annotationForIndex( annotIndex );
255 if ( annotation )
257 const int pageNumber = m_model->data( annotIndex, AnnotationModel::PageRole ).toInt();
258 popup.addAnnotation( annotation, pageNumber );
263 popup.exec( m_view->viewport()->mapToGlobal( pos ) );
266 #include "side_reviews.moc"