pageview.cpp 188 KB
Newer Older
1
/***************************************************************************
2
 *   Copyright (C) 2004-2005 by Enrico Ros <eros.kde@email.it>             *
Albert Astals Cid's avatar
Albert Astals Cid committed
3
 *   Copyright (C) 2004-2006 by Albert Astals Cid <tsdgeos@terra.es>       *
4
 *                                                                         *
5
 *   With portions of code from kpdf/kpdf_pagewidget.cc by:                *
6 7 8 9 10 11
 *     Copyright (C) 2002 by Wilco Greven <greven@kde.org>                 *
 *     Copyright (C) 2003 by Christophe Devriese                           *
 *                           <Christophe.Devriese@student.kuleuven.ac.be>  *
 *     Copyright (C) 2003 by Laurent Montel <montel@kde.org>               *
 *     Copyright (C) 2003 by Dirk Mueller <mueller@kde.org>                *
 *     Copyright (C) 2004 by James Ots <kde@jamesots.com>                  *
Jiri Baum's avatar
Jiri Baum committed
12
 *     Copyright (C) 2011 by Jiri Baum - NICTA <jiri@baum.com.au>          *
13 14 15 16 17 18 19
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

Albert Astals Cid's avatar
Albert Astals Cid committed
20 21
#include "pageview.h"

22
// qt/kde includes
23
#include <qcursor.h>
24
#include <qevent.h>
25
#include <qimage.h>
26 27
#include <qpainter.h>
#include <qtimer.h>
28
#include <qset.h>
29
#include <qscrollbar.h>
30
#include <qtooltip.h>
Enrico Ros's avatar
Enrico Ros committed
31 32
#include <qapplication.h>
#include <qclipboard.h>
33

34
#include <kaction.h>
35
#include <kactionmenu.h>
Laurent Montel's avatar
Laurent Montel committed
36
#include <kstandardaction.h>
37
#include <kactioncollection.h>
38
#include <kmenu.h>
39
#include <klocale.h>
40
#include <kfiledialog.h>
Pino Toscano's avatar
Pino Toscano committed
41
#include <kglobal.h>
42
#include <kglobalsettings.h>
43
#include <kinputdialog.h>
Albert Astals Cid's avatar
compile  
Albert Astals Cid committed
44 45
#include <kselectaction.h>
#include <ktoggleaction.h>
46
#include <kdebug.h>
Albert Astals Cid's avatar
Albert Astals Cid committed
47
#include <kdeversion.h>
48
#include <kmessagebox.h>
Laurent Montel's avatar
Laurent Montel committed
49
#include <kicon.h>
50 51 52
#include <kurifilter.h>
#include <kstringhandler.h>
#include <ktoolinvocation.h>
Albert Astals Cid's avatar
compile  
Albert Astals Cid committed
53

54
// system includes
55
#include <math.h>
56
#include <stdlib.h>
57

58
// local includes
59
#include "formwidgets.h"
60
#include "pageviewutils.h"
61
#include "pagepainter.h"
62
#include "core/annotations.h"
63
#include "annotwindow.h"
Pino Toscano's avatar
Pino Toscano committed
64
#include "guiutils.h"
65
#include "annotationpopup.h"
66
#include "pageviewannotator.h"
67
#include "toolaction.h"
68
#include "tts.h"
69
#include "videowidget.h"
Pino Toscano's avatar
Pino Toscano committed
70
#include "core/action.h"
71
#include "core/area.h"
72
#include "core/document.h"
73
#include "core/form.h"
74
#include "core/page.h"
75
#include "core/misc.h"
76
#include "core/generator.h"
77
#include "core/movie.h"
78
#include "core/sourcereference.h"
79
#include "core/tile.h"
80
#include "settings.h"
81
#include "settings_core.h"
82

83 84
static int pageflags = PagePainter::Accessibility | PagePainter::EnhanceLinks |
                       PagePainter::EnhanceImages | PagePainter::Highlights |
85
                       PagePainter::TextSelection | PagePainter::Annotations;
86

87 88 89 90 91
static inline double normClamp( double value, double def )
{
    return ( value < 0.0 || value > 1.0 ) ? def : value;
}

Jiri Baum's avatar
Jiri Baum committed
92 93 94 95 96
struct TableSelectionPart {
    PageViewItem * item;
    Okular::NormalizedRect rectInItem;
    Okular::NormalizedRect rectInSelection;

97
    TableSelectionPart(PageViewItem * item_p, const Okular::NormalizedRect &rectInItem_p, const Okular::NormalizedRect &rectInSelection_p);
Jiri Baum's avatar
Jiri Baum committed
98 99
};

100
TableSelectionPart::TableSelectionPart(  PageViewItem * item_p, const Okular::NormalizedRect &rectInItem_p, const Okular::NormalizedRect &rectInSelection_p)
Jiri Baum's avatar
Jiri Baum committed
101 102 103 104
    : item ( item_p ), rectInItem (rectInItem_p), rectInSelection (rectInSelection_p)
{
}

105 106 107 108
// structure used internally by PageView for data storage
class PageViewPrivate
{
public:
109 110 111
    PageViewPrivate( PageView *qq );

    FormWidgetsController* formWidgetsController();
112
    OkularTTS* tts();
113
    QString selectedText() const;
114

115
    // the document, pageviewItems and the 'visible cache'
116
    PageView *q;
117
    Okular::Document * document;
118 119
    QVector< PageViewItem * > items;
    QLinkedList< PageViewItem * > visibleItems;
120

121
    // view layout (columns and continuous in Settings), zoom and mouse
122 123 124
    PageView::ZoomMode zoomMode;
    float zoomFactor;
    QPoint mouseGrabPos;
125
    QPoint mousePressPos;
126
    QPoint mouseSelectPos;
127 128
    bool mouseMidZooming;
    int mouseMidLastY;
129
    bool mouseSelecting;
130
    QRect mouseSelectionRect;
131
    QColor mouseSelectionColor;
132
    bool mouseTextSelecting;
133
    QSet< int > pagesWithTextSelection;
134
    bool mouseOnRect;
135 136
    Okular::Annotation * mouseAnn;
    QPoint mouseAnnPos;
137
    int mouseAnnPageNum;
138

Jiri Baum's avatar
Jiri Baum committed
139 140 141 142
    // table selection
    QList<double> tableSelectionCols;
    QList<double> tableSelectionRows;
    QList<TableSelectionPart> tableSelectionParts;
143
    bool tableDividersGuessed;
Jiri Baum's avatar
Jiri Baum committed
144

Enrico Ros's avatar
Enrico Ros committed
145 146 147 148
    // viewport move
    bool viewportMoveActive;
    QTime viewportMoveTime;
    QPoint viewportMoveDest;
149 150 151
    int lastSourceLocationViewportPageNumber;
    double lastSourceLocationViewportNormalizedX;
    double lastSourceLocationViewportNormalizedY;
Enrico Ros's avatar
Enrico Ros committed
152 153
    QTimer * viewportMoveTimer;
    // auto scroll
154
    int scrollIncrement;
Enrico Ros's avatar
Enrico Ros committed
155
    QTimer * autoScrollTimer;
156
    // annotations
157
    PageViewAnnotator * annotator;
158
    //text annotation dialogs list
159
    QHash< Okular::Annotation *, AnnotWindow * > m_annowindows;
Enrico Ros's avatar
Enrico Ros committed
160
    // other stuff
161
    QTimer * delayResizeEventTimer;
162
    bool dirtyLayout;
Enrico Ros's avatar
Enrico Ros committed
163
    bool blockViewport;                 // prevents changes to viewport
164 165
    bool blockPixmapsRequest;           // prevent pixmap requests
    PageViewMessage * messageWindow;    // in pageviewutils.h
166
    bool m_formsVisible;
167
    FormWidgetsController *formsWidgetController;
168
    OkularTTS * m_tts;
169 170
    QTimer * refreshTimer;
    int refreshPage;
171

172
    // infinite resizing loop prevention
173
    bool verticalScrollBarVisible;
174

175 176 177 178
    // drag scroll
    QPoint dragScrollVector;
    QTimer dragScrollTimer;

179 180 181
    // left click depress
    QTimer leftClickTimer;

182
    // actions
183 184 185
    KAction * aRotateClockwise;
    KAction * aRotateCounterClockwise;
    KAction * aRotateOriginal;
186
    KSelectAction * aPageSizes;
187
    KToggleAction * aTrimMargins;
188 189 190
    KAction * aMouseNormal;
    KAction * aMouseSelect;
    KAction * aMouseTextSelect;
Jiri Baum's avatar
Jiri Baum committed
191
    KAction * aMouseTableSelect;
192
    KToggleAction * aToggleAnnotator;
193
    KSelectAction * aZoom;
194 195
    KAction * aZoomIn;
    KAction * aZoomOut;
196 197
    KToggleAction * aZoomFitWidth;
    KToggleAction * aZoomFitPage;
198
    KActionMenu * aViewMode;
199
    KToggleAction * aViewContinuous;
Laurent Montel's avatar
Laurent Montel committed
200
    QAction * aPrevAction;
201
    KAction * aToggleForms;
202 203
    KAction * aSpeakDoc;
    KAction * aSpeakPage;
204
    KAction * aSpeakStop;
205
    KActionCollection * actionCollection;
206
    QActionGroup * mouseModeActionGroup;
207

208
    int setting_viewCols;
209 210 211

    // Keep track of whether tablet pen is currently pressed down
    bool penDown;
212 213
};

214 215 216 217 218 219 220 221 222 223
PageViewPrivate::PageViewPrivate( PageView *qq )
    : q( qq )
{
}

FormWidgetsController* PageViewPrivate::formWidgetsController()
{
    if ( !formsWidgetController )
    {
        formsWidgetController = new FormWidgetsController();
Laurent Montel's avatar
Laurent Montel committed
224 225 226 227
        QObject::connect( formsWidgetController, SIGNAL(changed(FormWidgetIface*)),
                          q, SLOT(slotFormWidgetChanged(FormWidgetIface*)) );
        QObject::connect( formsWidgetController, SIGNAL(action(Okular::Action*)),
                          q, SLOT(slotAction(Okular::Action*)) );
228 229 230 231 232
    }

    return formsWidgetController;
}

233 234 235 236
OkularTTS* PageViewPrivate::tts()
{
    if ( !m_tts )
    {
237
        m_tts = new OkularTTS( q );
238 239
        if ( aSpeakStop )
        {
Laurent Montel's avatar
Laurent Montel committed
240 241 242 243
            QObject::connect( m_tts, SIGNAL(hasSpeechs(bool)),
                              aSpeakStop, SLOT(setEnabled(bool)) );
            QObject::connect( m_tts, SIGNAL(errorMessage(QString)),
                              q, SLOT(errorMessage(QString)) );
244
        }
245 246 247 248 249
    }

    return m_tts;
}

250

251 252 253 254
/* PageView. What's in this file? -> quick overview.
 * Code weight (in rows) and meaning:
 *  160 - constructor and creating actions plus their connected slots (empty stuff)
 *  70  - DocumentObserver inherited methodes (important)
255
 *  550 - events: mouse, keyboard, drag/drop
256
 *  170 - slotRelayoutPages: set contents of the scrollview on continuous/single modes
257
 *  100 - zoom: zooming pages in different ways, keeping update the toolbar actions, etc..
258
 *  other misc functions: only slotRequestVisiblePixmaps and pickItemOnPoint noticeable,
259
 * and many insignificant stuff like this comment :-)
260
 */
261
PageView::PageView( QWidget *parent, Okular::Document *document )
262
    : QAbstractScrollArea( parent )
263
    , Okular::View( QString::fromLatin1( "PageView" ) )
264 265
{
    // create and initialize private storage structure
266
    d = new PageViewPrivate( this );
267
    d->document = document;
268 269 270
    d->aRotateClockwise = 0;
    d->aRotateCounterClockwise = 0;
    d->aRotateOriginal = 0;
271
    d->aViewMode = 0;
272
    d->zoomMode = PageView::ZoomFitWidth;
273
    d->zoomFactor = 1.0;
274
    d->mouseMidZooming = false;
275
    d->mouseSelecting = false;
276
    d->mouseTextSelecting = false;
277
    d->mouseOnRect = false;
278
    d->mouseAnn = 0;
279
    d->tableDividersGuessed = false;
Enrico Ros's avatar
Enrico Ros committed
280
    d->viewportMoveActive = false;
281 282 283
    d->lastSourceLocationViewportPageNumber = -1;
    d->lastSourceLocationViewportNormalizedX = 0.0;
    d->lastSourceLocationViewportNormalizedY = 0.0;
Enrico Ros's avatar
Enrico Ros committed
284
    d->viewportMoveTimer = 0;
285
    d->scrollIncrement = 0;
Enrico Ros's avatar
Enrico Ros committed
286
    d->autoScrollTimer = 0;
287
    d->annotator = 0;
288
    d->dirtyLayout = false;
289
    d->blockViewport = false;
290
    d->blockPixmapsRequest = false;
291
    d->messageWindow = new PageViewMessage(this);
292
    d->m_formsVisible = false;
293
    d->formsWidgetController = 0;
294
    d->m_tts = 0;
295 296
    d->refreshTimer = 0;
    d->refreshPage = -1;
297 298 299 300
    d->aRotateClockwise = 0;
    d->aRotateCounterClockwise = 0;
    d->aRotateOriginal = 0;
    d->aPageSizes = 0;
301
    d->aTrimMargins = 0;
302 303 304 305 306 307 308 309
    d->aMouseNormal = 0;
    d->aMouseSelect = 0;
    d->aMouseTextSelect = 0;
    d->aToggleAnnotator = 0;
    d->aZoomFitWidth = 0;
    d->aZoomFitPage = 0;
    d->aViewMode = 0;
    d->aViewContinuous = 0;
310
    d->aPrevAction = 0;
311
    d->aToggleForms = 0;
312 313
    d->aSpeakDoc = 0;
    d->aSpeakPage = 0;
314
    d->aSpeakStop = 0;
315
    d->actionCollection = 0;
316
    d->aPageSizes=0;
317
    d->setting_viewCols = Okular::Settings::viewColumns();
318
    d->mouseModeActionGroup = 0;
319
    d->penDown = false;
320

Albert Astals Cid's avatar
Albert Astals Cid committed
321
    switch( Okular::Settings::zoomMode() )
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    {
        case 0:
        {
            d->zoomFactor = 1;
            d->zoomMode = PageView::ZoomFixed;
            break;
        }
        case 1:
        {
            d->zoomMode = PageView::ZoomFitWidth;
            break;
        }
        case 2:
        {
            d->zoomMode = PageView::ZoomFitPage;
            break;
        }
    }

341 342
    d->delayResizeEventTimer = new QTimer( this );
    d->delayResizeEventTimer->setSingleShot( true );
Laurent Montel's avatar
Laurent Montel committed
343
    connect( d->delayResizeEventTimer, SIGNAL(timeout()), this, SLOT(delayedResizeEvent()) );
344

345 346
    setFrameStyle(QFrame::NoFrame);

Pino Toscano's avatar
Pino Toscano committed
347 348
    setAttribute( Qt::WA_StaticContents );

Albert Astals Cid's avatar
Albert Astals Cid committed
349
    setObjectName( QLatin1String( "okular::pageView" ) );
Pino Toscano's avatar
Pino Toscano committed
350

351
    // viewport setup: setup focus, accept drops and track mouse
352
    viewport()->setFocusProxy( this );
353
    viewport()->setFocusPolicy( Qt::StrongFocus );
354 355
    viewport()->setAttribute( Qt::WA_OpaquePaintEvent );
    viewport()->setAttribute( Qt::WA_NoSystemBackground );
356
    setAcceptDrops( true );
357
    viewport()->setMouseTracking( true );
358
    viewport()->setAutoFillBackground( false );
359 360 361
    // the apparently "magic" value of 20 is the same used internally in QScrollArea
    verticalScrollBar()->setSingleStep( 20 );
    horizontalScrollBar()->setSingleStep( 20 );
362 363

    // conntect the padding of the viewport to pixmaps requests
364 365
    connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(slotRequestVisiblePixmaps(int)));
    connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(slotRequestVisiblePixmaps(int)));
366
    connect( &d->dragScrollTimer, SIGNAL(timeout()), this, SLOT(slotDragScroll()) );
367

368 369 370
    d->leftClickTimer.setSingleShot( true );
    connect( &d->leftClickTimer, SIGNAL(timeout()), this, SLOT(slotShowSizeAllCursor()) );

371
    // set a corner button to resize the view to the page size
372 373 374 375
//    QPushButton * resizeButton = new QPushButton( viewport() );
//    resizeButton->setPixmap( SmallIcon("crop") );
//    setCornerWidget( resizeButton );
//    resizeButton->setEnabled( false );
376
    // connect(...);
Tobias Koenig's avatar
Tobias Koenig committed
377
    setAttribute( Qt::WA_InputMethodEnabled, true );
378

379 380 381
    connect(document, SIGNAL(processMovieAction(const Okular::MovieAction*)), this, SLOT(slotProcessMovieAction(const Okular::MovieAction*)));
    connect(document, SIGNAL(processRenditionAction(const Okular::RenditionAction*)), this, SLOT(slotProcessRenditionAction(const Okular::RenditionAction*)));

382
    // schedule the welcome message
383
    QMetaObject::invokeMethod(this, "slotShowWelcome", Qt::QueuedConnection);
384 385 386 387
}

PageView::~PageView()
{
Pino Toscano's avatar
Pino Toscano committed
388 389 390
    if ( d->m_tts )
        d->m_tts->stopAllSpeechs();

391
    // delete the local storage structure
392 393 394 395 396 397 398
    
    // We need to assign it to a different list otherwise slotAnnotationWindowDestroyed
    // will bite us and clear d->m_annowindows
    QHash< Okular::Annotation *, AnnotWindow * > annowindows = d->m_annowindows;
    d->m_annowindows.clear();
    qDeleteAll( annowindows );

399
    // delete all widgets
Laurent Montel's avatar
Laurent Montel committed
400
    QVector< PageViewItem * >::const_iterator dIt = d->items.constBegin(), dEnd = d->items.constEnd();
401 402
    for ( ; dIt != dEnd; ++dIt )
        delete *dIt;
403
    delete d->formsWidgetController;
Albert Astals Cid's avatar
Albert Astals Cid committed
404
    d->document->removeObserver( this );
405 406 407
    delete d;
}

408 409 410 411 412
void PageView::setupBaseActions( KActionCollection * ac )
{
    d->actionCollection = ac;

    // Zoom actions ( higher scales takes lots of memory! )
413
    d->aZoom  = new KSelectAction(KIcon( "page-zoom" ), i18n("Zoom"), this);
414 415
    ac->addAction("zoom_to", d->aZoom );
    d->aZoom->setEditable( true );
416
    d->aZoom->setMaxComboViewCount( 14 );
Laurent Montel's avatar
Laurent Montel committed
417
    connect( d->aZoom, SIGNAL(triggered(QAction*)), this, SLOT(slotZoom()) );
418 419
    updateZoomText();

Laurent Montel's avatar
Laurent Montel committed
420
    d->aZoomIn = KStandardAction::zoomIn( this, SLOT(slotZoomIn()), ac );
421

Laurent Montel's avatar
Laurent Montel committed
422
    d->aZoomOut = KStandardAction::zoomOut( this, SLOT(slotZoomOut()), ac );
423 424
}

425
void PageView::setupViewerActions( KActionCollection * ac )
426
{
427 428
    d->actionCollection = ac;

429 430 431
    d->aZoomIn->setShortcut( QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Plus) );
    d->aZoomOut->setShortcut( QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Minus) );

432
    // orientation menu actions
433
    d->aRotateClockwise = new KAction( KIcon( "object-rotate-right" ), i18n( "Rotate &Right" ), this );
434
    d->aRotateClockwise->setIconText( i18nc( "Rotate right", "Right" ) );
435
    ac->addAction( "view_orientation_rotate_cw", d->aRotateClockwise );
436
    d->aRotateClockwise->setEnabled( false );
Laurent Montel's avatar
Laurent Montel committed
437
    connect( d->aRotateClockwise, SIGNAL(triggered()), this, SLOT(slotRotateClockwise()) );
438
    d->aRotateCounterClockwise = new KAction( KIcon( "object-rotate-left" ), i18n( "Rotate &Left" ), this );
439
    d->aRotateCounterClockwise->setIconText( i18nc( "Rotate left", "Left" ) );
440
    ac->addAction( "view_orientation_rotate_ccw", d->aRotateCounterClockwise );
441
    d->aRotateCounterClockwise->setEnabled( false );
Laurent Montel's avatar
Laurent Montel committed
442
    connect( d->aRotateCounterClockwise, SIGNAL(triggered()), this, SLOT(slotRotateCounterClockwise()) );
443 444
    d->aRotateOriginal = new KAction( i18n( "Original Orientation" ), this );
    ac->addAction( "view_orientation_original", d->aRotateOriginal );
445
    d->aRotateOriginal->setEnabled( false );
Laurent Montel's avatar
Laurent Montel committed
446
    connect( d->aRotateOriginal, SIGNAL(triggered()), this, SLOT(slotRotateOriginal()) );
447

Laurent Montel's avatar
Laurent Montel committed
448 449
    d->aPageSizes = new KSelectAction(i18n("&Page Size"), this);
    ac->addAction("view_pagesizes", d->aPageSizes);
450
    d->aPageSizes->setEnabled( false );
451

Laurent Montel's avatar
Laurent Montel committed
452 453
    connect( d->aPageSizes , SIGNAL(triggered(int)),
         this, SLOT(slotPageSizes(int)) );
454

455
    d->aTrimMargins  = new KToggleAction( i18n( "&Trim Margins" ), this );
456
    ac->addAction("view_trim_margins", d->aTrimMargins );
Laurent Montel's avatar
Laurent Montel committed
457
    connect( d->aTrimMargins, SIGNAL(toggled(bool)), SLOT(slotTrimMarginsToggled(bool)) );
458
    d->aTrimMargins->setChecked( Okular::Settings::trimMargins() );
459

460
    d->aZoomFitWidth  = new KToggleAction(KIcon( "zoom-fit-width" ), i18n("Fit &Width"), this);
Pino Toscano's avatar
Pino Toscano committed
461
    ac->addAction("view_fit_to_width", d->aZoomFitWidth );
Laurent Montel's avatar
Laurent Montel committed
462
    connect( d->aZoomFitWidth, SIGNAL(toggled(bool)), SLOT(slotFitToWidthToggled(bool)) );
463

464
    d->aZoomFitPage  = new KToggleAction(KIcon( "zoom-fit-best" ), i18n("Fit &Page"), this);
Pino Toscano's avatar
Pino Toscano committed
465
    ac->addAction("view_fit_to_page", d->aZoomFitPage );
Laurent Montel's avatar
Laurent Montel committed
466
    connect( d->aZoomFitPage, SIGNAL(toggled(bool)), SLOT(slotFitToPageToggled(bool)) );
467 468

    // View-Layout actions
469 470 471 472 473 474 475 476 477 478 479
    d->aViewMode = new KActionMenu( KIcon( "view-split-left-right" ), i18n( "&View Mode" ), this );
    d->aViewMode->setDelayed( false );
#define ADD_VIEWMODE_ACTION( text, name, id ) \
do { \
    KAction *vm = new KAction( text, d->aViewMode->menu() ); \
    vm->setCheckable( true ); \
    vm->setData( qVariantFromValue( id ) ); \
    d->aViewMode->addAction( vm ); \
    ac->addAction( name, vm ); \
    vmGroup->addAction( vm ); \
} while( 0 )
480
    ac->addAction("view_render_mode", d->aViewMode );
481
    QActionGroup *vmGroup = new QActionGroup( d->aViewMode->menu() );
482 483
    ADD_VIEWMODE_ACTION( i18n( "Single Page" ), "view_render_mode_single", (int)Okular::Settings::EnumViewMode::Single );
    ADD_VIEWMODE_ACTION( i18n( "Facing Pages" ), "view_render_mode_facing", (int)Okular::Settings::EnumViewMode::Facing );
484
    ADD_VIEWMODE_ACTION( i18n( "Facing Pages (Center First Page)" ), "view_render_mode_facing_center_first", (int)Okular::Settings::EnumViewMode::FacingFirstCentered );
485 486 487 488 489 490 491 492 493
    ADD_VIEWMODE_ACTION( i18n( "Overview" ), "view_render_mode_overview", (int)Okular::Settings::EnumViewMode::Summary );
    const QList<QAction *> viewModeActions = d->aViewMode->menu()->actions();
    foreach(QAction *viewModeAction, viewModeActions)
    {
        if (viewModeAction->data().toInt() == Okular::Settings::viewMode())
        {
            viewModeAction->setChecked( true );
        }
    }
Laurent Montel's avatar
Laurent Montel committed
494
    connect( vmGroup, SIGNAL(triggered(QAction*)), this, SLOT(slotViewMode(QAction*)) );
495
#undef ADD_VIEWMODE_ACTION
496

497
    d->aViewContinuous  = new KToggleAction(KIcon( "view-list-text" ), i18n("&Continuous"), this);
Laurent Montel's avatar
Laurent Montel committed
498
    ac->addAction("view_continuous", d->aViewContinuous );
Laurent Montel's avatar
Laurent Montel committed
499
    connect( d->aViewContinuous, SIGNAL(toggled(bool)), SLOT(slotContinuousToggled(bool)) );
500
    d->aViewContinuous->setChecked( Okular::Settings::viewContinuous() );
501

502 503 504
    // Mouse mode actions for viewer mode
    d->mouseModeActionGroup = new QActionGroup( this );
    d->mouseModeActionGroup->setExclusive( true );
505
    d->aMouseNormal  = new KAction( KIcon( "input-mouse" ), i18n( "&Browse Tool" ), this );
Laurent Montel's avatar
Laurent Montel committed
506
    ac->addAction("mouse_drag", d->aMouseNormal );
Laurent Montel's avatar
Laurent Montel committed
507
    connect( d->aMouseNormal, SIGNAL(triggered()), this, SLOT(slotSetMouseNormal()) );
Pino Toscano's avatar
Pino Toscano committed
508
    d->aMouseNormal->setIconText( i18nc( "Browse Tool", "Browse" ) );
Pino Toscano's avatar
Pino Toscano committed
509
    d->aMouseNormal->setCheckable( true );
510
    d->aMouseNormal->setShortcut( Qt::CTRL + Qt::Key_1 );
511
    d->aMouseNormal->setActionGroup( d->mouseModeActionGroup );
512
    d->aMouseNormal->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::Browse );
513

514
    KAction * mz  = new KAction(KIcon( "page-zoom" ), i18n("&Zoom Tool"), this);
Laurent Montel's avatar
Laurent Montel committed
515
    ac->addAction("mouse_zoom", mz );
Laurent Montel's avatar
Laurent Montel committed
516
    connect( mz, SIGNAL(triggered()), this, SLOT(slotSetMouseZoom()) );
Pino Toscano's avatar
Pino Toscano committed
517
    mz->setIconText( i18nc( "Zoom Tool", "Zoom" ) );
Pino Toscano's avatar
Pino Toscano committed
518
    mz->setCheckable( true );
519
    mz->setShortcut( Qt::CTRL + Qt::Key_2 );
520
    mz->setActionGroup( d->mouseModeActionGroup );
521
    mz->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::Zoom );
522 523 524 525 526 527 528 529 530 531 532
}

// WARNING: 'setupViewerActions' must have been called before this method
void PageView::setupActions( KActionCollection * ac )
{
    d->actionCollection = ac;

    d->aZoomIn->setShortcut( KStandardShortcut::zoomIn() );
    d->aZoomOut->setShortcut( KStandardShortcut::zoomOut() );

    // Mouse-Mode actions
533
    d->aMouseSelect  = new KAction(KIcon( "select-rectangular" ), i18n("&Selection Tool"), this);
Laurent Montel's avatar
Laurent Montel committed
534
    ac->addAction("mouse_select", d->aMouseSelect );
Laurent Montel's avatar
Laurent Montel committed
535
    connect( d->aMouseSelect, SIGNAL(triggered()), this, SLOT(slotSetMouseSelect()) );
Pino Toscano's avatar
Pino Toscano committed
536
    d->aMouseSelect->setIconText( i18nc( "Select Tool", "Selection" ) );
Pino Toscano's avatar
Pino Toscano committed
537
    d->aMouseSelect->setCheckable( true );
538
    d->aMouseSelect->setShortcut( Qt::CTRL + Qt::Key_3 );
539
    d->aMouseSelect->setActionGroup( d->mouseModeActionGroup );
540
    d->aMouseSelect->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::RectSelect );
541

542
    d->aMouseTextSelect  = new KAction(KIcon( "draw-text" ), i18n("&Text Selection Tool"), this);
Laurent Montel's avatar
Laurent Montel committed
543
    ac->addAction("mouse_textselect", d->aMouseTextSelect );
Laurent Montel's avatar
Laurent Montel committed
544
    connect( d->aMouseTextSelect, SIGNAL(triggered()), this, SLOT(slotSetMouseTextSelect()) );
Pino Toscano's avatar
Pino Toscano committed
545
    d->aMouseTextSelect->setIconText( i18nc( "Text Selection Tool", "Text Selection" ) );
546
    d->aMouseTextSelect->setCheckable( true );
547
    d->aMouseTextSelect->setShortcut( Qt::CTRL + Qt::Key_4 );
548
    d->aMouseTextSelect->setActionGroup( d->mouseModeActionGroup );
549
    d->aMouseTextSelect->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::TextSelect );
550

Jiri Baum's avatar
Jiri Baum committed
551 552 553 554 555 556
    d->aMouseTableSelect  = new KAction(KIcon( "select-table" ), i18n("T&able Selection Tool"), this);
    ac->addAction("mouse_tableselect", d->aMouseTableSelect );
    connect( d->aMouseTableSelect, SIGNAL( triggered() ), this, SLOT( slotSetMouseTableSelect() ) );
    d->aMouseTableSelect->setIconText( i18nc( "Table Selection Tool", "Table Selection" ) );
    d->aMouseTableSelect->setCheckable( true );
    d->aMouseTableSelect->setShortcut( Qt::CTRL + Qt::Key_5 );
557
    d->aMouseTableSelect->setActionGroup( d->mouseModeActionGroup );
558
    d->aMouseTableSelect->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::TableSelect );
Jiri Baum's avatar
Jiri Baum committed
559

Pino Toscano's avatar
Pino Toscano committed
560
    d->aToggleAnnotator  = new KToggleAction(KIcon( "draw-freehand" ), i18n("&Review"), this);
Laurent Montel's avatar
Laurent Montel committed
561
    ac->addAction("mouse_toggle_annotate", d->aToggleAnnotator );
Pino Toscano's avatar
Pino Toscano committed
562
    d->aToggleAnnotator->setCheckable( true );
Laurent Montel's avatar
Laurent Montel committed
563
    connect( d->aToggleAnnotator, SIGNAL(toggled(bool)), SLOT(slotToggleAnnotator(bool)) );
Albert Astals Cid's avatar
Albert Astals Cid committed
564
    d->aToggleAnnotator->setShortcut( Qt::Key_F6 );
565

566 567 568 569
    ToolAction *ta = new ToolAction( this );
    ac->addAction( "mouse_selecttools", ta );
    ta->addAction( d->aMouseSelect );
    ta->addAction( d->aMouseTextSelect );
570
    ta->addAction( d->aMouseTableSelect );
571

572
    // speak actions
573 574 575
    d->aSpeakDoc = new KAction( KIcon( "text-speak" ), i18n( "Speak Whole Document" ), this );
    ac->addAction( "speak_document", d->aSpeakDoc );
    d->aSpeakDoc->setEnabled( false );
Laurent Montel's avatar
Laurent Montel committed
576
    connect( d->aSpeakDoc, SIGNAL(triggered()), SLOT(slotSpeakDocument()) );
577

578 579 580
    d->aSpeakPage = new KAction( KIcon( "text-speak" ), i18n( "Speak Current Page" ), this );
    ac->addAction( "speak_current_page", d->aSpeakPage );
    d->aSpeakPage->setEnabled( false );
Laurent Montel's avatar
Laurent Montel committed
581
    connect( d->aSpeakPage, SIGNAL(triggered()), SLOT(slotSpeakCurrentPage()) );
582

583 584 585
    d->aSpeakStop = new KAction( KIcon( "media-playback-stop" ), i18n( "Stop Speaking" ), this );
    ac->addAction( "speak_stop_all", d->aSpeakStop );
    d->aSpeakStop->setEnabled( false );
Laurent Montel's avatar
Laurent Montel committed
586
    connect( d->aSpeakStop, SIGNAL(triggered()), SLOT(slotStopSpeaks()) );
587

588
    // Other actions
Laurent Montel's avatar
Laurent Montel committed
589 590
    KAction * su  = new KAction(i18n("Scroll Up"), this);
    ac->addAction("view_scroll_up", su );
591
    connect( su, SIGNAL(triggered()), this, SLOT(slotAutoScrollUp()) );
592
    su->setShortcut( QKeySequence(Qt::SHIFT + Qt::Key_Up) );
Albert Astals Cid's avatar
Albert Astals Cid committed
593
    addAction(su);
594

Laurent Montel's avatar
Laurent Montel committed
595 596
    KAction * sd  = new KAction(i18n("Scroll Down"), this);
    ac->addAction("view_scroll_down", sd );
597
    connect( sd, SIGNAL(triggered()), this, SLOT(slotAutoScrollDown()) );
598
    sd->setShortcut( QKeySequence(Qt::SHIFT + Qt::Key_Down) );
Albert Astals Cid's avatar
Albert Astals Cid committed
599
    addAction(sd);
600

601 602 603 604 605 606 607 608 609 610 611 612
    KAction * spu = new KAction(i18n("Scroll Page Up"), this);
    ac->addAction( "view_scroll_page_up", spu );
    connect( spu, SIGNAL(triggered()), this, SLOT(slotScrollUp()) );
    spu->setShortcut( QKeySequence(Qt::SHIFT + Qt::Key_Space) );
    addAction( spu );

    KAction * spd = new KAction(i18n("Scroll Page Down"), this);
    ac->addAction( "view_scroll_page_down", spd );
    connect( spd, SIGNAL(triggered()), this, SLOT(slotScrollDown()) );
    spd->setShortcut( QKeySequence(Qt::Key_Space) );
    addAction( spd );

613 614
    d->aToggleForms = new KAction( this );
    ac->addAction( "view_toggle_forms", d->aToggleForms );
Laurent Montel's avatar
Laurent Montel committed
615
    connect( d->aToggleForms, SIGNAL(triggered()), this, SLOT(slotToggleForms()) );
616
    d->aToggleForms->setEnabled( false );
617
    toggleFormWidgets( false );
618 619
}

Pino Toscano's avatar
Pino Toscano committed
620
bool PageView::canFitPageWidth() const
621
{
622
    return Okular::Settings::viewMode() != Okular::Settings::EnumViewMode::Single || d->zoomMode != ZoomFitWidth;
623 624
}

625
void PageView::fitPageWidth( int page )
626
{
627
    // zoom: Fit Width, columns: 1. setActions + relayout + setPage + update
628
    d->zoomMode = ZoomFitWidth;
629
    Okular::Settings::setViewMode( 0 );
630 631
    d->aZoomFitWidth->setChecked( true );
    d->aZoomFitPage->setChecked( false );
632
    d->aViewMode->menu()->actions().at( 0 )->setChecked( true );
633 634 635
    viewport()->setUpdatesEnabled( false );
    slotRelayoutPages();
    viewport()->setUpdatesEnabled( true );
636
    d->document->setViewportPage( page );
637
    updateZoomText();
Enrico Ros's avatar
Enrico Ros committed
638
    setFocus();
639 640
}

641
void PageView::openAnnotationWindow( Okular::Annotation * annotation, int pageNumber )
642
{
643
    if ( !annotation )
644
        return;
645 646 647

    // find the annot window
    AnnotWindow* existWindow = 0;
Laurent Montel's avatar
Laurent Montel committed
648 649
    QHash< Okular::Annotation *, AnnotWindow * >::ConstIterator it = d->m_annowindows.constFind( annotation );
    if ( it != d->m_annowindows.constEnd() )
650
    {
651
        existWindow = *it;
652
    }
653 654

    if ( existWindow == 0 )
655
    {
656 657
        existWindow = new AnnotWindow( this, annotation, d->document, pageNumber );
        connect(existWindow, SIGNAL(destroyed(QObject*)), this, SLOT(slotAnnotationWindowDestroyed(QObject*)));
658

659
        d->m_annowindows.insert( annotation, existWindow );
660
    }
661 662 663 664

    existWindow->show();
}

665
void PageView::slotAnnotationWindowDestroyed( QObject * window )
666
{
667 668 669
    QHash< Okular::Annotation*, AnnotWindow * >::Iterator it = d->m_annowindows.begin();
    QHash< Okular::Annotation*, AnnotWindow * >::Iterator itEnd = d->m_annowindows.end();
    while ( it != itEnd )
670
    {
671 672 673 674 675 676 677 678
        if ( it.value() == window )
        {
            it = d->m_annowindows.erase( it );
        }
        else
        {
            ++it;
        }
679
    }
680 681
}