compile
[kdegraphics.git] / okular / core / document.h
blob3f3d1a2aff2b403a44ac5a48f80dc13bdee0ad4a
1 /***************************************************************************
2 * Copyright (C) 2004-2005 by Enrico Ros <eros.kde@email.it> *
3 * Copyright (C) 2004-2008 by Albert Astals Cid <aacid@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 #ifndef _OKULAR_DOCUMENT_H_
12 #define _OKULAR_DOCUMENT_H_
14 #include <okular/core/okular_export.h>
15 #include <okular/core/area.h>
16 #include <okular/core/global.h>
17 #include <okular/core/pagesize.h>
19 #include <QtCore/QObject>
20 #include <QtCore/QStringList>
21 #include <QtCore/QVector>
22 #include <QtXml/QDomDocument>
24 #include <kmimetype.h>
26 class QPrinter;
27 class QPrintDialog;
28 class KComponentData;
29 class KBookmark;
30 class KConfigDialog;
31 class KXMLGUIClient;
32 class KUrl;
34 namespace Okular {
36 class Annotation;
37 class BookmarkManager;
38 class DocumentInfo;
39 class DocumentObserver;
40 class DocumentPrivate;
41 class DocumentSynopsis;
42 class DocumentViewport;
43 class EmbeddedFile;
44 class ExportFormat;
45 class FontInfo;
46 class Generator;
47 class Action;
48 class Page;
49 class PixmapRequest;
50 class SourceReference;
51 class View;
52 class VisiblePageRect;
54 /** IDs for seaches. Globally defined here. **/
55 #define PART_SEARCH_ID 1
56 #define PAGEVIEW_SEARCH_ID 2
57 #define SW_SEARCH_ID 3
58 #define PRESENTATION_SEARCH_ID 4
61 /**
62 * @short The Document. Heart of everything. Actions take place here.
64 * The Document is the main object in Okular. All views query the Document to
65 * get data/properties or even for accessing pages (in a 'const' way).
67 * It is designed to keep it detached from the document type (pdf, ps, you
68 * name it..) so whenever you want to get some data, it asks its internals
69 * generator to do the job and return results in a format-indepedent way.
71 * Apart from the generator (the currently running one) the document stores
72 * all the Pages ('Page' class) of the current document in a vector and
73 * notifies all the registered DocumentObservers when some content changes.
75 * For a better understanding of hieracies @see README.internals.png
76 * @see DocumentObserver, Page
78 class OKULAR_EXPORT Document : public QObject
80 Q_OBJECT
82 public:
83 /**
84 * Creates a new document with the given @p widget as widget to relay GUI things (messageboxes, ...).
86 explicit Document( QWidget *widget );
88 /**
89 * Destroys the document.
91 ~Document();
93 /**
94 * Opens the document.
96 bool openDocument( const QString & docFile, const KUrl & url, const KMimeType::Ptr &mime );
98 /**
99 * Closes the document.
101 void closeDocument();
104 * Registers a new @p observer for the document.
106 void addObserver( DocumentObserver *observer );
109 * Unregisters the given @p observer for the document.
111 void removeObserver( DocumentObserver *observer );
114 * Reparses and applies the configuration.
116 void reparseConfig();
119 * Returns the widget to be used for relaying GUI things (messageboxes, ...)
121 QWidget *widget() const;
124 * Returns whether the document is currently opened.
126 bool isOpened() const;
129 * Returns the meta data of the document or 0 if no meta data
130 * are available.
132 const DocumentInfo * documentInfo() const;
135 * Returns the table of content of the document or 0 if no
136 * table of content is available.
138 const DocumentSynopsis * documentSynopsis() const;
141 * Starts the reading of the information about the fonts in the
142 * document, if available.
144 * The results as well the end of the reading is notified using the
145 * signals gotFont(), fontReadingProgress() and fontReadingEnded()
147 void startFontReading();
150 * Force the termination of the reading of the information about the
151 * fonts in the document, if running.
153 void stopFontReading();
156 * Whether the current document can provide information about the
157 * fonts used in it.
159 bool canProvideFontInformation() const;
162 * Returns the list of embedded files or 0 if no embedded files
163 * are available.
165 const QList<EmbeddedFile*> *embeddedFiles() const;
168 * Returns the page object for the given page @p number or 0
169 * if the number is out of range.
171 const Page * page( int number ) const;
174 * Returns the current viewport of the document.
176 const DocumentViewport & viewport() const;
179 * Sets the list of visible page rectangles.
180 * @see VisiblePageRect
182 void setVisiblePageRects( const QVector< VisiblePageRect * > & visiblePageRects, int excludeId = -1 );
185 * Returns the list of visible page rectangles.
187 const QVector< VisiblePageRect * > & visiblePageRects() const;
190 * Returns the number of the current page.
192 uint currentPage() const;
195 * Returns the number of pages of the document.
197 uint pages() const;
200 * Returns the url of the currently opened document.
202 KUrl currentDocument() const;
205 * Returns whether the given @p action is allowed in the document.
206 * @see @ref Permission
208 bool isAllowed( Permission action ) const;
211 * Returns whether the document supports searching.
213 bool supportsSearching() const;
216 * Returns whether the document supports the listing of page sizes.
218 bool supportsPageSizes() const;
221 * Returns the list of supported page sizes or an empty list if this
222 * feature is not available.
223 * @see supportsPageSizes()
225 PageSize::List pageSizes() const;
228 * Returns whether the document supports the export to ASCII text.
230 bool canExportToText() const;
233 * Exports the document as ASCII text and saves it under @p fileName.
235 bool exportToText( const QString& fileName ) const;
238 * Returns the list of supported export formats.
239 * @see ExportFormat
241 QList<ExportFormat> exportFormats() const;
244 * Exports the document in the given @p format and saves it under @p fileName.
246 bool exportTo( const QString& fileName, const ExportFormat& format ) const;
249 * Returns whether the document history is at the begin.
251 bool historyAtBegin() const;
254 * Returns whether the document history is at the end.
256 bool historyAtEnd() const;
259 * Returns the meta data for the given @p key and @p option or an empty variant
260 * if the key doesn't exists.
262 QVariant metaData( const QString & key, const QVariant & option = QVariant() ) const;
265 * Returns the current rotation of the document.
267 Rotation rotation() const;
270 * If all pages have the same size this method returns it, if the page sizes
271 * differ an empty size object is returned.
273 QSizeF allPagesSize() const;
276 * Returns the size string for the given @p page or an empty string
277 * if the page is out of range.
279 QString pageSizeString( int page ) const;
282 * Returns the gui client of the generator, if it provides one.
284 KXMLGUIClient* guiClient();
287 * Sets the current document viewport to the given @p page.
289 * @param excludeId The observer ids which shouldn't be effected by this change.
290 * @param smoothMove Whether the move shall be animated smoothly.
292 void setViewportPage( int page, int excludeId = -1, bool smoothMove = false );
295 * Sets the current document viewport to the given @p viewport.
297 * @param excludeId The observer ids which shouldn't be effected by this change.
298 * @param smoothMove Whether the move shall be animated smoothly.
300 void setViewport( const DocumentViewport &viewport, int excludeId = -1, bool smoothMove = false );
303 * Sets the current document viewport to the next viewport in the
304 * viewport history.
306 void setPrevViewport();
309 * Sets the current document viewport to the previous viewport in the
310 * viewport history.
312 void setNextViewport();
315 * Sets the next @p viewport in the viewport history.
317 void setNextDocumentViewport( const DocumentViewport &viewport );
320 * Sets the zoom for the current document.
322 * @param excludeId The observer ids which shouldn't be effected by this change.
324 void setZoom( int factor, int excludeId = -1 );
327 * Describes the possible options for the pixmap requests.
329 enum PixmapRequestFlag
331 NoOption = 0, ///< No options
332 RemoveAllPrevious = 1 ///< Remove all the previous requests, even for non requested page pixmaps
334 Q_DECLARE_FLAGS( PixmapRequestFlags, PixmapRequestFlag )
337 * Sends @p requests for pixmap generation.
339 * The same as requestPixmaps( requests, RemoveAllPrevious );
341 void requestPixmaps( const QLinkedList<PixmapRequest*> &requests );
344 * Sends @p requests for pixmap generation.
346 * @param reqOptions the options for the request
348 * @since 0.7 (KDE 4.1)
350 void requestPixmaps( const QLinkedList<PixmapRequest*> &requests, PixmapRequestFlags reqOptions );
353 * Sends a request for text page generation for the given page @p number.
355 void requestTextPage( uint number );
358 * Adds a new @p annotation to the given @p page.
360 void addPageAnnotation( int page, Annotation *annotation );
363 * Modifies the given @p annotation on the given @p page.
365 void modifyPageAnnotation( int page, Annotation *annotation );
368 * Removes the given @p annotation from the given @p page.
370 void removePageAnnotation( int page, Annotation *annotation );
373 * Removes the given @p annotations from the given @p page.
375 void removePageAnnotations( int page, const QList<Annotation*> &annotations );
378 * Sets the text selection for the given @p page.
380 * @param rect The rectangle of the selection.
381 * @param color The color of the selection.
383 void setPageTextSelection( int page, RegularAreaRect * rect, const QColor & color );
386 * Describes the possible search types.
388 enum SearchType
390 NextMatch, ///< Search next match
391 PreviousMatch, ///< Search previous match
392 AllDocument, ///< Search complete document
393 GoogleAll, ///< Search all words in google style
394 GoogleAny ///< Search any words in google style
398 * Describes how search ended
400 enum SearchStatus
402 MatchFound, ///< Any match was found
403 NoMatchFound, ///< No match was found
404 SearchCancelled ///< The search was cancelled
408 * Searches the given @p text in the document.
410 * @param searchID The unique id for this search request.
411 * @param fromStart Whether the search should be started at begin of the document.
412 * @param caseSensitivity Whether the search is case sensitive.
413 * @param type The type of the search. @ref SearchType
414 * @param moveViewport Whether the viewport shall be moved to the position of the matches.
415 * @param color The highlighting color of the matches.
416 * @param noDialogs Whether a search dialog shall be shown.
418 void searchText( int searchID, const QString & text, bool fromStart, Qt::CaseSensitivity caseSensitivity,
419 SearchType type, bool moveViewport, const QColor & color, bool noDialogs = false );
422 * Continues the search for the given @p searchID.
424 void continueSearch( int searchID );
427 * Continues the search for the given @p searchID, optionally specifying
428 * a new direction for the search.
430 * @since 0.7 (KDE 4.1)
432 void continueSearch( int searchID, SearchType type );
435 * Resets the search for the given @p searchID.
437 void resetSearch( int searchID );
440 * Returns the bookmark manager of the document.
442 BookmarkManager * bookmarkManager() const;
445 * Processes the given @p action.
447 void processAction( const Action *action );
450 * Returns a list of the bookmarked.pages
452 QList<int> bookmarkedPageList() const;
455 * Returns the range of the bookmarked.pages
457 QString bookmarkedPageRange() const;
460 * Processes/Executes the given source @p reference.
462 void processSourceReference( const SourceReference *reference );
465 * Returns whether the document can configure the printer itself.
467 bool canConfigurePrinter() const;
470 * What type of printing a document supports
472 enum PrintingType
474 NoPrinting, ///< Printing Not Supported
475 NativePrinting, ///< Native Cross-Platform Printing
476 PostscriptPrinting ///< Postscript file printing
480 * Returns what sort of printing the document supports:
481 * Native, Postscript, None
483 PrintingType printingSupport() const;
486 * Returns whether the document supports printing to both PDF and PS files.
488 bool supportsPrintToFile() const;
491 * Prints the document to the given @p printer.
493 bool print( QPrinter &printer );
496 * Returns a custom printer configuration page or 0 if no
497 * custom printer configuration page is available.
499 QWidget* printConfigurationWidget() const;
502 * Fill the KConfigDialog @p dialog with the setting pages of the
503 * generators.
505 void fillConfigDialog( KConfigDialog * dialog );
508 * Returns the number of generators that have a configuration widget.
510 int configurableGenerators() const;
513 * Returns the list with the supported MIME types.
515 QStringList supportedMimeTypes() const;
518 * Returns the component data associated with the generator. May be null.
520 const KComponentData* componentData() const;
523 * Returns whether the changes to the document (modified annotations,
524 * values in form fields, etc) can be saved to another document.
526 * @since 0.7 (KDE 4.1)
528 bool canSaveChanges() const;
531 * Save the document and the optional changes to it to the specified
532 * @p fileName.
534 * @since 0.7 (KDE 4.1)
536 bool saveChanges( const QString &fileName );
539 * Register the specified @p view for the current document.
541 * It is unregistered from the previous document, if any.
543 * @since 0.7 (KDE 4.1)
545 void registerView( View *view );
548 * Unregister the specified @p view from the current document.
550 * @since 0.7 (KDE 4.1)
552 void unregisterView( View *view );
555 * Gets the font data for the given font
557 * @since 0.8 (KDE 4.2)
559 QByteArray fontData(const FontInfo &font) const;
562 * Opens a document archive.
564 * @since 0.8 (KDE 4.2)
566 bool openDocumentArchive( const QString & docFile, const KUrl & url );
569 * Saves a document archive.
571 * @since 0.8 (KDE 4.2)
573 bool saveDocumentArchive( const QString &fileName );
575 public Q_SLOTS:
577 * This slot is called whenever the user changes the @p rotation of
578 * the document.
580 void setRotation( int rotation );
583 * This slot is called whenever the user changes the page @p size
584 * of the document.
586 void setPageSize( const PageSize &size );
589 * Cancels the current search
591 void cancelSearch();
594 Q_SIGNALS:
596 * This signal is emitted whenever an action requests a
597 * document close operation.
599 void close();
602 * This signal is emitted whenever an action requests an
603 * application quit operation.
605 void quit();
608 * This signal is emitted whenever an action requests a
609 * find operation.
611 void linkFind();
614 * This signal is emitted whenever an action requests a
615 * goto operation.
617 void linkGoToPage();
620 * This signal is emitted whenever an action requests a
621 * start presentation operation.
623 void linkPresentation();
626 * This signal is emitted whenever an action requests an
627 * end presentation operation.
629 void linkEndPresentation();
632 * This signal is emitted whenever an action requests an
633 * open url operation for the given document @p url.
635 void openUrl( const KUrl &url );
638 * This signal is emitted whenever an error occurred.
640 * @param text The description of the error.
641 * @param duration The time in seconds the message should be shown to the user.
643 void error( const QString &text, int duration );
646 * This signal is emitted to signal a warning.
648 * @param text The description of the warning.
649 * @param duration The time in seconds the message should be shown to the user.
651 void warning( const QString &text, int duration );
654 * This signal is emitted to signal a notice.
656 * @param text The description of the notice.
657 * @param duration The time in seconds the message should be shown to the user.
659 void notice( const QString &text, int duration );
662 * Emitted when a new font is found during the reading of the fonts of
663 * the document.
665 void gotFont( const Okular::FontInfo& font );
668 * Reports the progress when reading the fonts in the document.
670 * \param page is the page that was just finished to scan for fonts
672 void fontReadingProgress( int page );
675 * Reports that the reading of the fonts in the document is finished.
677 void fontReadingEnded();
680 * Reports that the current search finished
682 void searchFinished( int id, Okular::Document::SearchStatus endStatus );
684 private:
685 /// @cond PRIVATE
686 friend class DocumentPrivate;
687 /// @endcond
688 DocumentPrivate *const d;
690 Q_DISABLE_COPY( Document )
692 Q_PRIVATE_SLOT( d, void saveDocumentInfo() const )
693 Q_PRIVATE_SLOT( d, void slotTimedMemoryCheck() )
694 Q_PRIVATE_SLOT( d, void sendGeneratorRequest() )
695 Q_PRIVATE_SLOT( d, void rotationFinished( int page ) )
696 Q_PRIVATE_SLOT( d, void fontReadingProgress( int page ) )
697 Q_PRIVATE_SLOT( d, void fontReadingGotFont( const Okular::FontInfo& font ) )
698 Q_PRIVATE_SLOT( d, void slotGeneratorConfigChanged( const QString& ) )
699 Q_PRIVATE_SLOT( d, void refreshPixmaps( int ) )
700 Q_PRIVATE_SLOT( d, void _o_configChanged() )
702 // search thread simulators
703 Q_PRIVATE_SLOT( d, void doContinueNextMatchSearch(void *pagesToNotifySet, void * match, int currentPage, int searchID, const QString & text, int caseSensitivity, bool moveViewport, const QColor & color, bool noDialogs, int donePages) )
704 Q_PRIVATE_SLOT( d, void doContinuePrevMatchSearch(void *pagesToNotifySet, void * match, int currentPage, int searchID, const QString & text, int caseSensitivity, bool moveViewport, const QColor & color, bool noDialogs, int donePages) )
705 Q_PRIVATE_SLOT( d, void doContinueAllDocumentSearch(void *pagesToNotifySet, void *pageMatchesMap, int currentPage, int searchID, const QString & text, int caseSensitivity, const QColor & color) )
706 Q_PRIVATE_SLOT( d, void doContinueGooglesDocumentSearch(void *pagesToNotifySet, void *pageMatchesMap, int currentPage, int searchID, const QStringList & words, int caseSensitivity, const QColor & color, bool matchAll) )
711 * @short A view on the document.
713 * The Viewport structure is the 'current view' over the document. Contained
714 * data is broadcasted between observers to synchronize their viewports to get
715 * the 'I scroll one view and others scroll too' views.
717 class OKULAR_EXPORT DocumentViewport
719 public:
721 * Creates a new viewport for the given page @p number.
723 DocumentViewport( int number = -1 );
726 * Creates a new viewport from the given xml @p description.
728 DocumentViewport( const QString &description );
731 * Returns the viewport as xml description.
733 QString toString() const;
736 * Returns whether the viewport is valid.
738 bool isValid() const;
741 * @internal
743 bool operator==( const DocumentViewport &other ) const;
746 * The number of the page nearest the center of the viewport.
748 int pageNumber;
751 * Describes the relative position of the viewport.
753 enum Position
755 Center = 1, ///< Relative to the center of the page.
756 TopLeft = 2 ///< Relative to the top left corner of the page.
760 * If 'rePos.enabled == true' then this structure contains the
761 * viewport center.
763 struct {
764 bool enabled;
765 double normalizedX;
766 double normalizedY;
767 Position pos;
768 } rePos;
771 * If 'autoFit.enabled == true' then the page must be autofitted in the viewport.
773 struct {
774 bool enabled;
775 bool width;
776 bool height;
777 } autoFit;
781 * @short A DOM tree containing information about the document.
783 * The DocumentInfo structure can be filled in by generators to display
784 * metadata about the currently opened file.
786 class OKULAR_EXPORT DocumentInfo : public QDomDocument
788 public:
790 * The list of predefined keys.
792 enum Key {
793 Title, ///< The title of the document
794 Subject, ///< The subject of the document
795 Description, ///< The description of the document
796 Author, ///< The author of the document
797 Creator, ///< The creator of the document (this can be different from the author)
798 Producer, ///< The producer of the document (e.g. some software)
799 Copyright, ///< The copyright of the document
800 Pages, ///< The number of pages of the document
801 CreationDate, ///< The date of creation of the document
802 ModificationDate, ///< The date of last modification of the document
803 MimeType, ///< The mime type of the document
804 Category, ///< The category of the document
805 Keywords ///< The keywords which describe the content of the document
809 * Creates a new document info.
811 DocumentInfo();
814 * Sets a value for a special key. The title should be an i18n'ed
815 * string, since it's used in the document information dialog.
817 void set( const QString &key, const QString &value,
818 const QString &title = QString() );
821 * Sets the value for a predefined key. You should use this method
822 * whenever a predefined key exists for your value.
824 void set( enum Key, const QString &value );
827 * Returns the value for a given key or an empty string when the
828 * key doesn't exist.
830 QString get( const QString &key ) const;
834 * @short A DOM tree that describes the Table of Contents.
836 * The Synopsis (TOC or Table Of Contents for friends) is represented via
837 * a dom tree where each node has an internal name (displayed in the TOC)
838 * and one or more attributes.
840 * In the tree the tag name is the 'screen' name of the entry. A tag can have
841 * attributes. Here follows the list of tag attributes with meaning:
842 * - Destination: A string description of the referred viewport
843 * - DestinationName: A 'named reference' to the viewport that must be converted
844 * using metaData( "NamedViewport", viewport_name )
845 * - ExternalFileName: A document to be opened, whose destination is specified
846 * with Destination or DestinationName
847 * - Open: a boolean saying whether its TOC branch is open or not (default: false)
848 * - URL: a URL to be open as destination; if set, no other Destination* or
849 * ExternalFileName entry is used
851 class OKULAR_EXPORT DocumentSynopsis : public QDomDocument
853 public:
855 * Creates a new document synopsis object.
857 DocumentSynopsis();
860 * Creates a new document synopsis object with the given
861 * @p document as parent node.
863 DocumentSynopsis( const QDomDocument &document );
867 * @short An embedded file into the document.
869 * This class represents a sort of interface of an embedded file in a document.
871 * Generators \b must re-implement its members to give the all the information
872 * about an embedded file, like its name, its description, the date of creation
873 * and modification, and the real data of the file.
875 class OKULAR_EXPORT EmbeddedFile
877 public:
879 * Creates a new embedded file.
881 EmbeddedFile();
884 * Destroys the embedded file.
886 virtual ~EmbeddedFile();
889 * Returns the name of the file
891 virtual QString name() const = 0;
894 * Returns the description of the file, or an empty string if not
895 * available
897 virtual QString description() const = 0;
900 * Returns the real data representing the file contents
902 virtual QByteArray data() const = 0;
905 * Returns the size (in bytes) of the file, if available, or -1 otherwise.
907 * @note this method should be a fast way to know the size of the file
908 * with no need to extract all the data from it
910 virtual int size() const = 0;
913 * Returns the modification date of the file, or an invalid date
914 * if not available
916 virtual QDateTime modificationDate() const = 0;
919 * Returns the creation date of the file, or an invalid date
920 * if not available
922 virtual QDateTime creationDate() const = 0;
927 * @short An area of a specified page
929 class OKULAR_EXPORT VisiblePageRect
931 public:
933 * Creates a new visible page rectangle.
935 * @param pageNumber The page number where the rectangle is located.
936 * @param rectangle The rectangle in normalized coordinates.
938 explicit VisiblePageRect( int pageNumber = -1, const NormalizedRect &rectangle = NormalizedRect() );
941 * The page number where the rectangle is located.
943 int pageNumber;
946 * The rectangle in normalized coordinates.
948 NormalizedRect rect;
953 Q_DECLARE_METATYPE( Okular::DocumentInfo::Key )
954 Q_DECLARE_OPERATORS_FOR_FLAGS( Okular::Document::PixmapRequestFlags )
956 #endif