compile
[kdegraphics.git] / okular / core / document.h
blob763d2c126e82d00d34ee81195ee7faa36454396b
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;
561 public Q_SLOTS:
563 * This slot is called whenever the user changes the @p rotation of
564 * the document.
566 void setRotation( int rotation );
569 * This slot is called whenever the user changes the page @p size
570 * of the document.
572 void setPageSize( const PageSize &size );
575 * Cancels the current search
577 void cancelSearch();
580 Q_SIGNALS:
582 * This signal is emitted whenever an action requests a
583 * document close operation.
585 void close();
588 * This signal is emitted whenever an action requests an
589 * application quit operation.
591 void quit();
594 * This signal is emitted whenever an action requests a
595 * find operation.
597 void linkFind();
600 * This signal is emitted whenever an action requests a
601 * goto operation.
603 void linkGoToPage();
606 * This signal is emitted whenever an action requests a
607 * start presentation operation.
609 void linkPresentation();
612 * This signal is emitted whenever an action requests an
613 * end presentation operation.
615 void linkEndPresentation();
618 * This signal is emitted whenever an action requests an
619 * open url operation for the given document @p url.
621 void openUrl( const KUrl &url );
624 * This signal is emitted whenever an error occurred.
626 * @param text The description of the error.
627 * @param duration The time in seconds the message should be shown to the user.
629 void error( const QString &text, int duration );
632 * This signal is emitted to signal a warning.
634 * @param text The description of the warning.
635 * @param duration The time in seconds the message should be shown to the user.
637 void warning( const QString &text, int duration );
640 * This signal is emitted to signal a notice.
642 * @param text The description of the notice.
643 * @param duration The time in seconds the message should be shown to the user.
645 void notice( const QString &text, int duration );
648 * Emitted when a new font is found during the reading of the fonts of
649 * the document.
651 void gotFont( const Okular::FontInfo& font );
654 * Reports the progress when reading the fonts in the document.
656 * \param page is the page that was just finished to scan for fonts
658 void fontReadingProgress( int page );
661 * Reports that the reading of the fonts in the document is finished.
663 void fontReadingEnded();
666 * Reports that the current search finished
668 void searchFinished( int id, Okular::Document::SearchStatus endStatus );
670 private:
671 /// @cond PRIVATE
672 friend class DocumentPrivate;
673 /// @endcond
674 DocumentPrivate *const d;
676 Q_DISABLE_COPY( Document )
678 Q_PRIVATE_SLOT( d, void saveDocumentInfo() const )
679 Q_PRIVATE_SLOT( d, void slotTimedMemoryCheck() )
680 Q_PRIVATE_SLOT( d, void sendGeneratorRequest() )
681 Q_PRIVATE_SLOT( d, void rotationFinished( int page ) )
682 Q_PRIVATE_SLOT( d, void fontReadingProgress( int page ) )
683 Q_PRIVATE_SLOT( d, void fontReadingGotFont( const Okular::FontInfo& font ) )
684 Q_PRIVATE_SLOT( d, void slotGeneratorConfigChanged( const QString& ) )
685 Q_PRIVATE_SLOT( d, void refreshPixmaps( int ) )
686 Q_PRIVATE_SLOT( d, void _o_configChanged() )
688 // search thread simulators
689 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) )
690 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) )
691 Q_PRIVATE_SLOT( d, void doContinueAllDocumentSearch(void *pagesToNotifySet, void *pageMatchesMap, int currentPage, int searchID, const QString & text, int caseSensitivity, const QColor & color) )
692 Q_PRIVATE_SLOT( d, void doContinueGooglesDocumentSearch(void *pagesToNotifySet, void *pageMatchesMap, int currentPage, int searchID, const QString & text, int caseSensitivity, const QColor & color, bool matchAll) )
697 * @short A view on the document.
699 * The Viewport structure is the 'current view' over the document. Contained
700 * data is broadcasted between observers to synchronize their viewports to get
701 * the 'I scroll one view and others scroll too' views.
703 class OKULAR_EXPORT DocumentViewport
705 public:
707 * Creates a new viewport for the given page @p number.
709 DocumentViewport( int number = -1 );
712 * Creates a new viewport from the given xml @p description.
714 DocumentViewport( const QString &description );
717 * Returns the viewport as xml description.
719 QString toString() const;
722 * Returns whether the viewport is valid.
724 bool isValid() const;
727 * @internal
729 bool operator==( const DocumentViewport &other ) const;
732 * The number of the page nearest the center of the viewport.
734 int pageNumber;
737 * Describes the relative position of the viewport.
739 enum Position
741 Center = 1, ///< Relative to the center of the page.
742 TopLeft = 2 ///< Relative to the top left corner of the page.
746 * If 'rePos.enabled == true' then this structure contains the
747 * viewport center.
749 struct {
750 bool enabled;
751 double normalizedX;
752 double normalizedY;
753 Position pos;
754 } rePos;
757 * If 'autoFit.enabled == true' then the page must be autofitted in the viewport.
759 struct {
760 bool enabled;
761 bool width;
762 bool height;
763 } autoFit;
767 * @short A DOM tree containing information about the document.
769 * The DocumentInfo structure can be filled in by generators to display
770 * metadata about the currently opened file.
772 class OKULAR_EXPORT DocumentInfo : public QDomDocument
774 public:
776 * The list of predefined keys.
778 enum Key {
779 Title, ///< The title of the document
780 Subject, ///< The subject of the document
781 Description, ///< The description of the document
782 Author, ///< The author of the document
783 Creator, ///< The creator of the document (this can be different from the author)
784 Producer, ///< The producer of the document (e.g. some software)
785 Copyright, ///< The copyright of the document
786 Pages, ///< The number of pages of the document
787 CreationDate, ///< The date of creation of the document
788 ModificationDate, ///< The date of last modification of the document
789 MimeType, ///< The mime type of the document
790 Category, ///< The category of the document
791 Keywords ///< The keywords which describe the content of the document
795 * Creates a new document info.
797 DocumentInfo();
800 * Sets a value for a special key. The title should be an i18n'ed
801 * string, since it's used in the document information dialog.
803 void set( const QString &key, const QString &value,
804 const QString &title = QString() );
807 * Sets the value for a predefined key. You should use this method
808 * whenever a predefined key exists for your value.
810 void set( enum Key, const QString &value );
813 * Returns the value for a given key or an empty string when the
814 * key doesn't exist.
816 QString get( const QString &key ) const;
820 * @short A DOM tree that describes the Table of Contents.
822 * The Synopsis (TOC or Table Of Contents for friends) is represented via
823 * a dom tree where each node has an internal name (displayed in the listview)
824 * and one or more attributes.
826 * In the tree the tag name is the 'screen' name of the entry. A tag can have
827 * attributes. Here follows the list of tag attributes with meaning:
828 * - Icon: An icon to be set in the Lisview for the node
829 * - Viewport: A string description of the referred viewport
830 * - ViewportName: A 'named reference' to the viewport that must be converted
831 * using metaData( "NamedViewport", *viewport_name* )
833 class OKULAR_EXPORT DocumentSynopsis : public QDomDocument
835 public:
837 * Creates a new document synopsis object.
839 DocumentSynopsis();
842 * Creates a new document synopsis object with the given
843 * @p document as parent node.
845 DocumentSynopsis( const QDomDocument &document );
849 * @short An embedded file into the document.
851 * This class represents a sort of interface of an embedded file in a document.
853 * Generators \b must re-implement its members to give the all the information
854 * about an embedded file, like its name, its description, the date of creation
855 * and modification, and the real data of the file.
857 class OKULAR_EXPORT EmbeddedFile
859 public:
861 * Creates a new embedded file.
863 EmbeddedFile();
866 * Destroys the embedded file.
868 virtual ~EmbeddedFile();
871 * Returns the name of the file
873 virtual QString name() const = 0;
876 * Returns the description of the file, or an empty string if not
877 * available
879 virtual QString description() const = 0;
882 * Returns the real data representing the file contents
884 virtual QByteArray data() const = 0;
887 * Returns the size (in bytes) of the file, if available, or -1 otherwise.
889 * @note this method should be a fast way to know the size of the file
890 * with no need to extract all the data from it
892 virtual int size() const = 0;
895 * Returns the modification date of the file, or an invalid date
896 * if not available
898 virtual QDateTime modificationDate() const = 0;
901 * Returns the creation date of the file, or an invalid date
902 * if not available
904 virtual QDateTime creationDate() const = 0;
909 * @short An area of a specified page
911 class OKULAR_EXPORT VisiblePageRect
913 public:
915 * Creates a new visible page rectangle.
917 * @param pageNumber The page number where the rectangle is located.
918 * @param rectangle The rectangle in normalized coordinates.
920 explicit VisiblePageRect( int pageNumber = -1, const NormalizedRect &rectangle = NormalizedRect() );
923 * The page number where the rectangle is located.
925 int pageNumber;
928 * The rectangle in normalized coordinates.
930 NormalizedRect rect;
935 Q_DECLARE_METATYPE( Okular::DocumentInfo::Key )
936 Q_DECLARE_OPERATORS_FOR_FLAGS( Okular::Document::PixmapRequestFlags )
938 #endif