pageview.cpp 217 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 <aacid@kde.org>          *
4 5 6
 *   Copyright (C) 2017    Klarälvdalens Datakonsult AB, a KDAB Group      *
 *                         company, info@kdab.com. Work sponsored by the   *
 *                         LiMux project of the city of Munich             *
7
 *                                                                         *
8
 *   With portions of code from kpdf/kpdf_pagewidget.cc by:                *
9 10 11 12 13 14
 *     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
15
 *     Copyright (C) 2011 by Jiri Baum - NICTA <jiri@baum.com.au>          *
16 17 18 19 20 21 22
 *                                                                         *
 *   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
23 24
#include "pageview.h"

25
// qt/kde includes
Yuri Chornoivan's avatar
Yuri Chornoivan committed
26
#include <qloggingcategory.h>
27
#include <qcursor.h>
28
#include <qevent.h>
29
#include <qimage.h>
30 31
#include <qpainter.h>
#include <qtimer.h>
32
#include <qset.h>
33
#include <qscrollbar.h>
34
#include <qtooltip.h>
Enrico Ros's avatar
Enrico Ros committed
35 36
#include <qapplication.h>
#include <qclipboard.h>
Vishesh Handa's avatar
Vishesh Handa committed
37
#include <qmenu.h>
38
#include <QInputDialog>
39
#include <qdesktopwidget.h>
40
#include <QDesktopServices>
Laurent Montel's avatar
Laurent Montel committed
41
#include <QMimeDatabase>
Olivier Churlaud's avatar
Olivier Churlaud committed
42
#include <QMimeData>
43
#include <QGestureEvent>
44

Laurent Montel's avatar
Laurent Montel committed
45
#include <qaction.h>
46
#include <kactionmenu.h>
Laurent Montel's avatar
Laurent Montel committed
47
#include <kstandardaction.h>
48
#include <kactioncollection.h>
Lukáš Tinkl's avatar
Lukáš Tinkl committed
49
#include <KLocalizedString>
Albert Astals Cid's avatar
compile  
Albert Astals Cid committed
50 51
#include <kselectaction.h>
#include <ktoggleaction.h>
Yuri Chornoivan's avatar
Yuri Chornoivan committed
52
#include <QDebug>
53
#include <kmessagebox.h>
Laurent Montel's avatar
Laurent Montel committed
54
#include <QIcon>
55 56 57
#include <kurifilter.h>
#include <kstringhandler.h>
#include <ktoolinvocation.h>
58
#include <krun.h>
Albert Astals Cid's avatar
compile  
Albert Astals Cid committed
59

60
// system includes
61
#include <math.h>
62
#include <stdlib.h>
63

64
// local includes
65
#include "debug_ui.h"
66
#include "formwidgets.h"
67
#include "pageviewutils.h"
68
#include "pagepainter.h"
69
#include "core/annotations.h"
70
#include "annotwindow.h"
Pino Toscano's avatar
Pino Toscano committed
71
#include "guiutils.h"
72
#include "annotationpopup.h"
73
#include "pageviewannotator.h"
74
#include "pageviewmouseannotation.h"
75
#include "priorities.h"
76
#include "toolaction.h"
77
#include "okmenutitle.h"
78
#ifdef HAVE_SPEECH
79
#include "tts.h"
80
#endif
81
#include "videowidget.h"
Pino Toscano's avatar
Pino Toscano committed
82
#include "core/action.h"
83
#include "core/area.h"
84
#include "core/document_p.h"
85
#include "core/form.h"
86
#include "core/page.h"
87
#include "core/page_p.h"
88
#include "core/misc.h"
89
#include "core/generator.h"
90
#include "core/movie.h"
Jaydeep Solanki's avatar
Jaydeep Solanki committed
91
#include "core/audioplayer.h"
92
#include "core/sourcereference.h"
93
#include "core/tile.h"
94
#include "settings.h"
95
#include "settings_core.h"
96
#include "url_utils.h"
Michal Humpula's avatar
Michal Humpula committed
97
#include "magnifierview.h"
98

99
static const int pageflags = PagePainter::Accessibility | PagePainter::EnhanceLinks |
100
                       PagePainter::EnhanceImages | PagePainter::Highlights |
101
                       PagePainter::TextSelection | PagePainter::Annotations;
102

Tingnan Zhang's avatar
Tingnan Zhang committed
103 104
static const float kZoomValues[] = { 0.12, 0.25, 0.33, 0.50, 0.66, 0.75, 1.00, 1.25, 1.50, 2.00, 4.00, 8.00, 16.00 };

105 106 107 108 109
static inline double normClamp( double value, double def )
{
    return ( value < 0.0 || value > 1.0 ) ? def : value;
}

Jiri Baum's avatar
Jiri Baum committed
110 111 112 113 114
struct TableSelectionPart {
    PageViewItem * item;
    Okular::NormalizedRect rectInItem;
    Okular::NormalizedRect rectInSelection;

115
    TableSelectionPart(PageViewItem * item_p, const Okular::NormalizedRect &rectInItem_p, const Okular::NormalizedRect &rectInSelection_p);
Jiri Baum's avatar
Jiri Baum committed
116 117
};

118
TableSelectionPart::TableSelectionPart(  PageViewItem * item_p, const Okular::NormalizedRect &rectInItem_p, const Okular::NormalizedRect &rectInSelection_p)
Jiri Baum's avatar
Jiri Baum committed
119 120 121 122
    : item ( item_p ), rectInItem (rectInItem_p), rectInSelection (rectInSelection_p)
{
}

123 124 125 126
// structure used internally by PageView for data storage
class PageViewPrivate
{
public:
127 128 129
    PageViewPrivate( PageView *qq );

    FormWidgetsController* formWidgetsController();
130
#ifdef HAVE_SPEECH
131
    OkularTTS* tts();
132
#endif
133
    QString selectedText() const;
134

135
    // the document, pageviewItems and the 'visible cache'
136
    PageView *q;
137
    Okular::Document * document;
138 139
    QVector< PageViewItem * > items;
    QLinkedList< PageViewItem * > visibleItems;
Michal Humpula's avatar
Michal Humpula committed
140
    MagnifierView *magnifierView;
141

142
    // view layout (columns and continuous in Settings), zoom and mouse
143 144 145
    PageView::ZoomMode zoomMode;
    float zoomFactor;
    QPoint mouseGrabPos;
146
    QPoint mousePressPos;
147
    QPoint mouseSelectPos;
148
    QPoint previousMouseMovePos;
149
    int mouseMidLastY;
150
    bool mouseSelecting;
151
    QRect mouseSelectionRect;
152
    QColor mouseSelectionColor;
153
    bool mouseTextSelecting;
154
    QSet< int > pagesWithTextSelection;
155
    bool mouseOnRect;
156
    int mouseMode;
157
    MouseAnnotation * mouseAnnotation;
158

Jiri Baum's avatar
Jiri Baum committed
159 160 161 162
    // table selection
    QList<double> tableSelectionCols;
    QList<double> tableSelectionRows;
    QList<TableSelectionPart> tableSelectionParts;
163
    bool tableDividersGuessed;
Jiri Baum's avatar
Jiri Baum committed
164

Enrico Ros's avatar
Enrico Ros committed
165 166 167 168
    // viewport move
    bool viewportMoveActive;
    QTime viewportMoveTime;
    QPoint viewportMoveDest;
169 170 171
    int lastSourceLocationViewportPageNumber;
    double lastSourceLocationViewportNormalizedX;
    double lastSourceLocationViewportNormalizedY;
Enrico Ros's avatar
Enrico Ros committed
172
    QTimer * viewportMoveTimer;
173
    int controlWheelAccumulatedDelta;
Enrico Ros's avatar
Enrico Ros committed
174
    // auto scroll
175
    int scrollIncrement;
Enrico Ros's avatar
Enrico Ros committed
176
    QTimer * autoScrollTimer;
177
    // annotations
178
    PageViewAnnotator * annotator;
179
    //text annotation dialogs list
180
    QSet< AnnotWindow * > m_annowindows;
Enrico Ros's avatar
Enrico Ros committed
181
    // other stuff
182
    QTimer * delayResizeEventTimer;
183
    bool dirtyLayout;
Enrico Ros's avatar
Enrico Ros committed
184
    bool blockViewport;                 // prevents changes to viewport
185 186
    bool blockPixmapsRequest;           // prevent pixmap requests
    PageViewMessage * messageWindow;    // in pageviewutils.h
187
    bool m_formsVisible;
188
    FormWidgetsController *formsWidgetController;
189
#ifdef HAVE_SPEECH
190
    OkularTTS * m_tts;
191
#endif
192
    QTimer * refreshTimer;
193
    QSet<int> refreshPages;
194

Jake Linder's avatar
Jake Linder committed
195 196
    // bbox state for Trim to Selection mode
    Okular::NormalizedRect trimBoundingBox;
197

198
    // infinite resizing loop prevention
199
    bool verticalScrollBarVisible;
Thomas Fischer's avatar
Thomas Fischer committed
200
    bool horizontalScrollBarVisible;
201

202 203 204 205
    // drag scroll
    QPoint dragScrollVector;
    QTimer dragScrollTimer;

206 207 208
    // left click depress
    QTimer leftClickTimer;

209
    // actions
Vishesh Handa's avatar
Vishesh Handa committed
210 211 212
    QAction * aRotateClockwise;
    QAction * aRotateCounterClockwise;
    QAction * aRotateOriginal;
213
    KSelectAction * aPageSizes;
Jake Linder's avatar
Jake Linder committed
214
    KActionMenu * aTrimMode;
215
    KToggleAction * aTrimMargins;
Vishesh Handa's avatar
Vishesh Handa committed
216 217 218 219 220
    QAction * aMouseNormal;
    QAction * aMouseSelect;
    QAction * aMouseTextSelect;
    QAction * aMouseTableSelect;
    QAction * aMouseMagnifier;
Jake Linder's avatar
Jake Linder committed
221
    KToggleAction * aTrimToSelection;
222
    KToggleAction * aToggleAnnotator;
223
    KSelectAction * aZoom;
224 225
    QAction * aZoomIn;
    QAction * aZoomOut;
Nate Graham's avatar
Nate Graham committed
226
    QAction * aZoomActual;
227 228
    KToggleAction * aZoomFitWidth;
    KToggleAction * aZoomFitPage;
Thomas Fischer's avatar
Thomas Fischer committed
229
    KToggleAction * aZoomAutoFit;
230
    KActionMenu * aViewMode;
231
    KToggleAction * aViewContinuous;
Laurent Montel's avatar
Laurent Montel committed
232
    QAction * aPrevAction;
Vishesh Handa's avatar
Vishesh Handa committed
233 234 235 236
    QAction * aToggleForms;
    QAction * aSpeakDoc;
    QAction * aSpeakPage;
    QAction * aSpeakStop;
237
    KActionCollection * actionCollection;
238
    QActionGroup * mouseModeActionGroup;
239
    QAction * aFitWindowToPage;
240

241
    int setting_viewCols;
242
    bool rtl_Mode;
243 244
    // Keep track of whether tablet pen is currently pressed down
    bool penDown;
245 246 247

    // Keep track of mouse over link object
    const Okular::ObjectRect * mouseOverLinkObject;
248 249
};

250 251
PageViewPrivate::PageViewPrivate( PageView *qq )
    : q( qq )
252
#ifdef HAVE_SPEECH
253
    , m_tts( nullptr )
254
#endif
255 256 257 258 259 260 261
{
}

FormWidgetsController* PageViewPrivate::formWidgetsController()
{
    if ( !formsWidgetController )
    {
Jon Mease's avatar
Jon Mease committed
262 263 264 265 266
        formsWidgetController = new FormWidgetsController( document );
        QObject::connect( formsWidgetController, SIGNAL( changed( int ) ),
                          q, SLOT( slotFormChanged( int ) ) );
        QObject::connect( formsWidgetController, SIGNAL( action( Okular::Action* ) ),
                          q, SLOT( slotAction( Okular::Action* ) ) );
267 268 269 270 271
    }

    return formsWidgetController;
}

272
#ifdef HAVE_SPEECH
273 274 275 276
OkularTTS* PageViewPrivate::tts()
{
    if ( !m_tts )
    {
277
        m_tts = new OkularTTS( q );
278 279
        if ( aSpeakStop )
        {
Laurent Montel's avatar
Laurent Montel committed
280 281
            QObject::connect( m_tts, &OkularTTS::isSpeaking,
                              aSpeakStop, &QAction::setEnabled );
282
        }
283 284 285 286
    }

    return m_tts;
}
287
#endif
288

289

290 291 292 293
/* 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)
294
 *  550 - events: mouse, keyboard, drag
295
 *  170 - slotRelayoutPages: set contents of the scrollview on continuous/single modes
296
 *  100 - zoom: zooming pages in different ways, keeping update the toolbar actions, etc..
297
 *  other misc functions: only slotRequestVisiblePixmaps and pickItemOnPoint noticeable,
298
 * and many insignificant stuff like this comment :-)
299
 */
300
PageView::PageView( QWidget *parent, Okular::Document *document )
301
    : QAbstractScrollArea( parent )
Laurent Montel's avatar
Laurent Montel committed
302
    , Okular::View( QStringLiteral( "PageView" ) )
303 304
{
    // create and initialize private storage structure
305
    d = new PageViewPrivate( this );
306
    d->document = document;
307 308 309 310
    d->aRotateClockwise = nullptr;
    d->aRotateCounterClockwise = nullptr;
    d->aRotateOriginal = nullptr;
    d->aViewMode = nullptr;
311
    d->zoomMode = PageView::ZoomFitWidth;
312
    d->zoomFactor = 1.0;
313
    d->mouseSelecting = false;
314
    d->mouseTextSelecting = false;
315
    d->mouseOnRect = false;
316
    d->mouseMode = Okular::Settings::mouseMode();
317
    d->mouseAnnotation = new MouseAnnotation( this, document );
318
    d->tableDividersGuessed = false;
Enrico Ros's avatar
Enrico Ros committed
319
    d->viewportMoveActive = false;
320 321 322
    d->lastSourceLocationViewportPageNumber = -1;
    d->lastSourceLocationViewportNormalizedX = 0.0;
    d->lastSourceLocationViewportNormalizedY = 0.0;
323
    d->viewportMoveTimer = nullptr;
324
    d->controlWheelAccumulatedDelta = 0;
325
    d->scrollIncrement = 0;
326 327
    d->autoScrollTimer = nullptr;
    d->annotator = nullptr;
328
    d->dirtyLayout = false;
329
    d->blockViewport = false;
330
    d->blockPixmapsRequest = false;
331
    d->messageWindow = new PageViewMessage(this);
332
    d->m_formsVisible = false;
333
    d->formsWidgetController = nullptr;
334
#ifdef HAVE_SPEECH
335
    d->m_tts = nullptr;
336
#endif
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    d->refreshTimer = nullptr;
    d->aRotateClockwise = nullptr;
    d->aRotateCounterClockwise = nullptr;
    d->aRotateOriginal = nullptr;
    d->aPageSizes = nullptr;
    d->aTrimMode = nullptr;
    d->aTrimMargins = nullptr;
    d->aTrimToSelection = nullptr;
    d->aMouseNormal = nullptr;
    d->aMouseSelect = nullptr;
    d->aMouseTextSelect = nullptr;
    d->aToggleAnnotator = nullptr;
    d->aZoomFitWidth = nullptr;
    d->aZoomFitPage = nullptr;
    d->aZoomAutoFit = nullptr;
    d->aViewMode = nullptr;
    d->aViewContinuous = nullptr;
    d->aPrevAction = nullptr;
    d->aToggleForms = nullptr;
    d->aSpeakDoc = nullptr;
    d->aSpeakPage = nullptr;
    d->aSpeakStop = nullptr;
    d->actionCollection = nullptr;
    d->aPageSizes=nullptr;
361
    d->setting_viewCols = Okular::Settings::viewColumns();
362
    d->rtl_Mode = Okular::Settings::rtlReadingDirection();
363
    d->mouseModeActionGroup = nullptr;
364
    d->penDown = false;
365 366
    d->aMouseMagnifier = nullptr;
    d->aFitWindowToPage = nullptr;
Jake Linder's avatar
Jake Linder committed
367
    d->trimBoundingBox = Okular::NormalizedRect(); // Null box
368

Albert Astals Cid's avatar
Albert Astals Cid committed
369
    switch( Okular::Settings::zoomMode() )
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    {
        case 0:
        {
            d->zoomFactor = 1;
            d->zoomMode = PageView::ZoomFixed;
            break;
        }
        case 1:
        {
            d->zoomMode = PageView::ZoomFitWidth;
            break;
        }
        case 2:
        {
            d->zoomMode = PageView::ZoomFitPage;
            break;
        }
Thomas Fischer's avatar
Thomas Fischer committed
387 388 389 390 391
        case 3:
        {
            d->zoomMode = PageView::ZoomFitAuto;
            break;
        }
392 393
    }

394 395
    d->delayResizeEventTimer = new QTimer( this );
    d->delayResizeEventTimer->setSingleShot( true );
Laurent Montel's avatar
Laurent Montel committed
396
    connect( d->delayResizeEventTimer, &QTimer::timeout, this, &PageView::delayedResizeEvent );
397

398 399
    setFrameStyle(QFrame::NoFrame);

Pino Toscano's avatar
Pino Toscano committed
400 401
    setAttribute( Qt::WA_StaticContents );

Laurent Montel's avatar
Laurent Montel committed
402
    setObjectName( QStringLiteral( "okular::pageView" ) );
Pino Toscano's avatar
Pino Toscano committed
403

404
    // viewport setup: setup focus, and track mouse
405
    viewport()->setFocusProxy( this );
406
    viewport()->setFocusPolicy( Qt::StrongFocus );
407 408 409
    viewport()->setAttribute( Qt::WA_OpaquePaintEvent );
    viewport()->setAttribute( Qt::WA_NoSystemBackground );
    viewport()->setMouseTracking( true );
410
    viewport()->setAutoFillBackground( false );
411
    // the apparently "magic" value of 20 is the same used internally in QScrollArea
412
    verticalScrollBar()->setCursor( Qt::ArrowCursor );
413
    verticalScrollBar()->setSingleStep( 20 );
414
    horizontalScrollBar()->setCursor( Qt::ArrowCursor );
415
    horizontalScrollBar()->setSingleStep( 20 );
416

Yuri Chornoivan's avatar
Yuri Chornoivan committed
417
    // connect the padding of the viewport to pixmaps requests
Laurent Montel's avatar
Laurent Montel committed
418 419 420
    connect(horizontalScrollBar(), &QAbstractSlider::valueChanged, this, &PageView::slotRequestVisiblePixmaps);
    connect(verticalScrollBar(), &QAbstractSlider::valueChanged, this, &PageView::slotRequestVisiblePixmaps);
    connect( &d->dragScrollTimer, &QTimer::timeout, this, &PageView::slotDragScroll );
421

422
    d->leftClickTimer.setSingleShot( true );
Laurent Montel's avatar
Laurent Montel committed
423
    connect( &d->leftClickTimer, &QTimer::timeout, this, &PageView::slotShowSizeAllCursor );
424

425
    // set a corner button to resize the view to the page size
426 427 428 429
//    QPushButton * resizeButton = new QPushButton( viewport() );
//    resizeButton->setPixmap( SmallIcon("crop") );
//    setCornerWidget( resizeButton );
//    resizeButton->setEnabled( false );
430
    // connect(...);
Tobias Koenig's avatar
Tobias Koenig committed
431
    setAttribute( Qt::WA_InputMethodEnabled, true );
432

433
    // Grab pinch gestures to zoom and rotate the view
434 435
    grabGesture(Qt::PinchGesture);

Michal Humpula's avatar
Michal Humpula committed
436 437
    d->magnifierView = new MagnifierView(document, this);
    d->magnifierView->hide();
438
    d->magnifierView->setGeometry(0, 0, 351, 201); // TODO: more dynamic?
Michal Humpula's avatar
Michal Humpula committed
439

Laurent Montel's avatar
Laurent Montel committed
440 441
    connect(document, &Okular::Document::processMovieAction, this, &PageView::slotProcessMovieAction);
    connect(document, &Okular::Document::processRenditionAction, this, &PageView::slotProcessRenditionAction);
442

443
    // schedule the welcome message
444
    QMetaObject::invokeMethod(this, "slotShowWelcome", Qt::QueuedConnection);
445 446 447 448
}

PageView::~PageView()
{
449
#ifdef HAVE_SPEECH
Pino Toscano's avatar
Pino Toscano committed
450 451
    if ( d->m_tts )
        d->m_tts->stopAllSpeechs();
452
#endif
Pino Toscano's avatar
Pino Toscano committed
453

454
    delete d->mouseAnnotation;
Pino Toscano's avatar
Pino Toscano committed
455

456
    // delete the local storage structure
457

458 459
    // We need to assign it to a different list otherwise slotAnnotationWindowDestroyed
    // will bite us and clear d->m_annowindows
460
    QSet< AnnotWindow * > annowindows = d->m_annowindows;
461 462 463
    d->m_annowindows.clear();
    qDeleteAll( annowindows );

464
    // delete all widgets
465
    qDeleteAll( d->items );
466
    delete d->formsWidgetController;
Albert Astals Cid's avatar
Albert Astals Cid committed
467
    d->document->removeObserver( this );
468 469 470
    delete d;
}

471 472 473 474 475
void PageView::setupBaseActions( KActionCollection * ac )
{
    d->actionCollection = ac;

    // Zoom actions ( higher scales takes lots of memory! )
Laurent Montel's avatar
Laurent Montel committed
476 477
    d->aZoom  = new KSelectAction(QIcon::fromTheme( QStringLiteral("page-zoom") ), i18n("Zoom"), this);
    ac->addAction(QStringLiteral("zoom_to"), d->aZoom );
478
    d->aZoom->setEditable( true );
479
    d->aZoom->setMaxComboViewCount( 14 );
Laurent Montel's avatar
Laurent Montel committed
480
    connect( d->aZoom, SIGNAL(triggered(QAction*)), this, SLOT(slotZoom()) );
481 482
    updateZoomText();

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

Laurent Montel's avatar
Laurent Montel committed
485
    d->aZoomOut = KStandardAction::zoomOut( this, SLOT(slotZoomOut()), ac );
Nate Graham's avatar
Nate Graham committed
486 487 488

    d->aZoomActual = KStandardAction::actualSize( this, &PageView::slotZoomActual, ac );
    d->aZoomActual->setText(i18n("Zoom to 100%"));
489 490
}

491
void PageView::setupViewerActions( KActionCollection * ac )
492
{
493 494
    d->actionCollection = ac;

495 496
    ac->setDefaultShortcut(d->aZoomIn, QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Plus));
    ac->setDefaultShortcut(d->aZoomOut, QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Minus));
497

498
    // orientation menu actions
Laurent Montel's avatar
Laurent Montel committed
499
    d->aRotateClockwise = new QAction( QIcon::fromTheme( QStringLiteral("object-rotate-right") ), i18n( "Rotate &Right" ), this );
500
    d->aRotateClockwise->setIconText( i18nc( "Rotate right", "Right" ) );
Laurent Montel's avatar
Laurent Montel committed
501
    ac->addAction( QStringLiteral("view_orientation_rotate_cw"), d->aRotateClockwise );
502
    d->aRotateClockwise->setEnabled( false );
Laurent Montel's avatar
Laurent Montel committed
503 504
    connect( d->aRotateClockwise, &QAction::triggered, this, &PageView::slotRotateClockwise );
    d->aRotateCounterClockwise = new QAction( QIcon::fromTheme( QStringLiteral("object-rotate-left") ), i18n( "Rotate &Left" ), this );
505
    d->aRotateCounterClockwise->setIconText( i18nc( "Rotate left", "Left" ) );
Laurent Montel's avatar
Laurent Montel committed
506
    ac->addAction( QStringLiteral("view_orientation_rotate_ccw"), d->aRotateCounterClockwise );
507
    d->aRotateCounterClockwise->setEnabled( false );
Laurent Montel's avatar
Laurent Montel committed
508
    connect( d->aRotateCounterClockwise, &QAction::triggered, this, &PageView::slotRotateCounterClockwise );
Vishesh Handa's avatar
Vishesh Handa committed
509
    d->aRotateOriginal = new QAction( i18n( "Original Orientation" ), this );
Laurent Montel's avatar
Laurent Montel committed
510
    ac->addAction( QStringLiteral("view_orientation_original"), d->aRotateOriginal );
511
    d->aRotateOriginal->setEnabled( false );
Laurent Montel's avatar
Laurent Montel committed
512
    connect( d->aRotateOriginal, &QAction::triggered, this, &PageView::slotRotateOriginal );
513

Laurent Montel's avatar
Laurent Montel committed
514
    d->aPageSizes = new KSelectAction(i18n("&Page Size"), this);
Laurent Montel's avatar
Laurent Montel committed
515
    ac->addAction(QStringLiteral("view_pagesizes"), d->aPageSizes);
516
    d->aPageSizes->setEnabled( false );
517

Laurent Montel's avatar
Laurent Montel committed
518 519
    connect( d->aPageSizes , SIGNAL(triggered(int)),
         this, SLOT(slotPageSizes(int)) );
520

Jake Linder's avatar
Jake Linder committed
521 522 523
    // Trim View actions
    d->aTrimMode = new KActionMenu(i18n( "&Trim View" ), this );
    d->aTrimMode->setDelayed( false );
Laurent Montel's avatar
Laurent Montel committed
524
    ac->addAction(QStringLiteral("view_trim_mode"), d->aTrimMode );
Jake Linder's avatar
Jake Linder committed
525

526
    d->aTrimMargins  = new KToggleAction(QIcon::fromTheme( QStringLiteral("trim-margins") ), i18n( "&Trim Margins" ), d->aTrimMode->menu() );
Jake Linder's avatar
Jake Linder committed
527
    d->aTrimMode->addAction( d->aTrimMargins  );
Laurent Montel's avatar
Laurent Montel committed
528
    ac->addAction( QStringLiteral("view_trim_margins"), d->aTrimMargins  );
Jake Linder's avatar
Jake Linder committed
529
    d->aTrimMargins->setData( qVariantFromValue( (int)Okular::Settings::EnumTrimMode::Margins ) );
Laurent Montel's avatar
Laurent Montel committed
530
    connect( d->aTrimMargins, &QAction::toggled, this, &PageView::slotTrimMarginsToggled );
531
    d->aTrimMargins->setChecked( Okular::Settings::trimMargins() );
532

533
    d->aTrimToSelection  = new KToggleAction(QIcon::fromTheme( QStringLiteral("trim-to-selection") ), i18n( "Trim To &Selection" ), d->aTrimMode->menu() );
Jake Linder's avatar
Jake Linder committed
534
    d->aTrimMode->addAction( d->aTrimToSelection);
Laurent Montel's avatar
Laurent Montel committed
535
    ac->addAction( QStringLiteral("view_trim_selection"), d->aTrimToSelection);
Jake Linder's avatar
Jake Linder committed
536
    d->aTrimToSelection->setData( qVariantFromValue( (int)Okular::Settings::EnumTrimMode::Selection ) );
Laurent Montel's avatar
Laurent Montel committed
537
    connect( d->aTrimToSelection, &QAction::toggled, this, &PageView::slotTrimToSelectionToggled );
Jake Linder's avatar
Jake Linder committed
538

Laurent Montel's avatar
Laurent Montel committed
539 540 541
    d->aZoomFitWidth  = new KToggleAction(QIcon::fromTheme( QStringLiteral("zoom-fit-width") ), i18n("Fit &Width"), this);
    ac->addAction(QStringLiteral("view_fit_to_width"), d->aZoomFitWidth );
    connect( d->aZoomFitWidth, &QAction::toggled, this, &PageView::slotFitToWidthToggled );
542

Laurent Montel's avatar
Laurent Montel committed
543 544 545
    d->aZoomFitPage  = new KToggleAction(QIcon::fromTheme( QStringLiteral("zoom-fit-best") ), i18n("Fit &Page"), this);
    ac->addAction(QStringLiteral("view_fit_to_page"), d->aZoomFitPage );
    connect( d->aZoomFitPage, &QAction::toggled, this, &PageView::slotFitToPageToggled );
546

Laurent Montel's avatar
Laurent Montel committed
547 548 549
    d->aZoomAutoFit  = new KToggleAction(QIcon::fromTheme( QStringLiteral("zoom-fit-best") ), i18n("&Auto Fit"), this);
    ac->addAction(QStringLiteral("view_auto_fit"), d->aZoomAutoFit );
    connect( d->aZoomAutoFit, &QAction::toggled, this, &PageView::slotAutoFitToggled );
Thomas Fischer's avatar
Thomas Fischer committed
550

Laurent Montel's avatar
Laurent Montel committed
551
    d->aFitWindowToPage = new QAction(QIcon::fromTheme( QStringLiteral("zoom-fit-width") ), i18n("Fit Wi&ndow to Page"), this);
552
    d->aFitWindowToPage->setEnabled( Okular::Settings::viewMode() == (int)Okular::Settings::EnumViewMode::Single );
Laurent Montel's avatar
Laurent Montel committed
553
    ac->setDefaultShortcut(d->aFitWindowToPage, QKeySequence(Qt::CTRL + Qt::Key_J) );
Laurent Montel's avatar
Laurent Montel committed
554 555
    ac->addAction( QStringLiteral("fit_window_to_page"), d->aFitWindowToPage );
    connect( d->aFitWindowToPage, &QAction::triggered, this, &PageView::slotFitWindowToPage );
Thomas Fischer's avatar
Thomas Fischer committed
556

557
    // View-Layout actions
Laurent Montel's avatar
Laurent Montel committed
558
    d->aViewMode = new KActionMenu( QIcon::fromTheme( QStringLiteral("view-split-left-right") ), i18n( "&View Mode" ), this );
559 560 561
    d->aViewMode->setDelayed( false );
#define ADD_VIEWMODE_ACTION( text, name, id ) \
do { \
Vishesh Handa's avatar
Vishesh Handa committed
562
    QAction *vm = new QAction( text, this ); \
563 564 565
    vm->setCheckable( true ); \
    vm->setData( qVariantFromValue( id ) ); \
    d->aViewMode->addAction( vm ); \
566
    ac->addAction( QStringLiteral(name), vm ); \
567 568
    vmGroup->addAction( vm ); \
} while( 0 )
Laurent Montel's avatar
Laurent Montel committed
569
    ac->addAction(QStringLiteral("view_render_mode"), d->aViewMode );
Vishesh Handa's avatar
Vishesh Handa committed
570
    QActionGroup *vmGroup = new QActionGroup( this ); //d->aViewMode->menu() );
571 572
    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 );
573
    ADD_VIEWMODE_ACTION( i18n( "Facing Pages (Center First Page)" ), "view_render_mode_facing_center_first", (int)Okular::Settings::EnumViewMode::FacingFirstCentered );
574 575 576 577 578 579 580 581 582
    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
583
    connect( vmGroup, &QActionGroup::triggered, this, &PageView::slotViewMode );
584
#undef ADD_VIEWMODE_ACTION
585

Laurent Montel's avatar
Laurent Montel committed
586 587 588
    d->aViewContinuous  = new KToggleAction(QIcon::fromTheme( QStringLiteral("view-list-text") ), i18n("&Continuous"), this);
    ac->addAction(QStringLiteral("view_continuous"), d->aViewContinuous );
    connect( d->aViewContinuous, &QAction::toggled, this, &PageView::slotContinuousToggled );
589
    d->aViewContinuous->setChecked( Okular::Settings::viewContinuous() );
590

591 592 593
    // Mouse mode actions for viewer mode
    d->mouseModeActionGroup = new QActionGroup( this );
    d->mouseModeActionGroup->setExclusive( true );
594
    d->aMouseNormal  = new QAction( QIcon::fromTheme( QStringLiteral("transform-browse") ), i18n( "&Browse" ), this );
Laurent Montel's avatar
Laurent Montel committed
595 596
    ac->addAction(QStringLiteral("mouse_drag"), d->aMouseNormal );
    connect( d->aMouseNormal, &QAction::triggered, this, &PageView::slotSetMouseNormal );
Pino Toscano's avatar
Pino Toscano committed
597
    d->aMouseNormal->setCheckable( true );
598
    ac->setDefaultShortcut(d->aMouseNormal, QKeySequence(Qt::CTRL + Qt::Key_1));
599
    d->aMouseNormal->setActionGroup( d->mouseModeActionGroup );
600
    d->aMouseNormal->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::Browse );
601

Nate Graham's avatar
Nate Graham committed
602
    QAction * mz  = new QAction(QIcon::fromTheme( QStringLiteral("page-zoom") ), i18n("&Zoom"), this);
Laurent Montel's avatar
Laurent Montel committed
603 604
    ac->addAction(QStringLiteral("mouse_zoom"), mz );
    connect( mz, &QAction::triggered, this, &PageView::slotSetMouseZoom );
Pino Toscano's avatar
Pino Toscano committed
605
    mz->setCheckable( true );
606
    ac->setDefaultShortcut(mz, QKeySequence(Qt::CTRL + Qt::Key_2));
607
    mz->setActionGroup( d->mouseModeActionGroup );
608
    mz->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::Zoom );
609

Vishesh Handa's avatar
Vishesh Handa committed
610
    QAction * aToggleChangeColors  = new QAction(i18n("&Toggle Change Colors"), this);
Laurent Montel's avatar
Laurent Montel committed
611 612
    ac->addAction(QStringLiteral("toggle_change_colors"), aToggleChangeColors );
    connect( aToggleChangeColors, &QAction::triggered, this, &PageView::slotToggleChangeColors );
613 614 615 616 617 618 619
}

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

620 621
    ac->setDefaultShortcuts(d->aZoomIn, KStandardShortcut::zoomIn());
    ac->setDefaultShortcuts(d->aZoomOut, KStandardShortcut::zoomOut());
622 623

    // Mouse-Mode actions
Nate Graham's avatar
Nate Graham committed
624
    d->aMouseSelect  = new QAction(QIcon::fromTheme( QStringLiteral("select-rectangular") ), i18n("Area &Selection"), this);
Laurent Montel's avatar
Laurent Montel committed
625 626
    ac->addAction(QStringLiteral("mouse_select"), d->aMouseSelect );
    connect( d->aMouseSelect, &QAction::triggered, this, &PageView::slotSetMouseSelect );
Pino Toscano's avatar
Pino Toscano committed
627
    d->aMouseSelect->setCheckable( true );
628 629
    ac->setDefaultShortcut(d->aMouseSelect, Qt::CTRL + Qt::Key_3);

630
    d->aMouseSelect->setActionGroup( d->mouseModeActionGroup );
631
    d->aMouseSelect->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::RectSelect );
632

633
    d->aMouseTextSelect  = new QAction(QIcon::fromTheme( QStringLiteral("edit-select-text") ), i18n("&Text Selection"), this);
Laurent Montel's avatar
Laurent Montel committed
634 635
    ac->addAction(QStringLiteral("mouse_textselect"), d->aMouseTextSelect );
    connect( d->aMouseTextSelect, &QAction::triggered, this, &PageView::slotSetMouseTextSelect );
636
    d->aMouseTextSelect->setCheckable( true );
637
    ac->setDefaultShortcut(d->aMouseTextSelect, Qt::CTRL + Qt::Key_4);
638
    d->aMouseTextSelect->setActionGroup( d->mouseModeActionGroup );
639
    d->aMouseTextSelect->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::TextSelect );
640

Nate Graham's avatar
Nate Graham committed
641
    d->aMouseTableSelect  = new QAction(QIcon::fromTheme( QStringLiteral("table") ), i18n("T&able Selection"), this);
Laurent Montel's avatar
Laurent Montel committed
642 643
    ac->addAction(QStringLiteral("mouse_tableselect"), d->aMouseTableSelect );
    connect( d->aMouseTableSelect, &QAction::triggered, this, &PageView::slotSetMouseTableSelect );
Jiri Baum's avatar
Jiri Baum committed
644
    d->aMouseTableSelect->setCheckable( true );
645
    ac->setDefaultShortcut(d->aMouseTableSelect, Qt::CTRL + Qt::Key_5);
646
    d->aMouseTableSelect->setActionGroup( d->mouseModeActionGroup );
647
    d->aMouseTableSelect->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::TableSelect );
Jiri Baum's avatar
Jiri Baum committed
648

Laurent Montel's avatar
Laurent Montel committed
649 650 651
    d->aMouseMagnifier = new QAction(QIcon::fromTheme( QStringLiteral("document-preview") ), i18n("&Magnifier"), this);
    ac->addAction(QStringLiteral("mouse_magnifier"), d->aMouseMagnifier );
    connect( d->aMouseMagnifier, &QAction::triggered, this, &PageView::slotSetMouseMagnifier );
Michal Humpula's avatar
Michal Humpula committed
652
    d->aMouseMagnifier->setCheckable( true );
653
    ac->setDefaultShortcut(d->aMouseMagnifier, Qt::CTRL + Qt::Key_6);
Michal Humpula's avatar
Michal Humpula committed
654 655 656
    d->aMouseMagnifier->setActionGroup( d->mouseModeActionGroup );
    d->aMouseMagnifier->setChecked( Okular::Settings::mouseMode() == Okular::Settings::EnumMouseMode::Magnifier );

Laurent Montel's avatar
Laurent Montel committed
657 658
    d->aToggleAnnotator  = new KToggleAction(QIcon::fromTheme( QStringLiteral("draw-freehand") ), i18n("&Review"), this);
    ac->addAction(QStringLiteral("mouse_toggle_annotate"), d->aToggleAnnotator );
Pino Toscano's avatar
Pino Toscano committed
659
    d->aToggleAnnotator->setCheckable( true );
Laurent Montel's avatar
Laurent Montel committed
660
    connect( d->aToggleAnnotator, &QAction::toggled, this, &PageView::slotToggleAnnotator );
661
    ac->setDefaultShortcut(d->aToggleAnnotator, Qt::Key_F6);
662