part.h 14.6 KB
Newer Older
1 2 3 4 5 6 7
/***************************************************************************
 *   Copyright (C) 2002 by Wilco Greven <greven@kde.org>                   *
 *   Copyright (C) 2003-2004 by Christophe Devriese                        *
 *                         <Christophe.Devriese@student.kuleuven.ac.be>    *
 *   Copyright (C) 2003 by Andy Goossens <andygoossens@telenet.be>         *
 *   Copyright (C) 2003 by Laurent Montel <montel@kde.org>                 *
 *   Copyright (C) 2004 by Dominique Devriese <devriese@kde.org>           *
Albert Astals Cid's avatar
Albert Astals Cid committed
8
 *   Copyright (C) 2004-2007 by Albert Astals Cid <aacid@kde.org>          *
9 10 11
 *   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             *
12 13 14 15 16 17 18
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

19 20
#ifndef _PART_H_
#define _PART_H_
21

22 23 24 25 26
#include <QIcon>
#include <QList>
#include <QPointer>
#include <QProcess>
#include <QtDBus>
Lukáš Tinkl's avatar
Lukáš Tinkl committed
27
#include <QUrl>
28 29 30 31 32 33 34

#include <KCompressionDevice>
#include <KIO/Job>
#include <KMessageWidget>
#include <KParts/ReadWritePart>
#include <KPluginFactory>

35
#include "core/observer.h"
36
#include "core/document.h"
37
#include "kdocumentviewer.h"
38
#include "interfaces/viewerinterface.h"
39

40
#include "okularpart_export.h"
41

Kevin Ottens's avatar
Kevin Ottens committed
42 43
#include <config-okular.h>

44
class QAction;
45
class QWidget;
46
class QPrinter;
47
class QMenu;
48

49
class KConfigDialog;
Tobias Koenig's avatar
Tobias Koenig committed
50
class KConfigGroup;
Albert Astals Cid's avatar
Albert Astals Cid committed
51
class KDirWatch;
52
class KToggleAction;
53
class KToggleFullScreenAction;
54
class KSelectAction;
55
class KAboutData;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
56
class QTemporaryFile;
Vishesh Handa's avatar
Vishesh Handa committed
57
class QAction;
Kevin Ottens's avatar
Kevin Ottens committed
58
class QJsonObject;
59
namespace KParts { class GUIActivateEvent; }
60

61
class FindBar;
Albert Astals Cid's avatar
Albert Astals Cid committed
62
class ThumbnailList;
Albert Astals Cid's avatar
Albert Astals Cid committed
63
class PageSizeLabel;
64
class PageView;
65
class PresentationWidget;
66
class ProgressWidget;
67
class SearchWidget;
Pino Toscano's avatar
Pino Toscano committed
68
class Sidebar;
Albert Astals Cid's avatar
Albert Astals Cid committed
69 70
class TOC;
class MiniBar;
Albert Astals Cid's avatar
Albert Astals Cid committed
71
class MiniBarLogic;
72
class FileKeeper;
73 74
class Reviews;
class BookmarkList;
75
class DrawingToolActions;
76
class Layers;
77

Kevin Ottens's avatar
Kevin Ottens committed
78 79 80 81
#if PURPOSE_FOUND
namespace Purpose { class Menu; }
#endif

82 83
namespace Okular
{
84 85

class BrowserExtension;
86
class ExportFormat;
87

88 89 90 91 92 93 94 95 96 97 98
/**
 * Describes the possible embedding modes of the part
 *
 * @since 0.14 (KDE 4.8)
 */
enum EmbedMode
{
    UnknownEmbedMode,
    NativeShellMode,         // embedded in the native Okular' shell
    PrintPreviewMode,        // embedded to show the print preview of a document
    KHTMLPartMode,           // embedded in KHTML
Laurent Montel's avatar
Laurent Montel committed
99
    ViewerWidgetMode         // the part acts as a widget that can display all kinds of documents
100 101
};

102 103 104 105 106 107 108 109
/**
 * This is a "Part".  It that does all the real work in a KPart
 * application.
 *
 * @short Main Part
 * @author Wilco Greven <greven@kde.org>
 * @version 0.2
 */
110
class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::DocumentObserver, public KDocumentViewer, public Okular::ViewerInterface
111
{
112
    Q_OBJECT
Pino Toscano's avatar
Pino Toscano committed
113 114
    Q_CLASSINFO("D-Bus Interface", "org.kde.okular")
    Q_INTERFACES(KDocumentViewer)
115
    Q_INTERFACES(Okular::ViewerInterface)
116

117
    friend class PartTest;
118

Pino Toscano's avatar
Pino Toscano committed
119
    public:
120
        // Default constructor
121 122 123 124 125 126
        /**
         * If one element of 'args' contains one of the strings "Print/Preview" or "ViewerWidget",
         * the part will be set up in the corresponding mode. Additionally, it is possible to specify
         * which config file should be used by adding a string containing "ConfigFileName=<file name>"
         * to 'args'.
         **/
127
        Part(QWidget* parentWidget, QObject* parent, const QVariantList& args);
128 129 130 131 132

        // Destructor
        ~Part();

        // inherited from DocumentObserver
133 134 135
        void notifySetup( const QVector< Okular::Page * > &pages, int setupFlags ) override;
        void notifyViewportChanged( bool smoothMove ) override;
        void notifyPageChanged( int page, int flags ) override;
136

137 138 139
        bool openDocument(const QUrl &url, uint page) override;
        void startPresentation() override;
        QStringList supportedMimeTypes() const override;
140

Lukáš Tinkl's avatar
Lukáš Tinkl committed
141
        QUrl realUrl() const;
142

143 144 145 146 147 148 149
        void showSourceLocation(const QString& fileName, int line, int column, bool showGraphically = true) override;
        void clearLastShownSourceLocation() override;
        bool isWatchFileModeEnabled() const override;
        void setWatchFileModeEnabled(bool enable) override;
        bool areSourceLocationsShownGraphically() const override;
        void setShowSourceLocationsGraphically(bool show) override;
        bool openNewFilesInTabs() const override;
150

151
    public Q_SLOTS:                // dbus
152
        Q_SCRIPTABLE Q_NOREPLY void goToPage(uint page) override;
153
        Q_SCRIPTABLE Q_NOREPLY void openDocument( const QString &doc );
154 155
        Q_SCRIPTABLE uint pages();
        Q_SCRIPTABLE uint currentPage();
156
        Q_SCRIPTABLE QString currentDocument();
157
        Q_SCRIPTABLE QString documentMetaData( const QString &metaData ) const;
158 159 160 161 162 163 164
        Q_SCRIPTABLE void slotPreferences();
        Q_SCRIPTABLE void slotFind();
        Q_SCRIPTABLE void slotPrintPreview();
        Q_SCRIPTABLE void slotPreviousPage();
        Q_SCRIPTABLE void slotNextPage();
        Q_SCRIPTABLE void slotGotoFirst();
        Q_SCRIPTABLE void slotGotoLast();
165
        Q_SCRIPTABLE void slotTogglePresentation();
166
        Q_SCRIPTABLE Q_NOREPLY void reload();
167
        Q_SCRIPTABLE Q_NOREPLY void enableStartWithPrint();
168

169
    Q_SIGNALS:
170
        void enablePrintAction(bool enable);
171
        void openSourceReference(const QString& absFileName, int line, int column);
172
        void viewerMenuStateChange(bool enabled);
173
        void enableCloseAction(bool enable);
Frederik Gladhorn's avatar
Frederik Gladhorn committed
174
        void mimeTypeChanged(QMimeType mimeType);
Vishesh Handa's avatar
Vishesh Handa committed
175
        void urlsDropped( const QList<QUrl>& urls );
176
        void fitWindowToPage( const QSize& pageViewPortSize, const QSize& pageSize );
177 178

    protected:
179
        // reimplemented from KParts::ReadWritePart
180 181 182
        bool openFile() override;
        bool openUrl(const QUrl &url) override;
        void guiActivateEvent(KParts::GUIActivateEvent *event) override;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
183
        void displayInfoMessage( const QString &message, KMessageWidget::MessageType messageType = KMessageWidget::Information, int duration = -1 );
184
    public:
185 186 187 188 189
        bool queryClose() override;
        bool closeUrl() override;
        bool closeUrl(bool promptToSave) override;
        void setReadWrite(bool readwrite) override;
        bool saveAs(const QUrl & saveUrl) override;
190

191
    protected Q_SLOTS:
192
        // connected to actions
Lukáš Tinkl's avatar
Lukáš Tinkl committed
193 194
        void openUrlFromDocument(const QUrl &url);
        void openUrlFromBookmarks(const QUrl &url);
Vishesh Handa's avatar
Vishesh Handa committed
195
        void handleDroppedUrls( const QList<QUrl>& urls );
196 197 198
        void slotGoToPage();
        void slotHistoryBack();
        void slotHistoryNext();
199
        void slotAddBookmark();
200
        void slotRenameBookmarkFromMenu();
201
        void slotRemoveBookmarkFromMenu();
202
        void slotRenameCurrentViewportBookmark();
203 204 205
        void slotPreviousBookmark();
        void slotNextBookmark();
        void slotFindNext();
Pino Toscano's avatar
Pino Toscano committed
206
        void slotFindPrev();
207
        bool slotSaveFileAs(bool showOkularArchiveAsDefaultFormat = false);
208 209 210
        void slotGetNewStuff();
        void slotNewConfig();
        void slotShowMenu(const Okular::Page *page, const QPoint &point);
Albert Astals Cid's avatar
Albert Astals Cid committed
211
        void slotShowTOCMenu(const Okular::DocumentViewport &vp, const QPoint &point, const QString &title);
212 213 214
        void slotShowProperties();
        void slotShowEmbeddedFiles();
        void slotShowLeftPanel();
215
        void slotShowBottomBar();
216 217 218 219
        void slotShowPresentation();
        void slotHidePresentation();
        void slotExportAs(QAction *);
        bool slotImportPSFile();
220
        void slotAboutBackend();
221
        void slotReload();
222 223
        void close();
        void cannotQuit();
224 225
        void slotShowFindBar();
        void slotHideFindBar();
226
        void slotJobStarted(KIO::Job *job);
227
        void slotJobFinished(KJob *job);
228 229
        void loadCancelled(const QString &reason);
        void setWindowTitleFromDocument();
230 231
        // can be connected to widget elements
        void updateViewActions();
232
        void updateBookmarksActions();
233
        void enableTOC(bool enable);
234
        void slotRebuildBookmarkMenu();
235
        void enableLayers( bool enable );
236

237
    public Q_SLOTS:
238
        bool saveFile() override;
239 240 241
        // connected to Shell action (and browserExtension), not local one
        void slotPrint();
        void slotFileDirty( const QString& );
242
        bool slotAttemptReload( bool oneShot = false, const QUrl &newUrl = QUrl() );
243
        void psTransformEnded(int, QProcess::ExitStatus);
244
        KConfigDialog * slotGeneratorPreferences();
245

246 247 248 249
        void errorMessage( const QString &message, int duration = 0 );
        void warningMessage( const QString &message, int duration = -1 );
        void noticeMessage( const QString &message, int duration = -1 );

250 251
        void moveSplitter( const int sideWidgetSize );

252
    private:
253
        bool aboutToShowContextMenu(QMenu *menu, QAction *action, QMenu *contextMenu);
254
        void showMenu(const Okular::Page *page, const QPoint &point, const QString &bookmarkTitle = QString(), const Okular::DocumentViewport &vp = DocumentViewport());
255
        bool eventFilter(QObject * watched, QEvent * event) override;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
256
        Document::OpenResult doOpenFile(const QMimeType &mime, const QString &fileNameToOpen, bool *isCompressedFile);
257
        bool openUrl( const QUrl &url, bool swapInsteadOfOpening );
258

259
        void setupViewerActions();
260
        void setViewerShortcuts();
261 262
        void setupActions();

John Layt's avatar
John Layt committed
263 264
        void setupPrint( QPrinter &printer );
        void doPrint( QPrinter &printer );
265
        bool handleCompressed(QString &destpath, const QString &path, KCompressionDevice::CompressionType compressionType );
266
        void rebuildBookmarkMenu( bool unplugActions = true );
267
        void updateAboutBackendAction();
Albert Astals Cid's avatar
Albert Astals Cid committed
268
        void unsetDummyMode();
269
        void slotRenameBookmark( const DocumentViewport &viewport );
270
        void slotRemoveBookmark( const DocumentViewport &viewport );
271
        void resetStartArguments();
272
        void checkNativeSaveDataLoss(bool *out_wontSaveForms, bool *out_wontSaveAnnotations) const;
273 274 275 276

        enum SaveAsFlag
        {
            NoSaveAsFlags = 0,         ///< No options
277
            SaveAsOkularArchive = 1    ///< Save as Okular Archive (.okular) instead of document's native format
278 279 280
        };
        Q_DECLARE_FLAGS( SaveAsFlags, SaveAsFlag )

281
        bool saveAs( const QUrl & saveUrl, SaveAsFlags flags );
282

283 284 285
        void setFileToWatch( const QString &filePath );
        void unsetFileToWatch();

Kevin Ottens's avatar
Kevin Ottens committed
286 287 288 289
#if PURPOSE_FOUND
        void slotShareActionFinished(const QJsonObject &output, int error, const QString &message);
#endif

290
        static int numberOfParts;
Albert Astals Cid's avatar
Albert Astals Cid committed
291

Frederik Gladhorn's avatar
Frederik Gladhorn committed
292
        QTemporaryFile *m_tempfile;
293 294 295 296

        // the document
        Okular::Document * m_document;
        QString m_temporaryLocalFile;
297
        bool isDocumentArchive;
298
        bool m_documentOpenWithPassword;
299
        bool m_swapInsteadOfOpening; // if set, the next open operation will replace the backing file (used when reloading just saved files)
300 301

        // main widgets
Pino Toscano's avatar
Pino Toscano committed
302
        Sidebar *m_sidebar;
303
        SearchWidget *m_searchWidget;
304
        FindBar * m_findBar;
305
        KMessageWidget * m_migrationMessage;
306 307
        KMessageWidget * m_topMessage;
        KMessageWidget * m_formsMessage;
308
        KMessageWidget * m_infoMessage;
309 310 311
        QPointer<ThumbnailList> m_thumbnailList;
        QPointer<PageView> m_pageView;
        QPointer<TOC> m_toc;
Albert Astals Cid's avatar
Albert Astals Cid committed
312
        QPointer<MiniBarLogic> m_miniBarLogic;
313
        QPointer<MiniBar> m_miniBar;
314
        QPointer<MiniBar> m_pageNumberTool;
315
        QPointer<QWidget> m_bottomBar;
316 317 318
        QPointer<PresentationWidget> m_presentationWidget;
        QPointer<ProgressWidget> m_progressWidget;
        QPointer<PageSizeLabel> m_pageSizeLabel;
319 320
        QPointer<Reviews> m_reviewsWidget;
        QPointer<BookmarkList> m_bookmarkList;
321
        QPointer<Layers> m_layers;
322 323 324

        // document watcher (and reloader) variables
        KDirWatch *m_watcher;
325
        QString m_watchedFilePath, m_watchedFileSymlinkTarget;
326
        QTimer *m_dirtyHandler;
Lukáš Tinkl's avatar
Lukáš Tinkl committed
327
        QUrl m_oldUrl;
328
        Okular::DocumentViewport m_viewportDirty;
329
        bool m_isReloading;
330
        bool m_wasPresentationOpen;
331
        QWidget *m_dirtyToolboxItem;
332
        bool m_wasSidebarVisible;
333
        bool m_wasSidebarCollapsed;
334
        bool m_fileWasRemoved;
335
        Rotation m_dirtyPageRotation;
336 337 338 339 340

        // Remember the search history
        QStringList m_searchHistory;

        // actions
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        QAction *m_gotoPage;
        QAction *m_prevPage;
        QAction *m_nextPage;
        QAction *m_beginningOfDocument;
        QAction *m_endOfDocument;
        QAction *m_historyBack;
        QAction *m_historyNext;
        QAction *m_addBookmark;
        QAction *m_renameBookmark;
        QAction *m_prevBookmark;
        QAction *m_nextBookmark;
        QAction *m_copy;
        QAction *m_selectAll;
        QAction *m_find;
        QAction *m_findNext;
        QAction *m_findPrev;
357
        QAction *m_save;
358 359 360 361 362 363
        QAction *m_saveAs;
        QAction *m_saveCopyAs;
        QAction *m_printPreview;
        QAction *m_showProperties;
        QAction *m_showEmbeddedFiles;
        QAction *m_exportAs;
364
        QAction *m_exportAsText;
365
        QAction *m_exportAsDocArchive;
Kevin Ottens's avatar
Kevin Ottens committed
366 367 368
#if PURPOSE_FOUND
        QAction *m_share;
#endif
369
        QAction *m_showPresentation;
370 371
        KToggleAction* m_showMenuBarAction;
        KToggleAction* m_showLeftPanel;
372
        KToggleAction* m_showBottomBar;
373
        KToggleFullScreenAction* m_showFullScreenAction;
374 375
        QAction *m_aboutBackend;
        QAction *m_reload;
376
        QMenu *m_exportAsMenu;
Kevin Ottens's avatar
Kevin Ottens committed
377 378 379
#if PURPOSE_FOUND
        Purpose::Menu *m_shareMenu;
#endif
380
        QAction *m_closeFindBar;
381
        DrawingToolActions *m_presentationDrawingActions;
382 383 384 385 386

        bool m_actionsSearched;
        BrowserExtension *m_bExtension;

        QList<Okular::ExportFormat> m_exportFormats;
387
        QList<QAction*> m_bookmarkActions;
388
        bool m_cliPresentation;
389
        bool m_cliPrint;
390
        QString m_addBookmarkText;
391
        QIcon m_addBookmarkIcon;
392

393
        EmbedMode m_embedMode;
394

Lukáš Tinkl's avatar
Lukáš Tinkl committed
395
        QUrl m_realUrl;
396

397
        KXMLGUIClient *m_generatorGuiClient;
398
        FileKeeper *m_keeper;
399

400 401 402
        // Timer for m_infoMessage
        QTimer *m_infoTimer;

Albert Astals Cid's avatar
Albert Astals Cid committed
403 404
        QString m_registerDbusName;

405
    private Q_SLOTS:
406
        void slotAnnotationPreferences();
Michel Ludwig's avatar
Michel Ludwig committed
407
        void slotHandleActivatedSourceReference(const QString& absFileName, int line, int col, bool *handled);
408
};
Wilco Greven's avatar
Wilco Greven committed
409

410 411
}

412 413
#endif

414
/* kate: replace-tabs on; indent-width 4; */